Skip to content

Commit

Permalink
Check DocumentType code for additional document referneces
Browse files Browse the repository at this point in the history
  • Loading branch information
ruff committed Jun 3, 2024
1 parent 0ac96fd commit 2977de6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/XmlConverterCiiToUbl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
);
}
);

Expand All @@ -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',
Expand Down
40 changes: 40 additions & 0 deletions src/XmlDocumentReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 2977de6

Please sign in to comment.