Updates for import member data
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 30 Apr 2018 17:48:29 +0000 (13:48 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 30 Apr 2018 17:48:29 +0000 (13:48 -0400)
new members now being imported
includes custom field data.

models/admin/migcsa/index.php

index 312a927..39faadb 100644 (file)
@@ -36,8 +36,10 @@ class GlmMembersAdmin_migcsa_index
      * @var $migcsaID
      * @access public
      */
-    public $migcsaID = false;
-    public $dbh = false;
+    public $migcsaID     = false;
+    public $dbh          = false;
+    public $member_types = array();
+    public $custom_fields = array();
 
     /**
      * Constructor
@@ -83,7 +85,7 @@ class GlmMembersAdmin_migcsa_index
         $response       = false;
 
         // Member classes to member_types
-        $member_types = array(
+        $this->member_types = array(
             'AA'  => 13,
             'A'   => 1,
             'SM'  => 3,
@@ -97,6 +99,25 @@ class GlmMembersAdmin_migcsa_index
             'R'   => 9,
             'FM'  => 11,
         );
+        // Custom Fields
+        $this->custom_fields = array(
+            'cgcs_member_number'      => 1,
+            'cgcs_member'             => 2,
+            'committee_volunteer'     => 3,
+            'board_of_directors'      => 4,
+            'holes_maintained'        => 5,
+            'operation_type'          => 6,
+            'company_position'        => 7,
+            'district'                => 8,
+            'spouse'                  => 9,
+            'active'                  => 10,
+            'join_date'               => 11,
+            'company_name'            => 12,
+            'preferred_communication' => 13,
+            'process_email'           => 14,
+            'certification'           => 15,
+        );
+
 
         $this->dbh = new PDO(
             'pgsql: host=ds4.gaslightmedia.com dbname=migcsa2_update user=nobody',
@@ -318,6 +339,8 @@ class GlmMembersAdmin_migcsa_index
     public function importNewMembers()
     {
         $response = '';
+        $error = 0;
+        $messages = array();
         // Looking for all members not already in the new database
         $old_members = array();
         $old_member_data = $this->wpdb->get_results(
@@ -347,12 +370,16 @@ class GlmMembersAdmin_migcsa_index
          WHERE MC.member_id = :member_id";
         $getMemberCategories = $this->dbh->prepare( $categorySql );
 
+        // Start Transaction
+        $this->wpdb->query( 'START TRANSACTION' );
+        $this->wpdb->show_errors();
+
         foreach ( $members as $member ) {
             // Initialize $insert_data array.
             $insert_data = array();
             $member_name = $member['primary_contact_lname'] . ', ' . $member['primary_contact_fname'];
             $access      = ( $member['active'] ) ? $this->config['access_numb']['Moderated'] : $this->config['access_numb']['NotDisplayed'];
-            $member_type = ( $member['member_class'] ) ? $member_types[$member['member_class']] : 0;
+            $member_type = ( $member['member_class'] ) ? $this->member_types[$member['member_class']] : 0;
             // Add the member data
             $insert_data['data'] = array(
                 'access'        => $access,
@@ -372,6 +399,9 @@ class GlmMembersAdmin_migcsa_index
             );
             $new_member_id = $this->importMemberData( $insert_data );
             if ( $new_member_id ) {
+                $contactRoleNumb = $this->config['contact_role_numb']['EntityManager'];
+                $wpRole          = $this->config['contact_role_wordpress'][$contactRoleNumb];
+                $mailing_city_id = $this->getCityId( $member['mailing_city'] );
                 // Add the member contact
                 $contact['data'] = array(
                     'active'          => true,
@@ -412,6 +442,35 @@ class GlmMembersAdmin_migcsa_index
                     '%s', // zip
                 );
                 $new_contact_id = $this->importMemberContactData( $contact );
+                if ( $new_contact_id ) {
+                    $wpUserID = wp_insert_user(
+                        array(
+                            'user_email' => $member['process_email'],
+                            'user_login' => $member['member_login'],
+                            'user_pass'  => $member['member_passwd'],
+                            'first_name' => $member['primary_contact_fname'],
+                            'last_name'  => $member['primary_contact_lname'],
+                            'role'       => $wpRole
+                        )
+                    );
+                    // echo '<pre>$wpUserID: ' . print_r( $wpUserID, true ) . '</pre>';
+                    if (is_int($wpUserID) && $wpUserID > 0) {
+                        // Store the contact ID and active status into user meta data.
+                        update_user_meta($wpUserID, 'glmMembersContactID', $newContactID);
+                        update_user_meta($wpUserID, 'glmMembersContactActive', true);
+                        echo '<pre>$wpUserID: ' . print_r( $wpUserID, true ) . '</pre>';
+                    } else if ( is_wp_error( $wpUserID ) ) {
+                        $error = true;
+                        $messages[''] = $errors['username'] = '<span style="color:red;">An error occurred! ' . $wpUserID->get_error_message() . '</span>';
+                    } else {
+                        $error = true;
+                        $messages[] = '<span style="color:red;">An error occurred! wpUserID</span>';
+                    }
+                } else {
+                    $this->wpdb->print_error();
+                    $error = true;
+                    $messages[] = '<span style="color:red;">An error occurred! new_contact_id</span>';
+                }
                 $response .= '<pre>$new_contact_id: ' . print_r( $new_contact_id, true ) . '</pre>';
                 // Reset $insert_data array.
                 $insert_data = array();
@@ -419,7 +478,6 @@ class GlmMembersAdmin_migcsa_index
 
                 // Add the member_info record
                 $city_id         = $this->getCityId( $member['city'] );
-                $mailing_city_id = $this->getCityId( $member['mailing_city'] );
                 $region_id       = $this->getRegionId( $member['region'] );
                 $insert_data['data'] = array(
                     'member'         => $new_member_id,
@@ -498,9 +556,107 @@ class GlmMembersAdmin_migcsa_index
                         );
                     }
 
+                } else {
+                    $error = true;
+                    $messages[] = '<span style="color:red;">An error occurred! new_member_info_id</span>';
                 }
-            }
+
+                // Add billing account
+                // Get the invoice type that matches the member type.
+                $invoice_type = $this->wpdb->get_row(
+                    $this->wpdb->prepare(
+                        "SELECT *
+                           FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoice_types
+                          WHERE member_type = %d",
+                        $member_type
+                    ),
+                    ARRAY_A
+                );
+                if ( $invoice_type ) {
+                    // echo '<pre>$invoice_type: ' . print_r( $invoice_type, true ) . '</pre>';
+                    $this->wpdb->insert(
+                        GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts',
+                        array(
+                            'ref_dest'         => $new_member_id,
+                            'ref_name'         => $member_name,
+                            'anniversary_date' => date( 'Y-m-d' ),
+                            'boss'             => $member['boss'],
+                            'invoice_type'     => $invoice_type,
+                            'billing_fname'    => $member['primary_contact_fname'],
+                            'billing_lname'    => $member['primary_contact_lname'],
+                            'billing_addr1'    => $member['mailing_address'],
+                            'billing_city'     => $member['mailing_city'],
+                            'billing_state'    => $member['state_abb'],
+                            'billing_zip'      => $member['mailing_zip'],
+                            'billing_phone'    => $member['phone'],
+                            'email'            => $member['process_email'],
+                        ),
+                        array(
+                            '%d', // ref_dest
+                            '%s', // ref_name
+                            '%s', // anniversary_date
+                            '%d', // boss
+                            '%d', // invoice_type
+                            '%s', // billing_fname
+                            '%s', // billing_lname
+                            '%s', // billing_addr1
+                            '%s', // billing_city
+                            '%s', // billing_state
+                            '%s', // billing_zip
+                            '%s', // billing_phone
+                            '%s', // email
+                        )
+                    );
+                    $accountID = $this->wpdb->insert_id;
+                    if ( !$accountID ) {
+                        $error = true;
+                        $messages[] = '<span style="color:red;">An error occurred! accountID</span>';
+                    }
+                } else {
+                    // No matching invoice type for this member type means this is a free membership account
+                    // So no account is created for billing.
+                }
+
+                // Custom Fields
+                foreach ( $this->custom_fields as $cf_label => $cf_id ) {
+                    if ( $member[$cf_label] ) {
+                        if ( in_array( $cf_id, array( 2,3,4,10 ) ) ) {
+                            $field_data = 'Yes';
+                        } else {
+                            $field_data = $member[$cf_label];
+                        }
+                        $this->wpdb->insert(
+                            GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_data',
+                            array(
+                                'entity_id'  => $new_contact_id,
+                                'field_id'   => $cf_id,
+                                'field_data' => $field_data
+                            ),
+                            array(
+                                '%d',
+                                '%d',
+                                '%s'
+                            )
+                        );
+                    }
+                }
+
+            } // End New member id
+        }
+        // End of member loop
+
+        // END Transaction
+        if ( !$error ) {
+            $this->wpdb->query( 'COMMIT' );
+            // $this->wpdb->query( 'ROLLBACK' );
+        } else {
+            $this->wpdb->query( 'ROLLBACK' );
         }
+
+        // Show any errors
+        echo '<pre>$error: ' . print_r( $error, true ) . '</pre>';
+        echo '<pre>$messages: ' . print_r( $messages, true ) . '</pre>';
+
         return $response;
     }
 
@@ -518,7 +674,7 @@ class GlmMembersAdmin_migcsa_index
 
     public function getCityId( $city_name )
     {
-        return $this->wpdb->get_var(
+        $city_id = $this->wpdb->get_var(
             $this->wpdb->prepare(
                 "SELECT id
                    FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities
@@ -526,6 +682,15 @@ class GlmMembersAdmin_migcsa_index
                 $city_name
             )
         );
+        if ( $city_id ) {
+            return $city_id;
+        }
+        $this->wpdb->insert(
+            GLM_MEMBERS_PLUGIN_DB_PREFIX . 'cities',
+            array( 'name' => $city_name ),
+            array( '%s' )
+        );
+        return $this->wpdb->insert_id;
     }
 
     public function getRegionId( $region_name )
@@ -564,8 +729,8 @@ class GlmMembersAdmin_migcsa_index
     {
         $this->wpdb->insert(
             GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . 'contacts',
-            $member_info['data'],
-            $member_info['format']
+            $contact['data'],
+            $contact['format']
         );
         return $this->wpdb->insert_id;
     }