From 422c134497c991e3ff52c3938c9228a0b4731fda Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 6 Dec 2017 16:53:16 -0500 Subject: [PATCH] WIP invioces Working on the invoices. creating some invoices and adding line items for each one. still working out how need to create member account entry when none exists. This could be done on the create invoice page so they pick a member name and fill in the account data. I don't think I have a field yet for their account number. may need that.Working on the account part of the add invoice page. also trying to add due date to the form but it's not being saved. --- classes/data/dataAccounts.php | 183 +++++++++++++++++++++++++++ classes/data/dataInvoices.php | 53 +++++++- css/admin.css | 3 + models/admin/billing/invoices.php | 77 +++++++++-- views/admin/billing/editInvoice.html | 103 +++++++++++++-- views/admin/billing/invoices.html | 29 ++--- 6 files changed, 406 insertions(+), 42 deletions(-) create mode 100644 classes/data/dataAccounts.php diff --git a/classes/data/dataAccounts.php b/classes/data/dataAccounts.php new file mode 100644 index 0000000..41181cd --- /dev/null +++ b/classes/data/dataAccounts.php @@ -0,0 +1,183 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release SVN: $Id: dataEvents.php,v 1.0 2011/01/25 19:31:47 cscott Exp $ + */ + +/** + * GlmDataBillingManagement class + * + * PHP version 5 + * + * @category Data + * @package GLM Member DB + * @author Chuck Scott + * @license http://www.gaslightmedia.com Gaslightmedia + * @release SVN: $Id: dataMembers.php,v 1.0 2011/01/25 19:31:47 cscott + * Exp $ + */ +class GlmDataAccounts extends GlmDataAbstract +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + /** + * Data Table Name + * + * @var $table + * @access public + */ + public $table; + /** + * Field definitions + * + * 'type' is type of field as defined by the application + * text Regular text field + * pointer Pointer to an entry in another table + * 'filters' is the filter name for a particular filter ID in PHP filter + * functions + * See PHP filter_id() + * + * 'use' is when to use the field + * l = List + * g = Get + * n = New + * i = Insert + * e = Edit + * u = Update + * d = Delete + * a = All + * + * @var $ini + * @access public + */ + public $fields = false; + + /** + * Constructor + * + * @param object $d database connection + * @param array $config Configuration array + * @param bool $limitedEdit Flag to say indicate limited edit requested + * + * @return void + * @access public + */ + public function __construct($wpdb, $config, $limitedEdit = false) + { + + // If this class is not being extended along with existing $wpdb and $config + if (!$this->wpdb) { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + } + + /* + * Table Name + */ + $this->table = GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts'; + + /* + * Table Data Fields + */ + + $this->fields = array ( + + 'id' => array ( + 'field' => 'id', + 'type' => 'integer', + 'view_only' => true, + 'use' => 'a', + ), + + // Account ref to accounts table + 'ref_dest' => array( + 'field' => 'ref_dest', + 'type' => 'pointer', + 'p_table' => GLM_MEMBERS_PLUGIN_DB_PREFIX . 'members', + 'p_field' => 'name', + 'p_orderby' => 'name', + 'p_blank' => true, + 'force_list' => true, + 'required' => false, + 'use' => 'a' + ), + + // Name of red_dest + 'ref_name' => array( + 'field' => 'ref_name', + 'type' => 'text', + 'use' => 'a', + ), + + // Anniversary Date + 'anniversary_date' => array( + 'field' => 'anniversary_date', + 'type' => 'date', + 'use' => 'a', + ), + + // Payment Data + 'payment_data' => array( + 'field' => 'payment_data', + 'type' => 'text', + 'use' => 'a', + ), + + // Email + 'email' => array( + 'field' => 'email', + 'type' => 'text', + 'use' => 'a', + ), + + ); + + + } + + /* + * Entry Post Processing Call-Back Method + * + * Perform post-processing for all result entries. + * + * In this case we're using it to append an array of category + * data to each member result and also sort by member name. + * + * @param array $r Array of field result data for a single entry + * @param string $a Action being performed (l, i, g, ...) + * + * @return object Class object + * + */ + public function entryPostProcessing($r, $a) + { + return $r; + } + +} diff --git a/classes/data/dataInvoices.php b/classes/data/dataInvoices.php index 3a0f8fb..948f0fd 100644 --- a/classes/data/dataInvoices.php +++ b/classes/data/dataInvoices.php @@ -135,7 +135,7 @@ class GlmDataInvoices extends GlmDataAbstract 'use' => 'a' ), - // Anniversary Date For ref_dest + // Amount Total 'amount_total' => array( 'field' => 'amount_total', 'type' => 'text', @@ -156,7 +156,7 @@ class GlmDataInvoices extends GlmDataAbstract 'use' => 'a', ), - // paid + // Paid 'paid' => array( 'field' => 'paid', 'type' => 'checkbox', @@ -207,7 +207,56 @@ class GlmDataInvoices extends GlmDataAbstract */ public function entryPostProcessing($r, $a) { + $sql = " + SELECT M.name + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members M, + " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices I, + " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts A + WHERE A.ref_dest = M.id + AND I.account = A.id"; + $r['member_name'] = $this->wpdb->get_var( $sql ); return $r; } + /** + * generateInvoiceTotal + * + * Grab all the line items and create the totals for invoice. + * + * @access public + * @return array + */ + public function generateInvoiceTotal( $id ) + { + $amount_total = $balance = 0.00; + $line_items = $this->wpdb->get_results( + $this->wpdb->prepare( + "SELECT * + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "line_items LI + WHERE LI.invoice = %d", + $id + ) + , ARRAY_A + ); + foreach( $line_items as $line_item ) { + $amount_total += $line_item['amount']; + } + $balance = $amount_total; + $this->wpdb->update( + GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'invoices', + array( + 'amount_total' => $amount_total, + 'balance' => $balance, + ), + array( 'id' => $id ), + array( '%s', '%s' ), + array( '%d' ) + ); + return array( + 'amount_total' => $amount_total, + 'balance' => $balance, + ); + } + + } diff --git a/css/admin.css b/css/admin.css index 486cae0..d8d4072 100644 --- a/css/admin.css +++ b/css/admin.css @@ -2,3 +2,6 @@ #billing-invoice-form .glm-column { padding: 0; } +#billing-invoice-form input[name^="line_item_qty"] { + width: 50px; +} diff --git a/models/admin/billing/invoices.php b/models/admin/billing/invoices.php index dc6ae5a..24ebda8 100644 --- a/models/admin/billing/invoices.php +++ b/models/admin/billing/invoices.php @@ -16,6 +16,7 @@ // Load Contacts data class require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/data/dataInvoices.php'; require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/data/dataInvoiceTypes.php'; +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/data/dataAccounts.php'; class GlmMembersAdmin_billing_invoices extends GlmDataInvoices { @@ -37,10 +38,10 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices /** * Transactions ID * - * @var $transactionID + * @var $invoice_id * @access public */ - public $transactionID = false; + public $invoice_id = false; /** * Constructor @@ -90,7 +91,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices { $option = 'list'; - $this->transactionID = false; + $this->invoice_id = false; $haveInvoices = false; $invoiceUpdated = false; $invoiceUpdateError = false; @@ -115,6 +116,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices $invTypes = array(); $invoiceTypes = false; $invoiceTypeJSON = ''; + $accounts = false; // Get any provided option if (isset($_REQUEST['option'])) { @@ -129,8 +131,12 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices $view = 'editInvoice'; $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config ); $invoiceTypes = $InvoiceTypesObj->getList(); + // Sort the types by parent child $invoiceTypes = $InvoiceTypesObj->sortParentChild($invoiceTypes); - // echo '
$invoiceTypes: ' . print_r( $invoiceTypes, true ) . '
'; + // Need to get the accounts + $Accounts = new GlmDataAccounts( $this->wpdb, $this->config ); + $accounts = $Accounts->getList(); + // echo '
$accounts: ' . print_r( $accounts, true ) . '
'; if ( isset( $invoiceTypes ) ) { foreach ( $invoiceTypes as $invoiceType ) { $invTypes[$invoiceType['id']] = array( @@ -139,7 +145,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices 'amount' => $invoiceType['amount'], ); } - $invoiceTypeJSON = json_encode( $invTypes, true ); + $invoiceTypeJSON = json_encode( $invTypes, JSON_NUMERIC_CHECK ); } break; @@ -147,13 +153,54 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices // Set transaction_time to current time. $_REQUEST['transaction_time'] = date('Y-m-d H:i:s'); echo '
' . print_r( $_REQUEST, true ) . '
'; + $_REQUEST['due_date'] = date('Y-m-d', strtotime($_REQUEST['due_date'])); $invoices = $this->insertEntry(); - $this->transactionID = $invoices['fieldData']['id']; + $this->invoice_id = $invoices['fieldData']['id']; + // After the Invoice is created need to add each line item + 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 ) { + // 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], + ), + array( + '%d', + '%d', + '%s', + '%s', + '%d', + ) + ); + } + } + } + $this->generateInvoiceTotal( $this->invoice_id ); $view = 'editInvoice'; + $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config ); + $invoiceTypes = $InvoiceTypesObj->getList(); + $invoiceTypes = $InvoiceTypesObj->sortParentChild($invoiceTypes); + if ( isset( $invoiceTypes ) ) { + foreach ( $invoiceTypes as $invoiceType ) { + $invTypes[$invoiceType['id']] = array( + 'id' => $invoiceType['id'], + 'name' => $invoiceType['name'], + 'amount' => $invoiceType['amount'], + ); + } + $invoiceTypeJSON = json_encode( $invTypes, true ); + } break; case 'edit': - $invoices = $this->editEntry($this->transactionID); + $invoices = $this->editEntry($this->invoice_id); // If we have a good invoices if ($invoices['status']) { @@ -172,13 +219,13 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices case 'update': // Try to update this invoices - $invoices = $this->updateEntry($this->transactionID); + $invoices = $this->updateEntry($this->invoice_id); // Check if that was successful if ($invoices['status']) { $invoiceUpdated = true; - $invoices = $this->editEntry($this->transactionID); + $invoices = $this->editEntry($this->invoice_id); } else { $invoiceUpdateError = true; } @@ -188,8 +235,8 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices break; case 'delete': - - $invoices = $this->deleteTransactions($this->transactionID); + // Need to remove any line items for the invoice alse + // $invoices = $this->deleteTransactions($this->invoice_id); if ($invoices) { $invoiceDeleted = true; @@ -275,10 +322,17 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices } unset($invoicesResult); + // echo '
$invoices: ' . print_r( $invoices, true ) . '
'; + + foreach ( $invoices as $invoice ) { + // $this->generateInvoiceTotal( $invoice['id'] ); + } + break; } + $templateData = array( 'option' => $option, 'invoices' => $invoices, @@ -300,6 +354,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices 'limit' => $limit, 'invoiceTypeJSON' => $invoiceTypeJSON, 'invoiceTypes' => $invoiceTypes, + 'accounts' => $accounts, ); // Return status, any suggested view, and any data to controller diff --git a/views/admin/billing/editInvoice.html b/views/admin/billing/editInvoice.html index a6f328b..7d1981c 100644 --- a/views/admin/billing/editInvoice.html +++ b/views/admin/billing/editInvoice.html @@ -17,17 +17,29 @@ {/if}
+
-
+
Member Account
+
+
+ Due Date +
+
+ +
+
+
@@ -41,7 +53,7 @@
-
Total Amount:
+
Total Amount:   
$0.00
@@ -84,20 +96,39 @@