2012-08-24 11:11:40 +02:00
|
|
|
# encoding: utf-8
|
2011-06-09 21:35:05 +02:00
|
|
|
class Finance::FinancialTransactionsController < ApplicationController
|
2019-10-28 21:11:35 +01:00
|
|
|
before_action :authenticate_finance
|
|
|
|
before_action :find_ordergroup, :except => [:new_collection, :create_collection, :index_collection]
|
2011-06-09 21:35:05 +02:00
|
|
|
inherit_resources
|
|
|
|
# belongs_to :ordergroup
|
|
|
|
|
|
|
|
def index
|
|
|
|
if params['sort']
|
|
|
|
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"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
sort = "created_on DESC"
|
|
|
|
end
|
|
|
|
|
2014-05-06 19:02:01 +02:00
|
|
|
@q = FinancialTransaction.search(params[:q])
|
|
|
|
@financial_transactions_all = @q.result(distinct: true).includes(:user).order(sort)
|
|
|
|
@financial_transactions_all = @financial_transactions_all.where(ordergroup_id: @ordergroup.id) if @ordergroup
|
|
|
|
@financial_transactions = @financial_transactions_all.page(params[:page]).per(@per_page)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js; format.html { render }
|
|
|
|
format.csv do
|
|
|
|
send_data FinancialTransactionsCsv.new(@financial_transactions_all).to_csv, filename: 'transactions.csv', type: 'text/csv'
|
|
|
|
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
|
|
|
|
@financial_transaction = @ordergroup.financial_transactions.build
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@financial_transaction = FinancialTransaction.new(params[:financial_transaction])
|
|
|
|
@financial_transaction.user = current_user
|
|
|
|
@financial_transaction.add_transaction!
|
2013-10-02 17:23:49 +02:00
|
|
|
redirect_to finance_ordergroup_transactions_url(@ordergroup), notice: I18n.t('finance.financial_transactions.controller.create.notice')
|
2012-09-30 21:15:55 +02:00
|
|
|
rescue ActiveRecord::RecordInvalid => error
|
|
|
|
flash.now[:alert] = error.message
|
|
|
|
render :action => :new
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_collection
|
|
|
|
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?
|
2018-12-10 15:32:56 +01:00
|
|
|
type = FinancialTransactionType.find_by_id(params[:type_id])
|
2011-06-09 21:35:05 +02:00
|
|
|
params[:financial_transactions].each do |trans|
|
|
|
|
# ignore empty amount fields ...
|
|
|
|
unless trans[:amount].blank?
|
2018-12-30 03:43:48 +01:00
|
|
|
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)
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
|
|
|
end
|
2013-10-02 17:23:49 +02:00
|
|
|
redirect_to finance_ordergroups_url, notice: I18n.t('finance.financial_transactions.controller.create_collection.notice')
|
2011-06-09 21:35:05 +02:00
|
|
|
rescue => error
|
2018-12-30 03:43:48 +01:00
|
|
|
flash.now[:alert] = error.message
|
|
|
|
render action: :new_collection
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def find_ordergroup
|
|
|
|
@ordergroup = Ordergroup.find(params[:ordergroup_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|