diff --git a/models/helpers.ts b/models/helpers.ts index e4f8566a..e8c490d3 100644 --- a/models/helpers.ts +++ b/models/helpers.ts @@ -1087,10 +1087,22 @@ export async function getPricingRule( continue; } + const itemQuantity: Record = {}; + + for (const item of doc.items) { + if (!item?.item) continue; + + if (!itemQuantity[item.item]) { + itemQuantity[item.item] = item.quantity ?? 0; + } else { + itemQuantity[item.item] += item.quantity ?? 0; + } + } + const filtered = filterPricingRules( doc as SalesInvoice, pricingRuleDocsForItem, - item.quantity as number, + itemQuantity[item.item as string], item.amount as Money ); diff --git a/src/pages/POS/POS.vue b/src/pages/POS/POS.vue index 472eecfd..a49748f7 100644 --- a/src/pages/POS/POS.vue +++ b/src/pages/POS/POS.vue @@ -690,12 +690,39 @@ export default defineComponent({ )) as number; if (item.hasBatch) { + const addQty = quantity ?? 1; + + if (existingItems.length > 0) { + for (let item of existingItems) { + const availableQty = await fyo.db.getStockQuantity( + item.item as string, + undefined, + undefined, + undefined, + item.batch + ); + if ( + item.batch != null && + availableQty != null && + availableQty > (item.quantity as number) + ) { + const currentQty = item.quantity ?? 0; + await item.set('quantity', currentQty + addQty); + await this.applyPricingRule(); + await this.sinvDoc.runFormulas(); + return; + } + } + } + await this.sinvDoc.append('items', { rate: item.rate as Money, item: item.name, - quantity: quantity ? quantity : 1, + quantity: addQty, hsnCode: itemsHsncode, }); + await this.applyPricingRule(); + await this.sinvDoc.runFormulas(); return; }