v2 first commit
This commit is contained in:
@ -2,18 +2,14 @@
|
||||
|
||||
namespace App\Jobs\Income;
|
||||
|
||||
use App\Models\Common\Item;
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Income\InvoiceItem;
|
||||
use App\Models\Income\InvoiceItemTax;
|
||||
use App\Models\Setting\Tax;
|
||||
use App\Notifications\Common\Item as ItemNotification;
|
||||
use App\Notifications\Common\ItemReminder as ItemReminderNotification;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreateInvoiceItem
|
||||
class CreateInvoiceItem extends Job
|
||||
{
|
||||
use Dispatchable;
|
||||
|
||||
protected $data;
|
||||
|
||||
protected $invoice;
|
||||
@ -41,8 +37,6 @@ class CreateInvoiceItem
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$item_sku = '';
|
||||
|
||||
$item_id = !empty($this->data['item_id']) ? $this->data['item_id'] : 0;
|
||||
$item_amount = (double) $this->data['price'] * (double) $this->data['quantity'];
|
||||
|
||||
@ -53,54 +47,14 @@ class CreateInvoiceItem
|
||||
$item_discount_amount = $item_amount - ($item_amount * ($this->discount / 100));
|
||||
}
|
||||
|
||||
if (!empty($item_id)) {
|
||||
$item_object = Item::find($item_id);
|
||||
|
||||
$this->data['name'] = $item_object->name;
|
||||
$item_sku = $item_object->sku;
|
||||
|
||||
// Decrease stock (item sold)
|
||||
$item_object->quantity -= (double) $this->data['quantity'];
|
||||
$item_object->save();
|
||||
|
||||
if (setting('general.send_item_reminder')) {
|
||||
$item_stocks = explode(',', setting('general.schedule_item_stocks'));
|
||||
|
||||
foreach ($item_stocks as $item_stock) {
|
||||
if ($item_object->quantity == $item_stock) {
|
||||
foreach ($item_object->company->users as $user) {
|
||||
if (!$user->can('read-notifications')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$user->notify(new ItemReminderNotification($item_object));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Notify users if out of stock
|
||||
if ($item_object->quantity == 0) {
|
||||
foreach ($item_object->company->users as $user) {
|
||||
if (!$user->can('read-notifications')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$user->notify(new ItemNotification($item_object));
|
||||
}
|
||||
}
|
||||
} elseif (!empty($this->data['sku'])) {
|
||||
$item_sku = $this->data['sku'];
|
||||
}
|
||||
|
||||
$tax_amount = 0;
|
||||
$item_taxes = [];
|
||||
$item_tax_total = 0;
|
||||
|
||||
if (!empty($this->data['tax_id'])) {
|
||||
$inclusives = $compounds = $taxes = [];
|
||||
$inclusives = $compounds = $taxes = $fixed_taxes = [];
|
||||
|
||||
foreach ((array) $this->data['tax_id'] as $tax_id) {
|
||||
foreach ((array)$this->data['tax_id'] as $tax_id) {
|
||||
$tax = Tax::find($tax_id);
|
||||
|
||||
switch ($tax->type) {
|
||||
@ -110,6 +64,20 @@ class CreateInvoiceItem
|
||||
case 'compound':
|
||||
$compounds[] = $tax;
|
||||
break;
|
||||
case 'fixed':
|
||||
$fixed_taxes[] = $tax;
|
||||
$tax_amount = $tax->rate;
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'tax_id' => $tax_id,
|
||||
'name' => $tax->name,
|
||||
'amount' => $tax_amount,
|
||||
];
|
||||
|
||||
$item_tax_total += $tax_amount;
|
||||
break;
|
||||
case 'normal':
|
||||
default:
|
||||
$taxes[] = $tax;
|
||||
@ -132,7 +100,7 @@ class CreateInvoiceItem
|
||||
if ($inclusives) {
|
||||
$item_amount = $item_discount_amount + $item_tax_total;
|
||||
|
||||
$item_base_rate = $item_amount / (1 + collect($inclusives)->sum('rate')/100);
|
||||
$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);
|
||||
@ -170,8 +138,7 @@ class CreateInvoiceItem
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'item_id' => $item_id,
|
||||
'name' => str_limit($this->data['name'], 180, ''),
|
||||
'sku' => $item_sku,
|
||||
'name' => Str::limit($this->data['name'], 180, ''),
|
||||
'quantity' => (double) $this->data['quantity'],
|
||||
'price' => (double) $this->data['price'],
|
||||
'tax' => $item_tax_total,
|
||||
|
Reference in New Issue
Block a user