Skip to content
Merged
Show file tree
Hide file tree
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
48 changes: 48 additions & 0 deletions src/DebitNote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace NumNum\UBL;

use Doctrine\Common\Collections\ArrayCollection;
use Sabre\Xml\Reader;
use Sabre\Xml\XmlDeserializable;
use Sabre\Xml\XmlSerializable;

use function Sabre\Xml\Deserializer\mixedContent;

class DebitNote extends Invoice implements XmlSerializable, XmlDeserializable
{
public $xmlTagName = 'DebitNote';
protected $invoiceTypeCode = InvoiceTypeCode::DEBIT_NOTE;

/**
* @return DebitNoteLine[]
*/
public function getDebitNoteLines(): ?array
{
return $this->invoiceLines;
}

/**
* @param DebitNoteLine[] $debitNoteLines
* @return static
*/
public function setDebitNoteLines(array $debitNoteLines)
{
$this->invoiceLines = $debitNoteLines;
return $this;
}

/**
* The xmlDeserialize method is called during xml reading.
* @param Reader $reader
* @return static
*/
public static function xmlDeserialize(Reader $reader)
{
$mixedContent = mixedContent($reader);
$collection = new ArrayCollection($mixedContent);

return (static::deserializedTag($mixedContent))
->setDebitNoteLines(ReaderHelper::getArrayValue(Schema::CAC . 'DebitNoteLine', $collection));
}
}
48 changes: 48 additions & 0 deletions src/DebitNoteLine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace NumNum\UBL;

use function Sabre\Xml\Deserializer\mixedContent;

use Doctrine\Common\Collections\ArrayCollection;
use Sabre\Xml\Reader;

class DebitNoteLine extends InvoiceLine
{
public $xmlTagName = 'DebitNoteLine';

/**
* @return float
*/
public function getDebitedQuantity(): ?float
{
return $this->invoicedQuantity;
}

/**
* @param ?float $debitedQuantity
* @return static
*/
public function setDebitedQuantity(?float $debitedQuantity)
{
$this->invoicedQuantity = $debitedQuantity;
return $this;
}

/**
* The xmlDeserialize method is called during xml reading.
* @param Reader $xml
* @return static
*/
public static function xmlDeserialize(Reader $reader)
{
$mixedContent = mixedContent($reader);
$collection = new ArrayCollection($mixedContent);

$debitedQuantityTag = ReaderHelper::getTag(Schema::CBC . 'DebitedQuantity', $collection);

return (static::deserializedTag($mixedContent))
->setDebitedQuantity(isset($debitedQuantityTag) ? floatval($debitedQuantityTag['value']) : null)
;
}
}
17 changes: 17 additions & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,21 @@ public static function creditNote(CreditNote $creditNote, $currencyId = 'EUR')
$creditNote
]);
}

public static function debitNote(DebitNote $debitNote, $currencyId = 'EUR')
{
self::$currencyID = $currencyId;

$xmlService = new Service();

$xmlService->namespaceMap = [
'urn:oasis:names:specification:ubl:schema:xsd:' . $debitNote->xmlTagName . '-2' => '',
'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' => 'cbc',
'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' => 'cac'
];

return $xmlService->write($debitNote->xmlTagName, [
$debitNote
]);
}
}
7 changes: 5 additions & 2 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,8 @@ public function xmlSerialize(Writer $writer): void
]);
}

if ($this->invoiceTypeCode !== null) {
// DebitNote does not have a TypeCode element in UBL 2.1
if ($this->invoiceTypeCode !== null && $this->xmlTagName !== 'DebitNote') {
$writer->write([
Schema::CBC .
$this->xmlTagName .
Expand Down Expand Up @@ -930,8 +931,10 @@ public function xmlSerialize(Writer $writer): void
]);
}

// DebitNote uses RequestedMonetaryTotal instead of LegalMonetaryTotal
$monetaryTotalTagName = $this->xmlTagName === 'DebitNote' ? 'RequestedMonetaryTotal' : 'LegalMonetaryTotal';
$writer->write([
Schema::CAC . "LegalMonetaryTotal" => $this->legalMonetaryTotal,
Schema::CAC . $monetaryTotalTagName => $this->legalMonetaryTotal,
]);

foreach ($this->invoiceLines as $invoiceLine) {
Expand Down
14 changes: 13 additions & 1 deletion src/InvoiceLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ private function isCreditNoteLine(): bool
return $this->xmlTagName === 'CreditNoteLine';
}

private function isDebitNoteLine(): bool
{
return $this->xmlTagName === 'DebitNoteLine';
}

/**
* @return string
*/
Expand Down Expand Up @@ -311,8 +316,15 @@ public function xmlSerialize(Writer $writer): void
$invoicedQuantityAttributes['unitCodeListID'] = $this->getUnitCodeListId();
}

$quantityTagName = 'InvoicedQuantity';
if ($this->isCreditNoteLine()) {
$quantityTagName = 'CreditedQuantity';
} elseif ($this->isDebitNoteLine()) {
$quantityTagName = 'DebitedQuantity';
}

$writer->write([
'name' => Schema::CBC . ($this->isCreditNoteLine() ? 'CreditedQuantity' : 'InvoicedQuantity'),
'name' => Schema::CBC . $quantityTagName,
'value' => NumberFormatter::format($this->invoicedQuantity),
'attributes' => $invoicedQuantityAttributes
]);
Expand Down
2 changes: 2 additions & 0 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static function ubl($currencyId = 'EUR'): Service
$xmlService->elementMap = [
Schema::INVOICE. 'Invoice' => fn ($reader) => Invoice::xmlDeserialize($reader),
Schema::CREDITNOTE.'CreditNote' => fn ($reader) => CreditNote::xmlDeserialize($reader),
Schema::DEBITNOTE. 'DebitNote' => fn ($reader) => DebitNote::xmlDeserialize($reader),
Schema::CAC. 'AccountingCustomerParty' => fn ($reader) => AccountingParty::xmlDeserialize($reader),
Schema::CAC. 'AccountingSupplierParty' => fn ($reader) => AccountingParty::xmlDeserialize($reader),
Schema::CAC. 'AdditionalDocumentReference' => fn ($reader) => AdditionalDocumentReference::xmlDeserialize($reader),
Expand All @@ -38,6 +39,7 @@ public static function ubl($currencyId = 'EUR'): Service
Schema::CAC. 'Country' => fn ($reader) => Country::xmlDeserialize($reader),
Schema::CAC. 'DespatchDocumentReference' => fn ($reader) => DespatchDocumentReference::xmlDeserialize($reader),
Schema::CAC. 'CreditNoteLine' => fn ($reader) => CreditNoteLine::xmlDeserialize($reader),
Schema::CAC. 'DebitNoteLine' => fn ($reader) => DebitNoteLine::xmlDeserialize($reader),
Schema::CAC. 'Delivery' => fn ($reader) => Delivery::xmlDeserialize($reader),
Schema::CAC. 'FinancialInstitutionBranch' => fn ($reader) => FinancialInstitutionBranch::xmlDeserialize($reader),
Schema::CAC. 'InvoiceDocumentReference' => fn ($reader) => InvoiceDocumentReference::xmlDeserialize($reader),
Expand Down
1 change: 1 addition & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Schema
{
public const INVOICE = '{urn:oasis:names:specification:ubl:schema:xsd:Invoice-2}';
public const CREDITNOTE = '{urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2}';
public const DEBITNOTE = '{urn:oasis:names:specification:ubl:schema:xsd:DebitNote-2}';
public const CBC = '{urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2}';
public const CAC = '{urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2}';
}
122 changes: 122 additions & 0 deletions tests/Write/SimpleDebitNoteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace NumNum\UBL\Tests\Write;

use PHPUnit\Framework\TestCase;

/**
* Test an UBL2.1 debit note document
*/
class SimpleDebitNoteTest extends TestCase
{
private $schema = 'http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-DebitNote-2.1.xsd';

/** @test */
public function testIfXMLIsValid()
{
// Address country
$country = (new \NumNum\UBL\Country())
->setIdentificationCode('BE');

// Full address
$address = (new \NumNum\UBL\Address())
->setStreetName('Korenmarkt')
->setBuildingNumber(1)
->setCityName('Gent')
->setPostalZone('9000')
->setCountry($country);

// Supplier company node
$supplierCompany = (new \NumNum\UBL\Party())
->setName('Supplier Company Name')
->setPhysicalLocation($address)
->setPostalAddress($address);

// Client company node
$clientCompany = (new \NumNum\UBL\Party())
->setName('My client')
->setPostalAddress($address);

$legalMonetaryTotal = (new \NumNum\UBL\LegalMonetaryTotal())
->setPayableAmount(10 + 2)
->setAllowanceTotalAmount(0);

// Tax scheme
$taxScheme = (new \NumNum\UBL\TaxScheme())
->setId(0);

// Product
$productItem = (new \NumNum\UBL\Item())
->setName('Product Name')
->setDescription('Product Description')
->setSellersItemIdentification('SELLERID')
->setBuyersItemIdentification('BUYERID');

// Price
$price = (new \NumNum\UBL\Price())
->setBaseQuantity(1)
->setUnitCode(\NumNum\UBL\UnitCode::UNIT)
->setPriceAmount(10);

// Invoice Line tax totals
$lineTaxTotal = (new \NumNum\UBL\TaxTotal())
->setTaxAmount(2.1);

// Debit Note Line(s)
$debitNoteLine = (new \NumNum\UBL\DebitNoteLine())
->setId(0)
->setItem($productItem)
->setPrice($price)
->setTaxTotal($lineTaxTotal)
->setDebitedQuantity(1);

$debitNoteLines = [$debitNoteLine];

// Total Taxes
$taxCategory = (new \NumNum\UBL\TaxCategory())
->setId(0)
->setName('VAT21%')
->setPercent(.21)
->setTaxScheme($taxScheme);

$taxSubTotal = (new \NumNum\UBL\TaxSubTotal())
->setTaxableAmount(10)
->setTaxAmount(2.1)
->setTaxCategory($taxCategory);

$taxTotal = (new \NumNum\UBL\TaxTotal())
->addTaxSubTotal($taxSubTotal)
->setTaxAmount(2.1);

$accountingSupplierParty = (new \NumNum\UBL\AccountingParty())
->setParty($supplierCompany);

$accountingCustomerParty = (new \NumNum\UBL\AccountingParty())
->setParty($clientCompany);

// Debit Note object
$debitNote = (new \NumNum\UBL\DebitNote())
->setId(1234)
->setCopyIndicator(false)
->setIssueDate(new \DateTime())
->setAccountingSupplierParty($accountingSupplierParty)
->setAccountingCustomerParty($accountingCustomerParty)
->setDebitNoteLines($debitNoteLines)
->setLegalMonetaryTotal($legalMonetaryTotal)
->setTaxTotal($taxTotal);

// Test created object
// Use \NumNum\UBL\Generator to generate an XML string
$generator = new \NumNum\UBL\Generator();
$outputXMLString = $generator->debitNote($debitNote);

// Create PHP Native DomDocument object, that can be
// used to validate the generate XML
$dom = new \DOMDocument();
$dom->loadXML($outputXMLString);

$dom->save('./tests/SimpleDebitNoteTest.xml');

$this->assertEquals(true, $dom->schemaValidate($this->schema));
}
}