Owner 2026-11-23 (plan 6F.1b): 26 new tips in English and Greek: Operation (own shifts, hidden takings, card payments, closing tables, the serve step, courses, quick notes), Sign-in (waiter and device modes, usernames, one session, idle sign-out), venue language and time zone, the fiscal device, locking, the server's address, the product editor (unit, available, quick add, VAT group, cost), a device's sign-in, a person's role, the cash counted at a shift's end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
233 lines
14 KiB
React
233 lines
14 KiB
React
// A waiter's shift, from the overview's shift list (plan step 6.2): its money (what should be in
|
||
// the drawer: the starting cash + cash taken), the cash / card split, sales per hour and counts,
|
||
// and every line of the shift with who ordered and who took the money (filters). In "close"
|
||
// mode it ends the shift: notes, then the cash the waiter hands over, compared with what was
|
||
// expected (short / over / even), then "End the shift".
|
||
import { useState } from 'react'
|
||
import { useTranslation, useFormat, parseNumber } from '@xenia/i18n'
|
||
import { Button, Dialog, Field, InfoTip, Input, Spinner, Textarea, cx } from '@xenia/ui'
|
||
import client from '../../api/client'
|
||
import { useLiveQuery } from '../../live/useLiveQuery'
|
||
import { ITEM_KINDS, cashCheck, hourBuckets, itemKind, itemShown, quickAmounts, shiftCounts, shiftMoney, shiftPairs } from './overviewLogic'
|
||
import { Avatar, Figure, HourBars, Money, Pill, SplitBar } from './parts'
|
||
import { SOFT } from './tones'
|
||
import { useDuration, useHourOf } from './hooks'
|
||
|
||
const KIND_TONE = { both: 'success', paid: 'info', ordered: 'warning', unpaid: 'warning', cancelled: 'danger', anomaly: 'danger' }
|
||
const CHECK_TONE = { even: 'success', over: 'success', short: 'danger' }
|
||
|
||
export default function ShiftDialog({ shift, mode, onClose, onConfirm, busy }) {
|
||
const { t } = useTranslation('manager')
|
||
const { formatTime, formatMoney } = useFormat()
|
||
const since = useDuration()
|
||
const hourOf = useHourOf()
|
||
const [screen, setScreen] = useState('overview') // overview | lines
|
||
const [step, setStep] = useState(1) // close mode: 1 review + notes, 2 cash handover
|
||
const [notes, setNotes] = useState('')
|
||
const [counted, setCounted] = useState('')
|
||
const [kind, setKind] = useState('all')
|
||
const [payment, setPayment] = useState('any')
|
||
|
||
const { data: summary, isPending } = useLiveQuery({
|
||
queryKey: ['shift-summary', shift.id],
|
||
queryFn: () => client.get(`/api/shifts/${shift.id}/summary`).then(r => r.data),
|
||
events: ['order_paid', 'order_updated', 'tabs.changed'],
|
||
})
|
||
const data = summary || shift
|
||
const money = shiftMoney(data)
|
||
const counts = shiftCounts(summary)
|
||
const countedValue = counted === '' ? null : parseNumber(counted)
|
||
const check = cashCheck(countedValue, money.expected)
|
||
const waiterId = shift.waiter_id ?? summary?.waiter_id
|
||
const closing = mode === 'close'
|
||
|
||
const footer = !closing ? (<>
|
||
<span className="flex-1 text-caption text-ink-3">{t('overview.shift.live')}</span>
|
||
<Button variant="secondary" onClick={onClose}>{t('overview.close')}</Button>
|
||
</>) : step === 1 ? (<>
|
||
<Button variant="secondary" onClick={onClose}>{t('overview.back')}</Button>
|
||
<span className="flex-1 text-caption text-ink-3">{t('overview.shift.checkFirst')}</span>
|
||
<Button onClick={() => { setScreen('overview'); setStep(2) }} data-testid="shift-next">{t('overview.shift.toHandover')}</Button>
|
||
</>) : (<>
|
||
<Button variant="secondary" onClick={() => setStep(1)}>{t('overview.back')}</Button>
|
||
<span className="flex-1" />
|
||
<Button variant="danger" loading={busy} data-testid="shift-end-confirm"
|
||
onClick={() => onConfirm(notes, countedValue != null && Number.isFinite(countedValue) ? countedValue : null)}>{t('overview.shift.endNow')}</Button>
|
||
</>)
|
||
|
||
return (
|
||
<Dialog open size="lg" onClose={onClose} footer={footer}
|
||
title={<span className="flex items-center gap-3">
|
||
<Avatar name={shift.waiter_name} url={shift.avatar_url} size="lg" />
|
||
<span className="grid">
|
||
<span className="flex items-center gap-2">{shift.waiter_name}
|
||
<Pill tone={closing ? 'warning' : 'success'}>{closing ? t('overview.shift.closing') : t('overview.shift.running')}</Pill>
|
||
</span>
|
||
<span className="text-caption font-normal text-ink-3">{t('overview.shift.number', { id: shift.id })}</span>
|
||
</span>
|
||
</span>}>
|
||
<div className="grid gap-4" data-testid="shift-dialog">
|
||
<div className="flex flex-wrap items-center gap-4">
|
||
<Figure label={t('overview.shift.start')}>{formatTime(shift.started_at)}</Figure>
|
||
<Figure label={t('overview.shift.length')}>{since(shift.started_at)}</Figure>
|
||
<span className="flex-1" />
|
||
<div className="flex gap-1" role="tablist">
|
||
{['overview', 'lines'].map(s => (
|
||
<Button key={s} size="sm" variant={screen === s ? 'primary' : 'secondary'} role="tab" aria-selected={screen === s}
|
||
onClick={() => setScreen(s)} data-testid={`shift-tab-${s}`}>{t(`overview.shift.tab.${s}`)}</Button>
|
||
))}
|
||
</div>
|
||
</div>
|
||
|
||
{screen === 'overview' && (<>
|
||
<div className={cx('rounded-card border p-4 flex flex-wrap items-center justify-between gap-3', SOFT.success, 'border-success')}>
|
||
<div className="grid">
|
||
<span className="text-overline font-semibold uppercase tracking-wide">{t('overview.shift.expected')}</span>
|
||
<Money value={money.expected} className="text-display font-bold text-ink" data-testid="shift-expected" />
|
||
</div>
|
||
<div className="text-label text-right">
|
||
{t('overview.shift.startCash')} <Money value={money.start} className="font-semibold text-ink" /><br />
|
||
+ {t('overview.shift.cashSales')} <Money value={money.cash} className="font-semibold text-ink" />
|
||
{money.tabCash > 0 && <><br />+ {t('overview.shift.tabCash')} <Money value={money.tabCash} className="font-semibold text-ink" /></>}
|
||
{money.refunded > 0 && <><br />− {t('overview.shift.refunded')} <Money value={money.refunded} className="font-semibold text-ink" /></>}
|
||
</div>
|
||
</div>
|
||
<div className="grid grid-cols-3 gap-3">
|
||
<Figure label={t('overview.shift.startCash')} sub={t('overview.shift.startCashHint')}><Money value={money.start} /></Figure>
|
||
<Figure label={t('overview.shift.cashSales')} sub={t('overview.shift.cashSalesHint')}><Money value={money.cash} /></Figure>
|
||
<Figure label={t('overview.shift.turnover')} sub={t('overview.shift.turnoverHint')}><Money value={money.total} /></Figure>
|
||
</div>
|
||
{money.total > 0 && <SplitBar parts={[
|
||
{ key: 'cash', tone: 'success', label: t('overview.pay.cash'), value: money.cash },
|
||
{ key: 'card', tone: 'info', label: t('overview.pay.card'), value: money.card },
|
||
{ key: 'other', tone: 'neutral', label: t('overview.pay.other'), value: money.other },
|
||
{ key: 'tab', tone: 'accent', label: t('overview.shift.tabPayments'), value: money.tab },
|
||
].filter(p => p.value > 0)} />}
|
||
{data.activity && <ShiftActivity a={data.activity} money={money} />}
|
||
{summary && shiftPairs(summary).length > 0 && (
|
||
<div className="grid gap-2">
|
||
<span className="text-overline font-semibold uppercase tracking-wide text-ink-3">{t('overview.shift.perHour')}</span>
|
||
<HourBars buckets={hourBuckets(shiftPairs(summary), hourOf, { from: 24, to: -1 })} height="h-16" />
|
||
</div>
|
||
)}
|
||
<div className="grid grid-cols-4 gap-3 rounded-card border border-line p-3">
|
||
<Figure label={t('overview.shift.items')}>{counts.items}</Figure>
|
||
<Figure label={t('overview.shift.tables')}>{counts.tables}</Figure>
|
||
<Figure label={t('overview.shift.orders')}>{counts.orders}</Figure>
|
||
<Figure label={t('overview.shift.pay')} sub={data.hourly_rate_snapshot ? t('overview.shift.perHourRate', { rate: formatMoney(data.hourly_rate_snapshot) }) : null}>
|
||
{data.shift_pay != null ? <Money value={data.shift_pay} /> : '—'}
|
||
</Figure>
|
||
</div>
|
||
|
||
{closing && step === 1 && (
|
||
<Field label={t('overview.shift.notes')} hint={t('overview.shift.notesHint')}>
|
||
<Textarea rows={2} value={notes} onChange={e => setNotes(e.target.value)} />
|
||
</Field>
|
||
)}
|
||
{closing && step === 2 && (
|
||
<div className="grid gap-3">
|
||
<h3 className="text-title font-semibold text-ink">{t('overview.shift.handover')}</h3>
|
||
<Field label={t('overview.shift.counted')} info={<InfoTip>{t('overview.shift.countedTip')}</InfoTip>} hint={t('overview.shift.countedHint')}>
|
||
<Input inputMode="decimal" autoFocus value={counted} onChange={e => setCounted(e.target.value)}
|
||
placeholder={String(money.expected.toFixed(2))} data-testid="shift-counted" />
|
||
</Field>
|
||
<div className="flex flex-wrap gap-2">
|
||
{quickAmounts(money.expected).map(v => (
|
||
<Button key={v} size="sm" variant="secondary" onClick={() => setCounted(v.toFixed(2))}>
|
||
{v === money.expected ? <>{t('overview.shift.exact')} <Money value={v} /></> : <Money value={v} />}
|
||
</Button>
|
||
))}
|
||
</div>
|
||
{check && (
|
||
<div className={cx('rounded-card border p-3 flex items-center justify-between gap-3', SOFT[CHECK_TONE[check.state]])} data-testid="shift-check" data-state={check.state}>
|
||
<span className="font-semibold">{t(`overview.cash.${check.state}`)}</span>
|
||
<Money value={check.diff} className="text-title font-bold" />
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
</>)}
|
||
|
||
{screen === 'lines' && (
|
||
<div className="grid gap-3">
|
||
<div className="flex flex-wrap gap-1">
|
||
{['all', ...ITEM_KINDS].map(k => (
|
||
<Button key={k} size="sm" variant={kind === k ? 'primary' : 'secondary'} onClick={() => setKind(k)}>{t(`overview.shift.kind.${k}`)}</Button>
|
||
))}
|
||
</div>
|
||
<div className="flex flex-wrap gap-1">
|
||
{['any', 'cash', 'card'].map(p => (
|
||
<Button key={p} size="sm" variant={payment === p ? 'primary' : 'ghost'} onClick={() => setPayment(p)}>{t(`overview.pay.${p}`)}</Button>
|
||
))}
|
||
</div>
|
||
{isPending && <div className="py-8 flex justify-center"><Spinner /></div>}
|
||
{summary && summary.orders.length === 0 && <p className="text-label text-ink-3 text-center py-6">{t('overview.shift.noLines')}</p>}
|
||
{summary?.orders.map(o => <OrderLines key={o.order_id} order={o} waiterId={waiterId} kind={kind} payment={payment} />)}
|
||
</div>
|
||
)}
|
||
</div>
|
||
{isPending && screen === 'overview' && <span className="sr-only">{t('overview.loading')}</span>}
|
||
</Dialog>
|
||
)
|
||
}
|
||
|
||
function OrderLines({ order, waiterId, kind, payment }) {
|
||
const { t } = useTranslation('manager')
|
||
const { formatTime } = useFormat()
|
||
const items = order.items.filter(i => itemShown(i, waiterId, kind, payment))
|
||
if (items.length === 0) return null
|
||
const paid = items.filter(i => i.status === 'paid').reduce((s, i) => s + i.subtotal, 0)
|
||
return (
|
||
<div className="rounded-card border border-line overflow-hidden">
|
||
<div className="flex flex-wrap items-center justify-between gap-2 px-3 py-2 bg-surface-2 text-label">
|
||
<span className="font-semibold text-ink">{t('overview.shift.order', { id: order.order_id })}{order.table_name && <span className="font-normal text-ink-3"> · {order.table_name}</span>}</span>
|
||
<span className="flex items-center gap-3">
|
||
{order.opener_name && <span className="text-ink-2">{t('overview.shift.openedBy', { name: order.opener_name })}{order.opened_at && ` · ${formatTime(order.opened_at)}`}</span>}
|
||
{paid > 0 && <Money value={paid} className="font-semibold text-success-ink" />}
|
||
</span>
|
||
</div>
|
||
<ul className="divide-y divide-line">
|
||
{items.map(item => {
|
||
const k = itemKind(item, waiterId)
|
||
const cancelled = item.status === 'cancelled'
|
||
return (
|
||
<li key={item.id} className="px-3 py-2 grid gap-1" data-testid="shift-line" data-kind={k}>
|
||
<div className="flex items-baseline justify-between gap-2">
|
||
<span className={cx('text-body font-semibold', cancelled ? 'text-danger-ink' : 'text-ink')}>{item.product_name} <span className="font-normal text-ink-3">×{item.quantity}</span></span>
|
||
<Money value={item.subtotal} className={cx('font-semibold', cancelled && 'line-through text-danger-ink')} />
|
||
</div>
|
||
<div className="flex flex-wrap items-center gap-x-3 gap-y-1 text-caption text-ink-2">
|
||
<span>{t('overview.shift.orderedBy', { name: item.added_by_name ?? '—' })}</span>
|
||
{!cancelled && <span>{t('overview.shift.paidTo', { name: item.paid_by_name ?? '—' })}</span>}
|
||
{!cancelled && item.payment_method && <Pill tone={item.payment_method === 'card' ? 'info' : 'success'}>{t(`overview.pay.${item.payment_method}`, { defaultValue: item.payment_method })}</Pill>}
|
||
<Pill tone={KIND_TONE[k]}>{t(`overview.shift.kind.${k}`)}</Pill>
|
||
</div>
|
||
</li>
|
||
)
|
||
})}
|
||
</ul>
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** What happened in the shift (6.13c): made, paid, put on a tab, and the tab money received. */
|
||
function ShiftActivity({ a, money }) {
|
||
const { t } = useTranslation('manager')
|
||
const { formatMoney } = useFormat()
|
||
const cell = (label, items, value, testId, tone) => (
|
||
<Figure label={label} sub={value != null ? formatMoney(value) : null}>
|
||
<span className={tone} data-testid={testId}>{items}</span>
|
||
</Figure>
|
||
)
|
||
return (
|
||
<div className="grid grid-cols-2 sm:grid-cols-4 gap-3 rounded-card border border-line p-3" data-testid="shift-activity">
|
||
{cell(t('overview.shift.made', { count: a.orders }), a.made_items, a.made_value, 'shift-made')}
|
||
{cell(t('overview.shift.paidItems'), a.paid_items, a.paid_value, 'shift-paid', 'text-success-ink')}
|
||
{cell(t('overview.shift.onTab'), a.tab_items, a.tab_value, 'shift-on-tab', a.tab_items ? 'text-warning-ink' : undefined)}
|
||
<Figure label={t('overview.shift.tabPayments')} sub={money.tab ? t('overview.shift.tabPaymentsSub', { cash: formatMoney(money.tabCash), card: formatMoney(money.tabCard + money.tabOther) }) : null}>
|
||
<Money value={money.tab} data-testid="shift-tab-received" />
|
||
</Figure>
|
||
</div>
|
||
)
|
||
}
|