Updating the billing for the employees
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 20 Apr 2018 20:58:35 +0000 (16:58 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 20 Apr 2018 20:58:35 +0000 (16:58 -0400)
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.

classes/billingSupport.php
classes/data/dataAccounts.php
classes/data/dataEmployees.php
models/admin/member/billing.php
setup/commonHooks.php
views/admin/billing/editAccount.html

index ceb1c9f..5179120 100644 (file)
@@ -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 '<pre>$main_billing_account: ' . print_r( $main_billing_account, true ) . '</pre>';
+        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 '<pre>$employees: ' . print_r( $employees, true ) . '</pre>';
+
         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;
     }
 
index 7d86f73..100650f 100644 (file)
@@ -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',
index bb3aaac..e16375c 100644 (file)
@@ -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'
+            ),
+
+
          );
 
 
index 485e375..73b1993 100644 (file)
@@ -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 '<pre>$accountID: ' . print_r( $accountID, true ) . '</pre>';
+            // 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 '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
                 // 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 '<pre>$employees: ' . print_r( $employees, true ) . '</pre>';
+                // 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 '<pre>$emp_id: ' . print_r( $emp_id, true ) . '</pre>';
+                        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 );
index f76d60d..a9b4363 100644 (file)
@@ -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 '<pre>$days_before_renewal: ' . print_r( $days_before_renewal, true ) . '</pre>';
         $days_before_time = mktime( 0, 0, 0,
             date( 'n', $renewal_time ),
             date( 'j', $renewal_time ) - $days_before_renewal,
             date( 'Y', $renewal_time ) + 1
         );
-        // echo '<pre>$days_before_time: ' . print_r( date( 'm/d/Y', $days_before_time), true ) . '</pre>';
 
         $days_after_expired  = $config['settings']['days_after_expired'];
-        // echo '<pre>$days_after_expired: ' . print_r( $days_after_expired, true ) . '</pre>';
         $days_after_time = mktime( 0, 0, 0,
             date( 'n', $renewal_time ),
             date( 'j', $renewal_time ) + $days_after_expired,
             date( 'Y', $renewal_time ) + 1
         );
-        // echo '<pre>$days_after_time: ' . print_r( date( 'm/d/Y', $days_after_time), true ) . '</pre>';
 
         if ( $current_time > $days_after_time ) {
             return 'Expired';
index 6088005..03af72e 100644 (file)
     {/if}
 
     {if $settings.allow_employees}
+
         <div class="glm-billing-field">
+            <div class="glm-billing-label{if $account.fieldRequired.boss} glm-required{/if}">Boss</div>
+            <div class="glm-billing-input{if $account.fieldFail.boss} glm-form-bad-input{/if}" data-tabid="glm-name">
+                <input id="billing-boss" type="checkbox" name="boss"{if $account.fieldData.boss.value} checked{/if}>
+            {if $account.fieldFail.boss}<p>{$account.fieldFail.boss}</p>{/if}<br>
+            </div>
+        </div>
+
+        <div id="glm-emp-list" class="glm-billing-field">
             <div class="glm-billing-label">
                 <strong>Associated Members/Employees</strong>
             </div>
                     {if $employees}
                         {foreach $employees as $employee}
                             <div class="glm-billing-employee">
-                                {$employee.ref_name}
-                                <a> Remove </a>
+                                <input type="hidden" name="employees[{$employee.employee}]" value="1">
+                                {$employee.employee_name}
+                                <span class="glm-billing-employee-del dashicons dashicons-trash glm-right"></span>
                             </div>
                         {/foreach}
                     {/if}
                 </div>
             </div>
         </div>
+
     {/if}
 
     </fieldset>
 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 = '<div class="glm-billign-employee" data-id="' + employee.id + '">';
-        html += '<input type="hidden" name="employee[' + employee.id + ']" value="1">';
+        var html = '<div class="glm-billing-employee">';
+        html += '<input type="hidden" name="employees[' + employee.id + ']" value="1">';
         html += employee.label;
-        html += '<a class="glm-billing-employee-del">Remove</a>';
+        html += '<span class="glm-billing-employee-del dashicons dashicons-trash glm-right"></span>';
         html += '</div>';
         $('#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'