Add set_balance to FinancialTransactions#new_collection (fixes #438)

A new checkbox will allow user to set the balance to a given ABSOLUTE value
in addition to changing it by a RELATIVE value. This can be used if the
balance is tracked outside of foodsoft and should be syncroniced or for
setting the balance to zero for multiple ordergroups.
This commit is contained in:
Patrick Gansterer 2018-12-30 03:43:48 +01:00
parent abe847c0ee
commit 389f205a6b
5 changed files with 24 additions and 3 deletions

View file

@ -59,12 +59,20 @@ class Finance::FinancialTransactionsController < ApplicationController
params[:financial_transactions].each do |trans|
# ignore empty amount fields ...
unless trans[:amount].blank?
Ordergroup.find(trans[:ordergroup_id]).add_financial_transaction!(trans[:amount], params[:note], @current_user, type)
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)
end
end
redirect_to finance_ordergroups_url, notice: I18n.t('finance.financial_transactions.controller.create_collection.notice')
rescue => error
redirect_to finance_new_transaction_collection_url, alert: I18n.t('finance.financial_transactions.controller.create_collection.alert', error: error.to_s)
flash.now[:alert] = error.message
render action: :new_collection
end
protected