From 91fa7508f5e309a78a3a0491e578d7250850029f Mon Sep 17 00:00:00 2001 From: "Luis F. Gonzalez" Date: Mon, 29 Jul 2019 15:47:09 -0300 Subject: [PATCH] feat: Add public method to get facade and get IPP Instance to Batch Sync --- composer.json | 2 +- readme.md | 2 + src/QuickBookable.php | 2 - src/QuickBooksAuthenticator.php | 2 +- src/QuickBooksResource.php | 19 ++--- src/SyncsToQuickBooks.php | 119 ++++++++++++++++++++++++++------ 6 files changed, 111 insertions(+), 35 deletions(-) diff --git a/composer.json b/composer.json index 488e46e..e1dc8b1 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/readme.md b/readme.md index dac0c48..325081e 100644 --- a/readme.md +++ b/readme.md @@ -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: diff --git a/src/QuickBookable.php b/src/QuickBookable.php index be34b9f..aee28c3 100644 --- a/src/QuickBookable.php +++ b/src/QuickBookable.php @@ -6,7 +6,5 @@ interface QuickBookable { public function getQuickBooksIdAttribute(); - public function getQuickBooksArray(); - public function syncToQuickbooks(); } \ No newline at end of file diff --git a/src/QuickBooksAuthenticator.php b/src/QuickBooksAuthenticator.php index 5f3d133..84a0c16 100644 --- a/src/QuickBooksAuthenticator.php +++ b/src/QuickBooksAuthenticator.php @@ -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; } diff --git a/src/QuickBooksResource.php b/src/QuickBooksResource.php index 624cd3c..5dfda46 100644 --- a/src/QuickBooksResource.php +++ b/src/QuickBooksResource.php @@ -55,10 +55,11 @@ 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); @@ -66,16 +67,18 @@ public function create($attributes) 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); @@ -89,7 +92,7 @@ public function update($id, $attributes) return false; } - return $response->Id; + return $returnObject ? $response : $response->Id; } /** @@ -182,7 +185,7 @@ protected function request($method, ...$params) * * @return string */ - protected function getResourceFacade() + public function getResourceFacade() { return $this->facade; } @@ -192,7 +195,7 @@ protected function getResourceFacade() * * @return string */ - protected function getResourceName() + public function getResourceName() { return $this->name; } diff --git a/src/SyncsToQuickBooks.php b/src/SyncsToQuickBooks.php index 8e536b1..03d1436 100644 --- a/src/SyncsToQuickBooks.php +++ b/src/SyncsToQuickBooks.php @@ -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. * @@ -17,6 +23,24 @@ 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. * @@ -24,15 +48,40 @@ abstract protected function getQuickBooksArray(): array; */ 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 */ @@ -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(); } @@ -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; + } }