akaunting/app/Jobs/Sale/CreateInvoiceItem.php

179 lines
5.8 KiB
PHP
Raw Normal View History

2018-11-05 01:25:43 +03:00
<?php
2019-12-31 15:49:09 +03:00
namespace App\Jobs\Sale;
2018-11-05 01:25:43 +03:00
2019-11-16 10:21:14 +03:00
use App\Abstracts\Job;
2019-12-31 15:49:09 +03:00
use App\Models\Sale\InvoiceItem;
use App\Models\Sale\InvoiceItemTax;
2018-11-05 01:25:43 +03:00
use App\Models\Setting\Tax;
2019-11-16 10:21:14 +03:00
use Illuminate\Support\Str;
2018-11-05 01:25:43 +03:00
2019-11-16 10:21:14 +03:00
class CreateInvoiceItem extends Job
2018-11-05 01:25:43 +03:00
{
protected $invoice;
2020-07-06 09:54:18 +03:00
protected $request;
2018-11-05 01:25:43 +03:00
/**
* Create a new job instance.
*
* @param $invoice
2020-07-06 09:54:18 +03:00
* @param $request
2018-11-05 01:25:43 +03:00
*/
2020-07-06 09:54:18 +03:00
public function __construct($invoice, $request)
2018-11-05 01:25:43 +03:00
{
$this->invoice = $invoice;
2020-07-06 09:54:18 +03:00
$this->request = $request;
2018-11-05 01:25:43 +03:00
}
/**
* Execute the job.
*
* @return InvoiceItem
*/
public function handle()
{
2019-12-07 12:54:13 +03:00
$item_id = !empty($this->request['item_id']) ? $this->request['item_id'] : 0;
2020-07-06 09:54:18 +03:00
$precision = config('money.' . $this->invoice->currency_code . '.precision');
2019-12-07 12:54:13 +03:00
$item_amount = (double) $this->request['price'] * (double) $this->request['quantity'];
2018-11-06 17:55:31 +03:00
2020-04-20 04:09:51 +03:00
$discount = 0;
2019-12-07 12:54:13 +03:00
$item_discounted_amount = $item_amount;
2018-11-06 17:55:31 +03:00
// Apply line discount to amount
2019-12-07 12:54:13 +03:00
if (!empty($this->request['discount'])) {
2020-04-20 04:09:51 +03:00
$discount += $this->request['discount'];
2020-03-21 04:42:45 +03:00
$item_discounted_amount = $item_amount -= ($item_amount * ($this->request['discount'] / 100));
2018-11-06 17:55:31 +03:00
}
2018-11-05 01:25:43 +03:00
// Apply global discount to amount
if (!empty($this->request['global_discount'])) {
2020-04-20 04:09:51 +03:00
$discount += $this->request['global_discount'];
$item_discounted_amount = $item_amount - ($item_amount * ($this->request['global_discount'] / 100));
}
2018-11-06 17:55:31 +03:00
$tax_amount = 0;
2018-11-05 01:25:43 +03:00
$item_taxes = [];
2018-11-06 17:55:31 +03:00
$item_tax_total = 0;
2018-11-05 01:25:43 +03:00
2019-12-07 12:54:13 +03:00
if (!empty($this->request['tax_id'])) {
$inclusives = $compounds = [];
2018-11-06 17:55:31 +03:00
2019-12-07 12:54:13 +03:00
foreach ((array) $this->request['tax_id'] as $tax_id) {
2018-11-06 17:55:31 +03:00
$tax = Tax::find($tax_id);
switch ($tax->type) {
2018-11-07 12:07:38 +03:00
case 'inclusive':
$inclusives[] = $tax;
2019-12-07 12:54:13 +03:00
2018-11-06 17:55:31 +03:00
break;
case 'compound':
$compounds[] = $tax;
2019-12-07 12:54:13 +03:00
2018-11-06 17:55:31 +03:00
break;
2019-11-16 10:21:14 +03:00
case 'fixed':
2019-12-07 12:54:13 +03:00
$tax_amount = $tax->rate * (double) $this->request['quantity'];
2019-11-16 10:21:14 +03:00
$item_taxes[] = [
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'tax_id' => $tax_id,
'name' => $tax->name,
'amount' => $tax_amount,
];
$item_tax_total += $tax_amount;
2019-12-07 12:54:13 +03:00
2019-11-16 10:21:14 +03:00
break;
2018-11-06 17:55:31 +03:00
default:
2019-12-07 12:54:13 +03:00
$tax_amount = ($item_discounted_amount / 100) * $tax->rate;
2018-11-06 17:55:31 +03:00
$item_taxes[] = [
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'tax_id' => $tax_id,
'name' => $tax->name,
'amount' => $tax_amount,
];
$item_tax_total += $tax_amount;
2019-12-07 12:54:13 +03:00
2018-11-06 17:55:31 +03:00
break;
}
}
2018-11-05 01:25:43 +03:00
2018-11-07 12:07:38 +03:00
if ($inclusives) {
2019-12-07 12:54:13 +03:00
$item_amount = $item_discounted_amount + $item_tax_total;
2018-11-06 18:39:57 +03:00
2019-11-16 10:21:14 +03:00
$item_base_rate = $item_amount / (1 + collect($inclusives)->sum('rate') / 100);
2018-11-06 18:39:57 +03:00
foreach ($inclusives as $inclusive) {
2020-07-06 09:54:18 +03:00
$tax_amount = $item_base_rate * ($inclusive->rate / 100);
2018-11-07 11:45:29 +03:00
$item_taxes[] = [
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'tax_id' => $inclusive->id,
'name' => $inclusive->name,
'amount' => $tax_amount,
];
2020-07-06 09:54:18 +03:00
$item_tax_total += $tax_amount;
2018-11-05 01:25:43 +03:00
}
2020-04-20 04:09:51 +03:00
$item_amount = ($item_amount - $item_tax_total) / (1 - $discount / 100);
2018-11-06 17:55:31 +03:00
}
if ($compounds) {
foreach ($compounds as $compound) {
2019-12-07 12:54:13 +03:00
$tax_amount = (($item_discounted_amount + $item_tax_total) / 100) * $compound->rate;
2018-11-05 01:25:43 +03:00
2018-11-06 17:55:31 +03:00
$item_taxes[] = [
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'tax_id' => $compound->id,
'name' => $compound->name,
'amount' => $tax_amount,
];
2020-07-06 09:54:18 +03:00
$item_tax_total += $tax_amount;
2018-11-06 17:55:31 +03:00
}
2018-11-05 01:25:43 +03:00
}
}
$invoice_item = InvoiceItem::create([
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'item_id' => $item_id,
2019-12-07 12:54:13 +03:00
'name' => Str::limit($this->request['name'], 180, ''),
'quantity' => (double) $this->request['quantity'],
2020-07-06 09:54:18 +03:00
'price' => round($this->request['price'], $precision),
'tax' => round($item_tax_total, $precision),
'discount_rate' => !empty($this->request['discount']) ? $this->request['discount'] : 0,
2020-07-06 09:54:18 +03:00
'total' => round($item_amount, $precision),
2018-11-05 01:25:43 +03:00
]);
$invoice_item->item_taxes = false;
$invoice_item->inclusives = false;
$invoice_item->compounds = false;
2019-12-07 12:54:13 +03:00
if (!empty($item_taxes)) {
2018-11-06 17:55:31 +03:00
$invoice_item->item_taxes = $item_taxes;
2018-11-07 12:07:38 +03:00
$invoice_item->inclusives = $inclusives;
2018-11-06 17:55:31 +03:00
$invoice_item->compounds = $compounds;
foreach ($item_taxes as $item_tax) {
$item_tax['invoice_item_id'] = $invoice_item->id;
2020-07-06 09:54:18 +03:00
$item_tax['amount'] = round($item_tax['amount'], $precision);
2018-11-05 01:25:43 +03:00
2018-11-06 17:55:31 +03:00
InvoiceItemTax::create($item_tax);
2018-11-05 01:25:43 +03:00
}
}
return $invoice_item;
}
2018-11-06 17:55:31 +03:00
}