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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
|
# File 'app/controllers/kaui/account_timelines_controller.rb', line 36
def download
timeline = Kaui::AccountTimeline.find_by_account_id(params.require(:account_id), 'FULL', options_for_klient)
start_date = begin
Date.parse(params[:startDate])
rescue StandardError
nil
end
end_date = begin
Date.parse(params[:endDate])
rescue StandardError
nil
end
event_type = params[:eventType]
@account = timeline.account
@bundles = timeline.bundles
@invoices = timeline.invoices
@payments = timeline.payments
(@invoices)
@account_blocking_states = fetch_account_blocking_states(@account)
@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', 'Event 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.presence || '[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? ? Date.parse(transaction.effective_date).to_s : '[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|
effective_date = event.effective_date.present? ? Date.parse(event.effective_date).to_s : '[unknown]'
bundle_keys = @bundle_names[bundle.external_key]
audit_logs = event.audit_logs.present? ? event.audit_logs.map { |entry| Kaui::AuditLog.description(entry) }.join(', ') : ''
details = if event.event_type == 'SERVICE_STATE_CHANGE' || event.is_blocked_billing || event.is_blocked_entitlement
"#{event.service_name}/#{event.service_state_name} (billing=#{event.is_blocked_billing}, entitlement=#{event.is_blocked_entitlement})"
else
event.phase
end
csv << [effective_date, bundle_keys, event.event_type, details, audit_logs] if filter_date?(effective_date, start_date, end_date)
end
end
end
end
if %w[ENTITLEMENT ALL].include?(event_type)
@account_blocking_states.each do |blocking_state|
effective_date = blocking_state.effective_date.present? ? Date.parse(blocking_state.effective_date).to_s : '[unknown]'
audit_logs = blocking_state.audit_logs.present? ? blocking_state.audit_logs.map { |entry| Kaui::AuditLog.description(entry) }.join(', ') : ''
details = "#{blocking_state.service}/#{blocking_state.state_name} (billing=#{blocking_state.is_block_billing}, entitlement=#{blocking_state.is_block_entitlement}, change=#{blocking_state.is_block_change})"
csv << [effective_date, '', 'SERVICE_STATE_CHANGE', details, audit_logs] if filter_date?(effective_date, start_date, end_date)
end
end
end
send_data csv_string, filename: "account-timelines-#{Time.zone.today}.csv", type: 'text/csv'
end
|