timesheet source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 satır
11KB

  1. <?php
  2. namespace Biukop;
  3. class XeroOauth2ShortCode
  4. {
  5. private $oauth2;
  6. public function __construct($oauth2){
  7. $this->oauth2 = $oauth2;
  8. add_shortcode('xero_org_name', array($this, 'xero_org_name'));
  9. add_shortcode('xero_org_contacts', array($this, 'xero_org_contacts'));
  10. add_shortcode('xero_org_invoices', array($this, 'xero_org_invoices'));
  11. add_shortcode('xero_org_pay_items', array($this, 'xero_org_pay_items'));
  12. add_shortcode('xero_org_employees', array($this, 'xero_org_employees'));
  13. add_shortcode('xero_org_clients', array($this, 'xero_org_clients'));
  14. add_shortcode('xero_org_payroll_calendar', array($this, 'xero_org_payroll_calendar'));
  15. }
  16. public function xero_org_name()
  17. {
  18. ini_set('display_errors', 'On');
  19. $apiInstance = $this->oauth2->get_accounting_instance();
  20. $apiResponse = $apiInstance->getOrganisations($this->oauth2->getTenantId());
  21. return 'Organisation Name: <b> ' . $apiResponse->getOrganisations()[0]->getName() . "</b> ";
  22. }
  23. public function xero_org_contacts()
  24. {
  25. ini_set('display_errors', 'On');
  26. $apiInstance = $this->oauth2->get_accounting_instance();
  27. $apiResponse = $apiInstance->getContacts($this->oauth2->getTenantId());
  28. $contacts = $apiResponse->getContacts();
  29. $message = "<table> ";
  30. $message .= "<tr> <td> # </td>
  31. <td> unique-id </td>
  32. <td> name </td>
  33. <td> Supplier </td>
  34. <td> Customer </td>
  35. <td> Group </td>
  36. <td> Status </td> </tr> ";
  37. $count = 1;
  38. foreach ($contacts as $c) {
  39. $group = "";
  40. foreach ($c->getContactGroups() as $g) {
  41. $group .= $g->getName() . " - " . $g->getStatus() . "<br>";
  42. }
  43. $message .= "<tr> <td>" . $count++ . "</td> " .
  44. "<td> " . $c->getContactId() . "</td> " .
  45. "<td> " . $c->getName() . "</td> " .
  46. "<td> " . ($c->getIsSupplier() ? "yes" : " - ") . "</td> " .
  47. "<td> " . ($c->getIsCustomer() ? "yes" : " - ") . "</td> " .
  48. "<td> " . $group . "</td> " .
  49. "<td> " . $c->getContactStatus() . "</td> " .
  50. "</tr>";
  51. }
  52. $message .= "</table>";
  53. return $message;
  54. }
  55. public function xero_org_invoices() {
  56. ini_set('display_errors', 'On');
  57. $apiInstance = $this->oauth2->get_accounting_instance();
  58. $apiResponse = $apiInstance->getInvoices($this->xeroTenantId);
  59. $invoices = $apiResponse->getInvoices();
  60. $message = "<table> ";
  61. $message .= "<tr> <td> # </td>
  62. <td> invoice num</td>
  63. <td> date </td>
  64. <td> contact </td>
  65. <td> amount </td>
  66. <td> Type </td>
  67. <td> Status </td> </tr> ";
  68. $count = 1;
  69. foreach ( $invoices as $c) {
  70. $strDate = "";
  71. $d = $c->getDate();
  72. if ( $d != null) {
  73. $da = $c->getDateAsDate();
  74. $strDate = $da->format("Y-m-d");
  75. } else {
  76. $strDate = $d;
  77. }
  78. $message .= "<tr> <td>" . $count ++ . "</td> " .
  79. "<td> " . $c->getInvoiceNumber() . "</td> " .
  80. "<td> " . $strDate . "</td> " .
  81. "<td> " . $c->getContact()->getName() . "</td> " .
  82. "<td> " . $c->getTotal() . "</td> " .
  83. "<td> " . $c->getType() . "</td> " .
  84. "<td> " . $c->getStatus() . "</td> " .
  85. "</tr>";
  86. }
  87. $message .= "</table>";
  88. return $message;
  89. }
  90. public function xero_org_pay_items(){
  91. ini_set('display_errors', 'On');
  92. $api = $this->oauth2->get_payroll_au_instance();
  93. $xeroTenantId = $this->oauth2->xeroTenantId;
  94. //$xeroTenantId = "e23fd416-3b66-43e9-b908-97fbefa24eb8"; // demo company;
  95. //$xeroTenantId = "4e2521ae-83e6-4895-aa90-b20aa0825ce1"; // Acaresydney ;
  96. $ifModifiedSince = null;
  97. $where = null; // "Status==\"ACTIVE\"";
  98. $order = null; // "EmailAddress%20DESC";
  99. $page = 1;
  100. // $result = null;
  101. try {
  102. $result = $api->getPayItems ($xeroTenantId, $ifModifiedSince, $where, $order, $page);
  103. $rates = $result->getPayItems()->getEarningsRates();
  104. $message = "<table> ";
  105. $message .= "<tr> <td> # </td>
  106. <td> Name </td>
  107. <td> EarningsType </td>
  108. <td> RateType </td>
  109. <td> AccountCode </td>
  110. <td> Multiplier </td>
  111. <td> IsExemptFromTax </td>
  112. <td> IsExemptFromSuper </td>
  113. <td> AccrueLeave </td>
  114. <td> IsReportableAsW1 </td>
  115. <td> UpdatedDateUTC </td>
  116. <td> CurrentRecord </td> </tr> ";
  117. $count = 1;
  118. foreach ( $rates as $r) {
  119. $message .= "<tr> <td>" . $r->getEarningsRateId() . "</td> " .
  120. "<td> " . $r->getName() . "</td> " .
  121. "<td> " . $r->getEarningsType() . "</td> " .
  122. "<td> " . $r->getRateType() . "</td> " .
  123. "<td> " . $r->getAccountCode() . "</td> " .
  124. "<td> " . $r->getTypeOfUnits() . "</td> " .
  125. "<td> " . $r->getRatePerUnit() . "</td> " .
  126. "<td> " . $r->getIsExemptFromTax() . "</td> " .
  127. "<td> " . $r->getIsExemptFromSuper() . "</td> " .
  128. "<td> " . $r->getIsReportableAsW1() . "</td> " .
  129. "<td> " . $r->getUpdatedDateUtc() . "</td> " .
  130. "<td> " . $r->getCurrentRecord() . "</td> " .
  131. "</tr>";
  132. }
  133. $message .= "</table>";
  134. return $message;
  135. } catch (\Exception $e) {
  136. echo 'Exception when calling PayrollAuApi->getPayItems: ', $e->getMessage(), PHP_EOL;
  137. return;
  138. }
  139. }
  140. public function xero_org_employees(){
  141. // $this->oauth2->getEmployees();
  142. //$this->oauth2->devGetEmployees();
  143. return get_option('bts_employees_updated_desc');
  144. }
  145. public function xero_org_clients(){
  146. //$this->oauth2->getClients();
  147. return get_option('bts_clients_updated_desc');
  148. }
  149. public function xero_org_payroll_calendar()
  150. {
  151. update_option('bts_pay_roll_calendar_last_sync', time());
  152. try {
  153. $pc = $this->oauth2->get_payroll_calendar();
  154. $start = $pc->getStartDateAsDate()->format('Y-m-d');
  155. $finish = new \DateTime($start);
  156. $finish = $finish->modify("+13 days")->format('Y-m-d');
  157. $paydate = $pc->getPaymentDateAsDate()->format('Y-m-d');
  158. $calendar["start"] = $start;
  159. $calendar["finish"] = $finish;
  160. $calendar["paydate"] = $paydate;
  161. update_option('bts_pay_roll_calendar', $calendar);
  162. return "start = $start, finish=$finish, paydate=$paydate";
  163. }catch (\Exception $e) {
  164. echo 'Exception when calling PayrollAuApi->getPayrollCalendar: ', $e->getMessage(), PHP_EOL;
  165. }
  166. }
  167. public function updateClientsShortCode($allClients) {
  168. // ini_set('display_errors', 'On');
  169. try {
  170. $contacts = $allClients;
  171. $message = "<table> ";
  172. $message .= "<tr> <td> # </td>
  173. <td> Name </td>
  174. <td> Last </td>
  175. <td> Status </td>
  176. <td> Email </td>
  177. <td> AccountNumber </td>
  178. <td> Addresses </td>
  179. <td> Updated (UTC) </td>
  180. </tr> ";
  181. $count = 1;
  182. foreach ($contacts as $r){
  183. $message .= "<tr> <td>" . $count . "</td> " .
  184. "<td> " . $r->getFirstName() . "</td> " .
  185. "<td> " . $r->getLastName() . "</td> " .
  186. "<td> " . $r->getContactStatus() . "</td> " .
  187. "<td> " . $r->getEmailAddress() . "</td> " .
  188. "<td> " . $r->getAccountNumber() . "</td> " .
  189. "<td> " . $this->oauth2->getClientAddress($r) . "</td> " .
  190. "<td> " . $r->getUpdatedDateUtcAsDate()->format("Y-m-d") . "</td> " .
  191. "</tr>";
  192. $count ++;
  193. }
  194. $message .= "</table>";
  195. update_option('bts_clients_updated_desc', $message);
  196. return $message;
  197. } catch (\Exception $e) {
  198. echo 'Exception when calling PayrollAuApi->getPayContacts: ', $e->getMessage(), PHP_EOL;
  199. return;
  200. }
  201. }
  202. public function updateEmployeeShortCode($allEmployees) {
  203. ini_set('display_errors', 'On');
  204. try {
  205. $employees = $allEmployees;
  206. $message = "<table> ";
  207. $message .= "<tr> <td> # </td>
  208. <td> First </td>
  209. <td> Last </td>
  210. <td> Status </td>
  211. <td> Email </td>
  212. <td> DOB </td>
  213. <td> Gender </td>
  214. <td> Mobile </td>
  215. <td> Start </td>
  216. <td> Updated(UTC) </td>
  217. </tr> ";
  218. $count = 1;
  219. foreach ($employees as $r){
  220. if ($r->getEmployeeGroupName() !== "Web-Employee" ){
  221. continue; //dont include them;
  222. }
  223. $message .= "<tr> <td>" . $count . "</td> " .
  224. "<td> " . $r->getFirstName() . "</td> " .
  225. "<td> " . $r->getLastName() . "</td> " .
  226. "<td> " . $r->getStatus() . "</td> " .
  227. "<td> " . $r->getEmail() . "</td> " .
  228. "<td> " . $r->getDateOfBirthAsDate()->format("M d Y") . "</td> " .
  229. "<td> " . $r->getGender() . "</td> " .
  230. "<td> " . $r->getMobile() . "</td> " .
  231. "<td> " . $r->getStartDateAsDate()->format("M d Y") . "</td> " .
  232. "<td> " . $r->getUpdatedDateUtcAsDate()->format("Y-m-d") . "</td> " .
  233. "</tr>";
  234. $count ++;
  235. }
  236. $message .= "</table>";
  237. update_option('bts_employees_updated_desc', $message);
  238. return $message;
  239. } catch (\Exception $e) {
  240. echo 'Exception when calling PayrollAuApi->getPayItems: ', $e->getMessage(), PHP_EOL;
  241. return;
  242. }
  243. }
  244. }