1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace InvoiceNinjaModule\Service; |
6
|
|
|
|
7
|
|
|
use InvoiceNinjaModule\Exception\ApiAuthException; |
8
|
|
|
use InvoiceNinjaModule\Exception\EmptyResponseException; |
9
|
|
|
use InvoiceNinjaModule\Exception\HttpClientAuthException; |
10
|
|
|
use InvoiceNinjaModule\Exception\HttpClientException; |
11
|
|
|
use InvoiceNinjaModule\Exception\InvalidParameterException; |
12
|
|
|
use InvoiceNinjaModule\Exception\InvalidResultException; |
13
|
|
|
use InvoiceNinjaModule\Exception\NotFoundException; |
14
|
|
|
use InvoiceNinjaModule\Model\Interfaces\BaseInterface; |
15
|
|
|
use InvoiceNinjaModule\Model\Interfaces\InvoiceInterface; |
16
|
|
|
use InvoiceNinjaModule\Model\Invoice; |
17
|
|
|
use InvoiceNinjaModule\Service\Interfaces\InvoiceManagerInterface; |
18
|
|
|
use InvoiceNinjaModule\Service\Interfaces\ObjectServiceInterface; |
19
|
|
|
use JetBrains\PhpStorm\Pure; |
20
|
|
|
use JsonException; |
21
|
|
|
|
22
|
|
|
use function count; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class InvoiceManager |
26
|
|
|
*/ |
27
|
|
|
final class InvoiceManager implements InvoiceManagerInterface |
28
|
|
|
{ |
29
|
|
|
private ObjectServiceInterface $objectManager; |
30
|
|
|
private string $reqRoute; |
31
|
|
|
private InvoiceInterface $objectType; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* InvoiceManager constructor. |
35
|
|
|
* |
36
|
|
|
* @param ObjectServiceInterface $objectManager |
37
|
|
|
*/ |
38
|
17 |
|
#[Pure] public function __construct(ObjectServiceInterface $objectManager) |
39
|
|
|
{ |
40
|
17 |
|
$this->objectManager = $objectManager; |
41
|
17 |
|
$this->reqRoute = '/invoices'; |
42
|
17 |
|
$this->objectType = new Invoice(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param InvoiceInterface $invoice |
47
|
|
|
* |
48
|
|
|
* @return InvoiceInterface |
49
|
|
|
* @throws ApiAuthException |
50
|
|
|
* @throws EmptyResponseException |
51
|
|
|
* @throws HttpClientAuthException |
52
|
|
|
* @throws InvalidResultException |
53
|
|
|
* @throws HttpClientException |
54
|
|
|
* @throws JsonException |
55
|
|
|
*/ |
56
|
1 |
|
public function createInvoice(InvoiceInterface $invoice): InvoiceInterface |
57
|
|
|
{ |
58
|
|
|
//@TODO: Invoice needs at least a clientId, a date and status! |
59
|
1 |
|
return $this->checkResult($this->objectManager->createObject($invoice, $this->reqRoute)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param InvoiceInterface $invoice |
64
|
|
|
* |
65
|
|
|
* @return InvoiceInterface |
66
|
|
|
* @throws ApiAuthException |
67
|
|
|
* @throws EmptyResponseException |
68
|
|
|
* @throws HttpClientAuthException |
69
|
|
|
* @throws HttpClientException |
70
|
|
|
* @throws InvalidResultException |
71
|
|
|
* @throws JsonException |
72
|
|
|
*/ |
73
|
1 |
|
public function delete(InvoiceInterface $invoice): InvoiceInterface |
74
|
|
|
{ |
75
|
1 |
|
return $this->checkResult($this->objectManager->deleteObject($invoice, $this->reqRoute)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param InvoiceInterface $invoice |
80
|
|
|
* |
81
|
|
|
* @return InvoiceInterface |
82
|
|
|
* @throws ApiAuthException |
83
|
|
|
* @throws EmptyResponseException |
84
|
|
|
* @throws HttpClientAuthException |
85
|
|
|
* @throws HttpClientException |
86
|
|
|
* @throws InvalidResultException |
87
|
|
|
* @throws JsonException |
88
|
|
|
*/ |
89
|
1 |
|
public function update(InvoiceInterface $invoice): InvoiceInterface |
90
|
|
|
{ |
91
|
1 |
|
return $this->checkResult($this->objectManager->updateObject($invoice, $this->reqRoute)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param InvoiceInterface $invoice |
96
|
|
|
* |
97
|
|
|
* @return InvoiceInterface |
98
|
|
|
* @throws ApiAuthException |
99
|
|
|
* @throws EmptyResponseException |
100
|
|
|
* @throws HttpClientAuthException |
101
|
|
|
* @throws HttpClientException |
102
|
|
|
* @throws InvalidResultException |
103
|
|
|
* @throws JsonException |
104
|
|
|
*/ |
105
|
1 |
|
public function restore(InvoiceInterface $invoice): InvoiceInterface |
106
|
|
|
{ |
107
|
1 |
|
return $this->checkResult($this->objectManager->restoreObject($invoice, $this->reqRoute)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param InvoiceInterface $invoice |
112
|
|
|
* |
113
|
|
|
* @return InvoiceInterface |
114
|
|
|
* @throws ApiAuthException |
115
|
|
|
* @throws EmptyResponseException |
116
|
|
|
* @throws HttpClientAuthException |
117
|
|
|
* @throws HttpClientException |
118
|
|
|
* @throws InvalidResultException |
119
|
|
|
* @throws JsonException |
120
|
|
|
*/ |
121
|
1 |
|
public function archive(InvoiceInterface $invoice): InvoiceInterface |
122
|
|
|
{ |
123
|
1 |
|
return $this->checkResult($this->objectManager->archiveObject($invoice, $this->reqRoute)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $id |
128
|
|
|
* |
129
|
|
|
* @return InvoiceInterface |
130
|
|
|
* @throws ApiAuthException |
131
|
|
|
* @throws HttpClientAuthException |
132
|
|
|
* @throws HttpClientException |
133
|
|
|
* @throws InvalidResultException |
134
|
|
|
* @throws JsonException |
135
|
|
|
* @throws NotFoundException |
136
|
|
|
*/ |
137
|
2 |
|
public function getInvoiceById(string $id): InvoiceInterface |
138
|
|
|
{ |
139
|
2 |
|
return $this->checkResult($this->objectManager->getObjectById($this->objectType, $id, $this->reqRoute)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $invoiceNumber |
144
|
|
|
* |
145
|
|
|
* @return InvoiceInterface |
146
|
|
|
* @throws ApiAuthException |
147
|
|
|
* @throws HttpClientAuthException |
148
|
|
|
* @throws HttpClientException |
149
|
|
|
* @throws InvalidParameterException |
150
|
|
|
* @throws InvalidResultException |
151
|
|
|
* @throws JsonException |
152
|
|
|
* @throws NotFoundException |
153
|
|
|
*/ |
154
|
3 |
|
public function getInvoiceByNumber(string $invoiceNumber): InvoiceInterface |
155
|
|
|
{ |
156
|
3 |
|
$result = $this->objectManager->findObjectBy( |
157
|
3 |
|
$this->objectType, |
158
|
3 |
|
[InvoiceInterface::INVOICE_NR => $invoiceNumber], |
159
|
3 |
|
$this->reqRoute |
160
|
3 |
|
); |
161
|
|
|
|
162
|
3 |
|
if (count($result) === 1 && $result[0] instanceof InvoiceInterface) { |
163
|
1 |
|
return $result[0]; |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
if (empty($result)) { |
167
|
1 |
|
throw new NotFoundException(InvoiceInterface::INVOICE_NR . ' ' . $invoiceNumber); |
168
|
|
|
} |
169
|
1 |
|
throw new InvalidResultException(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param int $page |
174
|
|
|
* @param int $pageSize |
175
|
|
|
* |
176
|
|
|
* @return InvoiceInterface[] |
177
|
|
|
* @throws ApiAuthException |
178
|
|
|
* @throws EmptyResponseException |
179
|
|
|
* @throws HttpClientAuthException |
180
|
|
|
* @throws HttpClientException |
181
|
|
|
* @throws InvalidResultException |
182
|
|
|
* @throws JsonException |
183
|
|
|
*/ |
184
|
3 |
|
public function getAllInvoices(int $page = 1, int $pageSize = 0): array |
185
|
|
|
{ |
186
|
3 |
|
$result = $this->objectManager->getAllObjects($this->objectType, $this->reqRoute, $page, $pageSize); |
187
|
3 |
|
foreach ($result as $invoice) { |
188
|
2 |
|
$this->checkResult($invoice); |
189
|
|
|
} |
190
|
2 |
|
return $result; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param string $invitationKey |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
* @throws ApiAuthException |
198
|
|
|
* @throws EmptyResponseException |
199
|
|
|
* @throws HttpClientAuthException |
200
|
|
|
* @throws HttpClientException |
201
|
|
|
* @throws JsonException |
202
|
|
|
*/ |
203
|
1 |
|
public function downloadInvoice(string $invitationKey): array |
204
|
|
|
{ |
205
|
1 |
|
return $this->objectManager->downloadFile($invitationKey, 'invoice'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param array $invoiceIds |
210
|
|
|
* |
211
|
|
|
* @throws ApiAuthException |
212
|
|
|
* @throws EmptyResponseException |
213
|
|
|
* @throws HttpClientAuthException |
214
|
|
|
* @throws HttpClientException |
215
|
|
|
* @throws JsonException |
216
|
|
|
*/ |
217
|
1 |
|
public function sendInvoicesEmail(array $invoiceIds): void |
218
|
|
|
{ |
219
|
1 |
|
$this->objectManager->sendBulkCommand( |
220
|
1 |
|
ObjectServiceInterface::ACTION_EMAIL, |
221
|
1 |
|
$invoiceIds, |
222
|
1 |
|
$this->reqRoute |
223
|
1 |
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param array $invoiceIds |
228
|
|
|
* |
229
|
|
|
* @throws ApiAuthException |
230
|
|
|
* @throws EmptyResponseException |
231
|
|
|
* @throws HttpClientAuthException |
232
|
|
|
* @throws HttpClientException |
233
|
|
|
* @throws JsonException |
234
|
|
|
*/ |
235
|
|
|
public function markInvoicesSent(array $invoiceIds): void |
236
|
|
|
{ |
237
|
|
|
$this->objectManager->sendBulkCommand( |
238
|
|
|
ObjectServiceInterface::ACTION_MARK_SENT, |
239
|
|
|
$invoiceIds, |
240
|
|
|
$this->reqRoute |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param array $invoiceIds |
246
|
|
|
* |
247
|
|
|
* @throws ApiAuthException |
248
|
|
|
* @throws EmptyResponseException |
249
|
|
|
* @throws HttpClientAuthException |
250
|
|
|
* @throws HttpClientException |
251
|
|
|
* @throws JsonException |
252
|
|
|
*/ |
253
|
|
|
public function markInvoicesPaid(array $invoiceIds): void |
254
|
|
|
{ |
255
|
|
|
$this->objectManager->sendBulkCommand( |
256
|
|
|
ObjectServiceInterface::ACTION_MARK_PAID, |
257
|
|
|
$invoiceIds, |
258
|
|
|
$this->reqRoute |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param BaseInterface $invoice |
264
|
|
|
* |
265
|
|
|
* @return InvoiceInterface |
266
|
|
|
* @throws InvalidResultException |
267
|
|
|
*/ |
268
|
8 |
|
private function checkResult(BaseInterface $invoice): InvoiceInterface |
269
|
|
|
{ |
270
|
8 |
|
if (!$invoice instanceof InvoiceInterface) { |
271
|
1 |
|
throw new InvalidResultException(); |
272
|
|
|
} |
273
|
7 |
|
return $invoice; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|