timesheet source code
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

193 行
6.4KB

  1. <?php
  2. namespace Biukop;
  3. use \XeroPHP\Application\PrivateApplication;
  4. class Xero {
  5. private $xero;
  6. private $clientgroup="48646f3d-cf5e-4fea-8c8b-5812bd540e1b";
  7. private $minimum_sync_interval_in_seconds = 600;
  8. private function default_config(){
  9. $config = array(
  10. 'oauth' => [
  11. 'callback' => 'http://acaresydney.com.au/',
  12. 'consumer_key' => 'G6AJIRWTH0X3C5SVS3ETZXNFCMDNGG',
  13. 'consumer_secret' => 'LP0PTSBCJLBB4CGYYKOHDXYF2NWXWD',
  14. 'rsa_private_key' => 'file://' . dirname(__FILE__) . '/keys/privatekey.pem',
  15. ],
  16. );
  17. return $config;
  18. }
  19. private function office_config(){
  20. $office_config = [
  21. 'xero' => [
  22. // 'payroll_version' =>'1.9',
  23. ],
  24. 'curl' => [
  25. CURLOPT_USERAGENT =>'AcareSydneyWebOffice',
  26. ],
  27. 'oauth' => [
  28. 'callback' => 'http://acaresydney.com.au/',
  29. 'consumer_key' => 'JE4LWKCJ6NHED30RFZWCT7WQYTS8JD',
  30. 'consumer_secret' => 'JXVZAZWKGM7MLUZSVIMK7ZSJE9GNYQ',
  31. 'rsa_private_key' => 'file://' . dirname(__FILE__) . '/keys/privatekey.pem',
  32. ],
  33. ];
  34. return $office_config;
  35. }
  36. public function __construct(){
  37. $this->xero = new PrivateApplication($this->office_config());
  38. }
  39. public function getClients($contact_group_id){
  40. $xero = $this->xero;
  41. $cg = $xero->loadByGUID("Accounting\\ContactGroup", $contact_group_id);
  42. $contacts = $cg->getContacts();
  43. return $contacts;
  44. }
  45. public function getContact($id){
  46. $user = $this->xero->loadByGUID("Accounting\\Contact",$id);
  47. return $user;
  48. }
  49. public function getEmployees(){
  50. $employees = $this->xero->load("PayrollAU\\Employee")
  51. ->where('EmployeeGroupName="Web-Employee"')
  52. ->execute();
  53. return $employees;
  54. }
  55. public function getEmployee($id){
  56. $employee = $this->xero->loadByGUID("PayrollAU\\Employee",$id);
  57. return $employee;
  58. }
  59. //
  60. //sync users to wordpress system
  61. //does not work for too many users or employees
  62. public function sync_users(){
  63. $this->sync_clients();
  64. //$this->sync_employees();
  65. }
  66. private function sync_clients(){
  67. $contacts = $this->getClients($this->clientgroup);
  68. foreach ($contacts as $c){
  69. $msg = sprintf("SYNC Client name=[%s] {%s} \n", $c->getName(), $c->getContactID());
  70. $this->logConsole($msg);
  71. $this->ensure_contact_exists($c);
  72. }
  73. }
  74. private function sync_employees(){
  75. $employees = $this->getEmployees();
  76. foreach ( $employees as $e){
  77. $msg = sprintf("SYNC employee name=[%s %s] {%s} \n", $e->getFirstName(), $e->getLastName(), $e->getEmployeeID());
  78. $this->logConsole($msg);
  79. $this->ensure_staff_exists($e);
  80. }
  81. }
  82. private function ensure_contact_exists($contact){
  83. $login = $contact->getContactID();
  84. $user = get_user_by('login', $login);
  85. if ($user === false){
  86. $xero_contact = $this->getContact($login);
  87. $args = $this->xero_contact_profile($xero_contact);
  88. $id = wp_insert_user($args);
  89. $user = get_user_by('ID', $id);
  90. }else{//update user
  91. if ($this->is_too_close_to_sync($user)){
  92. return;
  93. }
  94. $xero_contact = $this->getContact($login);
  95. $args = $this->xero_contact_profile($xero_contact);
  96. $args['ID'] = $user->ID;
  97. unset($args['user_pass']); //we don't change password
  98. wp_update_user($args);
  99. }
  100. $this->mark_updated($user->ID);
  101. }
  102. private function xero_contact_profile($c){
  103. $args = [
  104. 'user_login' => $username = $c->getContactID(),
  105. 'user_pass' => md5(uniqid(rand() + time(), true)),
  106. 'display_name' =>$c->getName(),
  107. 'user_email' => $c->getEmailAddress(),
  108. 'first_name' => $c->getFirstName(),
  109. 'last_name' => $c->getLastName(),
  110. 'nickname' => $c->getName(),
  111. 'role' => 'client',
  112. ];
  113. return $args;
  114. }
  115. private function ensure_staff_exists($employee)
  116. {
  117. $login = $employee->getEmployeeID();
  118. $xero_employee = $this->getEmployee($login);
  119. $args = $this->xero_employee_profile($xero_employee);
  120. $user = get_user_by('login', $login);
  121. if ($user === false){
  122. $id = wp_insert_user($args);
  123. $user = get_user_by('ID', $id);
  124. }else{
  125. if ($this->is_too_close_to_sync($user)){
  126. return;
  127. }
  128. $args['ID'] = $user->ID;
  129. unset($args['user_pass']);
  130. wp_update_user($args);
  131. }
  132. $this->mark_updated($user->ID);
  133. }
  134. private function xero_employee_profile($e){
  135. $args = [
  136. 'user_login' => $username = $e->getEmployeeID(),
  137. 'user_pass' => md5(uniqid(rand() + time(), true)),
  138. 'display_name' =>$e->getFirstName() . " " . $e->getLastName(),
  139. 'user_email' => $e->getEmail(),
  140. 'first_name' => $e->getFirstName(),
  141. 'last_name' => $e->getLastName(),
  142. 'nickname' => $e->getFirstName(),
  143. 'role' => 'staff',
  144. ];
  145. return $args;
  146. }
  147. private function mark_updated($userid){
  148. update_user_meta($userid, 'lastsync', time());
  149. }
  150. private function get_last_sync($userid){
  151. $lastsync = get_user_meta($userid, 'lastsync');
  152. return (int)($lastsync[0]);
  153. }
  154. private function is_too_close_to_sync($user){
  155. $userid = $user->ID;
  156. $lastsync = $this->get_last_sync($user->ID);
  157. $now = time();
  158. $diff = $now - (int) $lastsync;
  159. echo "lastsync = $lastsync, now = $now , diff = $diff; \n" ;
  160. if ($diff < $this->minimum_sync_interval_in_seconds){
  161. $msg = sprintf("SKIP sync user %d, login=%s, display_name=%s\n", $user->ID, $user->user_login, $user->display_name);
  162. $this->logConsole($msg);
  163. return true;
  164. }
  165. return false;
  166. }
  167. private function logConsole($str){
  168. //if is commandline
  169. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  170. echo $str;
  171. }
  172. }
  173. }