Skip to content

Commit

Permalink
Adding discount as an amount #kbcqjv
Browse files Browse the repository at this point in the history
  • Loading branch information
burakcakirel committed Aug 15, 2021
1 parent 538e116 commit e8720c6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 30 deletions.
22 changes: 12 additions & 10 deletions app/Jobs/Document/CreateDocumentItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,13 @@ public function handle()

// 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'] === 'normal') {
$item_discounted_amount = $item_amount -= ($item_amount * ($this->request['discount'] / 100));
} else {
$item_discounted_amount = $item_amount -= $this->request['discount'];
}
}

$tax_amount = 0;
Expand Down Expand Up @@ -153,7 +150,11 @@ public function handle()
$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'] === 'normal') {
$item_amount = ($item_amount - $item_tax_total) / (1 - $discount / 100);
} else {
$item_amount = ($item_amount - $item_tax_total) - $discount;
}
}

if ($compounds) {
Expand Down Expand Up @@ -185,6 +186,7 @@ public function handle()
$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'] : 'normal';
$this->request['discount_rate'] = !empty($this->request['discount']) ? $this->request['discount'] : 0;
$this->request['total'] = round($item_amount, $precision);

Expand Down
18 changes: 12 additions & 6 deletions app/Jobs/Document/CreateDocumentItemsAndTotals.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ public function handle()
'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'] === 'normal') {
$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,
Expand Down Expand Up @@ -193,11 +195,15 @@ protected function createItems()
$discount_amount = 0;

if (!empty($item['discount'])) {
$discount_amount = ($item_amount * ($item['discount'] / 100));
if ($item['discount_type'] === 'normal') {
$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;

Expand Down
12 changes: 7 additions & 5 deletions resources/assets/js/views/common/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const app = new Vue({
},

mounted() {
this.form.discount_type = 'percentage';
this.form.discount_type = 'normal';

if ((document.getElementById('items') != null) && (document.getElementById('items').rows)) {
this.colspan = document.getElementById("items").rows[0].cells.length - 1;
Expand Down Expand Up @@ -118,7 +118,7 @@ const app = new Vue({
let line_discount_amount = 0;

if (item.discount) {
if (item.discount_type === 'percentage') {
if (item.discount_type === 'normal') {
if (item.discount > 100) {
item.discount = 100;
}
Expand Down Expand Up @@ -249,7 +249,7 @@ const app = new Vue({

// Apply discount to total
if (global_discount) {
if (this.form.discount_type === 'percentage') {
if (this.form.discount_type === 'normal') {
discount_total = parseFloat(sub_total + inclusive_tax_total) * (global_discount / 100);
} else {
discount_total = global_discount;
Expand Down Expand Up @@ -398,7 +398,7 @@ const app = new Vue({
},

onAddLineDiscount(item_index) {
this.items[item_index].discount_type = 'percentage';
this.items[item_index].discount_type = 'normal';
this.items[item_index].add_discount = true;
},

Expand All @@ -415,7 +415,7 @@ const app = new Vue({
onAddTotalDiscount() {
let discount = document.getElementById('pre-discount').value;

if (this.form.discount_type === 'percentage') {
if (this.form.discount_type === 'normal') {
if (discount < 0) {
discount = 0;
} else if (discount > 100) {
Expand Down Expand Up @@ -649,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)
});

Expand Down Expand Up @@ -686,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: [],
Expand Down
8 changes: 4 additions & 4 deletions resources/views/components/documents/form/line-item.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ class="form-control text-center p-0 input-number-disabled"
<div class="form-group mb-0 w-100" style="display: inline-block; position: relative;">
<div class="input-group mb-0 select-tax">
<div class="input-group-prepend">
<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">
<button class="btn btn-sm" :class="[{'btn-outline-primary' : row.discount_type !== 'normal'}, {'btn-primary' : row.discount_type === 'normal'}]"
@click="onChangeLineDiscountType(index, 'normal')" type="button">
<i class="fa fa-percent fa-sm"></i>
</button>
<button class="btn btn-sm" :class="[{'btn-outline-primary' : row.discount_type !== 'amount'}, {'btn-primary' : row.discount_type === 'amount'}]"
@click="onChangeLineDiscountType(index, 'amount')" type="button">{{ $currency->symbol }}
<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"
Expand Down
8 changes: 4 additions & 4 deletions resources/views/components/documents/form/totals.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
<div class="col-sm-8">
<div class="input-group">
<div class="input-group-prepend">
<button class="btn btn-sm" :class="[{'btn-outline-primary' : form.discount_type !== 'percentage'}, {'btn-primary' : form.discount_type === 'percentage'}]"
@click="onChangeDiscountType('percentage')" type="button">
<button class="btn btn-sm" :class="[{'btn-outline-primary' : form.discount_type !== 'normal'}, {'btn-primary' : form.discount_type === 'normal'}]"
@click="onChangeDiscountType('normal')" type="button">
<i class="fa fa-percent fa-sm"></i>
</button>
<button class="btn btn-sm" :class="[{'btn-outline-primary' : form.discount_type !== 'amount'}, {'btn-primary' : form.discount_type === 'amount'}]"
@click="onChangeDiscountType('amount')" type="button">{{ $currency->symbol }}
<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']) !!}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 === 'normal')
<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
Expand Down

0 comments on commit e8720c6

Please sign in to comment.