From: Steve Sutton Date: Fri, 20 Apr 2018 20:58:35 +0000 (-0400) Subject: Updating the billing for the employees X-Git-Tag: v1.0.0^2~41 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=a61c8a810eae9a2034b7c62d52960b7ba2e9d990;p=WP-Plugins%2Fglm-member-db-billing.git Updating the billing for the employees After adding new table for the employees redid how the management of the employees are done. Now can edit these from the member billing info page. --- diff --git a/classes/billingSupport.php b/classes/billingSupport.php index ceb1c9f..5179120 100644 --- a/classes/billingSupport.php +++ b/classes/billingSupport.php @@ -888,30 +888,37 @@ class GlmBillingSupport return $accounts; } // This may need to have filters setup in the contact plugin as well. - $employees = apply_filters( 'glm_contact_has_employees', '', $member_id ); + // $employees = apply_filters( 'glm_contact_has_employees', '', $member_id ); + $main_billing_account = $this->getAccountByRefDest( $member_id ); + // echo '
$main_billing_account: ' . print_r( $main_billing_account, true ) . '
'; + require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataEmployees.php'; + $Employees = new GlmDataEmployees( $this->wpdb, $this->config ); + $employees = $Employees->getList( "T.account = " . $main_billing_account['id'] ); // echo '
$employees: ' . print_r( $employees, true ) . '
'; + if ( isset( $employees ) && is_array( $employees ) ) { - foreach ( $employees as $ref_dest ) { - $invoice = false; - $billing_account = $this->getAccountByRefDest( $ref_dest ); + foreach ( $employees as $emp ) { + $invoice = false; + $billing_account_id = $emp['employee']; + $billing_account = $this->getAccountById( $billing_account_id ); // If we don't have an account for this member then one // Needs to be created. + $ref_dest = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members + WHERE name = %s", + $emp['employee_name'] + ) + ); if ( !$billing_account ) { // Get the Member Name - $member_name = $this->wpdb->get_var( - $this->wpdb->prepare( - "SELECT name - FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members - WHERE id = %d", - $ref_dest - ) - ); if ( $member_name ) { $this->wpdb->insert( GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts', array( 'ref_dest' => $ref_dest, - 'ref_name' => $member_name, + 'ref_name' => $emp['employee_name'], 'anniversary_date' => date( 'Y-m-d' ) ), array( @@ -929,7 +936,7 @@ class GlmBillingSupport $invoice_type_id = $this->getMembersInvoiceTypeByRefDest( $ref_dest ); if ( $invoice_type_id ) { $invoice = $this->getInvoiceTypeById( $invoice_type_id ); - $accounts[$billing_account['id']] = array( + $accounts[$billing_account_id] = array( 'id' => $billing_account['id'], 'ref_dest' => $billing_account['ref_dest'], 'ref_name' => $billing_account['ref_name'], @@ -940,6 +947,7 @@ class GlmBillingSupport } } } + return $accounts; } diff --git a/classes/data/dataAccounts.php b/classes/data/dataAccounts.php index 7d86f73..100650f 100644 --- a/classes/data/dataAccounts.php +++ b/classes/data/dataAccounts.php @@ -120,6 +120,12 @@ class GlmDataAccounts extends GlmDataAbstract 'use' => 'a', ), + 'boss' => array( + 'field' => 'boss', + 'type' => 'checkbox', + 'use' => 'a', + ), + // Account ref to accounts table 'ref_dest' => array( 'field' => 'ref_dest', diff --git a/classes/data/dataEmployees.php b/classes/data/dataEmployees.php index bb3aaac..e16375c 100644 --- a/classes/data/dataEmployees.php +++ b/classes/data/dataEmployees.php @@ -124,8 +124,21 @@ class GlmDataEmployees extends GlmDataAbstract 'account' => array( 'field' => 'account', 'type' => 'pointer', - 'p_table' => GLM_MEMBERS_PLUGIN_DB_PREFIX . 'accounts', - 'p_field' => 'ref_dest', + 'p_table' => GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts', + 'p_field' => 'id', + 'p_orderby' => 'ref_name', + 'p_blank' => true, + 'required' => true, + 'use' => 'a' + ), + + // Account ref to accounts table + 'account_name' => array( + 'field' => 'account', + 'type' => 'pointer', + 'as' => 'account_name', + 'p_table' => GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts', + 'p_field' => 'ref_name', 'p_orderby' => 'ref_name', 'p_blank' => true, 'required' => true, @@ -136,14 +149,28 @@ class GlmDataEmployees extends GlmDataAbstract 'employee' => array( 'field' => 'employee', 'type' => 'pointer', - 'p_table' => GLM_MEMBERS_PLUGIN_DB_PREFIX . 'accounts', - 'p_field' => 'ref_dest', + 'p_table' => GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts', + 'p_field' => 'id', 'p_orderby' => 'ref_name', 'p_blank' => true, 'required' => true, 'use' => 'a' ), + // Account ref to accounts table + 'employee_name' => array( + 'field' => 'employee', + 'type' => 'pointer', + 'as' => 'employee_name', + 'p_table' => GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts', + 'p_field' => 'ref_name', + 'p_orderby' => 'ref_name', + 'p_blank' => true, + 'required' => true, + 'use' => 'a' + ), + + ); diff --git a/models/admin/member/billing.php b/models/admin/member/billing.php index 485e375..73b1993 100644 --- a/models/admin/member/billing.php +++ b/models/admin/member/billing.php @@ -371,9 +371,28 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling break; case 'account': + // 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 + ) + ); + // echo '
$accountID: ' . print_r( $accountID, true ) . '
'; + // Grab the employee data + if ( $accountID ) { + // Get a list of this accounts employees. If they have any. + require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataEmployees.php'; + $Employees = new GlmDataEmployees( $this->wpdb, $this->config ); + $employees = $Employees->getList( "T.account = $accountID" ); + } + // 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(); @@ -390,23 +409,67 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling $accountUpdated = true; } } + + if ( isset( $employees ) && !empty( $employees ) ) { + // Check if an employee was removed (deleted) + foreach ( $employees as $employee_id => $employee ) { + if ( !isset( $_REQUEST['employees'][$employee['employee']] ) ) { + $this->wpdb->delete( + GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'employees', + array( 'id' => $employee_id ), + array( '%d' ) + ); + } + // Remove the employee from the $employees array + unset( $employees[$employee_id] ); + } + } + // echo '
$employees: ' . print_r( $employees, true ) . '
'; + // If there's employees data then add them to this account + if ( isset($accountID) && $accountID && isset( $_REQUEST['employees'] ) && is_array( $_REQUEST['employees'] ) && !empty( $_REQUEST['employees'] ) ) { + foreach ( $_REQUEST['employees'] as $employee_id => $hasKey ) { + // Should check to see if there's one added with account and employee first + $emp_id = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "employees + WHERE account = %d + AND employee = %d", + $accountID, + $employee_id + ) + ); + // echo '
$emp_id: ' . print_r( $emp_id, true ) . '
'; + if ( !$emp_id ) { + $this->wpdb->insert( + GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'employees', + array( + 'account' => $accountID, + 'employee' => $employee_id, + ), + array( + '%d', + '%d' + ) + ); + } + } + } } - // Get a list of this accounts employees. If they have any. - $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID ); + + // Grab the employee data + if ( $accountID ) { + // Get a list of this accounts employees. If they have any. + require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataEmployees.php'; + $Employees = new GlmDataEmployees( $this->wpdb, $this->config ); + $employees = $Employees->getList( "T.account = $accountID" ); + } + // $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID ); // Need to get the accounts $Accounts = new GlmDataAccounts( $this->wpdb, $this->config ); - $accounts = $Accounts->getSimpleAccountList( '', 'ref_name' ); + $accounts = $Accounts->getSimpleAccountList( "T.boss <> true AND T.boss IS NOT NULL", 'ref_name' ); - // 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 ); diff --git a/setup/commonHooks.php b/setup/commonHooks.php index f76d60d..a9b4363 100644 --- a/setup/commonHooks.php +++ b/setup/commonHooks.php @@ -46,6 +46,13 @@ add_filter( 'glm-billing-get-account-status', function( $content, $account_id ){ if ( !$account ) { return $content; } + // See if this member account has a paid member type + $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); + $paid_mem_type = $BillingSupport->getMembersInvoiceTypeByRefDest( $account['ref_dest'] ); + if ( !$paid_mem_type ) { + return 'Free'; + } + $archived = $account['archived']; $renewal_date = $account['renewal_date']; $renewal_time = strtotime( $account['renewal_date'] ); @@ -59,22 +66,18 @@ add_filter( 'glm-billing-get-account-status', function( $content, $account_id ){ return 'Archived'; } $days_before_renewal = $config['settings']['days_before_renewal']; - // echo '
$days_before_renewal: ' . print_r( $days_before_renewal, true ) . '
'; $days_before_time = mktime( 0, 0, 0, date( 'n', $renewal_time ), date( 'j', $renewal_time ) - $days_before_renewal, date( 'Y', $renewal_time ) + 1 ); - // echo '
$days_before_time: ' . print_r( date( 'm/d/Y', $days_before_time), true ) . '
'; $days_after_expired = $config['settings']['days_after_expired']; - // echo '
$days_after_expired: ' . print_r( $days_after_expired, true ) . '
'; $days_after_time = mktime( 0, 0, 0, date( 'n', $renewal_time ), date( 'j', $renewal_time ) + $days_after_expired, date( 'Y', $renewal_time ) + 1 ); - // echo '
$days_after_time: ' . print_r( date( 'm/d/Y', $days_after_time), true ) . '
'; if ( $current_time > $days_after_time ) { return 'Expired'; diff --git a/views/admin/billing/editAccount.html b/views/admin/billing/editAccount.html index 6088005..03af72e 100644 --- a/views/admin/billing/editAccount.html +++ b/views/admin/billing/editAccount.html @@ -58,7 +58,16 @@ {/if} {if $settings.allow_employees} +
+
Boss
+
+ + {if $account.fieldFail.boss}

{$account.fieldFail.boss}

{/if}
+
+
+ +
Associated Members/Employees
@@ -68,14 +77,16 @@ {if $employees} {foreach $employees as $employee}
- {$employee.ref_name} - Remove + + {$employee.employee_name} +
{/foreach} {/if}
+ {/if} @@ -176,12 +187,12 @@ jQuery(document).ready(function($){ var accounts = [ {foreach $accounts as $m} { label: "{$m.ref_name|unescape:'html'|replace:'"':''}", value: "{$m.ref_name|unescape:'html'|replace:'"':''}", id: '{$m.id}' }, {/foreach} ]; + var isBoss = {if $account.fieldData.boss.value}true{else}false{/if}; // Setup autocomplete for both inputs $('#glm-billing-member-list').autocomplete({ source: accounts, select: function( event, ui ){ - console.log('selected: ', ui); addEmployee( ui.item ); $('#glm-billing-member-list').val( '' ); return false; @@ -195,14 +206,45 @@ jQuery(document).ready(function($){ }); function addEmployee( employee ) { - var html = '
'; - html += ''; + var html = '
'; + html += ''; html += employee.label; - html += 'Remove'; + html += ''; html += '
'; $('#glm-billing-employees').append(html); } + // If the boss flag in unchecked then hide the employees box and + // disable the employees inputs. + $('#billing-boss').change(function(){ + if ( $(this).is(':checked') ) { + isBoss = true; + } else { + isBoss = false; + } + setupEmployeesPart(); + }); + function setupEmployeesPart() { + if ( isBoss ) { + $('#glm-emp-list').show(); + $('input[name^="employees"]').each(function(){ + $(this).removeAttr('disabled'); + }); + } else { + $('#glm-emp-list').hide(); + $('input[name^="employees"]').each(function(){ + $(this).prop('disabled', true); + }); + } + } + setupEmployeesPart(); + + + $('#glm-billing-employees').on('click', '.glm-billing-employee-del', function(){ + //console.log( 'clicked glm-billing-employee-del on ' ); + $(this).parent('.glm-billing-employee').remove(); + }); + // Setup the date picker for the input field with the name anniversary_date $('input[name="anniversary_date"]').datepicker({ dateFormat: 'mm/dd/yy'