1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Jobs/ConvertInvoiceToUbl.php

148 lines
5.0 KiB
PHP
Raw Normal View History

2018-01-11 08:58:43 +01:00
<?php
namespace App\Jobs;
use App\Jobs\Job;
use Sabre\Xml\Service;
use CleverIt\UBL\Invoice\Invoice;
use CleverIt\UBL\Invoice\Party;
use CleverIt\UBL\Invoice\Address;
use CleverIt\UBL\Invoice\Country;
use CleverIt\UBL\Invoice\Contact;
use CleverIt\UBL\Invoice\TaxTotal;
use CleverIt\UBL\Invoice\TaxSubTotal;
use CleverIt\UBL\Invoice\TaxCategory;
use CleverIt\UBL\Invoice\InvoiceLine;
use CleverIt\UBL\Invoice\Item;
use CleverIt\UBL\Invoice\LegalMonetaryTotal;
class ConvertInvoiceToUbl extends Job
{
public function __construct($invoice)
{
$this->invoice = $invoice;
}
public function handle()
{
$xmlService = new Service();
$xmlService->namespaceMap = [
'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' => '',
'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' => 'cbc',
'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' => 'cac'
];
$invoice = $this->invoice;
$account = $invoice->account;
$client = $invoice->client;
$ublInvoice = new Invoice();
// invoice
$ublInvoice->setId($invoice->invoice_number);
2018-01-11 18:18:22 +01:00
$ublInvoice->setIssueDate(date_create($invoice->invoice_date));
2018-01-11 08:58:43 +01:00
$ublInvoice->setInvoiceTypeCode('SalesInvoice');
// account
$supplierParty = new Party();
$supplierParty->setName($account->name);
$supplierAddress = (new Address())
->setCityName($account->city)
->setStreetName($account->address1)
->setBuildingNumber($account->address2)
->setPostalZone($account->postal_code);
if ($account->country_id) {
$country = new Country();
$country->setIdentificationCode($account->country->iso_3166_2);
$supplierAddress->setCountry($country);
}
$supplierParty->setPostalAddress($supplierAddress);
$supplierParty->setPhysicalLocation($supplierAddress);
$contact = new Contact();
$contact->setElectronicMail($invoice->user->email);
$supplierParty->setContact($contact);
$ublInvoice->setAccountingSupplierParty($supplierParty);
// client
$customerParty = new Party();
$customerParty->setName($client->getDisplayName());
$customerAddress = (new Address())
->setCityName($client->city)
->setStreetName($client->address1)
->setBuildingNumber($client->address2)
->setPostalZone($client->postal_code);
if ($client->country_id) {
$country = new Country();
$country->setIdentificationCode($client->country->iso_3166_2);
2018-01-11 18:18:22 +01:00
$customerAddress->setCountry($country);
2018-01-11 08:58:43 +01:00
}
$customerParty->setPostalAddress($customerAddress);
$customerParty->setPhysicalLocation($customerAddress);
$contact = new Contact();
$contact->setElectronicMail($client->contacts[0]->email);
$customerParty->setContact($contact);
$ublInvoice->setAccountingCustomerParty($customerParty);
2018-01-11 18:18:22 +01:00
// line items
$invoiceLine = [];
foreach ($invoice->invoice_items as $index => $item) {
$invoiceLine = (new InvoiceLine())
->setId($index + 1)
->setInvoicedQuantity($item->qty)
->setLineExtensionAmount($item->cost)
->setItem((new Item())
->setName($item->product_key)
->setDescription($item->description));
//->setSellersItemIdentification("1ABCD"));
if ($item->tax_name1 || $item->tax_rate1) {
$taxtotal = (new TaxTotal())
->setTaxAmount(10)
2018-01-12 14:15:58 +01:00
->addTaxSubTotal((new TaxSubTotal())
2018-01-11 18:18:22 +01:00
->setTaxAmount(10)
->setTaxableAmount(100)
->setTaxCategory((new TaxCategory())
->setId("H")
->setName("NL, Hoog Tarief")
->setPercent(21.00)));
$invoiceLine->setTaxTotal($taxtotal);
}
$invoiceLines[] = $invoiceLine;
}
2018-01-11 08:58:43 +01:00
2018-01-11 18:18:22 +01:00
$ublInvoice->setInvoiceLines($invoiceLines);
if ($invoice->tax_name1 || $invoice->tax_rate1) {
$taxtotal = (new TaxTotal())
->setTaxAmount(10)
2018-01-12 14:15:58 +01:00
->addTaxSubTotal((new TaxSubTotal())
2018-01-11 18:18:22 +01:00
->setTaxAmount(10)
->setTaxableAmount(100)
->setTaxCategory((new TaxCategory())
->setId("H")
->setName("NL, Hoog Tarief")
->setPercent(21.00)));
$ublInvoice->setTaxTotal($taxtotal);
}
2018-01-11 08:58:43 +01:00
2018-01-11 18:18:22 +01:00
$ublInvoice->setLegalMonetaryTotal((new LegalMonetaryTotal())
2018-01-11 08:58:43 +01:00
->setLineExtensionAmount(100)
->setTaxExclusiveAmount(100)
->setPayableAmount(-1000)
->setAllowanceTotalAmount(50));
return $xmlService->write('Invoice', [
$ublInvoice
]);
}
}