Files
xenia-pos-local/terminal/src/apps/pos/BookingPanel.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

158 lines
8.7 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.
// One table booking at the counter (plan phase 3S, step S.6): who, how many, when, their note,
// and the food they ordered ahead (with a warning when the venue's prices differ). A request is
// confirmed or declined (the customer sees the answer and the reason); a confirmed booking says
// when its food goes to the kitchen, seats the guests at a free table (their food's order moves
// there, and is sent now if it hadn't been), or is cancelled / marked as a no-show.
import { useState } from 'react'
import { AlertTriangle, Ban, Check, ChefHat, MessageSquare, Phone, UserX, Users } from 'lucide-react'
import { errorMessage } from '@xenia/api'
import { useFormat, useTranslation } from '@xenia/i18n'
import { Button, Dialog, EmptyState, Input, Select } from '@xenia/ui'
import client from '../../api/client'
import { bookingSteps, foodState, freeTables, whenLabel } from './logic/bookings'
const REASONS = ['full', 'closed', 'party']
export default function BookingPanel({ booking, tables, onChanged }) {
const { t } = useTranslation('terminal')
const fmt = useFormat()
const [busy, setBusy] = useState(false)
const [error, setError] = useState('')
const [table, setTable] = useState('')
const [declining, setDeclining] = useState(false)
if (!booking) {
return (
<aside className="border-l border-line bg-surface flex items-center justify-center p-6" data-testid="booking-panel">
<EmptyState compact title={t('pos.bookings.pick')} />
</aside>
)
}
const steps = bookingSteps(booking)
const food = foodState(booking)
const free = freeTables(tables)
const run = async (path, body) => {
setBusy(true)
setError('')
try {
await client.post(`/api/connect/bookings/${booking.id}/${path}`, body ?? {})
setDeclining(false)
onChanged()
} catch (err) { setError(errorMessage(err, t)); onChanged() } finally { setBusy(false) }
}
return (
<aside className="border-l border-line bg-surface flex flex-col min-h-0" data-testid="booking-panel" data-booking={booking.id} data-status={booking.status}>
<div className="p-3 border-b border-line grid gap-1">
<div className="flex items-center gap-2">
<span className="flex-1 text-title font-semibold">{whenLabel(booking.reserved_for, fmt, t)}</span>
{booking.ref && <span className="text-label font-medium text-ink-2">{booking.ref}</span>}
</div>
<div className="text-caption text-ink-2">{t(`pos.bookings.status.${booking.status}`)}{booking.table_label ? ` · ${booking.table_label}` : ''}</div>
</div>
<div className="flex-1 overflow-y-auto">
<section className="p-3 border-b border-line grid gap-1.5 text-body" data-testid="booking-guest">
<div className="text-body-lg font-semibold">{booking.guest_name}</div>
<Detail icon={Users}>{t('pos.bookings.people', { count: booking.party_size })}</Detail>
{booking.phone && <Detail icon={Phone}>{booking.phone}</Detail>}
{booking.note && (
<div className="p-2 rounded-control bg-warning-soft text-warning-ink flex gap-2">
<MessageSquare size={16} className="mt-0.5 shrink-0" aria-hidden="true" /><span>{booking.note}</span>
</div>
)}
</section>
{food !== 'none' && (
<section className="border-b border-line" data-testid="booking-food">
<p className="px-3 pt-3 text-label font-semibold text-ink-2 flex items-center gap-1.5">
<ChefHat size={15} aria-hidden="true" />
{food === 'kitchen' ? t('pos.bookings.foodSent')
: booking.fire_at ? t('pos.bookings.foodAt', { time: whenLabel(booking.fire_at, fmt, t) }) : t('pos.bookings.foodOnConfirm')}
</p>
{booking.price_diff && (
<p className="mx-3 mt-2 p-2 rounded-control bg-warning-soft text-warning-ink text-label flex gap-1.5" role="note" data-testid="booking-price-diff">
<AlertTriangle size={15} className="mt-0.5 shrink-0" aria-hidden="true" />
{t('pos.online.diff.body', { charged: fmt.formatMoney(booking.price_diff.cloud_total), here: fmt.formatMoney(booking.price_diff.site_total) })}
</p>
)}
{booking.lines.map((l, i) => (
<div key={i} className="px-3 py-2 flex items-start gap-2 text-ink">
<span className="flex-1 min-w-0 text-body">{l.quantity}× {l.name}
{l.options?.length > 0 && <span className="block text-caption text-ink-2">{l.options.join(' · ')}</span>}
</span>
<span className="text-body">{fmt.formatMoney(l.unit_price * l.quantity)}</span>
</div>
))}
<div className="px-3 pb-3 flex justify-between font-semibold"><span>{t('pos.total')}</span><span>{fmt.formatMoney(booking.total)}</span></div>
</section>
)}
</div>
<div className="border-t border-line p-3 grid gap-2">
{error && <p role="alert" className="text-label text-danger-ink">{error}</p>}
{steps.includes('confirm') && (
<>
<Button size="lg" icon={<Check size={20} />} loading={busy} onClick={() => run('confirm')} data-testid="booking-confirm">{t('pos.bookings.confirm')}</Button>
<Button variant="secondary" icon={<Ban size={16} />} disabled={busy} onClick={() => setDeclining(true)} data-testid="booking-decline">{t('pos.bookings.decline')}</Button>
</>
)}
{steps.includes('arrive') && (
<>
<div className="flex gap-2">
<Select value={table} onChange={e => setTable(e.target.value)} aria-label={t('pos.bookings.table')} data-testid="booking-table" className="flex-1">
<option value="">{t('pos.bookings.noTable')}</option>
{free.map(x => <option key={x.id} value={x.id}>{x.name}</option>)}
</Select>
</div>
<Button size="lg" icon={<Check size={20} />} loading={busy} onClick={() => run('arrive', { table_id: table ? Number(table) : null })} data-testid="booking-arrive">
{t('pos.bookings.arrive')}
</Button>
<div className="grid grid-cols-2 gap-2">
<Button variant="secondary" icon={<Ban size={16} />} disabled={busy} onClick={() => run('cancel', { status: 'cancelled' })} data-testid="booking-cancel">{t('pos.bookings.cancel')}</Button>
<Button variant="secondary" icon={<UserX size={16} />} disabled={busy} onClick={() => run('cancel', { status: 'no_show' })} data-testid="booking-noshow">{t('pos.bookings.noShow')}</Button>
</div>
</>
)}
</div>
{declining && <DeclineDialog busy={busy} error={error} onClose={() => setDeclining(false)} onDecline={reason => run('decline', { reason })} />}
</aside>
)
}
function Detail({ icon: Icon, children }) {
return <div className="flex items-start gap-2 text-ink-2"><Icon size={16} className="mt-0.5 shrink-0" aria-hidden="true" /><span className="text-ink">{children}</span></div>
}
function DeclineDialog({ busy, error, onClose, onDecline }) {
const { t } = useTranslation('terminal')
const [choice, setChoice] = useState(null)
const [other, setOther] = useState('')
const reason = choice === 'other' ? other.trim() : choice ? t(`pos.bookings.reasons.${choice}`) : ''
return (
<Dialog open size="sm" title={t('pos.bookings.declineTitle')} onClose={busy ? undefined : onClose}
footer={<>
<Button variant="secondary" onClick={onClose} disabled={busy}>{t('pos.online.keep')}</Button>
<Button variant="danger" icon={<Ban size={16} />} loading={busy} disabled={!reason} onClick={() => onDecline(reason)} data-testid="decline-confirm">
{t('pos.bookings.decline')}
</Button>
</>}>
<div className="grid gap-2">
<p className="text-body text-ink-2">{t('pos.online.rejectBody')}</p>
<div className="grid gap-1" role="radiogroup" aria-label={t('pos.online.reason')}>
{[...REASONS, 'other'].map(r => (
<button key={r} type="button" role="radio" aria-checked={choice === r} onClick={() => setChoice(r)} data-testid={`decline-${r}`}
className={`min-h-[var(--x-control-md)] px-3 rounded-control text-left text-body transition-colors duration-fast
${choice === r ? 'bg-accent text-accent-fg' : 'bg-surface-2 text-ink hover:bg-surface-3'}`}>
{t(`pos.bookings.reasons.${r}`)}
</button>
))}
</div>
{choice === 'other' && <Input value={other} onChange={e => setOther(e.target.value)} maxLength={300} autoFocus aria-label={t('pos.online.reason')} />}
{error && <p role="alert" className="text-label text-danger-ink">{error}</p>}
</div>
</Dialog>
)
}