2017-10-13 14:36:56 +02:00
|
|
|
class Finance::FinancialLinksController < Finance::BaseController
|
2019-11-19 12:51:08 +01:00
|
|
|
before_action :find_financial_link, except: [:create, :incomplete]
|
2017-10-13 14:36:56 +02:00
|
|
|
|
|
|
|
def show
|
2018-09-14 19:33:27 +02:00
|
|
|
@items = @financial_link.bank_transactions.map do |bt|
|
|
|
|
{
|
|
|
|
date: bt.date,
|
|
|
|
type: t('activerecord.models.bank_transaction'),
|
|
|
|
description: bt.text,
|
|
|
|
amount: bt.amount,
|
2018-10-11 22:16:50 +02:00
|
|
|
link_to: finance_bank_transaction_path(bt),
|
|
|
|
remove_path: remove_bank_transaction_finance_link_path(@financial_link, bt)
|
2018-09-14 19:33:27 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
@items += @financial_link.financial_transactions.map do |ft|
|
2017-10-13 14:36:56 +02:00
|
|
|
{
|
|
|
|
date: ft.created_on,
|
|
|
|
type: t('activerecord.models.financial_transaction'),
|
2019-11-01 14:07:23 +01:00
|
|
|
description: "#{ft.ordergroup_name}: #{ft.note}",
|
2017-10-13 14:36:56 +02:00
|
|
|
amount: ft.amount,
|
2019-11-01 17:25:20 +01:00
|
|
|
link_to: finance_group_transactions_path(ft.ordergroup),
|
2018-10-11 22:16:50 +02:00
|
|
|
remove_path: remove_financial_transaction_finance_link_path(@financial_link, ft)
|
2017-10-13 14:36:56 +02:00
|
|
|
}
|
|
|
|
end
|
2019-10-30 11:53:44 +01:00
|
|
|
@items += @financial_link.invoices.includes(:supplier).map do |invoice|
|
2017-10-13 14:36:56 +02:00
|
|
|
{
|
|
|
|
date: invoice.date || invoice.created_at,
|
|
|
|
type: t('activerecord.models.invoice'),
|
|
|
|
description: "#{invoice.supplier.name}: #{invoice.number}",
|
|
|
|
amount: invoice.amount,
|
2018-10-11 22:16:50 +02:00
|
|
|
link_to: finance_invoice_path(invoice),
|
|
|
|
remove_path: remove_invoice_finance_link_path(@financial_link, invoice)
|
2017-10-13 14:36:56 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
@items.sort_by! { |item| item[:date] }
|
|
|
|
end
|
|
|
|
|
2018-10-11 22:16:50 +02:00
|
|
|
def create
|
2021-02-08 02:44:07 +01:00
|
|
|
@financial_link = FinancialLink.first_unused_or_create
|
2018-10-11 22:16:50 +02:00
|
|
|
if params[:bank_transaction] then
|
|
|
|
bank_transaction = BankTransaction.find(params[:bank_transaction])
|
|
|
|
bank_transaction.update_attribute :financial_link, @financial_link
|
|
|
|
end
|
2019-10-30 12:42:41 +01:00
|
|
|
redirect_to finance_link_url(@financial_link), notice: t('.notice')
|
2018-10-11 22:16:50 +02:00
|
|
|
end
|
|
|
|
|
2019-11-19 12:51:08 +01:00
|
|
|
def incomplete
|
|
|
|
@financial_links = FinancialLink.incomplete
|
|
|
|
end
|
|
|
|
|
2018-10-11 22:16:50 +02:00
|
|
|
def index_bank_transaction
|
|
|
|
@bank_transactions = BankTransaction.without_financial_link
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_bank_transaction
|
|
|
|
bank_transaction = BankTransaction.find(params[:bank_transaction])
|
|
|
|
bank_transaction.update_attribute :financial_link, @financial_link
|
2019-10-30 12:42:41 +01:00
|
|
|
redirect_to finance_link_url(@financial_link), notice: t('.notice')
|
2018-10-11 22:16:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_bank_transaction
|
|
|
|
bank_transaction = BankTransaction.find(params[:bank_transaction])
|
|
|
|
bank_transaction.update_attribute :financial_link, nil
|
2019-10-30 12:42:41 +01:00
|
|
|
redirect_to finance_link_url(@financial_link), notice: t('.notice')
|
2018-10-11 22:16:50 +02:00
|
|
|
end
|
|
|
|
|
2021-02-08 02:47:05 +01:00
|
|
|
def new_financial_transaction
|
|
|
|
@financial_transaction = FinancialTransaction.new(financial_link: @financial_link)
|
|
|
|
@financial_transaction.amount = @financial_link.amount
|
|
|
|
@financial_transaction.ordergroup_id = find_best_fitting_ordergroup_id_for_financial_link(@financial_link.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_financial_transaction
|
|
|
|
financial_transaction = FinancialTransaction.new(financial_transaction_params)
|
|
|
|
financial_transaction.ordergroup.add_financial_transaction! financial_transaction.amount, financial_transaction.note, current_user, financial_transaction.financial_transaction_type, @financial_link
|
|
|
|
redirect_to finance_link_url(@financial_link), notice: t('.notice')
|
|
|
|
rescue => error
|
|
|
|
redirect_to finance_link_url(@financial_link), alert: t('errors.general_msg', msg: error)
|
|
|
|
end
|
|
|
|
|
2018-10-11 22:16:50 +02:00
|
|
|
def index_financial_transaction
|
2019-10-30 11:53:44 +01:00
|
|
|
@financial_transactions = FinancialTransaction.without_financial_link.includes(:financial_transaction_type, :ordergroup)
|
2018-10-11 22:16:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_financial_transaction
|
|
|
|
financial_transaction = FinancialTransaction.find(params[:financial_transaction])
|
|
|
|
financial_transaction.update_attribute :financial_link, @financial_link
|
2019-10-30 12:42:41 +01:00
|
|
|
redirect_to finance_link_url(@financial_link), notice: t('.notice')
|
2018-10-11 22:16:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_financial_transaction
|
|
|
|
financial_transaction = FinancialTransaction.find(params[:financial_transaction])
|
|
|
|
financial_transaction.update_attribute :financial_link, nil
|
2019-10-30 12:42:41 +01:00
|
|
|
redirect_to finance_link_url(@financial_link), notice: t('.notice')
|
2018-10-11 22:16:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def index_invoice
|
2019-10-30 11:53:44 +01:00
|
|
|
@invoices = Invoice.without_financial_link.includes(:supplier)
|
2018-10-11 22:16:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_invoice
|
|
|
|
invoice = Invoice.find(params[:invoice])
|
|
|
|
invoice.update_attribute :financial_link, @financial_link
|
2019-10-30 12:42:41 +01:00
|
|
|
redirect_to finance_link_url(@financial_link), notice: t('.notice')
|
2018-10-11 22:16:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_invoice
|
|
|
|
invoice = Invoice.find(params[:invoice])
|
|
|
|
invoice.update_attribute :financial_link, nil
|
2019-10-30 12:42:41 +01:00
|
|
|
redirect_to finance_link_url(@financial_link), notice: t('.notice')
|
2018-10-11 22:16:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def find_financial_link
|
|
|
|
@financial_link = FinancialLink.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2021-02-08 02:47:05 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def financial_transaction_params
|
|
|
|
params.require(:financial_transaction).permit(:financial_transaction_type_id, :ordergroup_id, :amount, :note)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_best_fitting_ordergroup_id_for_financial_link(financial_link_id)
|
|
|
|
FinancialTransaction.joins(<<-SQL).order(created_on: :desc).pluck(:ordergroup_id).first
|
|
|
|
JOIN bank_transactions a ON financial_transactions.financial_link_id = a.financial_link_id
|
|
|
|
JOIN bank_transactions b ON a.iban = b.iban AND b.financial_link_id = #{financial_link_id.to_i}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2017-10-13 14:36:56 +02:00
|
|
|
end
|