# moloni/trunk/src/Services/Documents/OpenDocument.php

Moloni, version trunk. 57 lines.

- Page: https://pluginprobe.com/plugins/moloni/trunk/code/src/Services/Documents/OpenDocument.php
- Raw: https://pluginprobe.com/plugins/moloni/trunk/raw/src/Services/Documents/OpenDocument.php
- Modified: 2023-10-02T09:45:54+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/moloni/trunk/code/src/Services/Documents/OpenDocument.php#L10-L20`.

```php
<?php

namespace Moloni\Services\Documents;

use Moloni\Curl;
use Moloni\Enums\DocumentTypes;
use Moloni\Traits\DocumentTypeTrait;

class OpenDocument
{
    use DocumentTypeTrait;

    private $documentId;

    public function __construct($documentId)
    {
        $this->documentId = $documentId;

        $this->run();
    }

    public function run(): void
    {
        $props= [
            'document_id' => $this->documentId
        ];

        $invoice = Curl::simple('documents/getOne', $props);

        if (!isset($invoice['document_id'])) {
            return;
        }

        if ((int)$invoice['status'] === 1) {
            $url = Curl::simple('documents/getPDFLink', $props);

            $location = 'Location: ' . $url['url'];
        } else {
            $company = Curl::simple('companies/getOne', []);
            $slug = $company['slug'];

            $location = 'Location: https://moloni.pt/';
            $location .= $slug . '/' . $this->getDocumentTypeSlug($invoice['document_type']['saft_code']);
            $location .= '/showDetail/' . $invoice['document_id'];
        }

        header($location);
    }

    protected function getDocumentTypeSlug(string $saftcode = ''): string
    {
        $typeName = $this->parseTypeNameFromSaftCode($saftcode);

        return DocumentTypes::getDocumentTypeSlug($typeName);
    }
}

```
