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
|
2019-01-13 07:05:54 +01:00
|
|
|
class FinancialTransaction < ApplicationRecord
|
2022-02-03 08:37:38 +01:00
|
|
|
include LocalizeInput
|
|
|
|
|
2020-08-01 02:49:15 +02:00
|
|
|
belongs_to :ordergroup, optional: true
|
2009-01-06 11:49:19 +01:00
|
|
|
belongs_to :user
|
2020-08-01 02:49:15 +02:00
|
|
|
belongs_to :financial_link, optional: true
|
2017-03-04 14:15:18 +01:00
|
|
|
belongs_to :financial_transaction_type
|
2020-08-01 02:49:15 +02:00
|
|
|
belongs_to :group_order, optional: true
|
2023-05-12 13:01:12 +02:00
|
|
|
belongs_to :reverts, optional: true, class_name: 'FinancialTransaction'
|
2019-11-01 19:30:23 +01:00
|
|
|
has_one :reverted_by, class_name: 'FinancialTransaction', foreign_key: 'reverts_id'
|
2014-09-22 11:21:12 +02:00
|
|
|
|
2023-10-02 23:00:24 +02:00
|
|
|
validates :note, :user_id, presence: true
|
2023-05-12 13:01:12 +02:00
|
|
|
validates :amount, numericality: { greater_then: -100_000,
|
2023-10-02 23:00:24 +02:00
|
|
|
less_than: 100_000 },
|
|
|
|
allow_nil: -> { payment_amount.present? }
|
|
|
|
validates :payment_amount, :payment_fee, allow_nil: true, numericality: { greater_then: 0, less_than: 100_000 }
|
2009-01-10 21:28:22 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
scope :visible, lambda {
|
|
|
|
joins('LEFT JOIN financial_transactions r ON financial_transactions.id = r.reverts_id').where('r.id IS NULL').where(reverts: nil)
|
|
|
|
}
|
2018-10-11 22:16:50 +02:00
|
|
|
scope :without_financial_link, -> { where(financial_link: nil) }
|
2019-11-01 14:07:23 +01:00
|
|
|
scope :with_ordergroup, -> { where.not(ordergroup: nil) }
|
2018-10-11 22:16:50 +02:00
|
|
|
|
2011-06-09 21:35:05 +02:00
|
|
|
localize_input_of :amount
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2023-10-02 23:00:24 +02:00
|
|
|
after_save :update_ordergroup_balance
|
|
|
|
|
2017-03-04 14:15:18 +01:00
|
|
|
after_initialize do
|
|
|
|
initialize_financial_transaction_type
|
|
|
|
end
|
|
|
|
|
2020-07-25 16:18:59 +02:00
|
|
|
# @todo remove alias (and rename created_on to created_at below) after #575
|
|
|
|
ransack_alias :created_at, :created_on
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
def self.ransackable_attributes(_auth_object = nil)
|
|
|
|
%w[id amount note created_on user_id]
|
2020-07-25 16:18:59 +02:00
|
|
|
end
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
def self.ransackable_associations(_auth_object = nil)
|
|
|
|
%w[] # none, and certainly not user until we've secured that more
|
2020-07-25 16:18:59 +02:00
|
|
|
end
|
|
|
|
|
2019-11-01 19:30:23 +01:00
|
|
|
def revert!(user)
|
|
|
|
transaction do
|
2020-03-06 16:10:05 +01:00
|
|
|
update_attribute :financial_link, FinancialLink.new
|
2019-11-01 19:30:23 +01:00
|
|
|
rt = dup
|
2023-10-02 23:00:24 +02:00
|
|
|
rt.amount = rt.amount ? -rt.amount : nil
|
2019-11-01 19:30:23 +01:00
|
|
|
rt.reverts = self
|
|
|
|
rt.user = user
|
|
|
|
rt.save!
|
2019-11-01 17:25:20 +01:00
|
|
|
ordergroup.update_balance! if ordergroup
|
2019-11-01 19:30:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def hidden?
|
|
|
|
reverts.present? || reverted_by.present?
|
|
|
|
end
|
|
|
|
|
2019-11-01 14:07:23 +01:00
|
|
|
def ordergroup_name
|
|
|
|
ordergroup ? ordergroup.name : I18n.t('model.financial_transaction.foodcoop_name')
|
|
|
|
end
|
|
|
|
|
2020-07-25 16:18:59 +02:00
|
|
|
# @todo rename in model, see #575
|
|
|
|
def created_at
|
|
|
|
created_on
|
|
|
|
end
|
|
|
|
|
2017-03-04 14:15:18 +01:00
|
|
|
protected
|
|
|
|
|
|
|
|
def initialize_financial_transaction_type
|
|
|
|
self.financial_transaction_type ||= FinancialTransactionType.default
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
2023-10-02 23:00:24 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_ordergroup_balance
|
|
|
|
# @todo Make sure this transaction and the ordergroup update is in one database transaction.
|
|
|
|
# It may be possible to use an around filter if needed.
|
|
|
|
ordergroup.update_balance!
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|