diff --git a/.idea/composerJson.xml b/.idea/composerJson.xml new file mode 100644 index 0000000..1b07430 --- /dev/null +++ b/.idea/composerJson.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/UCRM/REST/Endpoints/Endpoint.php b/src/UCRM/REST/Endpoints/Endpoint.php index b582675..27d71f4 100644 --- a/src/UCRM/REST/Endpoints/Endpoint.php +++ b/src/UCRM/REST/Endpoints/Endpoint.php @@ -43,6 +43,7 @@ public function getId(): int /** * @param string $override * @param array $params + * @param array $query * @return Collection * @throws AnnotationReaderException * @throws CollectionException @@ -52,7 +53,7 @@ public function getId(): int * @throws \MVQN\Helpers\Exceptions\ArrayHelperException * @throws \ReflectionException */ - public static function get(string $override = "", array $params = []): Collection + public static function get(string $override = "", array $params = [], array $query = []): Collection { // Get a reference to the type of Endpoint calling this function. $class = get_called_class(); @@ -84,6 +85,17 @@ public static function get(string $override = "", array $params = []): Collectio //$endpoint = $override; } + // Appen any provided suffixes to the URL (i.e. query string). + if($query !== []) + { + $pairs = []; + + foreach($query as $key => $value) + $pairs[] = "$key=$value"; + + $endpoint .= "?".implode("&", $pairs); + } + // Attempt to GET the specified Endpoint. $response = RestClient::get($endpoint); diff --git a/src/UCRM/REST/Endpoints/Helpers/InvoiceHelper.php b/src/UCRM/REST/Endpoints/Helpers/InvoiceHelper.php index 3fec33c..5600b3e 100644 --- a/src/UCRM/REST/Endpoints/Helpers/InvoiceHelper.php +++ b/src/UCRM/REST/Endpoints/Helpers/InvoiceHelper.php @@ -12,7 +12,7 @@ use UCRM\REST\Exceptions\RestClientException; use UCRM\REST\Exceptions\RestObjectException; -use UCRM\REST\Endpoints\{Client, Invoice}; +use UCRM\REST\Endpoints\{Client, Collections\ClientCollection, Collections\InvoiceCollection, Invoice}; use UCRM\REST\Endpoints\Lookups\InvoiceItem; @@ -69,6 +69,40 @@ public function update(): Invoice return $invoice; } + // ================================================================================================================= + // READ FUNCTIONS + // ----------------------------------------------------------------------------------------------------------------- + + public static function getByClientId(int $clientId): InvoiceCollection + { + /** @var InvoiceCollection $invoices */ + $invoices = Invoice::get("", [], [ "clientId" => $clientId ]); + + return new InvoiceCollection($invoices->elements()); + } + + public static function getByCreatedDate(\DateTime $date): InvoiceCollection + { + /** @var InvoiceCollection $invoices */ + $invoices = Invoice::get("", [], [ "createdDateFrom" => $date->format("Y-m-d"), + "createdDateTo" => $date->format("Y-m-d") ]); + + return new InvoiceCollection($invoices->elements()); + } + + public static function getByCreatedDateBetween(\DateTime $from, \DateTime $to): InvoiceCollection + { + /** @var InvoiceCollection $invoices */ + $invoices = Invoice::get("", [], [ "createdDateFrom" => $from->format("Y-m-d"), + "createdDateTo" => $to->format("Y-m-d") ]); + + return new InvoiceCollection($invoices->elements()); + } + + // TODO: Add more helpers for the remaining query parameters! + + + // ================================================================================================================= // EXTRA FUNCTIONS // ----------------------------------------------------------------------------------------------------------------- diff --git a/tests/UCRM/REST/Endpoints/_02_ClientTests.php b/tests/UCRM/REST/Endpoints/_02_ClientTests.php index 67d64aa..916ca45 100644 --- a/tests/UCRM/REST/Endpoints/_02_ClientTests.php +++ b/tests/UCRM/REST/Endpoints/_02_ClientTests.php @@ -256,7 +256,6 @@ public function testGetById() } - // ================================================================================================================= // UPDATE METHODS // ----------------------------------------------------------------------------------------------------------------- diff --git a/tests/UCRM/REST/Endpoints/_12_InvoiceTests.php b/tests/UCRM/REST/Endpoints/_12_InvoiceTests.php index 10c5dee..94a08b2 100644 --- a/tests/UCRM/REST/Endpoints/_12_InvoiceTests.php +++ b/tests/UCRM/REST/Endpoints/_12_InvoiceTests.php @@ -64,6 +64,24 @@ public function testGetById() echo $invoice."\n"; } + + public function testGetByClientId() + { + $invoices = Invoice::getByClientId(1); + $this->assertNotNull($invoices); + + echo ">>> Invoice::getByClientId(1)\n"; + echo $invoices."\n"; + echo "\n"; + } + + + + + + + + public function testAddInvoiceItem() { $this->markTestSkipped("Each addition duplicates all other line items as well; waiting on info from UBNT!");