Implemented more customer functions

This commit is contained in:
Deon George 2022-08-18 23:18:14 +10:00
parent bbc747621e
commit 1567806b87
5 changed files with 278 additions and 110 deletions

View File

@ -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'])));
}
}

View File

@ -0,0 +1,77 @@
<?php
namespace Intuit\Jobs;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Intuit\Models\{Customer,ProviderToken};
class AccountingCustomerUpdate implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private const LOGKEY = 'JAS';
private Customer $customer;
private ProviderToken $to;
private bool $forceprod;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(ProviderToken $to,Customer $customer,bool $forcprod=FALSE)
{
$this->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));
}
}
}

View File

@ -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
*

25
src/Response/Customer.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace Intuit\Response;
/**
* This is a Customer Intuit Response to API calls
*/
class Customer extends Base
{
protected const LOGKEY = 'RCI';
public function __construct(object $response)
{
parent::__construct($response);
if (object_get($response,'time'))
unset($response->time);
$this->_model = new \Intuit\Models\Customer((array)$response->Customer);
}
public function __get($key) {
return $this->_model->__get($key);
}
}

View File

@ -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;
}
}