2
0
mirror of https://github.com/frappe/books.git synced 2024-11-15 09:54:04 +00:00
books/models/inventory/tests/helpers.ts

132 lines
2.8 KiB
TypeScript
Raw Normal View History

import { Fyo } from 'fyo';
2023-02-28 06:01:04 +00:00
import { Batch } from 'models/inventory/Batch';
import { ModelNameEnum } from 'models/types';
import { StockMovement } from '../StockMovement';
import { StockTransfer } from '../StockTransfer';
2023-05-04 10:45:12 +00:00
import { MovementTypeEnum } from '../types';
type ALE = {
date: string;
account: string;
party: string;
debit: string;
credit: string;
reverted: number;
};
type SLE = {
date: string;
name: string;
item: string;
location: string;
rate: string;
quantity: string;
};
type Transfer = {
item: string;
from?: string;
to?: string;
2023-02-28 06:01:04 +00:00
batch?: string;
2023-05-04 10:45:12 +00:00
serialNumber?: string;
quantity: number;
rate: number;
};
interface TransferTwo extends Omit<Transfer, 'from' | 'to'> {
location: string;
}
export function getItem(
name: string,
rate: number,
hasBatch = false,
hasSerialNumber = false
) {
2023-05-04 10:45:12 +00:00
return { name, rate, trackItem: true, hasBatch, hasSerialNumber };
2023-02-21 06:07:55 +00:00
}
export function getBatch(
2023-02-28 06:01:04 +00:00
schemaName: ModelNameEnum.Batch,
batch: string,
2023-02-21 06:07:55 +00:00
expiryDate: Date,
manufactureDate: Date,
fyo: Fyo
): Batch {
2023-02-21 06:07:55 +00:00
const doc = fyo.doc.getNewDoc(schemaName, {
2023-02-28 06:01:04 +00:00
batch,
2023-02-21 06:07:55 +00:00
expiryDate,
manufactureDate,
2023-02-28 06:01:04 +00:00
}) as Batch;
2023-02-21 06:07:55 +00:00
return doc;
}
export async function getStockTransfer(
schemaName: ModelNameEnum.PurchaseReceipt | ModelNameEnum.Shipment,
party: string,
date: Date,
transfers: TransferTwo[],
fyo: Fyo
): Promise<StockTransfer> {
const doc = fyo.doc.getNewDoc(schemaName, { party, date }) as StockTransfer;
for (const { item, location, quantity, rate } of transfers) {
await doc.append('items', { item, location, quantity, rate });
}
return doc;
}
export async function getStockMovement(
2023-05-04 10:45:12 +00:00
movementType: MovementTypeEnum,
2022-11-03 10:53:34 +00:00
date: Date,
transfers: Transfer[],
fyo: Fyo
): Promise<StockMovement> {
const doc = fyo.doc.getNewDoc(ModelNameEnum.StockMovement, {
movementType,
2022-11-03 10:53:34 +00:00
date,
}) as StockMovement;
for (const {
item,
from: fromLocation,
to: toLocation,
2023-02-28 06:01:04 +00:00
batch,
2023-05-04 10:45:12 +00:00
serialNumber,
quantity,
rate,
} of transfers) {
await doc.append('items', {
item,
fromLocation,
toLocation,
2023-02-28 06:01:04 +00:00
batch,
2023-05-04 10:45:12 +00:00
serialNumber,
rate,
quantity,
});
}
return doc;
}
export async function getSLEs(
referenceName: string,
referenceType: string,
fyo: Fyo
) {
return (await fyo.db.getAllRaw(ModelNameEnum.StockLedgerEntry, {
filters: { referenceName, referenceType },
fields: ['date', 'name', 'item', 'location', 'rate', 'quantity'],
})) as SLE[];
}
export async function getALEs(
referenceName: string,
referenceType: string,
fyo: Fyo
) {
return (await fyo.db.getAllRaw(ModelNameEnum.AccountingLedgerEntry, {
filters: { referenceName, referenceType },
fields: ['date', 'account', 'party', 'debit', 'credit', 'reverted'],
})) as ALE[];
}