diff --git a/src/layouts/menu.js b/src/layouts/menu.js index 71d0feb..1a31346 100644 --- a/src/layouts/menu.js +++ b/src/layouts/menu.js @@ -145,6 +145,22 @@ export function getMenuItems({ permissions = [], roles = [], domain = '', email }); } + if (domain === 'horizon') { + const designationName = String(designation).trim().toUpperCase(); + const fullAccess = permissions.includes('all_data_access') || designationName === 'HO'; + const isZbm = designationName === 'ZBM'; + if ((fullAccess || isZbm) && designationName !== 'RBM') { + items.push({ + key: 'horizon-custom-reports', + label: 'Custom Reports', + icon: LuFileText, + children: [ + child('horizon-field-work', 'Monthly Field Work Report', '/horizonreport/fieldwork'), + ], + }); + } + } + if (domain === 'pulmocare' && roles.includes('1650360330-5bf35594-674a-4f31-HO_Audit-47738d2151ab')) { items.push({ key: 'healthy-lung', diff --git a/src/routes/index.jsx b/src/routes/index.jsx index f50b715..932cf6d 100644 --- a/src/routes/index.jsx +++ b/src/routes/index.jsx @@ -101,6 +101,7 @@ const ActionPlanReport = lazy(() => import('@/views/coachingtooldashboard/Action const GoalProficiencyReport = lazy(() => import('@/views/coachingtooldashboard/GoalProficiencyReport')); const AdherencePobDriveReport = lazy(() => import('@/views/adherencepobdrivereport/Report')); const IvaReportPage = lazy(() => import('@/views/ivareport/ReportPage')); +const HorizonFieldWorkReport = lazy(() => import('@/views/horizonreport/Report')); function moduleRoutes(config) { const routes = [ @@ -244,6 +245,7 @@ export const routes = [ { path: '/ivareport/kamwise', element: }, { path: '/ivareport/drbrandwise', element: }, { path: '/ivareport/skuwise', element: }, + { path: '/horizonreport/fieldwork', element: }, { path: '/coming-soon', element: }, { path: '*', element: }, ], diff --git a/src/views/horizonreport/Report.jsx b/src/views/horizonreport/Report.jsx new file mode 100644 index 0000000..b39edc9 --- /dev/null +++ b/src/views/horizonreport/Report.jsx @@ -0,0 +1,235 @@ +import { Fragment, useEffect, useMemo, useState } from 'react'; +import { Button, Container, Form } from 'react-bootstrap'; +import { isSuccess, signedGet } from '@/api/client'; +import { useAuth } from '@/auth/AuthProvider'; +import PageMetaData from '@/components/PageMetaData'; +import { exportFieldWorkReport } from './exportXlsx'; + +const TITLE = 'Monthly Field Work Report'; +const REPORT_TIMEOUT = 120000; +const FIRST_MONTH = new Date(2026, 10, 1); +const IDENTITY = [ + 'RBM/ZBM Name', + 'Emp Code', + 'Role Name', + 'Position Name', + 'Manager 1 Code', + 'Manager 1 Name', + 'Manager 2 Code', + 'Manager 2 Name', +]; + +function monthName(date) { + return date.toLocaleString('en-US', { month: 'long', year: 'numeric' }); +} + +function reportMonths() { + const months = []; + const end = new Date(); + end.setDate(1); + const cursor = new Date(FIRST_MONTH); + while (cursor <= end) { + months.push(monthName(cursor)); + cursor.setMonth(cursor.getMonth() + 1); + } + return months.reverse(); +} + +function dayLabel(iso) { + const [year, month, day] = String(iso).split('-'); + return `${day}-${month}-${year}`; +} + +function reportPayload(payload) { + if (!payload) return null; + if (payload.body !== undefined) { + let body = payload.body; + if (typeof body === 'string') { + try { + body = JSON.parse(body); + } catch { + return null; + } + } + return body?.data || null; + } + return payload.data || null; +} + +export default function HorizonFieldWorkReport() { + const { domain } = useAuth(); + const allowed = domain === 'horizon'; + const months = useMemo(() => reportMonths(), []); + const [selected, setSelected] = useState(() => { + const current = monthName(new Date()); + return months.includes(current) ? current : months[0]; + }); + const [month, setMonth] = useState(selected); + const [loading, setLoading] = useState(allowed); + const [error, setError] = useState(''); + const [report, setReport] = useState(null); + const [exporting, setExporting] = useState(false); + + useEffect(() => { + if (!allowed) return undefined; + let cancelled = false; + setLoading(true); + setError(''); + signedGet('reports', 'horizon_reports/field_work_report', { month }, { timeout: REPORT_TIMEOUT }).then((response) => { + if (cancelled) return; + const data = reportPayload(response); + if (!isSuccess(response) || !data || typeof data !== 'object') { + setReport(null); + setError(response?.message || 'Unable to load the field work report.'); + return; + } + setReport(data); + }).finally(() => { + if (!cancelled) setLoading(false); + }); + return () => { + cancelled = true; + }; + }, [allowed, month]); + + const days = Array.isArray(report?.days) ? report.days : []; + const rows = Array.isArray(report?.rows) ? report.rows : []; + const columnCount = IDENTITY.length + days.length * 2 + 1; + + const handleSubmit = (event) => { + event.preventDefault(); + setMonth(selected); + }; + + const handleExport = async () => { + setExporting(true); + setError(''); + try { + const slug = String(month || 'report').toLowerCase().replace(/\s+/g, '-'); + await exportFieldWorkReport(report, `horizon-field-work-${slug}.xlsx`); + } catch (err) { + setError(err.message || 'Unable to export the field work report.'); + } finally { + setExporting(false); + } + }; + + if (!allowed || report?.access === false) { + return ( + + +
+
+

{TITLE}

+
You do not have access to this report.
+
+
+
+ ); + } + + return ( + + +
+
+

{TITLE}

+ {error ?
{error}
: null} +
+
+ Month + setSelected(event.target.value)}> + {months.map((option) => ( + + ))} + +
+
+ +
+ {!loading && rows.length ? ( +
+ +
+ ) : null} +
+ {loading ?

Loading the report wait.....

: null} +
+
+ + {!loading && report ? ( +
+
+
+ + + + {IDENTITY.map((label) => ( + + ))} + {days.map((day) => ( + + ))} + + + + {days.map((day) => ( + + + + + ))} + + + + {rows.length ? rows.map((row) => ( + + + + + + + + + + {days.map((day) => { + const cell = row.days?.[day] || { status: 'blank' }; + if (cell.status === 'link' && cell.link) { + return ( + + + + + ); + } + if (cell.status === 'no') { + return ; + } + return + + )) : null} + {rows.length ? ( + + + + + ) : ( + + + + )} + +
{label}{dayLabel(day)}RBM/ZBM wise Total
LinkTime
{row.name}{row.employee_code}{row.role_name}{row.position_name}{row.manager_1_code}{row.manager_1_name}{row.manager_2_code}{row.manager_2_name} + Link + {cell.time}No; + })} + {row.total ?? 0}
Total{rows.reduce((sum, row) => sum + Number(row.total || 0), 0)}
No rows for this month.
+
+
+
+ ) : null} +
+ ); +} diff --git a/src/views/horizonreport/exportXlsx.js b/src/views/horizonreport/exportXlsx.js new file mode 100644 index 0000000..e7f49d7 --- /dev/null +++ b/src/views/horizonreport/exportXlsx.js @@ -0,0 +1,118 @@ +const IDENTITY = [ + 'RBM/ZBM Name', + 'Emp Code', + 'Role Name', + 'Position Name', + 'Manager 1 Code', + 'Manager 1 Name', + 'Manager 2 Code', + 'Manager 2 Name', +]; + +function dayLabel(iso) { + const [year, month, day] = String(iso).split('-'); + return `${day}-${month}-${year}`; +} + +function textCell(sheet, row, column, value) { + const cell = sheet.getCell(row, column); + cell.value = value === null || value === undefined ? '' : String(value); + cell.alignment = { vertical: 'middle', horizontal: 'center', wrapText: true }; + return cell; +} + +export async function exportFieldWorkReport(report, filename) { + const { default: ExcelJS } = await import('exceljs'); + const workbook = new ExcelJS.Workbook(); + const sheet = workbook.addWorksheet('Field Work'); + const days = Array.isArray(report?.days) ? report.days : []; + const rows = Array.isArray(report?.rows) ? report.rows : []; + const identityCount = IDENTITY.length; + const lastColumn = identityCount + days.length * 2 + 1; + + textCell(sheet, 1, 1, 'Monthly Field Work Report'); + sheet.getCell(1, 1).font = { bold: true }; + sheet.getCell(1, 1).alignment = { vertical: 'middle', horizontal: 'left' }; + + IDENTITY.forEach((label, index) => { + const column = index + 1; + textCell(sheet, 2, column, label); + sheet.mergeCells(2, column, 3, column); + sheet.getCell(2, column).font = { bold: true }; + }); + days.forEach((day, index) => { + const column = identityCount + index * 2 + 1; + textCell(sheet, 2, column, dayLabel(day)); + sheet.mergeCells(2, column, 2, column + 1); + sheet.getCell(2, column).font = { bold: true }; + textCell(sheet, 3, column, 'Link'); + textCell(sheet, 3, column + 1, 'Time'); + sheet.getCell(3, column).font = { bold: true }; + sheet.getCell(3, column + 1).font = { bold: true }; + }); + textCell(sheet, 2, lastColumn, 'RBM/ZBM wise Total'); + sheet.mergeCells(2, lastColumn, 3, lastColumn); + sheet.getCell(2, lastColumn).font = { bold: true }; + + rows.forEach((row, rowIndex) => { + const excelRow = rowIndex + 4; + const values = [ + row.name, + row.employee_code, + row.role_name, + row.position_name, + row.manager_1_code, + row.manager_1_name, + row.manager_2_code, + row.manager_2_name, + ]; + values.forEach((value, index) => { + const cell = textCell(sheet, excelRow, index + 1, value); + if (index === 0 || index === 2 || index === 3 || index === 5 || index === 7) { + cell.alignment = { vertical: 'middle', horizontal: 'left' }; + } + }); + days.forEach((day, index) => { + const column = identityCount + index * 2 + 1; + const cell = row.days?.[day] || { status: 'blank', link: '', time: '' }; + if (cell.status === 'link' && cell.link) { + const linkCell = sheet.getCell(excelRow, column); + linkCell.value = { text: 'Link', hyperlink: cell.link }; + linkCell.font = { color: { argb: 'FF0563C1' }, underline: true }; + linkCell.alignment = { vertical: 'middle', horizontal: 'center' }; + textCell(sheet, excelRow, column + 1, cell.time || ''); + } else if (cell.status === 'no') { + sheet.mergeCells(excelRow, column, excelRow, column + 1); + textCell(sheet, excelRow, column, 'No'); + } else { + sheet.mergeCells(excelRow, column, excelRow, column + 1); + textCell(sheet, excelRow, column, ''); + } + }); + textCell(sheet, excelRow, lastColumn, row.total ?? 0); + }); + + if (rows.length) { + const totalRow = rows.length + 4; + const columnTotal = rows.reduce((sum, row) => sum + Number(row.total || 0), 0); + textCell(sheet, totalRow, 1, 'Total'); + sheet.getCell(totalRow, 1).font = { bold: true }; + sheet.getCell(totalRow, 1).alignment = { vertical: 'middle', horizontal: 'left' }; + textCell(sheet, totalRow, lastColumn, columnTotal); + sheet.getCell(totalRow, lastColumn).font = { bold: true }; + } + + for (let column = 1; column <= lastColumn; column += 1) { + sheet.getColumn(column).width = column <= identityCount ? 22 : 14; + } + + const buffer = await workbook.xlsx.writeBuffer(); + const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); + const anchor = document.createElement('a'); + anchor.href = URL.createObjectURL(blob); + anchor.download = filename; + document.body.appendChild(anchor); + anchor.click(); + document.body.removeChild(anchor); + URL.revokeObjectURL(anchor.href); +}