Add automatic generation of financial transactions and links
This commit is contained in:
parent
91eeac6c40
commit
8e2ca5e7d7
11 changed files with 284 additions and 0 deletions
|
|
@ -12,4 +12,14 @@ class BankAccount < ApplicationRecord
|
|||
# @return [Function] Method wich can be called to import transaction from a bank or nil if unsupported
|
||||
def find_import_method
|
||||
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
|
||||
end
|
||||
|
|
|
|||
|
|
@ -33,4 +33,48 @@ class BankTransaction < ApplicationRecord
|
|||
def image_url
|
||||
'data:image/png;base64,' + Base64.encode64(self.image)
|
||||
end
|
||||
|
||||
def assign_to_invoice
|
||||
return false unless supplier
|
||||
|
||||
content = text
|
||||
content += "\n" + reference if reference.present?
|
||||
invoices = supplier.invoices.unpaid.select {|i| content.include? i.number}
|
||||
invoices_sum = invoices.map(&:amount).sum
|
||||
return false if amount != -invoices_sum
|
||||
|
||||
transaction do
|
||||
link = FinancialLink.new
|
||||
invoices.each {|i| i.update_attributes! financial_link: link, paid_on: date }
|
||||
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
|
||||
group = Ordergroup.find_by_id(m[:group])
|
||||
return false unless group
|
||||
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
|
||||
group.add_financial_transaction! value, note, usr, ftt, link if value > 0
|
||||
end
|
||||
|
||||
update_attribute :financial_link, link
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue