2025-01-07 09:41:24 -05:00
|
|
|
class Transfer < ApplicationRecord
|
2025-04-14 11:40:34 -04:00
|
|
|
belongs_to :inflow_transaction, class_name: "Transaction"
|
|
|
|
belongs_to :outflow_transaction, class_name: "Transaction"
|
2025-01-07 09:41:24 -05:00
|
|
|
|
2025-01-27 16:56:46 -05:00
|
|
|
enum :status, { pending: "pending", confirmed: "confirmed" }
|
|
|
|
|
|
|
|
validates :inflow_transaction_id, uniqueness: true
|
|
|
|
validates :outflow_transaction_id, uniqueness: true
|
2025-01-07 09:41:24 -05:00
|
|
|
|
|
|
|
validate :transfer_has_different_accounts
|
|
|
|
validate :transfer_has_opposite_amounts
|
|
|
|
validate :transfer_within_date_range
|
2025-01-16 17:56:42 -05:00
|
|
|
validate :transfer_has_same_family
|
2025-01-07 09:41:24 -05:00
|
|
|
|
|
|
|
class << self
|
2025-06-20 13:31:58 -04:00
|
|
|
def kind_for_account(account)
|
|
|
|
if account.loan?
|
|
|
|
"loan_payment"
|
|
|
|
elsif account.liability?
|
|
|
|
"cc_payment"
|
|
|
|
else
|
|
|
|
"funds_movement"
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
end
|
2025-01-27 16:56:46 -05:00
|
|
|
end
|
2025-01-07 09:41:24 -05:00
|
|
|
|
2025-01-27 16:56:46 -05:00
|
|
|
def reject!
|
|
|
|
Transfer.transaction do
|
|
|
|
RejectedTransfer.find_or_create_by!(inflow_transaction_id: inflow_transaction_id, outflow_transaction_id: outflow_transaction_id)
|
|
|
|
destroy!
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-06-20 13:31:58 -04:00
|
|
|
# Once transfer is destroyed, we need to mark the denormalized kind fields on the transactions
|
|
|
|
def destroy!
|
|
|
|
Transfer.transaction do
|
|
|
|
inflow_transaction.update!(kind: "standard")
|
|
|
|
outflow_transaction.update!(kind: "standard")
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-01-27 16:56:46 -05:00
|
|
|
def confirm!
|
|
|
|
update!(status: "confirmed")
|
|
|
|
end
|
|
|
|
|
2025-06-20 13:31:58 -04:00
|
|
|
def date
|
|
|
|
inflow_transaction.entry.date
|
|
|
|
end
|
|
|
|
|
2025-01-07 09:41:24 -05:00
|
|
|
def sync_account_later
|
2025-04-30 14:20:09 -05:00
|
|
|
inflow_transaction&.entry&.sync_account_later
|
|
|
|
outflow_transaction&.entry&.sync_account_later
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_account
|
2025-04-30 14:20:09 -05:00
|
|
|
inflow_transaction&.entry&.account
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def from_account
|
2025-04-30 14:20:09 -05:00
|
|
|
outflow_transaction&.entry&.account
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def amount_abs
|
2025-04-30 14:20:09 -05:00
|
|
|
inflow_transaction&.entry&.amount_money&.abs
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
2025-04-30 14:20:09 -05:00
|
|
|
acc = to_account
|
2025-01-07 09:41:24 -05:00
|
|
|
if payment?
|
2025-04-30 14:20:09 -05:00
|
|
|
acc ? "Payment to #{acc.name}" : "Payment"
|
2025-01-07 09:41:24 -05:00
|
|
|
else
|
2025-04-30 14:20:09 -05:00
|
|
|
acc ? "Transfer to #{acc.name}" : "Transfer"
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def payment?
|
2025-04-30 14:20:09 -05:00
|
|
|
to_account&.liability?
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
|
2025-06-20 13:31:58 -04:00
|
|
|
def loan_payment?
|
|
|
|
outflow_transaction&.kind == "loan_payment"
|
|
|
|
end
|
|
|
|
|
|
|
|
def liability_payment?
|
|
|
|
outflow_transaction&.kind == "cc_payment"
|
|
|
|
end
|
|
|
|
|
|
|
|
def regular_transfer?
|
|
|
|
outflow_transaction&.kind == "funds_movement"
|
|
|
|
end
|
|
|
|
|
|
|
|
def transfer_type
|
|
|
|
return "loan_payment" if loan_payment?
|
|
|
|
return "liability_payment" if liability_payment?
|
|
|
|
"transfer"
|
|
|
|
end
|
|
|
|
|
2025-01-16 14:36:37 -05:00
|
|
|
def categorizable?
|
2025-04-30 14:20:09 -05:00
|
|
|
to_account&.accountable_type == "Loan"
|
2025-01-16 14:36:37 -05:00
|
|
|
end
|
|
|
|
|
2025-01-07 09:41:24 -05:00
|
|
|
private
|
|
|
|
def transfer_has_different_accounts
|
2025-04-30 14:20:09 -05:00
|
|
|
return unless inflow_transaction&.entry && outflow_transaction&.entry
|
|
|
|
errors.add(:base, "Must be from different accounts") if to_account == from_account
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
|
2025-01-16 17:56:42 -05:00
|
|
|
def transfer_has_same_family
|
2025-04-30 14:20:09 -05:00
|
|
|
return unless inflow_transaction&.entry && outflow_transaction&.entry
|
|
|
|
errors.add(:base, "Must be from same family") unless to_account&.family == from_account&.family
|
2025-01-16 17:56:42 -05:00
|
|
|
end
|
|
|
|
|
2025-01-07 09:41:24 -05:00
|
|
|
def transfer_has_opposite_amounts
|
2025-04-30 14:20:09 -05:00
|
|
|
return unless inflow_transaction&.entry && outflow_transaction&.entry
|
2025-01-07 09:41:24 -05:00
|
|
|
|
2025-04-30 14:20:09 -05:00
|
|
|
inflow_entry = inflow_transaction.entry
|
|
|
|
outflow_entry = outflow_transaction.entry
|
2025-01-07 09:41:24 -05:00
|
|
|
|
2025-04-30 14:20:09 -05:00
|
|
|
inflow_amount = inflow_entry.amount
|
|
|
|
outflow_amount = outflow_entry.amount
|
|
|
|
|
|
|
|
if inflow_entry.currency == outflow_entry.currency
|
2025-01-07 09:41:24 -05:00
|
|
|
# For same currency, amounts must be exactly opposite
|
2025-04-30 14:20:09 -05:00
|
|
|
errors.add(:base, "Must have opposite amounts") if inflow_amount + outflow_amount != 0
|
2025-01-07 09:41:24 -05:00
|
|
|
else
|
|
|
|
# For different currencies, just check the signs are opposite
|
2025-04-30 14:20:09 -05:00
|
|
|
errors.add(:base, "Must have opposite amounts") unless inflow_amount.negative? && outflow_amount.positive?
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def transfer_within_date_range
|
2025-04-30 14:20:09 -05:00
|
|
|
return unless inflow_transaction&.entry && outflow_transaction&.entry
|
2025-01-07 09:41:24 -05:00
|
|
|
|
|
|
|
date_diff = (inflow_transaction.entry.date - outflow_transaction.entry.date).abs
|
2025-04-30 14:20:09 -05:00
|
|
|
errors.add(:base, "Must be within 4 days") if date_diff > 4
|
2025-01-07 09:41:24 -05:00
|
|
|
end
|
|
|
|
end
|