Need account_number to be required and unique
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 14 Mar 2019 18:35:09 +0000 (14:35 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 14 Mar 2019 18:35:09 +0000 (14:35 -0400)
This will have to be check on:
Adding new account (create invoice)
Add edit on Member billing
Renew forms will use this if not logged in.

classes/data/dataAccounts.php
models/admin/member/billing.php
models/front/billing/renew.php
setup/shortcodes.php
views/admin/billing/editAccount.html
views/admin/billing/editInvoice.html
views/admin/billing/memberBillingSubHeader.html
views/admin/billing/statements.html
views/front/billing/renew.html

index ea64078..ea2ae3e 100644 (file)
@@ -186,7 +186,8 @@ class GlmDataAccounts extends GlmDataAbstract
                 'field'    => 'account_number',
                 'type'     => 'text',
                 'use'      => 'a',
-                'required' => false,
+                'required' => true,
+                'unique'   => true,
             ),
 
             // Invoice Delivery Methods
index 350b651..7ce5179 100644 (file)
@@ -835,6 +835,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
             break;
 
         case 'makepaymentadjustment':
+
             $view = 'makePaymentAdjustment';
 
             // Get unpaid invoices
index 18cf554..49e59ed 100644 (file)
  */
 define('GLM_MEMBERS_BILLING_MEMBER_MENU', true);
 require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/data/dataInvoices.php';
-// require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/data/dataPayments.php';
 require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/billingSupport.php';
 
-// Load Billing data abstract
-// require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/data/dataBilling.php';
-
 class GlmMembersFront_billing_renew // extends GlmDataBilling
 {
 
@@ -89,52 +85,63 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
 
     }
 
-    public function modelAction($actionData = false)
+    public function modelAction( $actionData = false )
     {
         // Initialize Variables Here
-        $fromMemberMenu     = false;
-        $this->memberID     = false;
-        $memberData         = false;
-        $memberName         = false;
-        $haveMember         = false;
-        $haveAccount        = false;
-        $option             = 'renew';
-        $view               = 'renew';
-        $account            = false;
-        $accountID          = 0;
-        $account_data       = false;
-        $balance_due        = false;
-        $invoiceHtml        = false;
-        $management         = false;
-        $messages           = array();
-        $paymentSuccess     = false;
-        $paymentError       = false;
-        $hasBillingAccount  = false;
-        $employees          = false;
-        $member_invoice     = false;
-        $payable_types      = false;
-        $account_status     = false;
+        $fromMemberMenu    = false;
+        $this->memberID    = false;
+        $memberData        = false;
+        $memberName        = false;
+        $haveMember        = false;
+        $haveAccount       = false;
+        $option            = 'renew';
+        $view              = 'renew';
+        $account           = false;
+        $accountID         = 0;
+        $account_data      = false;
+        $balance_due       = false;
+        $invoiceHtml       = false;
+        $management        = false;
+        $messages          = array();
+        $paymentSuccess    = false;
+        $paymentError      = false;
+        $hasBillingAccount = false;
+        $employees         = false;
+        $member_invoice    = false;
+        $payable_types     = false;
+        $account_status    = false;
+        $unregistered      = false;
 
         // For lockedToMember.
         $lockedToMember = false;
         $lockedWhereT   = 'true';
         $lockedWhere    = 'true';
 
+        // Check for unregistered.
+        if ( isset( $actionData['request']['unregistered'] ) && filter_var( $actionData['request']['unregistered'], FILTER_VALIDATE_BOOLEAN ) ) {
+            $unregistered = filter_var( $actionData['request']['unregistered'], FILTER_VALIDATE_BOOLEAN );
+        }
+
+        echo '<pre>$unregistered: ' . print_r( $unregistered, true ) . '</pre>';
+
         // Call in the support class
         $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config );
-        // echo '<pre>$this->config: ' . print_r( $this->config, true ) . '</pre>';
+
+        // Check if the user is logged in.
         if ( isset( $this->config['loggedInUser'] )
             && isset( $this->config['loggedInUser']['contactUser'] )
             && isset( $this->config['loggedInUser']['contactUser']['ref_dest'] )
         ) {
             $this->memberID = $this->config['loggedInUser']['contactUser']['ref_dest'];
+        } elseif ( $unregistered ) {
+            $option = 'renew';
         } else {
             $option = 'noaccount';
         }
 
         // Check if there's a logged in user who is locked to their own entity.
         $lockedToMember = apply_filters('glm_members_locked_to_member_id', false);
-        // echo '<pre>$lockedToMember: ' . print_r( $lockedToMember, true ) . '</pre>';
+
         if ($lockedToMember) {
             $memberID       = $lockedToMember;
             $this->memberID = $memberID;
@@ -159,30 +166,39 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
             $option = $_REQUEST['option'];
         }
 
-        require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataAccounts.php';
-        $Accounts = new GlmDataAccounts( $this->wpdb, $this->config );
-        $accountID = $this->wpdb->get_var(
-            $this->wpdb->prepare(
-                "SELECT id
-                   FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
-                  WHERE ref_dest = %d",
-                $this->memberID
-            )
-        );
-        $account_status = apply_filters( 'glm-billing-get-account-status', '', $accountID );
+        if ( isset( $memberID ) && $memberID ) {
+            require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataAccounts.php';
+            $Accounts = new GlmDataAccounts( $this->wpdb, $this->config );
+            $accountID = $this->wpdb->get_var(
+                $this->wpdb->prepare(
+                    "SELECT id
+                       FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
+                      WHERE ref_dest = %d",
+                    $this->memberID
+                )
+            );
+            if ( isset( $accountID ) && $accountID ) {
+                $account_status = apply_filters( 'glm-billing-get-account-status', '', $accountID );
+            }
+        }
 
         switch ( $option ) {
         case 'noaccount':
             $view = 'noaccount';
             break;
+
         case 'renew':
             // TODO: only the renew form if the member is not active
             if ( $account_status == 'Active' ) {
                 $view = 'nonrenew';
+            } elseif ( !$account_status && !$unregistered ) {
+                $view = 'noaccount';
             } else  {
                 $view = 'renew';
-                // echo '<pre>$this->memberID: ' . print_r( $this->memberID, true ) . '</pre>';
 
+                if ( $unregistered ) {
+                    $lockedToMember = true;
+                }
 
                 // Get list of payable invoice_types
                 $payable_types = $BillingSupport->getAllPayableInvoiceTypes();
@@ -192,20 +208,22 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
                 if ( $member_invoice_id ) {
                     $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id );
                 }
-                // echo '<pre>$member_invoice: ' . print_r( $member_invoice, true ) . '</pre>';
 
-                // Get a list of this accounts employees. If they have any.
-                $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
-                // echo '<pre>$employees: ' . print_r( $employees, true ) . '</pre>';
+                if ( $this->config['settings']['allow_employees'] ) {
+                    // Get a list of this accounts employees. If they have any.
+                    $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+                }
 
                 // Load DataClass for Management.
                 require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataManagement.php';
                 $Management = new GlmDataBillingManagement( $this->wpdb, $this->config );
                 $management = $Management->getEntry( 1 );
 
-                // Need to see if there's an account for this member.
-                require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataAccounts.php';
-                $account = $Accounts->editEntry( $accountID );
+                if ( $accountID ) {
+                    // Need to see if there's an account for this member.
+                    require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataAccounts.php';
+                    $account = $Accounts->editEntry( $accountID );
+                }
             }
 
             break;
@@ -214,6 +232,21 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
             $error = false;
             $view  = 'renewMembership';
 
+            // Check for account_number
+            // If given then this is from unregistered user.
+            // Have to verify that this number does match with billing_account.
+            if ( isset( $_REQUEST['account_number'] ) && $account_number = filter_var( $_REQUEST['account_number'], FILTER_SANITIZE_STRING ) ) {
+                // Verify account number.
+                $accountID = $this->wpdb->get_var(
+                    $this->wpdb->prepare(
+                        "SELECT id
+                           FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
+                          WHERE account_number = %s",
+                        $account_number
+                    )
+                );
+            }
+
             $member_invoice_id = $BillingSupport->getMembersInvoiceTypeByRefDest( $this->memberID );
             if ( $member_invoice_id ) {
                 $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id );
@@ -221,7 +254,9 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
                 $error = true;
             }
 
-            $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+            if ( $this->config['settings']['allow_employees'] ) {
+                $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+            }
 
             $invoice_data = array(
                 'account_id'     => $accountID,
@@ -236,6 +271,10 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
                 $error = true;
             }
 
+            // Start database transaction
+            $this->wpdb->show_errors();
+            $this->wpdb->query('START TRANSACTION');
+
             // Before we create an invoice for this member see if they already have one.
             // If we find one then delete it.
             $old_invoice_ids = $this->wpdb->get_results(
@@ -281,8 +320,10 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
                     $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id );
                 }
 
-                // Get a list of this accounts employees. If they have any.
-                $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+                if ( $this->config['settings']['allow_employees'] ) {
+                    // Get a list of this accounts employees. If they have any.
+                    $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+                }
 
                 // Load DataClass for Management.
                 require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataManagement.php';
@@ -292,6 +333,7 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
                 // Need to see if there's an account for this member.
                 $account = $Accounts->editEntry( $accountID );
             } else {
+
                 // Look at the payment option given
                 $payment_option = filter_var( $_REQUEST['payment_option'], FILTER_SANITIZE_STRING );
 
@@ -322,8 +364,10 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
                             $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id );
                         }
 
-                        // Get a list of this accounts employees. If they have any.
-                        $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+                        if ( $this->config['settings']['allow_employees'] ) {
+                            // Get a list of this accounts employees. If they have any.
+                            $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+                        }
 
                         // Load DataClass for Management.
                         require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataManagement.php';
@@ -340,6 +384,7 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
 
                 // If there's no error reported then show the invoice
                 if ( !$error ) {
+                    $this->wpdb->query( 'COMMIT' );
                     // Now need to show the invoice.
                     $view = 'viewInvoice';
 
@@ -377,6 +422,8 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
                             );
                         }
                     }
+                } else {
+                    $this->wpdb->query( 'ROLLBACK' );
                 }
             }
 
index 0cecf1d..3ab442d 100644 (file)
@@ -93,8 +93,9 @@ $glmMembersBillingShortcodes = array(
         'action'     => 'renew',
         'table'      => false,
         'attributes' => array(
-            'member_id' => false,
-            'template'  => false,
+            'member_id'    => false,
+            'template'     => false,
+            'unregistered' => false,
         ),
     ),
     'glm-members-billing-become-member-form' => array(
index feb2aa3..108ed62 100644 (file)
@@ -24,6 +24,7 @@
     {if $lockedToMember}
         <input type="hidden" name="anniversary_date" value="{$account.fieldData.anniversary_date.date}" />
         <input type="hidden" name="renewal_date" value="{$account.fieldData.renewal_date.date}" />
+        <input type="hidden" name="account_number" value="{$account.fieldData.account_number}" />
     {/if}
 
     {if $accountID}
@@ -41,6 +42,8 @@
             </div>
         </div>
 
+    {if !$lockedToMember}
+
         <div class="glm-billing-field">
             <div class="glm-billing-label{if $account.fieldRequired.account_number} glm-required{/if}">Account Number</div>
             <div class="glm-billing-input{if $account.fieldFail.account_number} glm-form-bad-input{/if}" data-tabid="glm-account_number">
@@ -49,9 +52,6 @@
             </div>
         </div>
 
-    {if !$lockedToMember}
-
-
         {if !$settings.member_types_enabled}
 
             <div class="glm-billing-field">
index cc222a7..bdc87b4 100644 (file)
                     <input id="glm_member_name" type="text" name="ref_name" value="">
                 </td>
             </tr>
+            <tr>
+                <th class="glm-required" align="right">Account Number</th>
+                <td>
+                    <input type="text" name="account_number" value="">
+                </td>
+            </tr>
             <tr>
                 <th class="glm-required" align="right">Anniversary Date</th>
                 <td>
@@ -315,6 +321,7 @@ jQuery(document).ready(function($){
         line_items        = [],                                   // Array holding line_items json objects
         allFields         = $( [] ).add( line_item_type ),        // Array holding the form fields - add line_item form
         ref_name          = $( 'input[name="ref_name"' ),
+        account_number    = $( 'input[name="account_number"' ),
         anniversary_date  = $( 'input[name="anniversary_date"' ),
         renewal_date      = $( 'input[name="renewal_date"' ),
         email             = $( 'input[name="email"' ),
@@ -337,7 +344,7 @@ jQuery(document).ready(function($){
         lineTips          = $( '.validateTips' ),
         accountTips       = $( '.validateAccountTips' ),
         customTips        = $( '.validateCustomTips' ),
-        allAccountFields  = $( [] ).add( ref_name ) .add( anniversary_date ) .add( renewal_date )
+        allAccountFields  = $( [] ).add( ref_name ) .add( account_number ) .add( anniversary_date ) .add( renewal_date )
             .add( email ) .add( billing_addr1 ) .add( billing_addr2 ) .add( billing_city )
             .add( billing_state ) .add( billing_zip ) .add(billing_county)
             {if $settings.billing_contact_name_enabled}.add( billing_contact_name ){else}.add( billing_fname ).add( billing_lname ){/if}
@@ -492,6 +499,7 @@ jQuery(document).ready(function($){
         allAccountFields.removeClass( 'ui-state-error' );
 
         valid = valid && checkRequired( ref_name, accountTips, 'Name is required!' );
+        valid = valid && checkRequired( account_number, accountTips, 'Account Number is required!' );
         valid = valid && checkRequired( anniversary_date, accountTips, 'Anniversary Date is required!' );
         valid = valid && checkRequired( email, accountTips, 'Email is required!' );
         valid = valid && checkRegexp( email, emailRegex, 'Use valid email!', accountTips );
@@ -519,7 +527,11 @@ jQuery(document).ready(function($){
                     // Close the dialog
                     newAccountDialog.dialog( 'close' );
                 } else {
-                    //console.log( 'return', msg );
+                    // Check for unqique account number
+                    if ( msg.fieldFail.account_number ) {
+                        alert( 'Account Number must be unique!' );
+                    }
+                    console.log( 'return', msg );
                 }
             });
         }
index 6b1ae01..972392c 100644 (file)
@@ -27,7 +27,7 @@
                     <a href="{$thisUrl}?page=glm-members-admin-menu-member&glm_action=billing&member={$memberID}&option=renew" class="">Membership Renewal</a>
                 </li>
             {/if}
-            {if isset( $billing_settings.uptravel_payment_form ) && $billing_settings.uptravel_payment_form && $accountID}
+            {if isset( $billing_settings.uptravel_payment_form ) && $billing_settings.uptravel_payment_form && $accountID && !$fromMemberMenu}
                 {$hasInvoice = apply_filters('glm-billing-account-has-invoice', false, $accountID )}
                 <li>
                     {if $hasInvoice}
index a4c3367..f73d491 100644 (file)
                         <td class="glm-shrink">${$transaction.transaction_data.amount_total}</td>
                         <td class="glm-shrink">${$transaction.transaction_data.balance}</td>
                         <td class="glm-shrink">
-                        {if isset( $billing_settings.uptravel_payment_form ) && $billing_settings.uptravel_payment_form}
+                        {if isset( $billing_settings.uptravel_payment_form ) && $billing_settings.uptravel_payment_form && !$fromMemberMenu}
                             {if $transaction.transaction_data.paid}&nbsp;{else}<a href="{$thisUrl}?page={$thisPage}&glm_action=billing&option=makepaymentadjustment&member={$memberID}">Make A Payment</a>{/if}
                         {else}
                             {if $transaction.transaction_data.paid}&nbsp;{else}<a href="{$thisUrl}?page={$thisPage}&glm_action=billing&option=makepayment&member={$memberID}">Pay Invoice</a>{/if}
index 3809422..1b80447 100644 (file)
         <input type="hidden" name="account_id" value="{$account_data.id}" />
         <input type="hidden" id="total_renew_amount" name="total_renew_amount" value="" />
 
-
         {if $member_invoice}
-        <div class="glm-billing-field">
-            <div class="glm-billing-label glm-required">
-                Membership Class
+            <div class="glm-billing-field">
+                <div class="glm-billing-label glm-required">
+                    Membership Class
+                </div>
+                <div class="glm-billing-input">
+                    {if $settings.allow_membership_choice}
+                        {foreach $payable_types as $type}
+                            <label>
+                                <input type="radio" name="member_renewing" data-amount="{$type.amount}" value="{$type.id}"
+                                {if $type.id == $member_invoice.id}checked{/if}
+                                />
+                                {$type.name} {$type.amount}
+                            </label>
+                        {/foreach}
+                    {else}
+                        <input type="hidden" name="member_renewing" data-amount="{$member_invoice.amount}" value="{$member_invoice.id}" />
+                        {$member_invoice.name} {$member_invoice.amount}
+                    {/if}
+                </div>
             </div>
-            <div class="glm-billing-input">
-                {if $settings.allow_membership_choice}
-                    {foreach $payable_types as $type}
-                        <label>
-                            <input type="radio" name="member_renewing" data-amount="{$type.amount}" value="{$type.id}"
-                            {if $type.id == $member_invoice.id}checked{/if}
-                            />
-                            {$type.name} {$type.amount}
-                        </label>
-                    {/foreach}
-                {else}
-                    <input type="hidden" name="member_renewing" data-amount="{$member_invoice.amount}" value="{$member_invoice.id}" />
-                    {$member_invoice.name} {$member_invoice.amount}
-                {/if}
+        {else}
+            <div class="glm-billing-field">
+                <div class="glm-billing-label glm-required">
+                    Member Billing #
+                </div>
+                <div class="glm-billing-input">
+                    <input name="account_number" value="" />
+                </div>
+            </div>
+            <div class="glm-billing-field">
+                <div class="glm-billing-label glm-required">
+                    Membership Class
+                </div>
+                <div class="glm-billing-input">
+                    {if $settings.allow_membership_choice}
+                        {foreach $payable_types as $type}
+                            <label>
+                                <input type="radio" name="member_renewing" data-amount="{$type.amount}" value="{$type.id}" />
+                                {$type.name} {$type.amount}
+                            </label>
+                        {/foreach}
+                    {/if}
+                </div>
             </div>
-        </div>
         {/if}
 
         {if $employees}
@@ -67,8 +90,8 @@
             </div>
         </div>
 
-{* Include the Payment Form template *}
-{include file='common/billing/paymentForm.html'}
+        {* Include the Payment Form template *}
+        {include file='common/billing/paymentForm.html'}
 
         <input class="button button-primary" type="submit" value="Renew">
 
@@ -89,6 +112,8 @@ jQuery(document).ready(function($){
         currency: 'USD',
         minimunFractionDigits: 2
     });
+
+    // Generate the total for the renewal.
     function getPageTotal(){
 
         // Calculate the total for this page.
@@ -96,9 +121,12 @@ jQuery(document).ready(function($){
         if ( $('input[name=member_renewing]:checked').length ) {
             var member_renewing_amount = parseFloat( $('input[name=member_renewing]:checked').data('amount') );
         } else {
-            var member_renewing_amount = parseFloat( $('input[name=member_renewing]').data('amount') );
+            var member_renewing_amount = parseFloat( $('input[name=member_renewing]:first').data('amount') );
+            $('input[name=member_renewing]:first').prop( 'checked', true );
         }
 
+        // console.log( 'member_renewing_amount', member_renewing_amount );
+
         // Get each employee and add their amounts.
         $('input[name^=employees]').each(function(){
             var isChecked = $(this).prop('checked');