From 877cc0b8d4a21bb209a7d8a26c08715c78fef456 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Thu, 14 Jul 2022 15:28:25 +0530 Subject: [PATCH] incr: display discount input on the form --- models/baseModels/Invoice/Invoice.ts | 51 +++++++++++----------------- src/components/Controls/Check.vue | 4 +-- src/pages/InvoiceForm.vue | 37 ++++++++++++++------ 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/models/baseModels/Invoice/Invoice.ts b/models/baseModels/Invoice/Invoice.ts index 72473926..902222d9 100644 --- a/models/baseModels/Invoice/Invoice.ts +++ b/models/baseModels/Invoice/Invoice.ts @@ -178,8 +178,8 @@ export abstract class Invoice extends Transactional { async getGrandTotal() { const itemDiscountAmount = this.getItemDiscountAmount(); - const discountAmount = this.discountAmount ?? this.fyo.pesa(0); - const totalDiscount = itemDiscountAmount.add(discountAmount); + const invoiceDiscountAmount = this.getInvoiceDiscountAmount(); + const totalDiscount = itemDiscountAmount.add(invoiceDiscountAmount); return ((this.taxes ?? []) as Doc[]) .map((doc) => doc.amount as Money) @@ -187,6 +187,23 @@ export abstract class Invoice extends Transactional { .sub(totalDiscount); } + getInvoiceDiscountAmount() { + if (this.setDiscountAmount) { + return this.discountAmount ?? this.fyo.pesa(0); + } + + let totalItemAmounts = this.fyo.pesa(0); + for (const item of this.items ?? []) { + if (this.discountAfterTax) { + totalItemAmounts = totalItemAmounts.add(item.itemTaxedTotal!); + } else { + totalItemAmounts = totalItemAmounts.add(item.itemDiscountedTotal!); + } + } + + return totalItemAmounts.percent(this.discountPercent ?? 0); + } + getItemDiscountAmount() { if (!this?.items?.length) { return this.fyo.pesa(0); @@ -248,24 +265,6 @@ export abstract class Invoice extends Transactional { formula: async () => this.netTotal!.mul(this.exchangeRate!), }, taxes: { formula: async () => await this.getTaxSummary() }, - discountAmount: { - formula: async (fieldname) => { - if (fieldname !== 'discountPercent') { - return this.discountAmount!; - } - - return this.getDiscountAmountFromPercent(); - }, - }, - discountPercent: { - formula: async (fieldname) => { - if (fieldname === 'discountAmount') { - return this.getDiscountPercentFromAmount(); - } - - return this.discountPercent!; - }, - }, grandTotal: { formula: async () => await this.getGrandTotal() }, baseGrandTotal: { formula: async () => (this.grandTotal as Money).mul(this.exchangeRate!), @@ -281,18 +280,6 @@ export abstract class Invoice extends Transactional { }, }; - getDiscountPercentFromAmount() { - const discountAmount = this.discountAmount ?? this.fyo.pesa(0); - const itemDiscountedAmounts = this.getItemDiscountedAmounts(); - return discountAmount.mul(100).div(itemDiscountedAmounts).float; - } - - getDiscountAmountFromPercent() { - const discountPercent = this.discountPercent ?? 0; - const itemDiscountedAmounts = this.getItemDiscountedAmounts(); - return itemDiscountedAmounts.percent(discountPercent); - } - getItemDiscountedAmounts() { let itemDiscountedAmounts = this.fyo.pesa(0); for (const item of this.items ?? []) { diff --git a/src/components/Controls/Check.vue b/src/components/Controls/Check.vue index f449381d..18103cd6 100644 --- a/src/components/Controls/Check.vue +++ b/src/components/Controls/Check.vue @@ -1,7 +1,7 @@