Update for edit invoice
authorSteve Sutton <ssutton@gmail.com>
Sun, 9 Dec 2018 21:16:56 +0000 (16:16 -0500)
committerSteve Sutton <ssutton@gmail.com>
Sun, 9 Dec 2018 21:16:56 +0000 (16:16 -0500)
Working on line items

models/admin/billing/invoices.php
views/admin/billing/editInvoice.html

index 4a3e053..32d8dcd 100644 (file)
@@ -371,17 +371,94 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
         //     break;
 
         case 'update':
+            $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config );
+            $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config );
+
+            // echo '<pre>' . print_r( $_REQUEST, true ) . '</pre>';
 
-            // Try to update this invoices
-            $invoices = $this->updateEntry($this->invoice_id);
+            if ( isset( $_REQUEST['id'] ) ) {
+                $this->invoice_id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT );
 
-            // Check if that was successful
-            if ($invoices['status']) {
-                $invoiceUpdated = true;
+                // Try to update this invoices
+                $invoices = $this->updateEntry($this->invoice_id);
 
-                $invoices = $this->editEntry($this->invoice_id);
-            } else {
-                $invoiceUpdateError = true;
+                // Check if that was successful
+                if ($invoices['status']) {
+                    $invoiceUpdated = true;
+
+                    $invoices = $this->editEntry($this->invoice_id);
+
+                    echo '<pre>' . print_r( $invoices, true) . '</pre>';
+
+                    // Remove the old line items.
+                    $this->wpdb->delete(
+                        GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'line_items',
+                        array( 'invoice' => $this->invoice_id ),
+                        array( '%d' )
+                    );
+                    // Add the new ones.
+                    if ( isset( $_REQUEST['line_items'] ) ) {
+                        $line_items =  $_REQUEST['line_items'];
+                        if ( is_array( $line_items ) && !empty( $line_items ) ) {
+                            foreach ( $line_items as $key => $line_item ) {
+                                // Get the invoice type
+                                $invoiceType = $InvoiceTypesObj->getEntry( $line_item );
+                                if ( $invoiceType['recurring']['value'] ) {
+                                    $renewal_invoice = true;
+                                }
+                                // Add individual line item record
+                                $this->wpdb->insert(
+                                    GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'line_items',
+                                    array(
+                                        'invoice'        => $this->invoice_id,
+                                        'line_item_type' => $line_item,
+                                        'name'           => $_REQUEST['line_item_name'][$line_item],
+                                        'amount'         => $_REQUEST['line_item_amount'][$line_item],
+                                        'quantity'       => $_REQUEST['line_item_qty'][$line_item],
+                                        'total'          => (float)$_REQUEST['line_item_qty'][$line_item] * (float)$_REQUEST['line_item_amount'][$line_item],
+                                        'created'        => date('Y-m-d'),
+                                        'first_due_date' => $_REQUEST['due_date'],
+                                        'next_due_date'  => $_REQUEST['due_date'],
+                                        'recurring'      => $invoiceType['recurring']['value'],
+                                        'recurrence'     => $invoiceType['recurrence']
+                                    ),
+                                    array(
+                                        '%d', // invoice
+                                        '%d', // line_item_type
+                                        '%s', // name
+                                        '%s', // amount
+                                        '%d', // quantity
+                                        '%d', // total
+                                        '%s', // created
+                                        '%s', // first_due_date
+                                        '%s', // next_due_date
+                                        '%d', // recurring
+                                        '%d', // recurrence
+                                    )
+                                );
+                            }
+                            // 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' )
+                                );
+                            }
+                        }
+                    }
+                    // Here we're going to generate the invoice total.
+                    $totals = $this->generateInvoiceTotal( $this->invoice_id );
+                    // echo '<pre>' . print_r( $totals, true ) . '</pre>';
+                    // Now we have a total for the invoice we can record the transaction
+                    $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config );
+                    // Calling the support class to save the invoice to transaction table.
+                    $BillingSupport->recordInvoice( $this->invoice_id, $_REQUEST['account'], $totals['amount_total'] );
+                } else {
+                    $invoiceUpdateError = true;
+                }
             }
 
             // Set the view file to editInvoice
index 540a74e..9521d42 100644 (file)
@@ -13,6 +13,7 @@
             <input type="hidden" name="option" value="update">
             <input type="hidden" name="id" value="{$invoice_id}">
             <input type="hidden" name="account" value="{$invoices.fieldData.account.value}">
+            <input type="hidden" name="transaction_time" value="{$invoices.fieldData.transaction_time.mysql_datetime}">
         {else}
             <input type="hidden" name="option" value="insert">
         {/if}
@@ -36,7 +37,7 @@
                         Due Date
                     </div>
                     <div class="glm-columns glm-small-12 glm-large-8">
-                        <input class="datepicker" id="invoice_due_date" type="text" name="due_date" required>
+                        <input class="datepicker" id="invoice_due_date" type="text" name="due_date" {if $invoice_id}value="{$invoices.fieldData.due_date.date}"{/if} required>
                     </div>
                 </div>
                 <div class="glm-row">
@@ -248,6 +249,8 @@ jQuery(document).ready(function($){
         { label: "{$m.name|unescape:'html'|replace:'"':''}", value: "{$m.name|unescape:'html'|replace:'"':''}", id: '{$m.id}' },
     {/foreach}
     ];
+
+
     // Setup autocomplete for the member account input.
     $('#glm_member_accounts').autocomplete({
         source: availableAccounts,
@@ -407,6 +410,14 @@ jQuery(document).ready(function($){
         '</div>');
     }
 
+    // Setup if doing edit
+    {if $invoice_id && isset($lineItems)}
+        {foreach $lineItems as $item}
+            addLineItemToInvoice( invoiceTypeJSON[{$item.line_item_type}] );
+        {/foreach}
+        totalInvoice();
+    {/if}
+
     // Add the price to the right of the new selection for add line item.
     $('#line_item_type').change(function(){
         if ( $(this).val() == 'new' ) {