inventory
This commit is contained in:
@@ -3,13 +3,18 @@ import type {
|
||||
InventoryBomLineInput,
|
||||
InventoryItemDetailDto,
|
||||
InventoryItemInput,
|
||||
InventoryStockBalanceDto,
|
||||
WarehouseDetailDto,
|
||||
WarehouseInput,
|
||||
WarehouseLocationOptionDto,
|
||||
WarehouseLocationDto,
|
||||
WarehouseLocationInput,
|
||||
WarehouseSummaryDto,
|
||||
InventoryItemStatus,
|
||||
InventoryItemSummaryDto,
|
||||
InventoryTransactionDto,
|
||||
InventoryTransactionInput,
|
||||
InventoryTransactionType,
|
||||
InventoryItemType,
|
||||
InventoryUnitOfMeasure,
|
||||
} from "@mrp/shared/dist/inventory/types.js";
|
||||
@@ -44,6 +49,30 @@ type InventoryDetailRecord = {
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
bomLines: BomLineRecord[];
|
||||
inventoryTransactions: InventoryTransactionRecord[];
|
||||
};
|
||||
|
||||
type InventoryTransactionRecord = {
|
||||
id: string;
|
||||
transactionType: string;
|
||||
quantity: number;
|
||||
reference: string;
|
||||
notes: string;
|
||||
createdAt: Date;
|
||||
warehouse: {
|
||||
id: string;
|
||||
code: string;
|
||||
name: string;
|
||||
};
|
||||
location: {
|
||||
id: string;
|
||||
code: string;
|
||||
name: string;
|
||||
};
|
||||
createdBy: {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
} | null;
|
||||
};
|
||||
|
||||
type WarehouseLocationRecord = {
|
||||
@@ -85,6 +114,64 @@ function mapWarehouseLocation(record: WarehouseLocationRecord): WarehouseLocatio
|
||||
};
|
||||
}
|
||||
|
||||
function getSignedQuantity(transactionType: InventoryTransactionType, quantity: number) {
|
||||
return transactionType === "RECEIPT" || transactionType === "ADJUSTMENT_IN" ? quantity : -quantity;
|
||||
}
|
||||
|
||||
function mapInventoryTransaction(record: InventoryTransactionRecord): InventoryTransactionDto {
|
||||
const transactionType = record.transactionType as InventoryTransactionType;
|
||||
const signedQuantity = getSignedQuantity(transactionType, record.quantity);
|
||||
|
||||
return {
|
||||
id: record.id,
|
||||
transactionType,
|
||||
quantity: record.quantity,
|
||||
signedQuantity,
|
||||
notes: record.notes,
|
||||
reference: record.reference,
|
||||
createdAt: record.createdAt.toISOString(),
|
||||
warehouseId: record.warehouse.id,
|
||||
warehouseCode: record.warehouse.code,
|
||||
warehouseName: record.warehouse.name,
|
||||
locationId: record.location.id,
|
||||
locationCode: record.location.code,
|
||||
locationName: record.location.name,
|
||||
createdByName: record.createdBy ? `${record.createdBy.firstName} ${record.createdBy.lastName}`.trim() : "System",
|
||||
};
|
||||
}
|
||||
|
||||
function buildStockBalances(transactions: InventoryTransactionRecord[]): InventoryStockBalanceDto[] {
|
||||
const grouped = new Map<string, InventoryStockBalanceDto>();
|
||||
|
||||
for (const transaction of transactions) {
|
||||
const transactionType = transaction.transactionType as InventoryTransactionType;
|
||||
const signedQuantity = getSignedQuantity(transactionType, transaction.quantity);
|
||||
const key = `${transaction.warehouse.id}:${transaction.location.id}`;
|
||||
const current = grouped.get(key);
|
||||
|
||||
if (current) {
|
||||
current.quantityOnHand += signedQuantity;
|
||||
continue;
|
||||
}
|
||||
|
||||
grouped.set(key, {
|
||||
warehouseId: transaction.warehouse.id,
|
||||
warehouseCode: transaction.warehouse.code,
|
||||
warehouseName: transaction.warehouse.name,
|
||||
locationId: transaction.location.id,
|
||||
locationCode: transaction.location.code,
|
||||
locationName: transaction.location.name,
|
||||
quantityOnHand: signedQuantity,
|
||||
});
|
||||
}
|
||||
|
||||
return [...grouped.values()]
|
||||
.filter((balance) => balance.quantityOnHand !== 0)
|
||||
.sort((left, right) =>
|
||||
`${left.warehouseCode}-${left.locationCode}`.localeCompare(`${right.warehouseCode}-${right.locationCode}`)
|
||||
);
|
||||
}
|
||||
|
||||
function mapSummary(record: {
|
||||
id: string;
|
||||
sku: string;
|
||||
@@ -112,6 +199,12 @@ function mapSummary(record: {
|
||||
}
|
||||
|
||||
function mapDetail(record: InventoryDetailRecord): InventoryItemDetailDto {
|
||||
const recentTransactions = record.inventoryTransactions
|
||||
.slice()
|
||||
.sort((left, right) => right.createdAt.getTime() - left.createdAt.getTime())
|
||||
.map(mapInventoryTransaction);
|
||||
const stockBalances = buildStockBalances(record.inventoryTransactions);
|
||||
|
||||
return {
|
||||
...mapSummary({
|
||||
id: record.id,
|
||||
@@ -130,6 +223,9 @@ function mapDetail(record: InventoryDetailRecord): InventoryItemDetailDto {
|
||||
notes: record.notes,
|
||||
createdAt: record.createdAt.toISOString(),
|
||||
bomLines: record.bomLines.slice().sort((a, b) => a.position - b.position).map(mapBomLine),
|
||||
onHandQuantity: stockBalances.reduce((sum, balance) => sum + balance.quantityOnHand, 0),
|
||||
stockBalances,
|
||||
recentTransactions,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -192,7 +288,7 @@ function normalizeBomLines(bomLines: InventoryBomLineInput[]) {
|
||||
return bomLines
|
||||
.map((line, index) => ({
|
||||
componentItemId: line.componentItemId,
|
||||
quantity: line.quantity,
|
||||
quantity: Number(line.quantity),
|
||||
unitOfMeasure: line.unitOfMeasure,
|
||||
notes: line.notes,
|
||||
position: line.position ?? (index + 1) * 10,
|
||||
@@ -217,6 +313,10 @@ async function validateBomLines(parentItemId: string | null, bomLines: Inventory
|
||||
return { ok: false as const, reason: "BOM line quantity must be greater than zero." };
|
||||
}
|
||||
|
||||
if (normalized.some((line) => !Number.isInteger(line.quantity))) {
|
||||
return { ok: false as const, reason: "BOM line quantity must be a whole number." };
|
||||
}
|
||||
|
||||
if (parentItemId && normalized.some((line) => line.componentItemId === parentItemId)) {
|
||||
return { ok: false as const, reason: "An item cannot reference itself in its BOM." };
|
||||
}
|
||||
@@ -298,12 +398,108 @@ export async function getInventoryItemById(itemId: string) {
|
||||
},
|
||||
orderBy: [{ position: "asc" }, { createdAt: "asc" }],
|
||||
},
|
||||
inventoryTransactions: {
|
||||
include: {
|
||||
warehouse: {
|
||||
select: {
|
||||
id: true,
|
||||
code: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
location: {
|
||||
select: {
|
||||
id: true,
|
||||
code: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
createdBy: {
|
||||
select: {
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: [{ createdAt: "desc" }],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return item ? mapDetail(item) : null;
|
||||
}
|
||||
|
||||
export async function listWarehouseLocationOptions() {
|
||||
const warehouses = await prisma.warehouse.findMany({
|
||||
include: {
|
||||
locations: {
|
||||
orderBy: [{ code: "asc" }],
|
||||
},
|
||||
},
|
||||
orderBy: [{ code: "asc" }],
|
||||
});
|
||||
|
||||
return warehouses.flatMap((warehouse): WarehouseLocationOptionDto[] =>
|
||||
warehouse.locations.map((location) => ({
|
||||
warehouseId: warehouse.id,
|
||||
warehouseCode: warehouse.code,
|
||||
warehouseName: warehouse.name,
|
||||
locationId: location.id,
|
||||
locationCode: location.code,
|
||||
locationName: location.name,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
export async function createInventoryTransaction(itemId: string, payload: InventoryTransactionInput, createdById?: string | null) {
|
||||
const item = await prisma.inventoryItem.findUnique({
|
||||
where: { id: itemId },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!item) {
|
||||
return { ok: false as const, reason: "Inventory item was not found." };
|
||||
}
|
||||
|
||||
const location = await prisma.warehouseLocation.findUnique({
|
||||
where: { id: payload.locationId },
|
||||
select: {
|
||||
id: true,
|
||||
warehouseId: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!location || location.warehouseId !== payload.warehouseId) {
|
||||
return { ok: false as const, reason: "Warehouse location is invalid for the selected warehouse." };
|
||||
}
|
||||
|
||||
const detail = await getInventoryItemById(itemId);
|
||||
if (!detail) {
|
||||
return { ok: false as const, reason: "Inventory item was not found." };
|
||||
}
|
||||
|
||||
const signedQuantity = getSignedQuantity(payload.transactionType, payload.quantity);
|
||||
if (signedQuantity < 0 && detail.onHandQuantity + signedQuantity < 0) {
|
||||
return { ok: false as const, reason: "Transaction would drive on-hand quantity below zero." };
|
||||
}
|
||||
|
||||
await prisma.inventoryTransaction.create({
|
||||
data: {
|
||||
itemId,
|
||||
warehouseId: payload.warehouseId,
|
||||
locationId: payload.locationId,
|
||||
transactionType: payload.transactionType,
|
||||
quantity: payload.quantity,
|
||||
reference: payload.reference.trim(),
|
||||
notes: payload.notes,
|
||||
createdById: createdById ?? null,
|
||||
},
|
||||
});
|
||||
|
||||
const nextDetail = await getInventoryItemById(itemId);
|
||||
return nextDetail ? { ok: true as const, item: nextDetail } : { ok: false as const, reason: "Unable to load updated inventory item." };
|
||||
}
|
||||
|
||||
export async function createInventoryItem(payload: InventoryItemInput) {
|
||||
const validatedBom = await validateBomLines(null, payload.bomLines);
|
||||
if (!validatedBom.ok) {
|
||||
@@ -328,23 +524,12 @@ export async function createInventoryItem(payload: InventoryItemInput) {
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
include: {
|
||||
bomLines: {
|
||||
include: {
|
||||
componentItem: {
|
||||
select: {
|
||||
id: true,
|
||||
sku: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: [{ position: "asc" }, { createdAt: "asc" }],
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
|
||||
return mapDetail(item);
|
||||
return getInventoryItemById(item.id);
|
||||
}
|
||||
|
||||
export async function updateInventoryItem(itemId: string, payload: InventoryItemInput) {
|
||||
@@ -379,23 +564,12 @@ export async function updateInventoryItem(itemId: string, payload: InventoryItem
|
||||
create: validatedBom.bomLines,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
bomLines: {
|
||||
include: {
|
||||
componentItem: {
|
||||
select: {
|
||||
id: true,
|
||||
sku: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: [{ position: "asc" }, { createdAt: "asc" }],
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
|
||||
return mapDetail(item);
|
||||
return getInventoryItemById(item.id);
|
||||
}
|
||||
|
||||
export async function listWarehouses() {
|
||||
|
||||
Reference in New Issue
Block a user