import React, { useCallback, useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { Button, DataTable, DataTableSkeleton, InlineLoading, Pagination, Table, TableBody, TableCell, TableContainer, TableExpandedRow, TableExpandHeader, TableExpandRow, TableHead, TableHeader, TableRow, TableToolbar, TableToolbarAction, TableToolbarContent, TableToolbarMenu, TableToolbarSearch, Tile, } from '@carbon/react'; import { CheckmarkOutline, Copy, Download, IncompleteCancel, MisuseOutline, View, WarningAltFilled, } from '@carbon/react/icons'; import { isDesktop, restBaseUrl, useSession } from '@openmrs/esm-framework'; import { useGetReports } from '../stock-reports.resource'; import { URL_BATCH_JOB_ARTIFACT, APP_STOCKMANAGEMENT_REPORTS_VIEW, TASK_STOCKMANAGEMENT_REPORTS_MUTATE, } from '../../constants'; import { formatDisplayDateTime } from '../../core/utils/datetimeUtils'; import { BatchJobStatusCancelled, BatchJobStatusCompleted, BatchJobStatusExpired, BatchJobStatusFailed, BatchJobStatusPending, } from '../../core/api/types/BatchJob'; import { handleMutate } from '../../utils'; import { PrivilegedView } from '../../core/components/privileged-view-component/privileged-view.component'; import NewReportActionButton from './new-report-button.component'; import StockReportStatus from './stock-report-status.component'; import StockReportParameters from './stock-report-parameters.component'; import styles from './stock-reports.scss'; const StockReports: React.FC = () => { const { t } = useTranslation(); const handleRefresh = () => { handleMutate(`${restBaseUrl}/stockmanagement/batchjob?batchJobType=Report&v=default`); }; const { reports, isLoading, currentPage, pageSizes, totalItems, goTo, currentPageSize, setPageSize } = useGetReports(); const { user } = useSession(); const canViewReports = user.privileges.filter((privilage) => privilage.display === APP_STOCKMANAGEMENT_REPORTS_VIEW).length > 0; const canCreateReport = user.privileges.filter((privilage) => privilage.display === TASK_STOCKMANAGEMENT_REPORTS_MUTATE).length > 0; const tableHeaders = useMemo( () => [ { id: 0, header: t('report', 'Report'), key: 'report', }, { id: 1, header: t('parameters', 'Parameters'), key: 'parameters', }, { id: 2, header: t('dateRequested', 'Date Requested'), key: 'dateRequested', }, { id: 3, header: t('requestedBy', 'Requested By'), key: 'requestedBy', }, { id: 4, header: t('status', 'Status'), key: 'status', }, { id: 5, header: t('timeTaken', 'Time Taken'), key: 'timeTaken', }, { id: 8, header: t('actions', 'Actions'), key: 'actions', }, ], [t], ); const onDownloadReportClick = useCallback((uuid: string, fileExit: string | undefined | null) => { if (uuid) { window.open(URL_BATCH_JOB_ARTIFACT(uuid, true), '_blank'); } }, []); const tableRows = useMemo(() => { return reports?.map((batchJob, index) => ({ ...batchJob, checkbox: 'isBatchJobActive', id: batchJob?.uuid, key: `key-${batchJob?.uuid}`, uuid: `${batchJob?.uuid}`, batchJobType: batchJob?.batchJobType, dateRequested: formatDisplayDateTime(batchJob.dateCreated), parameters: , report: batchJob?.description, requestedBy: batchJob?.owners?.map((p, index) => (
{`${p.ownerFamilyName} ${p.ownerGivenName}`}
)), status: ( <> {batchJob.status === BatchJobStatusPending ? ( ) : null} {batchJob.status === BatchJobStatusFailed && ( )} {batchJob.status === BatchJobStatusCancelled && ( )} {batchJob.status === BatchJobStatusCompleted && ( )} {batchJob.status === BatchJobStatusExpired && ( )} ), timeTaken: formatDuration(batchJob.dateCreated, batchJob.endTime), actions: (
{batchJob.outputArtifactViewable && batchJob.batchJobType === 'hide' && (
), })); }, [reports, onDownloadReportClick, t]); if (isLoading) { return ; } return (

{t('stockReportsTableHeader', 'List of reports requested by users.')}

{({ rows, headers, getHeaderProps, getTableProps, getRowProps, onInputChange }) => ( {t('refresh', 'Refresh')} {canCreateReport && } {headers.map((header: any) => ( {header.header?.content ?? header.header} ))} {rows.map((row: any, index) => { return ( {row.cells.map((cell: any) => ( {cell.value} ))} {row.isExpanded ? ( ) : ( )} ); })}
{!canViewReports ? ( ) : rows.length === 0 ? (

{t('noReportsToDisplay', 'No Stock reports to display')}

{t('checkFilters', 'Check the filters above')}

) : null}
)}
{ if (pageSize !== currentPageSize) { setPageSize(pageSize); } if (page !== currentPage) { goTo(page); } }} className={styles.paginationOverride} />
); }; const formatDuration = (start: string, end: string) => { const startDate = new Date(start).getTime(); const endDate = new Date(end).getTime(); if (!startDate || !endDate) return ''; const durationInMillis = endDate - startDate; const totalSeconds = Math.floor(durationInMillis / 1000); const hours = Math.floor(totalSeconds / 3600); const minutes = Math.floor((totalSeconds % 3600) / 60); const seconds = totalSeconds % 60; return [hours, minutes, seconds].map((unit) => String(unit).padStart(2, '0')).join(':'); }; export default StockReports;