added horizon custom reports
This commit is contained in:
parent
945f593fb1
commit
c1c7df0ace
|
|
@ -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')) {
|
if (domain === 'pulmocare' && roles.includes('1650360330-5bf35594-674a-4f31-HO_Audit-47738d2151ab')) {
|
||||||
items.push({
|
items.push({
|
||||||
key: 'healthy-lung',
|
key: 'healthy-lung',
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,7 @@ const ActionPlanReport = lazy(() => import('@/views/coachingtooldashboard/Action
|
||||||
const GoalProficiencyReport = lazy(() => import('@/views/coachingtooldashboard/GoalProficiencyReport'));
|
const GoalProficiencyReport = lazy(() => import('@/views/coachingtooldashboard/GoalProficiencyReport'));
|
||||||
const AdherencePobDriveReport = lazy(() => import('@/views/adherencepobdrivereport/Report'));
|
const AdherencePobDriveReport = lazy(() => import('@/views/adherencepobdrivereport/Report'));
|
||||||
const IvaReportPage = lazy(() => import('@/views/ivareport/ReportPage'));
|
const IvaReportPage = lazy(() => import('@/views/ivareport/ReportPage'));
|
||||||
|
const HorizonFieldWorkReport = lazy(() => import('@/views/horizonreport/Report'));
|
||||||
|
|
||||||
function moduleRoutes(config) {
|
function moduleRoutes(config) {
|
||||||
const routes = [
|
const routes = [
|
||||||
|
|
@ -244,6 +245,7 @@ export const routes = [
|
||||||
{ path: '/ivareport/kamwise', element: <IvaReportPage key="kamwise" tab="kamwise" /> },
|
{ path: '/ivareport/kamwise', element: <IvaReportPage key="kamwise" tab="kamwise" /> },
|
||||||
{ path: '/ivareport/drbrandwise', element: <IvaReportPage key="drbrandwise" tab="drbrandwise" /> },
|
{ path: '/ivareport/drbrandwise', element: <IvaReportPage key="drbrandwise" tab="drbrandwise" /> },
|
||||||
{ path: '/ivareport/skuwise', element: <IvaReportPage key="skuwise" tab="skuwise" /> },
|
{ path: '/ivareport/skuwise', element: <IvaReportPage key="skuwise" tab="skuwise" /> },
|
||||||
|
{ path: '/horizonreport/fieldwork', element: <HorizonFieldWorkReport /> },
|
||||||
{ path: '/coming-soon', element: <ComingSoon /> },
|
{ path: '/coming-soon', element: <ComingSoon /> },
|
||||||
{ path: '*', element: <ComingSoon /> },
|
{ path: '*', element: <ComingSoon /> },
|
||||||
],
|
],
|
||||||
|
|
|
||||||
|
|
@ -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 (
|
||||||
|
<Container fluid>
|
||||||
|
<PageMetaData title={TITLE} />
|
||||||
|
<div className="card mt-3">
|
||||||
|
<div className="card-body">
|
||||||
|
<h4 className="page-title">{TITLE}</h4>
|
||||||
|
<div className="alert alert-danger mb-0" role="alert">You do not have access to this report.</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Container fluid>
|
||||||
|
<PageMetaData title={TITLE} />
|
||||||
|
<div className="card mt-3">
|
||||||
|
<div className="card-body">
|
||||||
|
<h4 className="page-title mb-0">{TITLE}</h4>
|
||||||
|
{error ? <div className="alert alert-danger mt-3" role="alert">{error}</div> : null}
|
||||||
|
<Form onSubmit={handleSubmit} className="row g-2 align-items-end mt-2">
|
||||||
|
<div className="col-md-4">
|
||||||
|
<Form.Label htmlFor="horizon-field-month">Month</Form.Label>
|
||||||
|
<Form.Select id="horizon-field-month" value={selected} onChange={(event) => setSelected(event.target.value)}>
|
||||||
|
{months.map((option) => (
|
||||||
|
<option key={option} value={option}>{option}</option>
|
||||||
|
))}
|
||||||
|
</Form.Select>
|
||||||
|
</div>
|
||||||
|
<div className="col-auto">
|
||||||
|
<Button type="submit" variant="primary" disabled={loading}>Show report</Button>
|
||||||
|
</div>
|
||||||
|
{!loading && rows.length ? (
|
||||||
|
<div className="col-auto">
|
||||||
|
<Button variant="outline-primary" onClick={handleExport} disabled={exporting}>
|
||||||
|
{exporting ? 'Exporting...' : 'Download Excel'}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
</Form>
|
||||||
|
{loading ? <p className="text-muted fw-bold mt-2 mb-0">Loading the report wait.....</p> : null}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{!loading && report ? (
|
||||||
|
<div className="card">
|
||||||
|
<div className="card-body">
|
||||||
|
<div className="table-responsive" style={{ maxHeight: '70vh' }}>
|
||||||
|
<table className="table table-bordered table-sm mb-0 text-nowrap">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
{IDENTITY.map((label) => (
|
||||||
|
<th key={label} rowSpan={2} className="align-middle text-center">{label}</th>
|
||||||
|
))}
|
||||||
|
{days.map((day) => (
|
||||||
|
<th key={day} colSpan={2} className="text-center">{dayLabel(day)}</th>
|
||||||
|
))}
|
||||||
|
<th rowSpan={2} className="align-middle text-center">RBM/ZBM wise Total</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
{days.map((day) => (
|
||||||
|
<Fragment key={day}>
|
||||||
|
<th className="text-center">Link</th>
|
||||||
|
<th className="text-center">Time</th>
|
||||||
|
</Fragment>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{rows.length ? rows.map((row) => (
|
||||||
|
<tr key={`${row.employee_code}-${row.position_name}-${row.name}`}>
|
||||||
|
<td>{row.name}</td>
|
||||||
|
<td>{row.employee_code}</td>
|
||||||
|
<td>{row.role_name}</td>
|
||||||
|
<td>{row.position_name}</td>
|
||||||
|
<td>{row.manager_1_code}</td>
|
||||||
|
<td>{row.manager_1_name}</td>
|
||||||
|
<td>{row.manager_2_code}</td>
|
||||||
|
<td>{row.manager_2_name}</td>
|
||||||
|
{days.map((day) => {
|
||||||
|
const cell = row.days?.[day] || { status: 'blank' };
|
||||||
|
if (cell.status === 'link' && cell.link) {
|
||||||
|
return (
|
||||||
|
<Fragment key={day}>
|
||||||
|
<td className="text-center">
|
||||||
|
<a href={cell.link} target="_blank" rel="noreferrer">Link</a>
|
||||||
|
</td>
|
||||||
|
<td className="text-center">{cell.time}</td>
|
||||||
|
</Fragment>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (cell.status === 'no') {
|
||||||
|
return <td key={day} colSpan={2} className="text-center">No</td>;
|
||||||
|
}
|
||||||
|
return <td key={day} colSpan={2} />;
|
||||||
|
})}
|
||||||
|
<td className="text-center">{row.total ?? 0}</td>
|
||||||
|
</tr>
|
||||||
|
)) : null}
|
||||||
|
{rows.length ? (
|
||||||
|
<tr className="fw-semibold">
|
||||||
|
<td colSpan={IDENTITY.length + days.length * 2}>Total</td>
|
||||||
|
<td className="text-center">{rows.reduce((sum, row) => sum + Number(row.total || 0), 0)}</td>
|
||||||
|
</tr>
|
||||||
|
) : (
|
||||||
|
<tr>
|
||||||
|
<td colSpan={columnCount}>No rows for this month.</td>
|
||||||
|
</tr>
|
||||||
|
)}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue