-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
executable file
·119 lines (113 loc) · 4.5 KB
/
admin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
jQuery(document).ready(function($) {
$('.widefat').DataTable({
paging: true,
searching: true,
ordering: true,
info: true,
order: [[0, 'desc']],
language: {
paginate: {
previous: '«',
next: '»'
},
search: 'Search:',
lengthMenu: 'Show _MENU_ entries',
info: 'Showing _START_ to _END_ of _TOTAL_ entries',
infoEmpty: 'No entries available',
infoFiltered: '(filtered from _MAX_ total entries)'
}
});
// Update Ledger
$(document).on('click', '.update-ledgers', function() {
let form = $(this).closest('form');
let orderId = form.find('.order_id').val();
let customerEmail = form.find('.customer_email').val();
let receivedAmount = form.find('.received_amount').val();
let orderTotal = form.find('#order_total_' + orderId).val();
if (!receivedAmount || isNaN(receivedAmount) || parseFloat(receivedAmount) <= 0) {
alert('Please enter a valid positive number for the received amount.');
return false;
}
$('#loader').show();
$.ajax({
url: ajaxJsData.ajax_url,
type: 'POST',
data: {
action: 'update_ledger',
order_id: orderId,
user_email: customerEmail,
received_amount: receivedAmount,
order_total: orderTotal
},
success: function(response) {
// $('#loader').hide(); // Hide loader
if (response.success) {
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('#success-message').text(response.data.message + ' Please wait while grid refreshed').show();
setTimeout(function() {
location.reload();
}, 2500);
} else {
alert('Error: ' + response.data.message);
}
},
error: function(xhr, status, error) {
alert('AJAX Error: ' + error);
}
});
});
// Display Ledger History in popup modal
$('.ledger-history').click(function() {
let orderId = $(this).closest('form').data('order-id');
let orderCurrencyCode = $(this).closest('form').find('.order_currency_code').val();
$.ajax({
url: ajaxJsData.ajax_url,
type: 'POST',
data: {
action: 'fetch_ledger_history',
order_id: orderId,
order_currency_code: orderCurrencyCode
},
success: function(response) {
if (response.success) {
$('#ledger-history-content').html(response.data);
$('#ledgerHistoryModal').fadeIn();
} else {
$('#ledger-history-content').html(response.data);
$('#ledgerHistoryModal').fadeIn();
}
},
error: function() {
$('#ledger-history-content').html('<p>Error loading ledger history.</p>');
}
});
});
$('.close-button').click(function() {
$('#ledgerHistoryModal').fadeOut();
});
$(window).click(function(event) {
if ($(event.target).is('#ledgerHistoryModal')) {
$('#ledgerHistoryModal').fadeOut();
}
});
// Download Ledger History CSV script
$('#download-csv').click(function() {
var csvContent = "data:text/csv;charset=utf-8,";
$('#ledger-history-content').find('table').each(function() {
var table = $(this);
var rows = table.find('tr').map(function() {
return $(this).find('td, th').map(function() {
return $(this).text();
}).get().join(',');
}).get().join('\n');
csvContent += rows + '\n\n';
});
var encodedUri = encodeURI(csvContent);
var link = document.createElement('a');
link.setAttribute('href', encodedUri);
link.setAttribute('download', 'ledger_history.csv');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
});