2019-01-13 07:05:54 +01:00
|
|
|
class BankAccount < ApplicationRecord
|
2018-09-14 19:33:27 +02:00
|
|
|
has_many :bank_transactions, dependent: :destroy
|
2022-02-18 12:38:58 +01:00
|
|
|
has_many :supplier_categories, dependent: :nullify
|
2021-03-05 12:43:40 +01:00
|
|
|
belongs_to :bank_gateway, optional: true
|
2018-09-14 19:33:27 +02:00
|
|
|
|
|
|
|
normalize_attributes :name, :iban, :description
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
validates :name, presence: true, uniqueness: true, length: { minimum: 2 }
|
|
|
|
validates :iban, presence: true, uniqueness: true
|
|
|
|
validates :iban, format: { with: /\A[A-Z]{2}[0-9]{2}[0-9A-Z]{,30}\z/ }
|
|
|
|
validates :balance, numericality: { message: I18n.t('bank_account.model.invalid_balance') }
|
2018-09-14 19:33:27 +02:00
|
|
|
|
2018-10-11 23:26:12 +02:00
|
|
|
# @return [Function] Method wich can be called to import transaction from a bank or nil if unsupported
|
2020-02-24 14:22:58 +01:00
|
|
|
def find_connector
|
|
|
|
klass = BankAccountConnector.find iban
|
|
|
|
return klass.new self if klass
|
2018-10-11 23:26:12 +02:00
|
|
|
end
|
2017-01-26 13:27:30 +01:00
|
|
|
|
|
|
|
def assign_unlinked_transactions
|
|
|
|
count = 0
|
2023-05-12 13:01:12 +02:00
|
|
|
bank_transactions.without_financial_link.includes(:supplier, :user).find_each do |t|
|
|
|
|
count += 1 if t.assign_to_ordergroup || t.assign_to_invoice
|
2017-01-26 13:27:30 +01:00
|
|
|
end
|
|
|
|
count
|
|
|
|
end
|
2021-02-05 12:19:05 +01:00
|
|
|
|
|
|
|
def last_transaction_date
|
|
|
|
bank_transactions.order(date: :desc).first&.date
|
|
|
|
end
|
2018-09-14 19:33:27 +02:00
|
|
|
end
|