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
commit 10a4be8ca4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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;

View File

@ -70,6 +70,8 @@ const app = new Vue({
},
mounted() {
this.form.discount_type = 'percentage';
if ((document.getElementById('items') != null) && (document.getElementById('items').rows)) {
this.colspan = document.getElementById("items").rows[0].cells.length - 1;
}
@ -116,7 +118,19 @@ const app = new Vue({
let line_discount_amount = 0;
if (item.discount) {
line_discount_amount = item.total * (item.discount / 100);
if (item.discount_type === 'percentage') {
if (item.discount > 100) {
item.discount = 100;
}
line_discount_amount = item.total * (item.discount / 100);
} else {
if (parseInt(item.discount) > item.price) {
item.discount = item.price;
}
line_discount_amount = parseFloat(item.discount);
}
item.discount_amount = line_discount_amount
item_discounted_total = item.total -= line_discount_amount;
@ -125,12 +139,6 @@ const app = new Vue({
let item_discounted_total = item.total;
if (global_discount) {
item_discounted_total = item.total - (item.total * (global_discount / 100));
item_discount = global_discount;
}
// item tax calculate.
if (item.tax_ids) {
let inclusives = [];
@ -241,7 +249,11 @@ const app = new Vue({
// Apply discount to total
if (global_discount) {
discount_total = parseFloat(sub_total + inclusive_tax_total) * (global_discount / 100);
if (this.form.discount_type === 'percentage') {
discount_total = parseFloat(sub_total + inclusive_tax_total) * (global_discount / 100);
} else {
discount_total = global_discount;
}
this.totals.discount = discount_total;
@ -386,16 +398,35 @@ const app = new Vue({
},
onAddLineDiscount(item_index) {
this.items[item_index].discount_type = 'percentage';
this.items[item_index].add_discount = true;
},
onChangeDiscountType(type) {
this.form.discount_type = type;
this.onCalculateTotal();
},
onChangeLineDiscountType(item_index, type) {
this.items[item_index].discount_type = type;
this.onCalculateTotal();
},
onAddTotalDiscount() {
let discount = document.getElementById('pre-discount').value;
if (discount < 0) {
discount = 0;
} else if (discount > 100) {
discount = 100;
if (this.form.discount_type === 'percentage') {
if (discount < 0) {
discount = 0;
} else if (discount > 100) {
discount = 100;
}
} else {
if (discount < 0) {
discount = 0;
} else if (discount > this.totals.sub) {
discount = this.totals.sub;
}
}
document.getElementById('pre-discount').value = discount;
@ -618,6 +649,7 @@ const app = new Vue({
price: (item.price).toFixed(2),
tax_ids: item.tax_ids,
discount: item.discount_rate,
discount_type: item.discount_type,
total: (item.total).toFixed(2)
});
@ -655,6 +687,7 @@ const app = new Vue({
tax_ids: item_taxes,
add_discount: (item.discount_rate) ? true : false,
discount: item.discount_rate,
discount_type: item.discount_type,
total: (item.total).toFixed(2),
// @todo
// invoice_item_checkbox_sample: [],

View File

@ -161,11 +161,15 @@
</div>
@stack('discount_input_start')
<div class="form-group mb-0 w-100" style="display: inline-block; position: relative;">
<div class="input-group input-group-merge mb-0 select-tax">
<div class="input-group mb-0 select-tax">
<div class="input-group-prepend">
<span class="input-group-text" id="input-discount">
<i class="fa fa-percent"></i>
</span>
<button class="btn btn-sm" :class="[{'btn-outline-primary' : row.discount_type !== 'percentage'}, {'btn-primary' : row.discount_type === 'percentage'}]"
@click="onChangeLineDiscountType(index, 'percentage')" type="button">
<i class="fa fa-percent fa-sm"></i>
</button>
<button class="btn btn-sm" :class="[{'btn-outline-primary' : row.discount_type !== 'fixed'}, {'btn-primary' : row.discount_type === 'fixed'}]"
@click="onChangeLineDiscountType(index, 'fixed')" type="button">{{ $currency->symbol }}
</button>
</div>
<input type="number"
max="100"

View File

@ -49,27 +49,32 @@
<el-popover
popper-class="p-0 h-0"
placement="bottom"
width="300"
width="350"
v-model="discount">
<div class="card d-none" :class="[{'show' : discount}]">
<div class="discount card-body">
<div class="row align-items-center">
<div class="col-sm-6">
<div class="input-group input-group-merge">
<div class="col-sm-8">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="input-discount">
<i class="fa fa-percent"></i>
</span>
<button class="btn btn-sm" :class="[{'btn-outline-primary' : form.discount_type !== 'percentage'}, {'btn-primary' : form.discount_type === 'percentage'}]"
@click="onChangeDiscountType('percentage')" type="button">
<i class="fa fa-percent fa-sm"></i>
</button>
<button class="btn btn-sm" :class="[{'btn-outline-primary' : form.discount_type !== 'fixed'}, {'btn-primary' : form.discount_type === 'fixed'}]"
@click="onChangeDiscountType('fixed')" type="button">{{ $currency->symbol }}
</button>
</div>
{!! Form::number('pre_discount', null, ['id' => 'pre-discount', 'class' => 'form-control', 'v-model' => 'form.discount']) !!}
</div>
</div>
<div class="col-sm-6">
<div class="col-sm-4">
<div class="discount-description">
<strong>{{ trans('invoices.discount_desc') }}</strong>
</div>
</div>
</div>
</div>
</div>
<div class="discount card-footer">
<div class="row float-right">

View File

@ -33,7 +33,11 @@
@if (!$hideDiscount)
@if (in_array(setting('localisation.discount_location', 'total'), ['item', 'both']))
@stack('discount_td_start')
<td class="discount">{{ $item->discount }}</td>
@if ($item->discount_type === 'percentage')
<td class="discount">{{ $item->discount }}</td>
@else
<td class="discount">@money($item->discount, $document->currency_code, true)</td>
@endif
@stack('discount_td_end')
@endif
@endif