akaunting/app/Jobs/Purchase/CreateBillItem.php

163 lines
5.0 KiB
PHP
Raw Normal View History

<?php
2019-12-31 15:49:09 +03:00
namespace App\Jobs\Purchase;
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;
use App\Models\Setting\Tax;
2019-11-16 10:21:14 +03:00
use Illuminate\Support\Str;
2019-11-16 10:21:14 +03:00
class CreateBillItem extends Job
{
2019-12-07 12:54:13 +03:00
protected $request;
protected $bill;
/**
* Create a new job instance.
*
2019-12-07 12:54:13 +03:00
* @param $request
* @param $bill
*/
2019-12-07 12:54:13 +03:00
public function __construct($request, $bill)
{
2019-12-07 12:54:13 +03:00
$this->request = $request;
$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'];
2019-12-07 12:54:13 +03:00
$item_discounted_amount = $item_amount;
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));
}
$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 = [];
2019-12-07 12:54:13 +03:00
foreach ((array) $this->request['tax_id'] 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-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;
default:
2019-12-07 12:54:13 +03:00
$tax_amount = ($item_discounted_amount / 100) * $tax->rate;
$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
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) {
$item_tax_total += $tax_amount = $item_base_rate * ($inclusive->rate / 100);
$item_taxes[] = [
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'tax_id' => $inclusive->id,
'name' => $inclusive->name,
'amount' => $tax_amount,
];
}
2019-12-07 12:54:13 +03:00
$item_amount = ($item_amount - $item_tax_total) / (1 - $this->request['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_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'],
'tax' => $item_tax_total,
'total' => $item_amount,
]);
$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)) {
$bill_item->item_taxes = $item_taxes;
2018-11-07 12:09:14 +03:00
$bill_item->inclusives = $inclusives;
$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;
}
}