Skip to content

Commit

Permalink
fix: Greek VIES code uses ISO 639-1 language code (EL) instead of ISO…
Browse files Browse the repository at this point in the history
… 3166 country code.

Close #48
  • Loading branch information
arraintxo committed Oct 29, 2024
1 parent 06f0314 commit 185ad57
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/Barnetik/Tbai/Subject/Recipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,19 @@ public static function createFromXml(DOMXPath $xpath, DOMNode $contextNode): sel
public function xml(DOMDocument $domDocument): DOMNode
{
$recipient = $domDocument->createElement('IDDestinatario');

$vatId = (string)$this->vatId();
if ($this->hasNifAsVatId() && $this->isNational()) {
$recipient->appendChild(
$domDocument->createElement('NIF', $this->vatId())
$domDocument->createElement('NIF', $vatId)
);
} else {
$otherId = $domDocument->createElement('IDOtro');
$otherId->appendChild($domDocument->createElement('CodigoPais', $this->countryCode()));
$otherId->appendChild($domDocument->createElement('IDType', $this->vatIdType()));

$vatId = (string)$this->vatId();
if ($this->hasNifAsVatId() && substr($vatId, 0, 2) !== $this->countryCode()) {
$vatId = $this->countryCode . $vatId;
if ($this->hasNifAsVatId() && substr($vatId, 0, 2) !== $this->viesCountryCode()) {
$vatId = $this->viesCountryCode() . $vatId;
}
$otherId->appendChild($domDocument->createElement('ID', $vatId));

Expand All @@ -164,6 +165,15 @@ public function xml(DOMDocument $domDocument): DOMNode
return $recipient;
}

public function viesCountryCode(): string
{
if ($this->countryCode === 'GR') {
return 'EL';
}

return $this->countryCode;
}

public static function docJson(): array
{
return [
Expand Down
5 changes: 5 additions & 0 deletions tests/Barnetik/Tbai/LROE/ExpensesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public function test_self_employed_expenses_without_invoice_is_delivered(): void
$expensesData['recipient']['vatId'] = $nif;
$expensesData['recipient']['name'] = $recipient;
$expensesData['header']['invoiceNumber'] = time();
$expensesData['operationDate'] = date('d-m-Y'); // Only works onces a day, it will say that allready exists on other cases
$expensesData['income']['irpfExpenseAmount'] = rand(1, 10000000);
sleep(1);
$expenses = ExpensesWithoutInvoice::createFromJson($expensesData);

Expand All @@ -140,6 +142,9 @@ public function test_self_employed_expenses_without_invoice_is_delivered(): void
$responseFile = tempnam(__DIR__ . '/../__files/responses', 'response-');
file_put_contents($responseFile, $response->content());
if (!$response->isCorrect()) {
if ($response->errorDataRegistry()[0]['errorMessage']['eu'] === 'Erregistro bikoiztua.') {
$this->markTestSkipped('Duplicated registry. Already tested today.');
}
echo "\n";
echo "VatId / IFZ / NIF: " . $expenses->recipientVatId() . "\n";
echo "Date:" . date('Y-m-d H:i:s') . "\n";
Expand Down
8 changes: 4 additions & 4 deletions tests/Barnetik/Tbai/Mother/TicketBaiMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ public function createTicketBaiSimplifiedRectification(TicketBai $previousInvoic
);
}

public function createTicketBaiWithForeignServices(string $nif, string $issuer, string $license, string $developer, string $appName, string $appVersion, string $territory, bool $selfEmployed = false): TicketBai
public function createTicketBaiWithForeignServices(string $nif, string $issuer, string $license, string $developer, string $appName, string $appVersion, string $territory, bool $selfEmployed = false, string $clientIdType = VatId::VAT_ID_TYPE_PASSPORT, string $clientNif = '00000000T', string $countryCode = 'IE'): TicketBai
{
$subject = $this->getForeignSubject($nif, $issuer);
$subject = $this->getForeignSubject($nif, $issuer, $clientIdType, $clientNif, $countryCode);
$fingerprint = $this->getFingerprint($license, $developer, $appName, $appVersion);

$header = Header::create((string)time(), new Date(date('d-m-Y')), new Time(date('H:i:s')), $this->testSerie());
Expand Down Expand Up @@ -632,10 +632,10 @@ public function getSubjectWithoutRecipient(string $nif, string $name, bool $with
return new Subject($issuer, $recipient, Subject::ISSUED_BY_THIRD_PARTY);
}

public function getForeignSubject(string $nif, string $name): Subject
public function getForeignSubject(string $nif, string $name, string $clientIdType = VatId::VAT_ID_TYPE_PASSPORT, string $clientNif = '00000000T', string $countryCode = 'IE'): Subject
{
$issuer = new Issuer(new VatId($nif, VatId::VAT_ID_TYPE_PASSPORT), $name);
$recipient = Recipient::createGenericRecipient(new VatId('00000000T', VatId::VAT_ID_TYPE_PASSPORT), 'Client Name', '48270', 'Markina-Xemein', 'IE');
$recipient = Recipient::createGenericRecipient(new VatId($clientNif, $clientIdType), 'Client Name', '48270', 'Markina-Xemein', $countryCode);
return new Subject($issuer, $recipient, Subject::ISSUED_BY_THIRD_PARTY);
}

Expand Down
38 changes: 38 additions & 0 deletions tests/Barnetik/Tbai/TicketBaiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Barnetik\Tbai\PrivateKey;
use Barnetik\Tbai\TicketBai;
use Barnetik\Tbai\ValueObject\VatId;
use DOMDocument;
use DOMXPath;
use Exception;
Expand Down Expand Up @@ -240,4 +241,41 @@ public function test_gh29_TicketBai_sends_operation_date_element(): void
$operationDateValue = $xpath->evaluate('string(/T:TicketBai/Factura/DatosFactura/FechaOperacion)');
$this->assertFalse(empty($operationDateValue));
}

public function test_gh48_Greek_Vatid_prefix_EL_instead_of_GR(): void
{
$nif = $_ENV['TBAI_ARABA_ISSUER_NIF'];
$issuer = 'Test with & on issuer name';
$license = $_ENV['TBAI_ARABA_APP_LICENSE'];
$developer = $_ENV['TBAI_ARABA_APP_DEVELOPER_NIF'];
$appName = $_ENV['TBAI_ARABA_APP_NAME'];
$appVersion = $_ENV['TBAI_ARABA_APP_VERSION'];

$expectedVatId = 'EL00000000T';

$ticketbai = $this->ticketBaiMother->createTicketBaiWithForeignServices($nif, $issuer, $license, $developer, $appName, $appVersion, TicketBai::TERRITORY_ARABA, false, VatId::VAT_ID_TYPE_NIF, '00000000T', 'GR');
$xml = new DOMDocument('1.0', 'utf-8');
$ticketbai->xml($xml);
$xpath = new DOMXPath($xml);

$finalVatId = $xpath->evaluate('string(/T:TicketBai/Sujetos/Destinatarios/IDDestinatario/IDOtro/ID)');
$this->assertEquals($expectedVatId, $finalVatId);

$ticketbai = $this->ticketBaiMother->createTicketBaiWithForeignServices($nif, $issuer, $license, $developer, $appName, $appVersion, TicketBai::TERRITORY_ARABA, false, VatId::VAT_ID_TYPE_NIF, 'EL00000000T', 'GR');
$xml = new DOMDocument('1.0', 'utf-8');
$ticketbai->xml($xml);
$xpath = new DOMXPath($xml);

$finalVatId = $xpath->evaluate('string(/T:TicketBai/Sujetos/Destinatarios/IDDestinatario/IDOtro/ID)');
$this->assertEquals($expectedVatId, $finalVatId);

// Check other countries still work correctly
$ticketbai = $this->ticketBaiMother->createTicketBaiWithForeignServices($nif, $issuer, $license, $developer, $appName, $appVersion, TicketBai::TERRITORY_ARABA, false, VatId::VAT_ID_TYPE_NIF, '00000000T', 'IE');
$xml = new DOMDocument('1.0', 'utf-8');
$ticketbai->xml($xml);
$xpath = new DOMXPath($xml);

$finalVatId = $xpath->evaluate('string(/T:TicketBai/Sujetos/Destinatarios/IDDestinatario/IDOtro/ID)');
$this->assertEquals('IE00000000T', $finalVatId);
}
}

0 comments on commit 185ad57

Please sign in to comment.