Add "Item Discount" line in totals

This commit is contained in:
Burak Çakırel
2020-03-24 21:37:08 +03:00
parent 100fc2c07c
commit 3f068ec979
12 changed files with 189 additions and 42 deletions

View File

@ -64,7 +64,7 @@ class CreateBill extends Job
protected function createItemsAndTotals()
{
// Create items
list($sub_total, $taxes) = $this->createItems();
list($sub_total, $discount_amount_total, $taxes) = $this->createItems();
$sort_order = 1;
@ -83,8 +83,23 @@ class CreateBill extends Job
$sort_order++;
// Add discount
if ($discount_amount_total > 0) {
BillTotal::create([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'item_discount',
'name' => 'bills.item_discount',
'amount' => $discount_amount_total,
'sort_order' => $sort_order,
]);
$this->request['amount'] -= $discount_amount_total;
$sort_order++;
}
if (!empty($this->request['discount'])) {
$discount_total = $sub_total * ($this->request['discount'] / 100);
$discount_total = ($sub_total - $discount_amount_total) * ($this->request['discount'] / 100);
BillTotal::create([
'company_id' => $this->bill->company_id,
@ -155,7 +170,7 @@ class CreateBill extends Job
protected function createItems()
{
$sub_total = 0;
$sub_total = $discount_amount = $discount_amount_total = 0;
$taxes = [];
@ -170,8 +185,14 @@ class CreateBill extends Job
$bill_item = $this->dispatch(new CreateBillItem($item, $this->bill));
$item_amount = (double) $item['price'] * (double) $item['quantity'];
$discount_amount = ($item_amount * ($item['discount'] / 100));
// Calculate totals
$sub_total += $bill_item->total;
$sub_total += $bill_item->total + $discount_amount;
$discount_amount_total += $discount_amount;
if (!$bill_item->item_taxes) {
continue;
@ -190,6 +211,6 @@ class CreateBill extends Job
}
}
return [$sub_total, $taxes];
return [$sub_total, $discount_amount_total, $taxes];
}
}