Skip to content

Commit

Permalink
Fixed uid generation.
Browse files Browse the repository at this point in the history
Fixed setting correlated invoices.
  • Loading branch information
firebed committed Jun 27, 2024
1 parent 3f5250c commit c9739c7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/Actions/GenerateUid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Firebed\AadeMyData\Actions;

use Firebed\AadeMyData\Enums\InvoiceType;
use Firebed\AadeMyData\Enums\InvoiceVariationType;

class GenerateUid
{
private const NORMALIZE_SERIES = true;

public function handle(string $vatNumber, string $issueDate, int $branchId, InvoiceType|string $invoiceType, string $series, int $number, InvoiceVariationType|int $invoiceVariationType = null): string
{
$attributes = [
$vatNumber,
$issueDate,
$branchId,
$invoiceType instanceof InvoiceType ? $invoiceType->value : $invoiceType,
$this->normalizeSeries($series),
$number
];

if ($invoiceVariationType) {
$attributes[] = $invoiceVariationType instanceof InvoiceVariationType ? $invoiceVariationType->value : $invoiceVariationType;
}

return strtoupper(sha1(implode('-', $attributes)));
}

/**
* Converts series to iso-8859-7 to match myData's current format.
*/
private function normalizeSeries(string $series): string
{
return self::NORMALIZE_SERIES
? mb_convert_encoding($series, 'ISO-8859-7', 'UTF-8')
: $series;
}
}
15 changes: 15 additions & 0 deletions src/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


use DOMDocument;
use Firebed\AadeMyData\Actions\GenerateUid;
use Firebed\AadeMyData\Actions\SquashInvoiceRows;
use Firebed\AadeMyData\Actions\SummarizeInvoice;
use Firebed\AadeMyData\Enums\TransmissionFailure;
Expand Down Expand Up @@ -51,6 +52,20 @@ public function getUid(): ?string
return $this->get('uid');
}

public function guessUid(): ?string
{
$generator = new GenerateUid();
return $generator->handle(
vatNumber: $this->getIssuer()->getVatNumber(),
issueDate: $this->getInvoiceHeader()->getIssueDate(),
branchId: $this->getIssuer()->getBranch(),
invoiceType: $this->getInvoiceHeader()->getInvoiceType(),
series: $this->getInvoiceHeader()->getSeries(),
number: $this->getInvoiceHeader()->getAa(),
invoiceVariationType: $this->getInvoiceHeader()->getInvoiceVariationType(),
);
}

/**
* Συμπληρώνεται από την Υπηρεσία.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Models/InvoiceHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function addCorrelatedInvoice(int $correlatedInvoice): static
*/
public function setCorrelatedInvoices(?array $correlatedInvoices): static
{
return $this->push('correlatedInvoices', $correlatedInvoices);
return $this->set('correlatedInvoices', $correlatedInvoices);
}

/**
Expand Down

0 comments on commit c9739c7

Please sign in to comment.