Skip to content

Instantly share code, notes, and snippets.

Created November 28, 2013 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/7697738 to your computer and use it in GitHub Desktop.
Save anonymous/7697738 to your computer and use it in GitHub Desktop.
<?php
namespace Dash\ApplicationBundle\ServiceAdapter;
use Vespolina\Entity\Invoice\Item;
use Vespolina\Entity\Partner\Partner;
use Vespolina\Sync\ServiceAdapter\AbstractServiceAdapter;
use Vespolina\Sync\Entity\EntityData;
/**
* Interface to determine and handle dependencies for a given entity type
*
* @author Daniel Kucharski <daniel@vespolina.org>
*/
class ZohoInvoiceServiceAdapter extends AbstractServiceAdapter
{
protected $apiCount;
/**
* Setupt the Zoho service adapter
*
* @param array $config
* @param array $logger
* @param null $entityManagers
*/
public function __construct($config, $logger, $entityManagers)
{
foreach ($entityManagers as $name => $manager) {
$this->{$name . 'Manager'} = $manager;
}
// This adapter will handle customer and invoice entity type
parent::__construct(array('customer', 'invoice'), $config, $logger);
}
/**
* Dynamic calls to fetchEntity<name of the entity>
*
* @param $entityName
* @param $remoteId
* @return EntityData
*/
public function fetchEntity($entityName, $remoteId)
{
$method = 'fetchEntity' . ucfirst($entityName);
return $this->{$method}($entityName, $remoteId);
}
/**
* Retrieve the remote customer information and dump all data into EntityData.
*
* Later on the EntityData will be converted to the real (local) customer instance
*
* @param $entityName
* @param $remoteId
* @return EntityData
*/
protected function fetchEntityCustomer($entityName, $remoteId)
{
$xml = $this->queryZohoApi('customers/' . $remoteId);
return new EntityData('customer', $remoteId, $xml->Customer);
}
/**
* Fetch a collection of remote entities, starting from the last value know
* ( eg. last updated at value )
*
* @param $entityName
* @param $lastValue
* @param \Vespolina\Sync\ServiceAdapter\Max $packageSize
* @return array
*/
public function fetchEntities($entityName, $lastValue, $packageSize)
{
$entitiesData = array();
if ($entityName !== 'invoice') {
return $entitiesData;
}
$xml = $this->queryZohoApi('invoices');
foreach ($xml->Invoices->Invoice as $invoiceXml) {
$zohoInvoiceId = (string)$invoiceXml->InvoiceID;
$invoiceDetailXml = $this->queryZohoApi('invoices/' . $zohoInvoiceId);
//Store the entity data and requests to resolve dependencies
$e = new EntityData('invoice', $zohoInvoiceId, $invoiceDetailXml);
$this->logger->info('Discovered invoice ' . $zohoInvoiceId);
//Add any dependency so they can be resolved when the time is right
$e->addDependency('customer', $invoiceDetailXml->Invoice->CustomerID);
$entitiesData[] = $e;
}
return $entitiesData;
}
/**
* Transform the entity data instance into the real local entity
*
* @param EntityData $entityData
* @return mixed
*/
public function transformEntityData(EntityData $entityData)
{
return $this->{'transform' . ucfirst($entityData->getEntityName() . 'Data')}($entityData);
}
protected function transformCustomerData(EntityData $entityData)
{
$customer = $this->partnerManager->createPartner(Partner::ROLE_CUSTOMER, Partner::ORGANISATION);
$xmlData = $entityData->getData();
$customer->setName($xmlData->Name);
$this->partnerManager->updatePartner($customer);
return $customer;
}
protected function transformInvoiceData(EntityData $entityData)
{
//Use entity data to create the actual entity
$xmlData = $entityData->getData()->Invoice;
$invoice = $this->invoiceManager->createInvoice();
//Get the resolved dependency (local entity)
$customer = $entityData->getDependencyReference('customer');
$invoice->setCustomer($customer);
$invoice->setInvoiceNumber($xmlData->InvoiceNumber);
$invoice->setDueDate(\DateTime::createFromFormat('Y-m-d', $xmlData->DueDate));
$invoice->setIssuedDate(\DateTime::createFromFormat('Y-m-d', $xmlData->InvoiceDate));
$invoice->setReference($xmlData->PONumber);
$invoice->setFiscalYear(2013);
foreach ($xmlData->InvoiceItems as $xmlInvoiceItem) {
$xmlInvoiceItemNode = $xmlInvoiceItem->InvoiceItem;
//var_dump($xmlInvoiceItem);die();
//$zohoProductId = (string)$xmlInvoiceItem->ProductID;
$invoiceItem = new Item();
$invoiceItem->setDescription((string)$xmlInvoiceItemNode->ItemDescription);
$invoiceItem->setName((string)$xmlInvoiceItemNode->ItemName);
//$invoiceItem->setQuantity((string)$xmlInvoiceItem->ItemDescription);
$price = (string)$xmlInvoiceItemNode->Price;
$quantity = (string)$xmlInvoiceItemNode->Quantity;
$taxPercentage = (string)$xmlInvoiceItemNode->Tax1Percentage;
$itemTotal = (string)$xmlInvoiceItemNode->ItemTotal;
$invoice->addItem($invoiceItem);
}
$this->invoiceManager->updateInvoice($invoice);
return $invoice;
}
public function getApiCount()
{
return $this->apiCount;
}
protected function queryZohoApi($path)
{
$uri = 'https://invoice.zoho.com/api/' . $path . '?authtoken=' . $this->config['zoho_auth_token'] . '&scope=invoiceapi&apikey=' . $this->config['zoho_api_key'];
//$uri .= '&Per_Page=2';
$xml = simplexml_load_file($uri);
if (null == $xml) {
$this->logger->err('Could not connect to ZOHO api');
}
$this->apiCount++;
return $xml;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment