import { initValInUsage, initValOutUsage, subTotalObj, totalObj, type PrfType, type PrfUsageSummary, } from "@/app/store/prfmncReg/pbnstPrfmncReg/type"; export const getTablePrfData = (prfList: PrfType[]) => { const inUsageData = structuredClone(initValInUsage); const outUsageData = structuredClone(initValOutUsage); const inSubtotal = structuredClone(subTotalObj); const outSubtotal = structuredClone(subTotalObj); if (prfList) { prfList?.forEach((prfItem) => { const month = Number(prfItem?.ym?.slice(4)); // "MM" const monthKey = `${month}월`; inUsageData.forEach((inData: PrfUsageSummary) => { const usageKey = inData.usage as keyof PrfType; const value = (prfItem[usageKey] as number) ?? 0; (inData[monthKey as keyof PrfUsageSummary] as number) = value; inData.total += value; if (inSubtotal&& usageKey !== "subtotal" as keyof PrfType) { (inSubtotal[monthKey as keyof PrfUsageSummary] as number) = ((inSubtotal[monthKey as keyof PrfUsageSummary] as number) ?? 0) + value; inSubtotal.total += value; } }); outUsageData.forEach((outData: PrfUsageSummary) => { const usageKey = outData.usage as keyof PrfType; const value = (prfItem[usageKey] as number) ?? 0; // Type assertion for prfItem[usageKey] (outData[monthKey as keyof PrfUsageSummary] as number) = value; outData.total += value; if (outSubtotal && usageKey !== "subtotal" as keyof PrfType) { (outSubtotal [monthKey as keyof PrfUsageSummary] as number) = ((outSubtotal[monthKey as keyof PrfUsageSummary] as number) ?? 0) + value; outSubtotal.total += value; } // End of if (outSubtotal && usageKey !== "subtotal") }); // End of outUsageData.forEach }); (inSubtotal as any)["구분"] = "장내용수"; (outSubtotal as any)["구분"] = "장내용수"; inUsageData.push(inSubtotal); outUsageData.push(outSubtotal); const usageTotalObj: { [key: string]: any } = structuredClone(totalObj); for (const key in usageTotalObj) { if (["usage", "gubun", "edit"].includes(key)) continue; const val = (Number((inSubtotal as any)[key]) || 0) + (Number((outSubtotal as any)[key]) || 0); (usageTotalObj as any)[key] = val; } const tableData = [...inUsageData, ...outUsageData, usageTotalObj]; return { inPrf: inUsageData, outPrf: outUsageData, totalPrf: tableData, }; } }; // const usageKey = inData.usage as keyof PrfType; // const value = (prfItem[usageKey] as number) ?? 0; // (inData[monthKey as keyof PrfUsageSummary] as number) = value; // inData.total += value; // if (inSubtotal&& usageKey !== "subtotal" as keyof PrfType) { // (inSubtotal[monthKey as keyof PrfUsageSummary] as number) = // ((inSubtotal[monthKey as keyof PrfUsageSummary] as number) ?? 0) + // value; // inSubtotal.total += value; // }