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)
|
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|
|
|
|
|
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
|
2019-11-01 17:25:20 +01:00
|
|
|
if @ordergroup
|
|
|
|
@financial_transaction = @ordergroup.financial_transactions.build
|
|
|
|
else
|
|
|
|
@financial_transaction = 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
|
2019-11-01 17:25:20 +01:00
|
|
|
if @financial_transaction.ordergroup
|
|
|
|
@financial_transaction.add_transaction!
|
|
|
|
else
|
|
|
|
@financial_transaction.save!
|
|
|
|
end
|
|
|
|
redirect_to finance_group_transactions_path(@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
|
|
|
|
|
2019-11-01 19:30:23 +01:00
|
|
|
def destroy
|
|
|
|
transaction = FinancialTransaction.find(params[:id])
|
|
|
|
transaction.revert!(current_user)
|
2019-11-01 17:25:20 +01: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?
|
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 ...
|
|
|
|
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)
|
2019-11-01 17:29:07 +01:00
|
|
|
foodcoop_amount -= amount
|
2018-12-30 03:43:48 +01:00
|
|
|
end
|
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({
|
|
|
|
financial_transaction_type: type,
|
|
|
|
user: @current_user,
|
|
|
|
amount: foodcoop_amount,
|
|
|
|
note: params[:note],
|
|
|
|
financial_link: financial_link,
|
|
|
|
})
|
|
|
|
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')
|
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
|
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
|