Working on UBL support

This commit is contained in:
Hillel Coren 2018-01-14 20:17:59 +02:00
parent ee03ae7003
commit 861bbbb7d9
9 changed files with 186 additions and 113 deletions

View File

@ -24,7 +24,7 @@ class DashboardController extends BaseController
*/
public function index()
{
//dd(dispatch(new \App\Jobs\ConvertInvoiceToUbl(\App\Models\Invoice::find(51))));
dd(dispatch(new \App\Jobs\ConvertInvoiceToUbl(\App\Models\Invoice::find(51))));
$user = Auth::user();
$viewAll = $user->hasPermission('view_all');

View File

@ -134,6 +134,7 @@ class SubscriptionController extends BaseController
$subscription = Subscription::scope($subscriptionPublicId)->firstOrFail();
} else {
$subscription = Subscription::createNew();
$subscriptionPublicId = $subscription->public_id;
}
$validator = Validator::make(Input::all(), $rules);
@ -154,6 +155,14 @@ class SubscriptionController extends BaseController
Session::flash('message', $message);
}
return Redirect::to('subscriptions/' . $subscriptionPublicId . '/edit');
return redirect('/settings/api_tokens');
/*
if ($subscriptionPublicId) {
return Redirect::to('subscriptions/' . $subscriptionPublicId . '/edit');
} else {
return redirect('/settings/api_tokens');
}
*/
}
}

View File

@ -42,106 +42,131 @@ class ConvertInvoiceToUbl extends Job
$ublInvoice->setIssueDate(date_create($invoice->invoice_date));
$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);
$supplierParty = $this->createParty($account, $invoice->user);
$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);
$customerAddress->setCountry($country);
}
$customerParty->setPostalAddress($customerAddress);
$customerParty->setPhysicalLocation($customerAddress);
$contact = new Contact();
$contact->setElectronicMail($client->contacts[0]->email);
$customerParty->setContact($contact);
$customerParty = $this->createParty($client, $client->contacts[0]);
$ublInvoice->setAccountingCustomerParty($customerParty);
// line items
$invoiceLine = [];
$taxable = $invoice->getTaxable();
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)
->addTaxSubTotal((new TaxSubTotal())
->setTaxAmount(10)
->setTaxableAmount(100)
->setTaxCategory((new TaxCategory())
->setId("H")
->setName("NL, Hoog Tarief")
->setPercent(21.00)));
$invoiceLine->setTaxTotal($taxtotal);
}
$invoiceLines[] = $invoiceLine;
$itemTaxable = $invoice->getItemTaxable($item, $taxable);
$item->setRelation('invoice', $invoice);
$invoiceLines[] = $this->createInvoiceLine($invoice, $index, $item, $itemTaxable);
}
$ublInvoice->setInvoiceLines($invoiceLines);
if ($invoice->tax_name1 || $invoice->tax_rate1) {
$taxtotal = (new TaxTotal())
->setTaxAmount(10)
->addTaxSubTotal((new TaxSubTotal())
->setTaxAmount(10)
->setTaxableAmount(100)
->setTaxCategory((new TaxCategory())
->setId("H")
->setName("NL, Hoog Tarief")
->setPercent(21.00)));
if ($invoice->hasTaxes()) {
$taxtotal = new TaxTotal();
$taxAmount1 = $taxAmount2 = 0;
if ($item->tax_name1 || $item->tax_rate1) {
$taxAmount1 = $invoice->taxAmount($taxable, $invoice->tax_rate1);
$taxtotal->addTaxSubTotal((new TaxSubTotal())
->setTaxAmount($taxAmount1)
->setTaxableAmount($taxable)
->setTaxCategory((new TaxCategory())
->setId($item->tax_name1)
->setName($item->tax_name1)
->setPercent($item->tax_rate1)));
}
if ($item->tax_name2 || $item->tax_rate2) {
$itemTaxAmount2 = $invoice->taxAmount($taxable, $invoice->tax_rate2);
$taxtotal->addTaxSubTotal((new TaxSubTotal())
->setTaxAmount($taxAmount2)
->setTaxableAmount($taxable)
->setTaxCategory((new TaxCategory())
->setId($item->tax_name2)
->setName($item->tax_name2)
->setPercent($item->tax_rate2)));
}
$taxtotal->setTaxAmount($taxAmount1 + $taxAmount2);
$ublInvoice->setTaxTotal($taxtotal);
}
$ublInvoice->setLegalMonetaryTotal((new LegalMonetaryTotal())
->setLineExtensionAmount(100)
->setTaxExclusiveAmount(100)
->setPayableAmount(-1000)
->setAllowanceTotalAmount(50));
//->setLineExtensionAmount()
->setTaxExclusiveAmount($taxable)
->setPayableAmount($invoice->balance));
return $xmlService->write('Invoice', [
$ublInvoice
]);
}
public function createParty($company, $user)
{
$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;
}
public function createInvoiceLine($invoice, $index, $item, $taxable)
{
$invoiceLine = (new InvoiceLine())
->setId($index + 1)
->setInvoicedQuantity($item->qty)
->setLineExtensionAmount($item->costWithDiscount())
->setItem((new Item())
->setName($item->product_key)
->setDescription($item->description));
//->setSellersItemIdentification("1ABCD"));
if ($item->hasTaxes()) {
$taxtotal = new TaxTotal();
$itemTaxAmount1 = $itemTaxAmount2 = 0;
if ($item->tax_name1 || $item->tax_rate1) {
$itemTaxAmount1 = $invoice->taxAmount($taxable, $item->tax_rate1);
$taxtotal->addTaxSubTotal((new TaxSubTotal())
->setTaxAmount($itemTaxAmount1)
->setTaxableAmount($taxable)
->setTaxCategory((new TaxCategory())
->setId($item->tax_name1)
->setName($item->tax_name1)
->setPercent($item->tax_rate1)));
}
if ($item->tax_name2 || $item->tax_rate2) {
$itemTaxAmount2 = $invoice->taxAmount($taxable, $item->tax_rate2);
$taxtotal->addTaxSubTotal((new TaxSubTotal())
->setTaxAmount($itemTaxAmount2)
->setTaxableAmount($taxable)
->setTaxCategory((new TaxCategory())
->setId($item->tax_name2)
->setName($item->tax_name2)
->setPercent($item->tax_rate2)));
}
$taxtotal->setTaxAmount($itemTaxAmount1 + $itemTaxAmount2);
$invoiceLine->setTaxTotal($taxtotal);
}
return $invoiceLine;
}
}

View File

@ -1386,21 +1386,12 @@ class Invoice extends EntityModel implements BalanceAffecting
$paidAmount = $this->getAmountPaid($calculatePaid);
if ($this->tax_name1) {
if ($account->inclusive_taxes) {
$invoiceTaxAmount = round(($taxable * 100) / (100 + ($this->tax_rate1 * 100)), 2);
} else {
$invoiceTaxAmount = round($taxable * ($this->tax_rate1 / 100), 2);
}
$invoiceTaxAmount = $this->taxAmount($taxable, $this->tax_rate1);
$invoicePaidAmount = floatval($this->amount) && $invoiceTaxAmount ? ($paidAmount / $this->amount * $invoiceTaxAmount) : 0;
$this->calculateTax($taxes, $this->tax_name1, $this->tax_rate1, $invoiceTaxAmount, $invoicePaidAmount);
}
if ($this->tax_name2) {
if ($account->inclusive_taxes) {
$invoiceTaxAmount = round(($taxable * 100) / (100 + ($this->tax_rate2 * 100)), 2);
} else {
$invoiceTaxAmount = round($taxable * ($this->tax_rate2 / 100), 2);
}
$invoiceTaxAmount = $this->taxAmount($taxable, $this->tax_rate2);
$invoicePaidAmount = floatval($this->amount) && $invoiceTaxAmount ? ($paidAmount / $this->amount * $invoiceTaxAmount) : 0;
$this->calculateTax($taxes, $this->tax_name2, $this->tax_rate2, $invoiceTaxAmount, $invoicePaidAmount);
}
@ -1409,21 +1400,12 @@ class Invoice extends EntityModel implements BalanceAffecting
$itemTaxable = $this->getItemTaxable($invoiceItem, $taxable);
if ($invoiceItem->tax_name1) {
if ($account->inclusive_taxes) {
$itemTaxAmount = round(($itemTaxable * 100) / (100 + ($invoiceItem->tax_rate1 * 100)), 2);
} else {
$itemTaxAmount = round($itemTaxable * ($invoiceItem->tax_rate1 / 100), 2);
}
$itemTaxAmount = $this->taxAmount($itemTaxable, $invoiceItem->tax_rate1);
$itemPaidAmount = floatval($this->amount) && $itemTaxAmount ? ($paidAmount / $this->amount * $itemTaxAmount) : 0;
$this->calculateTax($taxes, $invoiceItem->tax_name1, $invoiceItem->tax_rate1, $itemTaxAmount, $itemPaidAmount);
}
if ($invoiceItem->tax_name2) {
if ($account->inclusive_taxes) {
$itemTaxAmount = round(($itemTaxable * 100) / (100 + ($invoiceItem->tax_rate2 * 100)), 2);
} else {
$itemTaxAmount = round($itemTaxable * ($invoiceItem->tax_rate2 / 100), 2);
}
$itemTaxAmount = $this->taxAmount($itemTaxable, $invoiceItem->tax_rate2);
$itemPaidAmount = floatval($this->amount) && $itemTaxAmount ? ($paidAmount / $this->amount * $itemTaxAmount) : 0;
$this->calculateTax($taxes, $invoiceItem->tax_name2, $invoiceItem->tax_rate2, $itemTaxAmount, $itemPaidAmount);
}
@ -1432,6 +1414,17 @@ class Invoice extends EntityModel implements BalanceAffecting
return $taxes;
}
public function taxAmount($taxable, $rate)
{
$account = $this->account;
if ($account->inclusive_taxes) {
return round(($taxable * 100) / (100 + ($rate * 100)), 2);
} else {
return round($taxable * ($rate / 100), 2);
}
}
/**
* @param $taxes
* @param $name
@ -1589,6 +1582,20 @@ class Invoice extends EntityModel implements BalanceAffecting
return true;
}
public function hasTaxes()
{
if ($this->tax_name1 || $this->tax_rate1) {
return true;
}
if ($this->tax_name2 || $this->tax_rate2) {
return false;
}
return false;
}
}
Invoice::creating(function ($invoice) {

View File

@ -107,4 +107,33 @@ class InvoiceItem extends EntityModel
$this->save();
}
}
public function hasTaxes()
{
if ($this->tax_name1 || $this->tax_rate1) {
return true;
}
if ($this->tax_name2 || $this->tax_rate2) {
return false;
}
return false;
}
public function costWithDiscount()
{
$cost = $this->cost;
if ($this->discount != 0) {
if ($this->invoice->is_amount_discount) {
$cost -= $discount / $this->qty;
} else {
$cost -= $cost * $discount / 100;
}
}
return $cost;
}
}

10
composer.lock generated
View File

@ -1085,16 +1085,16 @@
},
{
"name": "cleverit/ubl_invoice",
"version": "v1.0",
"version": "v1.0.1",
"source": {
"type": "git",
"url": "https://github.com/CleverIT/UBL_invoice.git",
"reference": "3c4ec1dd8dc826d6c8302e191565b8ac1a947022"
"reference": "8b98ed26b975cae24eea319f3f2cefd82add735e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/CleverIT/UBL_invoice/zipball/3c4ec1dd8dc826d6c8302e191565b8ac1a947022",
"reference": "3c4ec1dd8dc826d6c8302e191565b8ac1a947022",
"url": "https://api.github.com/repos/CleverIT/UBL_invoice/zipball/8b98ed26b975cae24eea319f3f2cefd82add735e",
"reference": "8b98ed26b975cae24eea319f3f2cefd82add735e",
"shasum": ""
},
"require": {
@ -1133,7 +1133,7 @@
"xml",
"xml invoice"
],
"time": "2018-01-11 19:26:20"
"time": "2018-01-13 00:27:05"
},
{
"name": "codedge/laravel-selfupdater",

View File

@ -2649,6 +2649,7 @@ $LANG = array(
'signature_on_pdf_help' => 'Show the client signature on the invoice/quote PDF.',
'expired_white_label' => 'The white label license has expired',
'return_to_login' => 'Return to Login',
'beta' => 'Beta',
);

View File

@ -54,7 +54,7 @@
])) : false) !!}
{!! Former::checkbox('ubl_email_attachment')
->text(trans('texts.enable'))
->text(trans('texts.enable') . ' [' . trans('texts.beta') . ']')
->value(1) !!}
{!! Former::checkbox('document_email_attachment')

View File

@ -7,7 +7,7 @@
{!! Former::open($url)->method($method)->addClass('warn-on-exit')->rules(array(
'event_id' => 'required',
'target_url' => 'required|url',
'format' => 'required',
//'format' => 'required',
)); !!}
<div class="panel panel-default">
@ -63,13 +63,15 @@
{!! Former::text('target_url')
->placeholder('https://example.com')!!}
<!--
{!! Former::select('format')
->options([
SUBSCRIPTION_FORMAT_JSON => SUBSCRIPTION_FORMAT_JSON,
SUBSCRIPTION_FORMAT_UBL => SUBSCRIPTION_FORMAT_UBL
])
->help('target_url_help') !!}
-->
</div>
</div>