2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/models/inventory/StockMovement.ts

77 lines
1.8 KiB
TypeScript
Raw Normal View History

import { Doc } from 'fyo/model/doc';
import {
DefaultMap,
FiltersMap,
FormulaMap,
ListViewSettings
} from 'fyo/model/types';
import { ModelNameEnum } from 'models/types';
import { Money } from 'pesa';
2022-10-29 06:15:23 +00:00
import { StockManager } from './StockManager';
import { StockMovementItem } from './StockMovementItem';
import { MovementType } from './types';
export class StockMovement extends Doc {
name?: string;
date?: Date;
numberSeries?: string;
movementType?: MovementType;
items?: StockMovementItem[];
amount?: Money;
formulas: FormulaMap = {
amount: {
formula: () => {
return this.items?.reduce(
(acc, item) => acc.add(item.amount ?? 0),
this.fyo.pesa(0)
);
},
dependsOn: ['items'],
},
};
static filters: FiltersMap = {
numberSeries: () => ({ referenceType: ModelNameEnum.StockMovement }),
};
static defaults: DefaultMap = {
date: () => new Date(),
};
static getListViewSettings(): ListViewSettings {
return { columns: ['name', 'date', 'movementType'] };
}
2022-10-29 06:15:23 +00:00
async afterSubmit(): Promise<void> {
const transferDetails = this._getTransferDetails();
await this._getStockManager().createTransfers(transferDetails);
2022-10-29 06:15:23 +00:00
}
async afterCancel(): Promise<void> {
await this._getStockManager().cancelTransfers();
2022-10-29 06:15:23 +00:00
}
_getTransferDetails() {
return (this.items ?? []).map((row) => ({
item: row.item!,
rate: row.rate!,
quantity: row.quantity!,
fromLocation: row.fromLocation,
toLocation: row.toLocation,
}));
2022-10-29 06:15:23 +00:00
}
2022-10-29 07:07:52 +00:00
_getStockManager(): StockManager {
2022-10-29 06:15:23 +00:00
return new StockManager(
{
date: this.date!,
referenceName: this.name!,
referenceType: this.schemaName,
},
2022-10-29 07:07:52 +00:00
this.isCancelled,
2022-10-29 06:15:23 +00:00
this.fyo
);
}
}