From b3e4073bb795db1b077c44f585044608bb4bbaf8 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Mon, 27 Feb 2023 19:41:39 +0530 Subject: [PATCH] feat: batchwise filters in stock ledger and stock balance --- reports/inventory/StockBalance.ts | 22 ++++++++++++++++++++- reports/inventory/StockLedger.ts | 32 ++++++++++++++++++++++++++----- reports/inventory/helpers.ts | 22 ++++++++++++++------- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/reports/inventory/StockBalance.ts b/reports/inventory/StockBalance.ts index 4cd8944c..827bb057 100644 --- a/reports/inventory/StockBalance.ts +++ b/reports/inventory/StockBalance.ts @@ -25,9 +25,11 @@ export class StockBalance extends StockLedger { const filters = { item: this.item, location: this.location, + batch: this.batch, fromDate: this.fromDate, toDate: this.toDate, }; + const rawData = getStockBalanceEntries(this._rawData ?? [], filters); return rawData.map((sbe, i) => { @@ -41,7 +43,7 @@ export class StockBalance extends StockLedger { } getFilters(): Field[] { - return [ + const filters = [ { fieldtype: 'Link', target: 'Item', @@ -56,6 +58,17 @@ export class StockBalance extends StockLedger { label: t`Location`, fieldname: 'location', }, + ...(this.hasBatches + ? [ + { + fieldtype: 'Link', + target: 'BatchNumber', + placeholder: t`Batch`, + label: t`Batch`, + fieldname: 'batch', + }, + ] + : []), { fieldtype: 'Date', placeholder: t`From Date`, @@ -69,6 +82,8 @@ export class StockBalance extends StockLedger { fieldname: 'toDate', }, ] as Field[]; + + return filters; } getColumns(): ColumnField[] { @@ -89,6 +104,11 @@ export class StockBalance extends StockLedger { label: 'Location', fieldtype: 'Link', }, + ...(this.hasBatches + ? ([ + { fieldname: 'batch', label: 'Batch', fieldtype: 'Link' }, + ] as ColumnField[]) + : []), { fieldname: 'balanceQuantity', label: 'Balance Qty.', diff --git a/reports/inventory/StockLedger.ts b/reports/inventory/StockLedger.ts index 2150ed7a..bed69661 100644 --- a/reports/inventory/StockLedger.ts +++ b/reports/inventory/StockLedger.ts @@ -3,6 +3,7 @@ import { RawValueMap } from 'fyo/core/types'; import { Action } from 'fyo/model/types'; import { cloneDeep } from 'lodash'; import { DateTime } from 'luxon'; +import { InventorySettings } from 'models/inventory/InventorySettings'; import { ValuationMethod } from 'models/inventory/types'; import { ModelNameEnum } from 'models/types'; import getCommonExportActions from 'reports/commonExporter'; @@ -26,6 +27,7 @@ export class StockLedger extends Report { item?: string; location?: string; + batch?: string; fromDate?: string; toDate?: string; ascending?: boolean; @@ -34,6 +36,11 @@ export class StockLedger extends Report { groupBy: 'none' | 'item' | 'location' = 'none'; + get hasBatches(): boolean { + return !!(this.fyo.singles.InventorySettings as InventorySettings) + .enableBatches; + } + constructor(fyo: Fyo) { super(fyo); this._setObservers(); @@ -110,6 +117,10 @@ export class StockLedger extends Report { continue; } + if (this.batch && row.batchNumber !== this.batch) { + continue; + } + const date = row.date.valueOf(); if (toDate && date > toDate) { continue; @@ -260,11 +271,11 @@ export class StockLedger extends Report { label: 'Location', fieldtype: 'Link', }, - { - fieldname: 'batchNumber', - label: 'Batch No.', - fieldtype: 'Link', - }, + ...(this.hasBatches + ? ([ + { fieldname: 'batch', label: 'Batch', fieldtype: 'Link' }, + ] as ColumnField[]) + : []), { fieldname: 'quantity', label: 'Quantity', @@ -344,6 +355,17 @@ export class StockLedger extends Report { label: t`Location`, fieldname: 'location', }, + ...(this.hasBatches + ? ([ + { + fieldtype: 'Link', + target: 'BatchNumber', + placeholder: t`Batch`, + label: t`Batch`, + fieldname: 'batch', + }, + ] as Field[]) + : []), { fieldtype: 'Date', placeholder: t`From Date`, diff --git a/reports/inventory/helpers.ts b/reports/inventory/helpers.ts index 5881fc2c..e110a96f 100644 --- a/reports/inventory/helpers.ts +++ b/reports/inventory/helpers.ts @@ -117,7 +117,10 @@ export function getStockBalanceEntries( toDate?: string; } ): StockBalanceEntry[] { - const sbeMap: Record> = {}; + const sbeMap: Record< + Item, + Record> + > = {}; const fromDate = filters.fromDate ? Date.parse(filters.fromDate) : null; const toDate = filters.toDate ? Date.parse(filters.toDate) : null; @@ -131,16 +134,19 @@ export function getStockBalanceEntries( continue; } + const batchNumber = sle.batchNumber || ''; + sbeMap[sle.item] ??= {}; - sbeMap[sle.item][sle.location] ??= getSBE( + sbeMap[sle.item][sle.location] ??= {}; + sbeMap[sle.item][sle.location][batchNumber] ??= getSBE( sle.item, sle.location, - sle.batchNumber + batchNumber ); const date = sle.date.valueOf(); if (fromDate && date < fromDate) { - const sbe = sbeMap[sle.item][sle.location]!; + const sbe = sbeMap[sle.item][sle.location][batchNumber]; updateOpeningBalances(sbe, sle); continue; } @@ -149,13 +155,15 @@ export function getStockBalanceEntries( continue; } - const sbe = sbeMap[sle.item][sle.location]!; + const sbe = sbeMap[sle.item][sle.location][batchNumber]; updateCurrentBalances(sbe, sle); } return Object.values(sbeMap) - .map((sbes) => Object.values(sbes)) - .flat(); + .map((sbeBatched) => + Object.values(sbeBatched).map((sbes) => Object.values(sbes)) + ) + .flat(2); } function getSBE(