Skip to content

Commit

Permalink
Added support to attachements ( UBL )
Browse files Browse the repository at this point in the history
  • Loading branch information
rahal committed Jan 17, 2024
1 parent 3e2b529 commit c9cda25
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
18 changes: 18 additions & 0 deletions src/FacturX.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Factur-X Xml Stuff
use Easybill\ZUGFeRD211\Builder;
use Easybill\ZUGFeRD211\Model\Amount;
use Easybill\ZUGFeRD211\Model\BinaryObject;
use Easybill\ZUGFeRD211\Model\CreditorFinancialAccount;
use Easybill\ZUGFeRD211\Model\CreditorFinancialInstitution;
use Easybill\ZUGFeRD211\Model\CrossIndustryInvoice;
Expand All @@ -25,6 +26,7 @@
use Easybill\ZUGFeRD211\Model\Note;
use Easybill\ZUGFeRD211\Model\Quantity;

use Easybill\ZUGFeRD211\Model\ReferencedDocument;
use Easybill\ZUGFeRD211\Model\SupplyChainEvent;
use Easybill\ZUGFeRD211\Model\SupplyChainTradeLineItem;
use Easybill\ZUGFeRD211\Model\SupplyChainTradeTransaction;
Expand Down Expand Up @@ -366,4 +368,20 @@ public function addNote(string $content, ?string $subjectCode = null, ?string $c
{
$this->invoice->exchangedDocument->notes[] = Note::create($content, $subjectCode, $contentCode);
}

public function addEmbeddedAttachment( ?string $id, ?string $scheme, ?string $filename, ?string $contents, ?string $mimeCode, ?string $description ){
// The attachement is correctly added but schematron fails, need to
// $attachment = ReferencedDocument::create($id);
// $attachment->name = $description;
// $binary = new BinaryObject();
// $binary->filename=$filename;
// $binary->mimeCode=$mimeCode;
// $binary->value=base64_encode($contents);
// $attachment->attachmentBinaryObject = $binary;

// switch($scheme) {
// // TODO, depending on the type of documents it could be attached
// default : $this->invoice->supplyChainTradeTransaction->applicableHeaderTradeSettlement->invoiceReferencedDocument = $attachment;
// }
}
}
4 changes: 4 additions & 0 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,8 @@ public function getPdf($pdf , $addFacturxLogo = false )
$addFacturxLogo );
}

public function addEmbeddedAttachment( ?string $id, ?string $scheme, ?string $filename, ?string $contents, ?string $mimeCode, ?string $description ){
$this->xmlGenerator->addEmbeddedAttachment( $id,$scheme, $filename, $contents, $mimeCode, $description );
}

}
24 changes: 23 additions & 1 deletion src/Ubl.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DigitalInvoice;

use DateTime;
use Einvoicing\Attachment;
use Einvoicing\Delivery;
use Einvoicing\Exceptions\ValidationException;
use Einvoicing\Identifier;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function validate(string $xml, $schematron)
* @return boolean
*/
protected function euValidation(string $contents, string $type) {

$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://www.itb.ec.europa.eu/vitb/rest/invoice/api/validate',
Expand Down Expand Up @@ -189,4 +190,25 @@ public function addPaymentMean(PaymentMeansCode $typeCode, ?string $ibanId = nul

$payment->addTransfer($transfer);
}

public function addEmbeddedAttachment( ?string $id, ?string $scheme, ?string $filename, ?string $contents, ?string $mimeCode, ?string $description ){
// not implemented
$embeddedAttachment = new Attachment();
if($id){
$embeddedAttachment->setId( new Identifier($id, $scheme) );
}
if($filename){
$embeddedAttachment->setFilename($filename );
}
if($contents){
$embeddedAttachment->setContents($contents );
}
if($mimeCode){
$embeddedAttachment->setMimeCode($mimeCode );
}
if($description){
$embeddedAttachment->setDescription($description );
}
$this->invoice->addAttachment($embeddedAttachment);
}
}
7 changes: 7 additions & 0 deletions src/XmlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function setBuyerAddress(string $lineOne, string $postCode, string $city,
public function addItem(string $name, float $price, float $taxRatePercent, float $quantity, UnitOfMeasurement $unit, ?string $globalID = null, ?string $globalIDCode =null ): float;
public function addNote(string $content, ?string $subjectCode = null, ?string $contentCode = null);
public function addPaymentMean(PaymentMeansCode $typeCode , ?string $ibanId = null,?string $accountName = null, ?string $bicId = null);
public function addEmbeddedAttachment( ?string $id, ?string $scheme, ?string $filename, ?string $contents, ?string $mimeCode, ?string $description );
}


Expand Down Expand Up @@ -76,6 +77,12 @@ public function addTaxLine($rate , $totalLineBasis ){

}

public function addEmbeddedAttachment( ?string $id, ?string $scheme, ?string $filename, ?string $contents, ?string $mimeCode, ?string $description ){
// not implemented
}






Expand Down
25 changes: 15 additions & 10 deletions test/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ public function profilesProvider()
[Zugferd::ZUGFERD_BASIC, true],
[Zugferd::ZUGFERD_CONFORT, true],
[Zugferd::ZUGFERD_EXTENDED, true],
[FacturX::XRECHNUNG, false],
[Ubl::PEPPOL, false],
[Ubl::NLCIUS, false],
[Ubl::CIUS_RO, false],
[Ubl::CIUS_IT, false],
[Ubl::CIUS_ES_FACE, false],
[Ubl::CIUS_AT_GOV, false],
[Ubl::CIUS_AT_NAT, false]
[FacturX::XRECHNUNG, false, false],
[Ubl::PEPPOL, false , true],
[Ubl::NLCIUS, false, true],
[Ubl::CIUS_RO, false, true],
[Ubl::CIUS_IT, false, true],
[Ubl::CIUS_ES_FACE, false, true],
[Ubl::CIUS_AT_GOV, false, true],
[Ubl::CIUS_AT_NAT, false, true]
];
}

/**
* @dataProvider profilesProvider
*/
public function testInvoiceXml($profile, $isPdf): void
public function testInvoiceXml($profile, $isPdf, $embedPdf = false ): void
{
$invoice = new Invoice('123', new \Datetime('2023-11-07'), null , CurrencyCode::EURO , $profile);

Expand Down Expand Up @@ -81,6 +81,12 @@ public function testInvoiceXml($profile, $isPdf): void

// add payment
$invoice->addPaymentMean('58','MA2120300000000202051' , 'Youniwemi');

// Embedding pdf
if ($embedPdf){
$pdfFile = file_get_contents(__DIR__.'/examples/basic.pdf');
$invoice->addEmbeddedAttachment('123', null, 'basic', $pdfFile, 'application/pdf', 'The pdf invoice');
}

$xml = $invoice->getXml();
self::assertNotEmpty($xml);
Expand All @@ -103,7 +109,6 @@ public function testInvoiceXml($profile, $isPdf): void
} catch (\Exception $e) {
$this->fail('Error extractiong xml '. $e->getMessage());
}

}

// A complete validation using schematron
Expand Down

0 comments on commit c9cda25

Please sign in to comment.