improved invoice/bill jobs
This commit is contained in:
@ -10,24 +10,20 @@ use Illuminate\Support\Str;
|
||||
|
||||
class CreateInvoiceItem extends Job
|
||||
{
|
||||
protected $data;
|
||||
protected $request;
|
||||
|
||||
protected $invoice;
|
||||
|
||||
protected $discount;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $data
|
||||
* @param $request
|
||||
* @param $invoice
|
||||
* @param $discount
|
||||
*/
|
||||
public function __construct($data, $invoice, $discount = null)
|
||||
public function __construct($request, $invoice)
|
||||
{
|
||||
$this->data = $data;
|
||||
$this->request = $request;
|
||||
$this->invoice = $invoice;
|
||||
$this->discount = $discount;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -37,36 +33,37 @@ class CreateInvoiceItem extends Job
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$item_id = !empty($this->data['item_id']) ? $this->data['item_id'] : 0;
|
||||
$item_amount = (double) $this->data['price'] * (double) $this->data['quantity'];
|
||||
$item_id = !empty($this->request['item_id']) ? $this->request['item_id'] : 0;
|
||||
$item_amount = (double) $this->request['price'] * (double) $this->request['quantity'];
|
||||
|
||||
$item_discount_amount = $item_amount;
|
||||
$item_discounted_amount = $item_amount;
|
||||
|
||||
// Apply discount to tax
|
||||
if ($this->discount) {
|
||||
$item_discount_amount = $item_amount - ($item_amount * ($this->discount / 100));
|
||||
// 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;
|
||||
|
||||
if (!empty($this->data['tax_id'])) {
|
||||
$inclusives = $compounds = $taxes = $fixed_taxes = [];
|
||||
if (!empty($this->request['tax_id'])) {
|
||||
$inclusives = $compounds = [];
|
||||
|
||||
foreach ((array)$this->data['tax_id'] as $tax_id) {
|
||||
foreach ((array) $this->request['tax_id'] as $tax_id) {
|
||||
$tax = Tax::find($tax_id);
|
||||
|
||||
switch ($tax->type) {
|
||||
case 'inclusive':
|
||||
$inclusives[] = $tax;
|
||||
|
||||
break;
|
||||
case 'compound':
|
||||
$compounds[] = $tax;
|
||||
|
||||
break;
|
||||
case 'fixed':
|
||||
$fixed_taxes[] = $tax;
|
||||
$tax_amount = $tax->rate;
|
||||
$tax_amount = $tax->rate * (double) $this->request['quantity'];
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->invoice->company_id,
|
||||
@ -77,12 +74,10 @@ class CreateInvoiceItem extends Job
|
||||
];
|
||||
|
||||
$item_tax_total += $tax_amount;
|
||||
|
||||
break;
|
||||
case 'normal':
|
||||
default:
|
||||
$taxes[] = $tax;
|
||||
|
||||
$tax_amount = ($item_discount_amount / 100) * $tax->rate;
|
||||
$tax_amount = ($item_discounted_amount / 100) * $tax->rate;
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->invoice->company_id,
|
||||
@ -93,12 +88,13 @@ class CreateInvoiceItem extends Job
|
||||
];
|
||||
|
||||
$item_tax_total += $tax_amount;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($inclusives) {
|
||||
$item_amount = $item_discount_amount + $item_tax_total;
|
||||
$item_amount = $item_discounted_amount + $item_tax_total;
|
||||
|
||||
$item_base_rate = $item_amount / (1 + collect($inclusives)->sum('rate') / 100);
|
||||
|
||||
@ -114,12 +110,12 @@ class CreateInvoiceItem extends Job
|
||||
];
|
||||
}
|
||||
|
||||
$item_amount = ($item_amount - $item_tax_total) / (1 - $this->discount / 100);
|
||||
$item_amount = ($item_amount - $item_tax_total) / (1 - $this->request['discount'] / 100);
|
||||
}
|
||||
|
||||
if ($compounds) {
|
||||
foreach ($compounds as $compound) {
|
||||
$tax_amount = (($item_discount_amount + $item_tax_total) / 100) * $compound->rate;
|
||||
$tax_amount = (($item_discounted_amount + $item_tax_total) / 100) * $compound->rate;
|
||||
|
||||
$item_tax_total += $tax_amount;
|
||||
|
||||
@ -138,9 +134,9 @@ class CreateInvoiceItem extends Job
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'item_id' => $item_id,
|
||||
'name' => Str::limit($this->data['name'], 180, ''),
|
||||
'quantity' => (double) $this->data['quantity'],
|
||||
'price' => (double) $this->data['price'],
|
||||
'name' => Str::limit($this->request['name'], 180, ''),
|
||||
'quantity' => (double) $this->request['quantity'],
|
||||
'price' => (double) $this->request['price'],
|
||||
'tax' => $item_tax_total,
|
||||
'total' => $item_amount,
|
||||
]);
|
||||
@ -149,28 +145,15 @@ class CreateInvoiceItem extends Job
|
||||
$invoice_item->inclusives = false;
|
||||
$invoice_item->compounds = false;
|
||||
|
||||
// set item_taxes for
|
||||
if (!empty($this->data['tax_id'])) {
|
||||
if (!empty($item_taxes)) {
|
||||
$invoice_item->item_taxes = $item_taxes;
|
||||
$invoice_item->inclusives = $inclusives;
|
||||
$invoice_item->compounds = $compounds;
|
||||
}
|
||||
|
||||
if ($item_taxes) {
|
||||
foreach ($item_taxes as $item_tax) {
|
||||
$item_tax['invoice_item_id'] = $invoice_item->id;
|
||||
|
||||
InvoiceItemTax::create($item_tax);
|
||||
|
||||
// Set taxes
|
||||
if (isset($taxes) && array_key_exists($item_tax['tax_id'], $taxes)) {
|
||||
$taxes[$item_tax['tax_id']]['amount'] += $item_tax['amount'];
|
||||
} else {
|
||||
$taxes[$item_tax['tax_id']] = [
|
||||
'name' => $item_tax['name'],
|
||||
'amount' => $item_tax['amount']
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user