Fix the create invoice page
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 16 May 2019 19:56:41 +0000 (15:56 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 16 May 2019 19:56:41 +0000 (15:56 -0400)
disallow adding same invoice type more than once.

views/admin/billing/editInvoice.html

index 199ea6e..8026962 100644 (file)
                             {/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>
@@ -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(
             '<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>' +
@@ -469,7 +481,7 @@ jQuery(document).ready(function($){
                     '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 + '">' +
@@ -478,6 +490,8 @@ jQuery(document).ready(function($){
                     '<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
@@ -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() );