2009-01-06 11:49:19 +01:00
|
|
|
# financial transactions are the foodcoop internal financial transactions
|
2009-01-14 12:46:01 +01:00
|
|
|
# only ordergroups have an account balance and are happy to transfer money
|
2009-01-06 11:49:19 +01:00
|
|
|
class FinancialTransaction < ActiveRecord::Base
|
2009-01-14 12:46:01 +01:00
|
|
|
belongs_to :ordergroup
|
2009-01-06 11:49:19 +01:00
|
|
|
belongs_to :user
|
2017-10-13 14:36:56 +02:00
|
|
|
belongs_to :financial_link
|
2017-03-04 14:15:18 +01:00
|
|
|
belongs_to :financial_transaction_type
|
2014-09-22 11:21:12 +02:00
|
|
|
|
2011-06-09 21:35:05 +02:00
|
|
|
validates_presence_of :amount, :note, :user_id, :ordergroup_id
|
2014-09-22 11:21:12 +02:00
|
|
|
validates_numericality_of :amount, greater_then: -100_000,
|
|
|
|
less_than: 100_000
|
2009-01-10 21:28:22 +01:00
|
|
|
|
2018-10-11 22:16:50 +02:00
|
|
|
scope :without_financial_link, -> { where(financial_link: nil) }
|
|
|
|
|
2011-06-09 21:35:05 +02:00
|
|
|
localize_input_of :amount
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2017-03-04 14:15:18 +01:00
|
|
|
after_initialize do
|
|
|
|
initialize_financial_transaction_type
|
|
|
|
end
|
|
|
|
|
2011-06-09 21:35:05 +02:00
|
|
|
# Use this save method instead of simple save and after callback
|
|
|
|
def add_transaction!
|
2017-03-04 14:15:18 +01:00
|
|
|
ordergroup.add_financial_transaction! amount, note, user, financial_transaction_type
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def initialize_financial_transaction_type
|
|
|
|
self.financial_transaction_type ||= FinancialTransactionType.default
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|