Skip to content

Commit

Permalink
Merge pull request #412 from tungleduyxyz/technical-support-137
Browse files Browse the repository at this point in the history
Fix table bugs and add Download CSV button
  • Loading branch information
pierre committed Sep 18, 2024
2 parents 9be6380 + 080299b commit 6087ed9
Show file tree
Hide file tree
Showing 20 changed files with 1,865 additions and 242 deletions.
121 changes: 121 additions & 0 deletions app/controllers/kaui/account_timelines_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'csv'

module Kaui
class AccountTimelinesController < Kaui::EngineController
def show
Expand Down Expand Up @@ -30,8 +32,127 @@ def show
@selected_bundle = params.key?(:external_key) ? @bundle_names[params[:external_key]] : nil
end

def download
timeline = Kaui::AccountTimeline.find_by_account_id(params.require(:account_id), 'FULL', options_for_klient)
start_date = params[:startDate]
end_date = params[:endDate]
start_date = begin
Date.parse(start_date)
rescue StandardError
nil
end
end_date = begin
Date.parse(end_date)
rescue StandardError
nil
end
start_date = params[:startDate].present? ? Date.parse(params[:startDate]) : nil
end_date = params[:endDate].present? ? Date.parse(params[:endDate]) : nil

event_type = params[:eventType]
@account = timeline.account
@bundles = timeline.bundles
@invoices = timeline.invoices
@payments = timeline.payments
extract_invoices_by_id(@invoices)

# Lookup all bundle names
@bundle_names = {}
@bundle_names_by_invoice_id = {}
@bundle_keys_by_invoice_id = {}
@bundles.each do |bundle|
load_bundle_name_for_timeline(bundle.external_key)
end
@invoices.each do |invoice|
@bundle_names_by_invoice_id[invoice.invoice_id] = Set.new
@bundle_keys_by_invoice_id[invoice.invoice_id] = Set.new
(invoice.bundle_keys || '').split(',').each do |bundle_key|
load_bundle_name_for_timeline(bundle_key)
@bundle_names_by_invoice_id[invoice.invoice_id] << @bundle_names[bundle_key]
@bundle_keys_by_invoice_id[invoice.invoice_id] << bundle_key
end
end

@selected_bundle = params.key?(:external_key) ? @bundle_names[params[:external_key]] : nil

csv_string = CSV.generate(headers: true) do |csv|
csv << ['Effective Date', 'Bundles', 'Even Type', 'Details', 'Reason Code/ Comments']
if %w[INVOICE ALL].include?(event_type)
@invoices.each do |invoice_stub|
invoice = invoice_stub.invoice_id.present? && @invoices_by_id.key?(invoice_stub.invoice_id) ? @invoices_by_id[invoice_stub.invoice_id] : invoice_stub
target_date = invoice.target_date.present? ? invoice.target_date : '[unknown]'
bundle_keys = invoice_stub.bundle_keys.present? ? invoice_stub.bundle_keys.split(',').map { |bundle_key| @bundle_names[bundle_key] }.join(', ') : ''
invoice_details = []
invoice_details << "Amount: #{invoice.amount_to_money} (#{@account.currency})"
invoice_details << "Balance: #{invoice.balance_to_money} (#{@account.currency})"
invoice_details << "Credit adjustment: #{invoice.credit_adjustment_to_money} (#{@account.currency})" if invoice.credit_adj.present? && invoice.credit_adj.positive?
invoice_details << "Refund adjustment: #{invoice.refund_adjustment_to_money} (#{@account.currency})" if invoice.refund_adj.present? && invoice.refund_adj.negative?
invoice_details << "Invoice #: #{invoice.invoice_number}"
audit_logs = invoice_stub.audit_logs.present? ? invoice_stub.audit_logs.map { |entry| Kaui::AuditLog.description(entry) }.join(', ') : ''
csv << [target_date, bundle_keys, 'INVOICE', invoice_details.join('; '), audit_logs] if filter_date(target_date, start_date, end_date)
end
end
if %w[PAYMENT ALL].include?(event_type)
@payments.each do |payment|
invoice = if payment.target_invoice_id.present?
@invoices_by_id[payment.target_invoice_id]
else
nil
end

payment.transactions.each do |transaction|
effective_date = transaction.effective_date.present? ? transaction.effective_date : '[unknown]'
bundle_keys = @bundle_keys_by_invoice_id[payment.target_invoice_id].present? ? @bundle_keys_by_invoice_id[payment.target_invoice_id].map { |bundle_key| @bundle_names[bundle_key] }.join(', ') : ''
transaction_type = transaction.transaction_type
details = []
details << "Amount: #{Kaui::Transaction.amount_to_money(transaction)} (#{transaction.currency})" unless transaction.transaction_type == 'VOID'
details << "Status: #{transaction.status}"
details << "Payment #: #{payment.payment_number}"
details << "Invoice #: #{invoice.invoice_number}" unless invoice.nil?

audit_logs = transaction.audit_logs.present? ? transaction.audit_logs.map { |entry| Kaui::AuditLog.description(entry) }.chunk { |x| x }.map(&:first).join(', ') : ''

csv << [effective_date, bundle_keys, transaction_type, details.join('; '), audit_logs] if filter_date(effective_date, start_date, end_date)
end
end
end

if %w[ENTITLEMENT ALL].include?(event_type)
@bundles.each do |bundle|
bundle.subscriptions.each do |sub|
sub.events.each do |event|
# Skip SERVICE_STATE_CHANGE events
next if event.event_type == 'SERVICE_STATE_CHANGE'

effective_date = event.effective_date.present? ? event.effective_date : '[unknown]'
bundle_keys = @bundle_names[bundle.external_key]
event_type = event.event_type
phase = event.phase
audit_logs = event.audit_logs.present? ? event.audit_logs.map { |entry| Kaui::AuditLog.description(entry) }.join(', ') : ''

csv << [effective_date, bundle_keys, event_type, phase, audit_logs] if filter_date(effective_date, start_date, end_date)
end
end
end
end
end

send_data csv_string, filename: "account-timelines-#{Date.today}.csv", type: 'text/csv'
end

private

def filter_date(target_date, start_date, end_date)
return true if start_date.nil? || end_date.nil?

target_date = begin
Date.parse(target_date)
rescue StandardError
nil
end
target_date >= start_date && target_date <= end_date
end

def load_bundle_name_for_timeline(bundle_key)
@bundle_names[bundle_key] ||= Kaui.bundle_key_display_string.call(bundle_key)
end
Expand Down
36 changes: 35 additions & 1 deletion app/controllers/kaui/accounts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'csv'
module Kaui
class AccountsController < Kaui::EngineController
def index
Expand All @@ -16,6 +17,8 @@ def index
return
end

@dropdown_default = default_columns(Kaui.account_search_columns.call[2], Kaui::Account::SENSIVITE_DATA_FIELDS)

@ordering = params[:ordering] || (@search_query.blank? ? 'desc' : 'asc')
@offset = params[:offset] || 0
@limit = params[:limit] || 50
Expand All @@ -42,7 +45,38 @@ def pagination
Kaui.account_search_columns.call(account, view_context)[1]
end

paginate searcher, data_extractor, formatter
paginate searcher, data_extractor, formatter, default_columns(Kaui.account_search_columns.call[2], Kaui::Account::SENSIVITE_DATA_FIELDS)
end

def download
columns = params.require(:columnsString).split(',').map { |attr| attr.split.join('_').downcase }
start_date = params[:startDate]
end_date = params[:endDate]
start_date = begin
Date.parse(start_date)
rescue StandardError
nil
end
end_date = begin
Date.parse(end_date)
rescue StandardError
nil
end
accounts = Kaui::Account.list_or_search(nil, 0, MAXIMUM_NUMBER_OF_RECORDS_DOWNLOAD, options_for_klient)

csv_string = CSV.generate(headers: true) do |csv|
csv << columns
accounts.each do |account|
change_date = Date.parse(account.reference_time)
data = columns.map do |attr|
account&.send(attr.downcase)
end
next if start_date && end_date && change_date && (change_date < start_date || change_date > end_date)

csv << data
end
end
send_data csv_string, filename: "accounts-#{Date.today}.csv", type: 'text/csv'
end

def new
Expand Down
37 changes: 37 additions & 0 deletions app/controllers/kaui/audit_logs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'csv'

module Kaui
class AuditLogsController < Kaui::EngineController
OBJECT_WITH_HISTORY = %w[ACCOUNT ACCOUNT_EMAIL BLOCKING_STATES BUNDLE CUSTOM_FIELD INVOICE INVOICE_ITEM PAYMENT_ATTEMPT PAYMENT PAYMENT_METHOD SUBSCRIPTION SUBSCRIPTION_EVENT TRANSACTION TAG TAG_DEFINITION].freeze
Expand Down Expand Up @@ -42,6 +44,41 @@ def index
@audit_logs_json = @audit_logs_json.to_json
end

def download
account_id = params.require(:account_id)
start_date = params[:startDate]
end_date = params[:endDate]
start_date = begin
Date.parse(start_date)
rescue StandardError
nil
end
end_date = begin
Date.parse(end_date)
rescue StandardError
nil
end

account = Kaui::Account.find_by_id_or_key(account_id, false, false, options_for_klient)
audit_logs = account.audit(options_for_klient)

csv_file = CSV.generate do |csv|
csv << Kaui.account_audit_logs_columns.call[0]
audit_logs.each do |log|
change_date = begin
Date.parse(log.change_date)
rescue StandardError
nil
end
next if start_date && end_date && change_date && !(change_date > start_date && change_date < end_date)

csv << [log.change_date, log.object_id, log.object_type, log.change_type, log.changed_by, log.reason_code, log.comments, log.user_token]
end
end

send_data csv_file, type: 'text/csv', filename: "audit_logs_#{account_id}.csv"
end

def history
json_response do
account_id = params.require(:account_id)
Expand Down
10 changes: 8 additions & 2 deletions app/controllers/kaui/engine_controller_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Kaui
module EngineControllerUtil
# See DefaultPaginationSqlDaoHelper.java
SIMPLE_PAGINATION_THRESHOLD = 20_000
MAXIMUM_NUMBER_OF_RECORDS_DOWNLOAD = 1000

protected

Expand All @@ -13,7 +14,7 @@ def get_layout
end
# rubocop:enable Lint/UselessAssignment, Naming/AccessorMethodName

def paginate(searcher, data_extractor, formatter)
def paginate(searcher, data_extractor, formatter, table_default_columns = [])
search_key = (params[:search] || {})[:value].presence
offset = (params[:start] || 0).to_i
limit = (params[:length] || 10).to_i
Expand All @@ -30,7 +31,8 @@ def paginate(searcher, data_extractor, formatter)
# We need to fill-in a number to make DataTables happy
recordsTotal: pages.nil? ? 0 : (pages.pagination_max_nb_records || SIMPLE_PAGINATION_THRESHOLD),
recordsFiltered: pages.nil? ? 0 : (pages.pagination_total_nb_records || SIMPLE_PAGINATION_THRESHOLD),
data: []
data: [],
columns: table_default_columns
}
json[:error] = error unless error.nil?

Expand Down Expand Up @@ -151,5 +153,9 @@ def json_response
end
render json: response, status: response_status
end

def default_columns(fields, sensivite_fields)
fields.map { |field| { data: fields.index(field), visible: !(sensivite_fields.include? field) } }
end
end
end
32 changes: 28 additions & 4 deletions app/controllers/kaui/invoices_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'csv'
module Kaui
class InvoicesController < Kaui::EngineController
def index
Expand All @@ -12,6 +13,31 @@ def index
@max_nb_records = @search_query.blank? ? Kaui::Invoice.list_or_search(nil, 0, 0, options_for_klient).pagination_max_nb_records : 0
end

def download
account_id = params[:account_id]
start_date = params[:startDate]
end_date = params[:endDate]
columns = params.require(:columnsString).split(',').map { |attr| attr.split.join('_').downcase }
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
if account_id.present?
account = Kaui::Account.find_by_id_or_key(account_id, false, false, options_for_klient)
invoices = account.invoices(options_for_klient.merge(params: kb_params))
else
invoices = Kaui::Invoice.list_or_search(nil, 0, MAXIMUM_NUMBER_OF_RECORDS_DOWNLOAD, options_for_klient.merge(params: kb_params))
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-#{Date.today}.csv", type: 'text/csv'
end

def pagination
cached_options_for_klient = options_for_klient

Expand All @@ -24,7 +50,7 @@ def pagination
if account.nil?
Kaui::Invoice.list_or_search(search_key, offset, limit, cached_options_for_klient)
else
Kaui::Account.paginated_invoices(search_key, offset, limit, 'NONE', cached_options_for_klient.merge({ params: { includeVoidedInvoices: true } })).map! { |invoice| Kaui::Invoice.build_from_raw_invoice(invoice) }
Kaui::Account.paginated_invoices(search_key, offset, limit, 'NONE', cached_options_for_klient).map! { |invoice| Kaui::Invoice.build_from_raw_invoice(invoice) }
end
end

Expand Down Expand Up @@ -53,9 +79,7 @@ def pagination
][column]
end
formatter = lambda do |invoice|
row = [view_context.link_to(invoice.invoice_number, view_context.url_for(controller: :invoices, action: :show, account_id: invoice.account_id, id: invoice.invoice_id))]
row += Kaui.account_invoices_columns.call(invoice, view_context)[1]
row
Kaui.account_invoices_columns.call(invoice, view_context)[1]
end
end

Expand Down
Loading

0 comments on commit 6087ed9

Please sign in to comment.