Class: Kaui::InvoicesController
- Inherits:
-
EngineController
- Object
- EngineController
- Kaui::InvoicesController
- Defined in:
- app/controllers/kaui/invoices_controller.rb
Instance Method Summary collapse
- #commit_invoice ⇒ Object
- #download ⇒ Object
- #index ⇒ Object
- #pagination ⇒ Object
- #restful_show ⇒ Object
- #restful_show_by_number ⇒ Object
-
#show ⇒ Object
rubocop:disable Lint/HashCompareByIdentity.
- #show_html ⇒ Object
-
#void_invoice ⇒ Object
rubocop:enable Lint/HashCompareByIdentity.
Instance Method Details
#commit_invoice ⇒ Object
183 184 185 186 187 188 |
# File 'app/controllers/kaui/invoices_controller.rb', line 183 def commit_invoice = invoice = KillBillClient::Model::Invoice.find_by_id(params.require(:id), false, 'NONE', ) invoice.commit(current_user.kb_username, params[:reason], params[:comment], ) redirect_to account_invoice_path(invoice.account_id, invoice.invoice_id), notice: 'Invoice successfully committed' end |
#download ⇒ Object
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 54 55 56 57 58 |
# File 'app/controllers/kaui/invoices_controller.rb', line 19 def download account_id = scalar_account_id_param start_date = params[:startDate] end_date = params[:endDate] all_fields_checked = params[:allFieldsChecked] == 'true' query_string = handle_balance_search(params[:search]) columns = if all_fields_checked KillBillClient::Model::InvoiceAttributes.instance_variable_get(:@json_attributes) - Kaui::Invoice::TABLE_IGNORE_COLUMNS else params.require(:columnsString).split(',').map { |attr| attr.split.join('_').downcase } end kb_params = {} kb_params[:startDate] = Date.parse(start_date).strftime('%Y-%m-%d') if start_date kb_params[:endDate] = Date.parse(end_date).strftime('%Y-%m-%d') if end_date query_string = remapping_addvanced_search_fields(query_string, Kaui::Invoice::ADVANCED_SEARCH_NAME_CHANGES) invoices = if account_id.present? && query_string.blank? Kaui::Account.paginated_invoices(account_id, nil, nil, 'NONE', ).map! { |invoice| Kaui::Invoice.build_from_raw_invoice(invoice) } else Kaui::Invoice.list_or_search(query_string, 0, MAXIMUM_NUMBER_OF_RECORDS_DOWNLOAD, .merge(params: kb_params)) end if start_date && end_date invoices = invoices.select do |invoice| start_date_parsed = Date.parse(start_date) end_date_parsed = Date.parse(end_date) invoice_date = Date.parse(invoice.invoice_date) invoice_date.between?(start_date_parsed, end_date_parsed) end end csv_string = CSV.generate(headers: true) do |csv| csv << columns invoices.each do |invoice| csv << columns.map { |attr| invoice&.send(attr.downcase) } end end send_data csv_string, filename: "invoices-#{Time.zone.today}.csv", type: 'text/csv' end |
#index ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'app/controllers/kaui/invoices_controller.rb', line 6 def index @search_query = scalar_account_id_param @advance_search_query = params[:q] || request.query_string @ordering = params[:ordering] || 'desc' @offset = params[:offset] || 0 @limit = params[:limit] || 50 @search_fields = Kaui::Invoice::ADVANCED_SEARCH_COLUMNS.map { |attr| [attr, attr.split('_').join(' ').capitalize] } @dropdown_default = default_columns(Kaui.account_invoices_columns.call[3], Kaui::Invoice::DEFAULT_VISIBLE_COLUMNS) @visible_columns = @dropdown_default @max_nb_records = @search_query.blank? ? Kaui::Invoice.list_or_search(nil, 0, 0, ).pagination_max_nb_records : 0 end |
#pagination ⇒ Object
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 |
# File 'app/controllers/kaui/invoices_controller.rb', line 60 def pagination = searcher = lambda do |search_key, offset, limit| account = begin Kaui::Account.find_by_id_or_key(search_key, false, false, ) unless advanced_search_query?(search_key) rescue StandardError nil end if account.nil? search_key = remapping_addvanced_search_fields(search_key, Kaui::Invoice::ADVANCED_SEARCH_NAME_CHANGES) Kaui::Invoice.list_or_search(search_key, offset, limit, ) else Kaui::Account.paginated_invoices(search_key, offset, limit, 'NONE', ).map! { |invoice| Kaui::Invoice.build_from_raw_invoice(invoice) } end end data_extractor = lambda do |invoice, column| Kaui.account_invoices_columns.call(invoice, view_context)[2][column] end formatter = lambda do |invoice| Kaui.account_invoices_columns.call(invoice, view_context)[1] end paginate searcher, data_extractor, formatter end |
#restful_show ⇒ Object
167 168 169 170 |
# File 'app/controllers/kaui/invoices_controller.rb', line 167 def restful_show invoice = Kaui::Invoice.find_by_id(params.require(:id), false, 'NONE', ) redirect_to account_invoice_path(invoice.account_id, invoice.invoice_id) end |
#restful_show_by_number ⇒ Object
172 173 174 175 |
# File 'app/controllers/kaui/invoices_controller.rb', line 172 def restful_show_by_number invoice = Kaui::Invoice.find_by_number(params.require(:number), false, 'NONE', ) redirect_to account_invoice_path(invoice.account_id, invoice.invoice_id) end |
#show ⇒ Object
rubocop:disable Lint/HashCompareByIdentity
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 |
# File 'app/controllers/kaui/invoices_controller.rb', line 88 def show # Go to the database once = @invoice = Kaui::Invoice.find_by_id(params.require(:id), false, 'FULL', ) # This will put the TAX items at the bottom precedence = { 'EXTERNAL_CHARGE' => 0, 'FIXED' => 1, 'RECURRING' => 2, 'REPAIR_ADJ' => 3, 'USAGE' => 4, 'PARENT_SUMMARY' => 5, 'ITEM_ADJ' => 6, 'CBA_ADJ' => 7, 'CREDIT_ADJ' => 8, 'TAX' => 9 } # TODO: The pretty description has to be shared with the view @invoice.items.sort_by! do |ii| # Make sure not to compare nil (ArgumentError comparison of Array with Array failed) a = precedence[ii.item_type] || 100 b = (ii.pretty_plan_name.blank? || !ii.item_type.in?(%w[USAGE RECURRING]) ? ii.description : ii.pretty_plan_name) || '' [a, b] end fetch_payments = promise { @invoice.payments(true, true, 'FULL', ).map { |payment| Kaui::InvoicePayment.build_from_raw_payment(payment) } } fetch_pms = fetch_payments.then { |payments| Kaui::PaymentMethod.payment_methods_for_payments(payments, ) } fetch_invoice_fields = promise { @invoice.custom_fields('NONE', ).sort { |cf_a, cf_b| cf_a.name.downcase <=> cf_b.name.downcase } } fetch_payment_fields = promise do all_payment_fields = @account.all_custom_fields(:PAYMENT, 'NONE', ) all_payment_fields.each_with_object({}) do |entry, hsh| (hsh[entry.object_id] ||= []) << entry end end = promise { Kaui::TagDefinition.all_for_invoice_item() } = promise do = @account.(:INVOICE_ITEM, false, 'NONE', ) .each_with_object({}) do |entry, hsh| (hsh[entry.object_id] ||= []) << entry end end fetch_custom_fields_per_invoice_item = promise do custom_fields_per_invoice_item = @account.all_custom_fields(:INVOICE_ITEM, 'NONE', ) custom_fields_per_invoice_item.each_with_object({}) do |entry, hsh| (hsh[entry.object_id] ||= []) << entry end end = promise { @invoice.(false, 'NONE', ).sort } = promise { Kaui::TagDefinition.all_for_invoice() } @payments = wait(fetch_payments) @payment_methods = wait(fetch_pms) @custom_fields = wait(fetch_invoice_fields) @payment_custom_fields = wait(fetch_payment_fields) @custom_fields_per_invoice_item = wait(fetch_custom_fields_per_invoice_item) @tags_per_invoice_item = wait() @available_invoice_item_tags = wait() @invoice_tags = wait() @available_invoice_tags = wait() @available_invoice_tags.reject! { |td| td.name == 'WRITTEN_OFF' } if @invoice.status == 'VOID' end |
#show_html ⇒ Object
177 178 179 180 181 |
# File 'app/controllers/kaui/invoices_controller.rb', line 177 def show_html # rubocop:disable Rails/OutputSafety -- Invoice HTML from Kill Bill backend is trusted render html: Kaui::Invoice.as_html(params.require(:id), ).html_safe # rubocop:enable Rails/OutputSafety end |
#void_invoice ⇒ Object
rubocop:enable Lint/HashCompareByIdentity
155 156 157 158 159 160 161 162 163 164 165 |
# File 'app/controllers/kaui/invoices_controller.rb', line 155 def void_invoice = invoice = KillBillClient::Model::Invoice.find_by_id(params.require(:id), false, 'NONE', ) begin invoice.void(current_user.kb_username, params[:reason], params[:comment], ) redirect_to account_invoice_path(invoice.account_id, invoice.invoice_id), notice: 'Invoice successfully voided' rescue StandardError => e flash[:error] = "Unable to void invoice: #{as_string(e)}" redirect_to account_invoice_path(invoice.account_id, invoice.invoice_id) end end |