From 1567806b87b27b588545a3a630cffc575ef07141 Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 18 Aug 2022 23:18:14 +1000 Subject: [PATCH] Implemented more customer functions --- src/API.php | 51 ++++++++-- src/Jobs/AccountingCustomerUpdate.php | 77 +++++++++++++++ src/Response/Base.php | 106 +-------------------- src/Response/Customer.php | 25 +++++ src/Response/ListList.php | 129 +++++++++++++++++++++++++- 5 files changed, 278 insertions(+), 110 deletions(-) create mode 100644 src/Jobs/AccountingCustomerUpdate.php create mode 100644 src/Response/Customer.php diff --git a/src/API.php b/src/API.php index c5a4b90..377e05c 100644 --- a/src/API.php +++ b/src/API.php @@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; use Intuit\Models\ProviderToken; -use Intuit\Response\ListList; +use Intuit\Response\{Customer,ListList}; final class API { @@ -77,19 +77,19 @@ final class API ); break; - /* case 'POST': $request = $this->prepareRequestPost( $url, $parameters, [ - 'accept: application/json', - 'Api-Request-Id: '.$request_id, - 'Api-Signature: '.$signature, + 'Accept' => 'application/json', + 'Authorization' => sprintf('Bearer %s',$this->token->access_token), + 'Content-Type' => 'application/json', ] ); break; + /* case 'PUT': $request = $this->prepareRequestPut( $url, @@ -139,6 +139,21 @@ final class API return $result; } + /** + * Get a specific customer record + * + * @param int $id + * @param array $parameters + * @return Customer + * @throws \Exception + */ + public function getCustomer(int $id,array $parameters=[]): Customer + { + Log::debug(sprintf('%s:Get a specific customer [%d]',static::LOGKEY,$id)); + + return new Customer($this->execute('customer/'.$id,$parameters)); + } + /** * Get a list of our clients * @@ -165,7 +180,7 @@ final class API * @param array $headers * @return \CurlHandle */ - private function prepareRequest($url,array $parameters=[],array $headers=[]): \CurlHandle + private function prepareRequest(string $url,array $parameters=[],array $headers=[]): \CurlHandle { $request = curl_init(); @@ -184,4 +199,28 @@ final class API return $request; } + + private function prepareRequestPost(string $url,array $parameters=[],array $headers=[]): \CurlHandle + { + $request = $this->prepareRequest($url,$parameters,$headers); + + curl_setopt($request,CURLOPT_CUSTOMREQUEST,'POST'); + curl_setopt($request,CURLOPT_POSTFIELDS,json_encode($parameters)); + + return $request; + } + + /** + * Update a customer + * + * @param array $parameters + * @return Customer + * @throws \Exception + */ + public function updateCustomer(array $parameters=[]): Customer + { + Log::debug(sprintf('%s:Update customer',static::LOGKEY),['params'=>$parameters]); + + return new Customer($this->execute('customer',array_merge($parameters,['method'=>'POST']))); + } } \ No newline at end of file diff --git a/src/Jobs/AccountingCustomerUpdate.php b/src/Jobs/AccountingCustomerUpdate.php new file mode 100644 index 0000000..1431af7 --- /dev/null +++ b/src/Jobs/AccountingCustomerUpdate.php @@ -0,0 +1,77 @@ +onQueue('intuit'); + + $this->customer = $customer; + $this->to = $to->withoutRelations(); + $this->forceprod = $forcprod; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + Config::set('site',$this->to->site); + + $api = $this->to->provider->API($this->to,$this->forceprod); + $updated = $api->updateCustomer(array_merge( + $this->customer->getDirty(), + [ + 'Id'=>$this->customer->id, + 'SyncToken'=>$this->customer->synctoken, + ])); + + if (($x=$this->to->provider->accounts->where('pivot.ref',$updated->id))->count() === 1) { + $ao = $x->pop(); + + $ao->providers()->syncWithoutDetaching([ + $this->to->provider->id => [ + 'ref' => $updated->id, + 'synctoken' => $updated->synctoken, + 'created_at'=>Carbon::create($updated->created_at), + 'updated_at'=>Carbon::create($updated->updated_at), + 'site_id'=>$ao->site_id, // @todo See if we can have this handled automatically + ], + ]); + + Log::info(sprintf('%s:Updated account [%s] (%s:%s), synctoken now [%s]',self::LOGKEY,$ao->sid,$updated->id,$updated->DisplayName,$updated->synctoken)); + + } else { + Log::error(sprintf('%s:Unable to update account refer for [%s:%s]',self::LOGKEY,$updated->id,$updated->DisplayName)); + } + } +} diff --git a/src/Response/Base.php b/src/Response/Base.php index c6d499d..fd656a0 100644 --- a/src/Response/Base.php +++ b/src/Response/Base.php @@ -5,6 +5,7 @@ namespace Intuit\Response; use Illuminate\Support\Collection; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Log; +use Jenssegers\Model\Model; use Intuit\Models\Customer; @@ -13,37 +14,20 @@ use Intuit\Models\Customer; * * @note: This class is used for events not specifically created. */ -abstract class Base implements \JsonSerializable, \Countable, \ArrayAccess, \Iterator +abstract class Base implements \JsonSerializable { protected const LOGKEY = 'RB-'; - protected Collection $_data; - protected ?string $_type; - protected int $startPosition; - protected int $maxResults; - - private ?int $counter = NULL; - protected const TYPES = [ - 'customers', - ]; + protected Model $_model; /** * Default Constructor Setup * * @param object $response - * @param string $type * @throws \Exception */ - public function __construct(object $response,string $type) + public function __construct(object $response) { - if (! in_array($type,self::TYPES)) - throw new \Exception('Unknown data type: '.$type); - - if (object_get($response,'time')) - unset($response->time); - - $this->_data = $this->data($response,$type); - // This is only for child classes if (get_class($this) == Base::class) { Log::debug(sprintf('%s:Intuit RESPONSE Initialised [%s]',static::LOGKEY,get_class($this)),['m'=>__METHOD__]); @@ -63,88 +47,6 @@ abstract class Base implements \JsonSerializable, \Countable, \ArrayAccess, \Ite return $this->_data ?: new \stdClass; } - public function current(): mixed - { - return $this->_data[$this->counter]; - } - - public function next(): void - { - $this->counter++; - } - - public function key(): mixed - { - return $this->counter; - } - - public function valid(): bool - { - return isset($this->_data[$this->counter]); - } - - public function rewind(): void - { - $this->counter = 0; - } - - public function offsetExists(mixed $offset): bool - { - return $this->_data->has($offset); - } - - public function offsetGet(mixed $offset): mixed - { - return $this->_data->get($offset); - } - - public function offsetSet(mixed $offset, mixed $value): void - { - throw new \Exception('Method not implemented: '.__METHOD__); - } - - public function offsetUnset(mixed $offset): void - { - $this->_data->forget($offset); - - // Rekey the collection - $this->_data = $this->_data->values(); - - // Reset the counter if we have deleted a value before it - if ($offset < $this->counter) - $this->counter--; - } - - public function count(): int - { - return $this->_data->count(); - } - - /* METHODS */ - - /** - * Convert our response into a collection of the appropriate model - * - * @param object $response - * @param string $type - * @return Collection - * @throws \Exception - */ - private function data(object $response,string $type): Collection - { - switch ($type) { - case 'customers': - $data = collect(Customer::hydrate($response->QueryResponse->Customer)); - $this->startPosition = $response->QueryResponse->startPosition; - $this->maxResults = $response->QueryResponse->maxResults; - break; - - default: throw new \Exception('Unknown object type: '.$this->_type); - } - - return $data; - } - /** * Search for an item in the result * diff --git a/src/Response/Customer.php b/src/Response/Customer.php new file mode 100644 index 0000000..80bc82d --- /dev/null +++ b/src/Response/Customer.php @@ -0,0 +1,25 @@ +time); + + $this->_model = new \Intuit\Models\Customer((array)$response->Customer); + } + + public function __get($key) { + return $this->_model->__get($key); + } +} \ No newline at end of file diff --git a/src/Response/ListList.php b/src/Response/ListList.php index 7fdd618..a89755a 100644 --- a/src/Response/ListList.php +++ b/src/Response/ListList.php @@ -2,10 +2,135 @@ namespace Intuit\Response; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\App; +use Illuminate\Support\Facades\Log; +use Intuit\Models\Customer; + /** - * This is a Generic Intuit Response to API calls + * This is a Generic Intuit Response to API calls that produces a list of objects */ -class ListList extends Base +class ListList extends Base implements \Countable, \ArrayAccess, \Iterator { protected const LOGKEY = 'RLI'; + + protected Collection $_data; + protected ?string $_type; + protected int $startPosition; + protected int $maxResults; + + private ?int $counter = NULL; + protected const TYPES = [ + 'customers', + ]; + + /** + * Default Constructor Setup + * + * @param object $response + * @param string $type + * @throws \Exception + */ + public function __construct(object $response,string $type) + { + parent::__construct($response); + + if (! in_array($type,self::TYPES)) + throw new \Exception('Unknown data type: '.$type); + + if (object_get($response,'time')) + unset($response->time); + + $this->_data = $this->data($response,$type); + + // This is only for child classes + if (get_class($this) == Base::class) { + Log::debug(sprintf('%s:Intuit RESPONSE Initialised [%s]',static::LOGKEY,get_class($this)),['m'=>__METHOD__]); + + if (App::environment() == 'dev') + file_put_contents('/tmp/response',print_r($this,TRUE),FILE_APPEND); + } + } + + public function current(): mixed + { + return $this->_data[$this->counter]; + } + + public function next(): void + { + $this->counter++; + } + + public function key(): mixed + { + return $this->counter; + } + + public function valid(): bool + { + return isset($this->_data[$this->counter]); + } + + public function rewind(): void + { + $this->counter = 0; + } + + public function offsetExists(mixed $offset): bool + { + return $this->_data->has($offset); + } + + public function offsetGet(mixed $offset): mixed + { + return $this->_data->get($offset); + } + + public function offsetSet(mixed $offset, mixed $value): void + { + throw new \Exception('Method not implemented: '.__METHOD__); + } + + public function offsetUnset(mixed $offset): void + { + $this->_data->forget($offset); + + // Rekey the collection + $this->_data = $this->_data->values(); + + // Reset the counter if we have deleted a value before it + if ($offset < $this->counter) + $this->counter--; + } + + public function count(): int + { + return $this->_data->count(); + } + + /* METHODS */ + + /** + * Convert our response into a collection of the appropriate model + * + * @param object $response + * @param string $type + * @return Collection + * @throws \Exception + */ + private function data(object $response,string $type): Collection + { + switch ($type) { + case 'customers': + $data = collect(Customer::hydrate($response->QueryResponse->Customer)); + $this->startPosition = $response->QueryResponse->startPosition; + $this->maxResults = $response->QueryResponse->maxResults; + break; + + default: throw new \Exception('Unknown object type: '.$this->_type); + } + + return $data; + } } \ No newline at end of file