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 Aug 2, 2019
1 parent 520845b commit 08d37cc
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 34 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
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();
}
19 changes: 11 additions & 8 deletions src/QuickBooksResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,30 @@ public function __construct(array $resource = null)
* 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
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) {
$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->Id) {
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 08d37cc

Please sign in to comment.