import { tryCatch } from "../../../../globalUtils/tryCatch.js"; import { createLog } from "../../../logger/logger.js"; import { serverSettings } from "../../../server/controller/settings/getSettings.js"; import { query } from "../../../sqlServer/prodSqlServer.js"; import { machineCheck } from "../../../sqlServer/querys/ocp/machineId.js"; import { mmQuery } from "../../../sqlServer/querys/ocp/mainMaterial.js"; export const isMainMatStaged = async (lot: any) => { const set = serverSettings.length === 0 ? [] : serverSettings; // make staged false by default and error logged if theres an issue let isStaged = { message: "Material is staged", success: true }; // validate the machine actaully needs materials to print const { data, error } = (await tryCatch( query( machineCheck.replace("where Active = 1 and [Location] = [loc]", ""), "check machine needs mm", ), )) as any; const machine = data.data.filter( (m: any) => m.HumanReadableId === lot.machineID, ); // just in case we encounter an issue with the machines if (machine.length === 0) { createLog( "error", "mainMaterial", "ocp-system", "Invalid machine passed over.", ); return { success: false, message: "Invalid machine passed over.", }; } // we have a check on ksc side to ignore the tetra machine for now as its not updating in 2.0 if (!machine[0].StagingMainMaterialMandatory) { createLog( "info", "mainMaterial", "ocp", `The machine dose not require mm to print and book in.`, ); return { message: "Machine dose not require material to be staged", success: true, }; } // strangely the lot is not always sending over in slc so adding this in for now to see what line is cauing this issue if (!lot) { createLog("info", "mainMaterial", "ocp", "No lot was passed correctly."); return isStaged; } if (typeof lot !== "object" || lot === null || Array.isArray(lot)) { createLog( "info", "mainMaterial", "ocp", `The lot sent over is not an object: ${JSON.stringify(lot)}`, ); return isStaged; } // get the materials needed for the passed over lot const { data: material, error: errorMat } = (await tryCatch( query(mmQuery.replaceAll("[lotNumber]", lot.lot), "Main Material Check"), )) as any; if (errorMat) { return { message: "Failed to get lot info", success: false }; } const mat = material.data; const mainMaterial = mat.find((n: any) => n.IsMainMaterial); const checkColorSetting = set.filter((n) => n.name === "checkColor"); const checkPKGSetting = set.filter((n) => n.name === "checkPKG"); // if we only care about having the check for mm staged and dont care about the rules we just let it fly by. // risk here is getting $Shortage if there really is nothing if ( mainMaterial?.Staged === 1 && (checkColorSetting[0].value !== "1" || checkPKGSetting[0].value !== "1") ) { createLog( "info", "mainMaterial", "ocp", `Main material: ${mainMaterial.MaterialHumanReadableId} - ${mainMaterial.MaterialDescription}: is staged for ${lot.lot}`, ); return { message: `Main material: ${mainMaterial.MaterialHumanReadableId} - ${mainMaterial.MaterialDescription}: is staged for ${lot.lot}`, success: true, }; } // do we have enough main material for the next pallet if (mainMaterial?.noMMShortage === "noMM") { createLog( "info", "mainMaterial", "ocp", `Main material: ${mainMaterial.MaterialHumanReadableId} - ${mainMaterial.MaterialDescription}: is not staged for ${lot.lot}`, ); return { message: `Main material: ${mainMaterial.MaterialHumanReadableId} - ${mainMaterial.MaterialDescription}: is not staged for ${lot.lot}`, success: false, }; } // do we have color to the line if (checkColorSetting[0].value === "1") { const autoConsumeColor = mat.find( (n: any) => !n.isManual && !("noPKGAutoShortage" in n) && !("noPKGManualShortage" in n), ); if (autoConsumeColor.autoConsumeCheck === "autoConsumeNOK") { createLog( "info", "mainMaterial", "ocp", `lot: ${lot.lot}, is missing: ${autoConsumeColor .map( (o: any) => `${o.MaterialHumanReadableId} - ${o.MaterialDescription}`, ) .join(",\n ")} for autoconsume`, ); return { message: `lot: ${lot.lot}, is missing: ${autoConsumeColor .map( (o: any) => `${o.MaterialHumanReadableId} - ${o.MaterialDescription}`, ) .join(",\n ")} for autoconsume`, success: false, }; } } };