143 lines
3.7 KiB
TypeScript
143 lines
3.7 KiB
TypeScript
import { format } from "date-fns";
|
|
import { eq, sql } from "drizzle-orm";
|
|
import { success } from "zod/v4";
|
|
import { db } from "../../../../database/dbclient.js";
|
|
import { invHistoricalData } from "../../../../database/schema/historicalINV.js";
|
|
import { settings } from "../../../../database/schema/settings.js";
|
|
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 { activeArticle } from "../../sqlServer/querys/dataMart/article.js";
|
|
import { missingHistInv } from "../../sqlServer/querys/eom/missingHistInv.js";
|
|
|
|
const missingHistoricalData = async (date: string) => {
|
|
const plantToken = serverSettings.filter((n) => n.name === "plantToken");
|
|
const { data: inv, error: invError } = (await tryCatch(
|
|
query(
|
|
missingHistInv.replace("[eomHistDate]", date),
|
|
"eom missing historical data",
|
|
),
|
|
)) as any;
|
|
|
|
if (invError) {
|
|
createLog("error", "eom", "eom", `There was an error getting the sql data`);
|
|
return;
|
|
}
|
|
|
|
const { data: articles, error: avError } = (await tryCatch(
|
|
query(activeArticle, "Get active articles"),
|
|
)) as any;
|
|
|
|
const av = articles.data.length > 0 ? articles.data : ([] as any);
|
|
|
|
const importInv = inv.data ? inv.data : [];
|
|
|
|
const remap = importInv.map((i: any) => {
|
|
return {
|
|
//histDate: sql`(NOW() - INTERVAL '1 day')::date`,
|
|
histDate: date,
|
|
plantToken: plantToken[0].value,
|
|
article: i.article,
|
|
articleDescription: i.articleDescription,
|
|
materialType:
|
|
av.filter((a: any) => a.IdArtikelvarianten === i.article).length > 0
|
|
? av.filter((a: any) => a.IdArtikelvarianten === i.article)[0]
|
|
?.TypeOfMaterial
|
|
: "Item not defined",
|
|
total_QTY: i.total_QTY,
|
|
avaliable_QTY: 0,
|
|
coa_QTY: 0,
|
|
held_QTY: 0,
|
|
consignment: 0,
|
|
lot_Number: 0,
|
|
};
|
|
});
|
|
|
|
const { data: dataImport, error: errorImport } = await tryCatch(
|
|
db.insert(invHistoricalData).values(remap),
|
|
);
|
|
|
|
if (errorImport) {
|
|
console.log(errorImport);
|
|
createLog(
|
|
"error",
|
|
"eom",
|
|
"eom",
|
|
`There was an error importing all the inventory data.`,
|
|
);
|
|
return [];
|
|
}
|
|
|
|
if (dataImport) {
|
|
createLog("info", "eom", "eom", `All data was imported successfully.`);
|
|
|
|
return remap;
|
|
}
|
|
};
|
|
|
|
export const historicalInvByDate = async (
|
|
date: string,
|
|
includePlantToken: boolean = false,
|
|
) => {
|
|
const histDate = new Date(date);
|
|
|
|
const { data, error } = (await tryCatch(
|
|
db
|
|
.select()
|
|
.from(invHistoricalData)
|
|
.where(eq(invHistoricalData.histDate, format(histDate, "yyyy-MM-dd"))),
|
|
)) as any;
|
|
|
|
if (error) {
|
|
return {
|
|
success: false,
|
|
message: "There was an error with getting the inventory",
|
|
data: error,
|
|
};
|
|
}
|
|
|
|
if (includePlantToken) {
|
|
const { data: s, error: se } = (await tryCatch(
|
|
db.select().from(settings).where(eq(settings.name, "plantToken")),
|
|
)) as any;
|
|
|
|
if (se) {
|
|
console.log("Error getting articles");
|
|
return data;
|
|
}
|
|
|
|
if (data.length === 0) {
|
|
// pass over the old lame
|
|
|
|
return {
|
|
success: true,
|
|
message: `Historical inventory is missing for ${date}`,
|
|
data: await missingHistoricalData(date),
|
|
};
|
|
}
|
|
return {
|
|
success: true,
|
|
message: `Historical inventory for ${date}`,
|
|
data: data.map((n: any) => {
|
|
return { plantToken: s[0].value, ...n };
|
|
}),
|
|
};
|
|
} else {
|
|
if (data.length === 0) {
|
|
// pass over the old lame
|
|
//const oldData = missingHistoricalData(date)
|
|
return {
|
|
success: true,
|
|
message: `Historical inventory is missing for ${date}`,
|
|
data: await missingHistoricalData(date),
|
|
};
|
|
}
|
|
return {
|
|
success: true,
|
|
message: `Historical inventory for ${date}`,
|
|
data: data,
|
|
};
|
|
}
|
|
};
|