Skip to content

Commit 048449b

Browse files
committed
Better invoice handling
1 parent cb8e309 commit 048449b

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

src/Builders/Builder.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,46 @@ public function first()
4747
/**
4848
* @return \Illuminate\Support\Collection|Model[]
4949
*/
50-
public function get( $filters = [] )
50+
public function get( $filters = [], $key = '', $byKey = false )
5151
{
5252
$urlFilters = '';
5353

54-
if ( count( $filters ) > 0 )
54+
if ( ! $byKey )
5555
{
56-
$urlFilters .= '?filters=';
57-
58-
$i = 1;
59-
foreach ( $filters as $filter )
56+
if ( count( $filters ) > 0 )
6057
{
61-
$urlFilters .= $filter[0] . $this->switchComparison( $filter[1] ) . $this->escapeFilter($filter[2]); // todo fix arrays aswell ([1,2,3,...] string)
58+
$urlFilters .= '?filters=';
6259

63-
if ( count( $filters ) > $i )
60+
$i = 1;
61+
foreach ( $filters as $filter )
6462
{
65-
$urlFilters .= '$and:'; // todo allow $or: also
66-
}
63+
$urlFilters .= $filter[0] . $this->switchComparison( $filter[1] ) . $this->escapeFilter( $filter[2] ); // todo fix arrays aswell ([1,2,3,...] string)
64+
65+
if ( count( $filters ) > $i )
66+
{
67+
$urlFilters .= '$and:'; // todo allow $or: also
68+
}
6769

68-
$i ++;
70+
$i ++;
71+
}
6972
}
70-
}
7173

72-
$response = $this->request->curl->get( "/{$this->entity}{$urlFilters}" );
74+
$response = $this->request->curl->get( "/{$this->entity}{$urlFilters}" );
75+
}
76+
else
77+
{
78+
$response = $this->request->curl->get( $key );
79+
}
7380

7481
// todo check for errors and such
7582

7683
$responseData = json_decode( $response->getBody()->getContents() );
84+
85+
if ( ! isset( $responseData->collection ) && isset($responseData->{$key}) )
86+
{
87+
return ( new self( $this->request ) )->get( $filters, $responseData->{$key}, true );
88+
}
89+
7790
$fetchedItems = $responseData->collection;
7891

7992
$items = collect( [] );

src/Builders/InvoiceBuilder.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,44 @@ class InvoiceBuilder extends Builder
66
{
77
protected $entity = 'invoices';
88
protected $model = Invoice::class;
9+
10+
public function get($filters = [], $key = 'self', $byKey = false)
11+
{
12+
return $this->get($filters, $key, $byKey);
13+
}
14+
15+
public function getDrafts(array $filters = [])
16+
{
17+
return $this->get($filters, 'drafts');
18+
}
19+
20+
public function getBooked(array $filters = [])
21+
{
22+
return $this->get($filters, 'booked');
23+
}
24+
25+
public function getNotDue(array $filters = [])
26+
{
27+
return $this->get($filters, 'notDue');
28+
}
29+
30+
public function getOverdue(array $filters = [])
31+
{
32+
return $this->get($filters, 'overdue');
33+
}
34+
35+
public function getPaid(array $filters = [])
36+
{
37+
return $this->get($filters, 'paid');
38+
}
39+
40+
public function getUnpaid(array $filters = [])
41+
{
42+
return $this->get($filters, 'unpaid');
43+
}
44+
45+
public function getTotals(array $filters = [])
46+
{
47+
return $this->get($filters, 'totals');
48+
}
949
}

0 commit comments

Comments
 (0)