From 2977de648152fe2d0c68c1ac12a1e3dee3d0e126 Mon Sep 17 00:00:00 2001 From: ruff Date: Mon, 3 Jun 2024 05:44:12 +0200 Subject: [PATCH] Check DocumentType code for additional document referneces --- src/XmlConverterCiiToUbl.php | 18 ++++++++++------ src/XmlDocumentReader.php | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/XmlConverterCiiToUbl.php b/src/XmlConverterCiiToUbl.php index a0c493f..62412da 100644 --- a/src/XmlConverterCiiToUbl.php +++ b/src/XmlConverterCiiToUbl.php @@ -357,11 +357,13 @@ function ($nodeFound) { $this->in->queryValues('.//ram:AdditionalReferencedDocument', $invoiceHeaderAgreement)->forEach( function ($nodeFound) { - if ($this->in->queryValue('.//ram:TypeCode', $nodeFound) == '50') { - $this->out->startElement('cac:OriginatorDocumentReference'); - $this->out->element('cbc:ID', $this->in->queryValue('.//ram:IssuerAssignedID', $nodeFound)); - $this->out->endElement(); - } + $this->in->whenEquals( + './/ram:TypeCode', $nodeFound, '50', function () use ($nodeFound) { + $this->out->startElement('cac:OriginatorDocumentReference'); + $this->out->element('cbc:ID', $this->in->queryValue('.//ram:IssuerAssignedID', $nodeFound)); + $this->out->endElement(); + } + ); } ); @@ -378,7 +380,11 @@ function ($additionalReferencedDocumentNode) { if ($this->in->queryValue('.//ram:TypeCode', $additionalReferencedDocumentNode) != '50') { $this->out->startElement('cac:AdditionalDocumentReference'); $this->out->element('cbc:ID', $this->in->queryValue('.//ram:IssuerAssignedID', $additionalReferencedDocumentNode)); - //$this->out->element('cbc:DocumentTypeCode', $this->in->queryValue('.//ram:TypeCode', $additionalReferencedDocumentNode)); + $this->in->whenEquals( + './/ram:TypeCode', $additionalReferencedDocumentNode, '130', function () use ($additionalReferencedDocumentNode) { + $this->out->element('cbc:DocumentTypeCode', $this->in->queryValue('.//ram:TypeCode', $additionalReferencedDocumentNode)); + } + ); $this->out->element('cbc:DocumentDescription', $this->in->queryValue('.//ram:Name', $additionalReferencedDocumentNode)); $this->in->whenExists( './/ram:AttachmentBinaryObject', diff --git a/src/XmlDocumentReader.php b/src/XmlDocumentReader.php index fc53fb5..2dc9efe 100644 --- a/src/XmlDocumentReader.php +++ b/src/XmlDocumentReader.php @@ -212,4 +212,44 @@ public function whenExists(string $expression, ?DOMNode $contextNode, $callback, return $this; } + + /** + * When an element equals value(s) the $callback is called + * + * @param string $expression + * @param DOMNode|null $contextNode + * @param string|string[] $values + * @param callable $callback + * @param callable|null $callbackElse + * @return XmlDocumentReader + */ + public function whenEquals(string $expression, ?DOMNode $contextNode, $values, $callback, $callbackElse = null): XmlDocumentReader + { + if (!is_array($values)) { + $values = [$values]; + } + + $equals = false; + + foreach ($values as $value) { + if ($this->queryValue($expression, $contextNode) === $value) { + $equals = true; + break; + } + } + + if ($equals === true) { + call_user_func( + $callback, + $this->query($expression, $contextNode)->item(0), + $this->query($expression, $contextNode)->item(0)->parentNode + ); + } else { + if (!is_null($callbackElse)) { + call_user_func($callbackElse); + } + } + + return $this; + } }