From 2ad80001ee830c71e132aed514fc3089cdfaa955 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Sat, 29 Oct 2022 12:37:52 +0530 Subject: [PATCH] incr: group Stock Manager entries --- models/inventory/StockManager.ts | 44 ++++++++++++++++++++++++++++--- models/inventory/StockMovement.ts | 39 +++++++++++---------------- models/inventory/types.ts | 14 +++++++--- 3 files changed, 67 insertions(+), 30 deletions(-) diff --git a/models/inventory/StockManager.ts b/models/inventory/StockManager.ts index 5eeb319e..739fa078 100644 --- a/models/inventory/StockManager.ts +++ b/models/inventory/StockManager.ts @@ -5,11 +5,49 @@ import { Money } from 'pesa'; import { getStockQueue } from './helpers'; import { StockLedgerEntry } from './StockLedgerEntry'; import { StockQueue } from './StockQueue'; -import { SMDetails } from './types'; +import { SMDetails, SMIDetails, SMTransferDetails } from './types'; export class StockManager { /** - * The Stock Manager is used to move stock to and from a location. It + * The Stock Manager manages a group of Stock Manager Items + * all of which would belong to a single transaction such as a + * single Stock Movement entry. + */ + + items: StockManagerItem[]; + details: SMDetails; + + isCancelled: boolean; + fyo: Fyo; + + constructor(details: SMDetails, isCancelled: boolean, fyo: Fyo) { + this.items = []; + this.details = details; + this.isCancelled = isCancelled; + this.fyo = fyo; + } + + async transferStock(transferDetails: SMTransferDetails) { + const details = this.#getSMIDetails(transferDetails); + const item = new StockManagerItem(details, this.fyo); + await item.transferStock(this.isCancelled); + this.items.push(item); + } + + async sync() { + for (const item of this.items) { + await item.sync(); + } + } + + #getSMIDetails(transferDetails: SMTransferDetails): SMIDetails { + return Object.assign({}, this.details, transferDetails); + } +} + +export class StockManagerItem { + /** + * The Stock Manager Item is used to move stock to and from a location. It * updates the Stock Queue and creates Stock Ledger Entries. * * 1. Get existing stock Queue @@ -35,7 +73,7 @@ export class StockManager { fyo: Fyo; - constructor(details: SMDetails, fyo: Fyo) { + constructor(details: SMIDetails, fyo: Fyo) { this.date = details.date; this.item = details.item; this.rate = details.rate; diff --git a/models/inventory/StockMovement.ts b/models/inventory/StockMovement.ts index bb9d2764..fb08c25f 100644 --- a/models/inventory/StockMovement.ts +++ b/models/inventory/StockMovement.ts @@ -52,38 +52,31 @@ export class StockMovement extends Doc { } async _transferStock() { - const stockManagers = this._getStockManagers(); - for (const sm of stockManagers) { - sm.transferStock(this.isCancelled); - } - - for (const sm of stockManagers) { - await sm.sync(); - } + const stockManager = this._getStockManager(); + this._makeTransfers(stockManager); + await stockManager.sync(); } - _getStockManagers(): StockManager[] { - const stockManagers: StockManager[] = []; + _makeTransfers(stockManager: StockManager) { for (const row of this.items ?? []) { - const stockManager = this._getStockManager(row); - stockManagers.push(stockManager); - } - - return stockManagers; - } - - _getStockManager(row: StockMovementItem): StockManager { - return new StockManager( - { - date: this.date!, + stockManager.transferStock({ item: row.item!, rate: row.rate!, quantity: row.quantity!, - referenceName: this.name!, - referenceType: this.schemaName, fromLocation: row.fromLocation, toLocation: row.toLocation, + }); + } + } + + _getStockManager(): StockManager { + return new StockManager( + { + date: this.date!, + referenceName: this.name!, + referenceType: this.schemaName, }, + this.isCancelled, this.fyo ); } diff --git a/models/inventory/types.ts b/models/inventory/types.ts index 2f9a3e9b..30be4908 100644 --- a/models/inventory/types.ts +++ b/models/inventory/types.ts @@ -1,4 +1,4 @@ -import { Money } from "pesa"; +import { Money } from 'pesa'; export type MovementType = | 'MaterialIssue' @@ -7,13 +7,19 @@ export type MovementType = export interface SMDetails { date: Date; + referenceName: string; + referenceType: string; +} + +export interface SMTransferDetails { item: string; rate: Money; quantity: number; - referenceName: string; - referenceType: string; fromLocation?: string; toLocation?: string; } -export type StockQueueItem = { rate: Money; quantity: number }; \ No newline at end of file +export interface SMIDetails extends SMDetails, SMTransferDetails {} + + +export type StockQueueItem = { rate: Money; quantity: number };