Add "Item Discount" line in totals

This commit is contained in:
Burak Çakırel 2020-03-24 21:37:08 +03:00
parent 100fc2c07c
commit 3f068ec979
12 changed files with 189 additions and 42 deletions

View File

@ -27,18 +27,12 @@ class Defaults extends Controller
$payment_methods = Modules::getPaymentMethods(); $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( return view('settings.default.edit', compact(
'setting', 'setting',
'accounts', 'accounts',
'currencies', 'currencies',
'taxes', 'taxes',
'payment_methods', 'payment_methods'
'discount_locations'
)); ));
} }
} }

View File

@ -64,7 +64,7 @@ class CreateBill extends Job
protected function createItemsAndTotals() protected function createItemsAndTotals()
{ {
// Create items // Create items
list($sub_total, $taxes) = $this->createItems(); list($sub_total, $discount_amount_total, $taxes) = $this->createItems();
$sort_order = 1; $sort_order = 1;
@ -83,8 +83,23 @@ class CreateBill extends Job
$sort_order++; $sort_order++;
// Add discount // 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'])) { 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([ BillTotal::create([
'company_id' => $this->bill->company_id, 'company_id' => $this->bill->company_id,
@ -155,7 +170,7 @@ class CreateBill extends Job
protected function createItems() protected function createItems()
{ {
$sub_total = 0; $sub_total = $discount_amount = $discount_amount_total = 0;
$taxes = []; $taxes = [];
@ -170,8 +185,14 @@ class CreateBill extends Job
$bill_item = $this->dispatch(new CreateBillItem($item, $this->bill)); $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 // Calculate totals
$sub_total += $bill_item->total; $sub_total += $bill_item->total + $discount_amount;
$discount_amount_total += $discount_amount;
if (!$bill_item->item_taxes) { if (!$bill_item->item_taxes) {
continue; continue;
@ -190,6 +211,6 @@ class CreateBill extends Job
} }
} }
return [$sub_total, $taxes]; return [$sub_total, $discount_amount_total, $taxes];
} }
} }

View File

@ -72,7 +72,7 @@ class UpdateBill extends Job
protected function createItemsAndTotals() protected function createItemsAndTotals()
{ {
// Create items // Create items
list($sub_total, $taxes) = $this->createItems(); list($sub_total, $discount_amount_total, $taxes) = $this->createItems();
// Delete current totals // Delete current totals
$this->deleteRelationships($this->bill, 'totals'); $this->deleteRelationships($this->bill, 'totals');
@ -94,8 +94,23 @@ class UpdateBill extends Job
$sort_order++; $sort_order++;
// Add discount // 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'])) { 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([ BillTotal::create([
'company_id' => $this->bill->company_id, 'company_id' => $this->bill->company_id,
@ -166,7 +181,7 @@ class UpdateBill extends Job
protected function createItems() protected function createItems()
{ {
$sub_total = 0; $sub_total = $discount_amount = $discount_amount_total = 0;
$taxes = []; $taxes = [];
@ -184,8 +199,14 @@ class UpdateBill extends Job
$bill_item = $this->dispatch(new CreateBillItem($item, $this->bill)); $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 // Calculate totals
$sub_total += $bill_item->total; $sub_total += $bill_item->total + $discount_amount;
$discount_amount_total += $discount_amount;
if (!$bill_item->item_taxes) { if (!$bill_item->item_taxes) {
continue; continue;
@ -204,6 +225,6 @@ class UpdateBill extends Job
} }
} }
return [$sub_total, $taxes]; return [$sub_total, $discount_amount_total, $taxes];
} }
} }

View File

@ -64,7 +64,7 @@ class CreateInvoice extends Job
protected function createItemsAndTotals() protected function createItemsAndTotals()
{ {
// Create items // Create items
list($sub_total, $taxes) = $this->createItems(); list($sub_total, $discount_amount_total, $taxes) = $this->createItems();
$sort_order = 1; $sort_order = 1;
@ -83,8 +83,23 @@ class CreateInvoice extends Job
$sort_order++; $sort_order++;
// Add discount // 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'])) { 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([ InvoiceTotal::create([
'company_id' => $this->invoice->company_id, 'company_id' => $this->invoice->company_id,
@ -155,7 +170,7 @@ class CreateInvoice extends Job
protected function createItems() protected function createItems()
{ {
$sub_total = 0; $sub_total = $discount_amount = $discount_amount_total = 0;
$taxes = []; $taxes = [];
@ -170,8 +185,14 @@ class CreateInvoice extends Job
$invoice_item = $this->dispatch(new CreateInvoiceItem($item, $this->invoice)); $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 // Calculate totals
$sub_total += $invoice_item->total; $sub_total += $invoice_item->total + $discount_amount;
$discount_amount_total += $discount_amount;
if (!$invoice_item->item_taxes) { if (!$invoice_item->item_taxes) {
continue; continue;
@ -190,6 +211,6 @@ class CreateInvoice extends Job
} }
} }
return [$sub_total, $taxes]; return [$sub_total, $discount_amount_total, $taxes];
} }
} }

View File

@ -72,7 +72,7 @@ class UpdateInvoice extends Job
protected function createItemsAndTotals() protected function createItemsAndTotals()
{ {
// Create items // Create items
list($sub_total, $taxes) = $this->createItems(); list($sub_total, $discount_amount_total, $taxes) = $this->createItems();
// Delete current totals // Delete current totals
$this->deleteRelationships($this->invoice, 'totals'); $this->deleteRelationships($this->invoice, 'totals');
@ -94,8 +94,23 @@ class UpdateInvoice extends Job
$sort_order++; $sort_order++;
// Add discount // 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'])) { 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([ InvoiceTotal::create([
'company_id' => $this->invoice->company_id, 'company_id' => $this->invoice->company_id,
@ -166,7 +181,7 @@ class UpdateInvoice extends Job
protected function createItems() protected function createItems()
{ {
$sub_total = 0; $sub_total = $discount_amount = $discount_amount_total = 0;
$taxes = []; $taxes = [];
@ -184,8 +199,14 @@ class UpdateInvoice extends Job
$invoice_item = $this->dispatch(new CreateInvoiceItem($item, $this->invoice)); $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 // Calculate totals
$sub_total += $invoice_item->total; $sub_total += $invoice_item->total + $discount_amount;
$discount_amount_total += $discount_amount;
if (!$invoice_item->item_taxes) { if (!$invoice_item->item_taxes) {
continue; continue;
@ -204,6 +225,6 @@ class UpdateInvoice extends Job
} }
} }
return [$sub_total, $taxes]; return [$sub_total, $discount_amount_total, $taxes];
} }
} }

View File

@ -20,6 +20,14 @@ class CoreV208 extends Migration
$table->string('discount_type')->default('normal')->after('discount_rate'); $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']); $table->dropColumn(['discount_rate', 'discount_type']);
} }
); );
Schema::table(
'bill_items',
function (Blueprint $table) {
$table->dropColumn(['discount_rate', 'discount_type']);
}
);
} }
} }

View File

@ -32,6 +32,7 @@ const app = new Vue({
bulk_action: new BulkAction('bills'), bulk_action: new BulkAction('bills'),
totals: { totals: {
sub: 0, sub: 0,
item_discount: '',
discount: '', discount: '',
discount_text: false, discount_text: false,
tax: 0, tax: 0,
@ -107,6 +108,7 @@ const app = new Vue({
onCalculateTotal() { onCalculateTotal() {
let sub_total = 0; let sub_total = 0;
let discount_total = 0; let discount_total = 0;
let item_discount_total = 0;
let tax_total = 0; let tax_total = 0;
let grand_total = 0; let grand_total = 0;
let items = this.form.items; let items = this.form.items;
@ -122,18 +124,21 @@ const app = new Vue({
let item = items[index]; let item = items[index];
// item sub total calcute. // item sub total calcute.
let item_sub_total = item.price * item.quantity; let item_total = item.price * item.quantity;
// item discount calculate. // item discount calculate.
let item_discounted_total = item_sub_total; let item_discounted_total = item_total;
if (discount_in_totals) { 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; discount = discount_in_totals;
} }
let discount_amount = 0;
if (item.discount) { 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; discount = item.discount;
} }
@ -186,7 +191,7 @@ const app = new Vue({
item_tax_total = item_sub_and_tax_total - item_base_rate; 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) { if (compounds.length) {
@ -197,10 +202,15 @@ const app = new Vue({
} }
// set item total // 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. // 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; tax_total += item_tax_total;
} }
} }
@ -208,6 +218,9 @@ const app = new Vue({
// set global total variable. // set global total variable.
this.totals.sub = sub_total; this.totals.sub = sub_total;
this.totals.tax = tax_total; this.totals.tax = tax_total;
this.totals.item_discount = item_discount_total;
sub_total -= item_discount_total;
// Apply discount to total // Apply discount to total
if (discount_in_totals) { if (discount_in_totals) {

View File

@ -32,6 +32,7 @@ const app = new Vue({
bulk_action: new BulkAction('invoices'), bulk_action: new BulkAction('invoices'),
totals: { totals: {
sub: 0, sub: 0,
item_discount: '',
discount: '', discount: '',
discount_text: false, discount_text: false,
tax: 0, tax: 0,
@ -107,6 +108,7 @@ const app = new Vue({
onCalculateTotal() { onCalculateTotal() {
let sub_total = 0; let sub_total = 0;
let discount_total = 0; let discount_total = 0;
let item_discount_total = 0;
let tax_total = 0; let tax_total = 0;
let grand_total = 0; let grand_total = 0;
let items = this.form.items; let items = this.form.items;
@ -122,18 +124,21 @@ const app = new Vue({
let item = items[index]; let item = items[index];
// item sub total calcute. // item sub total calcute.
let item_sub_total = item.price * item.quantity; let item_total = item.price * item.quantity;
// item discount calculate. // item discount calculate.
let item_discounted_total = item_sub_total; let item_discounted_total = item_total;
if (discount_in_totals) { 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; discount = discount_in_totals;
} }
let discount_amount = 0;
if (item.discount) { 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; discount = item.discount;
} }
@ -186,7 +191,7 @@ const app = new Vue({
item_tax_total = item_sub_and_tax_total - item_base_rate; 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) { if (compounds.length) {
@ -197,10 +202,15 @@ const app = new Vue({
} }
// set item total // 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. // 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; tax_total += item_tax_total;
} }
} }
@ -208,6 +218,9 @@ const app = new Vue({
// set global total variable. // set global total variable.
this.totals.sub = sub_total; this.totals.sub = sub_total;
this.totals.tax = tax_total; this.totals.tax = tax_total;
this.totals.item_discount = item_discount_total;
sub_total -= item_discount_total;
// Apply discount to total // Apply discount to total
if (discount_in_totals) { if (discount_in_totals) {

View File

@ -13,6 +13,7 @@ return [
'price' => 'Price', 'price' => 'Price',
'sub_total' => 'Subtotal', 'sub_total' => 'Subtotal',
'discount' => 'Discount', 'discount' => 'Discount',
'item_discount' => 'Item Discount',
'tax_total' => 'Tax Total', 'tax_total' => 'Tax Total',
'total' => 'Total', 'total' => 'Total',

View File

@ -13,6 +13,7 @@ return [
'price' => 'Price', 'price' => 'Price',
'sub_total' => 'Subtotal', 'sub_total' => 'Subtotal',
'discount' => 'Discount', 'discount' => 'Discount',
'item_discount' => 'Item Discount',
'tax_total' => 'Tax Total', 'tax_total' => 'Tax Total',
'total' => 'Total', 'total' => 'Total',

View File

@ -90,6 +90,19 @@
</tr> </tr>
@stack('sub_total_td_end') @stack('sub_total_td_end')
@stack('item_discount_td_start')
<tr id="tr-subtotal">
<td class="text-right border-right-0 border-bottom-0" colspan="6" :colspan="colspan">
<strong>{{ trans('bills.item_discount') }}</strong>
</td>
<td class="text-right border-bottom-0 long-texts">
{{ Form::moneyGroup('item_discount', '', '', ['disabled' => true, 'required' => 'required', 'v-model' => 'totals.item_discount', 'currency' => $currency, 'masked' => 'true'], 0.00, 'text-right d-none') }}
<span id="item-discount" v-if="totals.item_discount" v-html="totals.item_discount"></span>
<span v-else>@money(0, $currency->code, true)</span>
</td>
</tr>
@stack('item_discount_td_end')
@stack('add_discount_td_start') @stack('add_discount_td_start')
<tr id="tr-discount"> <tr id="tr-discount">
<td class="text-right border-right-0 border-bottom-0" colspan="6" :colspan="colspan"> <td class="text-right border-right-0 border-bottom-0" colspan="6" :colspan="colspan">

View File

@ -90,9 +90,22 @@
</tr> </tr>
@stack('sub_total_td_end') @stack('sub_total_td_end')
@stack('item_discount_td_start')
<tr id="tr-subtotal">
<td class="text-right border-right-0 border-bottom-0" colspan="6" :colspan="colspan">
<strong>{{ trans('invoices.item_discount') }}</strong>
</td>
<td class="text-right border-bottom-0 long-texts">
{{ Form::moneyGroup('item_discount', '', '', ['disabled' => true, 'required' => 'required', 'v-model' => 'totals.item_discount', 'currency' => $currency, 'masked' => 'true'], 0.00, 'text-right d-none') }}
<span id="item-discount" v-if="totals.item_discount" v-html="totals.item_discount"></span>
<span v-else>@money(0, $currency->code, true)</span>
</td>
</tr>
@stack('item_discount_td_end')
@stack('add_discount_td_start') @stack('add_discount_td_start')
<tr id="tr-discount"> <tr id="tr-discount">
<td class="text-right border-right-0 border-bottom-0" colspan="5" :colspan="colspan"> <td class="text-right border-right-0 border-bottom-0" colspan="6" :colspan="colspan">
<el-popover <el-popover
popper-class="p-0 h-0" popper-class="p-0 h-0"
placement="bottom" placement="bottom"