1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 00:11:35 +02:00
invoiceninja/app/Jobs/ConvertInvoiceToUbl.php

156 lines
5.1 KiB
PHP
Raw Normal View History

2018-01-11 08:58:43 +01:00
<?php
namespace App\Jobs;
2018-04-08 09:44:35 +02:00
use Utils;
use Exception;
2018-01-11 08:58:43 +01:00
use App\Jobs\Job;
2018-01-14 20:40:42 +01:00
use CleverIt\UBL\Invoice\Generator;
2018-01-11 08:58:43 +01:00
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;
2018-01-15 07:05:07 +01:00
use CleverIt\UBL\Invoice\TaxScheme;
2018-01-11 08:58:43 +01:00
use CleverIt\UBL\Invoice\InvoiceLine;
use CleverIt\UBL\Invoice\Item;
use CleverIt\UBL\Invoice\LegalMonetaryTotal;
class ConvertInvoiceToUbl extends Job
{
2018-01-15 15:11:50 +01:00
const INVOICE_TYPE_STANDARD = 380;
const INVOICE_TYPE_CREDIT = 381;
2018-01-11 08:58:43 +01:00
public function __construct($invoice)
{
$this->invoice = $invoice;
}
public function handle()
{
$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-15 15:11:50 +01:00
$ublInvoice->setInvoiceTypeCode($invoice->amount < 0 ? self::INVOICE_TYPE_CREDIT : self::INVOICE_TYPE_STANDARD);
2018-01-11 08:58:43 +01:00
2018-01-14 19:17:59 +01:00
$supplierParty = $this->createParty($account, $invoice->user);
2018-01-11 08:58:43 +01:00
$ublInvoice->setAccountingSupplierParty($supplierParty);
2018-01-14 19:17:59 +01:00
$customerParty = $this->createParty($client, $client->contacts[0]);
2018-01-11 08:58:43 +01:00
$ublInvoice->setAccountingCustomerParty($customerParty);
2018-01-11 18:18:22 +01:00
// line items
2018-05-08 11:21:39 +02:00
$invoiceLines = [];
2018-01-14 19:17:59 +01:00
$taxable = $invoice->getTaxable();
2018-01-11 18:18:22 +01:00
foreach ($invoice->invoice_items as $index => $item) {
2018-01-14 19:17:59 +01:00
$itemTaxable = $invoice->getItemTaxable($item, $taxable);
$item->setRelation('invoice', $invoice);
2018-01-15 15:11:50 +01:00
$invoiceLines[] = $this->createInvoiceLine($index, $item, $itemTaxable);
2018-01-14 19:17:59 +01:00
}
$ublInvoice->setInvoiceLines($invoiceLines);
2018-02-25 22:03:49 +01:00
$taxtotal = new TaxTotal();
$taxAmount1 = $taxAmount2 = 0;
2018-01-11 18:18:22 +01:00
2018-02-25 22:03:49 +01:00
$taxAmount1 = $this->createTaxRate($taxtotal, $taxable, $invoice->tax_rate1, $invoice->tax_name1);
if ($invoice->tax_name2 || floatval($invoice->tax_rate2)) {
$taxAmount2 = $this->createTaxRate($taxtotal, $taxable, $invoice->tax_rate2, $invoice->tax_name2);
2018-01-11 18:18:22 +01:00
}
2018-01-11 08:58:43 +01:00
2018-02-25 22:03:49 +01:00
$taxtotal->setTaxAmount($taxAmount1 + $taxAmount2);
$ublInvoice->setTaxTotal($taxtotal);
2018-01-11 18:18:22 +01:00
$ublInvoice->setLegalMonetaryTotal((new LegalMonetaryTotal())
2018-01-14 19:17:59 +01:00
//->setLineExtensionAmount()
->setTaxExclusiveAmount($taxable)
->setPayableAmount($invoice->balance));
2018-01-11 08:58:43 +01:00
2018-04-08 09:44:35 +02:00
try {
return Generator::invoice($ublInvoice, $invoice->client->getCurrencyCode());
} catch (Exception $exception) {
Utils::logError($exception);
return false;
}
2018-01-11 08:58:43 +01:00
}
2018-01-14 19:17:59 +01:00
2018-01-15 15:11:50 +01:00
private function createParty($company, $user)
2018-01-14 19:17:59 +01:00
{
$party = new Party();
$party->setName($company->name);
$address = (new Address())
->setCityName($company->city)
->setStreetName($company->address1)
->setBuildingNumber($company->address2)
->setPostalZone($company->postal_code);
if ($company->country_id) {
$country = new Country();
$country->setIdentificationCode($company->country->iso_3166_2);
$address->setCountry($country);
}
$party->setPostalAddress($address);
$party->setPhysicalLocation($address);
$contact = new Contact();
$contact->setElectronicMail($user->email);
$party->setContact($contact);
return $party;
}
2018-01-15 15:11:50 +01:00
private function createInvoiceLine($index, $item, $taxable)
2018-01-14 19:17:59 +01:00
{
$invoiceLine = (new InvoiceLine())
->setId($index + 1)
->setInvoicedQuantity($item->qty)
->setLineExtensionAmount($item->costWithDiscount())
->setItem((new Item())
->setName($item->product_key)
->setDescription($item->description));
//->setSellersItemIdentification("1ABCD"));
2018-02-25 22:03:49 +01:00
$taxtotal = new TaxTotal();
$itemTaxAmount1 = $itemTaxAmount2 = 0;
2018-01-14 19:17:59 +01:00
2018-02-25 22:03:49 +01:00
$itemTaxAmount1 = $this->createTaxRate($taxtotal, $taxable, $item->tax_rate1, $item->tax_name1);
if ($item->tax_name2 || floatval($item->tax_rate2)) {
$itemTaxAmount2 = $this->createTaxRate($taxtotal, $taxable, $item->tax_rate2, $item->tax_name2);
2018-01-14 19:17:59 +01:00
}
2018-02-25 22:03:49 +01:00
$taxtotal->setTaxAmount($itemTaxAmount1 + $itemTaxAmount2);
$invoiceLine->setTaxTotal($taxtotal);
2018-01-14 19:17:59 +01:00
return $invoiceLine;
}
2018-01-15 15:11:50 +01:00
private function createTaxRate(&$taxtotal, $taxable, $taxRate, $taxName)
{
$invoice = $this->invoice;
$taxAmount = $invoice->taxAmount($taxable, $taxRate);
$taxScheme = ((new TaxScheme()))->setId($taxName);
$taxtotal->addTaxSubTotal((new TaxSubTotal())
->setTaxAmount($taxAmount)
->setTaxableAmount($taxable)
->setTaxCategory((new TaxCategory())
->setId($taxName)
->setName($taxName)
->setTaxScheme($taxScheme)
->setPercent($taxRate)));
return $taxAmount;
}
2018-01-11 08:58:43 +01:00
}