joaomfrebelo / saft-pt_4_php
PHP core lib to work with SAF-T (PT). A library/framework to generate, parse, import and validate programmatically or cli the Portuguese Tax Audit file SAF-T (PT)
Requires
- php: 8.0.* || 8.1.* || 8.2.*
- ext-dom: *
- ext-libxml: *
- ext-openssl: *
- ext-simplexml: *
- joaomfrebelo/composer-revision-plugin: ^2.0.1
- joaomfrebelo/date: ^2.2
- joaomfrebelo/decimal: ^4.0.0
- joaomfrebelo/enum: ^3.0.0
- joaomfrebelo/log4php: 3.0.0-BETA2 || ^3.0.0
- spatie/async: ^1.5
- symfony/console: ^5.1
- symfony/process: ^5.1
Requires (Dev)
- phpstan/phpstan: ^1.9.17
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.7.1
This package is auto-updated.
Last update: 2025-03-10 13:47:28 UTC
README
Rebelo SAFT-PT is a PHP library/framework to lead with the Portuguese tax audit file. The community edition lets to generate, parse, import and validate programmatically or CLI the Portuguese Tax Audit file SAFT-PT. This library also create the digital signature that is impose by law to all invoice, transport documents and working documents issued im Portugal. The enterprise edition also submit the SAFT-PT file to the Portuguese Tax Authority e-fatura web site.
Install
composer require joaomfrebelo/saft-pt_4_php
Examples
https://github.com/joaomfrebelo/Saft-PT_4_PHP/tree/master/examples
Legal documentation, portuguse tax law
https://github.com/joaomfrebelo/Saft-PT_4_PHP/tree/master/legal_documentation
Parse and validate a SAFT-PT file:
$auditFile = \Rebelo\SaftPt\AuditFile\AuditFile::loadFile("/path/to/file"); $auditFile->validate(); if ($auditFile->getErrorRegistor()->hasErrors()) { // Do stuff } else { // read and integrate data to you database ERP }
Create the signature hash
$sign = new Rebelo\SaftPt\Sign\Sign(); $sign->setPrivateKey($privatekey); $hash = $sign->createSignature($docDate, $systemEntryDate, $doc, $grossTotal, $lastHash);
Validate the hash signature
$sign = new Rebelo\SaftPt\Sign\Sign(); $sign->setPublicKey($publicKey); $isValid = $sign->verifySignature($hash, $docDate, $systemEntryDate, $doc, $grossTotal, $lastHash);
Create a SAFT-PT file
$audit = new AuditFile(); $header = $audit->getHeader(); $header->setCompanyID($companyID); $header->setTaxRegistrationNumber($taxRegistrationNumber); $header->setTaxAccountingBasis(Rebelo\SaftPt\AuditFile\TaxAccountingBasis::FACTURACAO()); $header->setCompanyName($companyName); $header->setBusinessName($businessName); (...) // Expoert to file $audit->toFile("/path/to/file");
CLI file validation
./vendor/bin/saft4php validate -g=en -p=/path/to/Public_Key.txt" -- /path/saft.xml
./vendor/bin/saft4php validate /path/saft.xml
To more options like delta, help
./vendor/bin/saft4php validate --help
Using as stand alone SAFT-PT validator
First clone or download the project
git clone https://github.com/joaomfrebelo/Saft-PT_4_PHP.git
In the project root (php >= 7.4 installed)
php saft4php validate /path/saft.xml
Some type of validations
-
XML scheme
-
Document values
-
Total values
-
Documents number sequence
-
Signature
-
Address where are mandatory
-
Dates
-
And other stuff
API documentation
https://joaomfrebelo.github.io/Saft-PT_4_PHP/
Professional support:
Features | CE | EE |
---|---|---|
Generate SAFT | yes | yes |
Parse SAFT | yes | yes |
Create hash signature | yes | yes |
Validate file programmatically | yes | yes |
Validate file CLI | yes | yes |
Validate hash signature | yes | yes |
Submit file to e-fatura | no | yes |
Create XML file in memory | yes | yes |
Create XML file with StreamWriter to save memory | no | yes |
Integration with DataSource | no | yes |
Easy to integrate with Unit test | yes | yes |
Integration with other languages (With midleware) | yes |
License
MIT License