diff --git a/app/controllers/report.controller.js b/app/controllers/report.controller.js index d96250e..9ca7ab0 100644 --- a/app/controllers/report.controller.js +++ b/app/controllers/report.controller.js @@ -164,14 +164,6 @@ const findRelatedCustomersForReport = (route, customer, allRoutes) => { return { relativeRouteCustomer, customerInOtherRoute, otherRoute, otherOutboundRoute }; }; -const resolveNoteFromCandidates = (...candidates) => { - for (const candidate of candidates) { - const note = (candidate?.customer_note || "").toString().trim(); - if (note) return note; - } - return ""; -}; - const hasEnterCenterTimeByCandidate = (...candidates) => candidates.some((item) => item?.customer_enter_center_time !== undefined && item?.customer_enter_center_time !== null && item?.customer_enter_center_time !== ""); @@ -188,7 +180,7 @@ const appendNoticeLine = (parts, text) => { } }; -const buildRoutePdfBuffer = async (templateBytes, route, seqNum, driversMap, vehiclesMap, outboundCustomerStatuses, unicodeFontBytes, allRoutes, customerNotesById) => { +const buildRoutePdfBuffer = async (templateBytes, route, seqNum, driversMap, vehiclesMap, outboundCustomerStatuses, unicodeFontBytes, allRoutes, customerNotesById, debugRows) => { const pdfDoc = await PDFDocument.load(templateBytes); pdfDoc.registerFontkit(fontkit); const unicodeFont = await pdfDoc.embedFont(unicodeFontBytes, { subset: true }); @@ -218,7 +210,7 @@ const buildRoutePdfBuffer = async (templateBytes, route, seqNum, driversMap, veh safeSetField(form, `phone_${row}`, customer?.customer_phone || ""); const customerId = toObjectIdText(customer?.customer_id); const profileNoteToDriver = (customerNotesById?.get(customerId) || "").toString().trim(); - const customerNoteText = profileNoteToDriver || resolveNoteFromCandidates(customer, relativeRouteCustomer, customerInOtherRoute); + const customerNoteText = profileNoteToDriver; safeSetField(form, `note_${row}`, customerNoteText); const pickupTime = formatUtcToLocalHm( @@ -229,6 +221,8 @@ const buildRoutePdfBuffer = async (templateBytes, route, seqNum, driversMap, veh if (pickupTime) safeSetField(form, `pick_${row}`, pickupTime); const hasEnterCenterTime = hasEnterCenterTimeByCandidate(customer, relativeRouteCustomer, customerInOtherRoute); + let yValue = ""; + let nValue = ""; if (hasEnterCenterTime) { const enterCenterTime = formatUtcToLocalHm( customer?.customer_enter_center_time || @@ -238,14 +232,20 @@ const buildRoutePdfBuffer = async (templateBytes, route, seqNum, driversMap, veh if (enterCenterTime) { safeSetField(form, `arrive_${row}`, enterCenterTime); } - safeSetField(form, `y_${row}`, "✓", { fontSize: 12, center: true, blackText: true }); - safeSetField(form, `n_${row}`, "", { fontSize: 12, center: true, blackText: true }); + yValue = "Y"; + nValue = ""; + safeSetField(form, `y_${row}`, yValue, { fontSize: 12, center: true, blackText: true }); + safeSetField(form, `n_${row}`, nValue, { fontSize: 12, center: true, blackText: true }); } else if (hasInCenterStatusByCandidate(customer, relativeRouteCustomer, customerInOtherRoute)) { - safeSetField(form, `y_${row}`, "✓", { fontSize: 12, center: true, blackText: true }); - safeSetField(form, `n_${row}`, "", { fontSize: 12, center: true, blackText: true }); + yValue = "Y"; + nValue = ""; + safeSetField(form, `y_${row}`, yValue, { fontSize: 12, center: true, blackText: true }); + safeSetField(form, `n_${row}`, nValue, { fontSize: 12, center: true, blackText: true }); } else { - safeSetField(form, `y_${row}`, "", { fontSize: 12, center: true, blackText: true }); - safeSetField(form, `n_${row}`, "✕", { fontSize: 12, center: true, blackText: true }); + yValue = ""; + nValue = "N"; + safeSetField(form, `y_${row}`, yValue, { fontSize: 12, center: true, blackText: true }); + safeSetField(form, `n_${row}`, nValue, { fontSize: 12, center: true, blackText: true }); } const outboundStatus = findOutboundStatusByCustomerId(outboundCustomerStatuses, customer?.customer_id); @@ -307,8 +307,21 @@ const buildRoutePdfBuffer = async (templateBytes, route, seqNum, driversMap, veh } } - if (noticeParts.length > 0) { - safeSetField(form, `note_${row}`, noticeParts.join(" ")); + if (noticeParts.length > 0) safeSetField(form, `note_${row}`, noticeParts.join(" ")); + + if (Array.isArray(debugRows)) { + debugRows.push({ + route: route?.name || "", + row, + customer_id: customerId, + customer_name: customer?.customer_name || "", + hasEnterCenterTime, + hasInCenterStatus: hasInCenterStatusByCandidate(customer, relativeRouteCustomer, customerInOtherRoute), + y_field_value: yValue, + n_field_value: nValue, + note_to_driver: customerNoteText, + final_notice: noticeParts.join(" ") + }); } }); @@ -435,6 +448,7 @@ exports.updateReport = (req, res) => { exports.exportRouteReportZip = async (req, res) => { try { const date = req.query?.date; + const debugMode = `${req.query?.debug || ""}`.toLowerCase() === "1" || `${req.query?.debug || ""}`.toLowerCase() === "true"; if (!date) { return res.status(400).send({ message: "date query is required." }); } @@ -517,6 +531,7 @@ exports.exportRouteReportZip = async (req, res) => { archive.pipe(res); const filenameCounter = new Map(); + const debugRows = []; for (let i = 0; i < inboundRoutes.length; i += 1) { const route = inboundRoutes[i]; const pdfBytes = await buildRoutePdfBuffer( @@ -528,7 +543,8 @@ exports.exportRouteReportZip = async (req, res) => { outboundCustomerStatuses, unicodeFontBytes, routes, - customerNotesById + customerNotesById, + debugMode ? debugRows : null ); const base = sanitizeFileName(route?.name || `route_${i + 1}`) || `route_${i + 1}`; const existingCount = filenameCounter.get(base) || 0; @@ -537,6 +553,24 @@ exports.exportRouteReportZip = async (req, res) => { archive.append(Buffer.from(pdfBytes), { name: finalName }); } + if (debugMode) { + archive.append( + Buffer.from( + JSON.stringify( + { + date, + site, + generated_at: new Date().toISOString(), + rows: debugRows + }, + null, + 2 + ) + ), + { name: "debug_export.json" } + ); + } + archive.finalize(); } catch (err) { console.error("Failed to export route report zip:", err); diff --git a/client/src/components/trans-routes/RouteReportWithSignature.js b/client/src/components/trans-routes/RouteReportWithSignature.js index 8287392..605c8aa 100644 --- a/client/src/components/trans-routes/RouteReportWithSignature.js +++ b/client/src/components/trans-routes/RouteReportWithSignature.js @@ -187,10 +187,6 @@ const RouteReportWithSignature = () => { const getRouteNoteToDriverText = (customerId, ...customerCandidates) => { const profileNote = (customerMetaById.get(customerId)?.notes_for_driver || '').toString().trim(); if (profileNote) return profileNote; - for (const item of customerCandidates) { - const noteText = (item?.customer_note || '').toString().trim(); - if (noteText) return noteText; - } return ''; };