From: Steve Sutton Date: Sun, 9 Dec 2018 21:16:56 +0000 (-0500) Subject: Update for edit invoice X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=c8dead05aa6f4533ecf5d0e9129350b554bae1c9;p=WP-Plugins%2Fglm-member-db-billing.git Update for edit invoice Working on line items --- diff --git a/models/admin/billing/invoices.php b/models/admin/billing/invoices.php index 4a3e053..32d8dcd 100644 --- a/models/admin/billing/invoices.php +++ b/models/admin/billing/invoices.php @@ -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 '
' . print_r( $_REQUEST, true ) . '
'; - // 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 '
' . print_r( $invoices, true) . '
'; + + // 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 '
' . print_r( $totals, true ) . '
'; + // 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 diff --git a/views/admin/billing/editInvoice.html b/views/admin/billing/editInvoice.html index 540a74e..9521d42 100644 --- a/views/admin/billing/editInvoice.html +++ b/views/admin/billing/editInvoice.html @@ -13,6 +13,7 @@ + {else} {/if} @@ -36,7 +37,7 @@ Due Date
- +
@@ -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($){ '
'); } + // 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' ) {