manufacturing and gantt
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import type {
|
||||
ManufacturingStationDto,
|
||||
ManufacturingStationInput,
|
||||
ManufacturingItemOptionDto,
|
||||
ManufacturingProjectOptionDto,
|
||||
WorkOrderCompletionInput,
|
||||
WorkOrderDetailDto,
|
||||
WorkOrderInput,
|
||||
WorkOrderOperationDto,
|
||||
WorkOrderMaterialIssueInput,
|
||||
WorkOrderStatus,
|
||||
WorkOrderSummaryDto,
|
||||
@@ -13,6 +16,17 @@ import { prisma } from "../../lib/prisma.js";
|
||||
|
||||
const workOrderModel = (prisma as any).workOrder;
|
||||
|
||||
type StationRecord = {
|
||||
id: string;
|
||||
code: string;
|
||||
name: string;
|
||||
description: string;
|
||||
queueDays: number;
|
||||
isActive: boolean;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
type WorkOrderRecord = {
|
||||
id: string;
|
||||
workOrderNumber: string;
|
||||
@@ -29,6 +43,19 @@ type WorkOrderRecord = {
|
||||
name: string;
|
||||
type: string;
|
||||
unitOfMeasure: string;
|
||||
operations: Array<{
|
||||
setupMinutes: number;
|
||||
runMinutesPerUnit: number;
|
||||
moveMinutes: number;
|
||||
position: number;
|
||||
notes: string;
|
||||
station: {
|
||||
id: string;
|
||||
code: string;
|
||||
name: string;
|
||||
queueDays: number;
|
||||
};
|
||||
}>;
|
||||
bomLines: Array<{
|
||||
quantity: number;
|
||||
unitOfMeasure: string;
|
||||
@@ -57,6 +84,22 @@ type WorkOrderRecord = {
|
||||
code: string;
|
||||
name: string;
|
||||
};
|
||||
operations: Array<{
|
||||
id: string;
|
||||
sequence: number;
|
||||
setupMinutes: number;
|
||||
runMinutesPerUnit: number;
|
||||
moveMinutes: number;
|
||||
plannedMinutes: number;
|
||||
plannedStart: Date;
|
||||
plannedEnd: Date;
|
||||
notes: string;
|
||||
station: {
|
||||
id: string;
|
||||
code: string;
|
||||
name: string;
|
||||
};
|
||||
}>;
|
||||
materialIssues: Array<{
|
||||
id: string;
|
||||
quantity: number;
|
||||
@@ -94,10 +137,36 @@ type WorkOrderRecord = {
|
||||
}>;
|
||||
};
|
||||
|
||||
function mapStation(record: StationRecord): ManufacturingStationDto {
|
||||
return {
|
||||
id: record.id,
|
||||
code: record.code,
|
||||
name: record.name,
|
||||
description: record.description,
|
||||
queueDays: record.queueDays,
|
||||
isActive: record.isActive,
|
||||
createdAt: record.createdAt.toISOString(),
|
||||
updatedAt: record.updatedAt.toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
function buildInclude() {
|
||||
return {
|
||||
item: {
|
||||
include: {
|
||||
operations: {
|
||||
include: {
|
||||
station: {
|
||||
select: {
|
||||
id: true,
|
||||
code: true,
|
||||
name: true,
|
||||
queueDays: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: [{ position: "asc" }, { createdAt: "asc" }],
|
||||
},
|
||||
bomLines: {
|
||||
include: {
|
||||
componentItem: {
|
||||
@@ -135,6 +204,18 @@ function buildInclude() {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
operations: {
|
||||
include: {
|
||||
station: {
|
||||
select: {
|
||||
id: true,
|
||||
code: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: [{ sequence: "asc" }],
|
||||
},
|
||||
materialIssues: {
|
||||
include: {
|
||||
componentItem: {
|
||||
@@ -205,6 +286,8 @@ function mapSummary(record: WorkOrderRecord): WorkOrderSummaryDto {
|
||||
locationId: record.location.id,
|
||||
locationCode: record.location.code,
|
||||
locationName: record.location.name,
|
||||
operationCount: record.operations.length,
|
||||
totalPlannedMinutes: record.operations.reduce((sum, operation) => sum + operation.plannedMinutes, 0),
|
||||
updatedAt: record.updatedAt.toISOString(),
|
||||
};
|
||||
}
|
||||
@@ -224,6 +307,20 @@ function mapDetail(record: WorkOrderRecord): WorkOrderDetailDto {
|
||||
itemUnitOfMeasure: record.item.unitOfMeasure,
|
||||
projectCustomerName: record.project?.customer.name ?? null,
|
||||
dueQuantity: record.quantity - record.completedQuantity,
|
||||
operations: record.operations.map((operation): WorkOrderOperationDto => ({
|
||||
id: operation.id,
|
||||
stationId: operation.station.id,
|
||||
stationCode: operation.station.code,
|
||||
stationName: operation.station.name,
|
||||
sequence: operation.sequence,
|
||||
setupMinutes: operation.setupMinutes,
|
||||
runMinutesPerUnit: operation.runMinutesPerUnit,
|
||||
moveMinutes: operation.moveMinutes,
|
||||
plannedMinutes: operation.plannedMinutes,
|
||||
plannedStart: operation.plannedStart.toISOString(),
|
||||
plannedEnd: operation.plannedEnd.toISOString(),
|
||||
notes: operation.notes,
|
||||
})),
|
||||
materialRequirements: record.item.bomLines.map((line) => {
|
||||
const requiredQuantity = line.quantity * record.quantity;
|
||||
const issuedQuantity = issuedByComponent.get(line.componentItem.id) ?? 0;
|
||||
@@ -265,6 +362,107 @@ function mapDetail(record: WorkOrderRecord): WorkOrderDetailDto {
|
||||
};
|
||||
}
|
||||
|
||||
function addMinutes(value: Date, minutes: number) {
|
||||
return new Date(value.getTime() + minutes * 60 * 1000);
|
||||
}
|
||||
|
||||
function buildWorkOrderOperationPlan(
|
||||
itemOperations: WorkOrderRecord["item"]["operations"],
|
||||
quantity: number,
|
||||
dueDate: Date | null,
|
||||
fallbackStart: Date
|
||||
) {
|
||||
if (itemOperations.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const operationDurations = itemOperations.map((operation) => {
|
||||
const plannedMinutes = Math.max(operation.setupMinutes + operation.runMinutesPerUnit * quantity + operation.moveMinutes + operation.station.queueDays * 8 * 60, 1);
|
||||
return {
|
||||
stationId: operation.station.id,
|
||||
sequence: operation.position,
|
||||
setupMinutes: operation.setupMinutes,
|
||||
runMinutesPerUnit: operation.runMinutesPerUnit,
|
||||
moveMinutes: operation.moveMinutes,
|
||||
plannedMinutes,
|
||||
notes: operation.notes,
|
||||
};
|
||||
});
|
||||
|
||||
if (dueDate) {
|
||||
let nextEnd = new Date(dueDate);
|
||||
return operationDurations
|
||||
.slice()
|
||||
.sort((left, right) => right.sequence - left.sequence)
|
||||
.map((operation) => {
|
||||
const plannedStart = addMinutes(nextEnd, -operation.plannedMinutes);
|
||||
const planned = {
|
||||
...operation,
|
||||
plannedStart,
|
||||
plannedEnd: nextEnd,
|
||||
};
|
||||
nextEnd = plannedStart;
|
||||
return planned;
|
||||
})
|
||||
.reverse();
|
||||
}
|
||||
|
||||
let nextStart = new Date(fallbackStart);
|
||||
return operationDurations.map((operation) => {
|
||||
const plannedEnd = addMinutes(nextStart, operation.plannedMinutes);
|
||||
const planned = {
|
||||
...operation,
|
||||
plannedStart: nextStart,
|
||||
plannedEnd,
|
||||
};
|
||||
nextStart = plannedEnd;
|
||||
return planned;
|
||||
});
|
||||
}
|
||||
|
||||
async function regenerateWorkOrderOperations(workOrderId: string) {
|
||||
const workOrder = await workOrderModel.findUnique({
|
||||
where: { id: workOrderId },
|
||||
include: buildInclude(),
|
||||
});
|
||||
|
||||
if (!workOrder) {
|
||||
return;
|
||||
}
|
||||
|
||||
const plan = buildWorkOrderOperationPlan(
|
||||
(workOrder as WorkOrderRecord).item.operations,
|
||||
workOrder.quantity,
|
||||
workOrder.dueDate,
|
||||
workOrder.createdAt
|
||||
);
|
||||
|
||||
await prisma.workOrderOperation.deleteMany({
|
||||
where: {
|
||||
workOrderId,
|
||||
},
|
||||
});
|
||||
|
||||
if (plan.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
await prisma.workOrderOperation.createMany({
|
||||
data: plan.map((operation) => ({
|
||||
workOrderId,
|
||||
stationId: operation.stationId,
|
||||
sequence: operation.sequence,
|
||||
setupMinutes: operation.setupMinutes,
|
||||
runMinutesPerUnit: operation.runMinutesPerUnit,
|
||||
moveMinutes: operation.moveMinutes,
|
||||
plannedMinutes: operation.plannedMinutes,
|
||||
plannedStart: operation.plannedStart,
|
||||
plannedEnd: operation.plannedEnd,
|
||||
notes: operation.notes,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
async function nextWorkOrderNumber() {
|
||||
const next = (await workOrderModel.count()) + 1;
|
||||
return `WO-${String(next).padStart(5, "0")}`;
|
||||
@@ -295,6 +493,11 @@ async function validateWorkOrderInput(payload: WorkOrderInput) {
|
||||
id: true,
|
||||
type: true,
|
||||
status: true,
|
||||
_count: {
|
||||
select: {
|
||||
operations: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -310,6 +513,10 @@ async function validateWorkOrderInput(payload: WorkOrderInput) {
|
||||
return { ok: false as const, reason: "Work orders can only be created for assembly or manufactured items." };
|
||||
}
|
||||
|
||||
if (item._count.operations === 0) {
|
||||
return { ok: false as const, reason: "Build item must have at least one station operation before a work order can be created." };
|
||||
}
|
||||
|
||||
if (payload.projectId) {
|
||||
const project = await prisma.project.findUnique({
|
||||
where: { id: payload.projectId },
|
||||
@@ -350,11 +557,51 @@ export async function listManufacturingItemOptions(): Promise<ManufacturingItemO
|
||||
name: true,
|
||||
type: true,
|
||||
unitOfMeasure: true,
|
||||
operations: {
|
||||
select: {
|
||||
setupMinutes: true,
|
||||
runMinutesPerUnit: true,
|
||||
moveMinutes: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: [{ sku: "asc" }],
|
||||
});
|
||||
|
||||
return items;
|
||||
return items.map((item) => ({
|
||||
id: item.id,
|
||||
sku: item.sku,
|
||||
name: item.name,
|
||||
type: item.type,
|
||||
unitOfMeasure: item.unitOfMeasure,
|
||||
operationCount: item.operations.length,
|
||||
totalEstimatedMinutesPerUnit: item.operations.reduce(
|
||||
(sum, operation) => sum + operation.setupMinutes + operation.runMinutesPerUnit + operation.moveMinutes,
|
||||
0
|
||||
),
|
||||
}));
|
||||
}
|
||||
|
||||
export async function listManufacturingStations(): Promise<ManufacturingStationDto[]> {
|
||||
const stations = await prisma.manufacturingStation.findMany({
|
||||
orderBy: [{ code: "asc" }],
|
||||
});
|
||||
|
||||
return stations.map(mapStation);
|
||||
}
|
||||
|
||||
export async function createManufacturingStation(payload: ManufacturingStationInput) {
|
||||
const station = await prisma.manufacturingStation.create({
|
||||
data: {
|
||||
code: payload.code.trim(),
|
||||
name: payload.name.trim(),
|
||||
description: payload.description,
|
||||
queueDays: payload.queueDays,
|
||||
isActive: payload.isActive,
|
||||
},
|
||||
});
|
||||
|
||||
return mapStation(station);
|
||||
}
|
||||
|
||||
export async function listManufacturingProjectOptions(): Promise<ManufacturingProjectOptionDto[]> {
|
||||
@@ -448,6 +695,8 @@ export async function createWorkOrder(payload: WorkOrderInput) {
|
||||
},
|
||||
});
|
||||
|
||||
await regenerateWorkOrderOperations(created.id);
|
||||
|
||||
const workOrder = await getWorkOrderById(created.id);
|
||||
return workOrder ? { ok: true as const, workOrder } : { ok: false as const, reason: "Unable to load saved work order." };
|
||||
}
|
||||
@@ -488,6 +737,8 @@ export async function updateWorkOrder(workOrderId: string, payload: WorkOrderInp
|
||||
},
|
||||
});
|
||||
|
||||
await regenerateWorkOrderOperations(workOrderId);
|
||||
|
||||
const workOrder = await getWorkOrderById(workOrderId);
|
||||
return workOrder ? { ok: true as const, workOrder } : { ok: false as const, reason: "Unable to load saved work order." };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user