--- /dev/null
+<?php
+/**
+ * Gaslight Media Members Database
+ * Admin Transactions List
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package glmMembersDatabase
+ * @author Chuck Scott <cscott@gaslightmedia.com>
+ * @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 '<pre>$accounts: ' . print_r( $accounts, true ) . '</pre>';
+
+ // 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 '<pre>$nonAccountMembers: ' . print_r( $nonAccountMembers, true ) . '</pre>';
+
+ 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 '<pre>' . print_r( $_REQUEST, true ) . '</pre>';
+ $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 '<pre>$accounts: ' . print_r( $accounts, true ) . '</pre>';
+
+ 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
+ );
+
+ }
+
+}
<select id="member-account" name="account" required>
<option value="">Select an Account</option>
{foreach $accounts as $account}
+ {if $account.id}
<option value="{$account.id}">{$account.ref_name}</option>
+ {/if}
{/foreach}
</select>
<a id="newAccountButton" href="#">New Account</a>
</form>
</div>
<div id="newAccountDialog" class="glm-dialog-box" title="New Account">
- <p class="validateAccountTips">* required.</p>
+ <p class="validateAccountTips" style="color:red">* required</p>
<form id="addAccountForm">
+ <input id="non-member-account" type="hidden" name="ref_dest" value="">
<table>
<tr>
- <th class="glm-required">Member</th>
- <td>
- <select id="non-member-account" name="ref_dest" style="width: 300px;">
- <option value="">Select Member</option>
- {foreach $nonAccountMembers as $m}
- <option value="{$m.id}">{$m.name}</option>
- {/foreach}
- </select>
- </td>
- </tr>
- <tr>
- <th class="glm-required">Name</th>
+ <th class="glm-required">Member Name</th>
<td>
<input id="glm_member_name" type="text" name="ref_name" value="">
</td>
<script>
jQuery(document).ready(function($){
+ var availableAccounts = [
+ {foreach $accounts as $m}
+ { label: "{$m.ref_name|unescape:'html'|replace:'"':''}", value: "{$m.ref_name|unescape:'html'|replace:'"':''}", id: '{$m.id}' },
+ {/foreach}
+ ];
+ var availableNonAccountMembers = [
+ {foreach $nonAccountMembers as $m}
+ { label: "{$m.name|unescape:'html'|replace:'"':''}", value: "{$m.name|unescape:'html'|replace:'"':''}", id: '{$m.id}' },
+ {/foreach}
+ ];
+ $('#member_account_name').autocomplete({
+ source: availableAccounts
+ });
+ $('#glm_member_name').autocomplete({
+ source: availableNonAccountMembers,
+ appendTo: '#newAccountDialog',
+ select: function( event, ui ){
+ $('#non-member-account').val( ui.item.id );
+ },
+ });
// Initialize variables for this page.
var dialog, // Dialog for the add line_item form
newAccountDialog, // Dialog for the add new account
line_item_type = $( 'select[name="line_item_type"]' ), // Line item type in the add line_item form
line_items = [], // Array holding line_items json objects
allFields = $( [] ).add( line_item_type ), // Array holding the form fields - add line_item form
- ref_dest = $( 'select[name="ref_dest"' ),
ref_name = $( 'input[name="ref_name"' ),
anniversary_date = $( 'input[name="anniversary_date"' ),
renewal_date = $( 'input[name="renewal_date"' ),
billing_phone = $( 'input[name="billing_phone"' ),
lineTips = $('.validateTips'),
accountTips = $('.validateAccountTips'),
- allAccountFields = $( [] ).add( ref_dest ) .add( ref_name ) .add( anniversary_date ) .add( renewal_date )
+ allAccountFields = $( [] ).add( ref_name ) .add( anniversary_date ) .add( renewal_date )
.add( email ) .add( billing_addr1 ) .add( billing_addr2 ) .add( billing_city )
.add( billing_state ) .add( billing_zip )
.add( billing_phone ) , // Array holding the form fields - add line_item form
var valid = true;
allAccountFields.removeClass( 'ui-state-error' );
- valid = valid && checkRequired( ref_dest, accountTips, 'Member is required!' );
valid = valid && checkRequired( ref_name, accountTips, 'Name is required!' );
valid = valid && checkRequired( anniversary_date, accountTips, 'Anniversary Date is required!' );
valid = valid && checkRequired( email, accountTips, 'Email is required!' );
}).done(function( msg ){
if ( msg.status === true ) {
// Reload the account select
- updateBillingAccountSelect();
- updateNonAccountMemberSelect();
+ updateBillingAccountSelect( msg.account.fieldData.id );
+ updateNonAccountMemberList();
// Close the dialog
newAccountDialog.dialog( 'close' );
} else {
- console.log( 'return', msg );
+ // console.log( 'return', msg );
}
});
}
* Ajax call to get the list of billing accounts and update the select
* for the Billing account drop down.
*/
- function updateBillingAccountSelect() {
+ function updateBillingAccountSelect(selected_id = null) {
$.ajax({
url: '{$ajaxUrl}',
cache: false,
var obj = msg[index];
memberAccount.options[memberAccount.options.length] = new Option( obj.ref_name, obj.id );
}
+ memberAccount.value = selected_id;
});
}
/**
* Ajax call to get the list of members without accounts.
*/
- function updateNonAccountMemberSelect() {
+ function updateNonAccountMemberList() {
$.ajax({
url: '{$ajaxUrl}',
cache: false,
encode: true,
dataType: 'json'
}).done(function(msg){
- // console.log( msg.length );
- var nonMemberAccount = document.getElementById('non-member-account');
- nonMemberAccount.options.length = 0;
- nonMemberAccount.options[nonMemberAccount.options.length] = new Option( 'Select Member', '' );
+ availableNonAccountMembers = [];
for ( index in msg ) {
var obj = msg[index];
- nonMemberAccount.options[nonMemberAccount.options.length] = new Option( obj.name, obj.id );
+ availableNonAccountMembers.push( { label: obj.name, value: obj.name, id: obj.id } );
}
+ // also need to update the source for the autocomplete
+ $('#glm_member_name').autocomplete(
+ 'option',
+ { source: availableNonAccountMembers }
+ );
});
}
*/
newAccountForm = newAccountDialog.find( '#addAccountForm' ).on( 'submit', function(){
event.preventDefault();
- console.log('Adding new Account');
+ // console.log('Adding new Account');
});
/**