Update invoice create page to use renewal flag.
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 5 Aug 2019 20:39:41 +0000 (16:39 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 5 Aug 2019 20:39:41 +0000 (16:39 -0400)
Properly flag an invoice so it sets renewal date when paid.

classes/billingSupport.php
classes/data/dataInvoices.php
models/admin/billing/invoices.php
views/admin/billing/editInvoice.html
views/admin/billing/invoices.html

index 80f361e..334f89e 100644 (file)
@@ -241,14 +241,17 @@ class GlmBillingSupport
         );
 
         // find out if this invoice is a renewal.
-        $is_renewal = $this->wpdb->get_var(
+        $invoiceData = $this->wpdb->get_var(
             $this->wpdb->prepare(
-                "SELECT renewal
+                "SELECT renewal,account
                    FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices
                   WHERE id = %d",
                 $invoice_id
             )
         );
+        $account_id = $invoiceData['account'];
+        $is_renewal = $invoiceData['renewal'];
+        $this->updateAccountRenewalDate( $invoiceData['account'] );
 
         // If this is a renewal then get all line items and update their renewal dates.
         if ( $is_renewal ) {
@@ -263,7 +266,9 @@ class GlmBillingSupport
             );
             if ( $line_items ) {
                 foreach ( $line_items as $account ) {
-                    $this->updateAccountRenewalDate( $account['account'] );
+                    if ( $account['account'] && $account['account'] != $account_id ) {
+                        $this->updateAccountRenewalDate( $account['account'] );
+                    }
                 }
             }
         }
@@ -1141,7 +1146,7 @@ class GlmBillingSupport
                 'recurrence'     => $new_member_invoice_type['recurrence'],
             )
         );
-        echo '<pre>$invoice_id: ' . print_r( $invoice_id, true ) . '</pre>';
+        // echo '<pre>$invoice_id: ' . print_r( $invoice_id, true ) . '</pre>';
         if ( $invoice_id ) {
             if ( isset( $employees ) && is_array( $employees ) ) {
                 // Add line items for each employee.
index 9a5345e..374661b 100644 (file)
@@ -177,6 +177,14 @@ class GlmDataInvoices extends GlmDataAbstract
                 'use'   => 'a',
             ),
 
+            // Renewal
+            'renewal' => array(
+                'field'   => 'renewal',
+                'type'    => 'checkbox',
+                'default' => true,
+                'use'     => 'a',
+            ),
+
             // Recurring
             'recurring' => array(
                 'field'   => 'recurring',
index 124d7e9..03627b5 100644 (file)
@@ -374,40 +374,40 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
                         );
                     }
                     // If one of the invoice line items is recurring then mark the invoice as renewal.
-                    if ( $renewal_invoice ) {
-                        $this->wpdb->update(
-                            GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'invoices',
-                            array( 'renewal' => 1 ),
-                            array( 'id' => $this->invoice_id ),
-                            array( '%s' ),
-                            array( '%d' )
-                        );
-                        // Check the billing account and see if it needs to have invoice_type set.
-                        $invoice_type_id = $this->wpdb->get_var(
-                            $this->wpdb->prepare(
-                                "SELECT invoice_type
-                                   FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
-                                 WHERE id = %d",
-                                $_REQUEST['account']
-                            )
-                        );
-                        if ( !$invoice_type_id ) {
-                            // Then setup the invoice_type id
-                            $this->wpdb->update(
-                                GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts',
-                                array(
-                                    'invoice_type'  => $line_item,
-                                    'email_invoice' => true
-                                ),
-                                array( 'id' => $_REQUEST['account'] ),
-                                array(
-                                    '%d',
-                                    '%d'
-                                ),
-                                array( '%d' )
-                            );
-                        }
-                    }
+                    // if ( $renewal_invoice ) {
+                    //     $this->wpdb->update(
+                    //         GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'invoices',
+                    //         array( 'renewal' => 1 ),
+                    //         array( 'id' => $this->invoice_id ),
+                    //         array( '%s' ),
+                    //         array( '%d' )
+                    //     );
+                    //     // Check the billing account and see if it needs to have invoice_type set.
+                    //     $invoice_type_id = $this->wpdb->get_var(
+                    //         $this->wpdb->prepare(
+                    //             "SELECT invoice_type
+                    //                FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
+                    //              WHERE id = %d",
+                    //             $_REQUEST['account']
+                    //         )
+                    //     );
+                    //     if ( !$invoice_type_id ) {
+                    //         // Then setup the invoice_type id
+                    //         $this->wpdb->update(
+                    //             GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts',
+                    //             array(
+                    //                 'invoice_type'  => $line_item,
+                    //                 'email_invoice' => true
+                    //             ),
+                    //             array( 'id' => $_REQUEST['account'] ),
+                    //             array(
+                    //                 '%d',
+                    //                 '%d'
+                    //             ),
+                    //             array( '%d' )
+                    //         );
+                    //     }
+                    // }
                 }
             }
             $invoiceAdded = true;
@@ -526,13 +526,13 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
                             }
                             // If one of the invoice line items is recurring then mark the invoice as renewal.
                             if ( $renewal_invoice ) {
-                                $this->wpdb->update(
-                                    GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'invoices',
-                                    array( 'renewal' => 1 ),
-                                    array( 'id' => $this->invoice_id ),
-                                    array( '%s' ),
-                                    array( '%d' )
-                                );
+                                // $this->wpdb->update(
+                                //     GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'invoices',
+                                //     array( 'renewal' => 1 ),
+                                //     array( 'id' => $this->invoice_id ),
+                                //     array( '%s' ),
+                                //     array( '%d' )
+                                // );
                             }
                         }
                     }
index 100b709..b60e9ad 100644 (file)
                 </label>
                 <textarea id="notes" name="notes">{if $invoices.fieldData.notes}{$invoices.fieldData.notes}{/if}</textarea>
             </div>
+
+            {* Renewal *}
+            {$ui = [
+                'value'     => $invoices.fieldData.renewal.value|default:true,
+                'field'     => 'renewal',
+                'label'     => 'Renewal',
+                'required'  => false,
+                'errorText' => 'Renewal is Required',
+                'dataError' => '',
+                'tip'       => 'On will set the accounts renewal date when this invoice is paid'
+            ]}
+            {include file='ui/f6/checkbox.html'}
+
             <div class="cell small-12 medium-4">
                 <a id="newLineItemButton" class="glm-billing-add-line-item">Add Line Item</a><br>
                 <a id="newCustomLineItemButton" class="glm-billing-add-line-item">Create New Line Item</a>
index 6ce51c2..34e1cc8 100644 (file)
             <th width="80">Due Date</th>
             <th width="120">Amount Total</th>
             <th width="120">Balance</th>
+            <th width="120">Renewal</th>
         </tr>
     </thead>
     <tbody>
                     <td> {$t.due_date.date} </td>
                     <td> {$t.amount_total} </td>
                     <td> <span id="balance-{$t.id}">{$t.balance}</span> </td>
+                    <td> {if $t.renewal.value}Yes{else}No{/if} </td>
                 </tr>
                 <tr id="invoice-container-{$t.id}" class="glm-invoice-links hide-for-large {if $t@iteration is div by 2} alternate{/if}">
-                    <td colspan="7">
+                    <td colspan="8">
                         <span class="account-dashboard-link">
                             <a href="{$adminUrl}?page=glm-members-admin-menu-billing&glm_action=invoices&option=edit&id={$t.id}">Edit</a> |
                         </span>