Skip to content

Commit 4920688

Browse files
authored
Merge pull request #67 from turbo124/main
Documentation for SDK v5
2 parents 911edc4 + ae3fb84 commit 4920688

16 files changed

+289
-48
lines changed

README.md

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,37 @@ Retrieve all clients
3232
$clients = $ninja->clients->all();
3333
```
3434

35+
You can perform complex filtering on the ->all() method.
36+
37+
Query parameters can be chained together to form complex queries. The current supported values are:
38+
39+
**per_page**: The number of clients per page you want returned
40+
**page**: The page number
41+
**include**: A comma separated list of relations to include ie. contacts,documents,gateway_tokens
42+
**balance**: A query to return clients with a balance using an operator and value
43+
- ie ?balance=lt:10 Returns clients with a balance less than 10
44+
- available operators lt, lte, gt, gte, eq
45+
**between_balance**: Returns clients with a balance between two values
46+
- ie ?between_balance=10:20 - Returns clients with a balance between 10 and 20
47+
**email**: Returns clients with a contacts.email field equal to an email
48+
**id_number**: Search by id_number
49+
**number**: Search by number
50+
**filter**: Search across multiple columns (name, id_number, first_name, last_name, email, custom_value1, custom_value2, custom_value3, custom_value4)
51+
**created_at**: Search by created at (Unix timestamp)
52+
**is_deleted**: Search using is_deleted boolean flag
53+
54+
For example,
55+
56+
```php
57+
$clients = $ninja->clients->all([
58+
'balance' => 'lt:10', // get all clients with a balance less than 10
59+
'per_page' => 10, // return 10 results per page
60+
'page' => 2, // paginate to page 2
61+
'include' => 'documents', // include the documents relationship
62+
]);
63+
64+
```
65+
3566
Retrieve a client by its primary key.
3667
```php
3768
$client = $ninja->clients->get("CLIENT_HASHED_ID");
@@ -59,7 +90,54 @@ Create an invoice
5990
$client = $ninja->invoices->create(['client_id' => CLIENT_HASHED_ID]);
6091

6192
```
62-
Download an invoice PDF
93+
94+
When creating an invoice, you can perform actions on the invoice in a single call, for example, say you wish to create an invoice and also apply a payment to the invoice:
95+
96+
```php
97+
$invoice = $ninja->invoices->create(['client_id'=> 'CLIENT_HASHED_ID'], ['mark_paid' => true]);
98+
```
99+
100+
Or if you wish to apply a partial payment
101+
102+
```php
103+
$invoice = $ninja->invoices->create(['client_id'=> 'CLIENT_HASHED_ID'], ['amount_paid' => 10]);
104+
```
105+
106+
Or you may want to automatically send and charge the invoice **note requires a payment method on file**
107+
```php
108+
$invoice = $ninja->invoices->create(['client_id'=> 'CLIENT_HASHED_ID'], ['auto_bill' => true, 'send_email' => true]);
109+
```
110+
111+
112+
### Bulk actions
113+
114+
You can perform bulk actions against one or many entities. For example if you wish to batch archive a range of invoice you would do
115+
116+
```php
117+
$bulk = $ninja->invoices->archive(["hash_1","hash_2"]);
118+
```
119+
120+
You can access the raw bulk method using the following:
121+
122+
```php
123+
$bulk = $ninja->invoices->bulk("archive", ["hash_1","hash_2"]);
124+
```
125+
126+
If you wanted to download a invoice PDF
63127
```php
64-
$pdf = $invoice->download();
65-
```
128+
$pdf = $ninja->invoices->bulk("download", ["hash_1"]);
129+
```
130+
131+
The following are a list of available bulk actions for invoices:
132+
133+
+ mark_sent
134+
+ download
135+
+ restore
136+
+ archive
137+
+ delete
138+
+ mark_paid
139+
+ clone_to_quote
140+
+ clone_to_invoice
141+
+ cancel
142+
+ reverse
143+
+ email

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"guzzlehttp/guzzle": "^7.3"
1818
},
1919
"require-dev": {
20+
"fakerphp/faker": "^1.16",
2021
"phpunit/phpunit": "^9.5"
2122
},
2223
"autoload-dev": {

src/Endpoints/BaseEntity.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Invoice Ninja (https://invoiceninja.com).
4+
*
5+
* @link https://github.com/invoiceninja/sdk-php source repository
6+
*
7+
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
8+
*
9+
* @license https://opensource.org/licenses/MIT
10+
*/
11+
12+
namespace InvoiceNinja\Sdk\Endpoints;
13+
14+
use GuzzleHttp\Exception\GuzzleException;
15+
use InvoiceNinja\Sdk\InvoiceNinja;
16+
17+
class BaseEntity
18+
{
19+
20+
public function bulk(string $action, array $ids)
21+
{
22+
$query['form_params'] = ["action" => $action, "ids" => $ids];
23+
24+
return $this->ninja->send("POST", "{$this->uri}/bulk", $query);
25+
}
26+
27+
public function archive(array $entity_array)
28+
{
29+
return $this->bulk("archive", $entity_array);
30+
}
31+
32+
public function delete(array $entity_array)
33+
{
34+
return $this->bulk("delete", $entity_array);
35+
}
36+
37+
public function restore(array $entity_array)
38+
{
39+
return $this->bulk("restore", $entity_array);
40+
}
41+
42+
}
43+

src/Endpoints/Clients.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
use GuzzleHttp\Exception\GuzzleException;
1515
use InvoiceNinja\Sdk\InvoiceNinja;
1616

17-
class Clients
17+
class Clients extends BaseEntity
1818
{
1919

2020
protected InvoiceNinja $ninja;
2121

22+
protected string $uri = "/api/v1/clients";
23+
2224
public function __construct(InvoiceNinja $ninja)
2325
{
2426
$this->ninja = $ninja;
@@ -33,28 +35,28 @@ public function all(array $search = [])
3335
{
3436
$query = ['query' => $search];
3537

36-
return $this->ninja->send("GET", "/api/v1/clients", $query);
38+
return $this->ninja->send("GET", "{$this->uri}", $query);
3739
}
3840

3941
public function get(string $client_id, array $search = [])
4042
{
4143
$query = ['query' => $search];
4244

43-
return $this->ninja->send("GET", "/api/v1/clients/{$client_id}", $query);
45+
return $this->ninja->send("GET", "{$this->uri}/{$client_id}", $query);
4446
}
4547

4648
public function update(string $client_id, array $client)
4749
{
4850
$query = ['form_params' => $client];
4951

50-
return $this->ninja->send("PUT", "/api/v1/clients/{$client_id}", $query);
52+
return $this->ninja->send("PUT", "{$this->uri}/{$client_id}", $query);
5153
}
5254

5355
public function create(array $client, array $includes = [])
5456
{
5557
$query = ['form_params' => $client, 'query' => $includes];
5658

57-
return $this->ninja->send("POST", "/api/v1/clients", $query);
59+
return $this->ninja->send("POST", "{$this->uri}", $query);
5860
}
5961
}
6062

src/Endpoints/Invoices.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
use GuzzleHttp\Exception\GuzzleException;
1515
use InvoiceNinja\Sdk\InvoiceNinja;
1616

17-
class Invoices
17+
class Invoices extends BaseEntity
1818
{
1919

2020
protected InvoiceNinja $ninja;
2121

22+
protected string $uri = "/api/v1/invoices";
23+
2224
public function __construct(InvoiceNinja $ninja)
2325
{
2426
$this->ninja = $ninja;
@@ -33,28 +35,28 @@ public function all(array $search = [])
3335
{
3436
$query = ['query' => $search];
3537

36-
return $this->ninja->send("GET", "/api/v1/invoices", $query);
38+
return $this->ninja->send("GET", "{$this->uri}", $query);
3739
}
3840

3941
public function get(string $invoice_id, array $search = [])
4042
{
4143
$query = ['query' => $search];
4244

43-
return $this->ninja->send("GET", "/api/v1/invoices/{$invoice_id}", $query);
45+
return $this->ninja->send("GET", "{$this->uri}/{$invoice_id}", $query);
4446
}
4547

4648
public function update(string $invoice_id, array $invoices)
4749
{
4850
$query = ['form_params' => $invoices];
4951

50-
return $this->ninja->send("PUT", "/api/v1/invoices/{$invoice_id}", $query);
52+
return $this->ninja->send("PUT", "{$this->uri}/{$invoice_id}", $query);
5153
}
5254

5355
public function create(array $invoices, array $includes = [])
5456
{
5557
$query = ['form_params' => $invoices, 'query' => $includes];
5658

57-
return $this->ninja->send("POST", "/api/v1/invoices", $query);
59+
return $this->ninja->send("POST", "{$this->uri}", $query);
5860
}
5961
}
6062

src/Endpoints/Payments.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
use GuzzleHttp\Exception\GuzzleException;
1515
use InvoiceNinja\Sdk\InvoiceNinja;
1616

17-
class Payments
17+
class Payments extends BaseEntity
1818
{
1919

2020
protected InvoiceNinja $ninja;
2121

22+
protected string $uri = "/api/v1/payments";
23+
2224
public function __construct(InvoiceNinja $ninja)
2325
{
2426
$this->ninja = $ninja;
@@ -33,28 +35,28 @@ public function all(array $search = [])
3335
{
3436
$query = ['query' => $search];
3537

36-
return $this->ninja->send("GET", "/api/v1/payments", $query);
38+
return $this->ninja->send("GET", "{$this->uri}", $query);
3739
}
3840

3941
public function get(string $payment_id, array $search = [])
4042
{
4143
$query = ['query' => $search];
4244

43-
return $this->ninja->send("GET", "/api/v1/payments/{$payment_id}", $query);
45+
return $this->ninja->send("GET", "{$this->uri}/{$payment_id}", $query);
4446
}
4547

4648
public function update(string $payment_id, array $payment)
4749
{
4850
$query = ['form_params' => $payment];
4951

50-
return $this->ninja->send("PUT", "/api/v1/payments/{$payment_id}", $query);
52+
return $this->ninja->send("PUT", "{$this->uri}/{$payment_id}", $query);
5153
}
5254

5355
public function create(array $payment, array $includes = [])
5456
{
5557
$query = ['form_params' => $payment, 'query' => $includes];
5658

57-
return $this->ninja->send("POST", "/api/v1/payments", $query);
59+
return $this->ninja->send("POST", "{$this->uri}", $query);
5860
}
5961
}
6062

src/Endpoints/Products.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
use GuzzleHttp\Exception\GuzzleException;
1515
use InvoiceNinja\Sdk\InvoiceNinja;
1616

17-
class Products
17+
class Products extends BaseEntity
1818
{
1919

2020
protected InvoiceNinja $ninja;
2121

22+
protected string $uri = "/api/v1/products";
23+
2224
public function __construct(InvoiceNinja $ninja)
2325
{
2426
$this->ninja = $ninja;
@@ -33,28 +35,28 @@ public function all(array $search = [])
3335
{
3436
$query = ['query' => $search];
3537

36-
return $this->ninja->send("GET", "/api/v1/products", $query);
38+
return $this->ninja->send("GET", "{$this->uri}", $query);
3739
}
3840

3941
public function get(string $product_id, array $search = [])
4042
{
4143
$query = ['query' => $search];
4244

43-
return $this->ninja->send("GET", "/api/v1/products/{$product_id}", $query);
45+
return $this->ninja->send("GET", "{$this->uri}/{$product_id}", $query);
4446
}
4547

4648
public function update(string $product_id, array $product)
4749
{
4850
$query = ['form_params' => $product];
4951

50-
return $this->ninja->send("PUT", "/api/v1/products/{$product_id}", $query);
52+
return $this->ninja->send("PUT", "{$this->uri}/{$product_id}", $query);
5153
}
5254

5355
public function create(array $product, array $includes = [])
5456
{
5557
$query = ['form_params' => $product, 'query' => $includes];
5658

57-
return $this->ninja->send("POST", "/api/v1/products", $query);
59+
return $this->ninja->send("POST", "{$this->uri}", $query);
5860
}
5961
}
6062

src/Endpoints/Quotes.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
use GuzzleHttp\Exception\GuzzleException;
1515
use InvoiceNinja\Sdk\InvoiceNinja;
1616

17-
class Quotes
17+
class Quotes extends BaseEntity
1818
{
1919

2020
protected InvoiceNinja $ninja;
2121

22+
protected string $uri = "/api/v1/quotes";
23+
2224
public function __construct(InvoiceNinja $ninja)
2325
{
2426
$this->ninja = $ninja;
@@ -33,28 +35,28 @@ public function all(array $search = [])
3335
{
3436
$query = ['query' => $search];
3537

36-
return $this->ninja->send("GET", "/api/v1/quotes", $query);
38+
return $this->ninja->send("GET", "{$this->uri}", $query);
3739
}
3840

3941
public function get(string $quote_id, array $search = [])
4042
{
4143
$query = ['query' => $search];
4244

43-
return $this->ninja->send("GET", "/api/v1/quotes/{$quote_id}", $query);
45+
return $this->ninja->send("GET", "{$this->uri}/{$quote_id}", $query);
4446
}
4547

4648
public function update(string $quote_id, array $quote)
4749
{
4850
$query = ['form_params' => $quote];
4951

50-
return $this->ninja->send("PUT", "/api/v1/quotes/{$quote_id}", $query);
52+
return $this->ninja->send("PUT", "{$this->uri}/{$quote_id}", $query);
5153
}
5254

5355
public function create(array $entity, array $includes = [])
5456
{
5557
$query = ['form_params' => $entity, 'query' => $includes];
5658

57-
return $this->ninja->send("POST", "/api/v1/quotes", $query);
59+
return $this->ninja->send("POST", "{$this->uri}", $query);
5860
}
5961
}
6062

0 commit comments

Comments
 (0)