Update the renewal form
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 25 Mar 2019 15:07:30 +0000 (11:07 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 25 Mar 2019 15:07:54 +0000 (11:07 -0400)
use invoice # for lookup

models/admin/ajax/billingAccount.php
views/front/billing/renew.html

index adfbfe2..2df9bc5 100644 (file)
@@ -15,6 +15,7 @@
 
 // Load Members data abstract
 require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataAccounts.php';
+require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataInvoices.php';
 
 /**
  * This class performs the work of handling images passed to it via
@@ -78,12 +79,12 @@ class GlmMembersAdmin_ajax_billingAccount extends GlmDataAccounts
         $return = false;
 
         $option = filter_var( $_REQUEST['option'], FILTER_SANITIZE_STRING );
-        trigger_error( print_r( $_REQUEST, E_USER_NOTICE ) );
+        // trigger_error( print_r( $_REQUEST, true ), E_USER_NOTICE );
 
         switch ( $option ) {
         case 'add':
             $account = $this->insertEntry();
-            trigger_error( print_r( $account['fieldFail'], E_USER_NOTICE ) );
+            trigger_error( print_r( $account['fieldFail'], true ), E_USER_NOTICE );
             if ( !$account['status'] ) {
                 $accountInsertError = true;
                 $return = $account;
@@ -126,30 +127,62 @@ class GlmMembersAdmin_ajax_billingAccount extends GlmDataAccounts
                 );
             }
             break;
-        case 'verifyAccountNumber':
-            if ( isset( $_REQUEST['account_number'] ) && $account_number = filter_var( $_REQUEST['account_number'], FILTER_SANITIZE_STRING ) ) {
-                $account = $this->wpdb->get_row(
-                    $this->wpdb->prepare(
-                        "SELECT id,ref_dest,invoice_type
-                           FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
-                          WHERE account_number = %s",
-                        $account_number
-                    ),
-                    ARRAY_A
-                );
-                trigger_error( print_r( $account, E_USER_NOTICE ) );
-                if ( $account ) {
-                    $return = array(
-                        'status'       => true,
-                        'member_id'    => (int)$account['id'],
-                        'account_id'   => (int)$account['ref_dest'],
-                        'invoice_type' => (int)$account['invoice_type'],
+        case 'verifyInvoiceNumber':
+            if ( isset( $_REQUEST['invoice_number'] ) && $invoiceNumber = filter_var( $_REQUEST['invoice_number'], FILTER_SANITIZE_STRING ) ) {
+                // Get the invoice
+                $Invoices = new GlmDataInvoices( $this->wpdb, $this->config );
+                $Invoices->line_items_post = true;
+                $invoice = $Invoices->getEntry( $invoiceNumber );
+                $Invoices->line_items_post = false;
+                // trigger_error( print_r( $invoice, true ), E_USER_NOTICE );
+                if ( !$invoice ) {
+                    $return = array( 'status'  => false, 'message' => 'no invoice' );
+                }
+                trigger_error( print_r( $invoice, true ), E_USER_NOTICE );
+
+                $accountId = $invoice['account']['value'];
+                trigger_error( print_r( $accountId, true ), E_USER_NOTICE );
+                if ( !$accountId ) {
+                    $return = array( 'status'  => false, 'message' => 'no accountId' );
+                }
+                $invoiceTypeId = false;
+                // Get the invoice id for the renewal line item
+                if ( isset( $invoice['line_items'] ) && is_array( $invoice['line_items'] ) ) {
+
+                    foreach ( $invoice['line_items'] as $line_item ) {
+                        if ( $line_item['recurring'] ) {
+                            $invoiceTypeId = $line_item['line_item_type'];
+                        }
+                    }
+
+                    $account = $this->wpdb->get_row(
+                        $this->wpdb->prepare(
+                            "SELECT id,ref_dest,invoice_type
+                               FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
+                              WHERE id = %d",
+                            $accountId
+                        ),
+                        ARRAY_A
                     );
+                    trigger_error( print_r( $account, true ), E_USER_NOTICE );
+                    // Returning the invoice_type of the account
+                    // TODO: This maybe should be the actual invoice type_id
+                    if ( $account ) {
+                        $return = array(
+                            'status'       => true,
+                            'member_id'    => (int)$account['id'],
+                            'account_id'   => (int)$account['ref_dest'],
+                            'invoice_type' => (int)$invoiceTypeId,
+                            'invoice'      => $invoice,
+                        );
+                    } else {
+                        $return = array( 'status'  => false, 'message' => 'no account' );
+                    }
                 } else {
-                    $return = array( 'status'  => false );
+                    $return = array( 'status'  => false, 'message' => 'no line_items' );
                 }
             } else {
-                $return = array( 'status'  => false );
+                $return = array( 'status'  => false, 'message' => 'no invoiceNumber' );
             }
             break;
         }
index 883f22a..56ac6e5 100644 (file)
         {else}
             <div class="glm-billing-field">
                 <div class="glm-billing-label glm-required">
-                    Member Billing #
+                    Invoice #
                 </div>
                 <div class="glm-billing-input">
-                    <input id="account_number" name="account_number" required />
+                    <input id="invoice_number" name="invoice_number" required />
                 </div>
             </div>
             <div class="glm-billing-field">
@@ -55,7 +55,7 @@
                     {if $settings.allow_membership_choice}
                         {foreach $payable_types as $type}
                             <label>
-                                <input type="radio" name="member_renewing" data-amount="{$type.amount}" value="{$type.id}" />
+                                <input type="radio" name="member_renewing" data-amount="{$type.amount}" value="{$type.id}" id="invoice_type_id_{$type.id}" />
                                 {$type.name} {$type.amount}
                             </label>
                         {/foreach}
@@ -114,7 +114,7 @@ jQuery(document).ready(function($){
     });
 
     // verify the account number is correct.
-    $('#account_number').on( 'change', function(){
+    $('#invoice_number').on( 'change', function(){
         $.ajax({
             cache: false,
             url: '{$ajaxUrl}',
@@ -123,20 +123,22 @@ jQuery(document).ready(function($){
             data: {
                 'action': 'glm_members_admin_ajax',
                 'glm_action': 'billingAccount',
-                'option': 'verifyAccountNumber',
-                'account_number': $(this).val(),
+                'option': 'verifyInvoiceNumber',
+                'invoice_number': $(this).val(),
             }
         }).done(function(msg){
             console.log( 'msg', msg );
             if ( msg.status === false ) {
                 alert( 'Not a valid Account #' );
-                $('#account_number').val('');
+                $('#invoice_number').val('');
             } else {
                 // Set the account and member inputs
                 $('#account_id').val( msg.account_id );
                 $('#member_id').val( msg.member_id );
                 if ( msg.invoice_type ) {
-                    $('input[name=member_renewing], input[value=' + msg.invoice_type + ']').prop('checked', true);
+                    // $('input[name=member_renewing]').prop('checked', false);
+                    console.log( 'invoice_type', msg.invoice_type );
+                    $('#invoice_type_id_' + msg.invoice_type ).attr('checked', true);
                     getPageTotal();
                 }
             }