From ff6d079bd158e4d02a05b8b8afdf8c38842c9ea8 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 16 May 2019 15:56:41 -0400 Subject: [PATCH] Fix the create invoice page disallow adding same invoice type more than once. --- views/admin/billing/editInvoice.html | 45 ++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/views/admin/billing/editInvoice.html b/views/admin/billing/editInvoice.html index 199ea6e..8026962 100644 --- a/views/admin/billing/editInvoice.html +++ b/views/admin/billing/editInvoice.html @@ -222,13 +222,13 @@ {/if} {$parent = $inv.parent.value} {if $inv.parent.value == 0 && $inv.amount != '0.00'} - + {else if ($inv.parent.value == 0 && $inv.amount == '0.00' && $inv.dynamic_amount.value == 0)} {else if ($inv.parent.value == 0 && $inv.amount == '0.00')} - + {else if ($inv.parent.value != 0)} - + {/if} {/foreach} @@ -377,6 +377,8 @@ jQuery(document).ready(function($){ */ function totalInvoice() { var totalAmount = 0.00; + // console.table( line_items ); + // console.table( invoiceTypeJSON ); for ( var i = 0; i < line_items.length; ++i ) { totalAmount += parseFloat( line_items[i].amount ) * line_items[i].qty; } @@ -415,23 +417,32 @@ jQuery(document).ready(function($){ function addLineItem() { var valid = true; allFields.removeClass( 'ui-state-error' ); + var isDynamicInvoiceType = false; valid = valid && checkRequired( line_item_type, lineTips, 'Invoice Type is required!' ); - if ( valid ) { // console.log( 'line_item_type', line_item_type ); - var selectedLineItem = invoiceTypeJSON[line_item_type.val()]; + var selectedLineItem = $.extend({}, invoiceTypeJSON[line_item_type.val()] ); + // console.log( 'selectedLineItem', selectedLineItem ); + if ( selectedLineItem.dynamic_amount.value ) { + isDynamicInvoiceType = true; + var dynamic_price = $( 'input[name="dynamic_price"]' ); // Dynamic price line item + valid = valid && checkRequired( dynamic_price, lineTips, 'Amount is required!' ); + } + } + + if ( valid ) { // If selectedLineItem is undefined then we must be adding new line item if ( selectedLineItem == undefined ) { // Check that name and amount are not empty - // valid = valid && checkRequired( } else { + // console.log( 'isDynamicInvoiceType:', isDynamicInvoiceType ); // Check first to see if this line_item_type is already in line_items. - if ( !isLineItem( selectedLineItem ) ) { - var dynamic_price = $( 'input[name="dynamic_price"]' ).val(); // Dynamic price line item - console.log( 'dynamic_price:', dynamic_price ); + // Only for invoice types that are not dynamic amounts + if ( isDynamicInvoiceType || ( !isDynamicInvoiceType && !isLineItem( selectedLineItem ) ) ) { + // var dynamic_price = $( 'input[name="dynamic_price"]' ).val(); // Dynamic price line item if ( dynamic_price ) { - selectedLineItem.amount = parseFloat( dynamic_price ); + selectedLineItem.amount = parseFloat( dynamic_price.val() ); } // See if there's a discount if ( discount_amount ) { @@ -461,6 +472,7 @@ jQuery(document).ready(function($){ { lineItem.qty = parseInt(1); line_items.push( lineItem ); + var uid = line_items.length - 1; $( '#invoice-line-items' ). append( '
' + '
' + @@ -469,7 +481,7 @@ jQuery(document).ready(function($){ 'Discount ' + lineItem.discount + '%' + '
' + '
' + - '' + + '' + '
' + '
' + '' + @@ -478,6 +490,8 @@ jQuery(document).ready(function($){ '' + '$' + lineItem.amount + '
' + ''); + // remove the invoice type option from #line_item_type select + $('#line_item_type option[data-id="'+ lineItem.id +'"]').attr('disabled', 'disabled'); } // Setup if doing edit @@ -516,6 +530,7 @@ jQuery(document).ready(function($){ var selectedLineItem = invoiceTypeJSON[$(this).data( 'id' )]; selectedLineItem.amount = selectedLineItem.original_amount; + $('#line_item_type option[data-id="'+ $(this).data( 'id' ) +'"]').removeAttr('disabled'); // Need to remove from line_items also. // Loop through the line_items and remove the one with matching id. for ( i = 0; i < line_items.length; i++ ) { @@ -568,7 +583,7 @@ jQuery(document).ready(function($){ if ( msg.fieldFail.account_number ) { alert( 'Account Number must be unique!' ); } - console.log( 'return', msg ); + // console.log( 'return', msg ); } }); } @@ -900,8 +915,12 @@ jQuery(document).ready(function($){ * So the line_items array gets updated with the qty number. */ $('#invoice-line-items').on('change', 'input[name^="line_item_qty"]', function(){ - var itemId = $(this).data('id'); + var itemId = $(this).data('id'); + var itemUid = $(this).data('uid'); + // console.table( itemId ); // Update the qty for this line item in the json object + // Be sure to use the index or the line_items not the id + // line_items[itemUid].qty = parseInt( $(this).val() ); for ( i = 0; i < line_items.length; i++ ) { if ( line_items[i].id == itemId ) { line_items[i].qty = parseInt( $(this).val() ); -- 2.17.1