akaunting/app/Jobs/Document/CreateDocumentItem.php

200 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;
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;
2020-12-24 01:28:38 +03:00
class CreateDocumentItem extends Job
{
2020-12-24 01:28:38 +03:00
protected $document;
2020-07-06 09:54:18 +03:00
protected $request;
/**
* Create a new job instance.
*
2020-12-24 01:28:38 +03:00
* @param $document
2020-07-06 09:54:18 +03:00
* @param $request
*/
2020-12-24 01:28:38 +03:00
public function __construct($document, $request)
{
2020-12-24 01:28:38 +03:00
$this->document = $document;
2020-07-06 09:54:18 +03:00
$this->request = $request;
}
/**
* Execute the job.
*
2020-12-24 01:28:38 +03:00
* @return DocumentItem
*/
public function handle()
{
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'];
2020-04-20 04:44:55 +03:00
$discount = 0;
2019-12-07 12:54:13 +03:00
$item_discounted_amount = $item_amount;
// Apply line discount to amount
2019-12-07 12:54:13 +03:00
if (!empty($this->request['discount'])) {
2020-04-20 04:44:55 +03:00
$discount += $this->request['discount'];
$item_discounted_amount = $item_amount -= ($item_amount * ($this->request['discount'] / 100));
}
// Apply global discount to amount
if (!empty($this->request['global_discount'])) {
2020-04-20 04:44:55 +03:00
$discount += $this->request['global_discount'];
$item_discounted_amount = $item_amount - ($item_amount * ($this->request['global_discount'] / 100));
}
$tax_amount = 0;
$item_taxes = [];
$item_tax_total = 0;
2020-12-24 01:28:38 +03:00
if (!empty($this->request['tax_ids'])) {
2019-12-07 12:54:13 +03:00
$inclusives = $compounds = [];
2020-12-24 01:28:38 +03:00
foreach ((array) $this->request['tax_ids'] as $tax_id) {
$tax = Tax::find($tax_id);
switch ($tax->type) {
2018-12-05 20:49:44 +05:30
case 'inclusive':
2018-11-07 12:07:38 +03:00
$inclusives[] = $tax;
2019-12-07 12:54:13 +03:00
break;
case 'compound':
$compounds[] = $tax;
2019-12-07 12:54:13 +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[] = [
2020-12-24 01:28:38 +03:00
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
2019-11-16 10:21:14 +03:00
'tax_id' => $tax_id,
'name' => $tax->name,
'amount' => $tax_amount,
];
$item_tax_total += $tax_amount;
2019-12-07 12:54:13 +03:00
2020-07-22 15:11:31 +03:00
break;
case 'withholding':
$tax_amount = 0 - $item_discounted_amount * ($tax->rate / 100);
$item_taxes[] = [
2020-12-24 01:28:38 +03:00
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
2020-07-22 15:11:31 +03:00
'tax_id' => $tax_id,
'name' => $tax->name,
'amount' => $tax_amount,
];
$item_tax_total += $tax_amount;
2019-11-16 10:21:14 +03:00
break;
default:
2020-07-22 15:11:31 +03:00
$tax_amount = $item_discounted_amount * ($tax->rate / 100);
$item_taxes[] = [
2020-12-24 01:28:38 +03:00
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
'tax_id' => $tax_id,
'name' => $tax->name,
'amount' => $tax_amount,
];
$item_tax_total += $tax_amount;
2019-12-07 12:54:13 +03:00
break;
}
}
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;
2019-11-16 10:21:14 +03:00
$item_base_rate = $item_amount / (1 + collect($inclusives)->sum('rate') / 100);
foreach ($inclusives as $inclusive) {
2020-07-06 09:54:18 +03:00
$tax_amount = $item_base_rate * ($inclusive->rate / 100);
$item_taxes[] = [
2020-12-24 01:28:38 +03:00
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
'tax_id' => $inclusive->id,
'name' => $inclusive->name,
'amount' => $tax_amount,
];
2020-07-06 09:54:18 +03:00
$item_tax_total += $tax_amount;
}
2020-04-20 04:44:55 +03:00
$item_amount = ($item_amount - $item_tax_total) / (1 - $discount / 100);
}
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;
$item_taxes[] = [
2020-12-24 01:28:38 +03:00
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
'tax_id' => $compound->id,
'name' => $compound->name,
'amount' => $tax_amount,
];
2020-07-06 09:54:18 +03:00
$item_tax_total += $tax_amount;
}
}
}
2020-12-24 01:28:38 +03:00
$document_item = DocumentItem::create([
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
'item_id' => $item_id,
2019-12-07 12:54:13 +03:00
'name' => Str::limit($this->request['name'], 180, ''),
2021-01-24 03:06:41 +03:00
'description' => !empty($this->request['description']) ? $this->request['description'] : '',
2019-12-07 12:54:13 +03:00
'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),
]);
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;
$document_item->compounds = $compounds;
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);
2020-12-24 01:28:38 +03:00
DocumentItemTax::create($item_tax);
}
}
2020-12-24 01:28:38 +03:00
return $document_item;
}
}