Update for when employees have no account setup.
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 28 Mar 2018 20:50:40 +0000 (16:50 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 28 Mar 2018 20:50:40 +0000 (16:50 -0400)
Create an account for the employee member.

classes/billingSupport.php
models/admin/member/billing.php

index dcb9f18..2eec0c2 100644 (file)
@@ -785,21 +785,52 @@ class GlmBillingSupport
         $accounts = array();
         // This may need to have filters setup in the contact plugin as well.
         $employees = apply_filters( 'glm_contact_has_employees', '', $member_id );
+        // echo '<pre>$employees: ' . print_r( $employees, true ) . '</pre>';
         foreach ( $employees as $ref_dest ) {
             $invoice         = false;
             $billing_account = $this->getAccountByRefDest( $ref_dest );
-            // Need also to get the members type to look up which invoice_type to use
-            $invoice_type_id =  $this->getMembersInvoiceTypeByRefDest( $member_id );
-            if ( $invoice_type_id ) {
-                $invoice = $this->getInvoicTypeById( $invoice_type_id );
+            // If we don't have an account for this member then one
+            // Needs to be created.
+            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,
+                        ),
+                        array(
+                            '%d',
+                            '%s'
+                        )
+                    );
+                    $employee_account_id = $this->wpdb->insert_id;
+                    $billing_account     = $this->getAccountByRefDest( $ref_dest );
+                }
+            }
+            if ( $billing_account ) {
+                // Need also to get the members type to look up which invoice_type to use
+                $invoice_type_id =  $this->getMembersInvoiceTypeByRefDest( $member_id );
+                if ( $invoice_type_id ) {
+                    $invoice = $this->getInvoicTypeById( $invoice_type_id );
+                }
+                $accounts[$billing_account['id']] = array(
+                    'id'       => $billing_account['id'],
+                    'ref_dest' => $billing_account['ref_dest'],
+                    'ref_name' => $billing_account['ref_name'],
+                    'invoice'  => $invoice,
+                    'email'    => $billing_account['email']
+                );
             }
-            $accounts[$billing_account['id']] = array(
-                'id'       => $billing_account['id'],
-                'ref_dest' => $billing_account['ref_dest'],
-                'ref_name' => $billing_account['ref_name'],
-                'invoice'  => $invoice,
-                'email'    => $billing_account['email']
-            );
         }
         return $accounts;
     }
@@ -1077,7 +1108,7 @@ class GlmBillingSupport
                     array( '%s' ),
                     array( '%d' )
                 );
-                // TODO: Need to update any employees renewal dates
+                // Need to update any employees renewal dates
                 if ( isset( $employees ) && is_array( $employees ) && !empty( $employees ) ) {
                     foreach ( $employees as $employee ) {
                         $this->wpdb->update(
index 020880d..f29e71d 100644 (file)
@@ -206,6 +206,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
 
             // Get a list of this accounts employees. If they have any.
             $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+            // echo '<pre>$employees: ' . print_r( $employees, true ) . '</pre>';
 
             // Load DataClass for Management.
             require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataManagement.php';