first commit
This commit is contained in:
80
views/js/admin_order_fiscal.js
Normal file
80
views/js/admin_order_fiscal.js
Normal file
@@ -0,0 +1,80 @@
|
||||
$(document).ready(function() {
|
||||
const productsBody = $('#fiscal-products-body');
|
||||
const paymentsContainer = $('#fiscal-payments-container');
|
||||
const addPaymentBtn = $('#add-payment-btn');
|
||||
const paymentRowTemplate = $('#payment-row-template');
|
||||
let paymentIndex = 0; // A counter for unique payment input names
|
||||
|
||||
// --- Core Calculation Function ---
|
||||
function updateTotals() {
|
||||
let productsTotal = 0;
|
||||
productsBody.find('.product-row').each(function() {
|
||||
const quantity = parseFloat($(this).find('.fiscal-quantity').val()) || 0;
|
||||
const price = parseFloat($(this).find('.fiscal-price').val()) || 0;
|
||||
const rowTotal = quantity * price;
|
||||
$(this).find('.fiscal-row-total').text(rowTotal.toFixed(2));
|
||||
productsTotal += rowTotal;
|
||||
});
|
||||
|
||||
let discountsTotal = 0;
|
||||
$('.discount-row').each(function() {
|
||||
const discountValue = parseFloat($(this).find('.fiscal-discount-value').text().replace(',', '.')) || 0;
|
||||
discountsTotal += Math.abs(discountValue);
|
||||
});
|
||||
|
||||
const grandTotal = productsTotal - discountsTotal;
|
||||
|
||||
let paymentsTotal = 0;
|
||||
paymentsContainer.find('.payment-row').each(function() {
|
||||
const paymentAmount = parseFloat($(this).find('.payment-amount').val()) || 0;
|
||||
paymentsTotal += paymentAmount;
|
||||
});
|
||||
|
||||
// Update summary display
|
||||
$('#fiscal-products-total').text(productsTotal.toFixed(2));
|
||||
$('#fiscal-discounts-total').text(discountsTotal.toFixed(2));
|
||||
$('#fiscal-grand-total').text(grandTotal.toFixed(2));
|
||||
$('#fiscal-payments-total').text(paymentsTotal.toFixed(2));
|
||||
|
||||
const mismatchError = $('#fiscal-total-mismatch-error');
|
||||
const createCheckBtn = $('#create-fiscal-check-btn');
|
||||
|
||||
if (Math.abs(grandTotal - paymentsTotal) > 0.001) {
|
||||
mismatchError.show();
|
||||
createCheckBtn.prop('disabled', true);
|
||||
} else {
|
||||
mismatchError.hide();
|
||||
createCheckBtn.prop('disabled', grandTotal <= 0);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Event Handlers ---
|
||||
$('form').on('input', '.fiscal-quantity, .fiscal-price', updateTotals);
|
||||
paymentsContainer.on('input', '.payment-amount', updateTotals);
|
||||
|
||||
addPaymentBtn.on('click', function() {
|
||||
const newRow = $(paymentRowTemplate.html());
|
||||
newRow.find('.payment-type').attr('name', `payments[${paymentIndex}][type]`);
|
||||
newRow.find('.payment-amount').attr('name', `payments[${paymentIndex}][value]`);
|
||||
paymentsContainer.append(newRow);
|
||||
|
||||
if (paymentsContainer.find('.payment-row').length === 1) {
|
||||
const grandTotal = parseFloat($('#fiscal-grand-total').text());
|
||||
if (grandTotal > 0) {
|
||||
newRow.find('.payment-amount').val(grandTotal.toFixed(2));
|
||||
}
|
||||
}
|
||||
|
||||
paymentIndex++;
|
||||
updateTotals();
|
||||
});
|
||||
|
||||
paymentsContainer.on('click', '.remove-payment-btn', function() {
|
||||
$(this).closest('.payment-row').remove();
|
||||
updateTotals();
|
||||
});
|
||||
|
||||
// --- Initial State ---
|
||||
updateTotals();
|
||||
addPaymentBtn.trigger('click');
|
||||
});
|
||||
Reference in New Issue
Block a user