From: Steve Sutton Date: Fri, 20 Apr 2018 20:31:12 +0000 (-0400) Subject: working more on employeess X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=e173e3666e21d33cd6d1d5382116bff3074fd1c1;p=user%2Fsteve%2Fglm-member-db-migcsa.git working more on employeess reding employees so grabing the current data and add to new tables. --- diff --git a/models/admin/migcsa/index.php b/models/admin/migcsa/index.php index 488af20..a4b7519 100644 --- a/models/admin/migcsa/index.php +++ b/models/admin/migcsa/index.php @@ -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 '
$employees: ' . print_r( $employees, true ) . '
'; + } + + 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 .= '
$members: ' . print_r( $members, true ) . '
'; @@ -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 index 0000000..8be1b74 --- /dev/null +++ b/views/admin/migcsa/employees.html @@ -0,0 +1,5 @@ +

Import for migcsa members

+ +{if $response} + {$response} +{/if} diff --git a/views/admin/migcsa/index.html b/views/admin/migcsa/index.html index 54b7f54..48ca8fb 100644 --- a/views/admin/migcsa/index.html +++ b/views/admin/migcsa/index.html @@ -4,3 +4,5 @@ Import Members
Import Custom Fields +
+Import Employees