Class: Finance::FinancialLinksController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/finance/financial_links_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_bank_transactionObject



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

def add_bank_transaction
  bank_transaction = BankTransaction.find(params[:bank_transaction])
  bank_transaction.update_attribute :financial_link, @financial_link
  redirect_to finance_link_url(@financial_link), notice: t('.notice')
end

#add_financial_transactionObject



92
93
94
95
96
# File 'app/controllers/finance/financial_links_controller.rb', line 92

def add_financial_transaction
  financial_transaction = FinancialTransaction.find(params[:financial_transaction])
  financial_transaction.update_attribute :financial_link, @financial_link
  redirect_to finance_link_url(@financial_link), notice: t('.notice')
end

#add_invoiceObject



108
109
110
111
112
# File 'app/controllers/finance/financial_links_controller.rb', line 108

def add_invoice
  invoice = Invoice.find(params[:invoice])
  invoice.update_attribute :financial_link, @financial_link
  redirect_to finance_link_url(@financial_link), notice: t('.notice')
end

#createObject



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

def create
  @financial_link = FinancialLink.first_unused_or_create
  if params[:bank_transaction]
    bank_transaction = BankTransaction.find(params[:bank_transaction])
    bank_transaction.update_attribute :financial_link, @financial_link
  end
  redirect_to finance_link_url(@financial_link), notice: t('.notice')
end

#create_financial_transactionObject



79
80
81
82
83
84
85
86
# File 'app/controllers/finance/financial_links_controller.rb', line 79

def create_financial_transaction
  financial_transaction = FinancialTransaction.new(financial_transaction_params)
  financial_transaction.ordergroup.add_financial_transaction! financial_transaction.amount,
                                                              financial_transaction.note, current_user, financial_transaction.financial_transaction_type, @financial_link
  redirect_to finance_link_url(@financial_link), notice: t('.notice')
rescue StandardError => e
  redirect_to finance_link_url(@financial_link), alert: t('errors.general_msg', msg: e)
end


122
123
124
# File 'app/controllers/finance/financial_links_controller.rb', line 122

def find_financial_link
  @financial_link = FinancialLink.find(params[:id])
end

#incompleteObject



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

def incomplete
  @financial_links = FinancialLink.incomplete
end

#index_bank_transactionObject



57
58
59
# File 'app/controllers/finance/financial_links_controller.rb', line 57

def index_bank_transaction
  @bank_transactions = BankTransaction.without_financial_link
end

#index_financial_transactionObject



88
89
90
# File 'app/controllers/finance/financial_links_controller.rb', line 88

def index_financial_transaction
  @financial_transactions = FinancialTransaction.without_financial_link.includes(:financial_transaction_type, :ordergroup, :group_order)
end

#index_invoiceObject



104
105
106
# File 'app/controllers/finance/financial_links_controller.rb', line 104

def index_invoice
  @invoices = Invoice.without_financial_link.includes(:supplier)
end

#new_financial_transactionObject



73
74
75
76
77
# File 'app/controllers/finance/financial_links_controller.rb', line 73

def new_financial_transaction
  @financial_transaction = FinancialTransaction.new(financial_link: @financial_link)
  @financial_transaction.amount = @financial_link.amount
  @financial_transaction.ordergroup_id = find_best_fitting_ordergroup_id_for_financial_link(@financial_link.id)
end

#remove_bank_transactionObject



67
68
69
70
71
# File 'app/controllers/finance/financial_links_controller.rb', line 67

def remove_bank_transaction
  bank_transaction = BankTransaction.find(params[:bank_transaction])
  bank_transaction.update_attribute :financial_link, nil
  redirect_to finance_link_url(@financial_link), notice: t('.notice')
end

#remove_financial_transactionObject



98
99
100
101
102
# File 'app/controllers/finance/financial_links_controller.rb', line 98

def remove_financial_transaction
  financial_transaction = FinancialTransaction.find(params[:financial_transaction])
  financial_transaction.update_attribute :financial_link, nil
  redirect_to finance_link_url(@financial_link), notice: t('.notice')
end

#remove_invoiceObject



114
115
116
117
118
# File 'app/controllers/finance/financial_links_controller.rb', line 114

def remove_invoice
  invoice = Invoice.find(params[:invoice])
  invoice.update_attribute :financial_link, nil
  redirect_to finance_link_url(@financial_link), notice: t('.notice')
end

#showObject



4
5
6
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
37
38
39
40
41
42
# File 'app/controllers/finance/financial_links_controller.rb', line 4

def show
  @items = @financial_link.bank_transactions.map do |bt|
    {
      date: bt.date,
      type: t('activerecord.models.bank_transaction'),
      description: bt.text,
      amount: bt.amount,
      link_to: finance_bank_transaction_path(bt),
      remove_path: remove_bank_transaction_finance_link_path(@financial_link, bt)
    }
  end
  @items += @financial_link.financial_transactions.map do |ft|
    ft_note =
      if ft.group_order
        view_context.link_to ft.note, new_finance_order_path(order_id: ft.group_order.order.id)
      else
        ft.note
      end
    {
      date: ft.created_on,
      type: t('activerecord.models.financial_transaction'),
      description: "#{ft.ordergroup_name}: #{ft_note}",
      amount: ft.amount,
      link_to: finance_group_transactions_path(ft.ordergroup),
      remove_path: remove_financial_transaction_finance_link_path(@financial_link, ft)
    }
  end
  @items += @financial_link.invoices.includes(:supplier).map do |invoice|
    {
      date: invoice.date || invoice.created_at,
      type: t('activerecord.models.invoice'),
      description: "#{invoice.supplier.name}: #{invoice.number}",
      amount: invoice.amount,
      link_to: finance_invoice_path(invoice),
      remove_path: remove_invoice_finance_link_path(@financial_link, invoice)
    }
  end
  @items.sort_by! { |item| item[:date] }
end