Show the financial transaction type if there are more than one type #367
This commit is contained in:
parent
e7657b987f
commit
924f346b4c
9 changed files with 98 additions and 15 deletions
|
@ -1,19 +1,17 @@
|
||||||
class Finance::OrdergroupsController < Finance::BaseController
|
class Finance::OrdergroupsController < Finance::BaseController
|
||||||
|
|
||||||
def index
|
def index
|
||||||
if params["sort"]
|
m = /^(?<col>name|sum_of_class_\d+)(?<reverse>_reverse)?$/.match params["sort"]
|
||||||
sort = case params["sort"]
|
if m
|
||||||
when "name" then "name"
|
sort = m[:col]
|
||||||
when "account_balance" then "account_balance"
|
sort += ' DESC' if m[:reverse]
|
||||||
when "name_reverse" then "name DESC"
|
|
||||||
when "account_balance_reverse" then "account_balance DESC"
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
sort = "name"
|
sort = "name"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ordergroups = Ordergroup.undeleted.order(sort)
|
@ordergroups = Ordergroup.undeleted.order(sort)
|
||||||
@ordergroups = @ordergroups.where('name LIKE ?', "%#{params[:query]}%") unless params[:query].nil?
|
@ordergroups = @ordergroups.include_transaction_class_sum
|
||||||
|
@ordergroups = @ordergroups.where('groups.name LIKE ?', "%#{params[:query]}%") unless params[:query].nil?
|
||||||
|
|
||||||
@ordergroups = @ordergroups.page(params[:page]).per(@per_page)
|
@ordergroups = @ordergroups.page(params[:page]).per(@per_page)
|
||||||
end
|
end
|
||||||
|
|
|
@ -30,6 +30,8 @@ class HomeController < ApplicationController
|
||||||
|
|
||||||
unless @ordergroup.nil?
|
unless @ordergroup.nil?
|
||||||
|
|
||||||
|
@ordergroup = Ordergroup.include_transaction_class_sum.find(@ordergroup)
|
||||||
|
|
||||||
if params['sort']
|
if params['sort']
|
||||||
sort = case params['sort']
|
sort = case params['sort']
|
||||||
when "date" then "created_on"
|
when "date" then "created_on"
|
||||||
|
|
|
@ -3,4 +3,18 @@ class FinancialTransactionClass < ActiveRecord::Base
|
||||||
|
|
||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
validates_uniqueness_of :name
|
validates_uniqueness_of :name
|
||||||
|
|
||||||
|
scope :sorted, -> { order(name: :asc) }
|
||||||
|
|
||||||
|
def self.has_multiple_classes
|
||||||
|
FinancialTransactionClass.count > 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def display
|
||||||
|
if FinancialTransactionClass.has_multiple_classes
|
||||||
|
name
|
||||||
|
else
|
||||||
|
I18n.t('activerecord.attributes.financial_transaction.amount')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,6 +27,18 @@ class Ordergroup < Group
|
||||||
User.natural_order.all.reject { |u| (users.include?(u) || u.ordergroup) }
|
User.natural_order.all.reject { |u| (users.include?(u) || u.ordergroup) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.include_transaction_class_sum
|
||||||
|
columns = ['groups.*']
|
||||||
|
FinancialTransactionClass.all.each do |c|
|
||||||
|
columns << "sum(CASE financial_transaction_types.financial_transaction_class_id WHEN #{c.id} THEN financial_transactions.amount ELSE 0 END) AS sum_of_class_#{c.id}"
|
||||||
|
end
|
||||||
|
|
||||||
|
select(columns.join(', '))
|
||||||
|
.joins('LEFT JOIN financial_transactions ON groups.id = financial_transactions.ordergroup_id')
|
||||||
|
.joins('LEFT JOIN financial_transaction_types ON financial_transaction_types.id = financial_transactions.financial_transaction_type_id')
|
||||||
|
.group('groups.id')
|
||||||
|
end
|
||||||
|
|
||||||
def last_user_activity
|
def last_user_activity
|
||||||
last_active_user = users.order('users.last_activity DESC').first
|
last_active_user = users.order('users.last_activity DESC').first
|
||||||
if last_active_user
|
if last_active_user
|
||||||
|
|
|
@ -16,8 +16,12 @@
|
||||||
- if with_ordergroup
|
- if with_ordergroup
|
||||||
%th= heading_helper FinancialTransaction, :ordergroup
|
%th= heading_helper FinancialTransaction, :ordergroup
|
||||||
%th= heading_helper FinancialTransaction, :user
|
%th= heading_helper FinancialTransaction, :user
|
||||||
|
- if FinancialTransactionType.has_multiple_types
|
||||||
|
%th= heading_helper FinancialTransaction, :financial_transaction_type
|
||||||
%th= sort_link_helper heading_helper(FinancialTransaction, :note), "note"
|
%th= sort_link_helper heading_helper(FinancialTransaction, :note), "note"
|
||||||
%th= sort_link_helper heading_helper(FinancialTransaction, :amount), "amount"
|
- FinancialTransactionClass.sorted.each do |c|
|
||||||
|
%th
|
||||||
|
= sort_link_helper c.display, "amount"
|
||||||
%tbody
|
%tbody
|
||||||
- @financial_transactions.each do |t|
|
- @financial_transactions.each do |t|
|
||||||
%tr
|
%tr
|
||||||
|
@ -29,5 +33,10 @@
|
||||||
- if with_ordergroup
|
- if with_ordergroup
|
||||||
%td= h link_to t.ordergroup.name, finance_ordergroup_transactions_path(t.ordergroup)
|
%td= h link_to t.ordergroup.name, finance_ordergroup_transactions_path(t.ordergroup)
|
||||||
%td= h show_user(t.user)
|
%td= h show_user(t.user)
|
||||||
|
- if FinancialTransactionType.has_multiple_types
|
||||||
|
%td= h t.financial_transaction_type.name
|
||||||
%td= h t.note
|
%td= h t.note
|
||||||
%td.currency{:style => "color:#{t.amount < 0 ? 'red' : 'black'}; width:5em"}= number_to_currency(t.amount)
|
- FinancialTransactionClass.sorted.each do |c|
|
||||||
|
%td.currency{:style => "color:#{t.amount < 0 ? 'red' : 'black'}; width:5em"}
|
||||||
|
- if t.financial_transaction_type.financial_transaction_class == c
|
||||||
|
= number_to_currency(t.amount)
|
||||||
|
|
|
@ -6,15 +6,19 @@
|
||||||
%tr
|
%tr
|
||||||
%th= sort_link_helper heading_helper(Ordergroup, :name), "name", :per_page => @per_page
|
%th= sort_link_helper heading_helper(Ordergroup, :name), "name", :per_page => @per_page
|
||||||
%th= heading_helper Ordergroup, :contact
|
%th= heading_helper Ordergroup, :contact
|
||||||
%th.numeric= sort_link_helper heading_helper(Ordergroup, :account_balance), "account_balance", :per_page => @per_page
|
- FinancialTransactionClass.sorted.each do |c|
|
||||||
|
- name = FinancialTransactionClass.has_multiple_classes ? c.display : heading_helper(Ordergroup, :account_balance)
|
||||||
|
%th.numeric= sort_link_helper name, "sum_of_class_#{c.id}"
|
||||||
%th
|
%th
|
||||||
%tbody
|
%tbody
|
||||||
- for ordergroup in @ordergroups
|
- for ordergroup in @ordergroups
|
||||||
%tr
|
%tr
|
||||||
%td= ordergroup.name
|
%td= ordergroup.name
|
||||||
%td= ordergroup.contact
|
%td= ordergroup.contact
|
||||||
%td.numeric= number_to_currency(ordergroup.account_balance)
|
- FinancialTransactionClass.sorted.each do |c|
|
||||||
|
- amount = ordergroup["sum_of_class_#{c.id}"]
|
||||||
|
%td.numeric{:style => "color:#{amount < 0 ? 'red' : 'black'}"}
|
||||||
|
= number_to_currency amount
|
||||||
%td
|
%td
|
||||||
= link_to t('.new_transaction'), new_finance_ordergroup_transaction_path(ordergroup), class: 'btn btn-mini'
|
= link_to t('.new_transaction'), new_finance_ordergroup_transaction_path(ordergroup), class: 'btn btn-mini'
|
||||||
= link_to t('.account_statement'), finance_ordergroup_transactions_path(ordergroup), class: 'btn btn-mini'
|
= link_to t('.account_statement'), finance_ordergroup_transactions_path(ordergroup), class: 'btn btn-mini'
|
||||||
|
|
||||||
|
|
|
@ -67,15 +67,24 @@
|
||||||
%tr
|
%tr
|
||||||
%th= heading_helper FinancialTransaction, :created_on
|
%th= heading_helper FinancialTransaction, :created_on
|
||||||
%th= heading_helper FinancialTransaction, :user
|
%th= heading_helper FinancialTransaction, :user
|
||||||
|
- if FinancialTransactionType.has_multiple_types
|
||||||
|
%th= heading_helper FinancialTransaction, :financial_transaction_type
|
||||||
%th= heading_helper FinancialTransaction, :note
|
%th= heading_helper FinancialTransaction, :note
|
||||||
%th= heading_helper FinancialTransaction, :amount
|
- FinancialTransactionClass.sorted.each do |fc|
|
||||||
|
%th
|
||||||
|
= fc.display
|
||||||
- for ft in current_user.ordergroup.financial_transactions.limit(5).order('created_on DESC')
|
- for ft in current_user.ordergroup.financial_transactions.limit(5).order('created_on DESC')
|
||||||
%tr
|
%tr
|
||||||
%td= format_time(ft.created_on)
|
%td= format_time(ft.created_on)
|
||||||
%td= h(show_user(ft.user))
|
%td= h(show_user(ft.user))
|
||||||
|
- if FinancialTransactionType.has_multiple_types
|
||||||
|
%td= h(ft.financial_transaction_type.name)
|
||||||
%td= h(ft.note)
|
%td= h(ft.note)
|
||||||
- color = ft.amount < 0 ? 'red' : 'black'
|
- color = ft.amount < 0 ? 'red' : 'black'
|
||||||
%td{:style => "color:#{color}; width:5em", :class => "currency"}= number_to_currency(ft.amount)
|
- FinancialTransactionClass.sorted.each do |fc|
|
||||||
|
%td{:style => "color:#{color}; width:5em", :class => "currency"}
|
||||||
|
- if ft.financial_transaction_type.financial_transaction_class == fc
|
||||||
|
= number_to_currency(ft.amount)
|
||||||
|
|
||||||
-# placeholder deface to add content using erb[silent]:contains()
|
-# placeholder deface to add content using erb[silent]:contains()
|
||||||
- '<dashboard_bottom_mark>'
|
- '<dashboard_bottom_mark>'
|
||||||
|
|
|
@ -13,6 +13,11 @@
|
||||||
%p
|
%p
|
||||||
%b= heading_helper(Ordergroup, :available_funds) + ':'
|
%b= heading_helper(Ordergroup, :available_funds) + ':'
|
||||||
= number_to_currency(@ordergroup.get_available_funds())
|
= number_to_currency(@ordergroup.get_available_funds())
|
||||||
|
- if FinancialTransactionClass.has_multiple_classes
|
||||||
|
- FinancialTransactionClass.sorted.each do |c|
|
||||||
|
%p
|
||||||
|
%b= c.display + ':'
|
||||||
|
= number_to_currency(@ordergroup["sum_of_class_#{c.id}"])
|
||||||
%p
|
%p
|
||||||
%b= heading_helper(Ordergroup, :user_tokens) + ':'
|
%b= heading_helper(Ordergroup, :user_tokens) + ':'
|
||||||
= @ordergroup.memberships.map{|m| show_user m.user}.join(', ')
|
= @ordergroup.memberships.map{|m| show_user m.user}.join(', ')
|
||||||
|
|
30
spec/models/ordergroup_spec.rb
Normal file
30
spec/models/ordergroup_spec.rb
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
require_relative '../spec_helper'
|
||||||
|
|
||||||
|
describe Ordergroup do
|
||||||
|
let(:ftc1) { create :financial_transaction_class }
|
||||||
|
let(:ftc2) { create :financial_transaction_class }
|
||||||
|
let(:ftt1) { create :financial_transaction_type, financial_transaction_class: ftc1 }
|
||||||
|
let(:ftt2) { create :financial_transaction_type, financial_transaction_class: ftc2 }
|
||||||
|
let(:ftt3) { create :financial_transaction_type, financial_transaction_class: ftc2 }
|
||||||
|
let(:user) { create :user, groups:[create(:ordergroup)] }
|
||||||
|
|
||||||
|
it 'has correct FinancialTransactionClass sums' do
|
||||||
|
og = user.ordergroup
|
||||||
|
og.add_financial_transaction!(-1, '-1', user, ftt1)
|
||||||
|
og.add_financial_transaction!(2, '2', user, ftt1)
|
||||||
|
og.add_financial_transaction!(3, '3', user, ftt1)
|
||||||
|
|
||||||
|
og.add_financial_transaction!(-10, '-10', user, ftt2)
|
||||||
|
og.add_financial_transaction!(20, '20', user, ftt2)
|
||||||
|
og.add_financial_transaction!(30, '30', user, ftt2)
|
||||||
|
|
||||||
|
og.add_financial_transaction!(-100, '-100', user, ftt3)
|
||||||
|
og.add_financial_transaction!(200, '200', user, ftt3)
|
||||||
|
og.add_financial_transaction!(300, '300', user, ftt3)
|
||||||
|
|
||||||
|
result = Ordergroup.include_transaction_class_sum.where(id: og).first
|
||||||
|
expect(result["sum_of_class_#{ftc1.id}"]).to eq 4
|
||||||
|
expect(result["sum_of_class_#{ftc2.id}"]).to eq 440
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in a new issue