// The day summary (plan step 6.2), opened from the overview as "Day stats" (to look) or "Close // the day" (adds the cash count and the checks). Closing: count the drawer (optional), then // the checks: tables still open (with unpaid lines or not) and shifts still running. With open // tables the close is forced: they are closed, unpaid amounts are lost, running shifts end. import { useState } from 'react' import toast from 'react-hot-toast' import { errorMessage } from '@xenia/api' import { parseNumber, useFormat, useTranslation } from '@xenia/i18n' import { Button, Dialog, EmptyState, Spinner, Tabs } from '@xenia/ui' import client from '../../api/client' import { useLiveQuery } from '../../live/useLiveQuery' import { CashTab, ChecksTab, MoneyTab, PrintersTab, StaffTab, ZonesTab } from './DaySummaryTabs' export default function DaySummaryDialog({ dayId, closing = false, onClose, onClosed }) { const { t } = useTranslation('manager') const { formatDateTime } = useFormat() const [tab, setTab] = useState('money') const [counted, setCounted] = useState('') const [busy, setBusy] = useState(false) const { data, isPending, error } = useLiveQuery({ queryKey: ['day-summary', dayId ?? 'current'], queryFn: () => client.get('/api/business-day/summary', { params: dayId ? { day_id: dayId } : {} }).then(r => r.data), events: closing ? ['order_updated', 'order_paid', 'order_closed'] : [], refetchInterval: closing ? 15_000 : false, }) const countedValue = counted.trim() === '' ? null : parseNumber(counted) async function closeDay() { setBusy(true) try { const body = { force: data.checks.open_orders > 0 } if (countedValue != null && Number.isFinite(countedValue)) body.store_closing_cash = countedValue await client.post('/api/business-day/close', body) onClosed?.() } catch (err) { if (err.xenia?.code === 'business_day.orders_open') setTab('checks') toast.error(errorMessage(err, t)) } finally { setBusy(false) } } const tabs = [ { id: 'money', label: t('overview.day.tab.money'), testId: 'day-tab-money' }, { id: 'staff', label: t('overview.day.tab.staff'), badge: data?.waiters?.length, testId: 'day-tab-staff' }, { id: 'zones', label: t('overview.day.tab.zones'), badge: data?.zones?.length, testId: 'day-tab-zones' }, { id: 'printers', label: t('overview.day.tab.printers'), badge: data?.printers?.length, testId: 'day-tab-printers' }, ...(closing ? [ { id: 'cash', label: t('overview.day.tab.cash'), testId: 'day-tab-cash' }, { id: 'checks', label: t('overview.day.tab.checks'), badge: data?.checks ? data.checks.open_orders + data.checks.active_shifts : null, warn: data?.checks?.with_unpaid_items > 0, testId: 'day-tab-checks' }, ] : []), ] const footer = closing && data && tab !== 'checks' && (<> {t('overview.back')} {tab !== 'cash' ? setTab('cash')} data-testid="day-to-cash">{t('overview.day.toCash')} : setTab('checks')} data-testid="day-to-checks">{t('overview.day.toChecks')}} >) return ( {data.status === 'open' ? {t('overview.day.open')} : {t('overview.day.closedState')}} ยท {t('overview.day.openedAt', { time: formatDateTime(data.opened_at, 'datetimeShort') })} }> {isPending && } {error && } {data && (<> {tab === 'money' && } {tab === 'staff' && } {tab === 'zones' && } {tab === 'printers' && } {tab === 'cash' && } {tab === 'checks' && } >)} ) }