Skip to content

Commit

Permalink
efactura: move requests to classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ciungulete committed Jan 29, 2024
1 parent f690c98 commit 0cdbbbb
Show file tree
Hide file tree
Showing 13 changed files with 467 additions and 65 deletions.
47 changes: 27 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,15 @@ Upload an XML (eFactura) file to the SPV
TODO: improve error handling

```php
$upload = $authorizedClient->efactura()->upload(
xml_path: $pathToXmlFile,
taxIdentificationNumber: '12345678',
//standard: UploadStandard::UBL, // default value is UBL
//extern: false, // default value is false
//selfInvoice: false, // default value is false
);
use Anaf\Requests\Efactura\UploadRequest;
use Anaf\Enums\Efactura\UploadStandard;

$uploadRequest = UploadRequest::withXmlPath($pathToXmlFile)
->withTaxIdentificationNumber('12345678')
->withStandard(UploadStandard::UBL);
//->extern() // for external invoices
//->selfInvoice(); // for self invoices
$upload = $authorizedClient->efactura()->upload($uploadRequest);
$upload->responseDate, // 202401011640
$upload->executionStatus,
$upload->uploadIndex,
Expand All @@ -265,10 +267,14 @@ TODO: implement `status` from [here](https://mfinante.gov.ro/static/10/eFactura/
TODO: implement `paginated messages` from [here](https://mfinante.gov.ro/static/10/eFactura/listamesaje.html#/EFacturaListaMesaje/getPaginatie)
Get the list of available messages
```php
$spvMessages = $authorizedClient->efactura()->messages([
'zile' => 30, // between 1 and 60
'cif' => '12345678',
]);

use Anaf\Enums\Efactura\MessagesFilter;
use Anaf\Requests\Efactura\MessagesRequest;

$messagesRequest = MessagesRequest::withTaxIdetificationNumber('12345678')
->withDays(30);
//->withFilter(MessagesFilter::RECEIVED); // received messages
$spvMessages = $authorizedClient->efactura()->messages($messagesRequest);

$spvMessages->messages; // array
$spvMessages->serial;
Expand All @@ -287,9 +293,7 @@ $message->id,
#### [Download - eFactura XML](https://mfinante.gov.ro/static/10/eFactura/descarcare.html) Resource
Get a file from the SPV identified by the `id` received from the messages endpoint
```php
$file = $authorizedClient->efactura()->download([
'id' => '12345678',
]);
$file = $authorizedClient->efactura()->download(12345678);

$file->getContent(); // string - You can save/download the content to a file
```
Expand All @@ -299,14 +303,17 @@ TODO: implement `validate` from [here](https://mfinante.gov.ro/static/10/eFactur
#### [XmlToPdf](https://mfinante.gov.ro/static/10/eFactura/xmltopdf.html) Resource
Convert XML eFactura to PDF. For this endpoint you need to use unauthenticated client
```php
/*
* $xmlStandard can be one of the following: 'FACT1', 'FCN'.
* The default value is 'FACT1'
*/
$file = $client->efactura()->xmlToPdf($pathToXmlFile, $xmlStandard);

use Anaf\Enums\Efactura\ConvertXmlStandard;
use Anaf\Requests\Efactura\XmlToPdfRequest;

$xmlToPdfRequest = XmlToPdfRequest::withXmlPath($pathToXmlFile)
->withStandard(ConvertXmlStandard::UBL);
//->withoutValidation();

$file = $client->efactura()->xmlToPdf($xmlToPdfRequest);
$file->getContent(); // string - You can save the pdf content to a file
```

---

ANAF PHP is an open-sourced software licensed under the **[MIT license](https://opensource.org/licenses/MIT)**.
11 changes: 11 additions & 0 deletions src/Enums/Efactura/ConvertXmlStandard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Anaf\Enums\Efactura;

enum ConvertXmlStandard: string
{
case INVOICE = 'FACT1';
case CREDIT_NOTE = 'FCN';
}
13 changes: 13 additions & 0 deletions src/Enums/Efactura/MessagesFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Anaf\Enums\Efactura;

enum MessagesFilter: string
{
case ERROR = 'E';
case RECEIVED = 'P';
case SENT = 'T';
case MESSAGE_RESPONSE = 'R';
}
3 changes: 0 additions & 3 deletions src/Enums/Efactura/UploadStandard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Anaf\Enums\Efactura;

/**
* @internal
*/
enum UploadStandard: string
{
case UBL = 'UBL';
Expand Down
76 changes: 76 additions & 0 deletions src/Requests/Efactura/MessagesRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Anaf\Requests\Efactura;

use Anaf\Enums\Efactura\MessagesFilter;
use InvalidArgumentException;

class MessagesRequest
{
/**
* @param array<string, string> $parameters
*/
public function __construct(
private readonly array $parameters,
) {
// ..
}

/**
* Creates a new Messages value object
*/
public static function withTaxIdetificationNumber(string $taxIdentificationNumber): self
{
return new self(
parameters: [
'cif' => $taxIdentificationNumber,
],
);
}

/**
* Creates a new Message value object with the zile parameter
*/
public function withDays(int $days): self
{
if ($days < 1 || $days > 60) {
throw new InvalidArgumentException('Days must be between 1 and 60');
}

return new self(
parameters: [
...$this->parameters,
'zile' => (string) $days,
],
);
}

/**
* Creates a new Message value object with the filtru parameter
*/
public function withFilter(MessagesFilter $filter): self
{
return new self(
parameters: [
...$this->parameters,
'filtru' => $filter->value,
],
);
}

/**
* @return array<string, string> $parameters
*/
public function toArray(): array
{
$requiredKeys = ['zile', 'cif'];

$missingKeys = array_diff($requiredKeys, array_keys($this->parameters));

if ($missingKeys !== []) {
throw new InvalidArgumentException('Missing required parameters: '.implode(', ', $missingKeys));
}

return $this->parameters;
}
}
111 changes: 111 additions & 0 deletions src/Requests/Efactura/UploadRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Anaf\Requests\Efactura;

use Anaf\Enums\Efactura\UploadStandard;
use InvalidArgumentException;

class UploadRequest
{
/**
* @param array<string, string> $parameters
*/
public function __construct(
private readonly string $xml_path,
private readonly array $parameters,
) {
// ..
}

/**
* Creates a new UploadParameters value object with the xml_path parameter
*/
public static function withXmlPath(string $xml_path): self
{
return new self(xml_path: $xml_path, parameters: []);
}

/**
* Creates a new UploadParameters value object with the tax_identification_number parameter
*/
public function withTaxIdentificationNumber(string|int $tax_identification_number): self
{
return new self(
xml_path: $this->xml_path,
parameters: [
...$this->parameters,
'cif' => (string) $tax_identification_number,
],
);
}

/**
* Creates a new UploadParameters value object with the standard parameter
*/
public function withStandard(UploadStandard $standard): self
{
return new self(
xml_path: $this->xml_path,
parameters: [
...$this->parameters,
'standard' => $standard->value,
],
);
}

/**
* Creates a new UploadParameters value object with the extern parameter
*/
public function extern(): self
{
return new self(
xml_path: $this->xml_path,
parameters: [
...$this->parameters,
'extern' => 'DA',
],
);
}

/**
* Creates a new UploadParameters value object with the selfInvoice parameter
*/
public function selfInvoice(): self
{
return new self(
xml_path: $this->xml_path,
parameters: [
...$this->parameters,
'autofactura' => 'DA',
],
);
}

/**
* @return array<string, string> $parameters
*/
public function toArray(): array
{
$requiredKeys = ['cif', 'standard'];

$missingKeys = array_diff($requiredKeys, array_keys($this->parameters));

if ($missingKeys !== []) {
throw new InvalidArgumentException('Missing required parameters: '.implode(', ', $missingKeys));
}

return $this->parameters;
}

/**
* Xml path getter
*/
public function getXmlPath(): string
{
if ($this->xml_path === '' || $this->xml_path === '0') {
throw new InvalidArgumentException('Xml path is required');
}

return $this->xml_path;
}
}
61 changes: 61 additions & 0 deletions src/Requests/Efactura/XmlToPdfRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Anaf\Requests\Efactura;

use Anaf\Enums\Efactura\ConvertXmlStandard;
use InvalidArgumentException;

class XmlToPdfRequest
{
public function __construct(
private readonly string $xml_path,
public readonly ConvertXmlStandard $standard = ConvertXmlStandard::INVOICE,
public readonly bool $validate = true,
) {
// ..
}

/**
* Creates a new UploadParameters value object with the xml_path parameter
*/
public static function withXmlPath(string $xml_path): self
{
return new self(xml_path: $xml_path);
}

/**
* Creates XML to PDF request with the standard parameter
*/
public function withStandard(ConvertXmlStandard $standard): self
{
return new self(
xml_path: $this->xml_path,
standard: $standard,
validate: $this->validate,
);
}

/**
* Creates a new UploadParameters value object with the selfInvoice parameter
*/
public function withoutValidation(): self
{
return new self(
xml_path: $this->xml_path,
standard: $this->standard,
validate: false,
);
}

/**
* Xml path getter
*/
public function getXmlPath(): string
{
if ($this->xml_path === '' || $this->xml_path === '0') {
throw new InvalidArgumentException('Xml path is required');
}

return $this->xml_path;
}
}
Loading

0 comments on commit 0cdbbbb

Please sign in to comment.