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



50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/finance/financial_transactions_controller.rb', line 50

def create
  @financial_transaction = FinancialTransaction.new(params[:financial_transaction])
  @financial_transaction.user = current_user
  @financial_transaction.save!
  redirect_to finance_group_transactions_path(@ordergroup),
              notice: I18n.t('finance.financial_transactions.controller.create.notice')
rescue ActiveRecord::RecordInvalid => e
  flash.now[:alert] = e.message
  render action: :new
end

#create_collectionObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/finance/financial_transactions_controller.rb', line 79

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[:type_id])
  financial_link = nil

  ActiveRecord::Base.transaction do
    financial_link = FinancialLink.new if params[:create_financial_link]
    foodcoop_amount = 0

    params[:financial_transactions].each do |trans|
      # ignore empty amount fields ...
      next if trans[:amount].blank?

      amount = LocalizeInput.parse(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)
      foodcoop_amount -= amount
    end

    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

    financial_link.try(&:save!)
  end

  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')
rescue StandardError => e
  flash.now[:alert] = e.message
  render action: :new_collection
end

#destroyObject



61
62
63
64
65
66
# File 'app/controllers/finance/financial_transactions_controller.rb', line 61

def destroy
  transaction = FinancialTransaction.find(params[:id])
  transaction.revert!(current_user)
  redirect_to finance_group_transactions_path(transaction.ordergroup),
              notice: t('finance.financial_transactions.controller.destroy.notice')
end

#find_ordergroupObject (protected)



127
128
129
130
131
132
133
# File 'app/controllers/finance/financial_transactions_controller.rb', line 127

def find_ordergroup
  if params[:ordergroup_id]
    @ordergroup = Ordergroup.include_transaction_class_sum.find(params[:ordergroup_id])
  else
    @foodcoop = true
  end
end

#indexObject

belongs_to :ordergroup



7
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
34
35
36
# File 'app/controllers/finance/financial_transactions_controller.rb', line 7

def index
  sort = if params['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
           'created_on DESC'
         end

  @q = FinancialTransaction.ransack(params[:q])
  @financial_transactions_all = @q.result(distinct: true).includes(:user).order(sort)
  @financial_transactions_all = @financial_transactions_all.visible unless params[:show_hidden]
  @financial_transactions_all = @financial_transactions_all.where(ordergroup_id: @ordergroup.id) if @ordergroup
  @financial_transactions_all = @financial_transactions_all.where(ordergroup: nil) if @foodcoop
  @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



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

def index_collection
  index
end

#newObject



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

def new
  @financial_transaction = if @ordergroup
                             @ordergroup.financial_transactions.build
                           else
                             FinancialTransaction.new
                           end
end

#new_collectionObject



68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/finance/financial_transactions_controller.rb', line 68

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