Work on payments and invoices
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 12 Mar 2018 20:22:56 +0000 (16:22 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 12 Mar 2018 20:22:56 +0000 (16:22 -0400)
Payments:
Updating the payment processing to pass through the invoice id so it
marks that invoice as being paid.
Set select to use the balance instead of total_amount.

Invoices:
Fix issue where after adding an invoice the next page would not work and
was acting like edit invoice page.

classes/billingSupport.php
models/admin/billing/invoices.php
models/admin/member/billing.php
views/admin/billing/editInvoice.html
views/admin/billing/makePayment.html
views/admin/billing/paymentProcess.html
views/admin/settings/invoiceTypes.html

index d9b8a22..0f70398 100644 (file)
@@ -88,46 +88,76 @@ class GlmBillingSupport
      * Also need to record the payment to the invoice_payments
      * table so we know which payment was applied to the invoice.
      *
-     * @param mixed $invoice_id Id of the Invoice record
-     * @param mixed $payment    Total amount of the Payment
+     * @param int   $payment_id Payment id
+     * @param int   $account    Total amount of the Payment
+     * @param float $payment    Payment amount
+     * @param int   $invoice_id Id of the Invoice record
      *
      * @access public
      * @return void
      */
-    public function recordPayment( $payment_id, $account, $payment )
+    public function recordPayment( $payment_id, $account, $payment, $invoice_id = null )
     {
         // record into the transaction table
         $this->recordTransaction( $this->config['transaction_numb']['Payment'], $payment_id, $account, null, $payment );
 
-        // Get the unpaid invoices
-        $invoices = $this->getUnPaidInvoicesByAccount( $account );
-        // echo '<pre>$invoices: ' . print_r( $invoices, true ) . '</pre>';
-        if ( $invoices && is_array( $invoices ) && count( $invoices ) > 0 ) {
-            foreach ( $invoices as $invoice ) {
-                $balance = (float)$invoice['balance'];
-                if ( $payment == $invoice['balance'] ) {
-                    // Mark this as paid then
-                    $this->updateInvoiceBalance( $invoice['id'], (float)0.00 );
-                    // Record the payment to the invoice_payments table
-                    $this->recordInvoicePayment( $invoice['id'], $payment_id, $invoice['balance'] );
-                    // $payment is used up so break from the foreach loop
-                    break;
-                } else if ( $payment > $invoice['balance'] ) {
-                    $this->updateInvoiceBalance( $invoice['id'], (float)0.00 );
-                    $payment -= $invoice['balance'];
-                    // Record the payment to the invoice_payments table
-                    $this->recordInvoicePayment( $invoice['id'], $payment_id, $invoice['balance'] );
-                } else if ( $invoice['balance'] > $payment ) {
-                    // Update the balance of the invoice
-                    $balance = (float)$balance - (float)$payment;
-                    $this->updateInvoiceBalance( $invoice['id'], (float)$balance );
-                    // Record the payment to the invoice_payments table
-                    $this->recordInvoicePayment( $invoice['id'], $payment_id, (float)$payment );
-                    // $payment is used up so break from the foreach loop
-                    break;
+        // If there's an invoice_id
+        // If the payment amount is over then go through the other invoices.
+        if ( $invoice_id ) {
+            // Get the invoice
+            $invoice = $this->getInvoiceById( $invoice_id );
+            $balance = (float)$invoice['balance'];
+            if ( $payment == $invoice['balance'] ) {
+                // Mark this as paid then
+                $this->updateInvoiceBalance( $invoice['id'], (float)0.00 );
+                // Record the payment to the invoice_payments table
+                $this->recordInvoicePayment( $invoice['id'], $payment_id, $invoice['balance'] );
+            } else if ( $payment > $invoice['balance'] ) {
+                $this->updateInvoiceBalance( $invoice['id'], (float)0.00 );
+                $payment -= $invoice['balance'];
+            } else if ( $invoice['balance'] > $payment ) {
+                // Update the balance of the invoice
+                $balance = (float)$balance - (float)$payment;
+                $this->updateInvoiceBalance( $invoice['id'], (float)$balance );
+                // Record the payment to the invoice_payments table
+                $this->recordInvoicePayment( $invoice['id'], $payment_id, (float)$payment );
+            }
+
+        }
+
+        if ( $payment ) {
+
+            // Get the unpaid invoices
+            $invoices = $this->getUnPaidInvoicesByAccount( $account );
+            if ( $invoices && is_array( $invoices ) && count( $invoices ) > 0 ) {
+                foreach ( $invoices as $invoice ) {
+                    $balance = (float)$invoice['balance'];
+                    if ( $payment == $invoice['balance'] ) {
+                        // Mark this as paid then
+                        $this->updateInvoiceBalance( $invoice['id'], (float)0.00 );
+                        // Record the payment to the invoice_payments table
+                        $this->recordInvoicePayment( $invoice['id'], $payment_id, $invoice['balance'] );
+                        // $payment is used up so break from the foreach loop
+                        break;
+                    } else if ( $payment > $invoice['balance'] ) {
+                        $this->updateInvoiceBalance( $invoice['id'], (float)0.00 );
+                        $payment -= $invoice['balance'];
+                        // Record the payment to the invoice_payments table
+                        $this->recordInvoicePayment( $invoice['id'], $payment_id, $invoice['balance'] );
+                    } else if ( $invoice['balance'] > $payment ) {
+                        // Update the balance of the invoice
+                        $balance = (float)$balance - (float)$payment;
+                        $this->updateInvoiceBalance( $invoice['id'], (float)$balance );
+                        // Record the payment to the invoice_payments table
+                        $this->recordInvoicePayment( $invoice['id'], $payment_id, (float)$payment );
+                        // $payment is used up so break from the foreach loop
+                        break;
+                    }
                 }
             }
+
         }
+
     }
 
     public function createPayment( $account, $payment )
index b8d42e1..eea8fe4 100644 (file)
@@ -147,7 +147,6 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
             $Accounts = new GlmDataAccounts( $this->wpdb, $this->config );
             $billingAccount = $Accounts->newEntry();
             $accounts = $Accounts->getList( '', 'ref_name' );
-            // echo '<pre>$accounts: ' . print_r( $accounts, true ) . '</pre>';
 
             // Need a list of members that don't have an account.
             $nonAccountMembers = $this->wpdb->get_results(
@@ -159,7 +158,6 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
                 ORDER BY name",
                 ARRAY_A
             );
-            // echo '<pre>$nonAccountMembers: ' . print_r( $nonAccountMembers, true ) . '</pre>';
 
             if ( isset( $invoiceTypes ) ) {
                 foreach ( $invoiceTypes as $invoiceType ) {
@@ -226,6 +224,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
                     }
                 }
             }
+            $invoiceAdded = true;
             // Here we're going to generate the invoice total.
             $totals = $this->generateInvoiceTotal( $this->invoice_id );
             // Now we have a total for the invoice we can record the transaction
@@ -235,9 +234,29 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
 
             // Set the view file to editInvoice
             $view = 'editInvoice';
-            $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config );
-            $invoiceTypes    = $InvoiceTypesObj->getList();
-            $invoiceTypes    = $InvoiceTypesObj->sortParentChild($invoiceTypes);
+
+            // Reset this form to add a new one again
+            unset( $invoices );
+            $this->invoice_id = false;
+            $invoices         = $this->newEntry();
+            $InvoiceTypesObj  = new GlmDataInvoiceTypes( $this->wpdb, $this->config );
+            $invoiceTypes     = $InvoiceTypesObj->getList();
+            $invoiceTypes     = $InvoiceTypesObj->sortParentChild($invoiceTypes);
+            // Need to get the accounts
+            $Accounts = new GlmDataAccounts( $this->wpdb, $this->config );
+            $billingAccount = $Accounts->newEntry();
+            $accounts = $Accounts->getList( '', 'ref_name' );
+
+            // Need a list of members that don't have an account.
+            $nonAccountMembers = $this->wpdb->get_results(
+                "SELECT id,name
+                   FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+                  WHERE id NOT IN (
+                        SELECT distinct ref_dest
+                          FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX .  "accounts   )
+                ORDER BY name",
+                ARRAY_A
+            );
             if ( isset( $invoiceTypes ) ) {
                 foreach ( $invoiceTypes as $invoiceType ) {
                     $invTypes[$invoiceType['id']] = array(
index ea45795..900e8de 100644 (file)
@@ -117,6 +117,8 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
         $management         = false;
         $messages           = array();
         $invoices           = false;
+        $paymentSuccess     = false;
+        $paymentError       = false;
 
         // For lockedToMember.
         $lockedToMember = false;
@@ -340,6 +342,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
             }
 
             if (count($messages) == 0) {
+                $paymentSuccess = true;
 
                 // Determine the directory of the payment processor to load and instatiate it.
                 if ($ccProcessor && isset($this->config['proc_dir'][$ccProcessor])) {
@@ -373,7 +376,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
                     // Create new payment.
                     $payment_id = $BillingSupport->createPayment( $paymentData['account_id'], $paymentData['amount'] );
                     // Record the payment.
-                    $BillingSupport->recordPayment( $payment_id, $paymentData['account_id'], $paymentData['amount'] );
+                    $BillingSupport->recordPayment( $payment_id, $paymentData['account_id'], $paymentData['amount'], $paymentData['invoice_id'] );
 
                     $messages[] = $ccResult['statusText'];
                     $messages[] = $ccResult['description'];
@@ -381,6 +384,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
                 }
 
             } else {
+                $paymentError = true;
                 // Load DataClass for accounts.
                 require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataAccounts.php';
                 $Accounts = new GlmDataAccounts( $this->wpdb, $this->config );
@@ -501,6 +505,8 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
             'management'         => $management,
             'messages'           => $messages,
             'invoices'           => $invoices,
+            'paymentSuccess'     => $paymentSuccess,
+            'paymentError'       => $paymentError,
         );
 
         // Return status, any suggested view, and any data to controller.
index a0b90ed..8e09e4c 100644 (file)
@@ -2,10 +2,10 @@
 
 {include file='admin/billing/subHeader.html'}
 
-{if $invoiceUpdated}<span class="glm-notice glm-flash-updated">Notification Updated</span>{/if}
-{if $invoiceUpdateError}<span class="glm-notice glm-flash-updated">Notification Update Error</span>{/if}
-{if $invoiceInsertError}<span class="glm-notice glm-flash-updated">Notification Insert Error</span>{/if}
-{if $invoiceAdded}<span class="glm-notice glm-flash-updated">Notification Added</span>{/if}
+{if $invoiceUpdated}<span class="glm-notice glm-flash-updated">Invoice Updated</span>{/if}
+{if $invoiceUpdateError}<span class="glm-notice glm-flash-updated">Invoice Update Error</span>{/if}
+{if $invoiceInsertError}<span class="glm-notice glm-flash-updated">Invoice Insert Error</span>{/if}
+{if $invoiceAdded}<span class="glm-notice glm-flash-updated">Invoice Added</span>{/if}
 
 <div id="billing-invoice-form" style="max-width: 750px;">
     <form id="create-invoice-form" action="{$thisUrl}?page={$thisPage}&glm_action=invoices" method="post">
@@ -472,7 +472,7 @@ jQuery(document).ready(function($){
                     // Close the dialog
                     newAccountDialog.dialog( 'close' );
                 } else {
-                    console.log( 'return', msg );
+                    //console.log( 'return', msg );
                 }
             });
         }
index e578b70..0977c5c 100644 (file)
@@ -1,14 +1,18 @@
 {include file='admin/member/header.html'}
 {include file='admin/billing/memberBillingSubHeader.html'}
 
-{if $messages}
-    {foreach $messages as $message}
-        <div class="">{$message}</div>
-    {/foreach}
-{/if}
+<div id="billing-payment-form" style="max-width:750px;">
+
+    {if $paymentSuccess}<span class="glm-notice glm-flash-updated">Payment Completed</span>{/if}
+    {if $paymentError}<span class="glm-notice glm-flash-updated">Error With Payment</span>{/if}
+
+    {if $messages}
+        {foreach $messages as $message}
+            <div class="">{$message}</div>
+        {/foreach}
+    {/if}
 
 
-<div id="billing-payment-form" style="max-width:750px;">
     <form action="{$thisUrl}?page={$thisPage}&glm_action=billing&option=makePayment" method="post">
         <input type="hidden" name="page" value="{$thisPage}">
         <input type="hidden" name="glm_action" value="billing">
                 <div class="glm-row">
                     <div class="glm-columns glm-small-12 glm-medium-3 glm-required"> Select Invoice </div>
                     <div class="glm-columns glm-small-12 glm-large-8">
-                        <select name="invoice_id" required>
+                        <select id="billing-invoice-select" name="invoice_id" required>
                             <option value=""></option>
                             {if $invoices}
                                 {foreach $invoices as $invoice}
                                 <option
                                     value="{$invoice.id}"
-                                    {if isset($smarty.request.invoice_id) && $smarty.request.invoice_id == $invoice.id} selected{/if}>${$invoice.amount_total} Due {$invoice.due_date}</option>
+                                    data-amount="{$invoice.balance}"
+                                    {if isset($smarty.request.invoice_id) && $smarty.request.invoice_id == $invoice.id} selected{/if}>
+                                    ${$invoice.balance} Due {$invoice.due_date}
+                                </option>
                                 {/foreach}
                             {/if}
                         </select>
@@ -50,6 +57,7 @@
                         <input
                             class="glm-form-text-input-small"
                             type="number"
+                            id="billing-amount"
                             name="amount"
                             step="0.01"
                             min="0.01"
     </form>
 </div>
 
+<script>
+jQuery(document).ready(function($){
+
+    $('#billing-invoice-select').change(function(){
+        // Get the data-amount and set the amount being paid.
+        $('#billing-amount').val( $(this).find('option:selected').data( 'amount') );
+    });
+
+    // 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);
+
+});
+</script>
+
+
 {include file='admin/footer.html'}
index ed62455..c201286 100644 (file)
@@ -1,11 +1,21 @@
 {include file='admin/member/header.html'}
 {include file='admin/billing/memberBillingSubHeader.html'}
 
+{if $paymentSuccess}<span class="glm-notice glm-flash-updated">Payment Completed</span>{/if}
+
 {if $messages}
     {foreach $messages as $message}
-        {$message} <br>
+        <div class="">{$message}</div>
     {/foreach}
 {/if}
 
+<script>
+jQuery(document).ready(function($){
+
+    // 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);
+
+});
+</script>
 
 {include file='admin/footer.html'}
index 40c3c25..9c38b1b 100644 (file)
@@ -210,7 +210,7 @@ jQuery(document).ready(function($) {
         var invoiceRecurring  = $(this).data('invoice-recurring');
         var invoiceRecurrence = $(this).data('invoice-recurrence');
 
-        console.log( 'invoiceRecurring: ', invoiceRecurring );
+        //console.log( 'invoiceRecurring: ', invoiceRecurring );
 
         // Set the values of the edit form for the selected invoiceType
         $('#edit-id').val( invoiceID );