| 1 |
<?php |
| 2 |
|
| 3 |
namespace Moloni\Services\Documents; |
| 4 |
|
| 5 |
use Moloni\Curl; |
| 6 |
use Moloni\Enums\DocumentTypes; |
| 7 |
use Moloni\Traits\DocumentTypeTrait; |
| 8 |
|
| 9 |
class OpenDocument |
| 10 |
{ |
| 11 |
use DocumentTypeTrait; |
| 12 |
|
| 13 |
private $documentId; |
| 14 |
|
| 15 |
public function __construct($documentId) |
| 16 |
{ |
| 17 |
$this->documentId = $documentId; |
| 18 |
|
| 19 |
$this->run(); |
| 20 |
} |
| 21 |
|
| 22 |
public function run(): void |
| 23 |
{ |
| 24 |
$props= [ |
| 25 |
'document_id' => $this->documentId |
| 26 |
]; |
| 27 |
|
| 28 |
$invoice = Curl::simple('documents/getOne', $props); |
| 29 |
|
| 30 |
if (!isset($invoice['document_id'])) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
if ((int)$invoice['status'] === 1) { |
| 35 |
$url = Curl::simple('documents/getPDFLink', $props); |
| 36 |
|
| 37 |
$location = 'Location: ' . $url['url']; |
| 38 |
} else { |
| 39 |
$company = Curl::simple('companies/getOne', []); |
| 40 |
$slug = $company['slug']; |
| 41 |
|
| 42 |
$location = 'Location: https://moloni.pt/'; |
| 43 |
$location .= $slug . '/' . $this->getDocumentTypeSlug($invoice['document_type']['saft_code']); |
| 44 |
$location .= '/showDetail/' . $invoice['document_id']; |
| 45 |
} |
| 46 |
|
| 47 |
header($location); |
| 48 |
} |
| 49 |
|
| 50 |
protected function getDocumentTypeSlug(string $saftcode = ''): string |
| 51 |
{ |
| 52 |
$typeName = $this->parseTypeNameFromSaftCode($saftcode); |
| 53 |
|
| 54 |
return DocumentTypes::getDocumentTypeSlug($typeName); |
| 55 |
} |
| 56 |
} |
| 57 |
|