2019-01-13 07:05:54 +01:00
|
|
|
class FinancialTransactionClass < ApplicationRecord
|
2017-03-04 14:15:18 +01:00
|
|
|
has_many :financial_transaction_types, dependent: :destroy
|
2019-11-17 11:41:34 +01:00
|
|
|
has_many :supplier_category, dependent: :restrict_with_exception
|
2021-02-08 02:40:19 +01:00
|
|
|
has_many :financial_transactions, through: :financial_transaction_types
|
|
|
|
has_many :ordergroups, -> { distinct }, through: :financial_transactions
|
2017-03-04 14:15:18 +01:00
|
|
|
|
|
|
|
validates :name, presence: true
|
2023-05-12 13:01:12 +02:00
|
|
|
validates :name, uniqueness: true
|
2017-03-04 14:15:39 +01:00
|
|
|
|
2021-02-08 02:40:19 +01:00
|
|
|
after_save :update_balance_of_ordergroups
|
|
|
|
|
2017-03-04 14:15:39 +01:00
|
|
|
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
|
2021-02-08 02:40:19 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_balance_of_ordergroups
|
|
|
|
ordergroups.each { |og| og.update_balance! }
|
|
|
|
end
|
2017-03-04 14:15:18 +01:00
|
|
|
end
|