import { useMemo, useRef, useState } from 'react'; import { useMutation } from '@tanstack/react-query'; import { Alert, Box, Button, Dialog, DialogActions, DialogContent, DialogTitle, MenuItem, Stack, TextField, Typography } from '@mui/material'; import UploadFileIcon from '@mui/icons-material/UploadFile'; import { useAuth } from '../../auth/AuthContext'; import { rpc, ApiError, type Envelope } from '../../api/client'; import { uploadAttachment } from '../../api/attachments'; import { useLabel, useT, useUi } from '../../i18n'; import { useNotify } from '../../notify/NotifyProvider'; import { DateField } from '../../components/DateField'; import { resolveFields } from '../forms/fields'; import type { Option } from '../../api/entity'; /** * Legacy "Upload file" popup of the reconciliation screens: one file, uploaded first to the server's temporary folder, * then handed to RECONCILIATION_UPLOAD_FILE. The point-of-sale reconciliation also asks for the date and industrial estate. */ export function ReconciliationUpload({ entity, onClose, onDone }: { entity: 'RECONCILIATION' | 'RECONCILIATION_POS'; onClose: () => void; onDone: () => void }) { const { config } = useAuth(); const t = useT(); const lab = useLabel(); const ui = useUi(); const notify = useNotify(); const pos = entity === 'RECONCILIATION_POS'; const input = useRef(null); const [file, setFile] = useState(null); const [date, setDate] = useState(''); const [location, setLocation] = useState(''); const [touched, setTouched] = useState(false); const [fileError, setFileError] = useState(''); const fields = useMemo( () => (config && pos ? resolveFields(config, entity, 'CREATE', undefined, ['RECONCILE_DATE', 'LOCATION_IDS']) : []), [config, entity, pos], ); // the point-of-sale form has no estate field of its own in the role's metadata: use the estate list the other screens carry const locOptions = useMemo( () => (config?.ROLE_FIELD_STATUS_AUTHORITY_VIEW as unknown as { FIELD_NAME: string; DDL_VALUES?: Option[] }[] | undefined)?.find((r) => r.FIELD_NAME === 'LOCATION_IDS' && r.DDL_VALUES?.length)?.DDL_VALUES ?? [], [config], ); const dateField = fields.find((f) => f.name === 'RECONCILE_DATE'); const upload = useMutation({ meta: { loading: 'processing' }, mutationFn: async () => { const serverFile = await uploadAttachment(file!); const sent = [{ file: serverFile, name: file!.name }]; const args = pos ? { RECONCILIATION_POSEXCEL: { RECONCILIATION_POSEXCEL: sent, RECONCILE_DATE: date.replace(/\D/g, ''), LOCATION_IDS: location } } : { reconciliationExcel: { reconciliationExcel: sent } }; const r = await rpc(entity, 'RECONCILIATION_UPLOAD_FILE', args, { post: true }); // a rejected file can come back with an empty MESSAGE: fall through to the code, then to a plain "wrong file" text if (String(r.CODE) !== '000') throw new ApiError(String(r.MESSAGE || r.MESSAGECODE || (typeof r.DATA === 'string' ? r.DATA : '') || 'WRONG_FILE_UPLOADED'), r.CODE, r.DATA); return r; }, onSuccess: (r) => { notify.success(String(r.MESSAGE ?? 'SUCCESSFUL.OPERATION'), ui('saved')); onDone(); }, onError: (e) => notify.warning('WRONG_FILE_UPLOADED', failText(e as Error)), }); // never an empty alert: an unknown / blank message reads as "wrong file" const failText = (e: Error) => { const m = (e.message ?? '').trim(); const shown = m ? t(m) : ''; return shown && !/^[A-Z0-9_.]+$/.test(shown) ? shown : ui('wrongFileType'); }; const missing =!file || (pos && (!date || !location)); return ( {t('UPLOAD_GENERAL_FILE')} {pos && ( <> setLocation(e.target.value)} error={touched && !location} fullWidth > {locOptions.map((o) => ( {o.NAME || t(o.CAPTION_CODE ?? String(o.CODE))} ))} )} { const f = e.target.files?.[0] ?? null; e.target.value = ''; if (f && !/\.(csv|xls|xlsx)$/i.test(f.name)) { setFile(null); setFileError(ui('wrongFileType')); notify.warning('WRONG_FILE_TYPE_ONLY_CSV_XLS', ui('wrongFileType')); return; } setFileError(''); setFile(f); }} /> input.current?.click()} sx={{ border: 2, borderStyle: 'dashed', borderColor: touched && !file ? 'error.main' : 'divider', borderRadius: 2, p: 3, textAlign: 'center', cursor: 'pointer' }} > {file ? file.name : t('MUST_UPLOAD_FILE')} {fileError && {fileError}} {upload.error && {failText(upload.error as Error)}} ); }