2018-11-07 11:06:38 +03:00
|
|
|
<?php
|
|
|
|
|
2019-12-31 15:49:09 +03:00
|
|
|
namespace App\Jobs\Purchase;
|
2018-11-07 11:06:38 +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\Purchase\BillItem;
|
|
|
|
use App\Models\Purchase\BillItemTax;
|
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
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
class CreateBillItem extends Job
|
2018-11-07 11:06:38 +03:00
|
|
|
{
|
2019-12-07 12:54:13 +03:00
|
|
|
protected $request;
|
2018-11-07 11:06:38 +03:00
|
|
|
|
|
|
|
protected $bill;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
2019-12-07 12:54:13 +03:00
|
|
|
* @param $request
|
2018-11-07 11:06:38 +03:00
|
|
|
* @param $bill
|
|
|
|
*/
|
2019-12-07 12:54:13 +03:00
|
|
|
public function __construct($request, $bill)
|
2018-11-07 11:06:38 +03:00
|
|
|
{
|
2019-12-07 12:54:13 +03:00
|
|
|
$this->request = $request;
|
2018-11-07 11:06:38 +03:00
|
|
|
$this->bill = $bill;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return BillItem
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2019-12-07 12:54:13 +03:00
|
|
|
$item_id = !empty($this->request['item_id']) ? $this->request['item_id'] : 0;
|
|
|
|
$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
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
// Apply discount to amount
|
|
|
|
if (!empty($this->request['discount'])) {
|
|
|
|
$item_discounted_amount = $item_amount - ($item_amount * ($this->request['discount'] / 100));
|
2018-11-07 11:06:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$tax_amount = 0;
|
|
|
|
$item_taxes = [];
|
|
|
|
$item_tax_total = 0;
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
if (!empty($this->request['tax_id'])) {
|
|
|
|
$inclusives = $compounds = [];
|
2018-11-07 11:06:38 +03:00
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
foreach ((array) $this->request['tax_id'] as $tax_id) {
|
2018-11-07 11:06:38 +03:00
|
|
|
$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
|
|
|
|
2018-11-07 11:06:38 +03:00
|
|
|
break;
|
|
|
|
case 'compound':
|
|
|
|
$compounds[] = $tax;
|
2019-12-07 12:54:13 +03:00
|
|
|
|
2018-11-07 11:06:38 +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-03-16 00:36:33 +03:00
|
|
|
'company_id' => $this->bill->company_id,
|
|
|
|
'bill_id' => $this->bill->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
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
break;
|
2018-11-07 11:06:38 +03:00
|
|
|
default:
|
2019-12-07 12:54:13 +03:00
|
|
|
$tax_amount = ($item_discounted_amount / 100) * $tax->rate;
|
2018-11-07 11:06:38 +03:00
|
|
|
|
|
|
|
$item_taxes[] = [
|
|
|
|
'company_id' => $this->bill->company_id,
|
|
|
|
'bill_id' => $this->bill->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-07 11:06:38 +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-02-03 21:01:08 +05:30
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$item_base_rate = $item_amount / (1 + collect($inclusives)->sum('rate') / 100);
|
2019-03-07 12:01:32 +03:00
|
|
|
|
2019-02-03 21:01:08 +05:30
|
|
|
foreach ($inclusives as $inclusive) {
|
|
|
|
$item_tax_total += $tax_amount = $item_base_rate * ($inclusive->rate / 100);
|
|
|
|
|
|
|
|
$item_taxes[] = [
|
2019-03-07 12:01:32 +03:00
|
|
|
'company_id' => $this->bill->company_id,
|
|
|
|
'bill_id' => $this->bill->id,
|
|
|
|
'tax_id' => $inclusive->id,
|
|
|
|
'name' => $inclusive->name,
|
|
|
|
'amount' => $tax_amount,
|
2019-02-03 21:01:08 +05:30
|
|
|
];
|
2018-11-07 11:06:38 +03:00
|
|
|
}
|
2019-02-03 21:01:08 +05:30
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$item_amount = ($item_amount - $item_tax_total) / (1 - $this->request['discount'] / 100);
|
2018-11-07 11:06:38 +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-07 11:06:38 +03:00
|
|
|
|
|
|
|
$item_tax_total += $tax_amount;
|
|
|
|
|
|
|
|
$item_taxes[] = [
|
|
|
|
'company_id' => $this->bill->company_id,
|
|
|
|
'bill_id' => $this->bill->id,
|
|
|
|
'tax_id' => $compound->id,
|
|
|
|
'name' => $compound->name,
|
|
|
|
'amount' => $tax_amount,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$bill_item = BillItem::create([
|
|
|
|
'company_id' => $this->bill->company_id,
|
|
|
|
'bill_id' => $this->bill->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'],
|
|
|
|
'price' => (double) $this->request['price'],
|
2018-11-07 11:06:38 +03:00
|
|
|
'tax' => $item_tax_total,
|
|
|
|
'total' => $item_amount,
|
|
|
|
]);
|
|
|
|
|
2018-11-07 17:58:55 +03:00
|
|
|
$bill_item->item_taxes = false;
|
|
|
|
$bill_item->inclusives = false;
|
|
|
|
$bill_item->compounds = false;
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
if (!empty($item_taxes)) {
|
2018-11-07 11:06:38 +03:00
|
|
|
$bill_item->item_taxes = $item_taxes;
|
2018-11-07 12:09:14 +03:00
|
|
|
$bill_item->inclusives = $inclusives;
|
2018-11-07 11:06:38 +03:00
|
|
|
$bill_item->compounds = $compounds;
|
|
|
|
|
|
|
|
foreach ($item_taxes as $item_tax) {
|
|
|
|
$item_tax['bill_item_id'] = $bill_item->id;
|
|
|
|
|
|
|
|
BillItemTax::create($item_tax);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bill_item;
|
|
|
|
}
|
|
|
|
}
|