Skip to content

Commit

Permalink
Add option to create a financial_link when creating financial_transac…
Browse files Browse the repository at this point in the history
…tion

If multiple financial transaction belong to a bank transaction, it
is sometimes easier to create them as a collection and add the bank
transaction instead of adding all financial transaction to a link
created via a bank transaction.
  • Loading branch information
paroga committed Oct 30, 2019
1 parent ef61330 commit 260ef90
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
32 changes: 21 additions & 11 deletions app/controllers/finance/financial_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,30 @@ def new_collection
def create_collection
raise I18n.t('finance.financial_transactions.controller.create_collection.error_note_required') if params[:note].blank?
type = FinancialTransactionType.find_by_id(params[:type_id])
params[:financial_transactions].each do |trans|
# ignore empty amount fields ...
unless trans[:amount].blank?
amount = trans[:amount].to_f
note = params[:note]
ordergroup = Ordergroup.find(trans[:ordergroup_id])
if params[:set_balance]
note += " (#{amount})"
amount -= ordergroup.financial_transaction_class_balance(type.financial_transaction_class)
financial_link = nil

ActiveRecord::Base.transaction do
financial_link = FinancialLink.new if params[:create_financial_link]

params[:financial_transactions].each do |trans|
# ignore empty amount fields ...
unless trans[:amount].blank?
amount = trans[:amount].to_f
note = params[:note]
ordergroup = Ordergroup.find(trans[:ordergroup_id])
if params[:set_balance]
note += " (#{amount})"
amount -= ordergroup.financial_transaction_class_balance(type.financial_transaction_class)
end
ordergroup.add_financial_transaction!(amount, note, @current_user, type, financial_link)
end
ordergroup.add_financial_transaction!(amount, note, @current_user, type)
end

financial_link.try(&:save!)
end
redirect_to finance_ordergroups_url, notice: I18n.t('finance.financial_transactions.controller.create_collection.notice')

url = financial_link ? finance_link_url(financial_link.id) : finance_ordergroups_url
redirect_to url, notice: I18n.t('finance.financial_transactions.controller.create_collection.notice')
rescue => error
flash.now[:alert] = error.message
render action: :new_collection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
%p
= link_to t('.new_ordergroup'), '#', 'data-add-transaction' => true, class: 'btn'
= link_to t('.add_all_ordergroups'), '#', 'data-add-all-ordergroups' => true, class: 'btn'
- if BankAccount.any?
%p
%label
= check_box_tag :create_financial_link, true, params[:create_financial_link]
= t('.create_financial_link')
.form-actions
= submit_tag t('.save'), class: 'btn btn-primary'
= link_to t('ui.or_cancel'), finance_ordergroups_path
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ de:
title: Neue Transaktion
new_collection:
add_all_ordergroups: Alle Bestellgruppen hinzufügen
create_financial_link: Erstelle einen gemeinsamen Finanzlink für die neuen Transaktionen.
new_ordergroup: Weitere Bestellgruppe hinzufügen
save: Transaktionen speichern
set_balance: Setze den Kontostand der Bestellgrupppe auf den eingegebenen Betrag.
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ en:
title: New transaction
new_collection:
add_all_ordergroups: Add all ordergroups
create_financial_link: Create a common financial link for the new transactions.
new_ordergroup: Add new ordergroup
save: Save transaction
set_balance: Set the balance of the ordergroup to the entered amount.
Expand Down

0 comments on commit 260ef90

Please sign in to comment.