Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve FacturX creation #31

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions src/FacturX.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class FacturX

public function __construct(string $pdfContent, ?string $xmlContent = null)
{
$this->profile = null;
$isXmlExtractedFromPdf = false;

try {
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -97,7 +98,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,
Expand All @@ -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 */
Expand All @@ -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
Expand Down Expand Up @@ -303,9 +304,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) {
Expand Down Expand Up @@ -388,11 +390,11 @@ 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);

if (!$dateItem instanceof \DOMDocument) {
if (!$dateItem instanceof \DOMElement) {
throw new \Exception('DateTimeString element is missing in XML.');
}

Expand All @@ -402,38 +404,46 @@ private function extractInvoiceInformations(\DOMDocument $document): array
throw new \Exception('DateTimeString element is missing in XML.');
}

$strToTimeDate = strtotime($date);
$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.');
}

$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.');
}

Expand Down
Loading