2018-11-07 11:06:38 +03:00
|
|
|
<?php
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
namespace App\Jobs\Document;
|
2018-11-07 11:06:38 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Abstracts\Job;
|
2021-09-07 10:33:34 +03:00
|
|
|
use App\Interfaces\Job\HasOwner;
|
|
|
|
use App\Interfaces\Job\HasSource;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\ShouldCreate;
|
|
|
|
use App\Models\Document\Document;
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Models\Document\DocumentItem;
|
|
|
|
use App\Models\Document\DocumentItemTax;
|
2018-11-07 11:06:38 +03:00
|
|
|
use App\Models\Setting\Tax;
|
2019-11-16 10:21:14 +03:00
|
|
|
use Illuminate\Support\Str;
|
2018-11-07 11:06:38 +03:00
|
|
|
|
2021-09-07 10:33:34 +03:00
|
|
|
class CreateDocumentItem extends Job implements HasOwner, HasSource, ShouldCreate
|
2018-11-07 11:06:38 +03:00
|
|
|
{
|
2020-12-24 01:28:38 +03:00
|
|
|
protected $document;
|
2018-11-07 11:06:38 +03:00
|
|
|
|
2020-07-06 09:54:18 +03:00
|
|
|
protected $request;
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
public function __construct(Document $document, $request)
|
2018-11-07 11:06:38 +03:00
|
|
|
{
|
2020-12-24 01:28:38 +03:00
|
|
|
$this->document = $document;
|
2020-07-06 09:54:18 +03:00
|
|
|
$this->request = $request;
|
2021-09-06 11:53:57 +03:00
|
|
|
|
|
|
|
parent::__construct($document, $request);
|
2018-11-07 11:06:38 +03:00
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
public function handle(): DocumentItem
|
2018-11-07 11:06:38 +03:00
|
|
|
{
|
2019-12-07 12:54:13 +03:00
|
|
|
$item_id = !empty($this->request['item_id']) ? $this->request['item_id'] : 0;
|
2020-12-24 01:28:38 +03:00
|
|
|
$precision = config('money.' . $this->document->currency_code . '.precision');
|
2020-07-06 09:54:18 +03:00
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$item_amount = (double) $this->request['price'] * (double) $this->request['quantity'];
|
2018-11-07 11:06:38 +03:00
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$item_discounted_amount = $item_amount;
|
2018-11-07 11:06:38 +03:00
|
|
|
|
2020-04-20 04:06:20 +03:00
|
|
|
// Apply line discount to amount
|
2019-12-07 12:54:13 +03:00
|
|
|
if (!empty($this->request['discount'])) {
|
2021-08-15 22:35:05 +01:00
|
|
|
if ($this->request['discount_type'] === 'percentage') {
|
2022-04-15 17:53:45 +03:00
|
|
|
$item_discounted_amount -= ($item_amount * ($this->request['discount'] / 100));
|
|
|
|
} else {
|
|
|
|
$item_discounted_amount -= $this->request['discount'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply total discount to amount
|
|
|
|
if (!empty($this->request['global_discount'])) {
|
|
|
|
if ($this->request['global_discount_type'] === 'percentage') {
|
|
|
|
$item_discounted_amount -= $item_discounted_amount * ($this->request['global_discount'] / 100);
|
2021-08-15 17:20:39 +01:00
|
|
|
} else {
|
2022-04-15 17:53:45 +03:00
|
|
|
$item_discounted_amount -= $this->request['global_discount'];
|
2021-08-15 17:20:39 +01:00
|
|
|
}
|
2020-04-20 04:06:20 +03:00
|
|
|
}
|
|
|
|
|
2018-11-07 11:06:38 +03:00
|
|
|
$tax_amount = 0;
|
|
|
|
$item_tax_total = 0;
|
2022-04-15 17:53:45 +03:00
|
|
|
$actual_price_item = $item_amount = $item_discounted_amount;
|
2018-11-07 11:06:38 +03:00
|
|
|
|
2022-04-15 17:53:45 +03:00
|
|
|
$item_taxes = [];
|
|
|
|
$doc_parms = [
|
|
|
|
'company_id' => $this->document->company_id,
|
|
|
|
'type' => $this->document->type,
|
|
|
|
'document_id' => $this->document->id,
|
|
|
|
];
|
2018-11-07 11:06:38 +03:00
|
|
|
|
2022-04-15 17:53:45 +03:00
|
|
|
if (!empty($this->request['tax_ids'])) {
|
|
|
|
// New variables by tax type & tax sorting
|
2020-12-24 01:28:38 +03:00
|
|
|
foreach ((array) $this->request['tax_ids'] as $tax_id) {
|
2018-11-07 11:06:38 +03:00
|
|
|
$tax = Tax::find($tax_id);
|
2022-04-15 17:53:45 +03:00
|
|
|
${$tax->type . 's'}[] = $tax;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($inclusives)) {
|
|
|
|
foreach ($inclusives as $inclusive) {
|
|
|
|
$tax_amount = $item_discounted_amount - ($item_discounted_amount / (1 + $inclusive->rate / 100));
|
|
|
|
|
|
|
|
$item_taxes[] = $doc_parms + [
|
|
|
|
'tax_id' => $inclusive->id,
|
|
|
|
'name' => $inclusive->name,
|
|
|
|
'amount' => $tax_amount,
|
|
|
|
];
|
2018-11-07 11:06:38 +03:00
|
|
|
|
2022-04-15 17:53:45 +03:00
|
|
|
$item_tax_total += $tax_amount;
|
2018-11-07 11:06:38 +03:00
|
|
|
}
|
2022-04-15 17:53:45 +03:00
|
|
|
|
|
|
|
$actual_price_item = $item_discounted_amount - $item_tax_total;
|
2018-11-07 11:06:38 +03:00
|
|
|
}
|
|
|
|
|
2022-04-15 17:53:45 +03:00
|
|
|
if (isset($fixeds)) {
|
|
|
|
foreach ($fixeds as $tax) {
|
|
|
|
$tax_amount = $tax->rate * (double) $this->request['quantity'];
|
2019-02-03 21:01:08 +05:30
|
|
|
|
2022-04-15 17:53:45 +03:00
|
|
|
$item_taxes[] = $doc_parms + [
|
|
|
|
'tax_id' => $tax->id,
|
|
|
|
'name' => $tax->name,
|
|
|
|
'amount' => $tax_amount,
|
|
|
|
];
|
2019-03-07 12:01:32 +03:00
|
|
|
|
2022-04-15 17:53:45 +03:00
|
|
|
$item_tax_total += $tax_amount;
|
|
|
|
$item_amount += $tax_amount;
|
|
|
|
}
|
|
|
|
}
|
2019-02-03 21:01:08 +05:30
|
|
|
|
2022-04-15 17:53:45 +03:00
|
|
|
if (isset($normals)) {
|
|
|
|
foreach ($normals as $tax) {
|
|
|
|
$tax_amount = $actual_price_item * ($tax->rate / 100);
|
2021-05-30 17:22:48 +03:00
|
|
|
|
2022-04-15 17:53:45 +03:00
|
|
|
$item_taxes[] = $doc_parms + [
|
|
|
|
'tax_id' => $tax->id,
|
|
|
|
'name' => $tax->name,
|
2019-03-07 12:01:32 +03:00
|
|
|
'amount' => $tax_amount,
|
2019-02-03 21:01:08 +05:30
|
|
|
];
|
2020-07-06 09:54:18 +03:00
|
|
|
|
|
|
|
$item_tax_total += $tax_amount;
|
2022-04-15 17:53:45 +03:00
|
|
|
$item_amount += $tax_amount;
|
2018-11-07 11:06:38 +03:00
|
|
|
}
|
2022-04-15 17:53:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($withholdings)) {
|
|
|
|
foreach ($withholdings as $tax) {
|
|
|
|
$tax_amount = -($actual_price_item * ($tax->rate / 100));
|
|
|
|
|
|
|
|
$item_taxes[] = $doc_parms + [
|
|
|
|
'tax_id' => $tax->id,
|
|
|
|
'name' => $tax->name,
|
|
|
|
'amount' => $tax_amount,
|
|
|
|
];
|
2019-02-03 21:01:08 +05:30
|
|
|
|
2022-04-15 17:53:45 +03:00
|
|
|
$item_tax_total += $tax_amount;
|
|
|
|
$item_amount += $tax_amount;
|
2021-08-15 17:20:39 +01:00
|
|
|
}
|
2018-11-07 11:06:38 +03:00
|
|
|
}
|
|
|
|
|
2022-04-15 17:53:45 +03:00
|
|
|
if (isset($compounds)) {
|
2018-11-07 11:06:38 +03:00
|
|
|
foreach ($compounds as $compound) {
|
2022-04-15 17:53:45 +03:00
|
|
|
$tax_amount = ($item_amount / 100) * $compound->rate;
|
2021-05-30 17:22:48 +03:00
|
|
|
|
2022-04-15 17:53:45 +03:00
|
|
|
$item_taxes[] = $doc_parms + [
|
2018-11-07 11:06:38 +03:00
|
|
|
'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-07 11:06:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:52:53 +03:00
|
|
|
$this->request['company_id'] = $this->document->company_id;
|
|
|
|
$this->request['type'] = $this->document->type;
|
|
|
|
$this->request['document_id'] = $this->document->id;
|
|
|
|
$this->request['item_id'] = $item_id;
|
|
|
|
$this->request['name'] = Str::limit($this->request['name'], 180, '');
|
|
|
|
$this->request['description'] = !empty($this->request['description']) ? $this->request['description'] : '';
|
|
|
|
$this->request['quantity'] = (double) $this->request['quantity'];
|
|
|
|
$this->request['price'] = round($this->request['price'], $precision);
|
|
|
|
$this->request['tax'] = round($item_tax_total, $precision);
|
2021-08-15 22:35:05 +01:00
|
|
|
$this->request['discount_type'] = !empty($this->request['discount_type']) ? $this->request['discount_type'] : 'percentage';
|
2021-04-14 12:52:53 +03:00
|
|
|
$this->request['discount_rate'] = !empty($this->request['discount']) ? $this->request['discount'] : 0;
|
2022-04-15 17:53:45 +03:00
|
|
|
$this->request['total'] = round($actual_price_item, $precision);
|
2021-09-10 01:20:20 +03:00
|
|
|
$this->request['created_from'] = $this->request['created_from'];
|
|
|
|
$this->request['created_by'] = $this->request['created_by'];
|
2021-04-14 12:52:53 +03:00
|
|
|
|
|
|
|
$document_item = DocumentItem::create($this->request);
|
2018-11-07 11:06:38 +03:00
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
$document_item->item_taxes = false;
|
|
|
|
$document_item->inclusives = false;
|
|
|
|
$document_item->compounds = false;
|
2018-11-07 17:58:55 +03:00
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
if (!empty($item_taxes)) {
|
2020-12-24 01:28:38 +03:00
|
|
|
$document_item->item_taxes = $item_taxes;
|
2022-04-15 17:53:45 +03:00
|
|
|
$document_item->inclusives = $inclusives ?? null;
|
|
|
|
$document_item->compounds = $compounds ?? null;
|
2018-11-07 11:06:38 +03:00
|
|
|
|
|
|
|
foreach ($item_taxes as $item_tax) {
|
2020-12-24 01:28:38 +03:00
|
|
|
$item_tax['document_item_id'] = $document_item->id;
|
2020-07-22 15:11:31 +03:00
|
|
|
$item_tax['amount'] = round(abs($item_tax['amount']), $precision);
|
2021-09-07 10:33:34 +03:00
|
|
|
$item_tax['created_from'] = $this->request['created_from'];
|
|
|
|
$item_tax['created_by'] = $this->request['created_by'];
|
2018-11-07 11:06:38 +03:00
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
DocumentItemTax::create($item_tax);
|
2018-11-07 11:06:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
return $document_item;
|
2018-11-07 11:06:38 +03:00
|
|
|
}
|
|
|
|
}
|