From: Steve Sutton Date: Fri, 2 Mar 2018 18:01:47 +0000 (-0500) Subject: Adding account list page and update search filters invoices. X-Git-Tag: v1.0.0^2~167 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=204a1aacb0bfa894e43f7330527d72e34d782962;p=WP-Plugins%2Fglm-member-db-billing.git Adding account list page and update search filters invoices. Update search filters for invoices page. Add account list and search for anniversary and renewal dates. --- diff --git a/models/admin/ajax/account.php b/models/admin/ajax/account.php index 856d9d5..2d953af 100644 --- a/models/admin/ajax/account.php +++ b/models/admin/ajax/account.php @@ -90,7 +90,8 @@ class GlmMembersAdmin_ajax_account extends GlmDataAccounts } else { $accountAdded = true; $return = array( - 'status' => true + 'status' => true, + 'account' => $account ); } break; diff --git a/models/admin/billing/accounts.php b/models/admin/billing/accounts.php new file mode 100644 index 0000000..96c1cc3 --- /dev/null +++ b/models/admin/billing/accounts.php @@ -0,0 +1,445 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release index.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +// Load Contacts data class +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataAccounts.php'; + +class GlmMembersAdmin_billing_accounts extends GlmDataAccounts +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + /** + * Transactions ID + * + * @var $account_id + * @access public + */ + public $account_id = false; + + /** + * Constructor + * + * This constructor performs the work for this model. This model returns + * an array containing the following. + * + * 'status' + * + * True if successful and false if there was a fatal failure. + * + * 'view' + * + * A suggested view name that the controller should use instead of the + * default view for this model or false to indicate that the default view + * should be used. + * + * 'data' + * + * Data that the model is returning for use in merging with the view to + * produce output. + * + * @wpdb object WordPress database object + * + * @return array Array containing status, suggested view, and any data + */ + public function __construct ($wpdb, $config) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + /* + * Run constructor for the Contacts data class + * + * Note, the third parameter is a flag that indicates to the Contacts + * data class that it should flag a group of fields as 'view_only'. + */ + parent::__construct(false, false, true); + + } + + public function modelAction($actionData = false) + { + + $option = 'list'; + $this->account_id = false; + $haveAccounts = false; + $invoiceUpdated = false; + $invoiceUpdateError = false; + $invoiceAdded = false; + $invoiceAddError = false; + $view = 'accounts'; + $fromDate = false; + $toDate = false; + $filterArchived = false; + $filterPending = false; + $filterFeatured = false; + $invoiceDeleted = false; + $invoiceDeleteError = false; + $invoiceInsertError = false; + $numbDisplayed = false; + $lastDisplayed = false; + $paging = true; + $prevStart = false; + $nextStart = false; + $start = 1; + $limit = 20; // Set to the number of listings per page + $invTypes = array(); + $invoiceTypes = false; + $invoiceTypeJSON = ''; + $accounts = false; + $nonAccountMembers = false; + $invoiceHtml = ''; + $fromDate = ''; + $toDate = ''; + + // Get any provided option + if (isset($_REQUEST['option'])) { + $option = $_REQUEST['option']; + } + + // Do selected option + switch ($option) { + + case 'add': + $accounts = $this->newEntry(); + // Set the view file to editInvoice + $view = 'editInvoice'; + $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config ); + $invoiceTypes = $InvoiceTypesObj->getList(); + // Sort the types by parent child + $invoiceTypes = $InvoiceTypesObj->sortParentChild($invoiceTypes); + // Need to get the accounts + $Accounts = new GlmDataAccounts( $this->wpdb, $this->config ); + $accounts = $Accounts->getList( '', 'ref_name' ); + // echo '
$accounts: ' . print_r( $accounts, true ) . '
'; + + // Need a list of members that don't have an account. + $nonAccountMembers = $this->wpdb->get_results( + "SELECT id,name + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members + WHERE id NOT IN ( + SELECT distinct ref_dest + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts ) + ORDER BY name", + ARRAY_A + ); + // echo '
$nonAccountMembers: ' . print_r( $nonAccountMembers, true ) . '
'; + + if ( isset( $invoiceTypes ) ) { + foreach ( $invoiceTypes as $invoiceType ) { + $invTypes[$invoiceType['id']] = array( + 'id' => $invoiceType['id'], + 'name' => $invoiceType['name'], + 'amount' => $invoiceType['amount'], + ); + } + $invoiceTypeJSON = json_encode( $invTypes, JSON_NUMERIC_CHECK ); + } + break; + + case 'insert': + // Set transaction_time to current time. + $_REQUEST['transaction_time'] = date('Y-m-d H:i:s'); + $_REQUEST['due_date'] = date('Y-m-d', strtotime($_REQUEST['due_date'])); + // echo '
' . print_r( $_REQUEST, true ) . '
'; + $accounts = $this->insertEntry(); + $this->account_id = $accounts['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->account_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], + ), + array( + '%d', + '%d', + '%s', + '%s', + '%d', + '%d' + ) + ); + } + } + } + // Here we're going to generate the invoice total. + $totals = $this->generateInvoiceTotal( $this->account_id ); + // 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->account_id, $_REQUEST['account'], $totals['amount_total'] ); + + // Set the view file to editInvoice + $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': + $accounts = $this->editEntry($this->account_id); + + // If we have a good accounts + if ($accounts['status']) { + $haveTransactions = true; + } + + // If we're locked to a member as a contact user and the accounts member doesn't equal the contact member + if ($lockedToMember && $accounts['fieldData']['ref_dest_id'] != $lockedToMember) { + $haveTransactions = false; + $accounts = false; + } + + // Set the view file to editInvoice + $view = 'editInvoice'; + break; + + case 'update': + + // Try to update this accounts + $accounts = $this->updateEntry($this->account_id); + + // Check if that was successful + if ($accounts['status']) { + $invoiceUpdated = true; + + $accounts = $this->editEntry($this->account_id); + } else { + $invoiceUpdateError = true; + } + + // Set the view file to editInvoice + $view = 'editInvoice'; + + break; + case 'view': + // Call in the support class + $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); + + $invoiceHtml = $BillingSupport->viewInvoice( $_REQUEST['id'] ); + + // Set the file name for the view file. + $view = 'viewInvoice'; + + break; + + case 'delete': + // Need to remove any line items for the invoice also + // $accounts = $this->deleteTransactions($this->account_id); + + if ($accounts) { + $invoiceDeleted = true; + } else { + $invoiceDeleteError = true; + } + + case 'list': + default: + + $where_params = array( 'true' ); + + // Check for paging + if ( isset( $_REQUEST['pageSelect'] ) ) { + $_SESSION['search']['pageSelect'] = $_REQUEST['pageSelect']; + } else if ( isset( $_REQUEST['searched'] ) && !isset( $_REQUEST['pageSelect'] ) ) { + unset( $_SESSION['search']['pageSelect'] ); + } + if ( isset( $_REQUEST['nextStart'] ) ) { + $_SESSION['search']['nextStart'] = $_REQUEST['nextStart']; + } else if ( isset( $_REQUEST['searched'] ) && !isset( $_REQUEST['nextStart'] ) ) { + unset( $_SESSION['search']['nextStart'] ); + } + if ( isset( $_REQUEST['prevStart'] ) ) { + $_SESSION['search']['prevStart'] = $_REQUEST['prevStart']; + } else if ( isset( $_REQUEST['searched'] ) && !isset( $_REQUEST['prevStart'] ) ) { + unset( $_SESSION['search']['prevStart'] ); + } + + // Check if we're doing paging + if (isset($_REQUEST['pageSelect'])) { + // If request is for Next + if ($_REQUEST['pageSelect'][0] == 'N') { + $newStart = $_REQUEST['nextStart'] - 0; + + // Otherwise it must be Previous + } else { + $newStart = $_REQUEST['prevStart'] - 0; + } + + if ($newStart > 0) { + $start = $newStart; + } + } + + if( isset($_SESSION['search']['pageSelect']) ){ + // If request is for Next + if ($_SESSION['search']['pageSelect'][0] == 'N') { + $newStart = $_SESSION['search']['nextStart'] - 0; + + // Otherwise it must be Previous + } else { + $newStart = $_SESSION['search']['prevStart'] - 0; + } + if ($newStart > 0) { + $start = $newStart; + } + } + // Get any search parameters. + $reg_options = array( + 'options' => array( + 'regexp' => '%[0-9]{2}/[0-9]{2}/[0-9]{4}%' + ) + ); + // When searching + if ( isset( $_REQUEST['fromDate'] ) && $_REQUEST['fromDate'] + && $fromDate = filter_var( $_REQUEST['fromDate'], FILTER_VALIDATE_REGEXP, $reg_options ) + ) { + $from_date = date( 'Y-m-d', strtotime( $fromDate ) ); + $where_params[] = "T.renewal_date >= '$from_date'"; + } + if ( isset( $_REQUEST['toDate'] ) && $_REQUEST['toDate'] + && $toDate = filter_var( $_REQUEST['toDate'], FILTER_VALIDATE_REGEXP, $reg_options ) + ) { + $to_date = date( 'Y-m-d', strtotime( $toDate ) ); + $where_params[] = "T.renewal_date <= '$to_date'"; + } + if ( isset( $_REQUEST['annFromDate'] ) && $_REQUEST['annFromDate'] + && $annFromDate = filter_var( $_REQUEST['annFromDate'], FILTER_VALIDATE_REGEXP, $reg_options ) + ) { + $annFrom_date = date( 'Y-m-d', strtotime( $annFromDate ) ); + $where_params[] = "T.anniversary_date >= '$annFrom_date'"; + } + if ( isset( $_REQUEST['annToDate'] ) && $_REQUEST['annToDate'] + && $annToDate = filter_var( $_REQUEST['annToDate'], FILTER_VALIDATE_REGEXP, $reg_options ) + ) { + $annTo_date = date( 'Y-m-d', strtotime( $annToDate ) ); + $where_params[] = "T.anniversary_date <= '$annTo_date'"; + } + + // Build the $where string from the $where_parts array. + // By implode with AND. + $where = implode( ' AND ', $where_params ); + + // Get the list of accounts and determine number of accounts in list + $orderBy = 'ref_name'; + $accountsResult = $this->getList($where, $orderBy, true, 'id', $start, $limit); + + // Get paging results + $numbDisplayed = $accountsResult['returned']; + $lastDisplayed = $accountsResult['last']; + if ($start == 1) { + $prevStart = false; + } else { + $prevStart = $start - $limit; + if ($start < 1) { + $start = 1; + } + } + if ($accountsResult['returned'] == $limit) { + $nextStart = $start + $limit; + } + + // since we're doing paging, we have to break out just the accounts data + $accounts = $accountsResult['list']; + if (count($accounts)>0) { + $haveAccounts = true; + } + unset($accountsResult); + + // echo '
$accounts: ' . print_r( $accounts, true ) . '
'; + + break; + + } + + + $templateData = array( + 'option' => $option, + 'account_id' => $this->account_id, + 'accounts' => $accounts, + 'nonAccountMembers' => $nonAccountMembers, + 'haveAccounts' => $haveAccounts, + 'invoiceUpdated' => $invoiceUpdated, + 'invoiceUpdateError' => $invoiceUpdateError, + 'invoiceAdded' => $invoiceAdded, + 'invoiceAddError' => $invoiceAddError, + 'invoiceInsertError' => $invoiceInsertError, + 'invoiceDeleted' => $invoiceDeleted, + 'invoiceDeleteError' => $invoiceDeleteError, + 'invoiceInvoiceError' => $invoiceDeleteError, + 'numbDisplayed' => $numbDisplayed, + 'lastDisplayed' => $lastDisplayed, + 'paging' => $paging, + 'prevStart' => $prevStart, + 'nextStart' => $nextStart, + 'start' => $start = 1, + 'limit' => $limit, + 'invoiceTypeJSON' => $invoiceTypeJSON, + 'invoiceTypes' => $invoiceTypes, + 'accounts' => $accounts, + 'invoiceHtml' => $invoiceHtml, + 'fromDate' => $fromDate, + 'toDate' => $toDate, + 'annFromDate' => $annFromDate, + 'annToDate' => $annToDate, + ); + + // Return status, any suggested view, and any data to controller + return array( + 'status' => true, + 'modelRedirect' => false, + 'view' => "admin/billing/$view.html", + 'data' => $templateData + ); + + } + +} diff --git a/models/admin/billing/invoices.php b/models/admin/billing/invoices.php index bc83e53..5eb9656 100644 --- a/models/admin/billing/invoices.php +++ b/models/admin/billing/invoices.php @@ -287,7 +287,11 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices case 'list': default: - $where = 'true'; + // Need to get the accounts + $Accounts = new GlmDataAccounts( $this->wpdb, $this->config ); + $accounts = $Accounts->getList(); + + $where_params = array( 'true' ); // Check for paging if ( isset( $_REQUEST['pageSelect'] ) ) { @@ -336,6 +340,37 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices } } + // Get any search parameters. + $reg_options = array( + 'options' => array( + 'regexp' => '%[0-9]{2}/[0-9]{2}/[0-9]{4}%' + ) + ); + if ( isset( $_REQUEST['fromDate'] ) + && $_REQUEST['fromDate'] + && $fromDate = filter_var( $_REQUEST['fromDate'], FILTER_VALIDATE_REGEXP, $reg_options ) + ) { + $from_date = date( 'Y-m-d', strtotime( $fromDate ) ); + $where_params[] = "T.due_date >= '$from_date'"; + } + if ( isset( $_REQUEST['toDate'] ) + && $_REQUEST['toDate'] + && $toDate = filter_var( $_REQUEST['toDate'], FILTER_VALIDATE_REGEXP, $reg_options ) + ) { + $to_date = date( 'Y-m-d', strtotime( $toDate ) ); + $where_params[] = "T.due_date <= '$to_date'"; + } + if ( isset( $_REQUEST['filterAccounts'] ) + && $filterAccounts = filter_var( $_REQUEST['filterAccounts'], FILTER_VALIDATE_INT ) + ) { + $accounts[$filterAccounts]['selected'] = true; + $where_params[] = "T.account = $filterAccounts"; + } + + // Build the $where from $where_params array. + // By imploding them with AND between each array element. + $where = implode( ' AND ', $where_params ); + // Get the list of invoices and determine number of invoices in list $orderBy = 'transaction_time desc'; $invoicesResult = $this->getList($where, $orderBy, true, 'id', $start, $limit); diff --git a/setup/validActions.php b/setup/validActions.php index ce0161e..0f97213 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -77,6 +77,7 @@ $glmMembersBillingAddOnValidActions = array( 'list' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'invoices' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'payments' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + 'accounts' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'logs' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, ), 'member' => array( diff --git a/views/admin/billing/accounts.html b/views/admin/billing/accounts.html new file mode 100644 index 0000000..a39dc8a --- /dev/null +++ b/views/admin/billing/accounts.html @@ -0,0 +1,88 @@ +{include file='admin/billing/header.html'} + +

Accounts

+ +
+ + + + + + + + +
+

Search Renewal date

+ + From Date: + To Date: + +
+

Search Anniversary date

+ + From Date: + To Date: + +
+ + + + +
+
+ + {if $paging} + + + {/if} +
+ + + + + + + + + + + + + {if $haveAccounts} + {assign var="i" value="0"} + {foreach $accounts as $t} + {if $i++ is odd by 1} + + {else} + + {/if} + + + + + + + {/foreach} + {else} + + {/if} + +
IDMember NameBilling EmailAnniversary DateRenewal Date
{$t.id} {$t.ref_name} {$t.email} {$t.anniversary_date.date} {$t.renewal_date.date}
(no Invoice Types listed)
+ + {if $paging} + + + {/if} + +
+ + + +{include file='admin/footer.html'} diff --git a/views/admin/billing/editInvoice.html b/views/admin/billing/editInvoice.html index 1d471cf..34607d5 100644 --- a/views/admin/billing/editInvoice.html +++ b/views/admin/billing/editInvoice.html @@ -26,7 +26,9 @@ New Account @@ -75,22 +77,12 @@
-

* required.

+

* required

+ - - - - - + @@ -184,6 +176,26 @@
Member - -
NameMember Name