Class: Kaui::InvoicesController

Inherits:
EngineController show all
Defined in:
app/controllers/kaui/invoices_controller.rb

Instance Method Summary collapse

Methods inherited from EngineController

#current_ability, #current_user, #options_for_klient

Instance Method Details

#indexObject



3
4
5
6
7
# File 'app/controllers/kaui/invoices_controller.rb', line 3

def index
  if params[:invoice_id].present?
    redirect_to kaui_engine.invoice_path(params[:invoice_id])
  end
end

#paginationObject



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
# File 'app/controllers/kaui/invoices_controller.rb', line 9

def pagination
  json = {:sEcho => params[:sEcho], :iTotalRecords => 0, :iTotalDisplayRecords => 0, :aaData => []}

  search_key = params[:sSearch]
  if search_key.present?
    invoices = Kaui::Invoice.find_in_batches_by_search_key(search_key, params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
  else
    invoices = Kaui::Invoice.find_in_batches(params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
  end
  json[:iTotalDisplayRecords] = invoices.pagination_total_nb_records
  json[:iTotalRecords]        = invoices.pagination_max_nb_records

  invoices.each do |invoice|
    json[:aaData] << [
        view_context.link_to(view_context.truncate_uuid(invoice.invoice_id), view_context.url_for(:controller => :invoices, :action => :show, :id => invoice.invoice_id)),
        invoice.invoice_number,
        view_context.format_date(invoice.invoice_date),
        view_context.humanized_money_with_symbol(invoice.amount_to_money),
        view_context.humanized_money_with_symbol(invoice.balance_to_money)
    ]
  end

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

#showObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/kaui/invoices_controller.rb', line 36

def show
  invoice_id_or_number = params[:id]
  unless invoice_id_or_number.present?
    flash.now[:error] = 'No id given'
    render :index and return
  end

  begin
    @invoice         = Kaui::Invoice.find_by_id_or_number(invoice_id_or_number, true, 'FULL', options_for_klient)
    @invoice_id      = @invoice.invoice_id
    @account         = Kaui::Account.find_by_id(@invoice., false, false, options_for_klient)
    @payments        = Kaui::Invoice.new(:invoice_id => @invoice_id).payments(false, 'FULL', options_for_klient)
    @payment_methods = Kaui::PaymentMethod.payment_methods_for_payments(@payments, options_for_klient)

    @subscriptions            = {}
    @bundles                  = {}
    @cba_items_not_deleteable = []
    if @invoice.items.present?
      @invoice.items.each do |item|
        @cba_items_not_deleteable << item.linked_invoice_item_id if item.description =~ /account credit/ and item.amount < 0

        unless item.subscription_id.nil? || @subscriptions.has_key?(item.subscription_id)
          @subscriptions[item.subscription_id] = Kaui::Subscription::find_by_id(item.subscription_id, options_for_klient)
        end
        unless item.bundle_id.nil? || @bundles.has_key?(item.bundle_id)
          @bundles[item.bundle_id] = Kaui::Bundle::find_by_id(item.bundle_id, options_for_klient)
        end
      end
    else
      flash.now[:error] = "Invoice items for #{@invoice_id} not found"
    end
  rescue => e
    flash.now[:error] = "Error while getting information for invoice #{invoice_id_or_number}: #{as_string(e)}"
  end
end

#show_htmlObject



72
73
74
75
76
77
78
79
# File 'app/controllers/kaui/invoices_controller.rb', line 72

def show_html
  begin
    render :text => Kaui::Invoice.as_html(params[:id], options_for_klient)
  rescue => e
    flash.now[:error] = "Error rendering invoice html #{params[:id]}: #{as_string(e)}"
    render :action => :index
  end
end