Added Item/Category api queries

This commit is contained in:
Deon George 2022-08-25 11:22:24 +10:00
parent 1d209fdbdc
commit 179c96ca5d
5 changed files with 165 additions and 10 deletions

View File

@ -161,6 +161,22 @@ final class API
return new Customer($this->execute('customer/'.$id,$parameters));
}
/**
* Get a list of our classes
*
* @param array $parameters
* @return ListList
* @throws \Exception
*/
public function getClasses(array $parameters=[]): ListList
{
Log::debug(sprintf('%s:Get a list of classes',static::LOGKEY));
$key = 'classes';
$parameters['query'] = 'select * from Class';
return new ListList($this->execute('query',$parameters),$key);
}
/**
* Get a list of our clients
*
@ -193,10 +209,26 @@ final class API
return new ListList($this->execute('query',$parameters),$key);
}
/**
* Get a list of our items
*
* @param array $parameters
* @return ListList
* @throws \Exception
*/
public function getItems(array $parameters=[]): ListList
{
Log::debug(sprintf('%s:Get a list of items',static::LOGKEY));
$key = 'items';
$parameters['query'] = 'select * from Item';
return new ListList($this->execute('query',$parameters),$key);
}
/**
* Setup the API call
*
* @param $url
* @param string $url
* @param array $parameters
* @param array $headers
* @return \CurlHandle

35
src/Models/Category.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace Intuit\Models;
use Illuminate\Support\Arr;
use Jenssegers\Model\Model;
use Intuit\Traits\CompareAttributes;
/*
+"time": "2022-08-13T21:15:37.254-07:00"
*/
final class Category extends Model
{
use CompareAttributes;
public function __get($key) {
$keymap = [
'id' => 'Id',
'synctoken' => 'SyncToken',
];
switch ($key) {
case 'created_at':
return object_get($this->getAttribute('MetaData'),'CreateTime');
case 'updated_at':
return object_get($this->getAttribute('MetaData'),'LastUpdatedTime');
default:
return parent::__get(Arr::get($keymap,$key,$key));
}
}
}

77
src/Models/Item.php Normal file
View File

@ -0,0 +1,77 @@
<?php
namespace Intuit\Models;
use Illuminate\Support\Arr;
use Jenssegers\Model\Model;
use Intuit\Traits\CompareAttributes;
/*
{#1927
+"Name": "Silver retreat"
+"Description": "Retreat - silver level"
+"Active": true
+"SubItem": true
+"ParentRef": {#1928
+"value": "25"
+"name": "Retreat"
}
+"Level": 1
+"FullyQualifiedName": "Retreat:Silver retreat"
+"Taxable": false
+"SalesTaxIncluded": false
+"UnitPrice": 1000
+"Type": "Service"
+"IncomeAccountRef": {#1929
+"value": "1"
+"name": "Services"
}
+"PurchaseTaxIncluded": false
+"PurchaseCost": 0
+"ExpenseAccountRef": {#1930
+"value": "89"
+"name": "Purchases"
}
+"TrackQtyOnHand": false
+"SalesTaxCodeRef": {#1931
+"value": "10"
+"name": "GST"
}
+"PurchaseTaxCodeRef": {#1932
+"value": "2"
+"name": "GST on non-capital"
}
+"domain": "QBO"
+"sparse": false
+"Id": "15"
+"SyncToken": "1"
+"MetaData": {#1933
+"CreateTime": "2018-02-21T20:30:11-08:00"
+"LastUpdatedTime": "2018-06-15T21:19:52-07:00"
}
}
+"time": "2022-08-13T21:15:37.254-07:00"
*/
final class Item extends Model
{
use CompareAttributes;
public function __get($key) {
$keymap = [
'id' => 'Id',
'synctoken' => 'SyncToken',
];
switch ($key) {
case 'created_at':
return object_get($this->getAttribute('MetaData'),'CreateTime');
case 'updated_at':
return object_get($this->getAttribute('MetaData'),'LastUpdatedTime');
default:
return parent::__get(Arr::get($keymap,$key,$key));
}
}
}

View File

@ -2,13 +2,10 @@
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;
/**
* This parent class handles responses received from Intuit
*

View File

@ -2,11 +2,12 @@
namespace Intuit\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use Intuit\Models\{Customer,Invoice};
use Intuit\Models\{Category,Customer,Invoice,Item};
/**
* This is a Generic Intuit Response to API calls that produces a list of objects
@ -22,8 +23,10 @@ class ListList extends Base implements \Countable, \ArrayAccess, \Iterator
private ?int $counter = NULL;
protected const TYPES = [
'customers',
'invoices',
'classes' => 'Class',
'customers' => 'Customer',
'invoices' => 'Invoice',
'items' => 'Item',
];
/**
@ -37,7 +40,7 @@ class ListList extends Base implements \Countable, \ArrayAccess, \Iterator
{
parent::__construct($response);
if (! in_array($type,self::TYPES))
if (! in_array($type,array_keys(self::TYPES)))
throw new \Exception('Unknown data type: '.$type);
if (object_get($response,'time'))
@ -123,13 +126,24 @@ class ListList extends Base implements \Countable, \ArrayAccess, \Iterator
*/
private function data(object $response,string $type): Collection
{
if (! ($x=object_get($response->QueryResponse,Arr::get(self::TYPES,$type))))
return collect();
switch ($type) {
case 'classes':
$data = collect(Category::hydrate($x));
break;
case 'customers':
$data = collect(Customer::hydrate($response->QueryResponse->Customer));
$data = collect(Customer::hydrate($x));
break;
case 'invoices':
$data = collect(Invoice::hydrate($response->QueryResponse->Invoice));
$data = collect(Invoice::hydrate($x));
break;
case 'items':
$data = collect(Item::hydrate($x));
break;
default: throw new \Exception('Unknown object type: '.$this->_type);