Class: Deposit::CollectionController

Inherits:
EngineController show all
Defined in:
app/controllers/deposit/collection_controller.rb

Instance Method Summary collapse

Methods inherited from EngineController

#as_string, #current_tenant_user, #get_layout, #log_rescue_error

Instance Method Details

#account_invoicesObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
124
125
126
127
128
129
130
131
# File 'app/controllers/deposit/collection_controller.rb', line 55

def 
  cached_options_for_klient = options_for_klient

  searcher = lambda do |search_key, _offset, _limit|
     = begin
      KillBillClient::Model::Account.find_by_id(search_key, false, false,
                                                cached_options_for_klient)
    rescue StandardError
      nil
    end
    if .nil?
      []
    else
      .invoices(cached_options_for_klient)
    end
  end

  data_extractor = lambda do |invoice, column|
    [
      invoice.invoice_number.to_i,
      invoice.invoice_date,
      invoice.amount,
      invoice.balance,
      nil
    ][column]
  end

  formatter = lambda do |invoice|
    [
      invoice.invoice_number,
      invoice.invoice_date,
      view_context.humanized_money_with_symbol(Money.from_amount(invoice.amount.to_f, invoice.currency)),
      view_context.humanized_money_with_symbol(Money.from_amount(invoice.balance.to_f, invoice.currency)),
      nil
    ]
  end

  begin
    search_key = (params[:search] || {})[:value].presence
    offset = (params[:start] || 0).to_i
    limit = (params[:length] || 50).to_i
    pages = searcher.call(search_key, offset, limit)
  rescue StandardError => e
    error = e.to_s
  end

  json = {
    draw: (params[:draw] || 0).to_i,
    # We need to fill-in a number to make DataTables happy
    recordsTotal: pages.nil? ? 0 : limit,
    recordsFiltered: pages.nil? ? 0 : limit,
    data: []
  }
  json[:error] = error unless error.nil?

  pages ||= []

  # Until we support server-side sorting
  ordering = (params[:order] || {})[:'0'] || {}
  ordering_column = (ordering[:column] || 0).to_i
  ordering_dir = ordering[:dir] || 'asc'
  unless search_key.nil?
    pages.sort! do |a, b|
      a = data_extractor.call(a, ordering_column)
      b = data_extractor.call(b, ordering_column)
      sort = a <=> b
      sort.nil? ? -1 : sort
    end
  end
  pages.reverse! if (ordering_dir == 'desc' && limit >= 0) || (ordering_dir == 'asc' && limit.negative?)

  pages.each { |page| json[:data] << formatter.call(page) }

  respond_to do |format|
    format.json { render json: json }
  end
end

#indexObject



7
8
9
10
# File 'app/controllers/deposit/collection_controller.rb', line 7

def index
  @account_id = params[:account_id]
  @currency = params[:currency] || 'USD'
end

#record_paymentsObject



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
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/deposit/collection_controller.rb', line 12

def record_payments
  cached_options_for_klient = options_for_klient

  payments = {}
  params.each do |k, amount|
    next unless k.start_with?('payment_amount_')

    _, invoice_nb = k.split('payment_amount_')
    payments[invoice_nb] = amount
  end

  begin
    Killbill::Deposit::DepositClient.record_payments(params[:account_id],
                                                     params[:effective_date],
                                                     params[:payment_reference_number],
                                                     params[:deposit_type],
                                                     payments,
                                                     cached_options_for_klient[:username],
                                                     params[:reason],
                                                     params[:comment],
                                                     cached_options_for_klient)

    flash[:notice] = 'Deposit successfully collected'
    redirect_to kaui_engine.(params[:account_id])
  rescue StandardError => e
    flash.now[:error] = case e
                        when ::KillBillClient::API::NotFound
                          'Unable to record payment: invoice not found'
                        when ::KillBillClient::API::BadRequest
                          'Unable to record payment: deposit type, reference date, and reference number must be specified'
                        when ::KillBillClient::API::UnprocessableEntity
                          'Unable to record payment: amount too small or payment in UNKNOWN state'
                        else
                          "Internal error: #{as_string(e)}"
                        end

    @account_id = params[:account_id]
    @currency = params[:currency] || 'USD'

    render :index
  end
end