Skip to content

Commit

Permalink
Fixes to UBL classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Stormyy committed Jan 13, 2018
1 parent 3c4ec1d commit 8b98ed2
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 49 deletions.
61 changes: 31 additions & 30 deletions src/classes/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Invoice implements XmlSerializable{
*/
private $accountingCustomerParty;
/**
* @var TaxTotal[]
* @var TaxTotal
*/
private $taxTotal;
/**
Expand All @@ -59,81 +59,82 @@ class Invoice implements XmlSerializable{
private $allowanceCharges;


function validate(){
if($this->id === null){
function validate()
{
if ($this->id === null) {
throw new \InvalidArgumentException('Missing invoice id');
}

if($this->id === null){
if ($this->id === null) {
throw new \InvalidArgumentException('Missing invoice id');
}

if(!$this->issueDate instanceof \DateTime){
if (!$this->issueDate instanceof \DateTime) {
throw new \InvalidArgumentException('Invalid invoice issueDate');
}

if($this->invoiceTypeCode === null){
if ($this->invoiceTypeCode === null) {
throw new \InvalidArgumentException('Missing invoice invoiceTypeCode');
}

if($this->accountingSupplierParty === null){
if ($this->accountingSupplierParty === null) {
throw new \InvalidArgumentException('Missing invoice accountingSupplierParty');
}

if($this->accountingCustomerParty === null){
if ($this->accountingCustomerParty === null) {
throw new \InvalidArgumentException('Missing invoice accountingCustomerParty');
}

if($this->invoiceLines === null){
if ($this->invoiceLines === null) {
throw new \InvalidArgumentException('Missing invoice lines');
}

if($this->legalMonetaryTotal === null){
if ($this->legalMonetaryTotal === null) {
throw new \InvalidArgumentException('Missing invoice LegalMonetaryTotal');
}

if($this->taxTotal === null){
if ($this->taxTotal === null) {
throw new \InvalidArgumentException('Missing invoice taxTotal');
}
}

function xmlSerialize(Writer $writer) {
function xmlSerialize(Writer $writer)
{
$cbc = '{urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2}';
$cac = '{urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2}';

$this->validate();

$writer->write([
$cbc.'UBLVersionID' => $this->UBLVersionID,
$cbc.'ID' => $this->id,
$cbc.'CopyIndicator' => $this->copyIndicator ? 'true' : 'false',
$cbc.'IssueDate' => $this->issueDate->format('Y-m-d'),
$cbc.'InvoiceTypeCode' => $this->invoiceTypeCode,
$cac.'AccountingSupplierParty' => [$cac."Party" => $this->accountingSupplierParty],
$cac.'AccountingCustomerParty' => [$cac."Party" => $this->accountingCustomerParty],
$cbc . 'UBLVersionID' => $this->UBLVersionID,
$cbc . 'CustomizationID' => 'OIOUBL-2.01',
$cbc . 'ID' => $this->id,
$cbc . 'CopyIndicator' => $this->copyIndicator ? 'true' : 'false',
$cbc . 'IssueDate' => $this->issueDate->format('Y-m-d'),
$cbc . 'InvoiceTypeCode' => $this->invoiceTypeCode,
$cac . 'AccountingSupplierParty' => [$cac . "Party" => $this->accountingSupplierParty],
$cac . 'AccountingCustomerParty' => [$cac . "Party" => $this->accountingCustomerParty],
]);

if($this->allowanceCharges != null) {
if ($this->allowanceCharges != null) {
foreach ($this->allowanceCharges as $invoiceLine) {
$writer->write([
Schema::CAC . 'AllowanceCharge' => $invoiceLine
]);
}
}

foreach($this->taxTotal as $taxTotal){
$writer->write([
Schema::CAC.'TaxTotal' => $taxTotal
]);
}
$writer->write([
Schema::CAC . 'TaxTotal' => $this->taxTotal
]);

$writer->write([
$cac.'LegalMonetaryTotal' => $this->legalMonetaryTotal
$cac . 'LegalMonetaryTotal' => $this->legalMonetaryTotal
]);

foreach($this->invoiceLines as $invoiceLine){
foreach ($this->invoiceLines as $invoiceLine) {
$writer->write([
Schema::CAC.'InvoiceLine' => $invoiceLine
Schema::CAC . 'InvoiceLine' => $invoiceLine
]);
}

Expand Down Expand Up @@ -236,14 +237,14 @@ public function setAccountingCustomerParty($accountingCustomerParty) {
}

/**
* @return TaxTotal[]
* @return TaxTotal
*/
public function getTaxTotal() {
return $this->taxTotal;
}

/**
* @param TaxTotal[] $taxTotal
* @param TaxTotal $taxTotal
* @return Invoice
*/
public function setTaxTotal($taxTotal) {
Expand Down
2 changes: 1 addition & 1 deletion src/classes/InvoiceLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function xmlSerialize(Writer $writer) {
],
[
'name' => Schema::CBC . 'LineExtensionAmount',
'value' => $this->lineExtensionAmount,
'value' => number_format($this->lineExtensionAmount,2),
'attributes' => [
'currencyID' => Generator::$currencyID
]
Expand Down
8 changes: 4 additions & 4 deletions src/classes/LegalMonetaryTotal.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,31 @@ function xmlSerialize(Writer $writer) {
$writer->write([
[
'name' => Schema::CBC . 'LineExtensionAmount',
'value' => $this->lineExtensionAmount,
'value' => number_format($this->lineExtensionAmount, 2),
'attributes' => [
'currencyID' => Generator::$currencyID
]

],
[
'name' => Schema::CBC . 'TaxExclusiveAmount',
'value' => $this->taxExclusiveAmount,
'value' => number_format($this->taxExclusiveAmount, 2),
'attributes' => [
'currencyID' => Generator::$currencyID
]

],
[
'name' => Schema::CBC . 'AllowanceTotalAmount',
'value' => $this->allowanceTotalAmount,
'value' => number_format($this->allowanceTotalAmount, 2),
'attributes' => [
'currencyID' => Generator::$currencyID
]

],
[
'name' => Schema::CBC . 'PayableAmount',
'value' => $this->payableAmount,
'value' => number_format($this->payableAmount,2),
'attributes' => [
'currencyID' => Generator::$currencyID
]
Expand Down
5 changes: 4 additions & 1 deletion src/classes/TaxCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ function xmlSerialize(Writer $writer) {
Schema::CBC.'ID' => $this->id,
Schema::CBC.'Name' => $this->name,
Schema::CBC.'Percent' => $this->percent,
Schema::CAC.'TaxScheme' => $this->taxScheme
]);

if($this->taxScheme != null){
$writer->write([Schema::CAC.'TaxScheme' => $this->taxScheme]);
}
}
}
2 changes: 1 addition & 1 deletion src/classes/TaxScheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getId() {

/**
* @param mixed $id
* @return TaxCategory
* @return int
*/
public function setId($id) {
$this->id = $id;
Expand Down
4 changes: 2 additions & 2 deletions src/classes/TaxSubTotal.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ function xmlSerialize(Writer $writer) {
$writer->write([
[
'name' => Schema::CBC . 'TaxableAmount',
'value' => $this->taxableAmount,
'value' => number_format($this->taxableAmount, 2),
'attributes' => [
'currencyID' => Generator::$currencyID
]

],
[
'name' => Schema::CBC . 'TaxAmount',
'value' => $this->taxAmount,
'value' => number_format($this->taxAmount, 2),
'attributes' => [
'currencyID' => Generator::$currencyID
]
Expand Down
7 changes: 5 additions & 2 deletions src/classes/TaxTotal.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ function xmlSerialize(Writer $writer) {
$writer->write([
[
'name' => Schema::CBC . 'TaxAmount',
'value' => $this->taxAmount,
'value' => number_format($this->taxAmount,2),
'attributes' => [
'currencyID' => Generator::$currencyID
]
],
Schema::CAC . 'TaxSubtotal' => $this->taxSubTotal
]);

foreach($this->taxSubTotals as $taxSubTotal){
$writer->write([Schema::CAC . 'TaxSubtotal' => $taxSubTotal]);
}
}
}
21 changes: 13 additions & 8 deletions tests/test.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
require_once "vendor/autoload.php";
require_once "../vendor/autoload.php";

$xmlService = new Sabre\Xml\Service();

Expand Down Expand Up @@ -32,14 +32,21 @@
$invoice->setAccountingCustomerParty($accountingSupplierParty);

$taxtotal = (new \CleverIt\UBL\Invoice\TaxTotal())
->setTaxAmount(10)
->setTaxSubTotal((new \CleverIt\UBL\Invoice\TaxSubTotal())
->setTaxAmount(10)
->setTaxAmount(30)
->addTaxSubTotal((new \CleverIt\UBL\Invoice\TaxSubTotal())
->setTaxAmount(21)
->setTaxableAmount(100)
->setTaxCategory((new \CleverIt\UBL\Invoice\TaxCategory())
->setId("H")
->setName("NL, Hoog Tarief")
->setPercent(21.00)));
->setPercent(21.00)))
->addTaxSubTotal((new \CleverIt\UBL\Invoice\TaxSubTotal())
->setTaxAmount(9)
->setTaxableAmount(100)
->setTaxCategory((new \CleverIt\UBL\Invoice\TaxCategory())
->setId("X")
->setName("NL, Laag Tarief")
->setPercent(9.00)));

$invoiceLine = (new \CleverIt\UBL\Invoice\InvoiceLine())
->setId(1)
Expand All @@ -57,6 +64,4 @@
->setAllowanceTotalAmount(50));


file_put_contents("ubl.xml", $xmlService->write('Invoice', [
$invoice
]));
file_put_contents("ubl.xml", \CleverIt\UBL\Invoice\Generator::invoice($invoice, 'EUR'));
Loading

0 comments on commit 8b98ed2

Please sign in to comment.