foodsoft/app/models/bank_account.rb
Patrick Gansterer 4752a0aaa9 Add method to parse bank transactions as JSON
This implements parsing of the Account Information Service format as
defined in the Berlin Group Group NextGenPSD2 XS2A Framework, which
is widely used across various European banks.

This is a first step to replace the current bank import features with
a standardized JSON interface.
2021-02-24 14:59:55 +01:00

31 lines
1,002 B
Ruby

class BankAccount < ApplicationRecord
has_many :bank_transactions, dependent: :destroy
normalize_attributes :name, :iban, :description
validates :name, :presence => true, :uniqueness => true, :length => { :minimum => 2 }
validates :iban, :presence => true, :uniqueness => true
validates_format_of :iban, :with => /\A[A-Z]{2}[0-9]{2}[0-9A-Z]{,30}\z/
validates_numericality_of :balance, :message => I18n.t('bank_account.model.invalid_balance')
# @return [Function] Method wich can be called to import transaction from a bank or nil if unsupported
def find_connector
klass = BankAccountConnector.find iban
return klass.new self if klass
end
def assign_unlinked_transactions
count = 0
bank_transactions.without_financial_link.includes(:supplier, :user).each do |t|
if t.assign_to_ordergroup || t.assign_to_invoice
count += 1
end
end
count
end
def last_transaction_date
bank_transactions.order(date: :desc).first&.date
end
end