|
- <?php
-
- namespace Biukop;
-
- class XeroOauth2ShortCode
- {
- private $oauth2;
- public function __construct($oauth2){
- $this->oauth2 = $oauth2;
-
- add_shortcode('xero_org_name', array($this, 'xero_org_name'));
- add_shortcode('xero_org_contacts', array($this, 'xero_org_contacts'));
- add_shortcode('xero_org_invoices', array($this, 'xero_org_invoices'));
- add_shortcode('xero_org_pay_items', array($this, 'xero_org_pay_items'));
- add_shortcode('xero_org_employees', array($this, 'xero_org_employees'));
- add_shortcode('xero_org_clients', array($this, 'xero_org_clients'));
- add_shortcode('xero_org_payroll_calendar', array($this, 'xero_org_payroll_calendar'));
-
- }
-
- public function xero_org_name()
- {
- ini_set('display_errors', 'On');
- $apiInstance = $this->oauth2->get_accounting_instance();
- $apiResponse = $apiInstance->getOrganisations($this->oauth2->getTenantId());
- return 'Organisation Name: <b> ' . $apiResponse->getOrganisations()[0]->getName() . "</b> ";
- }
-
- public function xero_org_contacts()
- {
- ini_set('display_errors', 'On');
- $apiInstance = $this->oauth2->get_accounting_instance();
-
- $apiResponse = $apiInstance->getContacts($this->oauth2->getTenantId());
- $contacts = $apiResponse->getContacts();
- $message = "<table> ";
- $message .= "<tr> <td> # </td>
- <td> unique-id </td>
- <td> name </td>
- <td> Supplier </td>
- <td> Customer </td>
- <td> Group </td>
- <td> Status </td> </tr> ";
- $count = 1;
- foreach ($contacts as $c) {
-
- $group = "";
- foreach ($c->getContactGroups() as $g) {
- $group .= $g->getName() . " - " . $g->getStatus() . "<br>";
- }
-
- $message .= "<tr> <td>" . $count++ . "</td> " .
- "<td> " . $c->getContactId() . "</td> " .
- "<td> " . $c->getName() . "</td> " .
- "<td> " . ($c->getIsSupplier() ? "yes" : " - ") . "</td> " .
- "<td> " . ($c->getIsCustomer() ? "yes" : " - ") . "</td> " .
- "<td> " . $group . "</td> " .
- "<td> " . $c->getContactStatus() . "</td> " .
- "</tr>";
- }
- $message .= "</table>";
- return $message;
-
- }
-
- public function xero_org_invoices() {
- ini_set('display_errors', 'On');
- $apiInstance = $this->oauth2->get_accounting_instance();
-
- $apiResponse = $apiInstance->getInvoices($this->xeroTenantId);
- $invoices = $apiResponse->getInvoices();
-
- $message = "<table> ";
- $message .= "<tr> <td> # </td>
- <td> invoice num</td>
- <td> date </td>
- <td> contact </td>
- <td> amount </td>
- <td> Type </td>
- <td> Status </td> </tr> ";
- $count = 1;
-
- foreach ( $invoices as $c) {
- $strDate = "";
- $d = $c->getDate();
- if ( $d != null) {
- $da = $c->getDateAsDate();
- $strDate = $da->format("Y-m-d");
- } else {
- $strDate = $d;
- }
-
- $message .= "<tr> <td>" . $count ++ . "</td> " .
- "<td> " . $c->getInvoiceNumber() . "</td> " .
- "<td> " . $strDate . "</td> " .
- "<td> " . $c->getContact()->getName() . "</td> " .
- "<td> " . $c->getTotal() . "</td> " .
- "<td> " . $c->getType() . "</td> " .
- "<td> " . $c->getStatus() . "</td> " .
- "</tr>";
- }
- $message .= "</table>";
- return $message;
-
- }
-
- public function xero_org_pay_items(){
- ini_set('display_errors', 'On');
- $api = $this->oauth2->get_payroll_au_instance();
-
- $xeroTenantId = $this->oauth2->xeroTenantId;
- //$xeroTenantId = "e23fd416-3b66-43e9-b908-97fbefa24eb8"; // demo company;
- //$xeroTenantId = "4e2521ae-83e6-4895-aa90-b20aa0825ce1"; // Acaresydney ;
-
- $ifModifiedSince = null;
- $where = null; // "Status==\"ACTIVE\"";
- $order = null; // "EmailAddress%20DESC";
- $page = 1;
- // $result = null;
- try {
- $result = $api->getPayItems ($xeroTenantId, $ifModifiedSince, $where, $order, $page);
- $rates = $result->getPayItems()->getEarningsRates();
- $message = "<table> ";
- $message .= "<tr> <td> # </td>
- <td> Name </td>
- <td> EarningsType </td>
- <td> RateType </td>
- <td> AccountCode </td>
- <td> Multiplier </td>
- <td> IsExemptFromTax </td>
- <td> IsExemptFromSuper </td>
- <td> AccrueLeave </td>
- <td> IsReportableAsW1 </td>
- <td> UpdatedDateUTC </td>
- <td> CurrentRecord </td> </tr> ";
- $count = 1;
-
- foreach ( $rates as $r) {
- $message .= "<tr> <td>" . $r->getEarningsRateId() . "</td> " .
- "<td> " . $r->getName() . "</td> " .
- "<td> " . $r->getEarningsType() . "</td> " .
- "<td> " . $r->getRateType() . "</td> " .
- "<td> " . $r->getAccountCode() . "</td> " .
- "<td> " . $r->getTypeOfUnits() . "</td> " .
- "<td> " . $r->getRatePerUnit() . "</td> " .
- "<td> " . $r->getIsExemptFromTax() . "</td> " .
- "<td> " . $r->getIsExemptFromSuper() . "</td> " .
- "<td> " . $r->getIsReportableAsW1() . "</td> " .
- "<td> " . $r->getUpdatedDateUtc() . "</td> " .
- "<td> " . $r->getCurrentRecord() . "</td> " .
- "</tr>";
- }
- $message .= "</table>";
- return $message;
- } catch (\Exception $e) {
- echo 'Exception when calling PayrollAuApi->getPayItems: ', $e->getMessage(), PHP_EOL;
- return;
- }
- }
-
- public function xero_org_employees(){
- // $this->oauth2->getEmployees();
- //$this->oauth2->devGetEmployees();
- return get_option('bts_employees_updated_desc');
-
- }
-
- public function xero_org_clients(){
- //$this->oauth2->getClients();
- return get_option('bts_clients_updated_desc');
- }
-
-
- public function xero_org_payroll_calendar()
- {
- update_option('bts_pay_roll_calendar_last_sync', time());
- try {
- $pc = $this->oauth2->get_payroll_calendar();
- $start = $pc->getStartDateAsDate()->format('Y-m-d');
- $finish = new \DateTime($start);
- $finish = $finish->modify("+13 days")->format('Y-m-d');
- $paydate = $pc->getPaymentDateAsDate()->format('Y-m-d');
- $calendar["start"] = $start;
- $calendar["finish"] = $finish;
- $calendar["paydate"] = $paydate;
- update_option('bts_pay_roll_calendar', $calendar);
- return "start = $start, finish=$finish, paydate=$paydate";
- }catch (\Exception $e) {
- echo 'Exception when calling PayrollAuApi->getPayrollCalendar: ', $e->getMessage(), PHP_EOL;
- }
-
- }
-
-
- public function updateClientsShortCode($allClients) {
- // ini_set('display_errors', 'On');
- try {
- $contacts = $allClients;
-
- $message = "<table> ";
- $message .= "<tr> <td> # </td>
- <td> Name </td>
- <td> Last </td>
- <td> Status </td>
- <td> Email </td>
- <td> AccountNumber </td>
- <td> Addresses </td>
- <td> Updated (UTC) </td>
- </tr> ";
- $count = 1;
- foreach ($contacts as $r){
-
- $message .= "<tr> <td>" . $count . "</td> " .
- "<td> " . $r->getFirstName() . "</td> " .
- "<td> " . $r->getLastName() . "</td> " .
- "<td> " . $r->getContactStatus() . "</td> " .
- "<td> " . $r->getEmailAddress() . "</td> " .
- "<td> " . $r->getAccountNumber() . "</td> " .
- "<td> " . $this->oauth2->getClientAddress($r) . "</td> " .
- "<td> " . $r->getUpdatedDateUtcAsDate()->format("Y-m-d") . "</td> " .
- "</tr>";
- $count ++;
- }
- $message .= "</table>";
- update_option('bts_clients_updated_desc', $message);
- return $message;
- } catch (\Exception $e) {
- echo 'Exception when calling PayrollAuApi->getPayContacts: ', $e->getMessage(), PHP_EOL;
- return;
- }
- }
-
-
- public function updateEmployeeShortCode($allEmployees) {
- ini_set('display_errors', 'On');
-
- try {
- $employees = $allEmployees;
-
-
- $message = "<table> ";
- $message .= "<tr> <td> # </td>
- <td> First </td>
- <td> Last </td>
- <td> Status </td>
- <td> Email </td>
- <td> DOB </td>
- <td> Gender </td>
- <td> Mobile </td>
- <td> Start </td>
- <td> Updated(UTC) </td>
- </tr> ";
- $count = 1;
- foreach ($employees as $r){
- if ($r->getEmployeeGroupName() !== "Web-Employee" ){
- continue; //dont include them;
- }
- $message .= "<tr> <td>" . $count . "</td> " .
- "<td> " . $r->getFirstName() . "</td> " .
- "<td> " . $r->getLastName() . "</td> " .
- "<td> " . $r->getStatus() . "</td> " .
- "<td> " . $r->getEmail() . "</td> " .
- "<td> " . $r->getDateOfBirthAsDate()->format("M d Y") . "</td> " .
- "<td> " . $r->getGender() . "</td> " .
- "<td> " . $r->getMobile() . "</td> " .
- "<td> " . $r->getStartDateAsDate()->format("M d Y") . "</td> " .
- "<td> " . $r->getUpdatedDateUtcAsDate()->format("Y-m-d") . "</td> " .
- "</tr>";
- $count ++;
- }
- $message .= "</table>";
- update_option('bts_employees_updated_desc', $message);
- return $message;
- } catch (\Exception $e) {
- echo 'Exception when calling PayrollAuApi->getPayItems: ', $e->getMessage(), PHP_EOL;
- return;
- }
- }
-
- }
|