Skip to content

Commit

Permalink
Merge pull request #414 from tungleduyxyz/technical-support-137
Browse files Browse the repository at this point in the history
Fix bugs and code improvement
  • Loading branch information
pierre authored Oct 2, 2024
2 parents 6087ed9 + a1c5c6e commit 53acbc1
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 127 deletions.
8 changes: 7 additions & 1 deletion app/controllers/kaui/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ def pagination
end

def download
columns = params.require(:columnsString).split(',').map { |attr| attr.split.join('_').downcase }
start_date = params[:startDate]
end_date = params[:endDate]
all_fields_checked = params[:allFieldsChecked] == 'true'

columns = if all_fields_checked
KillBillClient::Model::AccountAttributes.instance_variable_get('@json_attributes')
else
params.require(:columnsString).split(',').map { |attr| attr.split.join('_').downcase }
end
start_date = begin
Date.parse(start_date)
rescue StandardError
Expand Down
55 changes: 28 additions & 27 deletions app/controllers/kaui/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ 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 }
all_fields_checked = params[:allFieldsChecked] == 'true'
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
Expand Down Expand Up @@ -55,32 +61,27 @@ def pagination
end

account_id = (params[:search] || {})[:value]
if account_id.blank?
# Don't show amount and balance, and they will not be populated
data_extractor = lambda do |invoice, column|
[
invoice.invoice_number.to_i,
invoice.invoice_date
][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.invoice_search_columns.call(invoice, view_context, cached_options_for_klient)[1]
row
end
else
data_extractor = lambda do |invoice, column|
[
invoice.invoice_number.to_i,
invoice.invoice_date,
invoice.amount,
invoice.balance,
invoice.status
][column]
end
formatter = lambda do |invoice|
Kaui.account_invoices_columns.call(invoice, view_context)[1]
end
data_extractor = if account_id.blank?
lambda do |invoice, column|
[
invoice.invoice_number.to_i,
invoice.invoice_date
][column]
end
else
lambda do |invoice, column|
[
invoice.invoice_number.to_i,
invoice.invoice_date,
invoice.amount,
invoice.balance,
invoice.status
][column]
end
end

formatter = lambda do |invoice|
Kaui.account_invoices_columns.call(invoice, view_context)[1]
end

paginate searcher, data_extractor, formatter
Expand Down
13 changes: 11 additions & 2 deletions app/controllers/kaui/payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ 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 }
all_fields_checked = params[:allFieldsChecked] == 'true'
columns = if all_fields_checked
KillBillClient::Model::PaymentAttributes.instance_variable_get('@json_attributes') - %w[transactions audit_logs]
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
Expand All @@ -28,11 +34,12 @@ def download
else
payments = Kaui::Payment.list_or_search(nil, 0, MAXIMUM_NUMBER_OF_RECORDS_DOWNLOAD, options_for_klient.merge(params: kb_params))
end

payments.each do |payment|
created_date = nil
payment.transactions.each do |transaction|
transaction_date = Date.parse(transaction.effective_date)
created_date = transaction_date if created_date.nil? || (transaction_date < created_date)
created_date ||= transaction_date if transaction_date < created_date
end
payment.payment_date = created_date
end
Expand All @@ -41,6 +48,8 @@ def download
csv << columns

payments.each do |payment|
next if start_date && end_date && (payment.payment_date < Date.parse(start_date) || payment.payment_date > Date.parse(end_date))

data = columns.map do |attr|
case attr
when 'payment_number'
Expand Down
2 changes: 2 additions & 0 deletions app/models/kaui/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Kaui
class Invoice < KillBillClient::Model::Invoice
TABLE_IGNORE_COLUMNS = %w[amount balance credit_adj refund_adj items is_parent_invoice parent_invoice_id parent_account_id].freeze

def self.build_from_raw_invoice(raw_invoice)
result = Kaui::Invoice.new
KillBillClient::Model::InvoiceAttributes.instance_variable_get('@json_attributes').each do |attr|
Expand Down
28 changes: 17 additions & 11 deletions app/views/kaui/account_timelines/_multi_functions_bar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="downloadCsvModalLabel">Download</h5>
<h3 class="modal-title" id="downloadCsvModalLabel">Download</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
Expand All @@ -19,13 +19,13 @@
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="startDate">Start Date:</label>
<label for="startDate">Effective Date:</label>
<input type="text" class="form-control" id="startDate" name="startDate">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="endDate">End Date:</label>
<label for="endDate">To:</label>
<input type="text" class="form-control" id="endDate" name="endDate">
</div>
</div>
Expand All @@ -39,7 +39,7 @@
</div>
<div>
<input type="radio" id="allData" name="download_option" value="all">
<label for="allData">All logs</label>
<label for="allData">All events</label>
</div>
<div>
<input type="radio" id="thisWeek" name="download_option" value="thisWeek">
Expand Down Expand Up @@ -98,13 +98,15 @@ $(document).ready(function() {
});

$('#downloadCsvModal').on('show.bs.modal', function (e) {
$('#customDate').prop('checked', true);
$('#startDate, #endDate').prop('disabled', false);
$('#allData').prop('checked', true);
$('#startDate, #endDate').prop('disabled', true);
$('#startDate').val(null);
$('#endDate').val(null);
});

$('#allData').change(function() {
$('#startDate').val(null);
$('#endDate').val(null);
var isChecked = $(this).is(':checked');
$('#startDate, #endDate').prop('disabled', true);
});
Expand All @@ -113,7 +115,11 @@ $(document).ready(function() {
var currentDate = new Date();
var startDate, endDate;

if (option === "week") {
if (option === "day") {
startDate = new Date();
endDate = new Date();
endDate.setDate(endDate.getDate() + 1);
} else if (option === "week") {
startDate = new Date(currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 1));
currentDate = new Date();
endDate = new Date(currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 7));
Expand Down Expand Up @@ -152,10 +158,10 @@ $(document).ready(function() {
});

$('#customDate').change(function() {
var isChecked = $(this).is(':checked');
$('#startDate, #endDate').prop('disabled', false);
$('#startDate').val(null);
$('#endDate').val(null);
if ($(this).is(':checked')) {
setDateRange("day");
$('#startDate, #endDate').prop('disabled', false);
}
});

var downloadButton = document.getElementById('downloadButton');
Expand Down
62 changes: 42 additions & 20 deletions app/views/kaui/accounts/_multi_functions_bar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="downloadCsvModalLabel">Download</h5>
<h3 class="modal-title" id="downloadCsvModalLabel">Download</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
Expand All @@ -35,13 +35,13 @@
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="startDate">Start Date:</label>
<label for="startDate">Account Creation Date From:</label>
<input type="text" class="form-control" id="startDate" name="startDate">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="endDate">End Date:</label>
<label for="endDate">To:</label>
<input type="text" class="form-control" id="endDate" name="endDate">
</div>
</div>
Expand All @@ -54,8 +54,8 @@
<label for="customDate">Custom date</label>
</div>
<div>
<input type="radio" id="allAccounts" name="download_option" value="all">
<label for="allAccounts">All accounts</label>
<input type="radio" id="allData" name="download_option" value="all">
<label for="allData">All accounts</label>
</div>
<div>
<input type="radio" id="thisWeek" name="download_option" value="thisWeek">
Expand All @@ -72,6 +72,19 @@
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h5>Additional Options</h5>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="allFields" name="allFields">
<label class="form-check-label" for="allFields">All fields</label>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
Expand Down Expand Up @@ -150,13 +163,13 @@ $(document).ready(function() {
});

$('#downloadCsvModal').on('show.bs.modal', function (e) {
$('#allAccounts').prop('checked', true);
$('#allData').prop('checked', true);
$('#startDate, #endDate').prop('disabled', true);
$('#startDate').val(null);
$('#endDate').val(null);
});

$('#allAccounts').change(function() {
$('#allData').change(function() {
var isChecked = $(this).is(':checked');
$('#startDate, #endDate').prop('disabled', true);
});
Expand All @@ -165,7 +178,11 @@ $(document).ready(function() {
var currentDate = new Date();
var startDate, endDate;

if (option === "week") {
if (option === "day") {
startDate = new Date();
endDate = new Date();
endDate.setDate(endDate.getDate() + 1);
} else if (option === "week") {
startDate = new Date(currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 1));
currentDate = new Date();
endDate = new Date(currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 7));
Expand Down Expand Up @@ -204,32 +221,37 @@ $(document).ready(function() {
});

$('#customDate').change(function() {
var isChecked = $(this).is(':checked');
$('#startDate, #endDate').prop('disabled', false);
$('#startDate').val(null);
$('#endDate').val(null);
if ($(this).is(':checked')) {
setDateRange("day");
$('#startDate, #endDate').prop('disabled', false);
}
});

var downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
event.preventDefault(); // Prevent the default form submission if the button is a submit type
event.preventDefault();

// Retrieve the values and checked state
var allFieldsChecked = document.getElementById('allFields').checked;
var startDate = $('#startDate').val();
var endDate = $('#endDate').val();
var downloadAll = $('#allAccounts').is(':checked');
var downloadAll = $('#allData').is(':checked');
var thElements = document.querySelectorAll('#accounts-table th');
var columnTitles = Array.from(thElements).map(function(th) {
return th.textContent.trim();
});

var columnsString = columnTitles.join(',')
if (downloadAll) {
window.open("<%= download_accounts_path %>?columnsString="+columnsString, '_blank');
} else {
window.open("<%= download_accounts_path %>?columnsString="+columnsString+"&startDate="+startDate+"&endDate="+endDate, '_blank');
var url = new URL("<%= download_accounts_path %>", window.location.origin);
var params = new URLSearchParams();
params.append('columnsString', columnsString);
if (!downloadAll) {
params.append('startDate', startDate);
params.append('endDate', endDate);
}
params.append('allFieldsChecked', allFieldsChecked);
url.search = params.toString();
console.log(url.toString());
window.open(url.toString(), '_blank');
});
}

Expand Down
Loading

0 comments on commit 53acbc1

Please sign in to comment.