Compare commits

..

2 Commits

2 changed files with 75 additions and 32 deletions

View File

@@ -76,36 +76,65 @@ const missingHistoricalData = async (date: string) => {
}
};
function parseFlexibleDate(date: string): Date {
const parts = date.replace(/[-.]/g, "/").split("/");
if (parts.length !== 3) throw new Error("Invalid date format");
let year: number, month: number, day: number;
// find which index looks like a year
if (parts[0].length === 4) {
// yyyy/mm/dd
year = +parts[0];
month = +parts[1];
day = +parts[2];
} else if (parts[2].length === 4) {
// mm/dd/yyyy
year = +parts[2];
month = +parts[0];
day = +parts[1];
} else {
throw new Error("Cannot determine year position");
}
return new Date(year, month - 1, day);
}
export const historicalInvByDate = async (
date: string,
includePlantToken: boolean = false,
) => {
console.log(date);
// date format should always be yyyy-MM-dd or yyyy-MM-dd
let splitDate: string[];
if (date.includes("/")) {
splitDate = date.split("/");
} else if (date.includes("-")) {
splitDate = date.split("-");
} else {
return {
success: false,
message: "An invalid date was passed over",
data: [],
};
}
// let splitDate: string[];
// if (date.includes("/")) {
// splitDate = date.split("/");
// } else if (date.includes("-")) {
// splitDate = date.split("-");
// } else {
// return {
// success: false,
// message: "An invalid date was passed over",
// data: [],
// };
// }
const year = parseInt(splitDate[0], 10);
const month = parseInt(splitDate[1], 10) - 1; // zero-based months
const day = parseInt(splitDate[2], 10);
const histDate = new Date(year, month, day);
console.log(histDate);
console.log(format(histDate, "yyyy-MM-dd"));
// const year = parseInt(splitDate[0], 10);
// const month = parseInt(splitDate[1], 10) - 1; // zero-based months
// const day = parseInt(splitDate[2], 10);
//const histDate = new Date(year, month, day);
console.log(parseFlexibleDate(date));
console.log(format(parseFlexibleDate(date), "yyyy-MM-dd"));
const { data, error } = (await tryCatch(
db
.select()
.from(invHistoricalData)
.where(eq(invHistoricalData.histDate, format(histDate, "yyyy-MM-dd"))),
.where(
eq(
invHistoricalData.histDate,
format(parseFlexibleDate(date), "yyyy-MM-dd"),
),
),
)) as any;
if (error) {

View File

@@ -124,6 +124,7 @@ V_Artikel.ProdArtikelBez as ProductFamily
LTRIM(REPLACE(pur.UOM,'UOM:','')),
CHARINDEX(' ', LTRIM(REPLACE(REPLACE(pur.UOM,'UOM:',''), CHAR(13)+CHAR(10), ' ')) + ' ') - 1
) end AS UOM
--,*
FROM dbo.V_Artikel (nolock)
@@ -166,20 +167,33 @@ left join
,GueltigabDatum as validDate
,EKPreis as price
,LiefArtNr as supplierNr
,CASE
WHEN Bemerkung IS NOT NULL AND Bemerkung LIKE '%UOM:%'
THEN
-- incase there is something funny going on in the remark well jsut check for new lines and what not
LEFT(
REPLACE(REPLACE(Bemerkung, CHAR(13)+CHAR(10), ' '), CHAR(10), ' '),
CASE
WHEN CHARINDEX(' ', REPLACE(REPLACE(Bemerkung, CHAR(13)+CHAR(10), ' '), CHAR(10), ' ')) > 0
THEN CHARINDEX(' ', REPLACE(REPLACE(Bemerkung, CHAR(13)+CHAR(10), ' '), CHAR(10), ' ')) - 1
ELSE LEN(Bemerkung)
END
--,CASE
-- WHEN Bemerkung IS NOT NULL AND Bemerkung LIKE '%UOM:%'
-- THEN
-- -- incase there is something funny going on in the remark well jsut check for new lines and what not
-- LEFT(
-- REPLACE(REPLACE(Bemerkung, CHAR(13)+CHAR(10), ' '), CHAR(10), ' '),
-- CASE
-- WHEN CHARINDEX(' ', REPLACE(REPLACE(Bemerkung, CHAR(13)+CHAR(10), ' '), CHAR(10), ' ')) > 0
-- THEN CHARINDEX(' ', REPLACE(REPLACE(Bemerkung, CHAR(13)+CHAR(10), ' '), CHAR(10), ' ')) - 1
-- ELSE LEN(Bemerkung)
-- END
-- )
-- ELSE 'UOM:1'
-- END AS UOM
,CASE
WHEN Bemerkung IS NOT NULL AND Bemerkung LIKE '%UOM:%'
THEN
LTRIM(
SUBSTRING(
Bemerkung,
CHARINDEX('UOM:', UPPER(Bemerkung)) + LEN('UOM:'),
LEN(Bemerkung)
)
)
ELSE 'UOM:1'
END AS UOM
ELSE
'UOM:1'
END AS UOM
,Bemerkung
--,*
from dbo.T_HistoryEK (nolock)