瀏覽代碼

short code demo function isolated from xero connection

master
sp 4 年之前
父節點
當前提交
f8c5fb6c1d
共有 2 個檔案被更改,包括 279 行新增262 行删除
  1. +8
    -262
      XeroOauth2.php
  2. +271
    -0
      XeroOauth2ShortCode.php

+ 8
- 262
XeroOauth2.php 查看文件

@@ -21,6 +21,8 @@ class XeroOAuth2
private $clientSecret = 'axgKF-Ri60D89conDFhqZsi1wu7uLdQFGvMpino9nI-nfO3f';
private $clientContactGroupID="48646f3d-cf5e-4fea-8c8b-5812bd540e1b";

private $shortcodes;

public $provider;
public $options = [
'scope' => ['openid email profile offline_access assets projects accounting.settings accounting.transactions accounting.contacts accounting.journals.read accounting.reports.read accounting.attachments payroll.employees payroll.payruns payroll.payslip payroll.timesheets payroll.settings files'],
@@ -54,6 +56,9 @@ class XeroOAuth2
public function __construct($office)
{
$this->office = $office;
$this->shortcodes = new XeroOauth2ShortCode($this);
$this->storage = new StorageClass();

add_action('init', array($this, 'init_wp'));
add_action('after_setup_theme', array($this, 'boot_carbon'));
// add_action('init', array('Carbon_Fields\\Carbon_Fields', 'boot'));
@@ -62,20 +67,10 @@ class XeroOAuth2
add_action('carbon_fields_container_activated', array($this, 'field_activated'));
add_action('parse_request', array($this, 'xero_callback'));

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'));



add_action('init', array($this, "xero_init"));
add_action('plugins_loaded', array($this, "load_storage"));
// $this->xero_init();
$this->storage = new StorageClass();

}

public function __call($method, $args) {
@@ -272,7 +267,7 @@ class XeroOAuth2
}
}

private function get_accounting_instance() {
public function get_accounting_instance() {
$this->refresh_token();
if ($this->apiAccountingInstance == null) {
$this->config = \XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken(
@@ -285,7 +280,7 @@ class XeroOAuth2
return $this->apiAccountingInstance;
}

private function get_payroll_au_instance() {
public function get_payroll_au_instance() {
$this->refresh_token();
if ($this->apiPayrollInstance == null) {
$this->config = \XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken(
@@ -300,255 +295,6 @@ class XeroOAuth2
return $this->apiPayrollInstance;
}

public function xero_org_name()
{
ini_set('display_errors', 'On');
$apiInstance = $this->get_accounting_instance();
$apiResponse = $apiInstance->getOrganisations($this->xeroTenantId);
return 'Organisation Name: <b> ' . $apiResponse->getOrganisations()[0]->getName() . "</b> ";
}

public function xero_org_contacts()
{
ini_set('display_errors', 'On');
$apiInstance = $this->get_accounting_instance();

$apiResponse = $apiInstance->getContacts($this->xeroTenantId);
$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->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->get_payroll_au_instance();

$xeroTenantId = $this->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(){
ini_set('display_errors', 'On');
$api = $this->get_payroll_au_instance();

$xeroTenantId = $this->xeroTenantId;
//$xeroTenantId = "e23fd416-3b66-43e9-b908-97fbefa24eb8"; // demo company;
//$xeroTenantId = "4e2521ae-83e6-4895-aa90-b20aa0825ce1"; // Acaresydney ;

$ifModifiedSince = date("M d Y H:i:s", strtotime("-30 days"));
$where = "Status==\"ACTIVE\"";
$order = null; // "EmailAddress%20DESC";
$page = 1;
// $result = null;
try {
$result = $api->getEmployees($xeroTenantId,$ifModifiedSince,$where,$order,$page);
$employees = $result->getEmployees();
$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> Phone </td>
<td> Mobile </td>
<td> Start </td>
<td> Group </td>
</tr> ";
$count = 1;
foreach ($employees as $r){

$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->getPhone() . "</td> " .
"<td> " . $r->getMobile() . "</td> " .
"<td> " . $r->getStartDateAsDate()->format("M d Y") . "</td> " .
"<td> " . $r->getEmployeeGroupName() . "</td> " .
"</tr>";
$count ++;
}
$message .= "</table>";
return $message;
} catch (\Exception $e) {
echo 'Exception when calling PayrollAuApi->getPayItems: ', $e->getMessage(), PHP_EOL;
return;
}
}

public function xero_org_clients(){
ini_set('display_errors', 'On');
try {
$contacts = $this->getClients();
$message = "<table> ";
$message .= "<tr> <td> # </td>
<td> Name </td>
<td> Last </td>
<td> Status </td>
<td> Email </td>
<td> AccountNumber </td>
<td> Addresses </td>
</tr> ";
$count = 1;
foreach ($contacts as $r){

$message .= "<tr> <td>" . $count . " - ". $r->getContactID() . "</td> " .
"<td> " . $r->getFirstName() . "</td> " .
"<td> " . $r->getLastName() . "</td> " .
"<td> " . $r->getContactStatus() . "</td> " .
"<td> " . $r->getEmailAddress() . "</td> " .
"<td> " . $r->getAccountNumber() . "</td> " .
"<td> " . $r->getAddresses() . "</td> " .
"</tr>";
$count ++;
}
$message .= "</table>";
return $message;
} catch (\Exception $e) {
echo 'Exception when calling PayrollAuApi->getPayContacts: ', $e->getMessage(), PHP_EOL;
return;
}
}


public function xero_org_payroll_calendar()
{
// update_option('bts_pay_roll_calendar_last_sync', time());
try {
$result = $this->get_payroll_calendar();
$pc = $result->getPayrollCalendars()[0];
$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;

return print_r ($calendar);
}catch (\Exception $e) {
echo 'Exception when calling PayrollAuApi->getPayrollCalendar: ', $e->getMessage(), PHP_EOL;
}
//update_option('bts_pay_roll_calendar', $calendar);
}

//
//TS implementation

+ 271
- 0
XeroOauth2ShortCode.php 查看文件

@@ -0,0 +1,271 @@
<?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->xeroTenantId);
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->xeroTenantId);
$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(){
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 = date("M d Y H:i:s", strtotime("-30 days"));
$where = "Status==\"ACTIVE\"";
$order = null; // "EmailAddress%20DESC";
$page = 1;
// $result = null;
try {
$result = $api->getEmployees($xeroTenantId,$ifModifiedSince,$where,$order,$page);
$employees = $result->getEmployees();
$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> Phone </td>
<td> Mobile </td>
<td> Start </td>
<td> Group </td>
</tr> ";
$count = 1;
foreach ($employees as $r){

$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->getPhone() . "</td> " .
"<td> " . $r->getMobile() . "</td> " .
"<td> " . $r->getStartDateAsDate()->format("M d Y") . "</td> " .
"<td> " . $r->getEmployeeGroupName() . "</td> " .
"</tr>";
$count ++;
}
$message .= "</table>";
return $message;
} catch (\Exception $e) {
echo 'Exception when calling PayrollAuApi->getPayItems: ', $e->getMessage(), PHP_EOL;
return;
}
}

public function xero_org_clients(){
ini_set('display_errors', 'On');
try {
$contacts = $this->oauth2->getClients();
$message = "<table> ";
$message .= "<tr> <td> # </td>
<td> Name </td>
<td> Last </td>
<td> Status </td>
<td> Email </td>
<td> AccountNumber </td>
<td> Addresses </td>
</tr> ";
$count = 1;
foreach ($contacts as $r){

$message .= "<tr> <td>" . $count . " - ". $r->getContactID() . "</td> " .
"<td> " . $r->getFirstName() . "</td> " .
"<td> " . $r->getLastName() . "</td> " .
"<td> " . $r->getContactStatus() . "</td> " .
"<td> " . $r->getEmailAddress() . "</td> " .
"<td> " . $r->getAccountNumber() . "</td> " .
"<td> " . $r->getAddresses() . "</td> " .
"</tr>";
$count ++;
}
$message .= "</table>";
return $message;
} catch (\Exception $e) {
echo 'Exception when calling PayrollAuApi->getPayContacts: ', $e->getMessage(), PHP_EOL;
return;
}
}


public function xero_org_payroll_calendar()
{
update_option('bts_pay_roll_calendar_last_sync', time());
try {
$result = $this->oauth2->get_payroll_calendar();
$pc = $result->getPayrollCalendars()[0];
$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;

return print_r ($calendar);
}catch (\Exception $e) {
echo 'Exception when calling PayrollAuApi->getPayrollCalendar: ', $e->getMessage(), PHP_EOL;
}
update_option('bts_pay_roll_calendar', $calendar);
}

}

Loading…
取消
儲存