2012-08-24 11:11:40 +02:00
|
|
|
# encoding: utf-8
|
2011-06-09 21:35:05 +02:00
|
|
|
class Finance::FinancialTransactionsController < ApplicationController
|
|
|
|
before_filter :authenticate_finance
|
|
|
|
before_filter :find_ordergroup, :except => [:new_collection, :create_collection]
|
|
|
|
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
|
|
|
|
|
2012-11-10 16:44:05 +01:00
|
|
|
@financial_transactions = @ordergroup.financial_transactions.unscoped.order(sort)
|
2012-09-30 21:15:55 +02:00
|
|
|
@financial_transactions = @financial_transactions.where('note LIKE ?', "%#{params[:query]}%") unless params[:query].nil?
|
2011-06-09 21:35:05 +02:00
|
|
|
|
2012-11-10 16:44:05 +01:00
|
|
|
@financial_transactions = @financial_transactions.page(params[:page]).per(@per_page)
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
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!
|
|
|
|
redirect_to finance_ordergroup_transactions_url(@ordergroup), :notice => "Die Transaktion wurde gespeichert."
|
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
|
|
|
|
raise "Notiz wird benötigt!" if params[:note].blank?
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
2012-11-10 16:44:05 +01:00
|
|
|
redirect_to finance_ordergroups_url, notice: "Alle Transaktionen wurden gespeichert."
|
2011-06-09 21:35:05 +02:00
|
|
|
rescue => error
|
2012-11-10 16:44:05 +01:00
|
|
|
redirect_to finance_new_transaction_collection_url, alert: "Ein Fehler ist aufgetreten: " + error.to_s
|
2011-06-09 21:35:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def find_ordergroup
|
|
|
|
@ordergroup = Ordergroup.find(params[:ordergroup_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|