Updating payment page for admin.
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 3 Apr 2018 15:36:57 +0000 (11:36 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 3 Apr 2018 15:36:57 +0000 (11:36 -0400)
Adding lookup for account invoices.
Mark each invoice as paid.

classes/billingSupport.php
models/admin/billing/payments.php
views/admin/billing/editPayment.html

index 1903541..4d67f73 100644 (file)
@@ -181,7 +181,7 @@ class GlmBillingSupport
      * @access public
      * @return payment id
      */
-    public function createPayment( $account, $payment )
+    public function createPayment( $account, $payment, $payment_method = '', $payment_data = '' )
     {
         // Insert the payment
         $this->wpdb->insert(
@@ -190,8 +190,8 @@ class GlmBillingSupport
                 'transaction_time' => date('Y-m-d H:i:s'),
                 'account'          => $account,
                 'amount'           => $payment,
-                'payment_method'   => '',
-                'payment_data'     => '',
+                'payment_method'   => $payment_method,
+                'payment_data'     => $payment_data,
             ),
             array(
                 '%s',
@@ -1135,7 +1135,7 @@ class GlmBillingSupport
 
             // Determine the directory of the payment processor to load and instantiate it.
             if ( $ccProcessor && isset( $this->config['proc_dir'][$ccProcessor] ) ) {
-                require_once GLM_MEMBERS_BILLING_PLUGIN_LIB_PATH.'/paymentProcessors/'.$this->config['proc_dir'][$ccProcessor].'/paymentGateway.php';
+                require_once GLM_MEMBERS_BILLING_PLUGIN_LIB_PATH . '/paymentProcessors/' . $this->config['proc_dir'][$ccProcessor] . '/paymentGateway.php';
                 $CcProcessor = new PaymentGateway( $account );
             }
 
@@ -1148,7 +1148,7 @@ class GlmBillingSupport
                 'ccnumb'  => $cardData['cc_numb'],                       // Card Number
                 'ccexp'   => $cardData['cc_exp'],                        // Expiration Date
                 'cccode'  => $cardData['cc_cvv'],                        // CCV - security code
-                'invoice' => 'billing-'//.$invoice_id                      // Invoice # is "reg-" plus cart ID
+                'invoice' => 'billing-'//.$invoice_id                    // Invoice # is "reg-" plus cart ID
             );
 
             // Now try to run the card processor
@@ -1159,7 +1159,7 @@ class GlmBillingSupport
                 $result = array(
                     'status'   => 1,
                     'errors'   => array(),
-                    'ccResult' => $ccResult
+                    'ccResult' => $ccResult,
                 );
 
             }
@@ -1195,7 +1195,7 @@ class GlmBillingSupport
             $_REQUEST['transaction_time'] = date( 'Y-m-d H:i:s' );
 
             // Create new payment.
-            $payment_id = $this->createPayment( $account_id, $amount );
+            $payment_id = $this->createPayment( $account_id, $amount, 'Credit Card' );
 
             // Record the payment.
             $this->recordPayment( $payment_id, $account_id, $amount, $invoice_id );
@@ -1245,7 +1245,7 @@ class GlmBillingSupport
             $_REQUEST['transaction_time'] = date( 'Y-m-d H:i:s' );
 
             // Create new payment.
-            $payment_id = $this->createPayment( $account_id, $amount );
+            $payment_id = $this->createPayment( $account_id, $amount, 'Credit Card' );
 
             // Record the payment.
             $this->recordPayment( $payment_id, $account_id, $amount, $invoices );
index 04e23f8..d4a2353 100644 (file)
@@ -142,12 +142,19 @@ class GlmMembersAdmin_billing_payments extends GlmDataPayments
             // Support class
             $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config );
 
-            // Set transaction_time to current time.
-            $_REQUEST['transaction_time'] = date('Y-m-d H:i:s');
-            // grab data for the payments
-            $payments = $this->insertEntry();
-            $this->payment_id = $payments['fieldData']['id'];
-            $BillingSupport->recordPayment( $this->payment_id, $_REQUEST['account'], $payments['fieldData']['amount'] );
+            // Make sure there's an amount
+            if ( $amount = filter_var( $_REQUEST['amount'], FILTER_VALIDATE_FLOAT ) ) {
+                $invoices = filter_var( $_REQUEST['invoices'], FILTER_VALIDATE_INT, array( 'flags' => FILTER_REQUIRE_ARRAY ) );
+                // echo '<pre>$amount: ' . print_r( $amount, true ) . '</pre>';
+                // echo '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
+                // Set transaction_time to current time.
+                $_REQUEST['transaction_time'] = date('Y-m-d H:i:s');
+                // grab data for the payments
+                $payments         = $this->insertEntry();
+                $this->payment_id = $payments['fieldData']['id'];
+                $BillingSupport->recordPayment( $this->payment_id, $_REQUEST['account'], $amount, $invoices );
+                $paymentsAdded = true;
+            }
 
             $view = 'editPayment';
             break;
index b0d7ad3..b280bc8 100644 (file)
@@ -2,52 +2,60 @@
 
 {include file='admin/billing/paymentHeader.html'}
 
-{if $paymentUpdated}<span class="glm-notice glm-flash-updated">Notification Updated</span>{/if}
-{if $paymentUpdateError}<span class="glm-notice glm-flash-updated">Notification Update Error</span>{/if}
-{if $paymentInsertError}<span class="glm-notice glm-flash-updated">Notification Insert Error</span>{/if}
-{if $paymentAdded}<span class="glm-notice glm-flash-updated">Notification Added</span>{/if}
+{if $paymentUpdated}<span class="glm-notice glm-flash-updated">Payment Updated</span>{/if}
+{if $paymentUpdateError}<span class="glm-notice glm-flash-updated">Payment Update Error</span>{/if}
+{if $paymentInsertError}<span class="glm-notice glm-flash-updated">Payment Insert Error</span>{/if}
+{if $paymentAdded}<span class="glm-notice glm-flash-updated">Payment Added</span>{/if}
 
 <div id="billing-payment-form" class="glm-billing-form">
     <form action="{$thisUrl}?page={$thisPage}&glm_action=payments" method="post">
+        <input type="hidden" id="total_payment_amount" name="amount" />
         {if $payment_id}
-            <input type="hidden" name="option" value="update">
-            <input type="hidden" name="id" value="{$payment_id}">
+            <input type="hidden" name="option" value="update" />
+            <input type="hidden" name="id" value="{$payment_id}" />
         {else}
-            <input type="hidden" name="option" value="insert">
+            <input type="hidden" name="option" value="insert" />
         {/if}
-                <div class="glm-billing-field">
-                    <div class="glm-billing-label glm-required">
-                        Lookup Member Invoices By Account
-                    </div>
-                    <div class="glm-billing-input">
-                        <input id="member-account" name="account" type="hidden" name="filterAccounts" />
-                        <input id="account_name" name="member_account" />
-                    </div>
-                </div>
-                <div class="glm-billing-field">
-                    <div class="glm-billing-label glm-required">
-                        Select Invoices
-                    </div>
-                    <div class="glm-billing-input">
-                    </div>
-                </div>
-                <div class="glm-billing-field">
-                    <div class="glm-billing-label glm-required">
-                        Amount
-                    </div>
-                    <div class="glm-billing-input">
-                        <div id="total_amount"></div>
-                        <input type="text" id="total_payment_amount" type="hidden" name="amount" required>
-                    </div>
-                </div>
-                <div class="glm-billing-field">
-                    <div class="glm-billing-label glm-required">
-                        Notes
-                    </div>
-                    <div class="glm-billing-input">
-                        <textarea name="notes"></textarea>
-                    </div>
-                </div>
+        <div class="glm-billing-field">
+            <div class="glm-billing-label glm-required">
+                Lookup Member Invoices By Account
+            </div>
+            <div class="glm-billing-input">
+                <input id="member-account" name="account" type="hidden" name="filterAccounts" />
+                <input id="account_name" name="member_account" />
+            </div>
+        </div>
+        <div class="glm-billing-field">
+            <div class="glm-billing-label glm-required">
+                Select Invoices
+            </div>
+            <div class="glm-billing-input" id="glm-payment-invoices">
+            </div>
+        </div>
+        <div class="glm-billing-field">
+            <div class="glm-billing-label glm-required">
+                Amount
+            </div>
+            <div class="glm-billing-input">
+                <div id="total_amount">$0.00</div>
+            </div>
+        </div>
+        <div class="glm-billing-field">
+            <div class="glm-billing-label">
+                Payment Method
+            </div>
+            <div class="glm-billing-input">
+                <input name="payment_method" />
+            </div>
+        </div>
+        <div class="glm-billing-field">
+            <div class="glm-billing-label">
+                Payment Notes
+            </div>
+            <div class="glm-billing-input">
+                <input name="payment_data" />
+            </div>
+        </div>
 
         <input class="button button-primary" type="submit" value="{if $payment_id}Save{else}Create{/if} Payment">
     </form>
@@ -62,6 +70,33 @@ jQuery(document).ready(function($){
     {/foreach}
     ]
 
+    function updateInvoiceList( invoices ) {
+        // Clear the invoices
+        $('#glm-payment-invoices').html('');
+        for ( var i = 0; i < invoices.length; i++ ) {
+            $('#glm-payment-invoices').append( '<label>' +
+            '<input type="checkbox" class="glm-invoice-item" name="invoices[' + invoices[i].id + ']" data-amount="' + invoices[i].balance + '" />$' +
+            invoices[i].balance + '</label><br />' );
+        }
+    }
+
+    function getPageTotal(){
+        // Caclulate the total for this page.
+        var page_total = parseFloat( '0.00' );
+        $('.glm-invoice-item').each(function(){
+            var isChecked = $(this).prop('checked');
+            if ( isChecked ) {
+                page_total = page_total + parseFloat( $(this).data('amount') );
+            }
+        });
+        $('#total_amount').html( '$' + page_total );
+        $('#total_payment_amount').val( page_total );
+    }
+
+    $('#glm-payment-invoices').on( 'click', '.glm-invoice-item', function(){
+        getPageTotal();
+    });
+
     // Setup autocomplete
     $('#account_name').autocomplete({
         source: availableAccounts,
@@ -76,13 +111,16 @@ jQuery(document).ready(function($){
                     account: ui.item.id
                 },
                 success: function( results ) {
-                    console.log( results );
+                    updateInvoiceList( results );
+                    //console.log( results );
                 },
             });
         },
         change: function( event, ui) {
             if( ui.item == null ) {
                 $('#member-account').val( '' );
+                $('#glm-payment-invoices').html('');
+                getPageTotal();
             }
         },
     });