From a1ccfc8b22752ce8d8fc34fd4c04349c5e641dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=87ak=C4=B1rel?= Date: Sat, 21 Mar 2020 04:42:45 +0300 Subject: [PATCH 1/7] Add discount per item for invoice --- app/Http/Controllers/Sales/Invoices.php | 19 +++- app/Http/Controllers/Settings/Defaults.php | 8 +- app/Jobs/Sale/CreateInvoiceItem.php | 3 +- app/Models/Sale/InvoiceItem.php | 29 ++++- app/Models/Sale/InvoiceItemDiscount.php | 42 ++++++++ .../2020_01_08_000000_core_v200.php | 5 + database/seeds/Settings.php | 1 + public/css/custom.css | 8 ++ resources/assets/js/views/sales/invoices.js | 20 ++-- resources/lang/en-GB/settings.php | 9 +- .../views/sales/invoices/create.blade.php | 100 ++++++++++-------- resources/views/sales/invoices/edit.blade.php | 6 +- resources/views/sales/invoices/item.blade.php | 31 ++++++ resources/views/sales/invoices/show.blade.php | 12 +++ .../views/settings/default/edit.blade.php | 2 + 15 files changed, 235 insertions(+), 60 deletions(-) create mode 100644 app/Models/Sale/InvoiceItemDiscount.php diff --git a/app/Http/Controllers/Sales/Invoices.php b/app/Http/Controllers/Sales/Invoices.php index 38a2d66e2..fd72a8f61 100644 --- a/app/Http/Controllers/Sales/Invoices.php +++ b/app/Http/Controllers/Sales/Invoices.php @@ -77,6 +77,8 @@ class Invoices extends Controller $date_format = $this->getCompanyDateFormat(); + $discount_location = $invoice->totals->contains($invoice->totals->where('code', 'discount')->first()) ? 'in_totals' : 'per_item'; + // Get Invoice Totals foreach ($invoice->totals as $invoice_total) { $invoice->{$invoice_total->code} = $invoice_total->amount; @@ -90,7 +92,22 @@ class Invoices extends Controller $invoice->grand_total = round($invoice->total - $invoice->paid, $currency->precision); } - return view('sales.invoices.show', compact('invoice', 'accounts', 'currencies', 'currency', 'account_currency_code', 'customers', 'categories', 'payment_methods', 'signed_url', 'date_format')); + return view( + 'sales.invoices.show', + compact( + 'invoice', + 'accounts', + 'currencies', + 'currency', + 'account_currency_code', + 'customers', + 'categories', + 'payment_methods', + 'signed_url', + 'date_format', + 'discount_location' + ) + ); } /** diff --git a/app/Http/Controllers/Settings/Defaults.php b/app/Http/Controllers/Settings/Defaults.php index 476c8a155..c9bf56608 100644 --- a/app/Http/Controllers/Settings/Defaults.php +++ b/app/Http/Controllers/Settings/Defaults.php @@ -27,12 +27,18 @@ class Defaults extends Controller $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' + 'payment_methods', + 'discount_locations' )); } } diff --git a/app/Jobs/Sale/CreateInvoiceItem.php b/app/Jobs/Sale/CreateInvoiceItem.php index 5e78a0fea..fc6994dd2 100644 --- a/app/Jobs/Sale/CreateInvoiceItem.php +++ b/app/Jobs/Sale/CreateInvoiceItem.php @@ -40,7 +40,7 @@ class CreateInvoiceItem extends Job // Apply discount to amount if (!empty($this->request['discount'])) { - $item_discounted_amount = $item_amount - ($item_amount * ($this->request['discount'] / 100)); + $item_discounted_amount = $item_amount -= ($item_amount * ($this->request['discount'] / 100)); } $tax_amount = 0; @@ -138,6 +138,7 @@ class CreateInvoiceItem extends Job 'quantity' => (double) $this->request['quantity'], 'price' => (double) $this->request['price'], 'tax' => $item_tax_total, + 'discount_rate' => $this->request['discount'], 'total' => $item_amount, ]); diff --git a/app/Models/Sale/InvoiceItem.php b/app/Models/Sale/InvoiceItem.php index 692269912..e55577291 100644 --- a/app/Models/Sale/InvoiceItem.php +++ b/app/Models/Sale/InvoiceItem.php @@ -17,7 +17,18 @@ class InvoiceItem extends Model * * @var array */ - protected $fillable = ['company_id', 'invoice_id', 'item_id', 'name', 'quantity', 'price', 'total', 'tax']; + protected $fillable = [ + 'company_id', + 'invoice_id', + 'item_id', + 'name', + 'quantity', + 'price', + 'total', + 'tax', + 'discount_rate', + 'discount_type', + ]; /** * Clonable relationships. @@ -83,6 +94,22 @@ class InvoiceItem extends Model $this->attributes['tax'] = (double) $value; } + /** + * Get the formatted discount. + * + * @return string + */ + public function getDiscountRateAttribute($value) + { + if (setting('localisation.percent_position', 'after') === 'after') { + $text = ($this->discount_type === 'normal') ? $value . '%' : $value; + } else { + $text = ($this->discount_type === 'normal') ? '%' . $value : $value; + } + + return $text; + } + /** * Convert tax to Array. * diff --git a/app/Models/Sale/InvoiceItemDiscount.php b/app/Models/Sale/InvoiceItemDiscount.php new file mode 100644 index 000000000..2e4ffffbc --- /dev/null +++ b/app/Models/Sale/InvoiceItemDiscount.php @@ -0,0 +1,42 @@ +belongsTo('App\Models\Sale\Invoice'); + } + + public function item() + { + return $this->belongsToThrough('App\Models\Common\Item', 'App\Models\Sale\InvoiceItem', 'invoice_item_id')->withDefault(['name' => trans('general.na')]); + } + + /** + * Convert rate to double. + * + * @param string $value + * @return void + */ + public function setRateAttribute($value) + { + $this->attributes['rate'] = (double) $value; + } +} diff --git a/database/migrations/2020_01_08_000000_core_v200.php b/database/migrations/2020_01_08_000000_core_v200.php index 0735f5a66..cb6acab5a 100644 --- a/database/migrations/2020_01_08_000000_core_v200.php +++ b/database/migrations/2020_01_08_000000_core_v200.php @@ -240,6 +240,11 @@ class CoreV200 extends Migration Schema::table('users', function (Blueprint $table) { $table->string('landing_page', 70)->nullable()->default('dashboard'); }); + + Schema::table('invoice_items', function (Blueprint $table) { + $table->double('discount_rate', 15, 4)->default('0.0000'); + $table->string('discount_type')->default('normal'); + }); } /** diff --git a/database/seeds/Settings.php b/database/seeds/Settings.php index d12ef1230..386472d27 100644 --- a/database/seeds/Settings.php +++ b/database/seeds/Settings.php @@ -63,6 +63,7 @@ class Settings extends Seeder 'invoice.color' => '#55588b', 'default.payment_method' => 'offline-payments.cash.1', 'default.list_limit' => '25', + 'default.discount_location' => 'in_totals', 'default.use_gravatar' => '0', 'email.protocol' => 'mail', 'email.sendmail_path' => '/usr/sbin/sendmail -bs', diff --git a/public/css/custom.css b/public/css/custom.css index de7a60b65..0ea69d22c 100644 --- a/public/css/custom.css +++ b/public/css/custom.css @@ -740,6 +740,14 @@ table .align-items-center td span.badge /*--Quantity Width Finish--*/ +/*--Discount Width--*/ +.w-12 +{ + width: 12%; +} +/*--Discount Width Finish--*/ + + /*--------Responsive--------*/ /*--Xs Breakpoint--*/ @media (max-width: 575.98px) diff --git a/resources/assets/js/views/sales/invoices.js b/resources/assets/js/views/sales/invoices.js index c5e485fbb..0983b53da 100644 --- a/resources/assets/js/views/sales/invoices.js +++ b/resources/assets/js/views/sales/invoices.js @@ -75,6 +75,7 @@ const app = new Vue({ price: (item.price).toFixed(2), quantity: item.quantity, tax_id: item.tax_id, + discount: item.discount_rate, total: (item.total).toFixed(2) }); }); @@ -109,7 +110,8 @@ const app = new Vue({ let tax_total = 0; let grand_total = 0; let items = this.form.items; - let discount = this.form.discount; + let discount_in_totals = this.form.discount; + let discount = ''; if (items.length) { let index = 0; @@ -125,8 +127,14 @@ const app = new Vue({ // item discount calculate. let item_discounted_total = item_sub_total; - if (discount) { - item_discounted_total = item_sub_total - (item_sub_total * (discount / 100)); + if (discount_in_totals) { + item_discounted_total = item_sub_total - (item_sub_total * (discount_in_totals / 100)); + discount = discount_in_totals; + } + + if (item.discount) { + item_discounted_total = item_sub_total = item_sub_total - (item_sub_total * (item.discount / 100)); + discount = item.discount; } // item tax calculate. @@ -202,12 +210,12 @@ const app = new Vue({ this.totals.tax = tax_total; // Apply discount to total - if (discount) { - discount_total = sub_total * (discount / 100); + if (discount_in_totals) { + discount_total = sub_total * (discount_in_totals / 100); this.totals.discount = discount_total; - sub_total = sub_total - (sub_total * (discount / 100)); + sub_total = sub_total - (sub_total * (discount_in_totals / 100)); } // set all item grand total. diff --git a/resources/lang/en-GB/settings.php b/resources/lang/en-GB/settings.php index 044061471..c6228526d 100644 --- a/resources/lang/en-GB/settings.php +++ b/resources/lang/en-GB/settings.php @@ -59,9 +59,12 @@ return [ ], 'default' => [ - 'description' => 'Default account, currency, language of your company', - 'list_limit' => 'Records Per Page', - 'use_gravatar' => 'Use Gravatar', + 'description' => 'Default account, currency, language of your company', + 'list_limit' => 'Records Per Page', + 'use_gravatar' => 'Use Gravatar', + 'discount_location' => 'Discount', + 'discount_per_item' => 'Per Item', + 'discount_in_totals' => 'In Totals', ], 'email' => [ diff --git a/resources/views/sales/invoices/create.blade.php b/resources/views/sales/invoices/create.blade.php index 74415c55c..2fd310441 100644 --- a/resources/views/sales/invoices/create.blade.php +++ b/resources/views/sales/invoices/create.blade.php @@ -51,6 +51,12 @@ {{ trans($text_override['price']) }} @stack('price_th_end') + @stack('discount_th_start') + @if(setting('default.discount_location', 'in_totals') === 'per_item') + {{ trans('invoices.discount') }} + @endif + @stack('discount_th_end') + @stack('taxes_th_start') {{ trans_choice('general.taxes', 1) }} @stack('taxes_th_end') @@ -69,13 +75,13 @@ - + @stack('add_item_td_end') @stack('sub_total_td_start') - + {{ trans('invoices.sub_total') }} @@ -87,60 +93,62 @@ @stack('sub_total_td_end') @stack('add_discount_td_start') - - - -
-
-
-
-
-
- - - + @if(setting('default.discount_location', 'in_totals') === 'in_totals') + + + +
+
+
+
+
+
+ + + +
+ {!! Form::number('pre_discount', null, ['id' => 'pre-discount', 'class' => 'form-control']) !!} +
+
+
+
+ {{ trans('invoices.discount_desc') }}
- {!! Form::number('pre_discount', null, ['id' => 'pre-discount', 'class' => 'form-control']) !!}
-
-
- {{ trans('invoices.discount_desc') }} +
+
- -
- {{ trans('invoices.add_discount') }} - - - - - {{ Form::moneyGroup('discount_total', '', '', ['disabled' => true, 'required' => 'required', 'v-model' => 'totals.discount', 'currency' => $currency, 'masked' => 'true'], 0.00, 'text-right d-none') }} - - @money(0, $currency->code, true) - {!! Form::hidden('discount', null, ['id' => 'discount', 'class' => 'form-control text-right', 'v-model' => 'form.discount']) !!} - - + {{ trans('invoices.add_discount') }} + + + + + {{ Form::moneyGroup('discount_total', '', '', ['disabled' => true, 'required' => 'required', 'v-model' => 'totals.discount', 'currency' => $currency, 'masked' => 'true'], 0.00, 'text-right d-none') }} + + @money(0, $currency->code, true) + {!! Form::hidden('discount', null, ['id' => 'discount', 'class' => 'form-control text-right', 'v-model' => 'form.discount']) !!} + + + @endif @stack('add_discount_td_end') @stack('tax_total_td_start') - + {{ trans_choice('general.taxes', 1) }} @@ -153,7 +161,7 @@ @stack('grand_total_td_start') - + {{ trans('invoices.total') }} diff --git a/resources/views/sales/invoices/edit.blade.php b/resources/views/sales/invoices/edit.blade.php index 165f0e884..a26e97582 100644 --- a/resources/views/sales/invoices/edit.blade.php +++ b/resources/views/sales/invoices/edit.blade.php @@ -52,6 +52,10 @@ {{ trans($text_override['price']) }} @stack('price_th_end') + @stack('discount_th_start') + {{ trans('invoices.discount') }} + @stack('discount_th_end') + @stack('taxes_th_start') {{ trans_choice('general.taxes', 1) }} @stack('taxes_th_end') @@ -88,7 +92,7 @@ @stack('sub_total_td_end') @stack('add_discount_td_start') - + @stack('price_td_end') + @stack('discount_td_start') + + @stack('discount_input_start') +
+
+ + + +
+ + +
+
+
+ @stack('discount_input_end') + + @stack('discount_td_end') + @stack('taxes_td_start') diff --git a/resources/views/sales/invoices/show.blade.php b/resources/views/sales/invoices/show.blade.php index 6a207f4b4..360f2720c 100644 --- a/resources/views/sales/invoices/show.blade.php +++ b/resources/views/sales/invoices/show.blade.php @@ -356,6 +356,12 @@ {{ trans($text_override['price']) }} @stack('price_th_end') + @stack('discount_th_start') + @if($discount_location === 'per_item') + {{ trans('invoices.discount') }} + @endif + @stack('discount_th_end') + @stack('total_th_start') {{ trans('invoices.total') }} @stack('total_th_end') @@ -379,6 +385,12 @@ @money($invoice_item->price, $invoice->currency_code, true) @stack('price_td_end') + @stack('discount_td_start') + @if($discount_location === 'per_item') + {{ $invoice_item->discount_rate }} + @endif + @stack('discount_td_end') + @stack('total_td_start') @money($invoice_item->total, $invoice->currency_code, true) @stack('total_td_end') diff --git a/resources/views/settings/default/edit.blade.php b/resources/views/settings/default/edit.blade.php index 5c9595c1a..b58bb4816 100644 --- a/resources/views/settings/default/edit.blade.php +++ b/resources/views/settings/default/edit.blade.php @@ -30,6 +30,8 @@ {{ Form::selectGroup('list_limit', trans('settings.default.list_limit'), 'columns', ['10' => '10', '25' => '25', '50' => '50', '100' => '100'], !empty($setting['list_limit']) ? $setting['list_limit'] : null, []) }} + {{ Form::selectGroup('discount_location', trans('settings.default.discount_location'), 'percent', $discount_locations, !empty($setting['discount_location']) ? $setting['discount_location'] : 'in_totals', []) }} + {{ Form::radioGroup('use_gravatar', trans('settings.default.use_gravatar'), $setting->get('use_gravatar')) }}
From e2148ef9a7727a45a3304742eb6caa66ff6c0d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=87ak=C4=B1rel?= Date: Mon, 23 Mar 2020 14:44:18 +0300 Subject: [PATCH 2/7] Move discount columns migration to a separate file --- .../2020_01_08_000000_core_v200.php | 5 --- ...d_discount_columns_invoice_items_table.php | 40 +++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 database/migrations/2020_03_20_183732_add_discount_columns_invoice_items_table.php diff --git a/database/migrations/2020_01_08_000000_core_v200.php b/database/migrations/2020_01_08_000000_core_v200.php index cb6acab5a..0735f5a66 100644 --- a/database/migrations/2020_01_08_000000_core_v200.php +++ b/database/migrations/2020_01_08_000000_core_v200.php @@ -240,11 +240,6 @@ class CoreV200 extends Migration Schema::table('users', function (Blueprint $table) { $table->string('landing_page', 70)->nullable()->default('dashboard'); }); - - Schema::table('invoice_items', function (Blueprint $table) { - $table->double('discount_rate', 15, 4)->default('0.0000'); - $table->string('discount_type')->default('normal'); - }); } /** diff --git a/database/migrations/2020_03_20_183732_add_discount_columns_invoice_items_table.php b/database/migrations/2020_03_20_183732_add_discount_columns_invoice_items_table.php new file mode 100644 index 000000000..a0532da1d --- /dev/null +++ b/database/migrations/2020_03_20_183732_add_discount_columns_invoice_items_table.php @@ -0,0 +1,40 @@ +double('discount_rate', 15, 4)->default('0.0000')->after('tax'); + $table->string('discount_type')->default('normal')->after('discount_rate'); + } + ); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table( + 'invoice_items', + function (Blueprint $table) { + $table->dropColumn('discount_rate'); + $table->dropColumn('discount_type'); + } + ); + } +} From 6903a44bce19e28e54d92ce903c929f645fdc7de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=87ak=C4=B1rel?= Date: Tue, 24 Mar 2020 00:16:11 +0300 Subject: [PATCH 3/7] Add discount per item feature to the bills --- app/Http/Controllers/Sales/Invoices.php | 19 +--- app/Jobs/Purchase/CreateBillItem.php | 3 +- app/Models/Purchase/BillItem.php | 29 +++++- app/Models/Sale/InvoiceItemDiscount.php | 42 --------- ...d_discount_columns_invoice_items_table.php | 3 +- database/seeds/Settings.php | 1 - resources/assets/js/views/purchases/bills.js | 22 +++-- resources/assets/js/views/sales/invoices.js | 2 +- resources/lang/en-GB/settings.php | 9 +- .../views/purchases/bills/create.blade.php | 14 +-- .../views/purchases/bills/edit.blade.php | 14 +-- .../views/purchases/bills/item.blade.php | 31 +++++++ .../views/purchases/bills/show.blade.php | 8 ++ .../views/sales/invoices/create.blade.php | 90 +++++++++---------- resources/views/sales/invoices/edit.blade.php | 12 +-- resources/views/sales/invoices/show.blade.php | 8 +- .../views/settings/default/edit.blade.php | 2 - 17 files changed, 159 insertions(+), 150 deletions(-) delete mode 100644 app/Models/Sale/InvoiceItemDiscount.php diff --git a/app/Http/Controllers/Sales/Invoices.php b/app/Http/Controllers/Sales/Invoices.php index fd72a8f61..38a2d66e2 100644 --- a/app/Http/Controllers/Sales/Invoices.php +++ b/app/Http/Controllers/Sales/Invoices.php @@ -77,8 +77,6 @@ class Invoices extends Controller $date_format = $this->getCompanyDateFormat(); - $discount_location = $invoice->totals->contains($invoice->totals->where('code', 'discount')->first()) ? 'in_totals' : 'per_item'; - // Get Invoice Totals foreach ($invoice->totals as $invoice_total) { $invoice->{$invoice_total->code} = $invoice_total->amount; @@ -92,22 +90,7 @@ class Invoices extends Controller $invoice->grand_total = round($invoice->total - $invoice->paid, $currency->precision); } - return view( - 'sales.invoices.show', - compact( - 'invoice', - 'accounts', - 'currencies', - 'currency', - 'account_currency_code', - 'customers', - 'categories', - 'payment_methods', - 'signed_url', - 'date_format', - 'discount_location' - ) - ); + return view('sales.invoices.show', compact('invoice', 'accounts', 'currencies', 'currency', 'account_currency_code', 'customers', 'categories', 'payment_methods', 'signed_url', 'date_format')); } /** diff --git a/app/Jobs/Purchase/CreateBillItem.php b/app/Jobs/Purchase/CreateBillItem.php index 37f19b3d7..99307d45f 100644 --- a/app/Jobs/Purchase/CreateBillItem.php +++ b/app/Jobs/Purchase/CreateBillItem.php @@ -40,7 +40,7 @@ class CreateBillItem extends Job // Apply discount to amount if (!empty($this->request['discount'])) { - $item_discounted_amount = $item_amount - ($item_amount * ($this->request['discount'] / 100)); + $item_discounted_amount = $item_amount -= ($item_amount * ($this->request['discount'] / 100)); } $tax_amount = 0; @@ -138,6 +138,7 @@ class CreateBillItem extends Job 'quantity' => (double) $this->request['quantity'], 'price' => (double) $this->request['price'], 'tax' => $item_tax_total, + 'discount_rate' => $this->request['discount'], 'total' => $item_amount, ]); diff --git a/app/Models/Purchase/BillItem.php b/app/Models/Purchase/BillItem.php index b56c5af6f..942454b40 100644 --- a/app/Models/Purchase/BillItem.php +++ b/app/Models/Purchase/BillItem.php @@ -18,7 +18,18 @@ class BillItem extends Model * * @var array */ - protected $fillable = ['company_id', 'bill_id', 'item_id', 'name', 'quantity', 'price', 'total', 'tax']; + protected $fillable = [ + 'company_id', + 'bill_id', + 'item_id', + 'name', + 'quantity', + 'price', + 'total', + 'tax', + 'discount_rate', + 'discount_type', + ]; /** * Clonable relationships. @@ -84,6 +95,22 @@ class BillItem extends Model $this->attributes['tax'] = (double) $value; } + /** + * Get the formatted discount. + * + * @return string + */ + public function getDiscountRateAttribute($value) + { + if (setting('localisation.percent_position', 'after') === 'after') { + $text = ($this->discount_type === 'normal') ? $value . '%' : $value; + } else { + $text = ($this->discount_type === 'normal') ? '%' . $value : $value; + } + + return $text; + } + /** * Convert tax to Array. * diff --git a/app/Models/Sale/InvoiceItemDiscount.php b/app/Models/Sale/InvoiceItemDiscount.php deleted file mode 100644 index 2e4ffffbc..000000000 --- a/app/Models/Sale/InvoiceItemDiscount.php +++ /dev/null @@ -1,42 +0,0 @@ -belongsTo('App\Models\Sale\Invoice'); - } - - public function item() - { - return $this->belongsToThrough('App\Models\Common\Item', 'App\Models\Sale\InvoiceItem', 'invoice_item_id')->withDefault(['name' => trans('general.na')]); - } - - /** - * Convert rate to double. - * - * @param string $value - * @return void - */ - public function setRateAttribute($value) - { - $this->attributes['rate'] = (double) $value; - } -} diff --git a/database/migrations/2020_03_20_183732_add_discount_columns_invoice_items_table.php b/database/migrations/2020_03_20_183732_add_discount_columns_invoice_items_table.php index a0532da1d..e35f57d25 100644 --- a/database/migrations/2020_03_20_183732_add_discount_columns_invoice_items_table.php +++ b/database/migrations/2020_03_20_183732_add_discount_columns_invoice_items_table.php @@ -32,8 +32,7 @@ class AddDiscountColumnsInvoiceItemsTable extends Migration Schema::table( 'invoice_items', function (Blueprint $table) { - $table->dropColumn('discount_rate'); - $table->dropColumn('discount_type'); + $table->dropColumn(['discount_rate', 'discount_type']); } ); } diff --git a/database/seeds/Settings.php b/database/seeds/Settings.php index 386472d27..d12ef1230 100644 --- a/database/seeds/Settings.php +++ b/database/seeds/Settings.php @@ -63,7 +63,6 @@ class Settings extends Seeder 'invoice.color' => '#55588b', 'default.payment_method' => 'offline-payments.cash.1', 'default.list_limit' => '25', - 'default.discount_location' => 'in_totals', 'default.use_gravatar' => '0', 'email.protocol' => 'mail', 'email.sendmail_path' => '/usr/sbin/sendmail -bs', diff --git a/resources/assets/js/views/purchases/bills.js b/resources/assets/js/views/purchases/bills.js index 9397710ab..ef333548d 100644 --- a/resources/assets/js/views/purchases/bills.js +++ b/resources/assets/js/views/purchases/bills.js @@ -50,7 +50,7 @@ const app = new Vue({ items: '', discount: false, taxes: null, - colspan: 5, + colspan: 6, } }, @@ -75,6 +75,7 @@ const app = new Vue({ price: (item.price).toFixed(2), quantity: item.quantity, tax_id: item.tax_id, + discount: item.discount_rate, total: (item.total).toFixed(2) }); }); @@ -109,7 +110,8 @@ const app = new Vue({ let tax_total = 0; let grand_total = 0; let items = this.form.items; - let discount = this.form.discount; + let discount_in_totals = this.form.discount; + let discount = ''; if (items.length) { let index = 0; @@ -125,8 +127,14 @@ const app = new Vue({ // item discount calculate. let item_discounted_total = item_sub_total; - if (discount) { - item_discounted_total = item_sub_total - (item_sub_total * (discount / 100)); + if (discount_in_totals) { + item_discounted_total = item_sub_total - (item_sub_total * (discount_in_totals / 100)); + discount = discount_in_totals; + } + + if (item.discount) { + item_discounted_total = item_sub_total = item_sub_total - (item_sub_total * (item.discount / 100)); + discount = item.discount; } // item tax calculate. @@ -202,12 +210,12 @@ const app = new Vue({ this.totals.tax = tax_total; // Apply discount to total - if (discount) { - discount_total = sub_total * (discount / 100); + if (discount_in_totals) { + discount_total = sub_total * (discount_in_totals / 100); this.totals.discount = discount_total; - sub_total = sub_total - (sub_total * (discount / 100)); + sub_total = sub_total - (sub_total * (discount_in_totals / 100)); } // set all item grand total. diff --git a/resources/assets/js/views/sales/invoices.js b/resources/assets/js/views/sales/invoices.js index 0983b53da..be3875acb 100644 --- a/resources/assets/js/views/sales/invoices.js +++ b/resources/assets/js/views/sales/invoices.js @@ -50,7 +50,7 @@ const app = new Vue({ items: '', discount: false, taxes: null, - colspan: 5, + colspan: 6, } }, diff --git a/resources/lang/en-GB/settings.php b/resources/lang/en-GB/settings.php index c6228526d..044061471 100644 --- a/resources/lang/en-GB/settings.php +++ b/resources/lang/en-GB/settings.php @@ -59,12 +59,9 @@ return [ ], 'default' => [ - 'description' => 'Default account, currency, language of your company', - 'list_limit' => 'Records Per Page', - 'use_gravatar' => 'Use Gravatar', - 'discount_location' => 'Discount', - 'discount_per_item' => 'Per Item', - 'discount_in_totals' => 'In Totals', + 'description' => 'Default account, currency, language of your company', + 'list_limit' => 'Records Per Page', + 'use_gravatar' => 'Use Gravatar', ], 'email' => [ diff --git a/resources/views/purchases/bills/create.blade.php b/resources/views/purchases/bills/create.blade.php index 10dcc00a5..86bcd85d3 100644 --- a/resources/views/purchases/bills/create.blade.php +++ b/resources/views/purchases/bills/create.blade.php @@ -51,6 +51,10 @@ {{ trans('bills.price') }} @stack('price_th_end') + @stack('discount_th_start') + {{ trans('bills.discount') }} + @stack('discount_th_end') + @stack('taxes_th_start') {{ trans_choice('general.taxes', 1) }} @stack('taxes_th_end') @@ -69,13 +73,13 @@ - + @stack('add_item_td_end') @stack('sub_total_td_start') - + {{ trans('bills.sub_total') }} @@ -88,7 +92,7 @@ @stack('add_discount_td_start') - + - + {{ trans_choice('general.taxes', 1) }} @@ -153,7 +157,7 @@ @stack('grand_total_td_start') - + {{ trans('bills.total') }} diff --git a/resources/views/purchases/bills/edit.blade.php b/resources/views/purchases/bills/edit.blade.php index 5b4cc21cb..56c29f50a 100644 --- a/resources/views/purchases/bills/edit.blade.php +++ b/resources/views/purchases/bills/edit.blade.php @@ -52,6 +52,10 @@ {{ trans('bills.price') }} @stack('price_th_end') + @stack('discount_th_start') + {{ trans('bills.discount') }} + @stack('discount_th_end') + @stack('taxes_th_start') {{ trans_choice('general.taxes', 1) }} @stack('taxes_th_end') @@ -70,13 +74,13 @@ - + @stack('add_item_td_end') @stack('sub_total_td_start') - + {{ trans('bills.sub_total') }} @@ -89,7 +93,7 @@ @stack('add_discount_td_start') - + - + {{ trans_choice('general.taxes', 1) }} @@ -154,7 +158,7 @@ @stack('grand_total_td_start') - + {{ trans('bills.total') }} diff --git a/resources/views/purchases/bills/item.blade.php b/resources/views/purchases/bills/item.blade.php index 1dd723617..17413efb0 100644 --- a/resources/views/purchases/bills/item.blade.php +++ b/resources/views/purchases/bills/item.blade.php @@ -98,6 +98,37 @@ @stack('price_td_end') + @stack('discount_td_start') + + @stack('discount_input_start') +
+
+ + + +
+ + +
+
+
+ @stack('discount_input_end') + + @stack('discount_td_end') + @stack('taxes_td_start') diff --git a/resources/views/purchases/bills/show.blade.php b/resources/views/purchases/bills/show.blade.php index 9cfbecde1..ec49b0428 100644 --- a/resources/views/purchases/bills/show.blade.php +++ b/resources/views/purchases/bills/show.blade.php @@ -339,6 +339,10 @@ {{ trans('bills.price') }} @stack('price_th_end') + @stack('discount_th_start') + {{ trans('bills.discount') }} + @stack('discount_th_end') + @stack('total_th_start') {{ trans('bills.total') }} @stack('total_th_end') @@ -362,6 +366,10 @@ @money($bill_item->price, $bill->currency_code, true) @stack('price_td_end') + @stack('discount_td_start') + {{ $bill_item->discount_rate }} + @stack('discount_td_end') + @stack('total_td_start') @money($bill_item->total, $bill->currency_code, true) @stack('total_td_end') diff --git a/resources/views/sales/invoices/create.blade.php b/resources/views/sales/invoices/create.blade.php index 2fd310441..696295ef9 100644 --- a/resources/views/sales/invoices/create.blade.php +++ b/resources/views/sales/invoices/create.blade.php @@ -52,9 +52,7 @@ @stack('price_th_end') @stack('discount_th_start') - @if(setting('default.discount_location', 'in_totals') === 'per_item') - {{ trans('invoices.discount') }} - @endif + {{ trans('invoices.discount') }} @stack('discount_th_end') @stack('taxes_th_start') @@ -93,57 +91,55 @@ @stack('sub_total_td_end') @stack('add_discount_td_start') - @if(setting('default.discount_location', 'in_totals') === 'in_totals') - - - -
-
-
-
-
-
- - - -
- {!! Form::number('pre_discount', null, ['id' => 'pre-discount', 'class' => 'form-control']) !!} -
-
-
-
- {{ trans('invoices.discount_desc') }} + + + +
+
+
+
+
+
+ + +
+ {!! Form::number('pre_discount', null, ['id' => 'pre-discount', 'class' => 'form-control']) !!}
-
- + {{ trans('invoices.add_discount') }} + + + + + {{ Form::moneyGroup('discount_total', '', '', ['disabled' => true, 'required' => 'required', 'v-model' => 'totals.discount', 'currency' => $currency, 'masked' => 'true'], 0.00, 'text-right d-none') }} + + @money(0, $currency->code, true) + {!! Form::hidden('discount', null, ['id' => 'discount', 'class' => 'form-control text-right', 'v-model' => 'form.discount']) !!} + + @stack('add_discount_td_end') @stack('tax_total_td_start') diff --git a/resources/views/sales/invoices/edit.blade.php b/resources/views/sales/invoices/edit.blade.php index a26e97582..2af8841d9 100644 --- a/resources/views/sales/invoices/edit.blade.php +++ b/resources/views/sales/invoices/edit.blade.php @@ -74,13 +74,13 @@ - + @stack('add_item_td_end') @stack('sub_total_td_start') - + {{ trans('invoices.sub_total') }} @@ -92,8 +92,8 @@ @stack('sub_total_td_end') @stack('add_discount_td_start') - - + + - + {{ trans_choice('general.taxes', 1) }} @@ -158,7 +158,7 @@ @stack('grand_total_td_start') - + {{ trans('invoices.total') }} diff --git a/resources/views/sales/invoices/show.blade.php b/resources/views/sales/invoices/show.blade.php index 360f2720c..6edf9edbb 100644 --- a/resources/views/sales/invoices/show.blade.php +++ b/resources/views/sales/invoices/show.blade.php @@ -357,9 +357,7 @@ @stack('price_th_end') @stack('discount_th_start') - @if($discount_location === 'per_item') - {{ trans('invoices.discount') }} - @endif + {{ trans('invoices.discount') }} @stack('discount_th_end') @stack('total_th_start') @@ -386,9 +384,7 @@ @stack('price_td_end') @stack('discount_td_start') - @if($discount_location === 'per_item') - {{ $invoice_item->discount_rate }} - @endif + {{ $invoice_item->discount_rate }} @stack('discount_td_end') @stack('total_td_start') diff --git a/resources/views/settings/default/edit.blade.php b/resources/views/settings/default/edit.blade.php index b58bb4816..5c9595c1a 100644 --- a/resources/views/settings/default/edit.blade.php +++ b/resources/views/settings/default/edit.blade.php @@ -30,8 +30,6 @@ {{ Form::selectGroup('list_limit', trans('settings.default.list_limit'), 'columns', ['10' => '10', '25' => '25', '50' => '50', '100' => '100'], !empty($setting['list_limit']) ? $setting['list_limit'] : null, []) }} - {{ Form::selectGroup('discount_location', trans('settings.default.discount_location'), 'percent', $discount_locations, !empty($setting['discount_location']) ? $setting['discount_location'] : 'in_totals', []) }} - {{ Form::radioGroup('use_gravatar', trans('settings.default.use_gravatar'), $setting->get('use_gravatar')) }}
From f4c98ca60ee63216356ef739459558407b271b16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=87ak=C4=B1rel?= Date: Tue, 24 Mar 2020 00:39:17 +0300 Subject: [PATCH 4/7] Rename migration file --- ...ns_invoice_items_table.php => 2020_03_20_183732_core_v208.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename database/migrations/{2020_03_20_183732_add_discount_columns_invoice_items_table.php => 2020_03_20_183732_core_v208.php} (100%) diff --git a/database/migrations/2020_03_20_183732_add_discount_columns_invoice_items_table.php b/database/migrations/2020_03_20_183732_core_v208.php similarity index 100% rename from database/migrations/2020_03_20_183732_add_discount_columns_invoice_items_table.php rename to database/migrations/2020_03_20_183732_core_v208.php From 2019f1c779ed81303e87ac496b38d71132939079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=87ak=C4=B1rel?= Date: Tue, 24 Mar 2020 02:03:10 +0300 Subject: [PATCH 5/7] Rename migration class --- database/migrations/2020_03_20_183732_core_v208.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/2020_03_20_183732_core_v208.php b/database/migrations/2020_03_20_183732_core_v208.php index e35f57d25..481ad86ed 100644 --- a/database/migrations/2020_03_20_183732_core_v208.php +++ b/database/migrations/2020_03_20_183732_core_v208.php @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class AddDiscountColumnsInvoiceItemsTable extends Migration +class CoreV208 extends Migration { /** * Run the migrations. From 100fc2c07cd2a4466764a8a2f3af5e1575040d2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=BCneyt=20=C5=9Eent=C3=BCrk?= Date: Tue, 24 Mar 2020 12:07:50 +0300 Subject: [PATCH 6/7] Add header_button stack. --- resources/views/partials/admin/header.blade.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/resources/views/partials/admin/header.blade.php b/resources/views/partials/admin/header.blade.php index 17580eded..0381c8d74 100644 --- a/resources/views/partials/admin/header.blade.php +++ b/resources/views/partials/admin/header.blade.php @@ -7,6 +7,7 @@

@yield('title')

@yield('dashboard_action')
+
@yield('new_button') @@ -20,6 +21,8 @@ @endforeach @endif @endpermission + + @stack('header_button')
From 3f068ec9791a91bb9ee22b5e163cc7b3dce13455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=87ak=C4=B1rel?= Date: Tue, 24 Mar 2020 21:37:08 +0300 Subject: [PATCH 7/7] Add "Item Discount" line in totals --- app/Http/Controllers/Settings/Defaults.php | 8 +---- app/Jobs/Purchase/CreateBill.php | 31 ++++++++++++++++--- app/Jobs/Purchase/UpdateBill.php | 31 ++++++++++++++++--- app/Jobs/Sale/CreateInvoice.php | 31 ++++++++++++++++--- app/Jobs/Sale/UpdateInvoice.php | 31 ++++++++++++++++--- .../2020_03_20_183732_core_v208.php | 15 +++++++++ resources/assets/js/views/purchases/bills.js | 27 +++++++++++----- resources/assets/js/views/sales/invoices.js | 27 +++++++++++----- resources/lang/en-GB/bills.php | 1 + resources/lang/en-GB/invoices.php | 1 + .../views/purchases/bills/create.blade.php | 13 ++++++++ .../views/sales/invoices/create.blade.php | 15 ++++++++- 12 files changed, 189 insertions(+), 42 deletions(-) diff --git a/app/Http/Controllers/Settings/Defaults.php b/app/Http/Controllers/Settings/Defaults.php index c9bf56608..476c8a155 100644 --- a/app/Http/Controllers/Settings/Defaults.php +++ b/app/Http/Controllers/Settings/Defaults.php @@ -27,18 +27,12 @@ class Defaults extends Controller $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' )); } } diff --git a/app/Jobs/Purchase/CreateBill.php b/app/Jobs/Purchase/CreateBill.php index 531091095..c87539e10 100644 --- a/app/Jobs/Purchase/CreateBill.php +++ b/app/Jobs/Purchase/CreateBill.php @@ -64,7 +64,7 @@ class CreateBill extends Job protected function createItemsAndTotals() { // Create items - list($sub_total, $taxes) = $this->createItems(); + list($sub_total, $discount_amount_total, $taxes) = $this->createItems(); $sort_order = 1; @@ -83,8 +83,23 @@ class CreateBill extends Job $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, @@ -155,7 +170,7 @@ class CreateBill extends Job protected function createItems() { - $sub_total = 0; + $sub_total = $discount_amount = $discount_amount_total = 0; $taxes = []; @@ -170,8 +185,14 @@ class CreateBill extends Job $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; @@ -190,6 +211,6 @@ class CreateBill extends Job } } - return [$sub_total, $taxes]; + return [$sub_total, $discount_amount_total, $taxes]; } } diff --git a/app/Jobs/Purchase/UpdateBill.php b/app/Jobs/Purchase/UpdateBill.php index 6b673d723..4e8420eff 100644 --- a/app/Jobs/Purchase/UpdateBill.php +++ b/app/Jobs/Purchase/UpdateBill.php @@ -72,7 +72,7 @@ class UpdateBill extends Job 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'); @@ -94,8 +94,23 @@ class UpdateBill extends Job $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, @@ -166,7 +181,7 @@ class UpdateBill extends Job protected function createItems() { - $sub_total = 0; + $sub_total = $discount_amount = $discount_amount_total = 0; $taxes = []; @@ -184,8 +199,14 @@ class UpdateBill extends Job $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; @@ -204,6 +225,6 @@ class UpdateBill extends Job } } - return [$sub_total, $taxes]; + return [$sub_total, $discount_amount_total, $taxes]; } } diff --git a/app/Jobs/Sale/CreateInvoice.php b/app/Jobs/Sale/CreateInvoice.php index 6ac4c4df1..7ade6315f 100644 --- a/app/Jobs/Sale/CreateInvoice.php +++ b/app/Jobs/Sale/CreateInvoice.php @@ -64,7 +64,7 @@ class CreateInvoice extends Job protected function createItemsAndTotals() { // Create items - list($sub_total, $taxes) = $this->createItems(); + list($sub_total, $discount_amount_total, $taxes) = $this->createItems(); $sort_order = 1; @@ -83,8 +83,23 @@ class CreateInvoice extends Job $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, @@ -155,7 +170,7 @@ class CreateInvoice extends Job protected function createItems() { - $sub_total = 0; + $sub_total = $discount_amount = $discount_amount_total = 0; $taxes = []; @@ -170,8 +185,14 @@ class CreateInvoice extends Job $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; @@ -190,6 +211,6 @@ class CreateInvoice extends Job } } - return [$sub_total, $taxes]; + return [$sub_total, $discount_amount_total, $taxes]; } } diff --git a/app/Jobs/Sale/UpdateInvoice.php b/app/Jobs/Sale/UpdateInvoice.php index 344534d2c..85c15c1c4 100644 --- a/app/Jobs/Sale/UpdateInvoice.php +++ b/app/Jobs/Sale/UpdateInvoice.php @@ -72,7 +72,7 @@ class UpdateInvoice extends Job 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'); @@ -94,8 +94,23 @@ class UpdateInvoice extends Job $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, @@ -166,7 +181,7 @@ class UpdateInvoice extends Job protected function createItems() { - $sub_total = 0; + $sub_total = $discount_amount = $discount_amount_total = 0; $taxes = []; @@ -184,8 +199,14 @@ class UpdateInvoice extends Job $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; @@ -204,6 +225,6 @@ class UpdateInvoice extends Job } } - return [$sub_total, $taxes]; + return [$sub_total, $discount_amount_total, $taxes]; } } diff --git a/database/migrations/2020_03_20_183732_core_v208.php b/database/migrations/2020_03_20_183732_core_v208.php index 481ad86ed..896df429d 100644 --- a/database/migrations/2020_03_20_183732_core_v208.php +++ b/database/migrations/2020_03_20_183732_core_v208.php @@ -20,6 +20,14 @@ class CoreV208 extends Migration $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'); + } + ); } /** @@ -35,5 +43,12 @@ class CoreV208 extends Migration $table->dropColumn(['discount_rate', 'discount_type']); } ); + + Schema::table( + 'bill_items', + function (Blueprint $table) { + $table->dropColumn(['discount_rate', 'discount_type']); + } + ); } } diff --git a/resources/assets/js/views/purchases/bills.js b/resources/assets/js/views/purchases/bills.js index ef333548d..67bf7e086 100644 --- a/resources/assets/js/views/purchases/bills.js +++ b/resources/assets/js/views/purchases/bills.js @@ -32,6 +32,7 @@ const app = new Vue({ bulk_action: new BulkAction('bills'), totals: { sub: 0, + item_discount: '', discount: '', discount_text: false, tax: 0, @@ -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; @@ -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; } @@ -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) { @@ -197,10 +202,15 @@ 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; } } @@ -208,6 +218,9 @@ const app = new Vue({ // 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) { diff --git a/resources/assets/js/views/sales/invoices.js b/resources/assets/js/views/sales/invoices.js index be3875acb..961f59d40 100644 --- a/resources/assets/js/views/sales/invoices.js +++ b/resources/assets/js/views/sales/invoices.js @@ -32,6 +32,7 @@ const app = new Vue({ bulk_action: new BulkAction('invoices'), totals: { sub: 0, + item_discount: '', discount: '', discount_text: false, tax: 0, @@ -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; @@ -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; } @@ -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) { @@ -197,10 +202,15 @@ 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; } } @@ -208,6 +218,9 @@ const app = new Vue({ // 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) { diff --git a/resources/lang/en-GB/bills.php b/resources/lang/en-GB/bills.php index 834694d52..5914cf59b 100644 --- a/resources/lang/en-GB/bills.php +++ b/resources/lang/en-GB/bills.php @@ -13,6 +13,7 @@ return [ 'price' => 'Price', 'sub_total' => 'Subtotal', 'discount' => 'Discount', + 'item_discount' => 'Item Discount', 'tax_total' => 'Tax Total', 'total' => 'Total', diff --git a/resources/lang/en-GB/invoices.php b/resources/lang/en-GB/invoices.php index 10e56bca3..e06ce9a68 100644 --- a/resources/lang/en-GB/invoices.php +++ b/resources/lang/en-GB/invoices.php @@ -13,6 +13,7 @@ return [ 'price' => 'Price', 'sub_total' => 'Subtotal', 'discount' => 'Discount', + 'item_discount' => 'Item Discount', 'tax_total' => 'Tax Total', 'total' => 'Total', diff --git a/resources/views/purchases/bills/create.blade.php b/resources/views/purchases/bills/create.blade.php index 86bcd85d3..f344556cb 100644 --- a/resources/views/purchases/bills/create.blade.php +++ b/resources/views/purchases/bills/create.blade.php @@ -90,6 +90,19 @@ @stack('sub_total_td_end') + @stack('item_discount_td_start') + + + {{ trans('bills.item_discount') }} + + + {{ Form::moneyGroup('item_discount', '', '', ['disabled' => true, 'required' => 'required', 'v-model' => 'totals.item_discount', 'currency' => $currency, 'masked' => 'true'], 0.00, 'text-right d-none') }} + + @money(0, $currency->code, true) + + + @stack('item_discount_td_end') + @stack('add_discount_td_start') diff --git a/resources/views/sales/invoices/create.blade.php b/resources/views/sales/invoices/create.blade.php index 696295ef9..c35745c1b 100644 --- a/resources/views/sales/invoices/create.blade.php +++ b/resources/views/sales/invoices/create.blade.php @@ -90,9 +90,22 @@ @stack('sub_total_td_end') + @stack('item_discount_td_start') + + + {{ trans('invoices.item_discount') }} + + + {{ Form::moneyGroup('item_discount', '', '', ['disabled' => true, 'required' => 'required', 'v-model' => 'totals.item_discount', 'currency' => $currency, 'masked' => 'true'], 0.00, 'text-right d-none') }} + + @money(0, $currency->code, true) + + + @stack('item_discount_td_end') + @stack('add_discount_td_start') - +