function recalculateTotals() { var total_amount_outstanding = 0; $('.auctionlot-checkbox').each(function(item, index) { if ($(this).prop('checked')) { total_amount_outstanding += Number($(this).data('amount')); } }); var totals = getTotals(); $('#total-auctionlots').html(currency(total_amount_outstanding)); $('#total-surcharge').html(currency(totals.surcharge)); $('#total-payments').html(currency(totals.payments + totals.surcharge)); $('#total-remaining').html(currency((Math.round((total_amount_outstanding+totals.surcharge)*100) - Math.round((totals.payments+totals.surcharge)*100))/100)); if (Math.round(total_amount_outstanding * 100) === Math.round(totals.payments * 100)) { $('.payment-warn').hide(); $('.payment-error').hide(); $('.payment-add').show(); } else if (totals.payments > 0 && total_amount_outstanding > totals.payments) { $('.payment-warn').show(); $('.payment-error').hide(); $('.payment-add').show(); } else { $('.payment-warn').hide(); $('.payment-error').show(); $('.payment-add').hide(); } } function getTotals() { var totals = { payments: 0, surcharge: 0, surcharges: [] }; $('.payment-row').each(function(item, index) { var payment_type = $(this).find('.payment-type').html(); var payment_amount = Number($(this).find('.payment-amount').val()); var surcharge_percentage = Number($(this).find('.surcharge-percentage').val()); var surcharge_amount = 0; if (Number(payment_amount) && Number(surcharge_percentage) > 0) { surcharge_amount = payment_amount * (surcharge_percentage / 100); totals.surcharge += surcharge_amount; totals.surcharges.push({ 'payment_type': payment_type, 'surcharge_amount': surcharge_amount }); } $(this).find('.surcharge-amount').html(currency(surcharge_amount)); totals.payments += payment_amount; }); return totals; } function currency(value) { var sign = value < 0 ? "-" : ""; var i = String(parseInt(value = Math.abs(Number(value) || 0).toFixed(2))); var j = (j = i.length) > 3 ? j % 3 : 0; return sign + "£" + (j ? i.substr(0, j) + "," : "") + i.substr(j).replace(/(\decSep{3})(?=\decSep)/g, "$1" + ",") + (2 ? "." + Math.abs(value - i).toFixed(2).slice(2) : ""); } $(function() { $('.auctionlot-checkbox').change(function(e) { var $auctionlot_row = $('#auctionlot-' + $(this).data('auctionlot-id')); var $parent = $('#auctionlot-' + $(this).data('auctionlot-id')).parent(); if ($(this).prop('checked')) { $auctionlot_row.show(); $auctionlot_row.remove(); $parent.append($auctionlot_row); $auctionlot_row.find('input').attr('disabled', false); } else { $auctionlot_row.hide(); $auctionlot_row.find('input').attr('disabled', true); } recalculateTotals(); }); $('.buyerinvoice-confirm').click(function(e) { e.preventDefault(); recalculateTotals(); var totals = getTotals(); if (!totals.surcharges.length) { $('#invoice-form').submit(); return; } var content = '
Please confirm that you have accepted the following additional surcharges:
' + 'Payment Type | ' + 'Surcharge Amount | ' + '
---|---|
' + value.payment_type + ' | ' + '' + currency(value.surcharge_amount) + ' | ' + '
TOTAL | ' + '' + currency(surcharge_total) + ' | ' + '