Class: Kaui::AccountsController

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

Instance Method Summary collapse

Methods inherited from EngineController

#current_ability, #current_user, #options_for_klient

Instance Method Details

#createObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/kaui/accounts_controller.rb', line 40

def create
  @account                          = Kaui::Account.new(params[:account].delete_if { |key, value| value.blank? })

  # Transform "1" into boolean
  @account.is_migrated              = @account.is_migrated == '1'
  @account.is_notified_for_invoices = @account.is_notified_for_invoices == '1'

  begin
    @account = @account.create(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
    redirect_to (@account.), :notice => 'Account was successfully created'
  rescue => e
    flash.now[:error] = "Error while creating account: #{as_string(e)}"
    render :action => :new
  end
end

#indexObject



3
4
# File 'app/controllers/kaui/accounts_controller.rb', line 3

def index
end

#newObject



36
37
38
# File 'app/controllers/kaui/accounts_controller.rb', line 36

def new
  @account = Kaui::Account.new
end

#paginationObject



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

def pagination
  search_key = params[:sSearch]
  offset     = params[:iDisplayStart] || 0
  limit      = params[:iDisplayLength] || 10

  accounts = Kaui::Account.list_or_search(search_key, offset, limit, options_for_klient)

  json = {
      :sEcho                => params[:sEcho],
      :iTotalRecords        => accounts.pagination_max_nb_records,
      :iTotalDisplayRecords => accounts.pagination_total_nb_records,
      :aaData               => []
  }

  accounts.each do ||
    json[:aaData] << [
        view_context.link_to(view_context.truncate_uuid(.), view_context.url_for(:action => :show, :id => .)),
        .name,
        .external_key,
        .currency,
        .city,
        .country
    ]
  end

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

#pay_all_invoicesObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/kaui/accounts_controller.rb', line 103

def pay_all_invoices
  payment = Kaui::InvoicePayment.new(:account_id => params[:id])

  begin
    payment.bulk_create(params[:is_external_payment], current_user.kb_username, params[:reason], params[:comment], options_for_klient)
    flash[:notice] = 'Successfully triggered a payment for all unpaid invoices'
  rescue => e
    flash[:error] = "Error while triggering payments: #{as_string(e)}"
  end

  redirect_to (payment.)
end

#set_default_payment_methodObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/kaui/accounts_controller.rb', line 76

def set_default_payment_method
          = params[:id]
  payment_method_id = params[:payment_method_id]

  begin
    Kaui::PaymentMethod.set_default(payment_method_id, , current_user.kb_username, params[:reason], params[:comment], options_for_klient)
    flash[:notice] = "Successfully set #{payment_method_id} as default"
  rescue => e
    flash[:error] = "Error while setting payment method #{payment_method_id} as default: #{as_string(e)}"
  end

  redirect_to ()
end

#showObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/kaui/accounts_controller.rb', line 56

def show
  begin
    @account       = Kaui::Account::find_by_id_or_key(params[:id], true, true, options_for_klient)
    @overdue_state = @account.overdue(options_for_klient)
    @bundles       = @account.bundles(options_for_klient)
    @tags          = @account.tags(false, 'NONE', options_for_klient).sort { |tag_a, tag_b| tag_a <=> tag_b }

    @account_emails  = Kaui::AccountEmail.(@account., 'NONE', options_for_klient)
    @payment_methods = Kaui::PaymentMethod.(@account., true, options_for_klient)
  rescue => e
    flash.now[:error] = "Error while retrieving account information: #{as_string(e)}"
    render :action => :index and return
  end

  @subscriptions_by_bundle_id = {}
  @bundles.each do |bundle|
    @subscriptions_by_bundle_id[bundle.bundle_id.to_s] = (@subscriptions_by_bundle_id[bundle.bundle_id.to_s] || []) + bundle.subscriptions
  end
end

#toggle_email_notificationsObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/kaui/accounts_controller.rb', line 90

def toggle_email_notifications
   = Kaui::Account.new(:account_id => params[:id], :is_notified_for_invoices => params[:is_notified])

  begin
    .update_email_notifications(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
    flash[:notice] = 'Email preferences updated'
  rescue => e
    flash[:error] = "Error while setting email notifications: #{as_string(e)}"
  end

  redirect_to (.)
end