From 542fa48e7b77075f85d4428d4b8c527a94d98e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Pillevesse?= Date: Thu, 17 Aug 2023 10:36:13 +0200 Subject: [PATCH 1/3] fix facturx creation --- src/FacturX.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/FacturX.php b/src/FacturX.php index d121322..3565856 100644 --- a/src/FacturX.php +++ b/src/FacturX.php @@ -97,7 +97,7 @@ public function getPdfContent(): string sprintf( '%s%s%s', __DIR__, - '/../vendor/atgp/factur-x/img/', + '/../../../../vendor/atgp/factur-x/img/', \Atgp\FacturX\Facturx::FACTURX_LOGO[$this->profile] ), 197, @@ -303,9 +303,10 @@ private function updatePdfMetadata(FdpiFacturx $pdfWriter, \DOMDocument $documen $xmp = simplexml_load_file(sprintf( '%s%s%s', __DIR__, - '/../vendor/atgp/factur-x/xmp/', + '/../../../../vendor/atgp/factur-x/xmp/', \Atgp\FacturX\Facturx::FACTURX_XMP )); + $descriptionElements = $xmp->xpath('rdf:Description'); if (!$descriptionElements) { @@ -392,7 +393,7 @@ private function extractInvoiceInformations(\DOMDocument $document): array $dateElements = $xpath->query('//rsm:ExchangedDocument/ram:IssueDateTime/udt:DateTimeString'); $dateItem = $dateElements->item(0); - if (!$dateItem instanceof \DOMDocument) { + if (!$dateItem instanceof \DOMElement) { throw new \Exception('DateTimeString element is missing in XML.'); } @@ -402,38 +403,40 @@ private function extractInvoiceInformations(\DOMDocument $document): array throw new \Exception('DateTimeString element is missing in XML.'); } - $strToTimeDate = strtotime($date); + $dateObject = (\DateTime::createFromFormat('Ymd', $date))->format('Y-m-d'); + $strToTimeDate = strtotime($dateObject); + if (!$strToTimeDate) { throw new \Exception('DateTimeString element is malformed in XML.'); } $dateReformatted = date('Y-m-d\TH:i:s', $strToTimeDate) . '+00:00'; - /** @var \DOMNodeList<\DOMDocument> $invoiceIdentifierElements */ + /** @var \DOMNodeList<\DOMElement> $invoiceIdentifierElements */ $invoiceIdentifierElements = $xpath->query('//rsm:ExchangedDocument/ram:ID'); $invoiceIdentifierItem = $invoiceIdentifierElements->item(0); - if (!$invoiceIdentifierItem instanceof \DOMDocument) { + if (!$invoiceIdentifierItem instanceof \DOMElement) { throw new \Exception('Invoice ID element is missing in XML.'); } $invoiceIdentifier = $invoiceIdentifierItem->nodeValue; - /** @var \DOMNodeList<\DOMDocument> $sellerElements */ + /** @var \DOMNodeList<\DOMElement> $sellerElements */ $sellerElements = $xpath->query('//ram:ApplicableHeaderTradeAgreement/ram:SellerTradeParty/ram:Name'); $sellerItem = $sellerElements->item(0); - if (!$sellerItem instanceof \DOMDocument) { + if (!$sellerItem instanceof \DOMElement) { throw new \Exception('TypeCode element is missing in XML.'); } $seller = $sellerItem->nodeValue; - /** @var \DOMNodeList<\DOMDocument> $docTypeElements */ + /** @var \DOMNodeList<\DOMElement> $docTypeElements */ $docTypeElements = $xpath->query('//rsm:ExchangedDocument/ram:TypeCode'); $docTypeItem = $docTypeElements->item(0); - if (!$docTypeItem instanceof \DOMDocument) { + if (!$docTypeItem instanceof \DOMElement) { throw new \Exception('TypeCode element is missing in XML.'); } From adf3ba8010aa9bd13858337122ba596d1ef8d555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Pillevesse?= Date: Thu, 17 Aug 2023 10:38:04 +0200 Subject: [PATCH 2/3] fix facturx creation --- src/FacturX.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/FacturX.php b/src/FacturX.php index 3565856..f3e925c 100644 --- a/src/FacturX.php +++ b/src/FacturX.php @@ -27,6 +27,7 @@ class FacturX public function __construct(string $pdfContent, ?string $xmlContent = null) { + $this->profile = null; $isXmlExtractedFromPdf = false; try { @@ -69,7 +70,6 @@ public function __construct(string $pdfContent, ?string $xmlContent = null) $this->isXmlExtractedFromPdf = $isXmlExtractedFromPdf; $this->addFacturXLogo = false; $this->attachments = []; - $this->profile = null; } public function getXmlContent(): string @@ -83,6 +83,7 @@ public function getPdfContent(): string $document->loadXML($this->xmlContent); $pdfStreamReader = StreamReader::createByString($this->pdfContent); + $xmlStreamReader= StreamReader::createByString($this->xmlContent); $pdfWriter = new FdpiFacturx(); $pageCount = $pdfWriter->setSourceFile($pdfStreamReader); @@ -108,7 +109,7 @@ public function getPdfContent(): string } if (!$this->isXmlExtractedFromPdf) { - $pdfWriter->Attach($this->xmlContent, self::FACTURX_FILENAME, 'Factur-X Invoice', 'Data', 'text#2Fxml'); + $pdfWriter->Attach($xmlStreamReader, self::FACTURX_FILENAME, 'Factur-X Invoice', 'Data', 'text#2Fxml'); } /** @var FacturXAttachment $attachment */ @@ -129,7 +130,7 @@ public function getPdfContent(): string $pdfWriter->SetPDFVersion('1.7', true); $pdfWriter = $this->updatePdfMetadata($pdfWriter, $document); - return $pdfWriter->Output('invoice-facturx-' . date('Ymdhis') . '.pdf', 'S'); + return $pdfWriter->Output( 'S', 'invoice-facturx-' . date('Ymdhis') . '.pdf'); } public function addFacturxLogo(): void From e1c7df2590e8a5d8d11bc70cb0ae85ad0ef412e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Pillevesse?= Date: Thu, 17 Aug 2023 11:59:31 +0200 Subject: [PATCH 3/3] fix phpstan --- src/FacturX.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/FacturX.php b/src/FacturX.php index f3e925c..2b19b4b 100644 --- a/src/FacturX.php +++ b/src/FacturX.php @@ -83,7 +83,7 @@ public function getPdfContent(): string $document->loadXML($this->xmlContent); $pdfStreamReader = StreamReader::createByString($this->pdfContent); - $xmlStreamReader= StreamReader::createByString($this->xmlContent); + $xmlStreamReader = StreamReader::createByString($this->xmlContent); $pdfWriter = new FdpiFacturx(); $pageCount = $pdfWriter->setSourceFile($pdfStreamReader); @@ -130,7 +130,7 @@ public function getPdfContent(): string $pdfWriter->SetPDFVersion('1.7', true); $pdfWriter = $this->updatePdfMetadata($pdfWriter, $document); - return $pdfWriter->Output( 'S', 'invoice-facturx-' . date('Ymdhis') . '.pdf'); + return $pdfWriter->Output('S', 'invoice-facturx-' . date('Ymdhis') . '.pdf'); } public function addFacturxLogo(): void @@ -390,7 +390,7 @@ private function extractInvoiceInformations(\DOMDocument $document): array { $xpath = new \DOMXpath($document); - /** @var \DOMNodeList<\DOMDocument> $dateElements */ + /** @var \DOMNodeList<\DOMElement> $dateElements */ $dateElements = $xpath->query('//rsm:ExchangedDocument/ram:IssueDateTime/udt:DateTimeString'); $dateItem = $dateElements->item(0); @@ -404,8 +404,14 @@ private function extractInvoiceInformations(\DOMDocument $document): array throw new \Exception('DateTimeString element is missing in XML.'); } - $dateObject = (\DateTime::createFromFormat('Ymd', $date))->format('Y-m-d'); - $strToTimeDate = strtotime($dateObject); + $dateObject = \DateTime::createFromFormat('Ymd', $date); + + if (!$dateObject) { + throw new \Exception('DateTimeString element is malformed.'); + } + + $formattedDateObject = $dateObject->format('Y-m-d'); + $strToTimeDate = strtotime($formattedDateObject); if (!$strToTimeDate) { throw new \Exception('DateTimeString element is malformed in XML.');