fatturaelettronicaphp / FatturaElettronica

Pacchetto PHP per la lettura, la generazione e la validazione della fattura elettronica, sia per la Pubblica Amministrazione che tra privati (B2B)
https://fatturaelettronicaphp.github.io/FatturaElettronica
MIT License
40 stars 14 forks source link

PEC Validation #22

Closed rmpaolillo closed 2 years ago

rmpaolillo commented 3 years ago

Your validation code throw an exception when PEC domains contain the dash char: "-". That is still a valid email address and indeed in Italy all the PEC belonging to "ordine avvocati" have this format "xxxxx@pec-avvocatiteramo.it" "xxxx@pec-avvocatiss.it" etc.

Skullbock commented 3 years ago

Good catch. Care to send a pr for this?

rmpaolillo commented 3 years ago

Don't know how to make a PR. I guess the error is in the .xsd file that according to the new specs should be:

`

` Reference: https://www.agenziaentrate.gov.it/portale/documents/20143/2451019/Allegato+A+-+Specifiche+tecniche+vers+1.6.1.pdf/a9aa5af5-cbf7-adc5-cbcd-32a26548aa5d
Skullbock commented 2 years ago

Could be. I think we're using and older version of the XSD and I see in the document you linked that in version 1.6 they updated the email definition.

Will introduce a new test for this and update the xsd

rmpaolillo commented 2 years ago

Correct my previous statement. Regex from AdE are not well written. `

That is also stated by: https://github.com/Slamdunk/php-validatore-fattura-elettronica

Skullbock commented 2 years ago

The issue is that we need to use the XSD, since that's what the SDI will then use to validate the file. If we change that, we risk validating something that will be rejected. Will update the XSD with their latest public version to see what happens

rmpaolillo commented 2 years ago

The issue has been discussed here: https://github.com/Slamdunk/php-validatore-fattura-elettronica/issues/11#issuecomment-706079124 which is a package you have referred to somehow. BTW, it is funny that AdE does not validate dashes even if there are PEC addresses that use it in Italy.

Skullbock commented 2 years ago

@rmpaolillo just updated the XSD, added a test with an xml that has a "test-example@example.com" email address and test passes, released 2.1.0 with it.

Can you confirm? Otherwise, send here a faulty xml file so we can update our test suite with it Thanks

rmpaolillo commented 2 years ago

You should test "example@test-example.com" which still fails even with the new xsd file. For instance this adrress: "rmpaolillo@avvocato-pec.it" does not pass the test.

Skullbock commented 2 years ago

Hi, i just tested the regexp with a dashed domain and a dashed prefix and it validates correctly. Check this regexp for example: regexr.com/69mlq

XML:

<?xml version="1.0" encoding="UTF-8"?>
<p:FatturaElettronica versione="1.1" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://www.fatturapa.gov.it/sdi/fatturapa/v1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <FatturaElettronicaHeader>
    <DatiTrasmissione>
      <IdTrasmittente>
        <IdPaese>IT</IdPaese>
        <IdCodice>01234567890</IdCodice>
      </IdTrasmittente>
      <ProgressivoInvio>00001</ProgressivoInvio>
      <FormatoTrasmissione>FPR12</FormatoTrasmissione>
      <CodiceDestinatario>AAAAAA</CodiceDestinatario>
      <PECDestinatario>test-dashed-email@dashed-example.com</PECDestinatario>
      <ContattiTrasmittente/>
    </DatiTrasmissione>
    <CedentePrestatore>
      <DatiAnagrafici>
        <IdFiscaleIVA>
          <IdPaese>IT</IdPaese>
          <IdCodice>01234567890</IdCodice>
        </IdFiscaleIVA>
        <Anagrafica>
          <Denominazione>SOCIETA' ALPHA SRL</Denominazione>
        </Anagrafica>
        <RegimeFiscale>RF19</RegimeFiscale>
      </DatiAnagrafici>
      <Sede>
        <Indirizzo>VIALE ROMA 543</Indirizzo>
        <CAP>07100</CAP>
        <Comune>SASSARI</Comune>
        <Provincia>SS</Provincia>
        <Nazione>IT</Nazione>
      </Sede>
    </CedentePrestatore>
    <CessionarioCommittente>
      <DatiAnagrafici>
        <CodiceFiscale>09876543210</CodiceFiscale>
        <Anagrafica>
          <Denominazione>AMMINISTRAZIONE BETA</Denominazione>
        </Anagrafica>
      </DatiAnagrafici>
      <Sede>
        <Indirizzo>VIA TORINO 38-B</Indirizzo>
        <CAP>00145</CAP>
        <Comune>ROMA</Comune>
        <Provincia>RM</Provincia>
        <Nazione>IT</Nazione>
      </Sede>
    </CessionarioCommittente>
  </FatturaElettronicaHeader>
  <FatturaElettronicaBody>
    <DatiGenerali>
      <DatiGeneraliDocumento>
        <TipoDocumento>TD01</TipoDocumento>
        <Divisa>EUR</Divisa>
        <Data>2014-12-18</Data>
        <Numero>123</Numero>
        <Causale>LA FATTURA FA RIFERIMENTO AD UNA OPERAZIONE AAAA BBBBBBBBBBBBBBBBBB CCC DDDDDDDDDDDDDDD E FFFFFFFFFFFFFFFFFFFF GGGGGGGGGG HHHHHHH II LLLLLLLLLLLLLLLLL MMM NNNNN OO PPPPPPPPPPP QQQQ RRRR SSSSSSSSSSSSSS</Causale>
        <Causale>SEGUE DESCRIZIONE CAUSALE NEL CASO IN CUI NON SIANO STATI SUFFICIENTI 200 CARATTERI AAAAAAAAAAA BBBBBBBBBBBBBBBBB</Causale>
      </DatiGeneraliDocumento>
      <DatiOrdineAcquisto>
        <RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
        <IdDocumento>66685</IdDocumento>
        <NumItem>1</NumItem>
        <CodiceCUP>123abc</CodiceCUP>
    <CodiceCIG>456def</CodiceCIG>
      </DatiOrdineAcquisto>
      <DatiContratto>
    <RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
    <IdDocumento>123</IdDocumento>
    <Data>2012-09-01</Data>
    <NumItem>5</NumItem>
    <CodiceCUP>123abc</CodiceCUP>
    <CodiceCIG>456def</CodiceCIG>
      </DatiContratto>
      <DatiConvenzione>
    <RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
    <IdDocumento>123</IdDocumento>
    <NumItem>5</NumItem>
    <CodiceCUP>123abc</CodiceCUP>
    <CodiceCIG>456def</CodiceCIG>
      </DatiConvenzione>
      <DatiRicezione>
    <RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
    <IdDocumento>123</IdDocumento>
    <NumItem>5</NumItem>
    <CodiceCUP>123abc</CodiceCUP>
    <CodiceCIG>456def</CodiceCIG>
      </DatiRicezione>
      <DatiTrasporto>
    <DatiAnagraficiVettore>
      <IdFiscaleIVA>
        <IdPaese>IT</IdPaese>
        <IdCodice>24681012141</IdCodice>
      </IdFiscaleIVA>
          <Anagrafica>
        <Denominazione>Trasporto spa</Denominazione>
      </Anagrafica>
    </DatiAnagraficiVettore>
    <DataOraConsegna>2012-10-22T16:46:12.000+02:00</DataOraConsegna>
      </DatiTrasporto>
    </DatiGenerali>
    <DatiBeniServizi>
      <DettaglioLinee>
        <NumeroLinea>1</NumeroLinea>
        <Descrizione>LA DESCRIZIONE DELLA FORNITURA PUO' SUPERARE I CENTO CARATTERI CHE RAPPRESENTAVANO IL PRECEDENTE LIMITE DIMENSIONALE. TALE LIMITE NELLA NUOVA VERSIONE E' STATO PORTATO A MILLE CARATTERI</Descrizione>
        <Quantita>5.00</Quantita>
        <PrezzoUnitario>1.00</PrezzoUnitario>
        <PrezzoTotale>5.00</PrezzoTotale>
        <AliquotaIVA>22.00</AliquotaIVA>
      </DettaglioLinee>
      <DatiRiepilogo>
        <AliquotaIVA>22.00</AliquotaIVA>
        <ImponibileImporto>5.00</ImponibileImporto>
        <Imposta>1.10</Imposta>
        <EsigibilitaIVA>I</EsigibilitaIVA>
      </DatiRiepilogo>
    </DatiBeniServizi>
    <DatiPagamento>
      <CondizioniPagamento>TP01</CondizioniPagamento>
      <DettaglioPagamento>
        <ModalitaPagamento>MP01</ModalitaPagamento>
        <DataScadenzaPagamento>2015-01-30</DataScadenzaPagamento>
        <ImportoPagamento>6.10</ImportoPagamento>
      </DettaglioPagamento>
    </DatiPagamento>
  </FatturaElettronicaBody>
</p:FatturaElettronica>

Validation Code

$eDocument = DigitalDocument::parseFrom(__DIR__ . '/fixtures/IT01234567890_11002.xml');
$this->assertTrue($eDocument->isValid());

Which code and xml is not validating for you?

Skullbock commented 2 years ago

Update: seems to be a windows vs linux issue:

https://github.com/fatturaelettronicaphp/FatturaElettronica/runs/4235319964?check_suite_focus=true

Will investigate further

rmpaolillo commented 2 years ago

Thank you for your reply. Indeed I do not use windows for coding. My local machine is Void Linux. The virtual environment is 'laravel/homestead' version '11.5.0' PHP 8.0.1 (cli) (built: Jan 13 2021 08:22:35) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.1, Copyright (c) Zend Technologies with Zend OPcache v8.0.1, Copyright (c), by Zend Technologies

This is my composer.json file: { "name": "laravel/laravel", "type": "project", "description": "The Laravel Framework.", "keywords": ["framework", "laravel"], "license": "MIT", "require": { "php": "^7.3|^8.0", "balping/laravel-hashslug": "^2.2", "fatturaelettronicaphp/fattura-elettronica": "^2.1", "fideloper/proxy": "^4.4", "fruitcake/laravel-cors": "^2.0", "guzzlehttp/guzzle": "^7.0.1", "laravel/framework": "^8.40", "laravel/tinker": "^2.5", "livewire/livewire": "^2.6" }, "require-dev": { "facade/ignition": "^2.5", "fakerphp/faker": "^1.9.1", "laravel/sail": "^1.0.1", "mockery/mockery": "^1.4.2", "nunomaduro/collision": "^5.0", "phpunit/phpunit": "^9.3.3" }, "autoload": { "psr-4": { "App\\": "app/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/" } }, "autoload-dev": { "psr-4": { "Tests\\": "tests/" } }, "scripts": { "post-autoload-dump": [ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", "@php artisan package:discover --ansi" ], "post-root-package-install": [ "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" ], "post-create-project-cmd": [ "@php artisan key:generate --ansi" ] }, "extra": { "laravel": { "dont-discover": [] } }, "config": { "optimize-autoloader": true, "preferred-install": "dist", "sort-packages": true }, "minimum-stability": "dev", "prefer-stable": true }

Skullbock commented 2 years ago

Can you please post the php code and the xml code you're using? As you can see here the code runs fine on linux based machines as far as the test suite is concerned

rmpaolillo commented 2 years ago

` Route::get('fattura-elettronica-check/{file}', function($file) { $external_path="http://xxx.xxx.xxx.xxx/fattura_elettronica_sdc_2021/{$file}";

$contents = file_get_contents($external_path);
Storage::disk('local')->put($file, $contents);

$storagePath = Storage::disk('local')->getAdapter()->getPathPrefix();
$path = "{$storagePath}{$file}";
$eDocument = DigitalDocument::parseFrom($path);

$errors = $eDocument->validate()->errors();
if ($errors) {
    foreach ($errors as $key => $error) {
        echo '<error>'.substr($key,0,-1).'</error>';
    }
    return "<br /><result>KO</result>";
}
return "<result>OK</result>";

});

the xml file is the one you provided above.

rmpaolillo commented 2 years ago

UPDATE: SOLVED After updating my production server and restarting all services the validation passes. Thank you for your support.

Skullbock commented 2 years ago

Great! Probably either a PHP-FPM issue (the service wasn't restarted and was using old code) or an OP-CACHE issue.

Thanks a lot!