2
0
mirror of https://github.com/frappe/books.git synced 2025-01-23 15:18:24 +00:00

fix: validation for discount amount and percentage

This commit is contained in:
AbleKSaju 2024-11-27 12:06:59 +05:30
parent 403bf3de98
commit a9942b7858
2 changed files with 68 additions and 42 deletions

View File

@ -293,14 +293,6 @@ export default defineComponent({
return; return;
} }
if (
(field == 'itemDiscountAmount' || field == 'itemDiscountPercent') &&
((row.itemDiscountPercent as number) > 0 ||
!row.itemDiscountAmount?.isZero())
) {
return;
}
this.$emit('selectedRow', row, field); this.$emit('selectedRow', row, field);
this.$emit('toggleModal', 'Keyboard'); this.$emit('toggleModal', 'Keyboard');
}, },

View File

@ -319,6 +319,8 @@ import Currency from 'src/components/Controls/Currency.vue';
import { updatePricingRuleItem } from 'models/helpers'; import { updatePricingRuleItem } from 'models/helpers';
import { SalesInvoice } from 'models/baseModels/SalesInvoice/SalesInvoice'; import { SalesInvoice } from 'models/baseModels/SalesInvoice/SalesInvoice';
import { SalesInvoiceItem } from 'models/baseModels/SalesInvoiceItem/SalesInvoiceItem'; import { SalesInvoiceItem } from 'models/baseModels/SalesInvoiceItem/SalesInvoiceItem';
import { ValidationError } from 'fyo/utils/errors';
import { showToast } from 'src/utils/interactive';
export default defineComponent({ export default defineComponent({
name: 'KeyboardModal', name: 'KeyboardModal',
@ -393,51 +395,83 @@ export default defineComponent({
this.selectedValue = value; this.selectedValue = value;
}, },
async saveSelectedItem() { async saveSelectedItem() {
if ( try {
this.selectedItemRow?.fieldMap[this.selectedItemField].fieldtype === if (
ModelNameEnum.Currency this.selectedItemRow?.fieldMap[this.selectedItemField].fieldtype ===
) { ModelNameEnum.Currency
this.selectedItemRow[this.selectedItemField] = this.fyo.pesa( ) {
Number(this.selectedValue) this.selectedItemRow[this.selectedItemField] = this.fyo.pesa(
);
if (this.selectedItemField === 'rate') {
this.selectedItemRow.setRate = this.fyo.pesa(
Number(this.selectedValue) Number(this.selectedValue)
); );
await this.sinvDoc.runFormulas(); if (this.selectedItemField === 'rate') {
this.$emit('toggleModal', 'Keyboard'); this.selectedItemRow.setRate = this.fyo.pesa(
Number(this.selectedValue)
);
return; await this.sinvDoc.runFormulas();
} this.$emit('toggleModal', 'Keyboard');
if (this.selectedItemField === 'itemDiscountAmount') { return;
this.selectedItemRow.setItemDiscountAmount = true; }
this.selectedItemRow.itemDiscountAmount = this.fyo.pesa(
Number(this.selectedValue)
);
}
} else {
this.selectedItemRow![this.selectedItemField] = Number(
this.selectedValue
);
if (this.selectedItemField === 'itemDiscountPercent') { if (this.selectedItemField === 'itemDiscountAmount') {
await this.selectedItemRow?.set('setItemDiscountAmount', false); if (this.sinvDoc.grandTotal?.lte(this.selectedValue)) {
await this.selectedItemRow?.set( this.selectedItemRow.itemDiscountAmount = this.fyo.pesa(
'itemDiscountPercent', Number(0)
);
throw new ValidationError(
this.fyo.t`Discount Amount (${this.fyo.format(
this.selectedValue,
'Currency'
)}) cannot be greated than Amount (${this.fyo.format(
this.sinvDoc.grandTotal,
'Currency'
)}).`
);
}
this.selectedItemRow.setItemDiscountAmount = true;
this.selectedItemRow.itemDiscountAmount = this.fyo.pesa(
Number(this.selectedValue)
);
}
} else {
this.selectedItemRow![this.selectedItemField] = Number(
this.selectedValue this.selectedValue
); );
if (this.selectedItemField === 'itemDiscountPercent') {
if (Number(this.selectedValue) > 100) {
await this.selectedItemRow?.set('itemDiscountPercent', 0);
throw new ValidationError(
this.fyo
.t`Discount Percent (${this.selectedValue}) cannot be greater than 100.`
);
}
await this.selectedItemRow?.set('setItemDiscountAmount', false);
await this.selectedItemRow?.set(
'itemDiscountPercent',
this.selectedValue
);
}
if (this.selectedItemField === 'quantity') {
await updatePricingRuleItem(this.sinvDoc);
}
} }
if (this.selectedItemField === 'quantity') { await this.sinvDoc.runFormulas();
await updatePricingRuleItem(this.sinvDoc); this.$emit('toggleModal', 'Keyboard');
} } catch (error) {
showToast({
type: 'error',
message: this.t`${error as string}`,
});
} }
await this.sinvDoc.runFormulas();
this.$emit('toggleModal', 'Keyboard');
}, },
async deleteLast() { async deleteLast() {
this.selectedValue = this.selectedValue?.slice(0, -1); this.selectedValue = this.selectedValue?.slice(0, -1);