From 03f91a3949e13ceca11de5dfbaab382563816207 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Mon, 11 Dec 2017 16:39:30 -0500 Subject: [PATCH] updates for statements Update the member statements for invoices and payments. fix the db update script by naming it correctly. --- classes/billingSupport.php | 40 ++- classes/data/dataPayments.php | 197 +++++++++++ models/admin/billing/payments.php | 316 ++++++++++++++++++ models/admin/member/billing.php | 4 + ..._V0.0.1.sql => update_database_V0.0.2.sql} | 0 setup/validActions.php | 1 + views/admin/billing/editPayment.html | 78 +++++ views/admin/billing/header.html | 1 + views/admin/billing/paymentHeader.html | 6 + views/admin/billing/payments.html | 46 +++ views/admin/billing/statements.html | 26 +- 11 files changed, 698 insertions(+), 17 deletions(-) create mode 100644 classes/data/dataPayments.php create mode 100644 models/admin/billing/payments.php rename setup/databaseScripts/{update_database_V0.0.1.sql => update_database_V0.0.2.sql} (100%) create mode 100644 views/admin/billing/editPayment.html create mode 100644 views/admin/billing/paymentHeader.html create mode 100644 views/admin/billing/payments.html diff --git a/classes/billingSupport.php b/classes/billingSupport.php index 18abc6d..0189b11 100644 --- a/classes/billingSupport.php +++ b/classes/billingSupport.php @@ -98,8 +98,8 @@ class GlmBillingSupport * @access public * @return void */ - public function recordPayment( $invoice_id, $payment ) { - $this->recordTransaction( $this->config['transaction_numb']['Payment'], $invoice_id, null, $payment ); + public function recordPayment( $payment_id, $account, $payment ) { + $this->recordTransaction( $this->config['transaction_numb']['Payment'], $payment_id, $account, null, $payment ); } /** @@ -164,6 +164,9 @@ class GlmBillingSupport */ public function getStatements( $ref_dest ) { + $balance_due = (float)0.00; + $invoice = false; + $payment = false; // First, need to get the Account id for this ref_dest $account_data = $this->getAccountByRefDest( $ref_dest ); @@ -182,9 +185,16 @@ class GlmBillingSupport $type = $transaction['type']; switch ( $type ){ case $this->config['transaction_numb']['Invoice']: - $transaction['transaction_data'] = $this->getInvoiceById( $transaction['type_id'] ); + $invoice = $this->getInvoiceById( $transaction['type_id'] ); + $balance_due = $balance_due + $invoice['amount_total']; + // add to the $transaction array + $transaction['transaction_data'] = $invoice; break; case $this->config['transaction_numb']['Payment']: + $payment = $this->getPaymentById( $transaction['type_id'] ); + $balance_due = $balance_due - $payment['amount']; + // add to the $transaction array + $transaction['transaction_data'] = $payment; break; } } @@ -193,6 +203,7 @@ class GlmBillingSupport return array( 'account_data' => $account_data, 'transactions' => $transactions, + 'balance_due' => $balance_due, ); } @@ -236,7 +247,7 @@ class GlmBillingSupport "SELECT * FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "transactions WHERE account = %d - ORDER BY transaction_time DESC", + ORDER BY transaction_time ASC", $account ), ARRAY_A @@ -266,5 +277,26 @@ class GlmBillingSupport ); } + /** + * getPaymentById + * + * Get the payment by it's id + * + * @param mixed $payment_id + * @access public + * @return void + */ + public function getPaymentById( $payment_id ) + { + return $this->wpdb->get_row( + $this->wpdb->prepare( + "SELECT * + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "payments + WHERE id = %d", + $payment_id + ), + ARRAY_A + ); + } } diff --git a/classes/data/dataPayments.php b/classes/data/dataPayments.php new file mode 100644 index 0000000..5dfb432 --- /dev/null +++ b/classes/data/dataPayments.php @@ -0,0 +1,197 @@ + + * @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 GlmDataPayments 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 . 'payments'; + + /* + * Table Data Fields + */ + + $this->fields = array ( + + 'id' => array ( + 'field' => 'id', + 'type' => 'integer', + 'view_only' => true, + 'use' => 'a', + ), + + // Transaction time + 'transaction_time' => array( + 'field' => 'transaction_time', + 'type' => 'datetime', + 'use' => 'a', + ), + + // Account ref to accounts table + 'account' => array( + 'field' => 'account', + 'type' => 'pointer', + 'p_table' => GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts', + 'p_field' => 'ref_dest', + 'p_orderby' => 'ref_name', + 'p_blank' => true, + 'force_list' => true, + 'required' => false, + 'use' => 'a' + ), + + // Account ref to invoice table + 'invoice' => array( + 'field' => 'invoice', + 'type' => 'pointer', + 'p_table' => GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'invoices', + 'p_field' => 'id', + 'p_orderby' => 'transaction_time', + 'p_blank' => true, + 'force_list' => true, + 'required' => false, + 'use' => 'a' + ), + + // Amount Total + 'amount' => array( + 'field' => 'amount', + 'type' => 'text', + 'use' => 'a', + ), + + // Payment Method + 'payment_method' => array( + 'field' => 'payment_method', + 'type' => 'text', + 'use' => 'a', + ), + + // Payment Data + 'payment_data' => array( + 'field' => 'payment_data', + '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/models/admin/billing/payments.php b/models/admin/billing/payments.php new file mode 100644 index 0000000..0eed3b2 --- /dev/null +++ b/models/admin/billing/payments.php @@ -0,0 +1,316 @@ + + * @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/dataPayments.php'; +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataAccounts.php'; +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/billingSupport.php'; + +class GlmMembersAdmin_billing_payments extends GlmDataPayments +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + /** + * Transactions ID + * + * @var $invoice_id + * @access public + */ + public $invoice_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->invoice_id = false; + $havePayments = false; + $invoiceUpdated = false; + $invoiceUpdateError = false; + $invoiceAdded = false; + $invoiceAddError = false; + $view = 'payments'; + $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; + + // Get any provided option + if (isset($_REQUEST['option'])) { + $option = $_REQUEST['option']; + } + + // Do selected option + switch ($option) { + + case 'add': + $payments = $this->newEntry(); + $view = 'editPayment'; + // Need to get the accounts + $Accounts = new GlmDataAccounts( $this->wpdb, $this->config ); + $accounts = $Accounts->getList(); + break; + + case 'insert': + // Set transaction_time to current time. + $_REQUEST['transaction_time'] = date('Y-m-d H:i:s'); + echo '
' . print_r( $_REQUEST, true ) . '
'; + $payments = $this->insertEntry(); + $this->payment_id = $payments['fieldData']['id']; + $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); + $BillingSupport->recordPayment( $this->payment_id, $_REQUEST['account'], $payments['fieldData']['amount'] ); + echo '
$payments: ' . print_r( $payments, true ) . '
'; + + $view = 'editPayment'; + break; + + case 'edit': + $payments = $this->editEntry($this->invoice_id); + + // If we have a good payments + if ($payments['status']) { + $haveTransactions = true; + } + + // If we're locked to a member as a contact user and the payments member doesn't equal the contact member + if ($lockedToMember && $payments['fieldData']['ref_dest_id'] != $lockedToMember) { + $haveTransactions = false; + $payments = false; + } + + $view = 'editPayment'; + break; + + case 'update': + + // Try to update this payments + $payments = $this->updateEntry($this->invoice_id); + + // Check if that was successful + if ($payments['status']) { + $invoiceUpdated = true; + + $payments = $this->editEntry($this->invoice_id); + } else { + $invoiceUpdateError = true; + } + + $view = 'editPayment'; + + break; + + case 'delete': + // Need to remove any line items for the invoice alse + // $payments = $this->deleteTransactions($this->invoice_id); + + if ($payments) { + $invoiceDeleted = true; + } else { + $invoiceDeleteError = true; + } + + case 'list': + default: + + $where = '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 the list of payments and determine number of payments in list + $orderBy = 'transaction_time asc'; + $paymentsResult = $this->getList($where, $orderBy, true, 'id', $start, $limit); + + // Get paging results + $numbDisplayed = $paymentsResult['returned']; + $lastDisplayed = $paymentsResult['last']; + if ($start == 1) { + $prevStart = false; + } else { + $prevStart = $start - $limit; + if ($start < 1) { + $start = 1; + } + } + if ($paymentsResult['returned'] == $limit) { + $nextStart = $start + $limit; + } + + // since we're doing paging, we have to break out just the payments data + $payments = $paymentsResult['list']; + if (count($payments)>0) { + $havePayments = true; + } + unset($paymentsResult); + + // echo '
$payments: ' . print_r( $payments, true ) . '
'; + + break; + + } + + + $templateData = array( + 'option' => $option, + 'invoice_id' => $this->invoice_id, + 'payments' => $payments, + 'havePayments' => $havePayments, + 'invoiceUpdated' => $invoiceUpdated, + 'invoiceUpdateError' => $invoiceUpdateError, + 'invoiceAdded' => $invoiceAdded, + 'invoiceAddError' => $invoiceAddError, + 'invoiceInsertError' => $invoiceInsertError, + // 'numbTransactions' => $numbTransactions, + '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, + ); + + // 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/member/billing.php b/models/admin/member/billing.php index 019fa7f..8bd7b1b 100644 --- a/models/admin/member/billing.php +++ b/models/admin/member/billing.php @@ -111,6 +111,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling $accountInsertError = false; $transactions = false; $account_data = false; + $balance_due = false; // For lockedToMember. $lockedToMember = false; @@ -226,7 +227,9 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling if ( $statements ) { $transactions = $statements['transactions']; $account_data = $statements['account_data']; + $balance_due = $statements['balance_due']; } + // echo '
$statements: ' . print_r( $statements, true ) . '
'; break; @@ -252,6 +255,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling 'accountInsertError' => $accountInsertError, 'transactions' => $transactions, 'account_data' => $account_data, + 'balance_due' => $balance_due, 'transaction_types' => $this->config['transaction_type'], ); diff --git a/setup/databaseScripts/update_database_V0.0.1.sql b/setup/databaseScripts/update_database_V0.0.2.sql similarity index 100% rename from setup/databaseScripts/update_database_V0.0.1.sql rename to setup/databaseScripts/update_database_V0.0.2.sql diff --git a/setup/validActions.php b/setup/validActions.php index 0198097..67cf729 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -68,6 +68,7 @@ $glmMembersBillingAddOnValidActions = array( 'index' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'list' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'invoices' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + 'payments' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, ), 'member' => array( 'billing' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, diff --git a/views/admin/billing/editPayment.html b/views/admin/billing/editPayment.html new file mode 100644 index 0000000..ecfdb85 --- /dev/null +++ b/views/admin/billing/editPayment.html @@ -0,0 +1,78 @@ +{include file='admin/billing/header.html'} + +{include file='admin/billing/paymentHeader.html'} + +{if $paymentUpdated}Notification Updated{/if} +{if $paymentUpdateError}Notification Update Error{/if} +{if $paymentInsertError}Notification Insert Error{/if} +{if $paymentAdded}Notification Added{/if} + +
+
+ {if $payment_id} + + + {else} + + {/if} +
+
+ +
+
+ Member Account +
+
+ +
+
+
+
+ Invoice Number +
+
+ +
+
+
+
+ Amount +
+
+ +
+
+
+
+ Notes +
+
+ +
+
+ +
+
+
+
+ +
+
+
+
+ + + +{include file='admin/footer.html'} diff --git a/views/admin/billing/header.html b/views/admin/billing/header.html index a2b5e4b..31289e7 100644 --- a/views/admin/billing/header.html +++ b/views/admin/billing/header.html @@ -3,6 +3,7 @@
diff --git a/views/admin/billing/paymentHeader.html b/views/admin/billing/paymentHeader.html new file mode 100644 index 0000000..8d6fe7b --- /dev/null +++ b/views/admin/billing/paymentHeader.html @@ -0,0 +1,6 @@ + diff --git a/views/admin/billing/payments.html b/views/admin/billing/payments.html new file mode 100644 index 0000000..1e5c82f --- /dev/null +++ b/views/admin/billing/payments.html @@ -0,0 +1,46 @@ +{include file='admin/billing/header.html'} + +{include file='admin/billing/paymentHeader.html'} + + +

Payments

+ + + + + + + + + + + + + {if $haveInvoices} + {assign var="i" value="0"} + {foreach $invoices as $t} + {if $i++ is odd by 1} + + {else} + + {/if} + + + + + + + {/foreach} + {else} + + {/if} + +
IDMember NameTimeDue DateBalance
{$t.id} {$t.member_name} {$t.transaction_time.datetime} {$t.due_date.date} {$t.balance}
(no Invoice Types listed)
+ + + +{include file='admin/footer.html'} diff --git a/views/admin/billing/statements.html b/views/admin/billing/statements.html index 218cef7..ec9e808 100644 --- a/views/admin/billing/statements.html +++ b/views/admin/billing/statements.html @@ -12,31 +12,31 @@ Due Date Type Amount - Balance {$alt = 0} - {$total_due = 0.00} - {$total_balance = 0.00} {foreach $transactions as $transaction} - {$transaction.transaction_data.transaction_time|date_format:"%D"} - {$transaction.transaction_data.due_date|date_format:"%D"} - {$transaction_types[$transaction.type]} - {$transaction.transaction_data.amount_total} - {$transaction.transaction_data.balance} + {if $transaction.type == '10'} + {$transaction.transaction_data.transaction_time|date_format:"%D"} + {$transaction.transaction_data.due_date|date_format:"%D"} + {$transaction_types[$transaction.type]} + ${$transaction.transaction_data.amount_total} + {elseif $transaction.type == '20'} + {$transaction.transaction_data.transaction_time|date_format:"%D"} + + {$transaction_types[$transaction.type]} + ${$transaction.transaction_data.amount} + {/if} {$alt = $alt + 1} - {$total_due = $total_due + $transaction.transaction_data.amount_total} - {$total_balance = $total_balance + $transaction.transaction_data.balance} {/foreach} - - + Balance Due - {$total_balance|string_format:"%.2f"} + ${$balance_due|string_format:"%.2f"} -- 2.17.1