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

View File

@ -67,15 +67,17 @@ class CreateDocumentItemsAndTotals extends Job
'sort_order' => $sort_order, 'sort_order' => $sort_order,
]); ]);
$this->request['amount'] -= $discount_amount_total;
$sort_order++; $sort_order++;
} }
if (!empty($this->request['discount'])) { 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, 'company_id' => $this->document->company_id,
'type' => $this->document->type, 'type' => $this->document->type,
'document_id' => $this->document->id, 'document_id' => $this->document->id,
@ -193,11 +195,15 @@ class CreateDocumentItemsAndTotals extends Job
$discount_amount = 0; $discount_amount = 0;
if (!empty($item['discount'])) { 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 // Calculate totals
$sub_total += $document_item->total + $discount_amount; $sub_total += $document_item->total;
$discount_amount_total += $discount_amount; $discount_amount_total += $discount_amount;

View File

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

View File

@ -76,7 +76,7 @@
@if (!$hideQuantity) @if (!$hideQuantity)
<div> <div>
@stack('quantity_input_start') @stack('quantity_input_start')
<input <input
type="number" type="number"
min="0" min="0"
class="form-control text-center p-0 input-number-disabled" class="form-control text-center p-0 input-number-disabled"
@ -161,11 +161,15 @@
</div> </div>
@stack('discount_input_start') @stack('discount_input_start')
<div class="form-group mb-0 w-100" style="display: inline-block; position: relative;"> <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"> <div class="input-group-prepend">
<span class="input-group-text" id="input-discount"> <button class="btn btn-sm" :class="[{'btn-outline-primary' : row.discount_type !== 'percentage'}, {'btn-primary' : row.discount_type === 'percentage'}]"
<i class="fa fa-percent"></i> @click="onChangeLineDiscountType(index, 'percentage')" type="button">
</span> <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> </div>
<input type="number" <input type="number"
max="100" max="100"
@ -227,7 +231,7 @@
></akaunting-select> ></akaunting-select>
@stack('taxes_input_end') @stack('taxes_input_end')
</div> </div>
<div class="line-item-content-right"> <div class="line-item-content-right">
<div class="line-item-content-right-price long-texts text-right"> <div class="line-item-content-right-price long-texts text-right">
{{ Form::moneyGroup('tax', '', '', ['required' => 'required', 'disabled' => 'true' , 'row-input' => 'true', 'v-model' => 'row_tax.price', 'data-item' => 'total', 'currency' => $currency, 'dynamic-currency' => 'currency'], 0.00, 'text-right input-price disabled-money') }} {{ Form::moneyGroup('tax', '', '', ['required' => 'required', 'disabled' => 'true' , 'row-input' => 'true', 'v-model' => 'row_tax.price', 'data-item' => 'total', 'currency' => $currency, 'dynamic-currency' => 'currency'], 0.00, 'text-right input-price disabled-money') }}
@ -242,7 +246,7 @@
<div v-if="row.add_tax" class="line-item-area pb-3" :class="{'pt-2' : row.add_discount}"> <div v-if="row.add_tax" class="line-item-area pb-3" :class="{'pt-2' : row.add_discount}">
<div class="line-item-content"> <div class="line-item-content">
<div class="long-texts line-item-text" style="float: left; margin-top: 15px; margin-right:2px; position: absolute; left: -63px;"> <div class="long-texts line-item-text" style="float: left; margin-top: 15px; margin-right:2px; position: absolute; left: -63px;">
{{ trans_choice('general.taxes', 1) }} {{ trans_choice('general.taxes', 1) }}
</div> </div>
@stack('taxes_input_start') @stack('taxes_input_start')

View File

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

View File

@ -33,7 +33,11 @@
@if (!$hideDiscount) @if (!$hideDiscount)
@if (in_array(setting('localisation.discount_location', 'total'), ['item', 'both'])) @if (in_array(setting('localisation.discount_location', 'total'), ['item', 'both']))
@stack('discount_td_start') @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') @stack('discount_td_end')
@endif @endif
@endif @endif