| 1 |
<?php |
| 2 |
|
| 3 |
namespace App\Billingo\Api; |
| 4 |
|
| 5 |
use App\Billingo\Api\Queries\DocumentQuery; |
| 6 |
use App\Billingo\Enums\HttpMethodEnum; |
| 7 |
use App\Billingo\Exceptions\BadContentException; |
| 8 |
use App\Billingo\Models\Document\Document; |
| 9 |
use App\Billingo\Models\Document\DocumentCancellation; |
| 10 |
use App\Billingo\Models\Document\DocumentInsert; |
| 11 |
use App\Billingo\Models\Document\InvoiceSettings; |
| 12 |
use App\Billingo\Models\Document\ModificationDocumentInsert; |
| 13 |
use App\Billingo\Models\Document\OnlineSzamlaStatus; |
| 14 |
use App\Billingo\Models\Document\ReceiptInsert; |
| 15 |
use App\Billingo\Models\PaymentHistory; |
| 16 |
use App\Billingo\Models\SendDocument; |
| 17 |
|
| 18 |
class DocumentApi extends BillingoApi |
| 19 |
{ |
| 20 |
const PREFIX = 'documents'; |
| 21 |
const DEFAULT_CONVERT_CLASS = Document::class; |
| 22 |
|
| 23 |
protected static string $convertTo = self::DEFAULT_CONVERT_CLASS; |
| 24 |
|
| 25 |
/** |
| 26 |
* This method creates and returns a new instance of the DocumentQuery class, |
| 27 |
* which can be used to query documents. |
| 28 |
* @return DocumentQuery |
| 29 |
*/ |
| 30 |
public function query(): DocumentQuery |
| 31 |
{ |
| 32 |
return new DocumentQuery(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns a list of your documents. The documents are returned sorted by creation date, |
| 37 |
* with the most recent documents appearing first. |
| 38 |
* @param array $content |
| 39 |
* @return self |
| 40 |
*/ |
| 41 |
public function getAll(array $content = []): self |
| 42 |
{ |
| 43 |
$this->apiContext |
| 44 |
->setMethod(HttpMethodEnum::GET) |
| 45 |
->setUrl(self::PREFIX) |
| 46 |
->setContent($content); |
| 47 |
|
| 48 |
return $this->send(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Create a new document. Returns a document object if the creation is succeeded. |
| 53 |
* @param array|DocumentInsert $document |
| 54 |
* @return DocumentApi |
| 55 |
* @throws BadContentException |
| 56 |
*/ |
| 57 |
public function create(array|DocumentInsert $document): self |
| 58 |
{ |
| 59 |
if (is_array($document)) { |
| 60 |
$document = new DocumentInsert($document); |
| 61 |
} |
| 62 |
|
| 63 |
if ($document->hasError()) { |
| 64 |
|
| 65 |
throw new BadContentException(esc_html( $document->getSelfTest())); |
| 66 |
} |
| 67 |
|
| 68 |
$this->apiContext |
| 69 |
->setMethod(HttpMethodEnum::POST) |
| 70 |
->setUrl(self::PREFIX) |
| 71 |
->setContent($document->toArray()); |
| 72 |
|
| 73 |
return $this->send(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Create a new receipt. Returns a document object if the creation is succeeded. |
| 78 |
* @param array|ReceiptInsert $receipt |
| 79 |
* @return DocumentApi |
| 80 |
* @throws BadContentException |
| 81 |
*/ |
| 82 |
public function createReceipt(array|ReceiptInsert $receipt): self |
| 83 |
{ |
| 84 |
if (is_array($receipt)) { |
| 85 |
$receipt = new ReceiptInsert($receipt); |
| 86 |
} |
| 87 |
|
| 88 |
if ($receipt->hasError()) { |
| 89 |
|
| 90 |
throw new BadContentException(esc_html( $receipt->getSelfTest())); |
| 91 |
} |
| 92 |
|
| 93 |
$this->apiContext |
| 94 |
->setMethod(HttpMethodEnum::POST) |
| 95 |
->setUrl(self::PREFIX . '/receipt') |
| 96 |
->setContent($receipt->toArray()); |
| 97 |
|
| 98 |
return $this->send(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Converts a draft to a receipt. Returns the receipt object if the convert is succeeded. |
| 103 |
* @param int $id |
| 104 |
* @param array|ReceiptInsert $receipt |
| 105 |
* @return DocumentApi |
| 106 |
* @throws BadContentException |
| 107 |
*/ |
| 108 |
public function convertDraftToReceipt(int $id, array|ReceiptInsert $receipt): self |
| 109 |
{ |
| 110 |
if (is_array($receipt)) { |
| 111 |
$receipt = new ReceiptInsert($receipt); |
| 112 |
} |
| 113 |
|
| 114 |
if ($receipt->hasError()) { |
| 115 |
|
| 116 |
throw new BadContentException(esc_html( $receipt->getSelfTest())); |
| 117 |
} |
| 118 |
|
| 119 |
$this->apiContext |
| 120 |
->setMethod(HttpMethodEnum::PUT) |
| 121 |
->setUrl(self::PREFIX . "/receipt/{$id}") |
| 122 |
->setContent($receipt->toArray()); |
| 123 |
|
| 124 |
return $this->send(); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Retrieves the details of an existing document by vendor id. |
| 129 |
* @param int $id |
| 130 |
* @return DocumentApi |
| 131 |
*/ |
| 132 |
public function getByVendorId(int $id): self |
| 133 |
{ |
| 134 |
$this->apiContext |
| 135 |
->setMethod(HttpMethodEnum::GET) |
| 136 |
->setUrl(self::PREFIX . "/vendor/{$id}"); |
| 137 |
|
| 138 |
return $this->send(); |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Retrieves the details of an existing document. |
| 144 |
* @param int $id |
| 145 |
* @return self |
| 146 |
*/ |
| 147 |
public function getById(int $id): self |
| 148 |
{ |
| 149 |
$this->apiContext |
| 150 |
->setMethod(HttpMethodEnum::GET) |
| 151 |
->setUrl(self::PREFIX . "/{$id}"); |
| 152 |
|
| 153 |
return $this->send(); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Converts a draft to an invoice. Returns the invoice object if the convert is succeeded. |
| 158 |
* @param int $id |
| 159 |
* @param array|DocumentInsert $document |
| 160 |
* @return DocumentApi |
| 161 |
* @throws BadContentException |
| 162 |
*/ |
| 163 |
public function convertDraftToInvoice(int $id, array|DocumentInsert $document): self |
| 164 |
{ |
| 165 |
if (is_array($document)) { |
| 166 |
$document = new DocumentInsert($document); |
| 167 |
} |
| 168 |
|
| 169 |
if ($document->hasError()) { |
| 170 |
|
| 171 |
throw new BadContentException(esc_html( $document->getSelfTest())); |
| 172 |
} |
| 173 |
|
| 174 |
$this->apiContext |
| 175 |
->setMethod(HttpMethodEnum::PUT) |
| 176 |
->setUrl(self::PREFIX . "/{$id}") |
| 177 |
->setContent($document->toArray()); |
| 178 |
|
| 179 |
return $this->send(); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Delete an existing draft. |
| 184 |
* @param int $id |
| 185 |
* @return DocumentApi |
| 186 |
*/ |
| 187 |
public function deleteDraft(int $id): self |
| 188 |
{ |
| 189 |
$this->apiContext |
| 190 |
->setMethod(HttpMethodEnum::DELETE) |
| 191 |
->setUrl(self::PREFIX . "/{$id}"); |
| 192 |
|
| 193 |
return $this->send(); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Archive an existing proforma document. |
| 198 |
* @param int $id |
| 199 |
* @return DocumentApi |
| 200 |
*/ |
| 201 |
public function archiveProformaDocument(int $id): self |
| 202 |
{ |
| 203 |
$this->apiContext |
| 204 |
->setMethod(HttpMethodEnum::PUT) |
| 205 |
->setUrl(self::PREFIX . "/{$id}/archive"); |
| 206 |
|
| 207 |
return $this->send(); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Cancel a document. Returns a cancellation document object if the cancellation is succeeded. |
| 212 |
* @param int $id |
| 213 |
* @param DocumentCancellation|array $documentCancellation |
| 214 |
* @return DocumentApi |
| 215 |
*/ |
| 216 |
public function cancelDocument(int $id, DocumentCancellation|array $documentCancellation = []): self |
| 217 |
{ |
| 218 |
if (is_array($documentCancellation) && !empty($documentCancellation)) { |
| 219 |
$documentCancellation = new DocumentCancellation($documentCancellation); |
| 220 |
} |
| 221 |
|
| 222 |
$this->apiContext |
| 223 |
->setMethod(HttpMethodEnum::POST) |
| 224 |
->setUrl(self::PREFIX . "/{$id}/cancel") |
| 225 |
->setContent(empty($documentCancellation) |
| 226 |
? $documentCancellation |
| 227 |
: $documentCancellation->toArray() |
| 228 |
); |
| 229 |
|
| 230 |
return $this->send(); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Copy a document. Returns the new document if the copy was succeeded. |
| 235 |
* @param int $id |
| 236 |
* @return DocumentApi |
| 237 |
*/ |
| 238 |
public function copyDocument(int $id): self |
| 239 |
{ |
| 240 |
$this->apiContext |
| 241 |
->setMethod(HttpMethodEnum::POST) |
| 242 |
->setUrl(self::PREFIX . "/{$id}/copy"); |
| 243 |
|
| 244 |
return $this->send(); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Create a new document from proforma. Returns a document object if the creation is succeeded. |
| 249 |
* @param int $id |
| 250 |
* @param InvoiceSettings|array $invoiceSetting |
| 251 |
* @return DocumentApi |
| 252 |
*/ |
| 253 |
public function createFromProforma(int $id, InvoiceSettings|array $invoiceSetting = []): self |
| 254 |
{ |
| 255 |
if (is_array($invoiceSetting) && !empty($invoiceSetting)) { |
| 256 |
$invoiceSetting = new InvoiceSettings($invoiceSetting); |
| 257 |
} |
| 258 |
|
| 259 |
$this->apiContext |
| 260 |
->setMethod(HttpMethodEnum::POST) |
| 261 |
->setUrl(self::PREFIX . "/{$id}/create-from-proforma") |
| 262 |
->setContent(empty($invoiceSetting) |
| 263 |
? $invoiceSetting |
| 264 |
: $invoiceSetting->toArray() |
| 265 |
); |
| 266 |
|
| 267 |
return $this->send(); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Create a modification document for the given document. |
| 272 |
* Returns a new document object if the creation is successful. |
| 273 |
* @param int $id |
| 274 |
* @param ModificationDocumentInsert|array $modificationDocument |
| 275 |
* @return DocumentApi |
| 276 |
* @throws BadContentException |
| 277 |
*/ |
| 278 |
public function createModificationDocument(int $id, ModificationDocumentInsert|array $modificationDocument): self |
| 279 |
{ |
| 280 |
if (is_array($modificationDocument)) { |
| 281 |
$modificationDocument = (new ModificationDocumentInsert($modificationDocument)); |
| 282 |
} |
| 283 |
|
| 284 |
if ($modificationDocument->hasError()) { |
| 285 |
|
| 286 |
throw new BadContentException(esc_html( $modificationDocument->getSelfTest())); |
| 287 |
} |
| 288 |
|
| 289 |
$this->apiContext |
| 290 |
->setMethod(HttpMethodEnum::POST) |
| 291 |
->setUrl(self::PREFIX . "/{$id}/create-modification-document") |
| 292 |
->setContent($modificationDocument->toArray()); |
| 293 |
|
| 294 |
return $this->send(); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Download a document. Returns a document in PDF format. |
| 299 |
* @param int $id |
| 300 |
* @param string|null $outputFile |
| 301 |
* @return DocumentApi |
| 302 |
*/ |
| 303 |
public function downloadDocumentInPdf(int $id, string $outputFile = null): self |
| 304 |
{ |
| 305 |
self::$convertTo = ''; |
| 306 |
|
| 307 |
$this->apiContext |
| 308 |
->setMethod(HttpMethodEnum::GET) |
| 309 |
->setUrl(self::PREFIX . "/{$id}/download"); |
| 310 |
|
| 311 |
$result = $this->send($outputFile); |
| 312 |
self::$convertTo = self::DEFAULT_CONVERT_CLASS; |
| 313 |
|
| 314 |
return $result; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Retrieves the details of an existing document status. |
| 319 |
* @param int $id |
| 320 |
* @return DocumentApi |
| 321 |
*/ |
| 322 |
public function getOnlineDocument(int $id): self |
| 323 |
{ |
| 324 |
self::$convertTo = OnlineSzamlaStatus::class; |
| 325 |
|
| 326 |
$this->apiContext |
| 327 |
->setMethod(HttpMethodEnum::GET) |
| 328 |
->setUrl(self::PREFIX . "/{$id}/online-szamla"); |
| 329 |
|
| 330 |
$result = $this->send(); |
| 331 |
self::$convertTo = self::DEFAULT_CONVERT_CLASS; |
| 332 |
|
| 333 |
return $result; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Retrieves the details of payment history an existing document. |
| 338 |
* @param int $id |
| 339 |
* @return DocumentApi |
| 340 |
*/ |
| 341 |
public function getPaymentHistory(int $id): self |
| 342 |
{ |
| 343 |
self::$convertTo = PaymentHistory::class; |
| 344 |
|
| 345 |
$this->apiContext |
| 346 |
->setMethod(HttpMethodEnum::GET) |
| 347 |
->setUrl(self::PREFIX . "/{$id}/payments"); |
| 348 |
|
| 349 |
$result = $this->send(); |
| 350 |
self::$convertTo = self::DEFAULT_CONVERT_CLASS; |
| 351 |
|
| 352 |
return $result; |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Update payment history an existing document. |
| 357 |
* Returns a payment history object if the update is succeeded. |
| 358 |
* @param int $id |
| 359 |
* @param PaymentHistory|array $paymentHistory |
| 360 |
* @return DocumentApi |
| 361 |
* @throws BadContentException |
| 362 |
*/ |
| 363 |
public function updatePaymentHistory(int $id, PaymentHistory|array $paymentHistory): self |
| 364 |
{ |
| 365 |
self::$convertTo = PaymentHistory::class; |
| 366 |
|
| 367 |
if (is_array($paymentHistory)) { |
| 368 |
$paymentHistory = (new PaymentHistory($paymentHistory)); |
| 369 |
} |
| 370 |
|
| 371 |
if ($paymentHistory->hasError()) { |
| 372 |
|
| 373 |
throw new BadContentException(esc_html( $paymentHistory->getSelfTest())); |
| 374 |
} |
| 375 |
|
| 376 |
$this->apiContext |
| 377 |
->setMethod(HttpMethodEnum::PUT) |
| 378 |
->setUrl(self::PREFIX . "/{$id}/payments") |
| 379 |
->setContent($paymentHistory->toArray()); |
| 380 |
|
| 381 |
$result = $this->send(); |
| 382 |
self::$convertTo = self::DEFAULT_CONVERT_CLASS; |
| 383 |
|
| 384 |
return $result; |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* Delete all exist payment history on document. |
| 389 |
* @param int $id |
| 390 |
* @return DocumentApi |
| 391 |
*/ |
| 392 |
public function deletePaymentHistory(int $id): self |
| 393 |
{ |
| 394 |
$this->apiContext |
| 395 |
->setMethod(HttpMethodEnum::DELETE) |
| 396 |
->setUrl(self::PREFIX . "/{$id}/payments"); |
| 397 |
|
| 398 |
return $this->send(); |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Returns a printable POS PDF file of a particular document. |
| 403 |
* @param int $id |
| 404 |
* @param string|null $fileOutput |
| 405 |
* @param bool $isSize58 |
| 406 |
* @return DocumentApi |
| 407 |
*/ |
| 408 |
public function downloadPosPdf(int $id, string $fileOutput = null, bool $isSize58 = true): self |
| 409 |
{ |
| 410 |
self::$convertTo = ''; |
| 411 |
|
| 412 |
$this->apiContext |
| 413 |
->setMethod(HttpMethodEnum::GET) |
| 414 |
->setUrl(self::PREFIX . "/{$id}/print/pos") |
| 415 |
->setContent(['size' => $isSize58 ? 58 : 80]); |
| 416 |
|
| 417 |
$result = $this->send($fileOutput); |
| 418 |
self::$convertTo = self::DEFAULT_CONVERT_CLASS; |
| 419 |
|
| 420 |
return $result; |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Retrieves public url to download an existing document. |
| 425 |
* @param int $id |
| 426 |
* @return DocumentApi |
| 427 |
*/ |
| 428 |
public function getPublicUrlForDocument(int $id): self |
| 429 |
{ |
| 430 |
self::$convertTo = ''; |
| 431 |
|
| 432 |
$this->apiContext |
| 433 |
->setMethod(HttpMethodEnum::GET) |
| 434 |
->setUrl(self::PREFIX . "/{$id}/public-url"); |
| 435 |
|
| 436 |
$result = $this->send(); |
| 437 |
self::$convertTo = self::DEFAULT_CONVERT_CLASS; |
| 438 |
|
| 439 |
return $result; |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Returns a list of emails, where the invoice is sent. |
| 444 |
* @param int $id |
| 445 |
* @param SendDocument|array $sendDocument |
| 446 |
* @return DocumentApi |
| 447 |
* @throws BadContentException |
| 448 |
*/ |
| 449 |
public function sendDocumentToEmail(int $id, SendDocument|array $sendDocument): self |
| 450 |
{ |
| 451 |
self::$convertTo = SendDocument::class; |
| 452 |
|
| 453 |
if (is_array($sendDocument)) { |
| 454 |
$sendDocument = (new SendDocument($sendDocument)); |
| 455 |
} |
| 456 |
|
| 457 |
if ($sendDocument->hasError()) { |
| 458 |
|
| 459 |
throw new BadContentException(esc_html( $sendDocument->getSelfTest())); |
| 460 |
} |
| 461 |
|
| 462 |
$this->apiContext |
| 463 |
->setMethod(HttpMethodEnum::POST) |
| 464 |
->setUrl(self::PREFIX . "/{$id}/send") |
| 465 |
->setContent($sendDocument->toArray()); |
| 466 |
|
| 467 |
$result = $this->send(); |
| 468 |
self::$convertTo = self::DEFAULT_CONVERT_CLASS; |
| 469 |
|
| 470 |
return $result; |
| 471 |
} |
| 472 |
} |
| 473 |
|