2019-01-13 07:05:54 +01:00
|
|
|
class BankTransaction < ApplicationRecord
|
2022-02-03 08:37:38 +01:00
|
|
|
include LocalizeInput
|
2018-09-14 19:33:27 +02:00
|
|
|
# @!attribute external_id
|
|
|
|
# @return [String] Unique Identifier of the transaction within the bank account.
|
|
|
|
# @!attribute date
|
|
|
|
# @return [Date] Date of the transaction.
|
|
|
|
# @!attribute amount
|
|
|
|
# @return [Number] Amount credited.
|
|
|
|
# @!attribute iban
|
|
|
|
# @return [String] Internation Bank Account Number of the sending/receiving account.
|
|
|
|
# @!attribute reference
|
|
|
|
# @return [String] 140 character long reference field as defined by SEPA.
|
|
|
|
# @!attribute text
|
|
|
|
# @return [String] Short description of the transaction.
|
|
|
|
# @!attribute receipt
|
|
|
|
# @return [String] Optional additional more detailed description of the transaction.
|
|
|
|
# @!attribute image
|
|
|
|
# @return [Binary] Optional PNG image for e.g. scan of paper receipt.
|
|
|
|
|
|
|
|
belongs_to :bank_account
|
2020-08-01 02:49:15 +02:00
|
|
|
belongs_to :financial_link, optional: true
|
|
|
|
belongs_to :supplier, optional: true, foreign_key: 'iban', primary_key: 'iban'
|
|
|
|
belongs_to :user, optional: true, foreign_key: 'iban', primary_key: 'iban'
|
2018-09-14 19:33:27 +02:00
|
|
|
|
|
|
|
validates_presence_of :date, :amount, :bank_account_id
|
|
|
|
validates_numericality_of :amount
|
|
|
|
|
2018-10-11 22:16:50 +02:00
|
|
|
scope :without_financial_link, -> { where(financial_link: nil) }
|
|
|
|
|
2018-09-14 19:33:27 +02:00
|
|
|
# Replace numeric seperator with database format
|
|
|
|
localize_input_of :amount
|
|
|
|
|
|
|
|
def image_url
|
|
|
|
'data:image/png;base64,' + Base64.encode64(self.image)
|
|
|
|
end
|
2017-01-26 13:27:30 +01:00
|
|
|
|
|
|
|
def assign_to_invoice
|
|
|
|
return false unless supplier
|
|
|
|
|
2021-10-16 15:07:53 +02:00
|
|
|
content = text || ""
|
2017-01-26 13:27:30 +01:00
|
|
|
content += "\n" + reference if reference.present?
|
2021-03-01 15:27:26 +01:00
|
|
|
invoices = supplier.invoices.unpaid.select { |i| content.include? i.number }
|
2017-01-26 13:27:30 +01:00
|
|
|
invoices_sum = invoices.map(&:amount).sum
|
|
|
|
return false if amount != -invoices_sum
|
|
|
|
|
|
|
|
transaction do
|
|
|
|
link = FinancialLink.new
|
2022-10-13 18:25:52 +02:00
|
|
|
invoices.each { |i| i.update! financial_link: link, paid_on: date }
|
2017-01-26 13:27:30 +01:00
|
|
|
update_attribute :financial_link, link
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_to_ordergroup
|
|
|
|
m = BankTransactionReference.parse(reference)
|
|
|
|
return unless m
|
|
|
|
|
|
|
|
return false if m[:parts].values.sum != amount
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2017-01-26 13:27:30 +01:00
|
|
|
group = Ordergroup.find_by_id(m[:group])
|
|
|
|
return false unless group
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2017-01-26 13:27:30 +01:00
|
|
|
usr = m[:user] ? User.find_by_id(m[:user]) : group.users.first
|
|
|
|
return false unless usr
|
|
|
|
|
|
|
|
transaction do
|
|
|
|
note = "ID=#{id} (#{amount})"
|
|
|
|
link = FinancialLink.new
|
|
|
|
|
|
|
|
m[:parts].each do |short, value|
|
|
|
|
ftt = FinancialTransactionType.find_by_name_short(short)
|
|
|
|
return false unless ftt
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2017-01-26 13:27:30 +01:00
|
|
|
group.add_financial_transaction! value, note, usr, ftt, link if value > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
update_attribute :financial_link, link
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
2018-09-14 19:33:27 +02:00
|
|
|
end
|