diff --git a/src/API.php b/src/API.php index 22063d2..a275cb4 100644 --- a/src/API.php +++ b/src/API.php @@ -7,9 +7,9 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; -use Intuit\Exceptions\{ConnectionIssueException,InvalidQueryResultException}; +use Intuit\Exceptions\{ConnectionIssueException,InvalidQueryResultException,NotFoundException}; use Intuit\Models\{ProviderToken,Payment}; -use Intuit\Response\{Customer,Invoice,ListList}; +use Intuit\Response\{Customer,Invoice,ListList,Taxcode}; final class API { @@ -25,10 +25,14 @@ final class API private const CURLOPT_HEADER = FALSE; private ProviderToken $token; + private string $url; public function __construct(ProviderToken $token,bool $tryprod=FALSE) { - $this->url = (config('app.env') == 'local' && ! $tryprod) ? 'https://sandbox-quickbooks.api.intuit.com' : 'https://quickbooks.api.intuit.com'; + $this->url = ((config('app.env') === 'local') && (! $tryprod)) + ? 'https://sandbox-quickbooks.api.intuit.com' + : 'https://quickbooks.api.intuit.com'; + $this->token = $token; Log::debug(sprintf('%s:Intuit API for id [%s]',static::LOGKEY,$token->realm_id)); @@ -36,9 +40,17 @@ final class API /* STATIC */ + /** + * URL to get log into Quickbooks + * + * @param bool $tryprod + * @return string + */ public static function url(bool $tryprod=FALSE): string { - return (config('app.env') == 'local' && ! $tryprod) ? 'https://app.sandbox.qbo.intuit.com/app' : 'https://app.qbo.intuit.com/app'; + return ((config('app.env') === 'local') && (! $tryprod)) + ? 'https://app.sandbox.qbo.intuit.com/app' + : 'https://app.qbo.intuit.com/app'; } /** @@ -50,7 +62,7 @@ final class API private function convertHeaders(array $header): array { return collect($header) - ->map(function($value,$key) { return sprintf('%s:%s',$key,$value); }) + ->map(fn($value,$key)=>sprintf('%s:%s',$key,$value)) ->values() ->toArray(); } @@ -63,7 +75,7 @@ final class API * @return object|array * @throws \Exception */ - private function execute(string $path,array $parameters=[]) + private function execute(string $path,array $parameters=[],string $query=NULL): mixed { $url = sprintf('%s/%s/company/%s/%s',$this->url,self::VERSION,$this->token->realm_id,$path); @@ -163,9 +175,41 @@ final class API } }); + switch ($path) { + case 'query': + if (! $result->QueryResponse) { + Log::debug(sprintf('%s:Query response malformed',self::LOGKEY),['parameters'=>$parameters,'x'=>$x]); + throw new InvalidQueryResultException(sprintf('%s:Query response malformed',self::LOGKEY)); + } + + if (! object_get($result->QueryResponse,$query)) + throw new NotFoundException(sprintf('%s:Query returned no results',self::LOGKEY)); + + return $result->QueryResponse; + } + return $result; } + /** + * Find a customer by their email address + * + * @param string $id + * @param array $parameters + * @return Customer + * @throws InvalidQueryResultException + */ + public function getAccountQuery(string $id,array $parameters=[]): Customer + { + Log::debug(sprintf('%s:Get a specific account [%s]',static::LOGKEY,$id)); + + $table = 'Customer'; + $parameters['query'] = sprintf("select * from %s where Id='%s'",$table,$id); + $result = object_get($this->execute('query',$parameters,$table),$table); + + return new Customer(array_pop($result)); + } + /** * Get a list of our classes * @@ -182,28 +226,6 @@ final class API return new ListList($this->execute('query',$parameters),$key); } - /** - * Find a customer by their email address - * - * @param string $id - * @param array $parameters - * @return Customer - * @throws InvalidQueryResultException - */ - public function getAccountQuery(string $id,array $parameters=[]): Customer - { - Log::debug(sprintf('%s:Get a specific account [%s]',static::LOGKEY,$id)); - - $parameters['query'] = sprintf("select * from Customer where PrimaryEmailAddr='%s'",$id); - - $x = $this->execute('query',$parameters); - - if ((! $x->QueryResponse) || (! object_get($x->QueryResponse,'Customer')) || (count($x->QueryResponse->Customer) !== 1)) - throw new InvalidQueryResultException(sprintf('%s:Query response malformed',self::LOGKEY)); - - return new Customer($x->QueryResponse); - } - /** * Get a specific customer record * @@ -247,14 +269,11 @@ final class API { Log::debug(sprintf('%s:Get a specific invoice [%s]',static::LOGKEY,$id)); - $parameters['query'] = sprintf("select * from Invoice where DocNumber='%s'",$id); + $table = 'Invoice'; + $parameters['query'] = sprintf("select * from %s where DocNumber='%s'",$table,$id); + $result = object_get($this->execute('query',$parameters,$table),$table); - $x = $this->execute('query',$parameters); - - if ((! $x->QueryResponse) || (! object_get($x->QueryResponse,'Invoice')) || (count($x->QueryResponse->Invoice) !== 1)) - throw new InvalidQueryResultException(sprintf('%s:Query response malformed [%s]',self::LOGKEY,serialize($x))); - - return new Invoice($x->QueryResponse); + return new Invoice(array_pop($result)); } /** @@ -326,7 +345,26 @@ final class API } /** - * Get a list of our invoices + * Find an taxcode by its ID + * + * @param string $id + * @param array $parameters + * @return Taxcode + * @throws InvalidQueryResultException + */ + public function getTaxCodeQuery(string $id,array $parameters=[]): Taxcode + { + Log::debug(sprintf('%s:Get a specific invoice [%s]',static::LOGKEY,$id)); + + $table = 'TaxCode'; + $parameters['query'] = sprintf("select * from %s where Id='%s'",$table,$id); + $result = object_get($this->execute('query',$parameters,$table),$table); + + return new Taxcode(array_pop($result)); + } + + /** + * Get a list of our tax codes * * @param array $parameters * @return ListList @@ -336,9 +374,9 @@ final class API { Log::debug(sprintf('%s:Get a list of taxes',static::LOGKEY)); $key = 'TaxCode'; - $parameters['query'] = 'select * from Taxcode'; + $parameters['query'] = 'select * from TaxCode'; - return new ListList($this->execute('query',$parameters),$key); + return new ListList($this->execute('query',$parameters,$key),$key); } /** diff --git a/src/Commands/AccountGet.php b/src/Commands/AccountGet.php new file mode 100644 index 0000000..fc358fe --- /dev/null +++ b/src/Commands/AccountGet.php @@ -0,0 +1,56 @@ +providerToken($this->argument('user')); + + try { + dump($to->API()->getAccountQuery($this->argument('id'))); + + } catch (ConnectException|ConnectionIssueException $e) { + $this->error($e->getMessage()); + + return self::FAILURE; + + } catch (NotFoundException $e) { + $this->error('Nothing found'); + + return self::FAILURE; + } + + return self::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Commands/InvoiceGet.php b/src/Commands/InvoiceGet.php new file mode 100644 index 0000000..45c5afe --- /dev/null +++ b/src/Commands/InvoiceGet.php @@ -0,0 +1,56 @@ +providerToken($this->argument('user')); + + try { + dump($to->API()->getInvoiceQuery($this->argument('id'))); + + } catch (ConnectException|ConnectionIssueException $e) { + $this->error($e->getMessage()); + + return self::FAILURE; + + } catch (NotFoundException $e) { + $this->error('Nothing found'); + + return self::FAILURE; + } + + return self::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Commands/PaymentGet.php b/src/Commands/PaymentGet.php new file mode 100644 index 0000000..57438f6 --- /dev/null +++ b/src/Commands/PaymentGet.php @@ -0,0 +1,61 @@ +providerToken($this->argument('user')); + + try { + dd($to->API()->getPayment($this->argument('id'))); + + } catch (ConnectException|ConnectionIssueException $e) { + $this->error($e->getMessage()); + + return self::FAILURE; + + } catch (NotFoundException $e) { + $this->error('Nothing found'); + + return self::FAILURE; + } + + if ($acc) + Job::dispatchSync($to,$acc); + + return self::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Commands/TaxCodeGet.php b/src/Commands/TaxCodeGet.php new file mode 100644 index 0000000..779b7e5 --- /dev/null +++ b/src/Commands/TaxCodeGet.php @@ -0,0 +1,57 @@ +providerToken($this->argument('user')); + + try { + $result = $to->API()->getTaxCodeQuery($this->argument('id')); + dd($result,$result->getTaxRateRef()); + + } catch (ConnectException|ConnectionIssueException $e) { + $this->error($e->getMessage()); + + return self::FAILURE; + + } catch (NotFoundException $e) { + $this->error('Nothing found'); + + return self::FAILURE; + } + + return self::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Exceptions/NotFoundException.php b/src/Exceptions/NotFoundException.php new file mode 100644 index 0000000..b355c01 --- /dev/null +++ b/src/Exceptions/NotFoundException.php @@ -0,0 +1,9 @@ +mergeConfigFrom($this->_path.'/config/intuit.php','intuit'); + + $this->commands([ + AccountGet::class, + InvoiceGet::class, + PaymentGet::class, + TaxCodeGet::class, + ]); } /** @@ -29,8 +37,7 @@ class IntuitServiceProvider extends ServiceProvider */ public function register() { - if (! $this->_path) { + if (! $this->_path) $this->_path = realpath(__DIR__.'/../../src'); - } } } \ No newline at end of file diff --git a/src/Response/Base.php b/src/Response/Base.php index 2e369a3..6d763b8 100644 --- a/src/Response/Base.php +++ b/src/Response/Base.php @@ -55,8 +55,8 @@ abstract class Base implements \JsonSerializable * @param mixed $value * @return mixed */ - public function search(string $key, mixed $value): mixed + public function search(string $key,mixed $value): mixed { - return $this->_data->search(function($item) use ($key,$value) { return $item->{$key} == $value; }); + return $this->_data->search(fn($item)=>$item->{$key}===$value); } } \ No newline at end of file diff --git a/src/Response/Customer.php b/src/Response/Customer.php index f72af44..7990d27 100644 --- a/src/Response/Customer.php +++ b/src/Response/Customer.php @@ -18,6 +18,6 @@ class Customer extends Base if (object_get($response,'time')) unset($response->time); - $this->_model = new CustomerModel((array)$response->Customer); + $this->_model = new CustomerModel((array)$response); } } \ No newline at end of file diff --git a/src/Response/Invoice.php b/src/Response/Invoice.php index 2542f64..3ebcb84 100644 --- a/src/Response/Invoice.php +++ b/src/Response/Invoice.php @@ -18,6 +18,6 @@ class Invoice extends Base if (object_get($response,'time')) unset($response->time); - $this->_model = new InvoiceModel((array)$response->Invoice); + $this->_model = new InvoiceModel((array)$response); } } \ No newline at end of file diff --git a/src/Response/Taxcode.php b/src/Response/Taxcode.php index 264cadd..c5a0a2c 100644 --- a/src/Response/Taxcode.php +++ b/src/Response/Taxcode.php @@ -2,7 +2,8 @@ namespace Intuit\Response; -use Intuit\Models\Tax as TaxModel; +use Illuminate\Support\Collection; +use Intuit\Models\Taxcode as TaxcodeModel; /** * This is an Invoice Intuit Response to API calls @@ -18,6 +19,12 @@ class Taxcode extends Base if (object_get($response,'time')) unset($response->time); - $this->_model = new TaxModel((array)$response->Tax); + $this->_model = new TaxcodeModel((array)$response); + } + + public function getTaxRateRef(): Collection + { + return collect($this->SalesTaxRateList->TaxRateDetail) + ->pluck('TaxRateRef.value'); } } \ No newline at end of file diff --git a/src/Traits/ProviderTokenTrait.php b/src/Traits/ProviderTokenTrait.php new file mode 100644 index 0000000..0334a3d --- /dev/null +++ b/src/Traits/ProviderTokenTrait.php @@ -0,0 +1,33 @@ +sole(); + + $so = ProviderOauth::where('name',self::provider) + ->sole(); + + if (! ($to=$so->token($uo))) + throw new NotTokenException(sprintf('Unknown Tokens for [%s]',$uo->email)); + + return $to; + } +} \ No newline at end of file