import { eq } from "drizzle-orm"; import { db } from "../../../../../database/dbclient.js"; import { printerData } from "../../../../../database/schema/printers.js"; import { runProdApi } from "../../../../globalUtils/runProdApi.js"; import { tryCatch } from "../../../../globalUtils/tryCatch.js"; import { createLog } from "../../../logger/logger.js"; import { query } from "../../../sqlServer/prodSqlServer.js"; import { labelInfo } from "../../../sqlServer/querys/warehouse/labelInfo.js"; type NewLotData = { runnungNumber: number; lotNumber: number; originalAmount: number; level: number; amount: number; type: "lot" | "eom"; }; /** * Move manual material to a new lot. * * The data sent over should be * Running number * Lot number * Orignal Quantity * level of gaylord * amount can be sent over as a precise amount * type what way are we lots */ export const lotMaterialTransfer = async (data: NewLotData) => { let timeoutTrans: number = data.type === "lot" ? 1 : 10; // get the barcode, and layoutID from the running number const { data: label, error: labelError } = (await tryCatch( query( labelInfo.replace("[runningNr]", `${data.runnungNumber}`), "Get label info" ) )) as any; if (labelError) { createLog( "error", "materials", "ocp", "There was an error getting the label info" ); return { success: false, message: "There was an error getting the label info", data: labelError, }; } if (label.data.length === 0) { createLog( "error", "materials", "ocp", `${data.runnungNumber}: dose not exist or no longer in stock.` ); return { success: false, message: `${data.runnungNumber}: dose not exist or no longer in stock.`, data: [], }; } //console.log(label); if (label.data[0]?.stockStatus === "onStock") { createLog( "error", "materials", "ocp", `${data.runnungNumber}: currently in stock and not consumed to a lot.` ); return { success: false, message: `${data.runnungNumber}: currently in stock and not consumed to a lot.`, data: [], }; } // get the pdf24 printer id const { data: printer, error: printerError } = (await tryCatch( db.select().from(printerData).where(eq(printerData.name, "PDF24")) )) as any; if (printerError) { createLog( "error", "materials", "ocp", "There was an error the printer info" ); return { success: false, message: "There was an error the printer info", data: printerError, }; } // calculate the remaining amount bascially it will be orignal number * level sent over // level should be sent in a decimal .25 .5 .75 .95 the 95 will allow basically the what looks to be a full gaylord but we always want to consume something const newQty = data.amount > 0 ? data.amount : (data.originalAmount * data.level).toFixed(0); //console.log(data.amount); // reprint the label and send it to pdf24 const reprintData = { clientId: 999, runningNo: label?.data[0].runnungNumber, printerId: printer[0].humanReadableId, layoutId: label?.data[0].labelLayout, noOfCopies: 0, quantity: newQty, } as any; //console.log(reprintData); const { data: reprint, error: reprintError } = (await tryCatch( runProdApi({ endpoint: "/public/v1.0/ProductionLabelling/ReprintLabel", data: [reprintData], }) )) as any; if (!reprint.success) { createLog( "error", "materials", "ocp", `RN:${data.runnungNumber}, Reprinting Error: ${reprint.data.data.message}` ); return { success: false, message: `RN:${data.runnungNumber}, Reprinting Error: ${reprint.data.data.message}`, data: reprint, }; } // return the label back to fm1 lane id 10001 const matReturnData = { barcode: label?.data[0].Barcode, laneId: 10001, }; //console.log(matReturnData); const { data: matReturn, error: matReturError } = (await tryCatch( runProdApi({ endpoint: "/public/v1.0/IssueMaterial/ReturnPartiallyConsumedManualMaterial", data: [matReturnData], }) )) as any; if (!matReturn.success) { createLog( "error", "materials", "ocp", `RN:${data.runnungNumber}, Return Error ${matReturn.data.data.errors[0].message}` ); return { success: false, message: `RN:${data.runnungNumber}, Return Error ${matReturn.data.data.errors[0].message}`, data: matReturn, }; } // consume to the lot provided. const consumeLot = { productionLot: data.lotNumber, barcode: label?.data[0].Barcode, }; // sets the time out based on the type of transfer sent over. setTimeout( async () => { const { data: matConsume, error: matConsumeError } = (await tryCatch( runProdApi({ endpoint: "/public/v1.0/IssueMaterial/ConsumeNonPreparedManualMaterial", data: [consumeLot], }) )) as any; if (!matConsume.success) { createLog( "error", "materials", "ocp", `RN:${data.runnungNumber}, Consume Error ${matConsume.data.data.errors[0].message}` ); return { success: false, message: `RN:${data.runnungNumber}, Consume Error ${matConsume.data.data.errors[0].message}`, data: matConsume, }; } createLog( "info", "materials", "ocp", `RN:${data.runnungNumber}: qty: ${newQty}, was transfered to lot:${data.lotNumber}` ); }, data.type === "lot" ? timeoutTrans * 1000 : timeoutTrans * 1000 * 60 ); if (data.type === "eom") { return { success: true, message: `RN:${data.runnungNumber}: qty: ${newQty}, will be transfered to:${data.lotNumber}, in ${timeoutTrans}min`, data: [], }; } else { return { success: true, message: `RN:${data.runnungNumber}: qty: ${newQty}, was transfered to lot:${data.lotNumber}`, data: [], }; } }; // setTimeout(async () => { // lotMaterialTransfer({ // runnungNumber: 603468, // lotNumber: 24897, // originalAmount: 380, // level: 0.95, // }); // }, 5000);