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
|
|
@ -5,6 +5,14 @@ class Finance::BankAccountsController < Finance::BaseController
|
|||
redirect_to finance_bank_account_transactions_url(@bank_accounts.first) if @bank_accounts.count == 1
|
||||
end
|
||||
|
||||
def assign_unlinked_transactions
|
||||
@bank_account = BankAccount.find(params[:id])
|
||||
count = @bank_account.assign_unlinked_transactions
|
||||
redirect_to finance_bank_account_transactions_url(@bank_account), notice: t('finance.bank_accounts.controller.assign.notice', count: count)
|
||||
rescue => error
|
||||
redirect_to finance_bank_account_transactions_url(@bank_account), alert: t('errors.general_msg', msg: error.message)
|
||||
end
|
||||
|
||||
def import
|
||||
@bank_account = BankAccount.find(params[:id])
|
||||
import_method = @bank_account.find_import_method
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
- title t('.title', name: @bank_account.name, balance: number_to_currency(@bank_account.balance))
|
||||
|
||||
- content_for :actionbar do
|
||||
= link_to t('.assign_unlinked_transactions'), assign_unlinked_transactions_finance_bank_account_path(@bank_account), class: 'btn'
|
||||
= link_to t('.import_transaction'), import_finance_bank_account_path(@bank_account), class: 'btn btn-primary'
|
||||
|
||||
.well.well-small
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue