Skip to content

Commit

Permalink
Allow specifying Line ID (BT-126)
Browse files Browse the repository at this point in the history
This commit allows for specifying the ID of a InvoiceLine. Fallback to the line index if not ID is specified.
  • Loading branch information
hmazter committed Nov 15, 2021
1 parent 30751aa commit f7b3c6e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/InvoiceLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class InvoiceLine {
protected $unit = "C62"; // TODO: add constants
protected $price = null;
protected $baseQuantity = 1;
protected $lineId = null;

use AllowanceOrChargeTrait;
use AttributesTrait;
Expand Down Expand Up @@ -332,4 +333,22 @@ public function getNetAmount(int $decimals=Invoice::DEFAULT_DECIMALS): ?float {
$netAmount += $this->getChargesAmount($decimals);
return $netAmount;
}

/**
* Get Invoice line identifier
* @return string|null
*/
public function getLineId(): ?string {
return $this->lineId;
}

/**
* Set Invoice line identifier
* @param string $lineId The ID for the Line
* @return self Invoice line instance
*/
public function setLineId(string $lineId): self {
$this->lineId = $lineId;
return $this;
}
}
6 changes: 6 additions & 0 deletions src/Readers/UblReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,12 @@ private function parseInvoiceLine(UXML $xml, array &$taxExemptions): InvoiceLine
$cac = UblWriter::NS_CAC;
$cbc = UblWriter::NS_CBC;

// BT-126: Line ID
$lineId = $xml->get("{{$cbc}}ID");
if ($lineId) {
$line->setLineId($lineId->asText());
}

// BT-127: Invoice line note
$noteNode = $xml->get("{{$cbc}}Note");
if ($noteNode !== null) {
Expand Down
2 changes: 1 addition & 1 deletion src/Writers/UblWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ private function addLineNode(UXML $parent, InvoiceLine $line, int $index, Invoic
$xml = $parent->add('cac:InvoiceLine');

// BT-126: Line ID
$xml->add('cbc:ID', (string) $index);
$xml->add('cbc:ID', $line->getLineId() ?? (string) $index);

// BT-127: Invoice line note
$note = $line->getNote();
Expand Down
1 change: 1 addition & 0 deletions tests/Writers/UblWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private function getSampleInvoice(): Invoice {
->addLine($complexLine)
->addLine((new InvoiceLine)->setName('Line #2')->setPrice(40, 2)->setVatRate(21)->setQuantity(4))
->addLine((new InvoiceLine)->setName('Line #3')->setPrice(0.56)->setVatRate(10)->setQuantity(2))
->addLine((new InvoiceLine)->setLineId('5')->setName('Line #4')->setPrice(0.56)->setVatRate(10)->setQuantity(2))
->addAllowance((new AllowanceOrCharge)->setReason('5% discount')->setAmount(5)->markAsPercentage()->setVatRate(21))
->addAttachment((new Attachment)->setId(new Identifier('INV-123', 'ABT')))
->addAttachment($externalAttachment)
Expand Down

0 comments on commit f7b3c6e

Please sign in to comment.