DB Update, work on add invoice page.
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 7 Mar 2018 19:56:03 +0000 (14:56 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 7 Mar 2018 19:56:03 +0000 (14:56 -0500)
Update db table line_item.
- Add three date fields
Working on being able to delete a line item once on the add invoice
form.

classes/billingSupport.php
classes/data/dataAccounts.php
index.php
models/admin/billing/invoices.php
setup/databaseScripts/create_database_V0.0.10.sql
setup/databaseScripts/dbVersions.php
setup/databaseScripts/update_database_V0.0.11.sql [new file with mode: 0644]
views/admin/billing/editInvoice.html

index 534ecce..ec82396 100644 (file)
@@ -129,6 +129,7 @@ class GlmBillingSupport
             }
         }
     }
+
     public function createPayment( $account, $payment )
     {
         // Insert the payment
@@ -439,7 +440,7 @@ class GlmBillingSupport
      * Get all unpaid invoices given an account id.
      *
      * @param mixed $account Id of the account.
- *
    *
      * @access public
      * @return array
      */
@@ -606,7 +607,6 @@ class GlmBillingSupport
         return $line_items;
     }
 
-
     /**
      * Merge template and data to produce HTML
      *
index ab2977e..763ce68 100644 (file)
@@ -267,7 +267,7 @@ class GlmDataAccounts extends GlmDataAbstract
             } else {
                 // Add one year to the anniversary date
                 $anniversary_date->modify('+1 year');
-                $r['next_anniversary_date'] = $anniversary_date->format('m-d-Y');
+                $r['next_anniversary_date'] = $anniversary_date->format('m/d/Y');
             }
         }
         return $r;
index 8d0c8f7..6d7b784 100644 (file)
--- a/index.php
+++ b/index.php
@@ -38,7 +38,7 @@
  *  version from this plugin.
  */
 define('GLM_MEMBERS_BILLING_PLUGIN_VERSION', '0.0.1');
-define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.10');
+define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.11');
 
 // This is the minimum version of the GLM Members DB plugin require for this plugin.
 define('GLM_MEMBERS_BILLING_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '2.8.0');
index 9bda0b7..3046426 100644 (file)
@@ -172,6 +172,8 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
             break;
 
         case 'insert':
+            // Get Data Class for Invoice Types
+            $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config );
             // Set transaction_time to current time.
             $_REQUEST['transaction_time'] = date('Y-m-d H:i:s');
             $_REQUEST['due_date'] = date('Y-m-d', strtotime($_REQUEST['due_date']));
@@ -183,6 +185,9 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
                 $line_items =  $_REQUEST['line_items'];
                 if ( is_array( $line_items ) && !empty( $line_items ) ) {
                     foreach ( $line_items as $key => $line_item ) {
+                        // Get the invoice type
+                        $invoiceType = $InvoiceTypesObj->getEntry( $line_item );
+                        //
                         // Add individual line item record
                         $this->wpdb->insert(
                             GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'line_items',
@@ -193,6 +198,9 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
                                 'amount'         => $_REQUEST['line_item_amount'][$line_item],
                                 'quantity'       => $_REQUEST['line_item_qty'][$line_item],
                                 'total'          => (float)$_REQUEST['line_item_qty'][$line_item] * (float)$_REQUEST['line_item_amount'][$line_item],
+                                'created'        => date('Y-m-d'),
+                                'first_due_date' => $_REQUEST['due_date'],
+                                'next_due_date'  => $_REQUEST['due_date'],
                             ),
                             array(
                                 '%d',
@@ -200,7 +208,10 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
                                 '%s',
                                 '%s',
                                 '%d',
-                                '%d'
+                                '%d',
+                                '%s',
+                                '%s',
+                                '%s',
                             )
                         );
                     }
index 729ef67..e4ca9cf 100644 (file)
@@ -86,6 +86,9 @@ CREATE TABLE {prefix}line_items (
     total DECIMAL(8,2) DEFAULT '0.00',                     -- line item total
     recurring BOOLEAN DEFAULT '0',                         -- true/false if recurring
     recurrence INT NULL DEFAULT 0,                         -- recurrence type
+    created DATE NULL,                                     -- Date this line item was first created
+    first_due_date DATE NULL,                              -- The first due date for this item
+    next_due_date DATE NULL,                               -- Next Due Date for this item
     PRIMARY KEY (id)
 );
 
index 4a7b504..9db6ea9 100644 (file)
@@ -24,5 +24,6 @@ $glmMembersBillingDbVersions = array(
     '0.0.8' => array('version' => '0.0.8', 'tables' => 14),
     '0.0.9' => array('version' => '0.0.9', 'tables' => 14),
     '0.0.10' => array('version' => '0.0.10', 'tables' => 14),
+    '0.0.11' => array('version' => '0.0.11', 'tables' => 14),
 );
 
diff --git a/setup/databaseScripts/update_database_V0.0.11.sql b/setup/databaseScripts/update_database_V0.0.11.sql
new file mode 100644 (file)
index 0000000..423c9a7
--- /dev/null
@@ -0,0 +1,20 @@
+-- Gaslight Media Billing Database
+-- File Created: 03/07/2018
+-- Database Version: 0.0.11
+--
+-- To permit each query below to be executed separately,
+-- all queries must be separated by a line with four dashes
+
+-- Add created to line_items
+ALTER TABLE {prefix}line_items ADD created DATE NULL;
+
+----
+
+-- Add first_due_date to line_items
+ALTER TABLE {prefix}line_items ADD first_due_date DATE NULL;
+
+----
+
+-- Add next_due_date to line_items
+ALTER TABLE {prefix}line_items ADD next_due_date DATE NULL;
+
index ef8c37e..3f2fc3e 100644 (file)
@@ -8,7 +8,7 @@
 {if $invoiceAdded}<span class="glm-notice glm-flash-updated">Notification Added</span>{/if}
 
 <div id="billing-invoice-form">
-    <form action="{$thisUrl}?page={$thisPage}&glm_action=invoices" method="post">
+    <form id="create-invoice-form" action="{$thisUrl}?page={$thisPage}&glm_action=invoices" method="post">
         {if $invoice_id}
             <input type="hidden" name="option" value="update">
             <input type="hidden" name="id" value="{$invoice_id}">
             </div>
             <div class="glm-columns glm-small-12 glm-large-10">
                 <div class="glm-row">
-                    <div class="glm-columns glm-small-8" style="text-align: right;"> Total Amount:&nbsp;&nbsp;&nbsp;</div>
+                    <div class="glm-columns glm-small-8" style="text-align: right;">
+                        Total Amount:&nbsp;&nbsp;&nbsp;
+                        <input type="hidden" id="invoice-total-amount" value="" />
+                    </div>
                     <div class="glm-columns glm-small-4" id="invoice-total"> $0.00 </div>
                 </div>
             </div>
             <tr>
                 <th class="glm-required">Invoice Type</th>
                 <td>
-                    <select name="line_item_type">
+                    <select id="line_item_type" name="line_item_type">
                         <option value="">Select Invoice Type</option>
                         {$parent = 0}
                         {foreach $invoiceTypes as $inv}
                                 <optgroup label="{$inv.name}">
                             {/if}
                             {if $inv.amount != '0.00'}
-                                <option value="{$inv.id}">{$inv.name}</option>
+                                <option value="{$inv.id}" data-price="{$inv.amount}">{$inv.name}</option>
                             {/if}
                             {if $inv.parent.value == 0}
                                 </optgroup>
                         {/foreach}
                     </select>
                 </td>
+                <td><p id="line_item_price"></p></td>
             </tr>
         </table>
     </form>
@@ -174,7 +178,12 @@ jQuery(document).ready(function($){
     // List of Billing Accounts.
     var availableAccounts = [
     {foreach $accounts as $m}
-        { label: "{$m.ref_name|unescape:'html'|replace:'"':''}", value: "{$m.ref_name|unescape:'html'|replace:'"':''}", id: '{$m.id}' },
+        {
+            label: "{$m.ref_name|unescape:'html'|replace:'"':''}",
+            value: "{$m.ref_name|unescape:'html'|replace:'"':''}",
+            id: '{$m.id}',
+            anniversary: '{$m.next_anniversary_date}'
+        },
     {/foreach}
     ];
 
@@ -193,6 +202,8 @@ jQuery(document).ready(function($){
         change: function( event, ui ) {
             // If not in select option then empty the field
             $(this).val((ui.item ? ui.item.label : ""));
+            // Update the anniversary date
+            $('#invoice_due_date').val( ui.item.anniversary );
         },
     });
     // Setup autocomplete for the non member input.
@@ -253,6 +264,7 @@ jQuery(document).ready(function($){
             totalAmount += parseFloat( line_items[i].amount ) * line_items[i].qty;
         }
         $('#invoice-total').html('$' + totalAmount.toFixed( 2 ) );
+        $('#invoice-total-amount').val( totalAmount.toFixed( 2 ) );
     }
 
     /**
@@ -296,9 +308,10 @@ jQuery(document).ready(function($){
                 selectedLineItem.qty = parseInt(1);
                 line_items.push( selectedLineItem );
                 $( '#invoice-line-items' ). append( '<div class="glm-row" id="line-item-type-' + selectedLineItem.id + '">' +
-                    '<div class="glm-columns glm-small-7">' + selectedLineItem.name + '</div>' +
+                    '<div class="glm-columns glm-small-1 line-item-delete dashicons dashicons-trash" data-id="' + selectedLineItem.id + '"></div>' +
+                    '<div class="glm-columns glm-small-6">' + selectedLineItem.name + '</div>' +
                     '<div class="glm-columns glm-small-1">' +
-                    '<input type="number" min="0" data-id="' + selectedLineItem.id + '" name="line_item_qty[' + selectedLineItem.id + ']" value="1">' +
+                    '<input type="number" min="1" data-id="' + selectedLineItem.id + '" name="line_item_qty[' + selectedLineItem.id + ']" value="1">' +
                     '</div><div class="glm-columns glm-small-4">' +
                     '<input type="hidden" name="line_items[]" value="' + selectedLineItem.id + '">' +
                     '<input type="hidden" name="line_item_amount[' + selectedLineItem.id + ']" value="' + selectedLineItem.amount + '">' +
@@ -310,12 +323,31 @@ jQuery(document).ready(function($){
                 selectedLineItem.qty++;
                 $('input[name="line_item_qty\\[' + selectedLineItem.id + '\\]').val(selectedLineItem.qty);
             }
+            $('#line_item_price').html( '' );
             dialog.dialog( 'close' );
         }
         totalInvoice();
         return valid;
     }
 
+    // Add the price to the right of the new selection for add line item.
+    $('#line_item_type').change(function(){
+        $('#line_item_price').html( '$' + $('#line_item_type option:selected').data( 'price') );
+    });
+
+    // Here we delete a line_item from the form.
+    $('body').on( 'click', '.line-item-delete', function(){
+        $('#line-item-type-' + $(this).data( 'id' ) ).remove();
+        // 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 ( line_items[i].id == $(this).data('id') ) {
+                line_items.splice( i, 1 );
+            }
+        }
+        totalInvoice();
+    } );
+
     /**
      * addNewAccount
      *
@@ -399,31 +431,6 @@ jQuery(document).ready(function($){
         }, 500);
     }
 
-    /**
-     * Ajax call to get the list of billing accounts and update the select
-     * for the Billing account drop down.
-     */
-    // function updateBillingAccountSelect(selected_id = null) {
-    //     $.ajax({
-    //         url: '{$ajaxUrl}',
-    //         cache: false,
-    //         type: 'GET',
-    //         data: 'action=glm_members_admin_ajax&glm_action=account&option=list',
-    //         encode: true,
-    //         dataType: 'json'
-    //     }).done(function(msg){
-    //         // console.log( msg.length );
-    //         var memberAccount = document.getElementById('member-account');
-    //         memberAccount.options.length = 0;
-    //         memberAccount.options[memberAccount.options.length] = new Option( 'Select an Account', '' );
-    //         for ( index in msg ) {
-    //             var obj = msg[index];
-    //             memberAccount.options[memberAccount.options.length] = new Option( obj.ref_name, obj.id );
-    //         }
-    //         memberAccount.value = selected_id;
-    //     });
-    // }
-
     /**
      * Ajax call to get the list of members with accounts.
      * Updating the account list used for the autocomplete.
@@ -490,6 +497,7 @@ jQuery(document).ready(function($){
         buttons: {
             "Add Line Item": addLineItem,
             Cancel: function(){
+                $('#line_item_price').html( '' );
                 dialog.dialog( 'close' );
             },
         },
@@ -510,6 +518,20 @@ jQuery(document).ready(function($){
         addLineItem();
     });
 
+    // Need to make sure something is added to the form.
+    // Should not be making empty invoices.
+    $('#create-invoice-form').submit(function(e){
+        // Check if there's a total amount.
+        // If it is empty then don't submit form.
+        if ( !$('#invoice-total-amount').val() || $('#invoice-total-amount').val() == '0.00' ) {
+            alert( 'Empty invoice. Please add something!' );
+            return false;
+        } else {
+            return true;
+        }
+        return false;
+    });
+
     /**
      * Click event for the id of #newLineItemButton.
      *
@@ -590,13 +612,6 @@ jQuery(document).ready(function($){
         $('#invoice_due_date').val( $(this).find('option:selected').data('anniversary') );
     });
 
-    /**
-     * Update the member name field when you select a member from the add account form.
-     */
-    $('#non-member-account').change(function(){
-        $('#glm_member_name').val( $(this).find('option:selected').text() );
-    });
-
     // Flash certain elements for a short time after display
     $(".glm-flash-updated").fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500);