Merge pull request #2223 from burakcakirel/add-discount-as-an-amount

Adding discount as an amount
This commit is contained in:
Cüneyt Şentürk
2021-08-16 00:53:32 +03:00
committed by GitHub
6 changed files with 97 additions and 43 deletions

View File

@ -43,16 +43,13 @@ class CreateDocumentItem extends Job
// Apply line discount to amount
if (!empty($this->request['discount'])) {
$discount += $this->request['discount'];
$discount = $this->request['discount'];
$item_discounted_amount = $item_amount -= ($item_amount * ($this->request['discount'] / 100));
}
// Apply global discount to amount
if (!empty($this->request['global_discount'])) {
$discount += $this->request['global_discount'];
$item_discounted_amount = $item_amount - ($item_amount * ($this->request['global_discount'] / 100));
if ($this->request['discount_type'] === 'percentage') {
$item_discounted_amount = $item_amount -= ($item_amount * ($this->request['discount'] / 100));
} else {
$item_discounted_amount = $item_amount -= $this->request['discount'];
}
}
$tax_amount = 0;
@ -153,7 +150,11 @@ class CreateDocumentItem extends Job
$item_tax_total += $tax_amount;
}
$item_amount = ($item_amount - $item_tax_total) / (1 - $discount / 100);
if (!empty($this->request['discount_type']) && $this->request['discount_type'] === 'fixed') {
$item_amount = ($item_amount - $item_tax_total) - $discount;
} else {
$item_amount = ($item_amount - $item_tax_total) / (1 - $discount / 100);
}
}
if ($compounds) {
@ -185,6 +186,7 @@ class CreateDocumentItem extends Job
$this->request['quantity'] = (double) $this->request['quantity'];
$this->request['price'] = round($this->request['price'], $precision);
$this->request['tax'] = round($item_tax_total, $precision);
$this->request['discount_type'] = !empty($this->request['discount_type']) ? $this->request['discount_type'] : 'percentage';
$this->request['discount_rate'] = !empty($this->request['discount']) ? $this->request['discount'] : 0;
$this->request['total'] = round($item_amount, $precision);

View File

@ -67,15 +67,17 @@ class CreateDocumentItemsAndTotals extends Job
'sort_order' => $sort_order,
]);
$this->request['amount'] -= $discount_amount_total;
$sort_order++;
}
if (!empty($this->request['discount'])) {
$discount_total = ($sub_total - $discount_amount_total) * ($this->request['discount'] / 100);
if ($this->request['discount_type'] === 'percentage') {
$discount_total = $sub_total * ($this->request['discount'] / 100);
} else {
$discount_total = $this->request['discount'];
}
DocumentTotal::create([
DocumentTotal::create([
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
@ -193,11 +195,15 @@ class CreateDocumentItemsAndTotals extends Job
$discount_amount = 0;
if (!empty($item['discount'])) {
$discount_amount = ($item_amount * ($item['discount'] / 100));
if ($item['discount_type'] === 'percentage') {
$discount_amount = ($item_amount * ($item['discount'] / 100));
} else {
$discount_amount = $item['discount'];
}
}
// Calculate totals
$sub_total += $document_item->total + $discount_amount;
$sub_total += $document_item->total;
$discount_amount_total += $discount_amount;