Add option to ignore financial transaction when calculating the balance

This commit is contained in:
Patrick Gansterer 2021-02-08 02:40:19 +01:00
parent a30d0d4453
commit 7d5155bef6
6 changed files with 68 additions and 16 deletions

View file

@ -1,10 +1,14 @@
class FinancialTransactionClass < ApplicationRecord
has_many :financial_transaction_types, dependent: :destroy
has_many :supplier_category, dependent: :restrict_with_exception
has_many :financial_transactions, through: :financial_transaction_types
has_many :ordergroups, -> { distinct }, through: :financial_transactions
validates :name, presence: true
validates_uniqueness_of :name
after_save :update_balance_of_ordergroups
scope :sorted, -> { order(name: :asc) }
def self.has_multiple_classes
@ -18,4 +22,10 @@ class FinancialTransactionClass < ApplicationRecord
I18n.t('activerecord.attributes.financial_transaction.amount')
end
end
private
def update_balance_of_ordergroups
ordergroups.each { |og| og.update_balance! }
end
end

View file

@ -2,6 +2,7 @@ class FinancialTransactionType < ApplicationRecord
belongs_to :financial_transaction_class
belongs_to :bank_account, optional: true
has_many :financial_transactions, dependent: :restrict_with_exception
has_many :ordergroups, -> { distinct }, through: :financial_transactions
validates :name, presence: true
validates_uniqueness_of :name
@ -9,6 +10,7 @@ class FinancialTransactionType < ApplicationRecord
validates_format_of :name_short, :with => /\A[A-Za-z]*\z/
validates :financial_transaction_class, presence: true
after_save :update_balance_of_ordergroups
before_destroy :restrict_deleting_last_financial_transaction_type
scope :with_name_short, -> { where.not(name_short: [nil, '']) }
@ -27,4 +29,10 @@ class FinancialTransactionType < ApplicationRecord
def restrict_deleting_last_financial_transaction_type
raise I18n.t('model.financial_transaction_type.no_delete_last') if FinancialTransactionType.count == 1
end
private
def update_balance_of_ordergroups
ordergroups.each { |og| og.update_balance! }
end
end

View file

@ -103,7 +103,11 @@ class Ordergroup < Group
end
def update_balance!
update_attribute :account_balance, financial_transactions.sum('amount')
new_account_balance = financial_transactions
.joins(financial_transaction_type: [:financial_transaction_class])
.where({ financial_transaction_classes: { ignore_for_account_balance: false} })
.sum(:amount)
update_attribute :account_balance, new_account_balance
end
def avg_jobs_per_euro

View file

@ -4,6 +4,7 @@
%h3= @financial_transaction_class.new_record? ? t('.title_new') : t('.title_edit')
.modal-body
= f.input :name
= f.input :ignore_for_account_balance
.modal-footer
= link_to t('ui.close'), '#', class: 'btn', data: {dismiss: 'modal'}
= f.submit class: 'btn btn-primary'