diff --git a/client/src/App.jsx b/client/src/App.jsx index a15ba9d8..2d4414a0 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -66,11 +66,13 @@ import { isStudentView, isSeminarAttendanceYemot, isAttendanceCleanupRules, + isAttendanceSheetImport, } from 'src/utils/appPermissions'; import ScannerUpload from '@shared/components/views/ScannerUpload'; import InLessonReport from 'src/reports/InLessonReport'; import MichlolFileHelper from '@shared/components/views/MichlolFileHelper'; import ApprovedAbsencesUpload from 'src/components/ApprovedAbsencesUpload'; +import AttendanceSheetImport from 'src/components/AttendanceSheetImport'; import BadgeIcon from '@mui/icons-material/BadgeOutlined'; import SupervisedUserCircleIcon from '@mui/icons-material/SupervisedUserCircleOutlined'; @@ -345,6 +347,9 @@ const App = () => ( } /> } /> } /> + {isAttendanceSheetImport(permissions) && ( + } /> + )} {CommonRoutes({ permissions, roadmapFeatures, settingsPage: })} diff --git a/client/src/GeneralLayout.jsx b/client/src/GeneralLayout.jsx index c36ee0d4..b495b45b 100644 --- a/client/src/GeneralLayout.jsx +++ b/client/src/GeneralLayout.jsx @@ -18,6 +18,7 @@ import ViewListIcon from '@mui/icons-material/ViewListOutlined'; import GridOnIcon from '@mui/icons-material/GridOnOutlined'; import ContactMailIcon from '@mui/icons-material/ContactMailOutlined'; import TrackChangesIcon from '@mui/icons-material/TrackChangesOutlined'; +import TableViewIcon from '@mui/icons-material/TableViewOutlined'; import BaseLayout from '@shared/components/layout/Layout'; import BaseDashboard from '@shared/components/views/Dashboard'; @@ -29,6 +30,7 @@ import { isTeacherView, isStudentView, isStudentAttendanceByKlass, + isAttendanceSheetImport, } from './utils/appPermissions'; import { useDashboardItems } from './settings/settingsUtil'; @@ -121,6 +123,15 @@ const menuGroups = [ primaryText="העלאת חיסורים מאושרים" leftIcon={} />, + ({ permissions }) => + isAttendanceSheetImport(permissions) && ( + } + /> + ), ], }, ({ permissions }) => diff --git a/client/src/components/AttendanceSheetImport.jsx b/client/src/components/AttendanceSheetImport.jsx new file mode 100644 index 00000000..25b8c533 --- /dev/null +++ b/client/src/components/AttendanceSheetImport.jsx @@ -0,0 +1,377 @@ +import { useCallback, useMemo, useRef, useState } from 'react'; +import Container from '@mui/material/Container'; +import Paper from '@mui/material/Paper'; +import Stack from '@mui/material/Stack'; +import Box from '@mui/material/Box'; +import Divider from '@mui/material/Divider'; +import Typography from '@mui/material/Typography'; +import Autocomplete from '@mui/material/Autocomplete'; +import TextField from '@mui/material/TextField'; +import Alert from '@mui/material/Alert'; +import Table from '@mui/material/Table'; +import TableBody from '@mui/material/TableBody'; +import TableCell from '@mui/material/TableCell'; +import TableHead from '@mui/material/TableHead'; +import TableRow from '@mui/material/TableRow'; +import CircularProgress from '@mui/material/CircularProgress'; +import Dialog from '@mui/material/Dialog'; +import DialogTitle from '@mui/material/DialogTitle'; +import DialogContent from '@mui/material/DialogContent'; +import DialogActions from '@mui/material/DialogActions'; +import LinearProgress from '@mui/material/LinearProgress'; +import FileDownloadIcon from '@mui/icons-material/FileDownload'; +import FileUploadIcon from '@mui/icons-material/FileUpload'; +import { Button, useDataProvider, useGetList, useNotify, useTranslate } from 'react-admin'; +import { handleError } from '@shared/utils/notifyUtil'; +import { readAsDataURL } from '@shared/utils/fileUtil'; +import { getCurrentHebrewYear } from '@shared/utils/yearFilter'; + +const listParams = { + pagination: { page: 1, perPage: 1000 }, + sort: { field: 'key', order: 'ASC' }, + filter: { year: getCurrentHebrewYear() }, +}; + +const PREVIEW_ROWS_LIMIT = 200; + +const getOptionLabel = (option) => `${option.key} - ${option.name}`; + +const isLessonOfKlass = (lesson, klass) => + Boolean(klass) && (lesson.klassReferenceIds ?? []).map(String).includes(String(klass.id)); + +const IssuesTable = ({ issues }) => { + const translate = useTranslate(); + return ( + + + + {translate('attendance_sheet.sheet')} + {translate('attendance_sheet.cell')} + {translate('attendance_sheet.reason')} + + + + {issues.map((issue, index) => ( + + {issue.sheet} + {issue.cell} + {issue.message} + + ))} + +
+ ); +}; + +const SummaryTable = ({ details }) => { + const translate = useTranslate(); + return ( + + + + {translate('attendance_sheet.sheet')} + {translate('attendance_sheet.teacher')} + {translate('attendance_sheet.lesson')} + {translate('attendance_sheet.created')} + {translate('attendance_sheet.updated')} + + + + {details.map((row, index) => ( + + {row.sheet} + {row.teacherName} + {`${row.lessonKey} - ${row.lessonName}`} + {row.created} + {row.updated} + + ))} + +
+ ); +}; + +const Warnings = ({ warnings }) => { + const translate = useTranslate(); + if (!warnings.length) return null; + return ( + <> + {translate('attendance_sheet.warnings_title', { count: warnings.length })} + + + ); +}; + +const PreviewRowsTable = ({ rows }) => { + const translate = useTranslate(); + return ( + <> + {rows.length > PREVIEW_ROWS_LIMIT && ( + + {translate('attendance_sheet.rows_limited', { shown: PREVIEW_ROWS_LIMIT, total: rows.length })} + + )} + + + + {translate('attendance_sheet.sheet')} + {translate('attendance_sheet.cell')} + {translate('attendance_sheet.student')} + {translate('attendance_sheet.teacher')} + {translate('attendance_sheet.lesson')} + {translate('attendance_sheet.date')} + {translate('attendance_sheet.how_many_lessons')} + {translate('attendance_sheet.abs_count')} + {translate('attendance_sheet.action')} + + + + {rows.slice(0, PREVIEW_ROWS_LIMIT).map((row, index) => ( + + {row.sheet} + {row.cell} + {row.studentName} + {row.teacherName} + {row.lessonName} + {row.reportDate.split('-').reverse().join('/')} + {row.howManyLessons} + {row.absCount} + + {translate(row.isUpdate ? 'attendance_sheet.row_update' : 'attendance_sheet.row_create')} + + + ))} + +
+ + ); +}; + +const PreviewDialog = ({ preview, saving, onCancel, onSave }) => { + const translate = useTranslate(); + return ( + + {translate('attendance_sheet.preview_title', { fileName: preview.fileName })} + + {saving && } + + {preview.result.success ? ( + <> + + {translate('attendance_sheet.preview_summary', { + created: preview.result.created, + updated: preview.result.updated, + })} + + + + + + ) : ( + <> + + {translate('attendance_sheet.errors_title', { count: preview.result.errors.length })} + + + + )} + + + + + ); +}; + +const ImportResult = ({ result }) => { + const translate = useTranslate(); + return ( + + + {translate('attendance_sheet.success', { created: result.created, updated: result.updated })} + + {result.details.length > 0 && } + + + ); +}; + +const AttendanceSheetImport = () => { + const translate = useTranslate(); + const dataProvider = useDataProvider(); + const notify = useNotify(); + const fileSelector = useRef(); + const [klass, setKlass] = useState(null); + const [lessons, setLessons] = useState([]); + const [loading, setLoading] = useState(false); + const [preview, setPreview] = useState(null); + const [result, setResult] = useState(null); + + const { data: klasses = [] } = useGetList('klass', listParams); + const { data: allLessons = [] } = useGetList('lesson', listParams); + const lessonOptions = useMemo( + () => + allLessons + .map((lesson) => ({ ...lesson, isKlassLesson: isLessonOfKlass(lesson, klass) })) + .sort((a, b) => Number(b.isKlassLesson) - Number(a.isKlassLesson)), + [allLessons, klass] + ); + + const handleDownload = useCallback(() => { + const queryParams = { + 'extra.klassId': klass.id, + 'extra.lessonIds': lessons.map((lesson) => lesson.id).join(','), + }; + setLoading(true); + dataProvider + .actionAndDownload('att_report', 'attendanceSheetTemplate', queryParams) + .catch(handleError(notify)) + .finally(() => setLoading(false)); + }, [dataProvider, notify, klass, lessons]); + + const handleFileSelect = useCallback((e) => { + e.preventDefault(); + fileSelector.current?.click(); + }, []); + + const importFile = useCallback( + (file, dryRun) => + dataProvider + .action('att_report', 'attendanceSheetImport', dryRun ? { 'extra.dryRun': true } : {}, file) + .then((response) => response.json), + [dataProvider] + ); + + const handleFileUpload = useCallback( + async (e) => { + const [selectedFile] = e.target.files ?? []; + e.target.value = null; + if (!selectedFile) return; + setLoading(true); + setResult(null); + try { + const dataUrl = await readAsDataURL(selectedFile); + const file = { fileName: selectedFile.name, fileData: dataUrl.split(',')[1] }; + setPreview({ fileName: file.fileName, file, result: await importFile(file, true) }); + } catch (error) { + handleError(notify)(error); + } finally { + setLoading(false); + } + }, + [importFile, notify] + ); + + const handleSave = useCallback(async () => { + setLoading(true); + try { + const saved = await importFile(preview.file, false); + if (saved.success) { + setPreview(null); + setResult(saved); + } else { + setPreview({ ...preview, result: saved }); + } + } catch (error) { + handleError(notify)(error); + } finally { + setLoading(false); + } + }, [importFile, notify, preview]); + + return ( + + + + + + {translate('attendance_sheet.title')} + + + {translate('attendance_sheet.description')} + + + + + + {translate('attendance_sheet.download_title')} + setKlass(value)} + renderInput={(params) => ( + + )} + /> + + translate( + option.isKlassLesson + ? 'attendance_sheet.klass_lessons' + : 'attendance_sheet.other_lessons' + ) + } + getOptionLabel={getOptionLabel} + isOptionEqualToValue={(option, value) => option.id === value.id} + value={lessons} + onChange={(e, value) => setLessons(value)} + renderInput={(params) => ( + + )} + /> + +