From 7029fa79ae71f9e8a95ed4637289faa64d373254 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Mon, 27 Feb 2023 18:54:49 +0530 Subject: [PATCH] fix: valuation calculation when batches - use InvoiceItem batch on getStockTransfer --- models/baseModels/Invoice/Invoice.ts | 2 ++ models/baseModels/InvoiceItem/InvoiceItem.ts | 1 + reports/inventory/helpers.ts | 21 ++++++++++---------- reports/inventory/types.ts | 2 +- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/models/baseModels/Invoice/Invoice.ts b/models/baseModels/Invoice/Invoice.ts index 9e4d2620..22e85dee 100644 --- a/models/baseModels/Invoice/Invoice.ts +++ b/models/baseModels/Invoice/Invoice.ts @@ -546,6 +546,7 @@ export abstract class Invoice extends Transactional { const item = row.item; const quantity = row.stockNotTransferred; const trackItem = itemDoc.trackItem; + const batchNumber = row.batchNumber || null; let rate = row.rate as Money; if (this.exchangeRate && this.exchangeRate > 1) { @@ -561,6 +562,7 @@ export abstract class Invoice extends Transactional { quantity, location, rate, + batchNumber, }); } diff --git a/models/baseModels/InvoiceItem/InvoiceItem.ts b/models/baseModels/InvoiceItem/InvoiceItem.ts index ec93b471..b70acb1a 100644 --- a/models/baseModels/InvoiceItem/InvoiceItem.ts +++ b/models/baseModels/InvoiceItem/InvoiceItem.ts @@ -29,6 +29,7 @@ export abstract class InvoiceItem extends Doc { quantity?: number; transferQuantity?: number; unitConversionFactor?: number; + batchNumber?: string; tax?: string; stockNotTransferred?: number; diff --git a/reports/inventory/helpers.ts b/reports/inventory/helpers.ts index c1bf70de..5881fc2c 100644 --- a/reports/inventory/helpers.ts +++ b/reports/inventory/helpers.ts @@ -11,6 +11,7 @@ import { type Item = string; type Location = string; +type BatchNo = string; export async function getRawStockLedgerEntries(fyo: Fyo) { const fieldnames = [ @@ -37,29 +38,27 @@ export function getStockLedgerEntries( valuationMethod: ValuationMethod ): ComputedStockLedgerEntry[] { const computedSLEs: ComputedStockLedgerEntry[] = []; - const stockQueues: Record> = {}; + const stockQueues: Record< + Item, + Record> + > = {}; for (const sle of rawSLEs) { const name = safeParseInt(sle.name); const date = new Date(sle.date); const rate = safeParseFloat(sle.rate); - const { - item, - location, - batchNumber, - quantity, - referenceName, - referenceType, - } = sle; + const { item, location, quantity, referenceName, referenceType } = sle; + const batchNumber = sle.batchNumber ?? ''; if (quantity === 0) { continue; } stockQueues[item] ??= {}; - stockQueues[item][location] ??= new StockQueue(); + stockQueues[item][location] ??= {}; + stockQueues[item][location][batchNumber] ??= new StockQueue(); - const q = stockQueues[item][location]; + const q = stockQueues[item][location][batchNumber]; const initialValue = q.value; let incomingRate: number | null; diff --git a/reports/inventory/types.ts b/reports/inventory/types.ts index ca433753..40531446 100644 --- a/reports/inventory/types.ts +++ b/reports/inventory/types.ts @@ -5,7 +5,7 @@ export interface RawStockLedgerEntry { date: string; item: string; rate: string; - batchNumber: string; + batchNumber: string | null; quantity: number; location: string; referenceName: string;