From b65cbcd168c1133c29b7347d72b768a2eb7e7df0 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Mon, 11 Dec 2017 13:26:56 -0500 Subject: [PATCH] Creating methods in support class for member statements. Creating the member statements and outputting the member statements in their member billing tab. --- classes/billingSupport.php | 182 +++++++++++++++++- models/admin/billing/invoices.php | 2 +- models/admin/member/billing.php | 31 ++- .../create_database_V0.0.2.sql | 1 + views/admin/billing/editAccount.html | 3 + views/admin/billing/editInvoice.html | 1 + views/admin/billing/statements.html | 44 +++++ 7 files changed, 247 insertions(+), 17 deletions(-) create mode 100644 views/admin/billing/statements.html diff --git a/classes/billingSupport.php b/classes/billingSupport.php index cf76888..18abc6d 100644 --- a/classes/billingSupport.php +++ b/classes/billingSupport.php @@ -71,25 +71,61 @@ class GlmBillingSupport } - public function recordInvoice( $invoice_id, $total ) + /** + * recordInvoice + * + * Record the Invoice in the transaction table. + * + * @param mixed $invoice_id Id of the Invoice record + * @param mixed $total Total amount from Invoice + + * @access public + * @return void + */ + public function recordInvoice( $invoice_id, $account, $total ) { - $this->recordTransaction( $this->config['transaction_numb']['Invoice'], $invoice_id, $total, null ); + $this->recordTransaction( $this->config['transaction_numb']['Invoice'], $invoice_id, $account, $total, null ); } + /** + * recordPayment + * + * Record the Payment in the transaction table. + * + * @param mixed $invoice_id Id of the Invoice record + * @param mixed $payment Total amount of the Payment + + * @access public + * @return void + */ public function recordPayment( $invoice_id, $payment ) { $this->recordTransaction( $this->config['transaction_numb']['Payment'], $invoice_id, null, $payment ); } - public function recordTransaction( $type, $type_id, $current_invoice_total = null, $current_payment_total = null ) + /** + * recordTransaction + * + * Record the transaction by type. + * + * @param mixed $type Type of transaction from $this->config['transaction_type'] + * @param mixed $type_id Id for invoice + * @param bool $current_invoice_total Invoice Total if Invoice + * @param bool $current_payment_total Payment Total if Payment type + + * @access public + * @return void + */ + public function recordTransaction( $type, $type_id, $account, $current_invoice_total = null, $current_payment_total = null ) { $current_invoice_total = ( $current_invoice_total == null ) ? '0.00': $current_invoice_total; $current_payment_total = ( $current_payment_total == null ) ? '0.00': $current_payment_total; $this->wpdb->insert( GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'transactions', array( - 'type' => $type, - 'type_id' => $type_id, - 'transaction_time' => date('Y-m-d H:i;s'), + 'type' => $type, + 'type_id' => $type_id, + 'account' => $account, + 'transaction_time' => date('Y-m-d H:i;s'), 'current_invoice_total' => $current_invoice_total, 'current_payment_total' => $current_payment_total, ), @@ -97,4 +133,138 @@ class GlmBillingSupport ); } + /** + * getStatements + * + * Create array that has the set of invoices and payments for an account. + * Returns false if nothing is found or array if it finds records. + * $results = array( + * 'account_data' = array(acountdate...), + * 'transactions' => array( + * 0 => array( + * 'id' => '', + * 'type' => '', + * 'type_id' => '', + * 'account' => '', + * 'transaction_time' => '', + * 'current_invoice_total' => '', + * 'current_payment_total' => '', + * 'transaction_data' => array( + * (payment or invoice data)... + * ) + * ) + * ), + * + * ) + * + * @param mixed $ref_dest Id for the associated entity (member, ?) + * + * @access public + * @return void + */ + public function getStatements( $ref_dest ) + { + // First, need to get the Account id for this ref_dest + $account_data = $this->getAccountByRefDest( $ref_dest ); + + // If there's no $account_id then return false. + if ( !$account_data ) { + return false; + } + + $account_id = $account_data['id']; + + // Get all transactions for this account. + $transactions = $this->getTransactionsByAccount( $account_id ); + + // For each transaction get the data (payment_data or invoice_data) + foreach ( $transactions as $key => &$transaction ) { + $type = $transaction['type']; + switch ( $type ){ + case $this->config['transaction_numb']['Invoice']: + $transaction['transaction_data'] = $this->getInvoiceById( $transaction['type_id'] ); + break; + case $this->config['transaction_numb']['Payment']: + break; + } + } + // echo '
$transactions: ' . print_r( $transactions, true ) . '
'; + + return array( + 'account_data' => $account_data, + 'transactions' => $transactions, + ); + } + + /** + * getAccountByRefDest + * + * Grab the account data for a refernce. + * + * @param mixed $ref_dest Id of the refered entity. + * + * @access public + * @return void + */ + public function getAccountByRefDest( $ref_dest ) + { + return $this->wpdb->get_row( + $this->wpdb->prepare( + "SELECT * + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts + WHERE ref_dest = %d", + $ref_dest + ), + ARRAY_A + ); + } + + /** + * getTransactionsByAccount + * + * Get all of the transaction records for a given account. + * + * @param mixed $account Id of the account record. + * + * @access public + * @return void + */ + public function getTransactionsByAccount( $account ) + { + return $this->wpdb->get_results( + $this->wpdb->prepare( + "SELECT * + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "transactions + WHERE account = %d + ORDER BY transaction_time DESC", + $account + ), + ARRAY_A + ); + } + + /** + * getInvoiceById + * + * Get the invoice by it's id. + * + * @param mixed $invoice_id Id field for the invoice record. + * + * @access public + * @return void + */ + public function getInvoiceById( $invoice_id ) + { + return $this->wpdb->get_row( + $this->wpdb->prepare( + "SELECT * + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices + WHERE id = %d", + $invoice_id + ), + ARRAY_A + ); + } + } + diff --git a/models/admin/billing/invoices.php b/models/admin/billing/invoices.php index 968c74e..9b8cc03 100644 --- a/models/admin/billing/invoices.php +++ b/models/admin/billing/invoices.php @@ -186,7 +186,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices $totals = $this->generateInvoiceTotal( $this->invoice_id ); // Now we have a total for the invoice we can record the transaction $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); - $BillingSupport->recordInvoice( $this->invoice_id, $totals['amount_total'] ); + $BillingSupport->recordInvoice( $this->invoice_id, $_REQUEST['account'], $totals['amount_total'] ); $view = 'editInvoice'; $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config ); diff --git a/models/admin/member/billing.php b/models/admin/member/billing.php index 7799289..019fa7f 100644 --- a/models/admin/member/billing.php +++ b/models/admin/member/billing.php @@ -14,6 +14,7 @@ */ define('GLM_MEMBERS_BILLING_MEMBER_MENU', true); require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/data/dataInvoices.php'; +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/billingSupport.php'; // Load Billing data abstract // require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/data/dataBilling.php'; @@ -108,13 +109,15 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling $accountUpdateError = false; $accountAdded = false; $accountInsertError = false; + $transactions = false; + $account_data = false; - // For lockedToMember + // For lockedToMember. $lockedToMember = false; $lockedWhereT = 'true'; $lockedWhere = 'true'; - // Check if there's a logged in user who is locked to their own entity + // Check if there's a logged in user who is locked to their own entity. $lockedToMember = apply_filters('glm_members_locked_to_member_id', false); if ($lockedToMember) { $memberID = $lockedToMember; @@ -167,7 +170,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling if ( isset( $_REQUEST['ref_name'] ) ) { $_REQUEST['anniversary_date'] = date('Y-m-d', strtotime($_REQUEST['anniversary_date'])); // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; - // if there's no id then add account + // if there's no id then add account. if ( !isset( $_REQUEST['id'] ) ) { $account = $Accounts->insertEntry(); if ( !$account['status'] ) { @@ -214,15 +217,21 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling $view = 'editAccount'; break; case 'list': - // Get the list of invoices for this member - echo '
$this->memberID: ' . print_r( $this->memberID, true) . '
'; + $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config); + $view = 'statements'; + // Get the list of invoices for this member. + // echo '
$this->memberID: ' . print_r( $this->memberID, true) . '
'; // echo '
$memberData: ' . print_r( $memberData, true) . '
'; - $Invoices = new GlmDataInvoices( $this->wpdb, $this->config ); - // $where = "T." - // $invoices = $Invoices->getList($where); + $statements = $BillingSupport->getStatements( $this->memberID ); + if ( $statements ) { + $transactions = $statements['transactions']; + $account_data = $statements['account_data']; + } + break; } + // echo '
$this->config: ' . print_r( $this->config, true ) . '
'; // Compile template data $templateData = array( @@ -241,10 +250,12 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling 'accountUpdateError' => $accountUpdateError, 'accountAdded' => $accountAdded, 'accountInsertError' => $accountInsertError, + 'transactions' => $transactions, + 'account_data' => $account_data, + 'transaction_types' => $this->config['transaction_type'], ); - // echo '
$templateData: ' . print_r( $templateData, true ) . '
'; - // Return status, any suggested view, and any data to controller + // Return status, any suggested view, and any data to controller. return array( 'status' => true, 'modelRedirect' => false, diff --git a/setup/databaseScripts/create_database_V0.0.2.sql b/setup/databaseScripts/create_database_V0.0.2.sql index 7d422a2..cae2779 100644 --- a/setup/databaseScripts/create_database_V0.0.2.sql +++ b/setup/databaseScripts/create_database_V0.0.2.sql @@ -27,6 +27,7 @@ CREATE TABLE {prefix}transactions ( id INT NOT NULL AUTO_INCREMENT, type INT NOT NULL, -- type of transaction (payment,invoice,etc) type_id INT NOT NULL, -- reference to type id + account INT NOT NULL, -- reference to account id transaction_time DATETIME NOT NULL, -- datetime for the transaction current_invoice_total DECIMAL(8, 2) NOT NULL, -- invoice total current_payment_total DECIMAL(8, 2) NOT NULL, -- payment total diff --git a/views/admin/billing/editAccount.html b/views/admin/billing/editAccount.html index 770596f..43cb672 100644 --- a/views/admin/billing/editAccount.html +++ b/views/admin/billing/editAccount.html @@ -60,6 +60,9 @@