92 lines
3.0 KiB
TypeScript
92 lines
3.0 KiB
TypeScript
import { db } from "../../../../../database/dbclient.js";
|
|
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
|
import { createLog } from "../../../logger/logger.js";
|
|
import { query } from "../../../sqlServer/prodSqlServer.js";
|
|
import { totalInvNoRn } from "../../../sqlServer/querys/dataMart/totalINV.js";
|
|
import { invHistoricalData } from "../../../../../database/schema/historicalINV.js";
|
|
import { format } from "date-fns-tz";
|
|
import { settings } from "../../../../../database/schema/settings.js";
|
|
import { sql } from "drizzle-orm";
|
|
import { createLogisticsJob } from "../../utils/logisticsIntervals.js";
|
|
|
|
export const runHistoricalData = async () => {
|
|
/**
|
|
* Runs a query at shift change on first shift each day this will be the closest date to the true historical data for blocked, consignment
|
|
*/
|
|
|
|
const { data: set, error: setError } = await tryCatch(
|
|
db.select().from(settings)
|
|
);
|
|
|
|
if (setError) {
|
|
createLog(
|
|
"error",
|
|
"lst",
|
|
"eom",
|
|
"There was an error getting eom historical inv data."
|
|
);
|
|
return;
|
|
}
|
|
|
|
const timeZone = set.filter((n: any) => n.name === "timezone");
|
|
|
|
createLogisticsJob("histInv", `0 6 * * *`, timeZone[0].value, async () => {
|
|
// remove the lotnumber from the query
|
|
const updatedQuery = totalInvNoRn.replaceAll(
|
|
",IdProdPlanung",
|
|
"--,IdProdPlanung"
|
|
);
|
|
|
|
const { data: inv, error: invError } = await tryCatch(
|
|
query(updatedQuery, "EOM historical inv")
|
|
);
|
|
|
|
if (invError) {
|
|
createLog(
|
|
"error",
|
|
"lst",
|
|
"eom",
|
|
"There was an error getting eom historical inv data."
|
|
);
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* add the inv into the hist table
|
|
*/
|
|
|
|
const setting: any = set;
|
|
|
|
for (let i = 0; i < inv?.data.length; i++) {
|
|
const current = inv?.data[i];
|
|
const { data, error } = await tryCatch(
|
|
db.insert(invHistoricalData).values({
|
|
histDate: format(new Date(), "MM-dd-yyyy"),
|
|
plantToken: setting.filter(
|
|
(n: any) => n.name === "plantToken"
|
|
)[0].value,
|
|
article: current.av,
|
|
articleDescription: current.Alias,
|
|
total_QTY: current.Total_PalletQTY,
|
|
avaliable_QTY: current.Avaliable_PalletQTY,
|
|
coa_QTY: current.COA_QTY,
|
|
held_QTY: current.Held_QTY,
|
|
consignment: current.Consigment,
|
|
//location: integer("location"),
|
|
upd_user: "LST",
|
|
upd_date: sql`NOW()`,
|
|
})
|
|
);
|
|
|
|
if (error) {
|
|
createLog(
|
|
"error",
|
|
"lst",
|
|
"eom",
|
|
`Error addeding historical data, ${error}`
|
|
);
|
|
}
|
|
}
|
|
});
|
|
};
|