| @@ -100,37 +100,81 @@ class Xero { | |||
| $this->logConsole($msg); | |||
| } | |||
| } | |||
| private function sync_clients(){ | |||
| private function sync_clients($add_new_only = false){ | |||
| $contacts = $this->getClients($this->clientgroup); | |||
| //add new first; | |||
| $to_update = []; | |||
| foreach ($contacts as $c){ | |||
| $login = $c->getContactID(); | |||
| $user = get_user_by('login', $login); | |||
| if ($user === false){ | |||
| $this->add_new_contact($c); | |||
| }else{ | |||
| $to_update[] = $c; // to update users later; | |||
| } | |||
| } | |||
| if ($add_new_only) | |||
| return; | |||
| //sync existing | |||
| foreach ($to_update as $c){ | |||
| $msg = sprintf("SYNC Client name=[%s] {%s} \n", $c->getName(), $c->getContactID()); | |||
| $this->logConsole($msg); | |||
| $this->ensure_contact_exists($c); | |||
| $this->update_existing_contact($c); | |||
| } | |||
| } | |||
| private function sync_employees(){ | |||
| private function sync_employees($add_new_only = false){ | |||
| $employees = $this->getEmployees(); | |||
| //add new staff | |||
| foreach ( $employees as $e){ | |||
| $login = $e->getContactID(); | |||
| $user = get_user_by('login', $login); | |||
| if ($user === false){ | |||
| $this->add_new_staff($e); | |||
| }else{ | |||
| $to_update[] = $e; // to update exiting users later; | |||
| } | |||
| } | |||
| if ($add_new_only) | |||
| return; | |||
| foreach ( $to_update as $e){ | |||
| $msg = sprintf("SYNC employee name=[%s %s] {%s} \n", $e->getFirstName(), $e->getLastName(), $e->getEmployeeID()); | |||
| $this->logConsole($msg); | |||
| // if ($e->getEmployeeID() != '3e8c2e62-8e28-4b68-ae98-9ef1d76188c4') | |||
| // continue; | |||
| $this->ensure_staff_exists($e); | |||
| $this->update_existing_staff($e); | |||
| } | |||
| } | |||
| private function ensure_contact_exists($contact){ | |||
| private function add_new_contact($contact){ | |||
| $login = $contact->getContactID(); | |||
| $user = get_user_by('login', $login); | |||
| if ($user === false){ | |||
| $msg = sprintf("ADD Client name=[%s] {%s} \n", $contact->getName(), $contact->getContactID()); | |||
| $this->logConsole($msg); | |||
| $xero_contact = $this->getContact($login); | |||
| $args = $this->xero_contact_profile($xero_contact); | |||
| $id = wp_insert_user($args); | |||
| $user = get_user_by('ID', $id); | |||
| update_user_meta($user->ID, 'address', $args['address']); | |||
| update_user_meta($user->ID, 'account', $args['account']); | |||
| }else{//update user | |||
| if (! id instanceof \WP_Error){ | |||
| $user = get_user_by('ID', $id); | |||
| update_user_meta($user->ID, 'address', $args['address']); | |||
| update_user_meta($user->ID, 'account', $args['account']); | |||
| }else{ | |||
| $msg = "==(Add Client failed)=="; | |||
| $this->logConsole($msg); | |||
| } | |||
| } | |||
| } | |||
| private function update_existing_contact($contact){ | |||
| $login = $contact->getContactID(); | |||
| $user = get_user_by('login', $login); | |||
| if ($user !== false) | |||
| {//update user - must be existing user; | |||
| if ($this->is_too_close_to_sync($user)){ | |||
| return; | |||
| } | |||
| @@ -200,22 +244,39 @@ class Xero { | |||
| return $addr; | |||
| } | |||
| private function ensure_staff_exists($employee) | |||
| private function add_new_staff($employee) | |||
| { | |||
| $login = $employee->getEmployeeID(); | |||
| $user = get_user_by('login', $login); | |||
| if ($user === false){ | |||
| echo "add new \n"; | |||
| $msg = sprintf("ADD employee name=[%s %s] {%s} \n", | |||
| $employee->getFirstName(), | |||
| $employee->getLastName(), | |||
| $employee->getEmployeeID()); | |||
| $this->logConsole($msg); | |||
| $xero_employee = $this->getEmployee($login); | |||
| $args = $this->xero_employee_profile($xero_employee); | |||
| $id = wp_insert_user($args); | |||
| $user = get_user_by('ID', $id); | |||
| update_user_meta($user->ID, 'mobile', $args['mobile']); | |||
| update_user_meta($user->ID, 'address', $args['address']); | |||
| }else{ | |||
| if ($this->is_too_close_to_sync($user)){ | |||
| return; | |||
| if (! id instanceof \WP_Error){ | |||
| $user = get_user_by('ID', $id); | |||
| update_user_meta($user->ID, 'mobile', $args['mobile']); | |||
| update_user_meta($user->ID, 'address', $args['address']); | |||
| }else{ | |||
| $msg = "==(Add staff failed)=="; | |||
| $this->logConsole($msg); | |||
| } | |||
| } | |||
| } | |||
| private function update_existing_staff($employee) | |||
| { | |||
| $login = $employee->getEmployeeID(); | |||
| $user = get_user_by('login', $login); | |||
| if ($this->is_too_close_to_sync($user)){ | |||
| return; | |||
| } | |||
| if ($user != false) { | |||
| $xero_employee = $this->getEmployee($login); | |||
| $args = $this->xero_employee_profile($xero_employee); | |||
| $args['ID'] = $user->ID; | |||
| @@ -308,10 +369,48 @@ class Xero { | |||
| public function init_wp(){ | |||
| try{ | |||
| $this->sync_payitem(); | |||
| $this->add_new_client(); | |||
| $this->add_new_employee(); | |||
| $this->sync_payroll_calendar(); | |||
| }catch(\XeroPHP\Remote\Exception $e){ | |||
| } | |||
| } | |||
| private function add_new_client(){ | |||
| if ($this->too_close_to_add_employee()){ | |||
| return; | |||
| } | |||
| update_option('bts_add_client_last_sync', time()); | |||
| $this->sync_clients(true);//add new only; | |||
| } | |||
| private function add_new_employee(){ | |||
| if ($this->too_close_to_add_client()){ | |||
| return; | |||
| } | |||
| update_option('bts_add_employee_last_sync', time()); | |||
| $this->sync_employee(true);//add new only; | |||
| } | |||
| private function sync_payroll_calendar() | |||
| { | |||
| if ($this->too_close_to_sync_payroll_calendar()){ | |||
| return; | |||
| } | |||
| update_option('bts_pay_roll_calendar_last_sync', time()); | |||
| $pc = $this->get_payroll_calendar(); | |||
| $start = $pc->getStartDate()->format('Y-m-d'); | |||
| $finish = new \DateTime($start); | |||
| $finish = $finish->modify("+13 days")->format('Y-m-d'); | |||
| $paydate = $pc->getPaymentDate()->format('Y-m-d'); | |||
| $calendar["start"] = $start; | |||
| $calendar["finish"] = $finish; | |||
| $calendar["paydate"] = $paydate; | |||
| update_option('bts_pay_roll_calendar', $calendar); | |||
| } | |||
| private function sync_payitem(){ | |||
| if ($this->too_close_to_sync_payitem()){ | |||
| @@ -359,7 +458,24 @@ class Xero { | |||
| $diff = $now - (int) $lastsync; | |||
| return $diff < $this->minimum_sync_interval_in_seconds; //default 10 minutes | |||
| } | |||
| private function too_close_to_add_employee(){ | |||
| $lastsync = get_option('bts_add_employee_last_sync', 0); | |||
| $now = time(); | |||
| $diff = $now - (int) $lastsync; | |||
| return $diff < 1.1 * $this->minimum_sync_interval_in_seconds; //default 1.1 * 10 minutes | |||
| } | |||
| private function too_close_to_add_client(){ | |||
| $lastsync = get_option('bts_add_client_last_sync', 0); | |||
| $now = time(); | |||
| $diff = $now - (int) $lastsync; | |||
| return $diff < 1.2 * $this->minimum_sync_interval_in_seconds; //default 1.2 * 10 minutes | |||
| } | |||
| private function too_close_to_sync_payroll_calendar(){ | |||
| $lastsync = get_option('bts_pay_roll_calendar_last_sync', 0); | |||
| $now = time(); | |||
| $diff = $now - (int) $lastsync; | |||
| return $diff < 1.3 * $this->minimum_sync_interval_in_seconds; //default 1.3 * 10 minutes | |||
| } | |||
| public function get_payroll_calendar() | |||
| { | |||