From b583a46c43eb86fc873bc9b665cb4f6b529bddf9 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/Facades/PaymentMethod.php | 33 +++++++++ src/QuickBookable.php | 2 - src/QuickBooksAuthenticator.php | 20 +++--- src/QuickBooksResource.php | 21 +++--- src/Resources/PaymentMethod.php | 23 ++++++ src/SyncsToQuickBooks.php | 119 ++++++++++++++++++++++++++------ 8 files changed, 176 insertions(+), 46 deletions(-) create mode 100644 src/Facades/PaymentMethod.php create mode 100644 src/Resources/PaymentMethod.php 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/Facades/PaymentMethod.php b/src/Facades/PaymentMethod.php new file mode 100644 index 0000000..24116d6 --- /dev/null +++ b/src/Facades/PaymentMethod.php @@ -0,0 +1,33 @@ + 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(); } /** @@ -135,7 +128,7 @@ public static function validConnectionExists(): bool } try { - App::make(QuickBooksConnection::class); + static::getConnection(); return true; } catch (Exception $e) { @@ -143,6 +136,11 @@ public static function validConnectionExists(): bool } } + protected static function getConnection() + { + return App::make('QuickBooksConnection'); + } + /** * Checks if the cookie is valid. * @return bool diff --git a/src/QuickBooksResource.php b/src/QuickBooksResource.php index 624cd3c..d1b7ba7 100644 --- a/src/QuickBooksResource.php +++ b/src/QuickBooksResource.php @@ -48,17 +48,18 @@ 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); @@ -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/Resources/PaymentMethod.php b/src/Resources/PaymentMethod.php new file mode 100644 index 0000000..97b74db --- /dev/null +++ b/src/Resources/PaymentMethod.php @@ -0,0 +1,23 @@ +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 && !is_null($SyncToken)) { + $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) { 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; + } }