working more on employeess
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 20 Apr 2018 20:31:12 +0000 (16:31 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 20 Apr 2018 20:31:12 +0000 (16:31 -0400)
reding employees so grabing the current data and add to new tables.

models/admin/migcsa/index.php
views/admin/migcsa/employees.html [new file with mode: 0644]
views/admin/migcsa/index.html

index 488af20..a4b7519 100644 (file)
@@ -37,6 +37,7 @@ class GlmMembersAdmin_migcsa_index
      * @access public
      */
     public $migcsaID = false;
+    public $dbh = false;
 
     /**
      * Constructor
@@ -97,7 +98,7 @@ class GlmMembersAdmin_migcsa_index
             'FM'  => 11,
         );
 
-        $dbh = new PDO(
+        $this->dbh = new PDO(
             'pgsql: host=ds4.gaslightmedia.com dbname=migcsa2_update user=nobody',
             null,
             null,
@@ -106,7 +107,7 @@ class GlmMembersAdmin_migcsa_index
             )
         );
 
-        $dbh->setAttribute(
+        $this->dbh->setAttribute(
             PDO::ATTR_ERRMODE,
             PDO::ERRMODE_EXCEPTION
         );
@@ -119,13 +120,17 @@ class GlmMembersAdmin_migcsa_index
 
         switch( $option ) {
         case 'customFields':
-            $view = 'customFields';
+            $view     = 'customFields';
             $response = $this->importCustomFields();
             break;
         case 'import':
-            $view = 'import';
+            $view     = 'import';
             $response = $this->importNewMembers();
             break;
+        case 'employees':
+            $view     = 'employees';
+            $response = $this->importEmployees();
+            break;
         case 'list':
             $view = 'index';
             break;
@@ -154,6 +159,84 @@ class GlmMembersAdmin_migcsa_index
         return $response;
     }
 
+    public function importEmployees()
+    {
+        $sql = "
+        SELECT *
+          FROM member_employees";
+        $stmt      = $this->dbh->query( $sql );
+        $employees = $stmt->fetchAll();
+
+        // We have the data
+        // Need to get the account id's for member_id and employee based on the old_member_id from member table
+        // So first find the member_id's
+        foreach ( $employees as &$employee ) {
+            // Find the old_member_id for member_id
+            $member_account_id = $this->wpdb->get_var(
+                $this->wpdb->prepare(
+                    "SELECT id
+                       FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
+                      WHERE ref_dest IN (SELECT id
+                            FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+                            WHERE old_member_id = %d)",
+                    $employee['member_id']
+                )
+            );
+            $employee['member_account_id'] = $member_account_id;
+            // Find the old_member_id for employee
+            $employee_account_id = $this->wpdb->get_var(
+                $this->wpdb->prepare(
+                    "SELECT id
+                       FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
+                      WHERE ref_dest IN (SELECT id
+                            FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+                            WHERE old_member_id = %d)",
+                    $employee['employee']
+                )
+            );
+            $employee['employee_account_id'] = $employee_account_id;
+            // If we have both of these then add employee (if not already there)
+            $this->addEmployee( $member_account_id, $employee_account_id );
+        }
+
+        return '<pre>$employees: ' . print_r( $employees, true ) . '</pre>';
+    }
+
+    public function addEmployee( $account, $employee )
+    {
+        // Double check that it already doesn't exists
+        $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",
+                $account,
+                $employee
+            )
+        );
+        if ( !$emp_id ) {
+            $this->wpdb->insert(
+                GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'employees',
+                array(
+                    'account'  => $account,
+                    'employee' => $employee
+                ),
+                array(
+                    '%d',
+                    '%d'
+                )
+            );
+        }
+        // Flag the main account as boss
+        $this->wpdb->update(
+            GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts',
+            array( 'boss' => 1 ),
+            array( 'id' => $account ),
+            array( '%d' )
+        );
+    }
+
     public function importNewMembers()
     {
         $response = '';
@@ -175,7 +258,7 @@ class GlmMembersAdmin_migcsa_index
           FROM member M LEFT OUTER JOIN state S ON (S.state_id = M.mailing_state_id)
          WHERE M.member_id NOT IN (" . implode( ',', $old_members ) . ")
         ORDER BY M.member_id";
-        $stmt = $dbh->query( $sql );
+        $stmt = $this->dbh->query( $sql );
         $members = $stmt->fetchAll();
         $response .= '<pre>$members: ' . print_r( $members, true ) . '</pre>';
 
@@ -184,7 +267,7 @@ class GlmMembersAdmin_migcsa_index
         SELECT C.name
           FROM category C LEFT OUTER JOIN member_category MC ON (C.category_id = MC.category_id)
          WHERE MC.member_id = :member_id";
-        $getMemberCategories = $dbh->prepare( $categorySql );
+        $getMemberCategories = $this->dbh->prepare( $categorySql );
 
         foreach ( $members as $member ) {
             // Initialize $insert_data array.
diff --git a/views/admin/migcsa/employees.html b/views/admin/migcsa/employees.html
new file mode 100644 (file)
index 0000000..8be1b74
--- /dev/null
@@ -0,0 +1,5 @@
+<h2>Import for migcsa members</h2>
+
+{if $response}
+    {$response}
+{/if}
index 54b7f54..48ca8fb 100644 (file)
@@ -4,3 +4,5 @@
 <a href="{$thisUrl}?page={$thisPage}&option=import">Import Members</a>
 <br />
 <a href="{$thisUrl}?page={$thisPage}&option=customFields">Import Custom Fields</a>
+<br />
+<a href="{$thisUrl}?page={$thisPage}&option=employees">Import Employees</a>