Skip to content

Commit

Permalink
Add "Item Discount" line in totals
Browse files Browse the repository at this point in the history
  • Loading branch information
burakcakirel committed Mar 24, 2020
1 parent 100fc2c commit 3f068ec
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 42 deletions.
8 changes: 1 addition & 7 deletions app/Http/Controllers/Settings/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@ public function edit()

$payment_methods = Modules::getPaymentMethods();

$discount_locations = [
'in_totals' => trans('settings.default.discount_in_totals'),
'per_item' => trans('settings.default.discount_per_item'),
];

return view('settings.default.edit', compact(
'setting',
'accounts',
'currencies',
'taxes',
'payment_methods',
'discount_locations'
'payment_methods'
));
}
}
31 changes: 26 additions & 5 deletions app/Jobs/Purchase/CreateBill.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function handle()
protected function createItemsAndTotals()
{
// Create items
list($sub_total, $taxes) = $this->createItems();
list($sub_total, $discount_amount_total, $taxes) = $this->createItems();

$sort_order = 1;

Expand All @@ -83,8 +83,23 @@ protected function createItemsAndTotals()
$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,
Expand Down Expand Up @@ -155,7 +170,7 @@ protected function createItemsAndTotals()

protected function createItems()
{
$sub_total = 0;
$sub_total = $discount_amount = $discount_amount_total = 0;

$taxes = [];

Expand All @@ -170,8 +185,14 @@ protected function createItems()

$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;
Expand All @@ -190,6 +211,6 @@ protected function createItems()
}
}

return [$sub_total, $taxes];
return [$sub_total, $discount_amount_total, $taxes];
}
}
31 changes: 26 additions & 5 deletions app/Jobs/Purchase/UpdateBill.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function handle()
protected function createItemsAndTotals()
{
// Create items
list($sub_total, $taxes) = $this->createItems();
list($sub_total, $discount_amount_total, $taxes) = $this->createItems();

// Delete current totals
$this->deleteRelationships($this->bill, 'totals');
Expand All @@ -94,8 +94,23 @@ protected function createItemsAndTotals()
$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,
Expand Down Expand Up @@ -166,7 +181,7 @@ protected function createItemsAndTotals()

protected function createItems()
{
$sub_total = 0;
$sub_total = $discount_amount = $discount_amount_total = 0;

$taxes = [];

Expand All @@ -184,8 +199,14 @@ protected function createItems()

$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;
Expand All @@ -204,6 +225,6 @@ protected function createItems()
}
}

return [$sub_total, $taxes];
return [$sub_total, $discount_amount_total, $taxes];
}
}
31 changes: 26 additions & 5 deletions app/Jobs/Sale/CreateInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function handle()
protected function createItemsAndTotals()
{
// Create items
list($sub_total, $taxes) = $this->createItems();
list($sub_total, $discount_amount_total, $taxes) = $this->createItems();

$sort_order = 1;

Expand All @@ -83,8 +83,23 @@ protected function createItemsAndTotals()
$sort_order++;

// Add discount
if ($discount_amount_total > 0) {
InvoiceTotal::create([
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'code' => 'item_discount',
'name' => 'invoices.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);

InvoiceTotal::create([
'company_id' => $this->invoice->company_id,
Expand Down Expand Up @@ -155,7 +170,7 @@ protected function createItemsAndTotals()

protected function createItems()
{
$sub_total = 0;
$sub_total = $discount_amount = $discount_amount_total = 0;

$taxes = [];

Expand All @@ -170,8 +185,14 @@ protected function createItems()

$invoice_item = $this->dispatch(new CreateInvoiceItem($item, $this->invoice));

$item_amount = (double) $item['price'] * (double) $item['quantity'];

$discount_amount = ($item_amount * ($item['discount'] / 100));

// Calculate totals
$sub_total += $invoice_item->total;
$sub_total += $invoice_item->total + $discount_amount;

$discount_amount_total += $discount_amount;

if (!$invoice_item->item_taxes) {
continue;
Expand All @@ -190,6 +211,6 @@ protected function createItems()
}
}

return [$sub_total, $taxes];
return [$sub_total, $discount_amount_total, $taxes];
}
}
31 changes: 26 additions & 5 deletions app/Jobs/Sale/UpdateInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function handle()
protected function createItemsAndTotals()
{
// Create items
list($sub_total, $taxes) = $this->createItems();
list($sub_total, $discount_amount_total, $taxes) = $this->createItems();

// Delete current totals
$this->deleteRelationships($this->invoice, 'totals');
Expand All @@ -94,8 +94,23 @@ protected function createItemsAndTotals()
$sort_order++;

// Add discount
if ($discount_amount_total > 0) {
InvoiceTotal::create([
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'code' => 'item_discount',
'name' => 'invoices.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);

InvoiceTotal::create([
'company_id' => $this->invoice->company_id,
Expand Down Expand Up @@ -166,7 +181,7 @@ protected function createItemsAndTotals()

protected function createItems()
{
$sub_total = 0;
$sub_total = $discount_amount = $discount_amount_total = 0;

$taxes = [];

Expand All @@ -184,8 +199,14 @@ protected function createItems()

$invoice_item = $this->dispatch(new CreateInvoiceItem($item, $this->invoice));

$item_amount = (double) $item['price'] * (double) $item['quantity'];

$discount_amount = ($item_amount * ($item['discount'] / 100));

// Calculate totals
$sub_total += $invoice_item->total;
$sub_total += $invoice_item->total + $discount_amount;

$discount_amount_total += $discount_amount;

if (!$invoice_item->item_taxes) {
continue;
Expand All @@ -204,6 +225,6 @@ protected function createItems()
}
}

return [$sub_total, $taxes];
return [$sub_total, $discount_amount_total, $taxes];
}
}
15 changes: 15 additions & 0 deletions database/migrations/2020_03_20_183732_core_v208.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ function (Blueprint $table) {
$table->string('discount_type')->default('normal')->after('discount_rate');
}
);

Schema::table(
'bill_items',
function (Blueprint $table) {
$table->double('discount_rate', 15, 4)->default('0.0000')->after('tax');
$table->string('discount_type')->default('normal')->after('discount_rate');
}
);
}

/**
Expand All @@ -35,5 +43,12 @@ function (Blueprint $table) {
$table->dropColumn(['discount_rate', 'discount_type']);
}
);

Schema::table(
'bill_items',
function (Blueprint $table) {
$table->dropColumn(['discount_rate', 'discount_type']);
}
);
}
}
27 changes: 20 additions & 7 deletions resources/assets/js/views/purchases/bills.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const app = new Vue({
bulk_action: new BulkAction('bills'),
totals: {
sub: 0,
item_discount: '',
discount: '',
discount_text: false,
tax: 0,
Expand Down Expand Up @@ -107,6 +108,7 @@ const app = new Vue({
onCalculateTotal() {
let sub_total = 0;
let discount_total = 0;
let item_discount_total = 0;
let tax_total = 0;
let grand_total = 0;
let items = this.form.items;
Expand All @@ -122,18 +124,21 @@ const app = new Vue({
let item = items[index];

// item sub total calcute.
let item_sub_total = item.price * item.quantity;
let item_total = item.price * item.quantity;

// item discount calculate.
let item_discounted_total = item_sub_total;
let item_discounted_total = item_total;

if (discount_in_totals) {
item_discounted_total = item_sub_total - (item_sub_total * (discount_in_totals / 100));
item_discounted_total = item_total - (item_total * (discount_in_totals / 100));
discount = discount_in_totals;
}

let discount_amount = 0;

if (item.discount) {
item_discounted_total = item_sub_total = item_sub_total - (item_sub_total * (item.discount / 100));
discount_amount = item_total * (item.discount / 100);
item_discounted_total = item_total - discount_amount;
discount = item.discount;
}

Expand Down Expand Up @@ -186,7 +191,7 @@ const app = new Vue({

item_tax_total = item_sub_and_tax_total - item_base_rate;

item_sub_total = item_base_rate + discount;
item_total = item_base_rate + discount;
}

if (compounds.length) {
Expand All @@ -197,17 +202,25 @@ const app = new Vue({
}

// set item total
items[index].total = item_sub_total;
if (item.discount) {
items[index].total = item_discounted_total;
} else {
items[index].total = item_total;
}

// calculate sub, tax, discount all items.
sub_total += item_sub_total;
item_discount_total += discount_amount;
sub_total += item_total;
tax_total += item_tax_total;
}
}

// set global total variable.
this.totals.sub = sub_total;
this.totals.tax = tax_total;
this.totals.item_discount = item_discount_total;

sub_total -= item_discount_total;

// Apply discount to total
if (discount_in_totals) {
Expand Down

0 comments on commit 3f068ec

Please sign in to comment.