feat(connect): the site side of online orders: fee line, customer's time, price check

Plan phase 3S, step S.5.
- Pull: an online order keeps the customer's time (orders.requested_for, None = as soon as
  possible) and delivery area; quick options are stored in the site's entry shape; items get
  their cost snapshot.
- The delivery fee is its own order line: a system product (products.system_key
  "delivery_fee", lifecycle "system", sold nowhere; services/system_products.py), created on
  first use in the venue's language with the VAT group most products use. Added printed and
  served, so kitchen tickets and the KDS never see it; paid at the counter with the items,
  counted in the shift, on the fiscal receipt with its VAT group. Never listed in the
  catalogue (products list, export, edit endpoints → 404) and never added by hand.
- Price check (services/online_check.py): each line priced as the snapshot prices it, at the
  order's time, and the fee through the same engine with the site's rules and the cloud's
  area id; differences kept on orders.online_price_diff and shown. The customer pays what
  the cloud charged.
- Counter: time chip (or as soon as possible), area, fee line, price warning; takeaway is
  its own kind (was shown as "at the venue") and finishes as "collected". KDS cards show the
  online reference and the time; kitchen tickets print the reference, the type and the time.
- Manager: Online ordering → "The fee on the receipt" (name, VAT group;
  PUT /api/connect/fees/fee-product, fees.vat_group_unknown); the online orders page shows
  the time, area and price warning.
- Migration 0019. Tests: tests/test_online_site_side.py (6), terminal unit test, e2e
  (terminal: fee, time, warning, paid in full; manager: fee line settings).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 15:38:25 +02:00
co-authored by Claude Opus 5.5
parent 8ea44d71aa
commit 0ca700279a
38 changed files with 899 additions and 69 deletions
@@ -1,4 +1,5 @@
import { useState, useEffect, useCallback, useRef } from 'react'
import { useFormat, useTranslation } from '@xenia/i18n'
import { useLiveEvent } from '../live/useLiveEvent'
import {
getIncomingOrders,
@@ -55,9 +56,17 @@ function OrderCard({ order, onAction }) {
return () => clearInterval(id)
}, [])
const { t } = useTranslation('manager')
const { formatDate, formatDateTime, formatMoney, formatTime } = useFormat()
const subtotal = calcTotals(order.items)
const isDelivery = order.online_order_type === 'delivery'
const isTakeaway = order.online_order_type === 'takeaway'
const st = order.online_status
// phase 3S (S.5): when the customer wants it, and the venue's own price check
const when = order.requested_for
? t('onlineOrders.for', { when: formatDate(order.requested_for) === formatDate(new Date()) ? formatTime(order.requested_for) : formatDateTime(order.requested_for, 'datetimeShort') })
: t('onlineOrders.asap')
const diff = order.online_price_diff
async function act(fn) {
setBusy(true)
@@ -75,7 +84,7 @@ function OrderCard({ order, onAction }) {
<span className={`text-xs font-semibold px-2 py-0.5 rounded-full ${
isDelivery ? 'bg-orange-100 text-orange-700' : 'bg-teal-100 text-teal-700'
}`}>
{isDelivery ? 'Delivery' : 'Dine-in'}
{isDelivery ? 'Delivery' : isTakeaway ? 'Takeaway' : 'Dine-in'}
</span>
<OnlineStatusBadge status={st} />
</div>
@@ -91,7 +100,15 @@ function OrderCard({ order, onAction }) {
<p className="text-gray-500">{order.online_customer_phone}</p>
)}
{isDelivery && order.online_customer_address && (
<p className="text-gray-500">{order.online_customer_address}</p>
<p className="text-gray-500">{order.online_customer_address}{order.online_delivery_area ? ` · ${order.online_delivery_area}` : ''}</p>
)}
{(isDelivery || isTakeaway) && (
<p className={`font-semibold ${order.requested_for ? 'text-info-ink' : 'text-ink-2'}`} data-testid="online-when">{when}</p>
)}
{diff && (
<p className="text-warning-ink bg-warning-soft rounded px-2 py-1 text-xs mt-1" data-testid="online-price-diff">
{t('onlineOrders.priceDiff', { charged: formatMoney(diff.cloud_total), here: formatMoney(diff.site_total) })}
</p>
)}
{order.online_customer_notes && (
<p className="text-amber-700 bg-amber-50 rounded px-2 py-1 text-xs mt-1">
@@ -105,7 +122,9 @@ function OrderCard({ order, onAction }) {
<div className="space-y-1 text-sm text-gray-700">
{order.items.map((it, i) => (
<div key={i} className="flex justify-between">
<span>{it.quantity} × {it.product?.name ?? `#${it.product_id}`}</span>
<span className={it.system_key ? 'text-ink-2' : undefined}>
{it.system_key ? (it.product_name ?? it.product?.name) : `${it.quantity} × ${it.product_name ?? it.product?.name ?? `#${it.product_id}`}`}
</span>
<span className="text-gray-500">€{(((it.unit_price ?? 0) + (it.price_adjustment ?? 0)) * it.quantity).toFixed(2)}</span>
</div>
))}
@@ -0,0 +1,51 @@
// The delivery fee on the bill and the fiscal receipt (plan phase 3S, step S.5). An online
// delivery order's fee arrives as its own line (a system product the venue never sells by
// hand), so the counter takes items + fee and the receipt prints it. Here the venue names
// that line and picks its VAT group. PUT /api/connect/fees/fee-product.
import { useState } from 'react'
import toast from 'react-hot-toast'
import { errorMessage } from '@xenia/api'
import { useTranslation } from '@xenia/i18n'
import { Button, Field, Input, Select } from '@xenia/ui'
import client from '../../api/client'
export default function FeeProductCard({ product, onSaved }) {
const { t } = useTranslation('manager')
const [name, setName] = useState(product.name ?? '')
const [vat, setVat] = useState(product.vat_group_id == null ? '' : String(product.vat_group_id))
const [busy, setBusy] = useState(false)
const groups = product.vat_groups ?? []
const save = async () => {
setBusy(true)
try {
await client.put('/api/connect/fees/fee-product', { name: name.trim(), vat_group_id: vat === '' ? null : Number(vat) })
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-3" aria-labelledby="fee-product-h" data-testid="fee-product">
<div className="grid gap-1">
<h2 id="fee-product-h" className="text-title font-semibold text-ink">{t('feeProduct.title')}</h2>
<p className="text-label text-ink-2">{t('feeProduct.intro')}</p>
</div>
<Field label={t('feeProduct.name')}>
<Input value={name} maxLength={40} onChange={e => setName(e.target.value)} data-testid="fee-product-name" />
</Field>
<Field label={t('feeProduct.vat')} hint={groups.length ? undefined : t('feeProduct.noGroups')}>
{groups.length ? (
<Select value={vat} onChange={e => setVat(e.target.value)} data-testid="fee-product-vat">
<option value="">{t('feeProduct.vatNone')}</option>
{groups.map(g => <option key={g.machine_id} value={String(g.machine_id)}>{g.friendly_name || g.machine_id}</option>)}
</Select>
) : (
<Input inputMode="numeric" value={vat} onChange={e => setVat(e.target.value.replace(/\D/g, ''))} data-testid="fee-product-vat" />
)}
</Field>
<div><Button onClick={save} loading={busy} disabled={!name.trim()} data-testid="fee-product-save">{t('fees.save')}</Button></div>
</section>
)
}
@@ -1,5 +1,5 @@
// Online ordering (plan phase 3S, steps S.3–S.4): the ordering settings (OrderingSettings),
// delivery areas and fees. Backend: /api/connect/fees
// Online ordering (plan phase 3S, steps S.3–S.5): the ordering settings (OrderingSettings),
// delivery areas and fees, and the fee's line on the receipt (FeeProductCard). Backend: /api/connect/fees
// (connect_orders.fees). The venue lists the areas it delivers to and the fee rules, looked
// at from the top: a rule whose conditions hold changes the fee (set, add, % of the
// products, free) or refuses the order below a minimum; "stop here" makes the first match
@@ -13,6 +13,7 @@ import { useFormat, useTranslation } from '@xenia/i18n'
import { SALES_POINTS } from '@xenia/access'
import { Button, Dialog, EmptyState, Field, IconButton, Input, Select, Spinner, Switch } from '@xenia/ui'
import client from '../../api/client'
import FeeProductCard from './FeeProductCard'
import OrderingSettings from './OrderingSettings'
import { ACTIONS, DAYS, describeRule, emptyRule, moveRule, parsePostcodes, previewBody, ruleBody, ruleProblems, ruleToForm } from './feesLogic'
@@ -109,6 +110,7 @@ export default function FeesPage() {
)}
</section>
<TestOrder areas={data.areas} />
{data.fee_product && <FeeProductCard key={JSON.stringify(data.fee_product)} product={data.fee_product} onSaved={load} />}
</div>
</div>