-
Notifications
You must be signed in to change notification settings - Fork 4
/
invoice_form.js
54 lines (49 loc) · 1.93 KB
/
invoice_form.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
$(document).on('click', '.removeRow', function(){
var numLines = $('.invoiceLine').length;
if (numLines > 1) {
$(this).closest('.invoiceLine').remove();
}
else
alert('Invoice needs at least one product.');
updateTotals();
});
function addRow() {
var newRow = $('.invoiceLines table:last tr:last').clone();
var lastLineNumber = $('.invoiceLines table:last tr:last').attr('id');
var newLineNumber = parseInt(lastLineNumber) + 1;
newRow.find('th >:first-child').each(function(){
var fieldName = $(this).attr('name');
if ( fieldName ) {
fieldName = fieldName.replace(lastLineNumber, newLineNumber.toString());
$(this).attr('name', fieldName);
}
})
$('.invoiceLines table').append('<tr class="invoiceLine" id=' + newLineNumber + '>' + newRow.html() + '</tr>');
updateLine($('.invoiceLine:last'));
updateTotals();
}
function updateLine($element) {
var line = $element.closest('.invoiceLine');
var unitPrice = parseFloat(line.find('.productCode option:selected').data('unitprice'));
var quantity = parseInt(line.find('.quantity').val());
line.find('.unitPrice').val(unitPrice.toFixed(2));
line.find('.creditAmount').val((unitPrice * quantity).toFixed(2));
updateTotals();
}
function updateAllLines() {
$('.invoiceLine').each(function() {
updateLine($(this));
});
}
function updateTotals() {
var netTotal = 0.0;
var payableTax = 0.0;
$('#invoiceLines').find('.invoiceLine').each(function(){
netTotal += parseFloat($(this).find('.creditAmount').val());
payableTax += parseFloat($(this).find('.creditAmount').val()) * 0.01 * parseFloat($(this).find('.taxId option:selected').data('taxpercentage'));
});
var grossTotal = netTotal + payableTax;
$('#taxPay').text(payableTax.toFixed(2));
$('#netTotal').text(netTotal.toFixed(2));
$('#grossTotal').text(grossTotal.toFixed(2));
}