2026-03-14 21:10:35 -05:00
|
|
|
import type {
|
|
|
|
|
InventoryBomLineDto,
|
|
|
|
|
InventoryBomLineInput,
|
|
|
|
|
InventoryItemDetailDto,
|
|
|
|
|
InventoryItemInput,
|
2026-03-15 12:11:46 -05:00
|
|
|
InventoryItemOperationDto,
|
2026-03-15 14:00:12 -05:00
|
|
|
InventoryReservationDto,
|
|
|
|
|
InventoryReservationInput,
|
|
|
|
|
InventoryReservationStatus,
|
2026-03-14 22:37:09 -05:00
|
|
|
InventoryStockBalanceDto,
|
2026-03-15 14:00:12 -05:00
|
|
|
InventoryTransferDto,
|
|
|
|
|
InventoryTransferInput,
|
2026-03-14 21:23:22 -05:00
|
|
|
WarehouseDetailDto,
|
|
|
|
|
WarehouseInput,
|
2026-03-14 22:37:09 -05:00
|
|
|
WarehouseLocationOptionDto,
|
2026-03-14 21:23:22 -05:00
|
|
|
WarehouseLocationDto,
|
|
|
|
|
WarehouseLocationInput,
|
|
|
|
|
WarehouseSummaryDto,
|
2026-03-14 21:10:35 -05:00
|
|
|
InventoryItemStatus,
|
|
|
|
|
InventoryItemSummaryDto,
|
2026-03-14 22:37:09 -05:00
|
|
|
InventoryTransactionDto,
|
|
|
|
|
InventoryTransactionInput,
|
|
|
|
|
InventoryTransactionType,
|
2026-03-14 21:10:35 -05:00
|
|
|
InventoryItemType,
|
|
|
|
|
InventoryUnitOfMeasure,
|
|
|
|
|
} from "@mrp/shared/dist/inventory/types.js";
|
|
|
|
|
|
|
|
|
|
import { prisma } from "../../lib/prisma.js";
|
|
|
|
|
|
|
|
|
|
type BomLineRecord = {
|
|
|
|
|
id: string;
|
|
|
|
|
quantity: number;
|
|
|
|
|
unitOfMeasure: string;
|
|
|
|
|
notes: string;
|
|
|
|
|
position: number;
|
|
|
|
|
componentItem: {
|
|
|
|
|
id: string;
|
|
|
|
|
sku: string;
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-15 12:11:46 -05:00
|
|
|
type OperationRecord = {
|
|
|
|
|
id: string;
|
|
|
|
|
setupMinutes: number;
|
|
|
|
|
runMinutesPerUnit: number;
|
|
|
|
|
moveMinutes: number;
|
|
|
|
|
notes: string;
|
|
|
|
|
position: number;
|
|
|
|
|
station: {
|
|
|
|
|
id: string;
|
|
|
|
|
code: string;
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-14 21:10:35 -05:00
|
|
|
type InventoryDetailRecord = {
|
|
|
|
|
id: string;
|
|
|
|
|
sku: string;
|
|
|
|
|
name: string;
|
|
|
|
|
description: string;
|
|
|
|
|
type: string;
|
|
|
|
|
status: string;
|
|
|
|
|
unitOfMeasure: string;
|
|
|
|
|
isSellable: boolean;
|
|
|
|
|
isPurchasable: boolean;
|
|
|
|
|
defaultCost: number | null;
|
2026-03-14 23:23:43 -05:00
|
|
|
defaultPrice: number | null;
|
2026-03-14 21:10:35 -05:00
|
|
|
notes: string;
|
|
|
|
|
createdAt: Date;
|
|
|
|
|
updatedAt: Date;
|
|
|
|
|
bomLines: BomLineRecord[];
|
2026-03-15 12:11:46 -05:00
|
|
|
operations: OperationRecord[];
|
2026-03-14 22:37:09 -05:00
|
|
|
inventoryTransactions: InventoryTransactionRecord[];
|
2026-03-15 14:00:12 -05:00
|
|
|
reservations: InventoryReservationRecord[];
|
|
|
|
|
transfers: InventoryTransferRecord[];
|
2026-03-14 22:37:09 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
2026-03-14 21:10:35 -05:00
|
|
|
};
|
|
|
|
|
|
2026-03-14 21:23:22 -05:00
|
|
|
type WarehouseLocationRecord = {
|
|
|
|
|
id: string;
|
|
|
|
|
code: string;
|
|
|
|
|
name: string;
|
|
|
|
|
notes: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type WarehouseDetailRecord = {
|
|
|
|
|
id: string;
|
|
|
|
|
code: string;
|
|
|
|
|
name: string;
|
|
|
|
|
notes: string;
|
|
|
|
|
createdAt: Date;
|
|
|
|
|
updatedAt: Date;
|
|
|
|
|
locations: WarehouseLocationRecord[];
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-15 14:00:12 -05:00
|
|
|
type InventoryReservationRecord = {
|
|
|
|
|
id: string;
|
|
|
|
|
quantity: number;
|
|
|
|
|
status: string;
|
|
|
|
|
sourceType: string;
|
|
|
|
|
sourceId: string | null;
|
|
|
|
|
notes: string;
|
|
|
|
|
createdAt: Date;
|
|
|
|
|
warehouse: {
|
|
|
|
|
id: string;
|
|
|
|
|
code: string;
|
|
|
|
|
name: string;
|
|
|
|
|
} | null;
|
|
|
|
|
location: {
|
|
|
|
|
id: string;
|
|
|
|
|
code: string;
|
|
|
|
|
name: string;
|
|
|
|
|
} | null;
|
|
|
|
|
workOrder: {
|
|
|
|
|
id: string;
|
|
|
|
|
workOrderNumber: string;
|
|
|
|
|
} | null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type InventoryTransferRecord = {
|
|
|
|
|
id: string;
|
|
|
|
|
quantity: number;
|
|
|
|
|
notes: string;
|
|
|
|
|
createdAt: Date;
|
|
|
|
|
fromWarehouse: {
|
|
|
|
|
id: string;
|
|
|
|
|
code: string;
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
|
|
|
|
fromLocation: {
|
|
|
|
|
id: string;
|
|
|
|
|
code: string;
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
|
|
|
|
toWarehouse: {
|
|
|
|
|
id: string;
|
|
|
|
|
code: string;
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
|
|
|
|
toLocation: {
|
|
|
|
|
id: string;
|
|
|
|
|
code: string;
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
|
|
|
|
createdBy: {
|
|
|
|
|
firstName: string;
|
|
|
|
|
lastName: string;
|
|
|
|
|
} | null;
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-14 21:10:35 -05:00
|
|
|
function mapBomLine(record: BomLineRecord): InventoryBomLineDto {
|
|
|
|
|
return {
|
|
|
|
|
id: record.id,
|
|
|
|
|
componentItemId: record.componentItem.id,
|
|
|
|
|
componentSku: record.componentItem.sku,
|
|
|
|
|
componentName: record.componentItem.name,
|
|
|
|
|
quantity: record.quantity,
|
|
|
|
|
unitOfMeasure: record.unitOfMeasure as InventoryUnitOfMeasure,
|
|
|
|
|
notes: record.notes,
|
|
|
|
|
position: record.position,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 12:11:46 -05:00
|
|
|
function mapOperation(record: OperationRecord): InventoryItemOperationDto {
|
|
|
|
|
return {
|
|
|
|
|
id: record.id,
|
|
|
|
|
stationId: record.station.id,
|
|
|
|
|
stationCode: record.station.code,
|
|
|
|
|
stationName: record.station.name,
|
|
|
|
|
setupMinutes: record.setupMinutes,
|
|
|
|
|
runMinutesPerUnit: record.runMinutesPerUnit,
|
|
|
|
|
moveMinutes: record.moveMinutes,
|
|
|
|
|
estimatedMinutesPerUnit: record.setupMinutes + record.runMinutesPerUnit + record.moveMinutes,
|
|
|
|
|
position: record.position,
|
|
|
|
|
notes: record.notes,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:23:22 -05:00
|
|
|
function mapWarehouseLocation(record: WarehouseLocationRecord): WarehouseLocationDto {
|
|
|
|
|
return {
|
|
|
|
|
id: record.id,
|
|
|
|
|
code: record.code,
|
|
|
|
|
name: record.name,
|
|
|
|
|
notes: record.notes,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 22:37:09 -05:00
|
|
|
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",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 14:00:12 -05:00
|
|
|
function mapReservation(record: InventoryReservationRecord): InventoryReservationDto {
|
|
|
|
|
return {
|
|
|
|
|
id: record.id,
|
|
|
|
|
quantity: record.quantity,
|
|
|
|
|
status: record.status as InventoryReservationStatus,
|
|
|
|
|
sourceType: record.sourceType,
|
|
|
|
|
sourceId: record.sourceId,
|
|
|
|
|
sourceLabel: record.workOrder ? record.workOrder.workOrderNumber : null,
|
|
|
|
|
notes: record.notes,
|
|
|
|
|
createdAt: record.createdAt.toISOString(),
|
|
|
|
|
warehouseId: record.warehouse?.id ?? null,
|
|
|
|
|
warehouseCode: record.warehouse?.code ?? null,
|
|
|
|
|
warehouseName: record.warehouse?.name ?? null,
|
|
|
|
|
locationId: record.location?.id ?? null,
|
|
|
|
|
locationCode: record.location?.code ?? null,
|
|
|
|
|
locationName: record.location?.name ?? null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mapTransfer(record: InventoryTransferRecord): InventoryTransferDto {
|
|
|
|
|
return {
|
|
|
|
|
id: record.id,
|
|
|
|
|
quantity: record.quantity,
|
|
|
|
|
notes: record.notes,
|
|
|
|
|
createdAt: record.createdAt.toISOString(),
|
|
|
|
|
createdByName: record.createdBy ? `${record.createdBy.firstName} ${record.createdBy.lastName}`.trim() : "System",
|
|
|
|
|
fromWarehouseId: record.fromWarehouse.id,
|
|
|
|
|
fromWarehouseCode: record.fromWarehouse.code,
|
|
|
|
|
fromWarehouseName: record.fromWarehouse.name,
|
|
|
|
|
fromLocationId: record.fromLocation.id,
|
|
|
|
|
fromLocationCode: record.fromLocation.code,
|
|
|
|
|
fromLocationName: record.fromLocation.name,
|
|
|
|
|
toWarehouseId: record.toWarehouse.id,
|
|
|
|
|
toWarehouseCode: record.toWarehouse.code,
|
|
|
|
|
toWarehouseName: record.toWarehouse.name,
|
|
|
|
|
toLocationId: record.toLocation.id,
|
|
|
|
|
toLocationCode: record.toLocation.code,
|
|
|
|
|
toLocationName: record.toLocation.name,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildStockBalances(transactions: InventoryTransactionRecord[], reservations: InventoryReservationRecord[]): InventoryStockBalanceDto[] {
|
2026-03-14 22:37:09 -05:00
|
|
|
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,
|
2026-03-15 14:00:12 -05:00
|
|
|
quantityReserved: 0,
|
|
|
|
|
quantityAvailable: signedQuantity,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const reservation of reservations) {
|
|
|
|
|
if (!reservation.warehouse || !reservation.location || reservation.status !== "ACTIVE") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const key = `${reservation.warehouse.id}:${reservation.location.id}`;
|
|
|
|
|
const current = grouped.get(key);
|
|
|
|
|
|
|
|
|
|
if (current) {
|
|
|
|
|
current.quantityReserved += reservation.quantity;
|
|
|
|
|
current.quantityAvailable = current.quantityOnHand - current.quantityReserved;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
grouped.set(key, {
|
|
|
|
|
warehouseId: reservation.warehouse.id,
|
|
|
|
|
warehouseCode: reservation.warehouse.code,
|
|
|
|
|
warehouseName: reservation.warehouse.name,
|
|
|
|
|
locationId: reservation.location.id,
|
|
|
|
|
locationCode: reservation.location.code,
|
|
|
|
|
locationName: reservation.location.name,
|
|
|
|
|
quantityOnHand: 0,
|
|
|
|
|
quantityReserved: reservation.quantity,
|
|
|
|
|
quantityAvailable: -reservation.quantity,
|
2026-03-14 22:37:09 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [...grouped.values()]
|
2026-03-15 14:00:12 -05:00
|
|
|
.filter((balance) => balance.quantityOnHand !== 0 || balance.quantityReserved !== 0)
|
2026-03-14 22:37:09 -05:00
|
|
|
.sort((left, right) =>
|
|
|
|
|
`${left.warehouseCode}-${left.locationCode}`.localeCompare(`${right.warehouseCode}-${right.locationCode}`)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:10:35 -05:00
|
|
|
function mapSummary(record: {
|
|
|
|
|
id: string;
|
|
|
|
|
sku: string;
|
|
|
|
|
name: string;
|
|
|
|
|
type: string;
|
|
|
|
|
status: string;
|
|
|
|
|
unitOfMeasure: string;
|
|
|
|
|
isSellable: boolean;
|
|
|
|
|
isPurchasable: boolean;
|
|
|
|
|
updatedAt: Date;
|
|
|
|
|
_count: { bomLines: number };
|
|
|
|
|
}): InventoryItemSummaryDto {
|
|
|
|
|
return {
|
|
|
|
|
id: record.id,
|
|
|
|
|
sku: record.sku,
|
|
|
|
|
name: record.name,
|
|
|
|
|
type: record.type as InventoryItemType,
|
|
|
|
|
status: record.status as InventoryItemStatus,
|
|
|
|
|
unitOfMeasure: record.unitOfMeasure as InventoryUnitOfMeasure,
|
|
|
|
|
isSellable: record.isSellable,
|
|
|
|
|
isPurchasable: record.isPurchasable,
|
|
|
|
|
bomLineCount: record._count.bomLines,
|
|
|
|
|
updatedAt: record.updatedAt.toISOString(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mapDetail(record: InventoryDetailRecord): InventoryItemDetailDto {
|
2026-03-14 22:37:09 -05:00
|
|
|
const recentTransactions = record.inventoryTransactions
|
|
|
|
|
.slice()
|
|
|
|
|
.sort((left, right) => right.createdAt.getTime() - left.createdAt.getTime())
|
|
|
|
|
.map(mapInventoryTransaction);
|
2026-03-15 14:00:12 -05:00
|
|
|
const activeReservations = record.reservations.filter((reservation) => reservation.status === "ACTIVE");
|
|
|
|
|
const stockBalances = buildStockBalances(record.inventoryTransactions, activeReservations);
|
|
|
|
|
const reservedQuantity = activeReservations.reduce((sum, reservation) => sum + reservation.quantity, 0);
|
|
|
|
|
const transferHistory = record.transfers
|
|
|
|
|
.slice()
|
|
|
|
|
.sort((left, right) => right.createdAt.getTime() - left.createdAt.getTime())
|
|
|
|
|
.map(mapTransfer);
|
2026-03-14 22:37:09 -05:00
|
|
|
|
2026-03-14 21:10:35 -05:00
|
|
|
return {
|
|
|
|
|
...mapSummary({
|
|
|
|
|
id: record.id,
|
|
|
|
|
sku: record.sku,
|
|
|
|
|
name: record.name,
|
|
|
|
|
type: record.type,
|
|
|
|
|
status: record.status,
|
|
|
|
|
unitOfMeasure: record.unitOfMeasure,
|
|
|
|
|
isSellable: record.isSellable,
|
|
|
|
|
isPurchasable: record.isPurchasable,
|
|
|
|
|
updatedAt: record.updatedAt,
|
|
|
|
|
_count: { bomLines: record.bomLines.length },
|
|
|
|
|
}),
|
|
|
|
|
description: record.description,
|
|
|
|
|
defaultCost: record.defaultCost,
|
2026-03-14 23:23:43 -05:00
|
|
|
defaultPrice: record.defaultPrice,
|
2026-03-14 21:10:35 -05:00
|
|
|
notes: record.notes,
|
|
|
|
|
createdAt: record.createdAt.toISOString(),
|
|
|
|
|
bomLines: record.bomLines.slice().sort((a, b) => a.position - b.position).map(mapBomLine),
|
2026-03-15 12:11:46 -05:00
|
|
|
operations: record.operations.slice().sort((a, b) => a.position - b.position).map(mapOperation),
|
2026-03-14 22:37:09 -05:00
|
|
|
onHandQuantity: stockBalances.reduce((sum, balance) => sum + balance.quantityOnHand, 0),
|
2026-03-15 14:00:12 -05:00
|
|
|
reservedQuantity,
|
|
|
|
|
availableQuantity: stockBalances.reduce((sum, balance) => sum + balance.quantityAvailable, 0),
|
2026-03-14 22:37:09 -05:00
|
|
|
stockBalances,
|
|
|
|
|
recentTransactions,
|
2026-03-15 14:00:12 -05:00
|
|
|
transfers: transferHistory,
|
|
|
|
|
reservations: record.reservations.slice().sort((left, right) => right.createdAt.getTime() - left.createdAt.getTime()).map(mapReservation),
|
2026-03-14 21:10:35 -05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:23:22 -05:00
|
|
|
function mapWarehouseSummary(record: {
|
|
|
|
|
id: string;
|
|
|
|
|
code: string;
|
|
|
|
|
name: string;
|
|
|
|
|
updatedAt: Date;
|
|
|
|
|
_count: { locations: number };
|
|
|
|
|
}): WarehouseSummaryDto {
|
|
|
|
|
return {
|
|
|
|
|
id: record.id,
|
|
|
|
|
code: record.code,
|
|
|
|
|
name: record.name,
|
|
|
|
|
locationCount: record._count.locations,
|
|
|
|
|
updatedAt: record.updatedAt.toISOString(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mapWarehouseDetail(record: WarehouseDetailRecord): WarehouseDetailDto {
|
|
|
|
|
return {
|
|
|
|
|
...mapWarehouseSummary({
|
|
|
|
|
id: record.id,
|
|
|
|
|
code: record.code,
|
|
|
|
|
name: record.name,
|
|
|
|
|
updatedAt: record.updatedAt,
|
|
|
|
|
_count: { locations: record.locations.length },
|
|
|
|
|
}),
|
|
|
|
|
notes: record.notes,
|
|
|
|
|
createdAt: record.createdAt.toISOString(),
|
|
|
|
|
locations: record.locations.map(mapWarehouseLocation),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:10:35 -05:00
|
|
|
interface InventoryListFilters {
|
|
|
|
|
query?: string;
|
|
|
|
|
status?: InventoryItemStatus;
|
|
|
|
|
type?: InventoryItemType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildWhereClause(filters: InventoryListFilters) {
|
|
|
|
|
const trimmedQuery = filters.query?.trim();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...(filters.status ? { status: filters.status } : {}),
|
|
|
|
|
...(filters.type ? { type: filters.type } : {}),
|
|
|
|
|
...(trimmedQuery
|
|
|
|
|
? {
|
|
|
|
|
OR: [
|
|
|
|
|
{ sku: { contains: trimmedQuery } },
|
|
|
|
|
{ name: { contains: trimmedQuery } },
|
|
|
|
|
{ description: { contains: trimmedQuery } },
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function normalizeBomLines(bomLines: InventoryBomLineInput[]) {
|
|
|
|
|
return bomLines
|
|
|
|
|
.map((line, index) => ({
|
|
|
|
|
componentItemId: line.componentItemId,
|
2026-03-14 22:37:09 -05:00
|
|
|
quantity: Number(line.quantity),
|
2026-03-14 21:10:35 -05:00
|
|
|
unitOfMeasure: line.unitOfMeasure,
|
|
|
|
|
notes: line.notes,
|
|
|
|
|
position: line.position ?? (index + 1) * 10,
|
|
|
|
|
}))
|
|
|
|
|
.filter((line) => line.componentItemId.trim().length > 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 12:11:46 -05:00
|
|
|
function normalizeOperations(operations: InventoryItemInput["operations"]) {
|
|
|
|
|
return operations
|
|
|
|
|
.map((operation, index) => ({
|
|
|
|
|
stationId: operation.stationId,
|
|
|
|
|
setupMinutes: Number(operation.setupMinutes),
|
|
|
|
|
runMinutesPerUnit: Number(operation.runMinutesPerUnit),
|
|
|
|
|
moveMinutes: Number(operation.moveMinutes),
|
|
|
|
|
notes: operation.notes,
|
|
|
|
|
position: operation.position ?? (index + 1) * 10,
|
|
|
|
|
}))
|
|
|
|
|
.filter((operation) => operation.stationId.trim().length > 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:23:22 -05:00
|
|
|
function normalizeWarehouseLocations(locations: WarehouseLocationInput[]) {
|
|
|
|
|
return locations
|
|
|
|
|
.map((location) => ({
|
|
|
|
|
code: location.code.trim(),
|
|
|
|
|
name: location.name.trim(),
|
|
|
|
|
notes: location.notes,
|
|
|
|
|
}))
|
|
|
|
|
.filter((location) => location.code.length > 0 && location.name.length > 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 14:00:12 -05:00
|
|
|
async function getItemLocationOnHand(itemId: string, warehouseId: string, locationId: string) {
|
|
|
|
|
const transactions = await prisma.inventoryTransaction.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
itemId,
|
|
|
|
|
warehouseId,
|
|
|
|
|
locationId,
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
transactionType: true,
|
|
|
|
|
quantity: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return transactions.reduce((total, transaction) => {
|
|
|
|
|
return total + (transaction.transactionType === "RECEIPT" || transaction.transactionType === "ADJUSTMENT_IN" ? transaction.quantity : -transaction.quantity);
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:10:35 -05:00
|
|
|
async function validateBomLines(parentItemId: string | null, bomLines: InventoryBomLineInput[]) {
|
|
|
|
|
const normalized = normalizeBomLines(bomLines);
|
|
|
|
|
|
|
|
|
|
if (normalized.some((line) => line.quantity <= 0)) {
|
|
|
|
|
return { ok: false as const, reason: "BOM line quantity must be greater than zero." };
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 22:37:09 -05:00
|
|
|
if (normalized.some((line) => !Number.isInteger(line.quantity))) {
|
|
|
|
|
return { ok: false as const, reason: "BOM line quantity must be a whole number." };
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:10:35 -05:00
|
|
|
if (parentItemId && normalized.some((line) => line.componentItemId === parentItemId)) {
|
|
|
|
|
return { ok: false as const, reason: "An item cannot reference itself in its BOM." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const componentIds = [...new Set(normalized.map((line) => line.componentItemId))];
|
|
|
|
|
if (componentIds.length === 0) {
|
|
|
|
|
return { ok: true as const, bomLines: normalized };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const existingComponents = await prisma.inventoryItem.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
id: {
|
|
|
|
|
in: componentIds,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (existingComponents.length !== componentIds.length) {
|
|
|
|
|
return { ok: false as const, reason: "One or more BOM components do not exist." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { ok: true as const, bomLines: normalized };
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 12:11:46 -05:00
|
|
|
async function validateOperations(type: InventoryItemType, operations: InventoryItemInput["operations"]) {
|
|
|
|
|
const normalized = normalizeOperations(operations);
|
|
|
|
|
|
|
|
|
|
if (type === "ASSEMBLY" || type === "MANUFACTURED") {
|
|
|
|
|
if (normalized.length === 0) {
|
|
|
|
|
return { ok: false as const, reason: "Assembly and manufactured items require at least one station operation." };
|
|
|
|
|
}
|
|
|
|
|
} else if (normalized.length > 0) {
|
|
|
|
|
return { ok: false as const, reason: "Only assembly and manufactured items may define station operations." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (normalized.some((operation) => operation.setupMinutes < 0 || operation.runMinutesPerUnit < 0 || operation.moveMinutes < 0)) {
|
|
|
|
|
return { ok: false as const, reason: "Operation times must be zero or greater." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (normalized.some((operation) => operation.setupMinutes + operation.runMinutesPerUnit + operation.moveMinutes <= 0)) {
|
|
|
|
|
return { ok: false as const, reason: "Each operation must have at least some planned time." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stationIds = [...new Set(normalized.map((operation) => operation.stationId))];
|
|
|
|
|
if (stationIds.length === 0) {
|
|
|
|
|
return { ok: true as const, operations: normalized };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const existingStations = await prisma.manufacturingStation.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
id: {
|
|
|
|
|
in: stationIds,
|
|
|
|
|
},
|
|
|
|
|
isActive: true,
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (existingStations.length !== stationIds.length) {
|
|
|
|
|
return { ok: false as const, reason: "One or more selected stations do not exist or are inactive." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { ok: true as const, operations: normalized };
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 14:00:12 -05:00
|
|
|
async function getActiveReservedQuantity(itemId: string, warehouseId: string, locationId: string) {
|
|
|
|
|
const reservations = await prisma.inventoryReservation.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
itemId,
|
|
|
|
|
warehouseId,
|
|
|
|
|
locationId,
|
|
|
|
|
status: "ACTIVE",
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
quantity: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return reservations.reduce((sum, reservation) => sum + reservation.quantity, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:10:35 -05:00
|
|
|
export async function listInventoryItems(filters: InventoryListFilters = {}) {
|
|
|
|
|
const items = await prisma.inventoryItem.findMany({
|
|
|
|
|
where: buildWhereClause(filters),
|
|
|
|
|
include: {
|
|
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
bomLines: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
orderBy: [{ sku: "asc" }],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return items.map(mapSummary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function listInventoryItemOptions() {
|
|
|
|
|
const items = await prisma.inventoryItem.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
status: {
|
|
|
|
|
not: "OBSOLETE",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
sku: true,
|
|
|
|
|
name: true,
|
2026-03-15 00:47:16 -05:00
|
|
|
isPurchasable: true,
|
2026-03-14 23:23:43 -05:00
|
|
|
defaultPrice: true,
|
2026-03-14 21:10:35 -05:00
|
|
|
},
|
|
|
|
|
orderBy: [{ sku: "asc" }],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return items.map((item) => ({
|
|
|
|
|
id: item.id,
|
|
|
|
|
sku: item.sku,
|
|
|
|
|
name: item.name,
|
2026-03-15 00:47:16 -05:00
|
|
|
isPurchasable: item.isPurchasable,
|
2026-03-14 23:23:43 -05:00
|
|
|
defaultPrice: item.defaultPrice,
|
2026-03-14 21:10:35 -05:00
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getInventoryItemById(itemId: string) {
|
|
|
|
|
const item = await prisma.inventoryItem.findUnique({
|
|
|
|
|
where: { id: itemId },
|
|
|
|
|
include: {
|
|
|
|
|
bomLines: {
|
|
|
|
|
include: {
|
|
|
|
|
componentItem: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
sku: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
orderBy: [{ position: "asc" }, { createdAt: "asc" }],
|
|
|
|
|
},
|
2026-03-15 12:11:46 -05:00
|
|
|
operations: {
|
|
|
|
|
include: {
|
|
|
|
|
station: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
code: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
orderBy: [{ position: "asc" }, { createdAt: "asc" }],
|
|
|
|
|
},
|
2026-03-14 22:37:09 -05:00
|
|
|
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" }],
|
|
|
|
|
},
|
2026-03-15 14:00:12 -05:00
|
|
|
reservations: {
|
|
|
|
|
include: {
|
|
|
|
|
warehouse: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
code: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
location: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
code: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
workOrder: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
workOrderNumber: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
orderBy: [{ createdAt: "desc" }],
|
|
|
|
|
},
|
|
|
|
|
transfers: {
|
|
|
|
|
include: {
|
|
|
|
|
fromWarehouse: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
code: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
fromLocation: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
code: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
toWarehouse: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
code: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
toLocation: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
code: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
createdBy: {
|
|
|
|
|
select: {
|
|
|
|
|
firstName: true,
|
|
|
|
|
lastName: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
orderBy: [{ createdAt: "desc" }],
|
|
|
|
|
},
|
2026-03-14 21:10:35 -05:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return item ? mapDetail(item) : null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 22:37:09 -05:00
|
|
|
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 signedQuantity = getSignedQuantity(payload.transactionType, payload.quantity);
|
2026-03-15 14:00:12 -05:00
|
|
|
if (signedQuantity < 0) {
|
|
|
|
|
const onHand = await getItemLocationOnHand(itemId, payload.warehouseId, payload.locationId);
|
|
|
|
|
const reserved = await getActiveReservedQuantity(itemId, payload.warehouseId, payload.locationId);
|
|
|
|
|
if (onHand - reserved + signedQuantity < 0) {
|
|
|
|
|
return { ok: false as const, reason: "Transaction would drive available quantity below zero at the selected location." };
|
|
|
|
|
}
|
2026-03-14 22:37:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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." };
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 14:00:12 -05:00
|
|
|
export async function createInventoryTransfer(itemId: string, payload: InventoryTransferInput, 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 [fromLocation, toLocation] = await Promise.all([
|
|
|
|
|
prisma.warehouseLocation.findUnique({
|
|
|
|
|
where: { id: payload.fromLocationId },
|
|
|
|
|
select: { id: true, warehouseId: true },
|
|
|
|
|
}),
|
|
|
|
|
prisma.warehouseLocation.findUnique({
|
|
|
|
|
where: { id: payload.toLocationId },
|
|
|
|
|
select: { id: true, warehouseId: true },
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!fromLocation || fromLocation.warehouseId !== payload.fromWarehouseId) {
|
|
|
|
|
return { ok: false as const, reason: "Source location is invalid for the selected source warehouse." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!toLocation || toLocation.warehouseId !== payload.toWarehouseId) {
|
|
|
|
|
return { ok: false as const, reason: "Destination location is invalid for the selected destination warehouse." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onHand = await getItemLocationOnHand(itemId, payload.fromWarehouseId, payload.fromLocationId);
|
|
|
|
|
const reserved = await getActiveReservedQuantity(itemId, payload.fromWarehouseId, payload.fromLocationId);
|
|
|
|
|
if (onHand - reserved < payload.quantity) {
|
|
|
|
|
return { ok: false as const, reason: "Transfer quantity exceeds available stock at the source location." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await prisma.$transaction(async (tx) => {
|
|
|
|
|
await tx.inventoryTransfer.create({
|
|
|
|
|
data: {
|
|
|
|
|
itemId,
|
|
|
|
|
fromWarehouseId: payload.fromWarehouseId,
|
|
|
|
|
fromLocationId: payload.fromLocationId,
|
|
|
|
|
toWarehouseId: payload.toWarehouseId,
|
|
|
|
|
toLocationId: payload.toLocationId,
|
|
|
|
|
quantity: payload.quantity,
|
|
|
|
|
notes: payload.notes,
|
|
|
|
|
createdById: createdById ?? null,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await tx.inventoryTransaction.create({
|
|
|
|
|
data: {
|
|
|
|
|
itemId,
|
|
|
|
|
warehouseId: payload.fromWarehouseId,
|
|
|
|
|
locationId: payload.fromLocationId,
|
|
|
|
|
transactionType: "ISSUE",
|
|
|
|
|
quantity: payload.quantity,
|
|
|
|
|
reference: "Inventory transfer out",
|
|
|
|
|
notes: payload.notes,
|
|
|
|
|
createdById: createdById ?? null,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await tx.inventoryTransaction.create({
|
|
|
|
|
data: {
|
|
|
|
|
itemId,
|
|
|
|
|
warehouseId: payload.toWarehouseId,
|
|
|
|
|
locationId: payload.toLocationId,
|
|
|
|
|
transactionType: "RECEIPT",
|
|
|
|
|
quantity: payload.quantity,
|
|
|
|
|
reference: "Inventory transfer in",
|
|
|
|
|
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 createInventoryReservation(itemId: string, payload: InventoryReservationInput) {
|
|
|
|
|
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." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((payload.warehouseId && !payload.locationId) || (!payload.warehouseId && payload.locationId)) {
|
|
|
|
|
return { ok: false as const, reason: "Reservation warehouse and location must be provided together." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (payload.warehouseId && payload.locationId) {
|
|
|
|
|
const location = await prisma.warehouseLocation.findUnique({
|
|
|
|
|
where: { id: payload.locationId },
|
|
|
|
|
select: { warehouseId: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!location || location.warehouseId !== payload.warehouseId) {
|
|
|
|
|
return { ok: false as const, reason: "Reservation location is invalid for the selected warehouse." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onHand = await getItemLocationOnHand(itemId, payload.warehouseId, payload.locationId);
|
|
|
|
|
const reserved = await getActiveReservedQuantity(itemId, payload.warehouseId, payload.locationId);
|
|
|
|
|
if (onHand - reserved < payload.quantity) {
|
|
|
|
|
return { ok: false as const, reason: "Reservation quantity exceeds available stock at the selected location." };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await prisma.inventoryReservation.create({
|
|
|
|
|
data: {
|
|
|
|
|
itemId,
|
|
|
|
|
warehouseId: payload.warehouseId,
|
|
|
|
|
locationId: payload.locationId,
|
|
|
|
|
sourceType: "MANUAL",
|
|
|
|
|
sourceId: null,
|
|
|
|
|
quantity: payload.quantity,
|
|
|
|
|
status: "ACTIVE",
|
|
|
|
|
notes: payload.notes,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const nextDetail = await getInventoryItemById(itemId);
|
|
|
|
|
return nextDetail ? { ok: true as const, item: nextDetail } : { ok: false as const, reason: "Unable to load updated inventory item." };
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:10:35 -05:00
|
|
|
export async function createInventoryItem(payload: InventoryItemInput) {
|
|
|
|
|
const validatedBom = await validateBomLines(null, payload.bomLines);
|
|
|
|
|
if (!validatedBom.ok) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-03-15 12:11:46 -05:00
|
|
|
const validatedOperations = await validateOperations(payload.type, payload.operations);
|
|
|
|
|
if (!validatedOperations.ok) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-03-14 21:10:35 -05:00
|
|
|
|
|
|
|
|
const item = await prisma.inventoryItem.create({
|
|
|
|
|
data: {
|
|
|
|
|
sku: payload.sku,
|
|
|
|
|
name: payload.name,
|
|
|
|
|
description: payload.description,
|
|
|
|
|
type: payload.type,
|
|
|
|
|
status: payload.status,
|
|
|
|
|
unitOfMeasure: payload.unitOfMeasure,
|
|
|
|
|
isSellable: payload.isSellable,
|
|
|
|
|
isPurchasable: payload.isPurchasable,
|
|
|
|
|
defaultCost: payload.defaultCost,
|
2026-03-14 23:23:43 -05:00
|
|
|
defaultPrice: payload.defaultPrice,
|
2026-03-14 21:10:35 -05:00
|
|
|
notes: payload.notes,
|
|
|
|
|
bomLines: validatedBom.bomLines.length
|
|
|
|
|
? {
|
|
|
|
|
create: validatedBom.bomLines,
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
2026-03-15 12:11:46 -05:00
|
|
|
operations: validatedOperations.operations.length
|
|
|
|
|
? {
|
|
|
|
|
create: validatedOperations.operations,
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
2026-03-14 21:10:35 -05:00
|
|
|
},
|
2026-03-14 22:37:09 -05:00
|
|
|
select: {
|
|
|
|
|
id: true,
|
2026-03-14 21:10:35 -05:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-14 22:37:09 -05:00
|
|
|
return getInventoryItemById(item.id);
|
2026-03-14 21:10:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function updateInventoryItem(itemId: string, payload: InventoryItemInput) {
|
|
|
|
|
const existingItem = await prisma.inventoryItem.findUnique({
|
|
|
|
|
where: { id: itemId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!existingItem) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const validatedBom = await validateBomLines(itemId, payload.bomLines);
|
|
|
|
|
if (!validatedBom.ok) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-03-15 12:11:46 -05:00
|
|
|
const validatedOperations = await validateOperations(payload.type, payload.operations);
|
|
|
|
|
if (!validatedOperations.ok) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-03-14 21:10:35 -05:00
|
|
|
|
|
|
|
|
const item = await prisma.inventoryItem.update({
|
|
|
|
|
where: { id: itemId },
|
|
|
|
|
data: {
|
|
|
|
|
sku: payload.sku,
|
|
|
|
|
name: payload.name,
|
|
|
|
|
description: payload.description,
|
|
|
|
|
type: payload.type,
|
|
|
|
|
status: payload.status,
|
|
|
|
|
unitOfMeasure: payload.unitOfMeasure,
|
|
|
|
|
isSellable: payload.isSellable,
|
|
|
|
|
isPurchasable: payload.isPurchasable,
|
|
|
|
|
defaultCost: payload.defaultCost,
|
2026-03-14 23:23:43 -05:00
|
|
|
defaultPrice: payload.defaultPrice,
|
2026-03-14 21:10:35 -05:00
|
|
|
notes: payload.notes,
|
|
|
|
|
bomLines: {
|
|
|
|
|
deleteMany: {},
|
|
|
|
|
create: validatedBom.bomLines,
|
|
|
|
|
},
|
2026-03-15 12:11:46 -05:00
|
|
|
operations: {
|
|
|
|
|
deleteMany: {},
|
|
|
|
|
create: validatedOperations.operations,
|
|
|
|
|
},
|
2026-03-14 21:10:35 -05:00
|
|
|
},
|
2026-03-14 22:37:09 -05:00
|
|
|
select: {
|
|
|
|
|
id: true,
|
2026-03-14 21:10:35 -05:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-14 22:37:09 -05:00
|
|
|
return getInventoryItemById(item.id);
|
2026-03-14 21:10:35 -05:00
|
|
|
}
|
2026-03-14 21:23:22 -05:00
|
|
|
|
|
|
|
|
export async function listWarehouses() {
|
|
|
|
|
const warehouses = await prisma.warehouse.findMany({
|
|
|
|
|
include: {
|
|
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
locations: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
orderBy: [{ code: "asc" }],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return warehouses.map(mapWarehouseSummary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getWarehouseById(warehouseId: string) {
|
|
|
|
|
const warehouse = await prisma.warehouse.findUnique({
|
|
|
|
|
where: { id: warehouseId },
|
|
|
|
|
include: {
|
|
|
|
|
locations: {
|
|
|
|
|
orderBy: [{ code: "asc" }],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return warehouse ? mapWarehouseDetail(warehouse) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createWarehouse(payload: WarehouseInput) {
|
|
|
|
|
const locations = normalizeWarehouseLocations(payload.locations);
|
|
|
|
|
|
|
|
|
|
const warehouse = await prisma.warehouse.create({
|
|
|
|
|
data: {
|
|
|
|
|
code: payload.code.trim(),
|
|
|
|
|
name: payload.name.trim(),
|
|
|
|
|
notes: payload.notes,
|
|
|
|
|
locations: locations.length
|
|
|
|
|
? {
|
|
|
|
|
create: locations,
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
locations: {
|
|
|
|
|
orderBy: [{ code: "asc" }],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return mapWarehouseDetail(warehouse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function updateWarehouse(warehouseId: string, payload: WarehouseInput) {
|
|
|
|
|
const existingWarehouse = await prisma.warehouse.findUnique({
|
|
|
|
|
where: { id: warehouseId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!existingWarehouse) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const locations = normalizeWarehouseLocations(payload.locations);
|
|
|
|
|
|
|
|
|
|
const warehouse = await prisma.warehouse.update({
|
|
|
|
|
where: { id: warehouseId },
|
|
|
|
|
data: {
|
|
|
|
|
code: payload.code.trim(),
|
|
|
|
|
name: payload.name.trim(),
|
|
|
|
|
notes: payload.notes,
|
|
|
|
|
locations: {
|
|
|
|
|
deleteMany: {},
|
|
|
|
|
create: locations,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
locations: {
|
|
|
|
|
orderBy: [{ code: "asc" }],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return mapWarehouseDetail(warehouse);
|
|
|
|
|
}
|