|
|
|
@@ -3,6 +3,7 @@ namespace Biukop; |
|
|
|
|
|
|
|
use \XeroPHP\Application\PrivateApplication; |
|
|
|
use \XeroPHP\Remote\Exception\RateLimitExceededException; |
|
|
|
use \XeroPHP\Remote\Exception\NotFoundException; |
|
|
|
|
|
|
|
class Xero { |
|
|
|
private $xero; |
|
|
|
@@ -78,12 +79,16 @@ class Xero { |
|
|
|
$this->logConsole($msg); |
|
|
|
|
|
|
|
try{ |
|
|
|
//$this->sync_clients(); |
|
|
|
$this->sync_employees(); |
|
|
|
$this->sync_clients(); |
|
|
|
//$this->sync_employees(); |
|
|
|
}catch(RateLimitExceededException $e){ |
|
|
|
$msg= "Xero API rate limit exceeded, please try again later, existing sync within 600 seconds will by passed automatically\n"; |
|
|
|
$this->logConsole($msg); |
|
|
|
}catch(NotFoundException $e){ |
|
|
|
$msg= "Xero API resource not found rate limit exceeded, please try again later, existing sync within 600 seconds will by passed automatically\n"; |
|
|
|
$this->logConsole($msg); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
private function sync_clients(){ |
|
|
|
$contacts = $this->getClients($this->clientgroup); |
|
|
|
@@ -111,6 +116,8 @@ class Xero { |
|
|
|
$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 ($this->is_too_close_to_sync($user)){ |
|
|
|
return; |
|
|
|
@@ -120,6 +127,8 @@ class Xero { |
|
|
|
$args['ID'] = $user->ID; |
|
|
|
unset($args['user_pass']); //we don't change password |
|
|
|
wp_update_user($args); |
|
|
|
update_user_meta($user->ID, 'address', $args['address']); |
|
|
|
update_user_meta($user->ID, 'account', $args['account']); |
|
|
|
} |
|
|
|
$this->mark_updated($user->ID); |
|
|
|
} |
|
|
|
@@ -133,11 +142,51 @@ class Xero { |
|
|
|
'first_name' => $c->getFirstName(), |
|
|
|
'last_name' => $c->getLastName(), |
|
|
|
'nickname' => $c->getName(), |
|
|
|
'account' => $c->getAccountNumber(), |
|
|
|
'address'=> $this->get_post_address($c), |
|
|
|
'role' => 'client', |
|
|
|
]; |
|
|
|
return $args; |
|
|
|
} |
|
|
|
|
|
|
|
private function get_post_address($client){ |
|
|
|
$result = ""; |
|
|
|
$addr = $this->get_client_address_by_type($client, 'POBOX'); |
|
|
|
|
|
|
|
if ( $addr != false){ |
|
|
|
if ($addr->getAddressLine1() != ""){ |
|
|
|
$result .= $addr->getAddressLine1() . ";"; |
|
|
|
} |
|
|
|
if ($addr->getAddressLine2() != ""){ |
|
|
|
$result .= $addr->getAddressLine2() . ";"; |
|
|
|
} |
|
|
|
if ($addr->getAddressLine3() != ""){ |
|
|
|
$result .= $addr->getAddressLine3() . ";"; |
|
|
|
} |
|
|
|
if ($addr->getAddressLine4() != ""){ |
|
|
|
$result .= $addr->getAddressLine4() . ";"; |
|
|
|
} |
|
|
|
if ($addr->getCity() != ""){ |
|
|
|
$result .= $addr->getCity() . ";"; |
|
|
|
} |
|
|
|
if ($addr->getPostalCode() != ""){ |
|
|
|
$result .= $addr->getPostalCode() . ";"; |
|
|
|
} |
|
|
|
} |
|
|
|
echo "result for client is " . $result . "\n"; |
|
|
|
return $result; |
|
|
|
} |
|
|
|
|
|
|
|
private function get_client_address_by_type($client, $t){ |
|
|
|
$addr = false; |
|
|
|
foreach( $client->getAddresses() as $a){ |
|
|
|
if( $a->getAddressType() == $t){ |
|
|
|
$addr = $a; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
return $addr; |
|
|
|
} |
|
|
|
|
|
|
|
private function ensure_staff_exists($employee) |
|
|
|
{ |
|
|
|
@@ -148,6 +197,7 @@ class Xero { |
|
|
|
$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']); |
|
|
|
}else{ |
|
|
|
if ($this->is_too_close_to_sync($user)){ |
|
|
|
return; |