Class: Finance::FinancialTransactionsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/finance/financial_transactions_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject


43
44
45
46
47
48
49
50
51
# File 'app/controllers/finance/financial_transactions_controller.rb', line 43

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: I18n.t('finance.financial_transactions.controller.create.notice')
rescue ActiveRecord::RecordInvalid => error
  flash.now[:alert] = error.message
  render :action => :new
end

#create_collectionObject


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/finance/financial_transactions_controller.rb', line 56

def create_collection
  raise I18n.t('finance.financial_transactions.controller.create_collection.error_note_required') if params[:note].blank?
  type = FinancialTransactionType.find_by_id(params.permit(:type))
  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, type)
    end
  end
  redirect_to finance_ordergroups_url, notice: I18n.t('finance.financial_transactions.controller.create_collection.notice')
rescue => error
  redirect_to finance_new_transaction_collection_url, alert: I18n.t('finance.financial_transactions.controller.create_collection.alert', error: error.to_s)
end

#find_ordergroupObject (protected)


72
73
74
# File 'app/controllers/finance/financial_transactions_controller.rb', line 72

def find_ordergroup
  @ordergroup = Ordergroup.find(params[:ordergroup_id])
end

#indexObject

belongs_to :ordergroup


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/finance/financial_transactions_controller.rb', line 8

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

  @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
  end
end

#index_collectionObject


35
36
37
# File 'app/controllers/finance/financial_transactions_controller.rb', line 35

def index_collection
  index
end

#newObject


39
40
41
# File 'app/controllers/finance/financial_transactions_controller.rb', line 39

def new
  @financial_transaction = @ordergroup.financial_transactions.build
end

#new_collectionObject


53
54
# File 'app/controllers/finance/financial_transactions_controller.rb', line 53

def new_collection
end