Skip to content

Commit

Permalink
fix: validation for discount amount and percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
AbleKSaju committed Nov 27, 2024
1 parent 403bf3d commit a9942b7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 42 deletions.
8 changes: 0 additions & 8 deletions src/components/POS/Modern/ModernPOSSelectedItemRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,6 @@ export default defineComponent({
return;
}
if (
(field == 'itemDiscountAmount' || field == 'itemDiscountPercent') &&
((row.itemDiscountPercent as number) > 0 ||
!row.itemDiscountAmount?.isZero())
) {
return;
}
this.$emit('selectedRow', row, field);
this.$emit('toggleModal', 'Keyboard');
},
Expand Down
102 changes: 68 additions & 34 deletions src/pages/POS/KeyboardModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ import Currency from 'src/components/Controls/Currency.vue';
import { updatePricingRuleItem } from 'models/helpers';
import { SalesInvoice } from 'models/baseModels/SalesInvoice/SalesInvoice';
import { SalesInvoiceItem } from 'models/baseModels/SalesInvoiceItem/SalesInvoiceItem';
import { ValidationError } from 'fyo/utils/errors';
import { showToast } from 'src/utils/interactive';
export default defineComponent({
name: 'KeyboardModal',
Expand Down Expand Up @@ -393,51 +395,83 @@ export default defineComponent({
this.selectedValue = value;
},
async saveSelectedItem() {
if (
this.selectedItemRow?.fieldMap[this.selectedItemField].fieldtype ===
ModelNameEnum.Currency
) {
this.selectedItemRow[this.selectedItemField] = this.fyo.pesa(
Number(this.selectedValue)
);
if (this.selectedItemField === 'rate') {
this.selectedItemRow.setRate = this.fyo.pesa(
try {
if (
this.selectedItemRow?.fieldMap[this.selectedItemField].fieldtype ===
ModelNameEnum.Currency
) {
this.selectedItemRow[this.selectedItemField] = this.fyo.pesa(
Number(this.selectedValue)
);
await this.sinvDoc.runFormulas();
this.$emit('toggleModal', 'Keyboard');
if (this.selectedItemField === 'rate') {
this.selectedItemRow.setRate = this.fyo.pesa(
Number(this.selectedValue)
);
return;
}
await this.sinvDoc.runFormulas();
this.$emit('toggleModal', 'Keyboard');
if (this.selectedItemField === 'itemDiscountAmount') {
this.selectedItemRow.setItemDiscountAmount = true;
this.selectedItemRow.itemDiscountAmount = this.fyo.pesa(
Number(this.selectedValue)
);
}
} else {
this.selectedItemRow![this.selectedItemField] = Number(
this.selectedValue
);
return;
}
if (this.selectedItemField === 'itemDiscountPercent') {
await this.selectedItemRow?.set('setItemDiscountAmount', false);
await this.selectedItemRow?.set(
'itemDiscountPercent',
if (this.selectedItemField === 'itemDiscountAmount') {
if (this.sinvDoc.grandTotal?.lte(this.selectedValue)) {
this.selectedItemRow.itemDiscountAmount = this.fyo.pesa(
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
);
}
if (this.selectedItemField === 'quantity') {
await updatePricingRuleItem(this.sinvDoc);
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);
}
}
}
await this.sinvDoc.runFormulas();
this.$emit('toggleModal', 'Keyboard');
await this.sinvDoc.runFormulas();
this.$emit('toggleModal', 'Keyboard');
} catch (error) {
showToast({
type: 'error',
message: this.t`${error as string}`,
});
}
},
async deleteLast() {
this.selectedValue = this.selectedValue?.slice(0, -1);
Expand Down

0 comments on commit a9942b7

Please sign in to comment.