mirror of
https://github.com/frappe/books.git
synced 2026-09-24 15:19:45 -05:00
incr: group Stock Manager entries
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user