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')) }}