2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 19:09:01 +00:00

incr: display discount input on the form

This commit is contained in:
18alantom 2022-07-14 15:28:25 +05:30
parent bab18dc8f7
commit 877cc0b8d4
3 changed files with 47 additions and 45 deletions

View File

@ -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 ?? []) {

View File

@ -1,7 +1,7 @@
<template>
<div>
<label class="flex items-center">
<div class="mr-3 text-gray-900 text-sm" v-if="showLabel && !labelRight">
<div class="mr-3 text-gray-600 text-sm" v-if="showLabel && !labelRight">
{{ df.label }}
</div>
<div style="width: 14px; height: 14px; overflow: hidden; cursor: pointer">
@ -61,7 +61,7 @@
@focus="(e) => $emit('focus', e)"
/>
</div>
<div class="ml-3 text-gray-900 text-sm" v-if="showLabel && labelRight">
<div class="ml-3 text-gray-600 text-sm" v-if="showLabel && labelRight">
{{ df.label }}
</div>
</label>

View File

@ -95,7 +95,26 @@
:read-only="doc?.submitted"
/>
<FormControl
v-if="doc.discountPercent > 0 && !doc.setDiscountAmount"
v-if="doc.enableDiscounting"
:show-label="true"
:label-right="false"
class="
text-base
bg-gray-100
rounded
flex
items-center
justify-center
w-ful
"
input-class="px-3 py-2 text-base bg-transparent text-right"
:df="getField('setDiscountAmount')"
:value="doc.setDiscountAmount"
@change="(value) => doc.set('setDiscountAmount', value)"
:read-only="doc?.submitted"
/>
<FormControl
v-if="doc.enableDiscounting && !doc.setDiscountAmount"
class="text-base bg-gray-100 rounded"
input-class="px-3 py-2 text-base bg-transparent text-right"
:df="getField('discountPercent')"
@ -104,7 +123,7 @@
:read-only="doc?.submitted"
/>
<FormControl
v-if="doc.discountAmount.float > 0 && doc.setDiscountAmount"
v-if="doc.enableDiscounting && doc.setDiscountAmount"
class="text-base bg-gray-100 rounded"
input-class="px-3 py-2 text-base bg-transparent text-right"
:df="getField('discountAmount')"
@ -352,11 +371,10 @@ export default {
return text;
},
totalDiscount() {
const discountAmount = this.doc?.discountAmount ?? fyo.pesa(0);
return discountAmount.add(this.itemDiscountAmount);
return this.discountAmount.add(this.itemDiscountAmount);
},
discountAmount() {
return this.doc?.discountAmount ?? fyo.pesa(0);
return this.doc?.getInvoiceDiscountAmount();
},
itemDiscountAmount() {
return this.doc.getItemDiscountAmount();
@ -397,12 +415,9 @@ export default {
return;
}
const fields = [
'discountAfterTax',
'setDiscountAmount',
'discountAmount',
'discountPercent',
].map((fn) => fyo.getField(this.schemaName, fn));
const fields = ['discountAfterTax'].map((fn) =>
fyo.getField(this.schemaName, fn)
);
this.toggleQuickEditDoc(this.doc, fields);
},