Skip to content

Commit

Permalink
Added some Invoice helpers to get by clientId and creation date(s). A…
Browse files Browse the repository at this point in the history
…lso added the query params to the Endpoint::get() method. Added some test code as well!
  • Loading branch information
rspaeth79 committed Aug 23, 2018
1 parent 06b9fa2 commit d86af2d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .idea/composerJson.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion src/UCRM/REST/Endpoints/Endpoint.php
Expand Up @@ -43,6 +43,7 @@ public function getId(): int
/**
* @param string $override
* @param array $params
* @param array $query
* @return Collection
* @throws AnnotationReaderException
* @throws CollectionException
Expand All @@ -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();
Expand Down Expand Up @@ -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);

Expand Down
36 changes: 35 additions & 1 deletion src/UCRM/REST/Endpoints/Helpers/InvoiceHelper.php
Expand Up @@ -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;


Expand Down Expand Up @@ -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
// -----------------------------------------------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion tests/UCRM/REST/Endpoints/_02_ClientTests.php
Expand Up @@ -256,7 +256,6 @@ public function testGetById()
}



// =================================================================================================================
// UPDATE METHODS
// -----------------------------------------------------------------------------------------------------------------
Expand Down
18 changes: 18 additions & 0 deletions tests/UCRM/REST/Endpoints/_12_InvoiceTests.php
Expand Up @@ -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!");
Expand Down

0 comments on commit d86af2d

Please sign in to comment.