Skip to content

Commit

Permalink
Fix bugs and code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
tungleduyxyz committed Sep 22, 2024
1 parent 6087ed9 commit 5da80f1
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 89 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
8 changes: 7 additions & 1 deletion 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') - %w[amount balance]
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
14 changes: 12 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,9 @@ def download
csv << columns

payments.each do |payment|
if start_date && end_date && (payment.payment_date < Date.parse(start_date) || payment.payment_date > Date.parse(end_date))
next
end
data = columns.map do |attr|
case attr
when 'payment_number'
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">Reference 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
26 changes: 16 additions & 10 deletions app/views/kaui/audit_logs/_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">Created 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 Down Expand Up @@ -133,13 +133,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 @@ -148,7 +150,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 @@ -187,10 +193,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
Loading

0 comments on commit 5da80f1

Please sign in to comment.