incr: group Stock Manager entries

This commit is contained in:
18alantom
2022-11-07 13:28:35 +05:30
parent d9f47c01e3
commit 2ad80001ee
3 changed files with 67 additions and 30 deletions
+41 -3
View File
@@ -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;
+16 -23
View File
@@ -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
);
}
+10 -4
View File
@@ -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 };
export interface SMIDetails extends SMDetails, SMTransferDetails {}
export type StockQueueItem = { rate: Money; quantity: number };