akaunting/app/Jobs/Document/CreateDocumentItem.php

189 lines
6.9 KiB
PHP
Raw Normal View History

<?php
2020-12-24 01:28:38 +03:00
namespace App\Jobs\Document;
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;
use App\Models\Setting\Tax;
2019-11-16 10:21:14 +03:00
use Illuminate\Support\Str;
2021-09-07 10:33:34 +03:00
class CreateDocumentItem extends Job implements HasOwner, HasSource, ShouldCreate
{
2020-12-24 01:28:38 +03:00
protected $document;
2020-07-06 09:54:18 +03:00
protected $request;
2021-09-06 11:53:57 +03:00
public function __construct(Document $document, $request)
{
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);
}
2021-09-06 11:53:57 +03:00
public function handle(): DocumentItem
{
2022-10-05 10:09:10 +03:00
$item_id = ! empty($this->request['item_id']) ? $this->request['item_id'] : 0;
2023-07-10 12:53:43 +03:00
$precision = config('money.currencies.' . $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'];
2019-12-07 12:54:13 +03:00
$item_discounted_amount = $item_amount;
// Apply line discount to amount
2022-10-05 10:09:10 +03:00
if (! empty($this->request['discount'])) {
if ($this->request['discount_type'] === 'percentage') {
$item_discounted_amount -= ($item_amount * ($this->request['discount'] / 100));
} else {
$item_discounted_amount -= $this->request['discount'];
}
}
// Apply total discount to amount
2022-09-15 14:11:37 +03:00
if (! empty($this->request['global_discount'])) {
if ($this->request['global_discount_type'] === 'percentage') {
2022-09-15 14:11:37 +03:00
$item_discounted_amount -= $item_discounted_amount * ($this->request['global_discount'] / 100);
2021-08-15 17:20:39 +01:00
} else {
2022-09-15 14:11:37 +03:00
$item_discounted_amount -= $this->request['global_discount'];
2021-08-15 17:20:39 +01:00
}
}
$tax_amount = 0;
$item_tax_total = 0;
$actual_price_item = $item_amount = $item_discounted_amount;
$item_taxes = [];
$doc_parms = [
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
];
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) {
$tax = Tax::find($tax_id);
${$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,
];
$item_tax_total += $tax_amount;
}
$actual_price_item = $item_discounted_amount - $item_tax_total;
}
if (isset($fixeds)) {
foreach ($fixeds as $tax) {
$tax_amount = $tax->rate * (double) $this->request['quantity'];
$item_taxes[] = $doc_parms + [
'tax_id' => $tax->id,
'name' => $tax->name,
'amount' => $tax_amount,
];
$item_tax_total += $tax_amount;
$item_amount += $tax_amount;
}
}
if (isset($normals)) {
foreach ($normals as $tax) {
$tax_amount = $actual_price_item * ($tax->rate / 100);
2021-05-30 17:22:48 +03:00
$item_taxes[] = $doc_parms + [
'tax_id' => $tax->id,
'name' => $tax->name,
'amount' => $tax_amount,
];
2020-07-06 09:54:18 +03:00
$item_tax_total += $tax_amount;
$item_amount += $tax_amount;
}
}
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,
];
$item_tax_total += $tax_amount;
$item_amount += $tax_amount;
2021-08-15 17:20:39 +01:00
}
}
if (isset($compounds)) {
foreach ($compounds as $compound) {
$tax_amount = ($item_amount / 100) * $compound->rate;
2021-05-30 17:22:48 +03:00
$item_taxes[] = $doc_parms + [
'tax_id' => $compound->id,
'name' => $compound->name,
'amount' => $tax_amount,
];
2020-07-06 09:54:18 +03:00
$item_tax_total += $tax_amount;
}
}
}
$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, '');
2022-09-15 14:11:37 +03:00
$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);
2022-09-15 14:11:37 +03:00
$this->request['discount_type'] = ! empty($this->request['discount_type']) ? $this->request['discount_type'] : 'percent';
$this->request['discount_rate'] = ! empty($this->request['discount']) ? $this->request['discount'] : 0;
$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'];
$document_item = DocumentItem::create($this->request);
2020-12-24 01:28:38 +03:00
$document_item->item_taxes = false;
$document_item->inclusives = false;
$document_item->compounds = false;
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;
$document_item->inclusives = $inclusives ?? null;
$document_item->compounds = $compounds ?? null;
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'];
2020-12-24 01:28:38 +03:00
DocumentItemTax::create($item_tax);
}
}
2020-12-24 01:28:38 +03:00
return $document_item;
}
}