Skip to content

Commit

Permalink
feat: Add public method to get facade and get IPP Instance to Batch Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis F. Gonzalez committed Nov 8, 2019
1 parent 520845b commit b583a46
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 46 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"keywords": ["Laravel", "laravel-quickbooks"],
"require": {
"illuminate/support": "~5",
"quickbooks/v3-php-sdk": "^5.0.3"
"quickbooks/v3-php-sdk": "5.0.5"
},
"require-dev": {
"phpunit/phpunit": "~6.0",
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ Then you have to define:
* `quickBooksResource` - One of the QuickBooks resources classes (e.g.. `\LifeOnScreen\LaravelQuickBooks\Resources\Company::class`).
* `getQuickBooksArray()` - This method must return the associative array which will be synced to QuickBooks.
* `quickBooksIdColumn` (optional) - The column to use for storing the QuickBooks ID (defaults to `quickbooks_id`)
* `quickBooksSyncTokenColumn` (optional) - The column to use for storing the QuickBooks SyncToken (defaults to `sync_token`)
* `returnObject` (optional) - The property to define if INSERT and UPDATE Operation return the QuickBooks Object or a boolean (defaults to `false`)

Usage example:

Expand Down
33 changes: 33 additions & 0 deletions src/Facades/PaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace LifeOnScreen\LaravelQuickBooks\Facades;

use QuickBooksOnline\API\Facades\FacadeHelper;

/**
* Provisional Facade while is incorporated in QuickBooksOnline Package
* Class PaymentMethod
* @package LifeOnScreen\LaravelQuickBooks\Facades
*/
class PaymentMethod{

public static function create(array $data, $throwException = TRUE){
if(!isset($data) || empty($data)) throw new \Exception("Passed array for creating PaymentMethod is Empty");
$PaymentObject = FacadeHelper::reflectArrayToObject("PaymentMethod", $data, $throwException );
return $PaymentObject;
}

/**
* This is an immutable function
*/
public static function update($objToUpdate, array $data){
$classOfObj = get_class($objToUpdate);
if(strcmp($classOfObj, FacadeHelper::simpleAppendClassNameSpace("PaymentMethod")) != 0){
throw new \Exception("Target object class:{" . $classOfObj . "} is not an instance of PaymentMethod.");
}
$newPaymentObj = Payment::create($data);
$clonedOfObj = FacadeHelper::cloneObj($objToUpdate);
FacadeHelper::mergeObj($clonedOfObj, $newPaymentObj);
return $clonedOfObj;
}

}
2 changes: 0 additions & 2 deletions src/QuickBookable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ interface QuickBookable
{
public function getQuickBooksIdAttribute();

public function getQuickBooksArray();

public function syncToQuickbooks();
}
20 changes: 9 additions & 11 deletions src/QuickBooksAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function processHook(): bool
$realmID = Request::get('realmId');
$requestCode = Request::get('code');

if (empty($realmID) || empty($requestCode) || !self::cookieIsValid()) {
if (empty($realmID) || empty($requestCode) || !static::cookieIsValid()) {
return false;
}

Expand Down Expand Up @@ -94,16 +94,9 @@ public static function revokeAccess()
* Get QuickBooksOnline\API\DataService\DataService object.
* @throws \QuickBooksOnline\API\Exception\SdkException
*/
protected static function getDataService(): DataService
protected static function getDataService()
{
return DataService::Configure([
'auth_mode' => config('quickbooks.data-service.auth-mode'),
'ClientID' => config('quickbooks.data-service.client-id'),
'ClientSecret' => config('quickbooks.data-service.client-secret'),
'RedirectURI' => config('quickbooks.data-service.redirect-uri'),
'scope' => config('quickbooks.data-service.scope'),
'baseUrl' => config('quickbooks.data-service.base-url')
]);
return static::getConnection()->getDataService();
}

/**
Expand Down Expand Up @@ -135,14 +128,19 @@ public static function validConnectionExists(): bool
}

try {
App::make(QuickBooksConnection::class);
static::getConnection();
return true;
}
catch (Exception $e) {
return false;
}
}

protected static function getConnection()
{
return App::make('QuickBooksConnection');
}

/**
* Checks if the cookie is valid.
* @return bool
Expand Down
21 changes: 12 additions & 9 deletions src/QuickBooksResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,37 @@ public function __construct(array $resource = null)
$this->name = $resource['name'];
}

$this->connection = App::make(QuickBooksConnection::class);
$this->connection = App::make('QuickBooksConnection');
}

/**
* Create a resource
*
* @param array $attributes
* @param bool $returnObject This parameter manages if this method return An object or an Id
* @return bool|int
* @throws \QuickBooksOnline\API\Exception\IdsException
*/
public function create($attributes)
public function create($attributes, bool $returnObject = false)
{
$object = $this->getResourceFacade()::create($attributes);

if (!$response = $this->request('Add', $object)) {
return false;
}

return (int) $response->Id;
return $returnObject ? $response : (int) $response->Id;
}

/**
* Update a resource.
*
* @param $where
* @param $id
* @param $attributes
* @param bool $returnObject
* @return bool
* @throws \Exception
*/
public function update($id, $attributes)
public function update($id, $attributes, bool $returnObject = false)
{
$resource = $this->find($id);

Expand All @@ -89,7 +92,7 @@ public function update($id, $attributes)
return false;
}

return $response->Id;
return $returnObject ? $response : $response->Id;
}

/**
Expand Down Expand Up @@ -182,7 +185,7 @@ protected function request($method, ...$params)
*
* @return string
*/
protected function getResourceFacade()
public function getResourceFacade()
{
return $this->facade;
}
Expand All @@ -192,7 +195,7 @@ protected function getResourceFacade()
*
* @return string
*/
protected function getResourceName()
public function getResourceName()
{
return $this->name;
}
Expand Down
23 changes: 23 additions & 0 deletions src/Resources/PaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace LifeOnScreen\LaravelQuickBooks\Resources;

use LifeOnScreen\LaravelQuickBooks\QuickBooksResource;
use LifeOnScreen\LaravelQuickBooks\Facades as QuickBooksFacades;

class PaymentMethod extends QuickBooksResource
{
/**
* The name of this resource.
*
* @var string
*/
protected $name = 'PaymentMethod';

/**
* QuickBooks Online API Facade
*
* @var string
*/
protected $facade = QuickBooksFacades\PaymentMethod::class;
}
119 changes: 96 additions & 23 deletions src/SyncsToQuickBooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ trait SyncsToQuickBooks
*/
protected $quickBooksResourceInstance;

/**
* This variable magane if each operation (Insert | Update) return an Object or an Id
* @var bool
*/
protected static $returnObject = false;

/**
* The data to sync to QuickBooks.
*
Expand All @@ -17,22 +23,65 @@ trait SyncsToQuickBooks
*/
abstract protected function getQuickBooksArray(): array;

/**
* Method to get QuickBooksAttributeIdName
* @return string
*/
public function getQuickBooksAttributeIdName()
{
return $this->quickBooksIdColumn ?? 'quickbooks_id';
}

/**
* Method to get QuickBooksAttributeSyncTokenName
* @return string
*/
public function getQuickBooksAttributeSyncTokenName()
{
return $this->quickBooksSyncTokenColumn ?? 'sync_token';
}

/**
* Allows you to use `$model->quickbooks_id` regardless of the actual column being used.
*
* @return null|string
*/
public function getQuickBooksIdAttribute(): ?string
{
$quickBooksIdColumn = $this->quickBooksIdColumn ?? 'quickbooks_id';

if (isset($this->attributes[$quickBooksIdColumn])) {
return $this->attributes[$quickBooksIdColumn];
if (isset($this->attributes[$this->getQuickBooksAttributeIdName()])) {
return $this->attributes[$this->getQuickBooksAttributeIdName()];
}
return null;
}

/**
* Allows you to use `$model->sync_token` regardless of the actual column being used.
*
* @return null|int
*/
public function getQuickBooksSyncTokenAttribute(): ?int
{
if (isset($this->attributes[$this->getQuickBooksAttributeSyncTokenName()])) {
return $this->attributes[$this->getQuickBooksAttributeSyncTokenName()];
}
return null;
}

/**
* Allows you to save `$model->quickbooks_id`.
* @param $value
* @return boolean
*/
protected function saveQuickBooksIdAttribute($Id, $SyncToken): bool
{
$this->{$this->getQuickBooksAttributeIdName()} = $Id;
if(static::$returnObject && !is_null($SyncToken)) {
$this->{$this->getQuickBooksAttributeSyncTokenName()} = $SyncToken;
}

return $this->save();
}

/**
* @return null|\QuickBooksOnline\API\Core\HttpClients\FaultHandler
*/
Expand All @@ -43,52 +92,53 @@ public function getLastQuickBooksError()

/**
* Creates the model in QuickBooks.
*
* @return bool
* @throws \QuickBooksOnline\API\Exception\IdsException
* @return mixed
* @throws \Exception
*/
public function insertToQuickBooks(): bool
public function insertToQuickBooks()
{
$attributes = $this->getQuickBooksArray();
$resourceId = $this->getQuickBooksResourceInstance()->create($attributes);
$resource = $this->getQuickBooksResourceInstance()->create($attributes, static::$returnObject);

if (!$resourceId) {
if (!$resource) {
return false;
}

$this->quickbooks_id = $resourceId;
$this->save();
$this->saveQuickBooksIdAttribute($resource->Id, $resource->SyncToken);

return true;
return static::$returnObject && is_object($resource) ? $resource : true;
}

/**
* Updates the model in QuickBooks.
*
* @return bool
* @return mixed
* @throws \QuickBooksOnline\API\Exception\IdsException
* @throws \Exception
*/
public function updateToQuickBooks(): bool
public function updateToQuickBooks()
{
if (empty($this->quickbooks_id)) {
if (empty($this->getQuickBooksIdAttribute())) {
return false;
}

$attributes = $this->getQuickBooksArray();
$dataSync = $this->getQuickBooksResourceInstance()->update($this->getQuickBooksIdAttribute(), $attributes, static::$returnObject);
if($dataSync && static::$returnObject){
$this->saveQuickBooksIdAttribute($dataSync->Id, $dataSync->SyncToken);
}

return $this->getQuickBooksResourceInstance()->update($this->quickbooks_id, $attributes);
return $dataSync;
}

/**
* Syncs the model to QuickBooks.
*
* @return bool
* @throws \QuickBooksOnline\API\Exception\IdsException
* Syncs the model to QuickBooks
* @return bool|mixed
* @throws \Exception
*/
public function syncToQuickBooks(): bool
public function syncToQuickBooks()
{
if (empty($this->quickbooks_id)) {
if (empty($this->getQuickBooksIdAttribute())) {
return $this->insertToQuickBooks();
}

Expand Down Expand Up @@ -124,4 +174,27 @@ protected function getQuickBooksResourceInstance()

return $this->quickBooksResourceInstance;
}

/**
* Method to get the Resource Name
* @return mixed
* @throws \Exception
*/
public function getResourceName()
{
return $this->getQuickBooksResourceInstance()->getResourceName();
}

/**
* Method to get an instance to a synchronize by Batch
* @param $data
* @return mixed
* @throws \Exception
*/
public function getQuickBooksInstanceToBatch(array $data = null)
{
$facade = $this->getQuickBooksResourceInstance()->getResourceFacade();
$object = $facade::create($data ?? $this->getQuickBooksArray());
return $object;
}
}

0 comments on commit b583a46

Please sign in to comment.