From 102043bb58f1a4625a840c6d17181d4f2a762554 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 7 Dec 2017 16:50:21 -0500 Subject: [PATCH] WIP adding account form for member billing tab Working on getting the member billing tab with management of the account. If the member doesn't have an account it can add one. If there's one already there it updates it. --- classes/data/dataAccounts.php | 31 +++-- classes/data/dataInvoices.php | 10 +- index.php | 2 +- models/admin/billing/index.php | 129 ++++++++++++++++-- models/admin/billing/invoices.php | 2 +- models/admin/member/billing.php | 108 +++++++++++++++ setup/adminTabs.php | 29 ++-- ..._V0.0.1.sql => create_database_V0.0.2.sql} | 3 +- setup/databaseScripts/dbVersions.php | 1 + .../update_database_V0.0.1.sql | 9 ++ setup/validActions.php | 3 + views/admin/billing/editAccount.html | 69 ++++++++++ views/admin/billing/index.html | 7 +- .../admin/billing/memberBillingSubHeader.html | 6 + 14 files changed, 364 insertions(+), 45 deletions(-) create mode 100644 models/admin/member/billing.php rename setup/databaseScripts/{create_database_V0.0.1.sql => create_database_V0.0.2.sql} (98%) create mode 100644 setup/databaseScripts/update_database_V0.0.1.sql create mode 100644 views/admin/billing/editAccount.html create mode 100644 views/admin/billing/memberBillingSubHeader.html diff --git a/classes/data/dataAccounts.php b/classes/data/dataAccounts.php index 41181cd..4ce220a 100644 --- a/classes/data/dataAccounts.php +++ b/classes/data/dataAccounts.php @@ -124,36 +124,41 @@ class GlmDataAccounts extends GlmDataAbstract 'p_orderby' => 'name', 'p_blank' => true, 'force_list' => true, - 'required' => false, + 'required' => true, 'use' => 'a' ), // Name of red_dest 'ref_name' => array( - 'field' => 'ref_name', - 'type' => 'text', - 'use' => 'a', + 'field' => 'ref_name', + 'type' => 'text', + 'use' => 'a', + 'required' => true, ), // Anniversary Date 'anniversary_date' => array( - 'field' => 'anniversary_date', - 'type' => 'date', - 'use' => 'a', + 'field' => 'anniversary_date', + 'type' => 'date', + 'use' => 'a', + 'default' => '', + 'required' => true, ), // Payment Data 'payment_data' => array( - 'field' => 'payment_data', - 'type' => 'text', - 'use' => 'a', + 'field' => 'payment_data', + 'type' => 'text', + 'use' => 'a', + 'required' => false, ), // Email 'email' => array( - 'field' => 'email', - 'type' => 'text', - 'use' => 'a', + 'field' => 'email', + 'type' => 'text', + 'use' => 'a', + 'required' => true, ), ); diff --git a/classes/data/dataInvoices.php b/classes/data/dataInvoices.php index a9a295f..e1ede13 100644 --- a/classes/data/dataInvoices.php +++ b/classes/data/dataInvoices.php @@ -208,12 +208,10 @@ 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"; + SELECT A.ref_name + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices I + LEFT OUTER JOIN " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts A + ON ( I.account = A.id )"; $r['member_name'] = $this->wpdb->get_var( $sql ); return $r; } diff --git a/index.php b/index.php index f6d22f2..7ac5744 100644 --- a/index.php +++ b/index.php @@ -38,7 +38,7 @@ * version from this plugin. */ define('GLM_MEMBERS_BILLING_PLUGIN_VERSION', '0.0.1'); -define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.1'); +define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.2'); // This is the minimum version of the GLM Members DB plugin require for this plugin. define('GLM_MEMBERS_BILLING_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '2.8.0'); diff --git a/models/admin/billing/index.php b/models/admin/billing/index.php index 7ffd2ba..047721b 100644 --- a/models/admin/billing/index.php +++ b/models/admin/billing/index.php @@ -36,10 +36,10 @@ class GlmMembersAdmin_billing_index // extends GlmDataBilling /** * Billing ID * - * @var $billingID + * @var $memberID * @access public */ - public $billingID = false; + public $memberID = false; /** * Constructor @@ -88,10 +88,24 @@ class GlmMembersAdmin_billing_index // extends GlmDataBilling public function modelAction($actionData = false) { // Initialize Variables Here - $numbPending = 0; - $namesList = false; - $haveCategories = false; - $numberPending = 0; + $numbPending = 0; + $namesList = false; + $haveCategories = false; + $numberPending = 0; + $fromMemberMenu = false; + $this->memberID = false; + $memberData = false; + $memberName = false; + $haveMember = false; + $haveAccount = false; + $option = 'list'; + $view = 'index'; + $account = false; + $accountID = 0; + $accountUpdated = false; + $accountUpdateError = false; + $accountAdded = false; + $accountInsertError = false; // For lockedToMember $lockedToMember = false; @@ -118,17 +132,114 @@ class GlmMembersAdmin_billing_index // extends GlmDataBilling $lockedWhere .= ' AND '; } + if ( defined( 'GLM_MEMBERS_BILLING_MEMBER_MENU' ) && GLM_MEMBERS_BILLING_MEMBER_MENU ) { + $fromMemberMenu = true; + + if ( isset( $_REQUEST['member'] ) ) { + $this->memberID = $_REQUEST['member']; + } + } + + require_once GLM_MEMBERS_PLUGIN_CLASS_PATH . '/data/dataMembers.php'; + $this->Members = new GlmDataMembers( $this->wpdb, $this->config ); + + if ( $this->memberID ) { + $memberData = $this->Members->getEntry( $this->memberID ); + } + + if ( isset( $memberData ) && is_array( $memberData ) && $memberData['id'] > 0 ) { + $haveMember = true; + $memberName = $memberData['name']; + } + + if ( isset( $_REQUEST['option'] ) ) { + $option = $_REQUEST['option']; + } + + switch ( $option ) { + case 'account': + require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataAccounts.php'; + $Accounts = new GlmDataAccounts( $this->wpdb, $this->config ); + + // Check to see if we're adding an account or editing one. + 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 ( !isset( $_REQUEST['id'] ) ) { + $account = $Accounts->insertEntry(); + if ( !$account['status'] ) { + $accountInsertError = true; + } else { + $accountAdded = true; + } + } else { + $account = $Accounts->updateEntry( $_REQUEST['id'] ); + if ( !$account['status'] ) { + $accountUpdateError = true; + } else { + $accountUpdated = true; + } + } + } + + // Need to see if there's an account for this member. + $accountID = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts + WHERE ref_dest = %d", + $this->memberID + ) + ); + if ( !$account ) { + if ( $accountID ) { + $account = $Accounts->editEntry( $accountID ); + $haveAccount = true; + } else { + $accountID = 0; + $account = $Accounts->newEntry(); + // Set the ref_name from memberData + $account['fieldData']['ref_name'] = $memberData['name']; + $haveAccount = false; + } + } else { + $haveAccount = true; + // echo '
$account: ' . print_r( $account, true ) . '
'; + } + + // echo '
$account: ' . print_r( $account, true ) . '
'; + $view = 'editAccount'; + break; + case 'list': + break; + } + // Compile template data $templateData = array( - 'lockedToMember' => $lockedToMember, - 'numberPending' => $numberPending, + 'accountID' => $accountID, + 'option' => $option, + 'fromMemberMenu' => ( defined('GLM_MEMBERS_BILLING_MEMBER_MENU' ) ? true: false ), + 'lockedToMember' => $lockedToMember, + 'numberPending' => $numberPending, + 'memberID' => $this->memberID, + 'haveMember' => $haveMember, + 'haveAccount' => $haveAccount, + 'memberData' => $memberData, + 'memberName' => $memberName, + 'account' => $account, + 'accountUpdated' => $accountUpdated, + 'accountUpdateError' => $accountUpdateError, + 'accountAdded' => $accountAdded, + 'accountInsertError' => $accountInsertError, ); + // echo '
$templateData: ' . print_r( $templateData, true ) . '
'; // Return status, any suggested view, and any data to controller return array( 'status' => true, 'modelRedirect' => false, - 'view' => 'admin/billing/index.html', + 'view' => 'admin/billing/'.$view.'.html', 'data' => $templateData ); diff --git a/models/admin/billing/invoices.php b/models/admin/billing/invoices.php index dbe8b15..df78b8b 100644 --- a/models/admin/billing/invoices.php +++ b/models/admin/billing/invoices.php @@ -322,7 +322,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices } unset($invoicesResult); - // echo '
$invoices: ' . print_r( $invoices, true ) . '
'; + echo '
$invoices: ' . print_r( $invoices, true ) . '
'; foreach ( $invoices as $invoice ) { // $this->generateInvoiceTotal( $invoice['id'] ); diff --git a/models/admin/member/billing.php b/models/admin/member/billing.php new file mode 100644 index 0000000..35752dc --- /dev/null +++ b/models/admin/member/billing.php @@ -0,0 +1,108 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release admin.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +// Inform the billing code that we're working from the member area +define('GLM_MEMBERS_BILLING_MEMBER_MENU', true); + +// Load the events index +require GLM_MEMBERS_BILLING_PLUGIN_PATH."/models/admin/billing/index.php"; + +class GlmMembersAdmin_member_billing extends GlmMembersAdmin_billing_index +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + /** + * Billing List + * + * @var $billing + * @access public + */ + public $billing = false; + /** + * Contact Info + * + * @var $contactInfo + * @access public + */ + public $contactInfo = false; + /** + * Member ID + * + * @var $memberID + * @access public + */ + public $memberID = false; + /** + * Contact ID + * + * @var $contactID + * @access public + */ + public $contactID = false; + + + /* + * Constructor + * + * This contructor performs the work for this model. This model returns + * an array containing the following. + * + * 'status' + * + * True if successfull and false if there was a fatal failure. + * + * 'view' + * + * A suggested view name that the contoller 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 members data class + parent::__construct($wpdb, $config); + + } + +} diff --git a/setup/adminTabs.php b/setup/adminTabs.php index 99827b5..261a5cc 100644 --- a/setup/adminTabs.php +++ b/setup/adminTabs.php @@ -46,19 +46,7 @@ if ( current_user_can( 'glm_members_members' ) ) { return $addOnTabs; } ); - // add_filter( 'glm-member-db-add-tab-for-settings', - // function( $addOnTabs ) { - // $newTabs = array( - // array( - // 'text' => 'Payment Types', - // 'menu' => 'settings', - // 'action' => 'paymentTypes', - // ) - // ); - // $addOnTabs = array_merge( $addOnTabs, $newTabs ); - // return $addOnTabs; - // } - // ); + add_filter( 'glm-member-db-add-tab-for-billing', function( $addOnTabs ){ $newTabs = array( @@ -71,4 +59,19 @@ if ( current_user_can( 'glm_members_members' ) ) { ); } ); + + add_filter( + 'glm-member-db-add-tab-for-member', + function( $addOnTabs ){ + $newTabs = array( + array( + 'text' => 'Billing', + 'menu' => 'billing', + 'action' => 'billing' + ) + ); + $addOnTabs = array_merge( $addOnTabs, $newTabs ); + return $addOnTabs; + } + ); } diff --git a/setup/databaseScripts/create_database_V0.0.1.sql b/setup/databaseScripts/create_database_V0.0.2.sql similarity index 98% rename from setup/databaseScripts/create_database_V0.0.1.sql rename to setup/databaseScripts/create_database_V0.0.2.sql index 82a1761..06d4a23 100644 --- a/setup/databaseScripts/create_database_V0.0.1.sql +++ b/setup/databaseScripts/create_database_V0.0.2.sql @@ -84,7 +84,8 @@ CREATE TABLE {prefix}line_items ( CREATE TABLE {prefix}payments ( id INT NOT NULL AUTO_INCREMENT, transaction_time DATETIME NOT NULL, -- datetime of payment - account INT NOT NULL, -- ref to account + account INT NOT NULL, -- ref to account table + invoice INT NOT NULL, -- ref to an invoice table amount DECIMAL(8, 2) NOT NULL, -- payment amount payment_method TINYTEXT NOT NULL, -- payment method payment_data TINYTEXT NULL, -- additional payment info diff --git a/setup/databaseScripts/dbVersions.php b/setup/databaseScripts/dbVersions.php index 1ab11c9..40efb5c 100644 --- a/setup/databaseScripts/dbVersions.php +++ b/setup/databaseScripts/dbVersions.php @@ -15,5 +15,6 @@ $glmMembersBillingDbVersions = array( '0.0.1' => array('version' => '0.0.1', 'tables' => 10), + '0.0.2' => array('version' => '0.0.2', 'tables' => 10), ); diff --git a/setup/databaseScripts/update_database_V0.0.1.sql b/setup/databaseScripts/update_database_V0.0.1.sql new file mode 100644 index 0000000..ed54969 --- /dev/null +++ b/setup/databaseScripts/update_database_V0.0.1.sql @@ -0,0 +1,9 @@ +-- Gaslight Media Billing Database +-- File Created: 12/07/2017 +-- Database Version: 0.0.2 +-- +-- To permit each query below to be executed separately, +-- all queries must be separeted by a line with four dashes + +-- Add new field for payments table +ALTER TABLE {prefix}payments ADD invoice INT NOT NULL; -- ref to invoice table diff --git a/setup/validActions.php b/setup/validActions.php index 378389d..926a4ad 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -69,6 +69,9 @@ $glmMembersBillingAddOnValidActions = array( 'list' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'invoices' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, ), + 'member' => array( + 'billing' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + ), ), 'frontActions' => array( ) diff --git a/views/admin/billing/editAccount.html b/views/admin/billing/editAccount.html new file mode 100644 index 0000000..770596f --- /dev/null +++ b/views/admin/billing/editAccount.html @@ -0,0 +1,69 @@ +{if $fromMemberMenu} + {include file='admin/member/header.html'} + {include file='admin/billing/memberBillingSubHeader.html'} +{else} + {include file='admin/billing/header.html'} +{/if} + +{if $accountUpdated}Account Updated{/if} +{if $accountUpdateError}Account Update Error{/if} +{if $accountInsertError}Account Insert Error{/if} +{if $accountAdded}Account Added{/if} + +
+ + + + + + + {if $accountID} + + {/if} + + + + + + + + + + + + + + + + + + + + + + + +
Name + + {if $account.fieldFail.ref_name}

{$account.fieldFail.ref_name}

{/if}
+
Anniversary Date + + {if $account.fieldFail.anniversary_date}

{$account.fieldFail.anniversary_date}

{/if}
+
Billing Email + + {if $account.fieldFail.email}

{$account.fieldFail.email}

{/if}
+
+ +
+
+ + + +{include file='admin/footer.html'} diff --git a/views/admin/billing/index.html b/views/admin/billing/index.html index af2773d..0f74edc 100644 --- a/views/admin/billing/index.html +++ b/views/admin/billing/index.html @@ -1,4 +1,9 @@ -{include file='admin/billing/header.html'} +{if $fromMemberMenu} + {include file='admin/member/header.html'} + {include file='admin/billing/memberBillingSubHeader.html'} +{else} + {include file='admin/billing/header.html'} +{/if} diff --git a/views/admin/billing/memberBillingSubHeader.html b/views/admin/billing/memberBillingSubHeader.html new file mode 100644 index 0000000..851366b --- /dev/null +++ b/views/admin/billing/memberBillingSubHeader.html @@ -0,0 +1,6 @@ + -- 2.17.1