Files
sure/app/models/transfer.rb
Brendon Scheiber 0c126b1674 feat(i18n): extract hardcoded English strings to locale files (#1806)
* Extract hardcoded strings to i18n

Replace numerous hardcoded English strings with I18n lookups (t / I18n.t) across controllers, views, helpers, and components, and convert model validation error messages to symbol keys. Added multiple locale files under config/locales for models and views. This centralizes user-facing notices/alerts, UI text, import/validation messages, and prepares the app for localization and easier translation maintenance.

* Update en.yml

* Update preview-cleanup.yml

* Revert "Update preview-cleanup.yml"

This reverts commit 1ba6d3c34c.

* test: align i18n assertions with translated messages

* Standardize balance error key and tweak locales

Replace SophtronAccount's :requires_balance error key with :no_balance and update related locale strings for sophtron, plaid, and simplefin accounts to use the new key and clearer copy. Also switch the QIF upload redirect notice to use a relative translation key (t('.qif_uploaded')), remove an unused SSO providers help line, and fix a trailing-newline/whitespace issue in the subscriptions locale. These changes standardize validation keys and improve translation consistency and messaging.

---------

Co-authored-by: KiloClaw <kiloclaw@openclaw.ai>
2026-05-17 09:52:49 +02:00

144 lines
3.8 KiB
Ruby

class Transfer < ApplicationRecord
belongs_to :inflow_transaction, class_name: "Transaction"
belongs_to :outflow_transaction, class_name: "Transaction"
enum :status, { pending: "pending", confirmed: "confirmed" }
validates :inflow_transaction_id, uniqueness: true
validates :outflow_transaction_id, uniqueness: true
validate :transfer_has_different_accounts
validate :transfer_has_opposite_amounts
validate :transfer_within_date_range
validate :transfer_has_same_family
class << self
def kind_for_account(account)
if account.loan?
"loan_payment"
elsif account.credit_card?
"cc_payment"
elsif account.investment? || account.crypto?
"investment_contribution"
elsif account.liability?
"cc_payment"
else
"funds_movement"
end
end
end
def reject!
Transfer.transaction do
RejectedTransfer.find_or_create_by!(inflow_transaction_id: inflow_transaction_id, outflow_transaction_id: outflow_transaction_id)
destroy!
end
end
# 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
def confirm!
update!(status: "confirmed")
end
def date
inflow_transaction.entry.date
end
def sync_account_later
inflow_transaction&.entry&.sync_account_later
outflow_transaction&.entry&.sync_account_later
end
def to_account
inflow_transaction&.entry&.account
end
def from_account
outflow_transaction&.entry&.account
end
def amount_abs
inflow_transaction&.entry&.amount_money&.abs
end
def name
acc = to_account
if payment?
acc ? "Payment to #{acc.name}" : "Payment"
else
acc ? "Transfer to #{acc.name}" : "Transfer"
end
end
def payment?
to_account&.liability?
end
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
def categorizable?
to_account&.accountable_type == "Loan"
end
private
def transfer_has_different_accounts
return unless inflow_transaction&.entry && outflow_transaction&.entry
errors.add(:base, :different_accounts) if to_account == from_account
end
def transfer_has_same_family
return unless inflow_transaction&.entry && outflow_transaction&.entry
errors.add(:base, :same_family) unless to_account&.family == from_account&.family
end
def transfer_has_opposite_amounts
return unless inflow_transaction&.entry && outflow_transaction&.entry
inflow_entry = inflow_transaction.entry
outflow_entry = outflow_transaction.entry
inflow_amount = inflow_entry.amount
outflow_amount = outflow_entry.amount
if inflow_entry.currency == outflow_entry.currency
# For same currency, amounts must be exactly opposite
errors.add(:base, :opposite_amounts) if inflow_amount + outflow_amount != 0
else
# For different currencies, just check the signs are opposite
errors.add(:base, :opposite_amounts) unless inflow_amount.negative? && outflow_amount.positive?
end
end
def transfer_within_date_range
return unless inflow_transaction&.entry && outflow_transaction&.entry
date_diff = (inflow_transaction.entry.date - outflow_transaction.entry.date).abs
max_days = status == "confirmed" ? 30 : 4
errors.add(:base, :within_days, count: max_days) if date_diff > max_days
end
end