{/if}
{$parent = $inv.parent.value}
{if $inv.parent.value == 0 && $inv.amount != '0.00'}
- <option value="{$inv.id}" data-price="{$inv.amount}" data-dynamic="{$inv.dynamic_amount.value}">{$inv.name}</option>
+ <option value="{$inv.id}" data-id="{$inv.id}" data-price="{$inv.amount}" data-dynamic="{$inv.dynamic_amount.value}">{$inv.name}</option>
{else if ($inv.parent.value == 0 && $inv.amount == '0.00' && $inv.dynamic_amount.value == 0)}
<optgroup label="{$inv.name}"></optgroup>
{else if ($inv.parent.value == 0 && $inv.amount == '0.00')}
- <option value="{$inv.id}" data-price="{$inv.amount}" data-dynamic="{$inv.dynamic_amount.value}">{$inv.name}</option>
+ <option value="{$inv.id}" data-id="{$inv.id}" data-price="{$inv.amount}" data-dynamic="{$inv.dynamic_amount.value}">{$inv.name}</option>
{else if ($inv.parent.value != 0)}
- <option value="{$inv.id}" data-price="{$inv.amount}" data-dynamic="{$inv.dynamic_amount.value}">{$inv.name}</option>
+ <option value="{$inv.id}" data-id="{$inv.id}" data-price="{$inv.amount}" data-dynamic="{$inv.dynamic_amount.value}">{$inv.name}</option>
{/if}
{/foreach}
</select>
*/
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;
}
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 ) {
{
lineItem.qty = parseInt(1);
line_items.push( lineItem );
+ var uid = line_items.length - 1;
$( '#invoice-line-items' ). append(
'<div class="glm-row" id="line-item-type-' + lineItem.id + '">' +
'<div class="glm-columns glm-small-1 line-item-delete dashicons dashicons-trash" data-id="' + lineItem.id + '"></div>' +
'Discount ' + lineItem.discount + '%' +
'</div>' +
'<div class="glm-columns glm-small-1">' +
- '<input type="number" min="1" data-id="' + lineItem.id + '" name="line_item_qty[' + lineItem.id + ']" value="1">' +
+ '<input type="number" min="1" data-id="' + lineItem.id + '" data-uid="' + uid + '" name="line_item_qty[' + lineItem.id + ']" value="1">' +
'</div>' +
'<div class="glm-columns glm-small-2">' +
'<input type="hidden" name="line_items[]" value="' + lineItem.id + '">' +
'<input type="hidden" name="line_item_discount[' + lineItem.id + ']" value="' + lineItem.discount + '">' + '$' + lineItem.amount +
'</div>' +
'</div>');
+ // 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
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++ ) {
if ( msg.fieldFail.account_number ) {
alert( 'Account Number must be unique!' );
}
- console.log( 'return', msg );
+ // console.log( 'return', msg );
}
});
}
* 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() );