diff --git a/models/baseModels/InvoiceItem/InvoiceItem.ts b/models/baseModels/InvoiceItem/InvoiceItem.ts index bbf71787..05ac01dd 100644 --- a/models/baseModels/InvoiceItem/InvoiceItem.ts +++ b/models/baseModels/InvoiceItem/InvoiceItem.ts @@ -439,62 +439,66 @@ export abstract class InvoiceItem extends Doc { }, setItemDiscountAmount: { formula: async () => { - if ( - !this.fyo.singles.AccountingSettings?.enablePricingRule && - !this.parentdoc?.pricingRuleDetail - ) { - return (this.setItemDiscountAmount = false); + if (!this.fyo.singles.AccountingSettings?.enablePricingRule) { + return this.setItemDiscountAmount; + } + + const hasPricingRule = this.parentdoc?.pricingRuleDetail?.some( + (rule) => rule.referenceItem === this.item + ); + + if (!hasPricingRule && (this.itemDiscountAmount as Money).isZero()) { + return false; } const applicablePricingRules = await getPricingRule( this.parentdoc as SalesInvoice ); - let pricingRuleDoc; + const itemRule = applicablePricingRules?.find( + (rule) => rule.applyOnItem === this.item + ); - applicablePricingRules?.map((val) => { - if (val.applyOnItem == this.item) { - pricingRuleDoc = val.pricingRule; + if (!itemRule) { + if (!this.prule) { + await this.set('itemDiscountAmount', this.itemDiscountAmount); + return true; + } else { + await this.set('itemDiscountAmount', this.fyo.pesa(0)); } - }); - - if (!pricingRuleDoc) { - return this.setItemDiscountAmount; + return false; } + this.prule = itemRule; - if ( - (pricingRuleDoc as PricingRule).discountType === 'Product Discount' - ) { - return this.setItemDiscountAmount; - } + const pricingRuleDoc = itemRule.pricingRule; - if ((pricingRuleDoc as PricingRule).priceDiscountType === 'amount') { - await this.set( - 'itemDiscountAmount', - (pricingRuleDoc as PricingRule).discountAmount - ); + if (pricingRuleDoc.priceDiscountType === 'amount') { + const discountAmount = + pricingRuleDoc.discountAmount ?? this.fyo.pesa(0); + await this.set('itemDiscountAmount', discountAmount); return true; } - return this.setItemDiscountAmount; + return false; }, - dependsOn: ['pricingRuleDetail'], + dependsOn: ['pricingRuleDetail', 'quantity', 'item'], }, itemDiscountPercent: { formula: async () => { - if ( - !this.fyo.singles.AccountingSettings?.enablePricingRule || - !this.parentdoc?.pricingRuleDetail - ) { - return (this.itemDiscountPercent = 0); + if (!this.fyo.singles.AccountingSettings?.enablePricingRule) { + return this.itemDiscountPercent ?? 0; } const pricingRule = this.parentdoc?.pricingRuleDetail?.filter( (prDetail) => prDetail.referenceItem === this.item ); - if (pricingRule && !pricingRule.length) { - return (this.itemDiscountPercent = 0); + if (!pricingRule || !pricingRule.length) { + if (!this.prule) { + return this.itemDiscountPercent; + } else { + return this.fyo.pesa(0); + } } const pricingRuleDoc = (await this.fyo.doc.getDoc( @@ -503,18 +507,17 @@ export abstract class InvoiceItem extends Doc { )) as PricingRule; if (pricingRuleDoc.discountType === 'Product Discount') { - return this.itemDiscountPercent; + return this.itemDiscountPercent ?? 0; } if (pricingRuleDoc.priceDiscountType === 'percentage') { await this.set('setItemDiscountAmount', false); - - return pricingRuleDoc.discountPercentage; + return pricingRuleDoc.discountPercentage ?? 0; } - return this.itemDiscountPercent; + return this.itemDiscountPercent ?? 0; }, - dependsOn: ['pricingRuleDetail'], + dependsOn: ['pricingRuleDetail', 'item'], }, }; diff --git a/src/components/POS/Classic/SelectedItemRow.vue b/src/components/POS/Classic/SelectedItemRow.vue index 449143b0..924a46db 100644 --- a/src/components/POS/Classic/SelectedItemRow.vue +++ b/src/components/POS/Classic/SelectedItemRow.vue @@ -407,7 +407,12 @@ export default defineComponent({ this.$emit('runSinvFormulas'); }, async setQuantity(quantity: number) { - if (quantity <= 0) { + const hasManualDiscount = this.row.setItemDiscountAmount; + const isPercentageDiscount = + !hasManualDiscount && this.row.itemDiscountPercent !== 0; + const manualDiscountAmount = this.row.itemDiscountAmount; + const manualDiscountPercent = this.row.itemDiscountPercent; + if (!this.row.isReturn && quantity <= 0) { showToast({ type: 'error', message: 'Quantity must be greater than zero.', @@ -446,9 +451,20 @@ export default defineComponent({ if (!this.row.isFreeItem) { this.$emit('applyPricingRule'); this.$emit('runSinvFormulas'); - this.row.set('setItemDiscountAmount', false); - this.row.set('itemDiscountPercent', 0); + + if (!hasManualDiscount && !isPercentageDiscount) { + this.row.set('setItemDiscountAmount', false); + this.row.set('itemDiscountPercent', 0); + } this.row.set('rate', this.fyo.pesa(0)); + + if (hasManualDiscount) { + this.row.set('setItemDiscountAmount', true); + this.row.set('itemDiscountAmount', manualDiscountAmount); + } else if (isPercentageDiscount) { + this.row.set('setItemDiscountAmount', false); + this.row.set('itemDiscountPercent', manualDiscountPercent); + } } }, async removeAddedItem(row: SalesInvoiceItem) {