2009-01-10 21:28:22 +01:00
|
|
|
class Finance::TransactionsController < ApplicationController
|
|
|
|
before_filter :authenticate_finance
|
|
|
|
|
|
|
|
def index
|
|
|
|
if (params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 100)
|
|
|
|
@per_page = params[:per_page].to_i
|
|
|
|
else
|
|
|
|
@per_page = 20
|
|
|
|
end
|
|
|
|
if params["sort"]
|
|
|
|
sort = case params["sort"]
|
|
|
|
when "name" then "name"
|
|
|
|
when "account_balance" then "account_balance"
|
|
|
|
when "name_reverse" then "name DESC"
|
|
|
|
when "account_balance_reverse" then "account_balance DESC"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
sort = "name"
|
|
|
|
end
|
|
|
|
|
|
|
|
conditions = "name LIKE '%#{params[:query]}%'" unless params[:query].nil?
|
|
|
|
|
2009-02-11 15:23:59 +01:00
|
|
|
@total = Ordergroup.without_deleted.count(:conditions => conditions)
|
|
|
|
@groups = Ordergroup.without_deleted.paginate :conditions => conditions,
|
|
|
|
:page => params[:page], :per_page => @per_page, :order => sort
|
2009-01-10 21:28:22 +01:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2009-01-15 12:14:01 +01:00
|
|
|
format.js { render :partial => "ordergroups" }
|
2009-01-10 21:28:22 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def list
|
2009-01-14 12:46:01 +01:00
|
|
|
@group = Ordergroup.find(params[:id])
|
2009-01-10 21:28:22 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
conditions = ["note LIKE ?", "%#{params[:query]}%"] unless params[:query].nil?
|
|
|
|
|
|
|
|
@total = @group.financial_transactions.count(:conditions => conditions)
|
2009-01-15 12:14:01 +01:00
|
|
|
@financial_transactions = @group.financial_transactions.paginate(
|
|
|
|
:page => params[:page],
|
|
|
|
:per_page => 10,
|
|
|
|
:conditions => conditions,
|
|
|
|
:order => sort)
|
|
|
|
|
2009-01-10 21:28:22 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2009-01-15 12:14:01 +01:00
|
|
|
format.js { render :partial => "list" }
|
2009-01-10 21:28:22 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2009-01-14 12:46:01 +01:00
|
|
|
@group = Ordergroup.find(params[:id])
|
2009-01-10 21:28:22 +01:00
|
|
|
@financial_transaction = @group.financial_transactions.build
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2009-01-14 12:46:01 +01:00
|
|
|
@group = Ordergroup.find(params[:financial_transaction][:ordergroup_id])
|
2009-01-10 21:28:22 +01:00
|
|
|
amount = params[:financial_transaction][:amount]
|
|
|
|
note = params[:financial_transaction][:note]
|
|
|
|
begin
|
2009-02-04 16:41:01 +01:00
|
|
|
@group.add_financial_transaction(amount, note, @current_user)
|
2009-01-10 21:28:22 +01:00
|
|
|
flash[:notice] = 'Transaktion erfolgreich angelegt.'
|
2009-08-04 13:13:46 +02:00
|
|
|
redirect_to :action => 'list', :id => @group
|
2009-01-10 21:28:22 +01:00
|
|
|
rescue => e
|
|
|
|
@financial_transaction = FinancialTransaction.new(params[:financial_transaction])
|
|
|
|
flash.now[:error] = 'Transaktion konnte nicht angelegt werden!' + ' (' + e.message + ')'
|
|
|
|
render :action => 'new'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_collection
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_collection
|
|
|
|
note = params[:note]
|
2009-02-12 21:38:41 +01:00
|
|
|
raise "Notiz wird benötigt!" if note.blank?
|
2009-01-10 21:28:22 +01:00
|
|
|
params[:financial_transactions].each do |trans|
|
|
|
|
# ignore empty amount fields ...
|
|
|
|
unless trans[:amount].blank?
|
2009-02-04 16:41:01 +01:00
|
|
|
Ordergroup.find(trans[:ordergroup_id]).add_financial_transaction trans[:amount], note, @current_user
|
2009-01-10 21:28:22 +01:00
|
|
|
end
|
|
|
|
end
|
2009-02-12 21:38:41 +01:00
|
|
|
flash[:notice] = "Alle Transaktionen wurden gespeichert."
|
2009-01-10 21:28:22 +01:00
|
|
|
redirect_to :action => 'index'
|
|
|
|
rescue => error
|
2009-02-12 21:38:41 +01:00
|
|
|
flash[:error] = "Ein Fehler ist aufgetreten: " + error.to_s
|
2009-01-10 21:28:22 +01:00
|
|
|
redirect_to :action => 'new_collection'
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|