Class: Kaui::AccountsController
- Inherits:
-
EngineController
- Object
- ApplicationController
- EngineController
- Kaui::AccountsController
- Defined in:
- app/controllers/kaui/accounts_controller.rb
Constant Summary
Constants included from EngineControllerUtil
EngineControllerUtil::SIMPLE_PAGINATION_THRESHOLD
Instance Method Summary collapse
- #create ⇒ Object
-
#destroy ⇒ Object
rubocop:enable Style/MultilineBlockChain.
- #edit ⇒ Object
- #events_to_consider ⇒ Object
- #index ⇒ Object
- #link_to_parent ⇒ Object
- #new ⇒ Object
-
#next_invoice_date ⇒ Object
Fetched asynchronously, as it takes time.
- #pagination ⇒ Object
- #pay_all_invoices ⇒ Object
- #set_default_payment_method ⇒ Object
- #set_email_notifications_configuration ⇒ Object
-
#show ⇒ Object
rubocop:disable Style/MultilineBlockChain.
- #trigger_invoice ⇒ Object
- #unlink_to_parent ⇒ Object
- #update ⇒ Object
- #validate_external_key ⇒ Object
Methods inherited from EngineController
#check_for_redirect_to_tenant_screen, #current_ability, #current_user, #options_for_klient, #populate_account_details, #retrieve_allowed_users_for_current_user, #retrieve_tenants_for_current_user
Instance Method Details
#create ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/controllers/kaui/accounts_controller.rb', line 59 def create @account = Kaui::Account.new(params.require(:account).delete_if { |_key, value| value.blank? }) @account.errors.add(:phone, :invalid_phone) if !@account.phone.nil? && !@account.check_account_details_phone @account.errors.add(:check_account_details_bill_cycle_day_local, :invalid_bill_cycle_day_local) if !@account.bill_cycle_day_local.nil? && !@account.check_account_details_bill_cycle_day_local unless @account.errors.empty? flash.now[:errors] = @account.errors..values.flatten render action: :new and return end # Transform "1" into boolean @account.is_migrated = @account.is_migrated == '1' begin @account = @account.create(current_user.kb_username, params[:reason], params[:comment], ) redirect_to account_path(@account.account_id), notice: 'Account was successfully created' rescue StandardError => e flash.now[:error] = "Error while creating account: #{as_string(e)}" render action: :new end end |
#destroy ⇒ Object
rubocop:enable Style/MultilineBlockChain
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'app/controllers/kaui/accounts_controller.rb', line 156 def destroy account_id = params.require(:account_id) = params[:options] || [] cancel_subscriptions = .include?('cancel_all_subscriptions') writeoff_unpaid_invoices = .include?('writeoff_unpaid_invoices') item_adjust_unpaid_invoices = .include?('item_adjust_unpaid_invoices') = begin @account = Kaui::Account.find_by_id_or_key(account_id, false, false, ) @account.close(cancel_subscriptions, writeoff_unpaid_invoices, item_adjust_unpaid_invoices, current_user.kb_username, nil, nil, ) flash[:notice] = "Account #{account_id} successfully closed" rescue StandardError => e flash[:error] = "Error while closing account: #{as_string(e)}" end redirect_to account_path(account_id) end |
#edit ⇒ Object
217 |
# File 'app/controllers/kaui/accounts_controller.rb', line 217 def edit; end |
#events_to_consider ⇒ Object
329 330 331 332 333 |
# File 'app/controllers/kaui/accounts_controller.rb', line 329 def events_to_consider json_response do { data: Kenui::EmailNotificationService.get_events_to_consider() } end end |
#index ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/controllers/kaui/accounts_controller.rb', line 5 def index @search_query = params[:q] if params[:fast] == '1' && !@search_query.blank? account = Kaui::Account.list_or_search(@search_query, -1, 1, ).first if account.nil? flash[:error] = "No account matches \"#{@search_query}\"" redirect_to kaui_engine.home_path else redirect_to kaui_engine.account_path(account.account_id) end return end @ordering = params[:ordering] || (@search_query.blank? ? 'desc' : 'asc') @offset = params[:offset] || 0 @limit = params[:limit] || 50 @max_nb_records = @search_query.blank? ? Kaui::Account.list_or_search(nil, 0, 0, ).pagination_max_nb_records : 0 end |
#link_to_parent ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'app/controllers/kaui/accounts_controller.rb', line 264 def link_to_parent @account = Kaui::Account.new(params.require(:account).delete_if { |_key, value| value.blank? }) @account.account_id = params.require(:account_id) @account.is_payment_delegated_to_parent = @account.is_payment_delegated_to_parent == '1' raise('Account id and account parent id cannot be equal.') if @account.account_id == @account.parent_account_id = # check if parent id is valid Kaui::Account.find_by_id(@account.parent_account_id, false, false, ) @account.update(false, current_user.kb_username, params[:reason], params[:comment], ) redirect_to account_path(@account.account_id), notice: 'Account successfully updated' rescue StandardError => e flash[:error] = if e.is_a?(KillBillClient::API::NotFound) "Parent account id not found: #{@account.parent_account_id}" else "Error while linking parent account: #{as_string(e)}" end redirect_to account_path(@account.account_id) end |
#new ⇒ Object
55 56 57 |
# File 'app/controllers/kaui/accounts_controller.rb', line 55 def new @account = Kaui::Account.new end |
#next_invoice_date ⇒ Object
Fetched asynchronously, as it takes time. This also helps with enforcing permissions.
210 211 212 213 214 215 |
# File 'app/controllers/kaui/accounts_controller.rb', line 210 def next_invoice_date json_response do next_invoice = Kaui::Invoice.trigger_invoice_dry_run(params.require(:account_id), nil, true, ) next_invoice ? next_invoice.target_date.to_json : nil end end |
#pagination ⇒ Object
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/kaui/accounts_controller.rb', line 26 def pagination = searcher = lambda do |search_key, offset, limit| Kaui::Account.list_or_search(search_key, offset, limit, ) end data_extractor = lambda do |account, column| [ account.parent_account_id, account.account_id, account.external_key, account.account_balance ][column] end formatter = lambda do |account| child_label = '' unless account.parent_account_id.nil? child_label = account.parent_account_id.nil? ? '' : view_context.content_tag(:span, 'Child', class: %w[label label-info account-child-label]) end row = [child_label, view_context.link_to(account.account_id, view_context.url_for(action: :show, account_id: account.account_id))] row += Kaui.account_search_columns.call(account, view_context)[1] row end paginate searcher, data_extractor, formatter end |
#pay_all_invoices ⇒ Object
243 244 245 246 247 248 249 |
# File 'app/controllers/kaui/accounts_controller.rb', line 243 def pay_all_invoices payment = Kaui::InvoicePayment.new(account_id: params.require(:account_id)) payment.bulk_create(params[:is_external_payment] == 'true', nil, nil, current_user.kb_username, params[:reason], params[:comment], ) redirect_to account_path(payment.account_id), notice: 'Successfully triggered a payment for all unpaid invoices' end |
#set_default_payment_method ⇒ Object
234 235 236 237 238 239 240 241 |
# File 'app/controllers/kaui/accounts_controller.rb', line 234 def set_default_payment_method account_id = params.require(:account_id) payment_method_id = params.require(:payment_method_id) Kaui::PaymentMethod.set_default(payment_method_id, account_id, current_user.kb_username, params[:reason], params[:comment], ) redirect_to account_path(account_id), notice: "Successfully set #{payment_method_id} as default" end |
#set_email_notifications_configuration ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'app/controllers/kaui/accounts_controller.rb', line 305 def set_email_notifications_configuration configuration = params.require(:configuration) account_id = configuration[:account_id] event_types = configuration[:event_types] = is_success, = email_notification_plugin_available?() if is_success is_success, = Kenui::EmailNotificationService.set_configuration_per_account(account_id, event_types, current_user.kb_username, params[:reason], params[:comment], ) end if is_success flash[:notice] = else flash[:error] = end redirect_to account_path(account_id) end |
#show ⇒ Object
rubocop:disable Style/MultilineBlockChain
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/controllers/kaui/accounts_controller.rb', line 85 def show # Go to the database once = # Re-fetch the account with balance and CBA @account = Kaui::Account.find_by_id_or_key(params.require(:account_id), true, true, ) fetch_children = promise { @account.children(false, false, 'NONE', ) } fetch_parent = @account.parent_account_id.nil? ? nil : promise { Kaui::Account.find_by_id(@account.parent_account_id, false, false, ) } fetch_overdue_state = promise { @account.overdue() } = promise { @account.(false, 'NONE', ).sort { |tag_a, tag_b| tag_a <=> tag_b } } fetch_account_fields = promise { @account.custom_fields('NONE', ).sort { |cf_a, cf_b| cf_a.name.downcase <=> cf_b.name.downcase } } fetch_account_emails = promise { Kaui::AccountEmail.find_all_sorted_by_account_id(@account.account_id, 'NONE', ) } fetch_payments = promise { @account.payments().map! { |payment| Kaui::Payment.build_from_raw_payment(payment) } } fetch_payment_methods = promise { Kaui::PaymentMethod.find_all_by_account_id(@account.account_id, false, ) } # is email notification plugin available is_email_notifications_plugin_available = Kenui::EmailNotificationService.email_notification_plugin_available?().first fetch_email_notification_configuration = if is_email_notifications_plugin_available promise do Kenui::EmailNotificationService.get_configuration_per_account(params.require(:account_id), ) end.then do |configuration| if configuration.first.is_a?(FalseClass) Rails.logger.warn(configuration[1]) configuration = [] end configuration end else nil end fetch_payment_methods_with_details = fetch_payment_methods.then do |pms| ops = [] pms.each do |pm| ops << promise do Kaui::PaymentMethod.find_by_id(pm.payment_method_id, true, ) rescue StandardError => e # Maybe the plugin is not registered or the plugin threw an exception Rails.logger.warn(e) nil end end ops end = promise { Kaui::TagDefinition.all_for_account() } @overdue_state = wait(fetch_overdue_state) @tags = wait() @custom_fields = wait(fetch_account_fields) @account_emails = wait(fetch_account_emails) wait(fetch_payment_methods) @payment_methods = wait(fetch_payment_methods_with_details).map { |pm_f| wait(pm_f) }.compact @available_tags = wait() @children = wait(fetch_children) @account_parent = @account.parent_account_id.nil? ? nil : wait(fetch_parent) @email_notification_configuration = wait(fetch_email_notification_configuration) if is_email_notifications_plugin_available @last_transaction_by_payment_method_id = {} wait(fetch_payments).each do |payment| transaction = payment.transactions.last transaction_date = Date.parse(transaction.effective_date) last_seen_transaction_date = @last_transaction_by_payment_method_id[payment.payment_method_id] @last_transaction_by_payment_method_id[payment.payment_method_id] = transaction if last_seen_transaction_date.nil? || Date.parse(last_seen_transaction_date.effective_date) < transaction_date end params.permit! end |
#trigger_invoice ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'app/controllers/kaui/accounts_controller.rb', line 177 def trigger_invoice account_id = params.require(:account_id) target_date = params[:target_date].presence dry_run = params[:dry_run].nil? ? false : params[:dry_run] == '1' invoice = nil begin invoice = if dry_run Kaui::Invoice.trigger_invoice_dry_run(account_id, target_date, false, ) else Kaui::Invoice.trigger_invoice(account_id, target_date, current_user.kb_username, params[:reason], params[:comment], ) end rescue KillBillClient::API::NotFound # Null invoice end if invoice.nil? redirect_to account_path(account_id), notice: "Nothing to generate for target date #{target_date.nil? ? 'today' : target_date}" elsif dry_run @invoice = Kaui::Invoice.build_from_raw_invoice(invoice) @invoice_tags = [] @available_invoice_tags = [] @payments = [] @payment_methods = nil @account = Kaui::Account.find_by_id(account_id, false, false, ) render template: 'kaui/invoices/show' else # Redirect to fetch payments, etc. redirect_to invoice_path(invoice.invoice_id, account_id: account_id), notice: "Generated invoice #{invoice.invoice_number} for target date #{invoice.target_date}" end end |
#unlink_to_parent ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'app/controllers/kaui/accounts_controller.rb', line 288 def unlink_to_parent account_id = params.require(:account_id) = # search for the account and remove the parent account id # check if parent id is valid account = Kaui::Account.find_by_id(account_id, false, false, ) account.is_payment_delegated_to_parent = false account.parent_account_id = nil account.update(true, current_user.kb_username, params[:reason], params[:comment], ) redirect_to account_path(@account.account_id), notice: 'Account successfully updated' rescue StandardError => e flash[:error] = "Error while un-linking parent account: #{as_string(e)}" redirect_to account_path(@account.account_id) end |
#update ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'app/controllers/kaui/accounts_controller.rb', line 219 def update @account = Kaui::Account.new(params.require(:account).delete_if { |_key, value| value.blank? }) @account.account_id = params.require(:account_id) # Transform "1" into boolean @account.is_migrated = @account.is_migrated == '1' @account.update(true, current_user.kb_username, params[:reason], params[:comment], ) redirect_to account_path(@account.account_id), notice: 'Account successfully updated' rescue StandardError => e flash.now[:error] = "Error while updating account: #{as_string(e)}" render action: :edit end |
#validate_external_key ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'app/controllers/kaui/accounts_controller.rb', line 251 def validate_external_key json_response do external_key = params.require(:external_key) begin account = Kaui::Account.find_by_external_key(external_key, false, false, ) rescue KillBillClient::API::NotFound account = nil end { is_found: !account.nil? } end end |