2011-06-09 21:35:05 +02:00
|
|
|
class Finance::FinancialTransactionsController < ApplicationController
|
2019-10-28 21:11:35 +01:00
|
|
|
before_action :authenticate_finance
|
2023-05-12 13:01:12 +02:00
|
|
|
before_action :find_ordergroup, except: %i[new_collection create_collection index_collection]
|
2011-06-09 21:35:05 +02:00
|
|
|
inherit_resources
|
2021-03-01 15:27:26 +01:00
|
|
|
# belongs_to :ordergroup
|
2011-06-09 21:35:05 +02:00
|
|
|
|
|
|
|
def index
|
2023-05-12 13:01:12 +02:00
|
|
|
sort = if params['sort']
|
|
|
|
case params['sort']
|
|
|
|
when 'date' then 'created_on'
|
|
|
|
when 'note' then 'note'
|
|
|
|
when 'amount' then 'amount'
|
|
|
|
when 'date_reverse' then 'created_on DESC'
|
|
|
|
when 'note_reverse' then 'note DESC'
|
|
|
|
when 'amount_reverse' then 'amount DESC'
|
2021-03-01 15:27:26 +01:00
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
else
|
|
|
|
'created_on DESC'
|
|
|
|
end
|
2011-06-09 21:35:05 +02:00
|
|
|
|
2023-02-14 12:24:11 +01:00
|
|
|
@q = FinancialTransaction.ransack(params[:q])
|
2014-05-06 19:02:01 +02:00
|
|
|
@financial_transactions_all = @q.result(distinct: true).includes(:user).order(sort)
|
2019-11-01 19:30:23 +01:00
|
|
|
@financial_transactions_all = @financial_transactions_all.visible unless params[:show_hidden]
|
2014-05-06 19:02:01 +02:00
|
|
|
@financial_transactions_all = @financial_transactions_all.where(ordergroup_id: @ordergroup.id) if @ordergroup
|
2019-11-01 17:25:20 +01:00
|
|
|
@financial_transactions_all = @financial_transactions_all.where(ordergroup: nil) if @foodcoop
|
2014-05-06 19:02:01 +02:00
|
|
|
@financial_transactions = @financial_transactions_all.page(params[:page]).per(@per_page)
|
|
|
|
|
|
|
|
respond_to do |format|
|
2023-05-12 13:01:12 +02:00
|
|
|
format.js
|
|
|
|
format.html { render }
|
2014-05-06 19:02:01 +02:00
|
|
|
format.csv do
|
2023-05-12 13:01:12 +02:00
|
|
|
send_data FinancialTransactionsCsv.new(@financial_transactions_all).to_csv, filename: 'transactions.csv',
|
|
|
|
type: 'text/csv'
|
2014-05-06 19:02:01 +02:00
|
|
|
end
|
2012-12-22 14:23:58 +01:00
|
|
|
end
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
|
|
|
|
2014-05-06 19:02:01 +02:00
|
|
|
def index_collection
|
|
|
|
index
|
|
|
|
end
|
|
|
|
|
2011-06-09 21:35:05 +02:00
|
|
|
def new
|
2023-05-12 13:01:12 +02:00
|
|
|
@financial_transaction = if @ordergroup
|
|
|
|
@ordergroup.financial_transactions.build
|
|
|
|
else
|
|
|
|
FinancialTransaction.new
|
|
|
|
end
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@financial_transaction = FinancialTransaction.new(params[:financial_transaction])
|
|
|
|
@financial_transaction.user = current_user
|
2023-10-02 23:00:24 +02:00
|
|
|
@financial_transaction.save!
|
2023-05-12 13:01:12 +02:00
|
|
|
redirect_to finance_group_transactions_path(@ordergroup),
|
|
|
|
notice: I18n.t('finance.financial_transactions.controller.create.notice')
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
flash.now[:alert] = e.message
|
|
|
|
render action: :new
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
|
|
|
|
2019-11-01 19:30:23 +01:00
|
|
|
def destroy
|
|
|
|
transaction = FinancialTransaction.find(params[:id])
|
|
|
|
transaction.revert!(current_user)
|
2023-05-12 13:01:12 +02:00
|
|
|
redirect_to finance_group_transactions_path(transaction.ordergroup),
|
|
|
|
notice: t('finance.financial_transactions.controller.destroy.notice')
|
2019-11-01 19:30:23 +01:00
|
|
|
end
|
|
|
|
|
2011-06-09 21:35:05 +02:00
|
|
|
def new_collection
|
2019-11-11 12:18:00 +01:00
|
|
|
@ordergroups = {}
|
|
|
|
Ordergroup.undeleted.order(:name).map do |ordergroup|
|
|
|
|
obj = { name: ordergroup.name }
|
|
|
|
Ordergroup.custom_fields.each do |field|
|
|
|
|
obj[field[:name]] = ordergroup.settings.custom_fields[field[:name]]
|
|
|
|
end
|
|
|
|
@ordergroups[ordergroup.id] = obj
|
|
|
|
end
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_collection
|
2013-10-02 17:23:49 +02:00
|
|
|
raise I18n.t('finance.financial_transactions.controller.create_collection.error_note_required') if params[:note].blank?
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2018-12-10 15:32:56 +01:00
|
|
|
type = FinancialTransactionType.find_by_id(params[:type_id])
|
2019-10-30 22:27:18 +01:00
|
|
|
financial_link = nil
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
financial_link = FinancialLink.new if params[:create_financial_link]
|
2019-11-01 17:29:07 +01:00
|
|
|
foodcoop_amount = 0
|
2019-10-30 22:27:18 +01:00
|
|
|
|
|
|
|
params[:financial_transactions].each do |trans|
|
|
|
|
# ignore empty amount fields ...
|
2023-05-12 13:01:12 +02:00
|
|
|
next if trans[:amount].blank?
|
|
|
|
|
|
|
|
amount = LocalizeInput.parse(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)
|
2018-12-30 03:43:48 +01:00
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
ordergroup.add_financial_transaction!(amount, note, @current_user, type, financial_link)
|
|
|
|
foodcoop_amount -= amount
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
2019-10-30 22:27:18 +01:00
|
|
|
|
2019-11-01 17:29:07 +01:00
|
|
|
if params[:create_foodcoop_transaction]
|
|
|
|
ft = FinancialTransaction.new({
|
2021-03-01 15:27:26 +01:00
|
|
|
financial_transaction_type: type,
|
|
|
|
user: @current_user,
|
|
|
|
amount: foodcoop_amount,
|
|
|
|
note: params[:note],
|
2023-05-12 13:01:12 +02:00
|
|
|
financial_link: financial_link
|
2021-03-01 15:27:26 +01:00
|
|
|
})
|
2019-11-01 17:29:07 +01:00
|
|
|
ft.save!
|
|
|
|
end
|
|
|
|
|
2019-10-30 22:27:18 +01:00
|
|
|
financial_link.try(&:save!)
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
2019-10-30 22:27:18 +01:00
|
|
|
|
|
|
|
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')
|
2023-05-12 13:01:12 +02:00
|
|
|
rescue StandardError => e
|
|
|
|
flash.now[:alert] = e.message
|
2018-12-30 03:43:48 +01:00
|
|
|
render action: :new_collection
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def find_ordergroup
|
2019-11-01 17:25:20 +01:00
|
|
|
if params[:ordergroup_id]
|
|
|
|
@ordergroup = Ordergroup.include_transaction_class_sum.find(params[:ordergroup_id])
|
|
|
|
else
|
|
|
|
@foodcoop = true
|
|
|
|
end
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
|
|
|
end
|