diff --git a/src/modules/10_registry/utils/exportPosition.ts b/src/modules/10_registry/utils/exportPosition.ts index 7745306..1761408 100644 --- a/src/modules/10_registry/utils/exportPosition.ts +++ b/src/modules/10_registry/utils/exportPosition.ts @@ -1,12 +1,24 @@ import ExcelJS from "exceljs"; -import { useCounterMixin } from "@/stores/mixin"; import { useGovernmentPosDataStore } from "@/modules/10_registry/store/Position"; import type { DataPosition } from "@/modules/10_registry/interface/review/Edit"; -const { date2Thai } = useCounterMixin(); const store = useGovernmentPosDataStore(); +// ฟังก์ชันแปลงวันที่จาก ISO format เป็นรูปแบบ dd/mm/yyyy (เช่น 18/05/2564) +function formatDateToDDMMYYYY(dateString: string | null | Date): string { + if (!dateString) return ""; + + const date = new Date(dateString); + if (isNaN(date.getTime())) return ""; + + const day = String(date.getDate()).padStart(2, "0"); + const month = String(date.getMonth() + 1).padStart(2, "0"); + const year = date.getFullYear() + 543; // แปลงเป็นปีพุทธศักราช + + return `${day}/${month}/${year}`; +} + export async function exportToExcelPosition(data: DataPosition[]) { const workbook = new ExcelJS.Workbook(); const worksheet = workbook.addWorksheet("รายการประวัติตำแหน่งเงินเดือน"); @@ -62,7 +74,9 @@ export async function exportToExcelPosition(data: DataPosition[]) { // 3. Map ข้อมูล const newData = data.map((e, index) => ({ no: index + 1, - commandDateAffect: e.commandDateAffect, + commandDateAffect: e.commandDateAffect + ? formatDateToDDMMYYYY(e.commandDateAffect) + : "", positionName: e.positionName, positionType: e.positionType, positionLevel: e.positionLevel, @@ -71,9 +85,9 @@ export async function exportToExcelPosition(data: DataPosition[]) { positionPathSide: e.positionPathSide || "", positionExecutive: e.positionExecutive, positionExecutiveField: e.positionExecutiveField || "", - amount: e.amount, + amount: e.amount || 0, mouthSalaryAmount: e.mouthSalaryAmount || 0, - positionSalaryAmount: e.positionSalaryAmount, + positionSalaryAmount: e.positionSalaryAmount || 0, amountSpecial: e.amountSpecial || 0, organization: e.orgRoot, orgChild1: e.orgChild1, @@ -86,11 +100,13 @@ export async function exportToExcelPosition(data: DataPosition[]) { posNumCodeSitAbb: e.posNumCodeSitAbb, commandNo: e.commandNo, commandYear: e.commandYear ? Number(e.commandYear) + 543 : "", - commandDateSign: e.commandDateSign, + commandDateSign: e.commandDateSign + ? formatDateToDDMMYYYY(e.commandDateSign) + : "", commandCodeName: store.convertCommandCodeName(e.commandCode), remark: e.remark, - commandId: e.commandId, - commandCode: e.commandCode, // ใส่ค่าเริ่มต้นไว้ + commandId: e.commandId || "", + commandCode: e.commandCode || "", // ใส่ค่าเริ่มต้นไว้ })); worksheet.addRows(newData); @@ -99,12 +115,12 @@ export async function exportToExcelPosition(data: DataPosition[]) { newData.forEach((_, index) => { const rowIndex = index + 2; + // --- ตั้งค่า Format สำหรับเซลล์วันที่ --- + // เซลล์ B (commandDateAffect) และ Z (commandDateSign) const dateAffectCell = worksheet.getCell(`B${rowIndex}`); const dateSignCell = worksheet.getCell(`Z${rowIndex}`); [dateAffectCell, dateSignCell].forEach((cell) => { - // ใช้ Custom Format: วัน/เดือน/ปี(4หลัก) - // โดยบังคับให้เลขวันและเดือนมี 2 หลักเสมอ (dd/mm) cell.numFmt = "dd/mm/yyyy"; });