Files
xenia-pos-local/manager_dashboard/src/pages/online/OrderingSettings.jsx
T
bonaminandClaude Opus 5.5 70c8f2bb57 feat(connect): table bookings from the online ordering app; food fired to the kitchen before arrival
Plan phase 3S, step S.6.
- Pull: "reservation" / "preorder" orders become Reservations waiting for an answer
  (status "requested"; services/online_reservations.py), with the food ordered ahead kept on
  the booking (items_json at the cloud's prices, the site's price check) — no order yet.
  Migration 0020 (reservations: online_order_cloud_id, online_ref, items_json, total,
  online_price_diff, fire_at, order_id, response_note).
- Confirm → the food's fire time = booking time − fire minutes (setting); the fire worker
  (connect_orders, every 20 s) turns the food into an order at that time (needs an open
  business day; waits otherwise): kitchen tickets, KDS (reference and time). Early arrival
  fires at once. Seating moves the order to the table (refused if the table has an open
  order). Decline / cancel / no-show with a reason. Every step mirrored to the cloud.
- Found: the KDS filter dropped every online order without an online status (SQL NULL in
  NOT(a AND b)) — made NULL-safe.
- Counter: Online → Bookings (requests first, then upcoming), BookingPanel (confirm,
  decline with reasons, food and when it goes to the kitchen, seat at a free table, cancel,
  no-show); the Online badge and chime count booking requests.
- Manager: Online ordering → Table bookings settings (on, food ahead, hours, slot, notice,
  days ahead, party size, fire minutes); the dashboard's reservations card confirms /
  declines online requests; the reservations router delegates online bookings' status steps.
- Tests: tests/test_online_bookings.py (8), unit tests (terminal, manager), e2e: order app
  (book with food → the venue confirms), terminal (request → confirm → the two dishes reach
  the KDS at the fire time, not before → seated), manager (booking settings).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 19:31:44 +02:00

146 lines
8.3 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Online ordering settings (plan phase 3S, steps S.4 and S.6): which services the online
// ordering app offers (takeaway, delivery), when (ordering hours per day, or any time), how
// long before an order can be ready (preparation), the times offered (every N minutes, days
// ahead) and "as soon as possible"; and table bookings (a table only, or with food ordered
// ahead): their own hours, time grid, notice, days ahead, party size, and when the kitchen gets
// the food before the guests arrive. The cloud enforces them. PUT /api/connect/fees/ordering.
import { useState } from 'react'
import toast from 'react-hot-toast'
import { Plus, X } from 'lucide-react'
import { errorMessage } from '@xenia/api'
import { useTranslation } from '@xenia/i18n'
import { Button, Field, IconButton, Input, Select, Switch } from '@xenia/ui'
import client from '../../api/client'
import { DAYS, orderingBody, orderingToForm } from './feesLogic'
export default function OrderingSettings({ ordering, onSaved }) {
const { t } = useTranslation('manager')
const [form, setForm] = useState(() => orderingToForm(ordering))
const [busy, setBusy] = useState(false)
const set = (k, v) => setForm(f => ({ ...f, [k]: v }))
const setRes = (k, v) => setForm(f => ({ ...f, res: { ...f.res, [k]: v } }))
const toggleService = s => set('services', form.services.includes(s) ? form.services.filter(x => x !== s) : ['takeaway', 'delivery'].filter(x => x === s || form.services.includes(x)))
const save = async () => {
setBusy(true)
try {
const { data } = await client.put('/api/connect/fees/ordering', orderingBody(form))
setForm(orderingToForm(data))
toast.success(t('fees.saved'))
onSaved?.()
} catch (err) { toast.error(errorMessage(err, t)) }
setBusy(false)
}
return (
<section className="p-4 rounded-card border border-line bg-surface grid gap-4" aria-labelledby="ordering-h" data-testid="ordering-settings">
<div className="grid gap-1">
<h2 id="ordering-h" className="text-title font-semibold text-ink">{t('ordering.title')}</h2>
<p className="text-label text-ink-2">{t('ordering.intro')}</p>
</div>
<div className="grid gap-2 sm:grid-cols-2">
{['takeaway', 'delivery'].map(s => (
<Switch key={s} checked={form.services.includes(s)} onChange={() => toggleService(s)}
label={t(`ordering.service.${s}`)} description={t(`ordering.serviceHint.${s}`)} data-testid={`ordering-${s}`} />
))}
</div>
<Hours anyTime={form.anyTime} days={form.days} onAnyTime={v => set('anyTime', v)} onDays={d => set('days', d)} testId="ordering-hours" />
<div className="grid gap-3 sm:grid-cols-3">
<Field label={t('ordering.prep')} hint={t('ordering.prepHint')}>
<Input inputMode="numeric" value={form.prep_minutes} onChange={e => set('prep_minutes', e.target.value)} />
</Field>
<Field label={t('ordering.slot')} hint={t('ordering.slotHint')}>
<Select value={form.slot_minutes} onChange={e => set('slot_minutes', e.target.value)}>
{['10', '15', '20', '30', '60'].map(m => <option key={m} value={m}>{t('ordering.minutes', { n: m })}</option>)}
</Select>
</Field>
<Field label={t('ordering.ahead')}>
<Select value={form.days_ahead} onChange={e => set('days_ahead', e.target.value)}>
{['0', '1', '2', '3', '7'].map(n => <option key={n} value={n}>{t(`ordering.aheadN.${n}`)}</option>)}
</Select>
</Field>
</div>
<Switch checked={form.asap} onChange={v => set('asap', v)} label={t('ordering.asap')} description={t('ordering.asapHint')} />
<div className="grid gap-3 pt-4 border-t border-line" data-testid="booking-settings">
<div className="grid gap-1">
<h3 className="text-body-lg font-semibold text-ink">{t('booking.title')}</h3>
<p className="text-label text-ink-2">{t('booking.intro')}</p>
</div>
<Switch checked={form.res.enabled} onChange={v => setRes('enabled', v)} label={t('booking.enabled')} description={t('booking.enabledHint')} data-testid="booking-enabled" />
{form.res.enabled && (
<>
<Switch checked={form.res.with_food} onChange={v => setRes('with_food', v)} label={t('booking.withFood')} description={t('booking.withFoodHint')} data-testid="booking-food" />
<Hours anyTime={form.res.anyTime} days={form.res.days} onAnyTime={v => setRes('anyTime', v)} onDays={d => setRes('days', d)}
anyLabel={t('booking.anyTime')} anyHint={t('booking.anyTimeHint')} testId="booking-hours" />
<div className="grid gap-3 sm:grid-cols-3">
<Field label={t('ordering.slot')} hint={t('booking.slotHint')}>
<Select value={form.res.slot_minutes} onChange={e => setRes('slot_minutes', e.target.value)}>
{['15', '30', '60'].map(m => <option key={m} value={m}>{t('ordering.minutes', { n: m })}</option>)}
</Select>
</Field>
<Field label={t('booking.notice')} hint={t('booking.noticeHint')}>
<Input inputMode="numeric" value={form.res.notice_minutes} onChange={e => setRes('notice_minutes', e.target.value)} />
</Field>
<Field label={t('booking.ahead')}>
<Input inputMode="numeric" value={form.res.days_ahead} onChange={e => setRes('days_ahead', e.target.value)} data-testid="booking-ahead" />
</Field>
<Field label={t('booking.party')}>
<Input inputMode="numeric" value={form.res.party_max} onChange={e => setRes('party_max', e.target.value)} data-testid="booking-party" />
</Field>
{form.res.with_food && (
<Field label={t('booking.fire')} hint={t('booking.fireHint')}>
<Input inputMode="numeric" value={form.res.fire_minutes} onChange={e => setRes('fire_minutes', e.target.value)} data-testid="booking-fire" />
</Field>
)}
</div>
</>
)}
</div>
<div><Button onClick={save} loading={busy} data-testid="ordering-save">{t('fees.save')}</Button></div>
</section>
)
}
/** Hours per day (several ranges, past midnight), or any time. */
function Hours({ anyTime, days, onAnyTime, onDays, anyLabel, anyHint, testId }) {
const { t } = useTranslation('manager')
const setDay = (d, ranges) => onDays(days.map((r, i) => (i === d ? ranges : r)))
return (
<div className="grid gap-2">
<Switch checked={anyTime} onChange={onAnyTime} label={anyLabel ?? t('ordering.anyTime')} description={anyHint ?? t('ordering.anyTimeHint')} />
{!anyTime && (
<ul className="grid gap-1.5" data-testid={testId}>
{DAYS.map(d => (
<li key={d} className="grid grid-cols-[6rem_1fr] gap-2 items-start">
<span className="text-body text-ink pt-2">{t(`fees.dayLong.${d}`)}</span>
<div className="grid gap-1.5">
{days[d].length === 0 && <span className="text-label text-ink-3 pt-2">{t('ordering.closedDay')}</span>}
{days[d].map((r, i) => (
<div key={i} className="flex items-center gap-2">
<Input type="time" size="sm" value={r.from} aria-label={t('fees.timeFrom')}
onChange={e => setDay(d, days[d].map((x, j) => (j === i ? { ...x, from: e.target.value } : x)))} />
<span className="text-ink-3">–</span>
<Input type="time" size="sm" value={r.to} aria-label={t('fees.timeTo')}
onChange={e => setDay(d, days[d].map((x, j) => (j === i ? { ...x, to: e.target.value } : x)))} />
<IconButton size="sm" label={t('fees.delete')} onClick={() => setDay(d, days[d].filter((_, j) => j !== i))}><X size={16} /></IconButton>
</div>
))}
{days[d].length < 4 && (
<button type="button" className="justify-self-start text-label font-medium text-accent-ink flex items-center gap-1"
onClick={() => setDay(d, [...days[d], { from: '12:00', to: '23:00' }])}>
<Plus size={14} aria-hidden="true" />{t('ordering.addHours')}
</button>
)}
</div>
</li>
))}
</ul>
)}
</div>
)
}