Created
October 24, 2019 21:56
-
-
Save DorsetDigital/dd7ddc7433592496fe5f365db7dace2d to your computer and use it in GitHub Desktop.
Xero
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Helpers; | |
use SilverStripe\Control\Director; | |
use SilverStripe\Security\Member; | |
use XeroPHP\Application\PrivateApplication; | |
use XeroPHP\Models\Accounting\Contact; | |
use XeroPHP\Models\Accounting\Invoice; | |
use App\DataObjects\ShopOrder; | |
use SilverStripe\Core\Environment; | |
use XeroPHP\Models\Accounting\Address; | |
use XeroPHP\Models\Accounting\TaxType; | |
class XeroHelper | |
{ | |
private $xero; | |
private $salesAccount; | |
private $shippingAccount; | |
public function __construct() | |
{ | |
$key = Environment::getEnv('XERO_CONS_KEY'); | |
$secret = Environment::getEnv('XERO_CONS_SECRET'); | |
$keyfile = Environment::getEnv('XERO_PRIVATE_KEY'); | |
$this->salesAccount = Environment::getEnv('XERO_SALES_ACCOUNT'); | |
$this->shippingAccount = Environment::getEnv('XERO_SHIPPING_ACCOUNT'); | |
$keypath = Director::baseFolder() . DIRECTORY_SEPARATOR . $keyfile; | |
if (($key == "") || ($secret == "") || (!is_readable($keypath))) { | |
throw new \Exception('Missing information from Xero configuration'); | |
} else { | |
$config = [ | |
'oauth' => [ | |
'callback' => Director::baseURL(), | |
'consumer_key' => $key, | |
'consumer_secret' => $secret, | |
'rsa_private_key' => 'file://' . $keypath | |
] | |
]; | |
$this->xero = new PrivateApplication($config); | |
} | |
} | |
public function addMemberToXero($memberid, $orderid = 0) | |
{ | |
$member = Member::get()->byID($memberid); | |
if ($member) { | |
if ($member->XeroUID == "") { | |
$contact = new Contact($this->xero); | |
$contact->setEmailAddress($member->Email) | |
->setFirstName($member->FirstName) | |
->setLastName($member->Surname) | |
->setName($member->FirstName . " " . $member->Surname . " - Web " . $member->ID) | |
; | |
$contact->save(); | |
$member->XeroUID = $contact->getContactID(); | |
$member->write(); | |
} else { | |
$contact = $this->xero->loadByGUID('Accounting\\Contact', $member->XeroUID); | |
} | |
if ($orderid > 0) { | |
$order = ShopOrder::get()->byID($orderid); | |
if ($order) { | |
$address = new Address($this->xero); | |
if ($order->BillingCompany == "") { | |
$address1 = $order->BillingCompany; | |
$address2 = $order->Address1; | |
$address3 = $order->Address2; | |
} else { | |
$address1 = $order->Address1; | |
$address2 = $order->Address2; | |
$address3 = ''; | |
} | |
$address->setAddressType(Address::ADDRESS_TYPE_STREET) | |
->setAddressLine1($address1) | |
->setAddressLine2($address2) | |
->setAddressLine3($address3) | |
->setCity($order->BillingCity) | |
->setPostalCode($order->BillingPostcode) | |
->setCountry($order->BillingCountry()->CountryName) | |
; | |
$contact->addAddress($address); | |
$contact->save(); | |
} | |
} | |
return $contact; | |
} | |
return false; | |
} | |
public function addInvoiceToXero($orderid, $contact) | |
{ | |
$order = ShopOrder::get()->byID($orderid); | |
if ($order) { | |
$invoice = new Invoice($this->xero); | |
$now = new \DateTime($order->Created); | |
$invoice->setType(Invoice::INVOICE_TYPE_ACCREC) | |
->setContact($contact) | |
->setCurrencyCode('GBP') | |
->setReference('Web' . $orderid) | |
->setStatus(Invoice::INVOICE_STATUS_AUTHORISED) | |
->setDate($now) | |
; | |
$now->modify('+10 days'); | |
$invoice->setDueDate($now); | |
foreach ($order->Items() as $orderItem) { | |
$item = new Invoice\LineItem(); | |
$item->setDescription($orderItem->ItemName) | |
->setQuantity($orderItem->Qty) | |
->setUnitAmount($orderItem->Price) | |
->setLineAmount($orderItem->Qty * $orderItem->Price) | |
->setAccountCode($this->salesAccount) | |
; | |
if ($order->ShippingCountry()->ChargeVAT == 0) { | |
$item->setTaxType(TaxType::UNITED_KINGDOM_NONE); | |
} | |
$invoice->addLineItem($item); | |
} | |
$item = new Invoice\LineItem(); | |
$item->setDescription('Shipping') | |
->setQuantity(1) | |
->setUnitAmount($order->Shipping) | |
->setLineAmount($order->Shipping) | |
->setAccountCode($this->shippingAccount) | |
; | |
if ($order->ShippingCountry()->ChargeVAT == 0) { | |
$item->setTaxType(TaxType::UNITED_KINGDOM_NONE); | |
} | |
$invoice->addLineItem($item); | |
$item = new Invoice\LineItem(); | |
$item->setDescription('Paid by ' . $order->Payments()->first()->Gateway); | |
$invoice->addLineItem($item); | |
$invoice->save(); | |
$order->XeroUID = $invoice->getInvoiceID(); | |
$order->write(); | |
return $invoice; | |
} | |
return false; | |
} | |
public function getDebugInfo() { | |
return [ | |
'Sales:' => $this->salesAccount, | |
'Shipping:' => $this->shippingAccount | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment