An area is a list of postcodes, a shape drawn on a map or a circle around a point. The fee engine (both copies, byte-identical) matches a shape only by the customer's pin, then postcodes; point-in-polygon (edges inside) and haversine circles; shared cases in fee_cases.json. - Alembic 0053: delivery_areas.kind / geometry, orders.online_lat / lng - /api/connect/fees: areas with kind + geometry (validated: 3-200 corners, 50 m - 50 km), the venue's place (PUT /venue-point, setting venue.location, sent to the cloud as fees.venue_point), preview by lat / lng - manager: DeliveryMap (Leaflet + OpenStreetMap tiles): draw / move / remove corners, circles, the venue on the map, an overview, test an order by a point - pulled online orders keep the pin; the site re-checks the fee with it; the counter links the pin to a map - tests: backend (shapes, venue point, pinned orders), manager logic, e2e manager.mjs and order_app.mjs 6R.G blocks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
219 lines
12 KiB
React
219 lines
12 KiB
React
// One online order at the counter (plan step 3.3d): the right column in Online mode. It shows
|
|
// the customer (phone, address, their note) and the items, and offers the next step only:
|
|
// accept (kitchen tickets, KDS) or reject (with a reason the customer sees), start preparing,
|
|
// ready or out for delivery, then finish: take what's unpaid (the counter's ordinary payment,
|
|
// cash/card/split with the drawer) and mark it delivered. Someone else's change meanwhile is
|
|
// refused by the backend and the order shows where it is now.
|
|
// Phase 3S (S.5): the customer's time (or "as soon as possible"), the delivery area, the
|
|
// delivery fee as its own line (paid with the items), and a warning when the venue's own
|
|
// prices differ from what the customer was charged online (the customer pays what they saw).
|
|
import { useState } from 'react'
|
|
import { AlertTriangle, Ban, Banknote, Bike, Check, ChefHat, Clock, MapPin, MessageSquare, PackageCheck, Phone, ShoppingBag, Utensils } from 'lucide-react'
|
|
import { errorMessage } from '@xenia/api'
|
|
import { useFormat, useTranslation } from '@xenia/i18n'
|
|
import { Button, Dialog, EmptyState, ReasonPicker, reasonText } from '@xenia/ui'
|
|
import client from '../../api/client'
|
|
import { diffRows, finishWord as finishOf, kindOf, nextStep } from './logic/online'
|
|
import { itemPayments, orderLines, orderSums, qtyLabel } from './logic/tables'
|
|
import PaymentDialog from './PaymentDialog'
|
|
|
|
const REASONS = ['busy', 'unavailable', 'closing', 'area']
|
|
const STEP_ICON = { preparing: ChefHat, ready: PackageCheck, out_for_delivery: Bike }
|
|
const KIND_ICON = { delivery: Bike, takeaway: ShoppingBag, dine_in: Utensils }
|
|
|
|
export default function OnlinePanel({ order, onChanged }) {
|
|
const { t } = useTranslation('terminal')
|
|
const { formatMoney, formatTime } = useFormat()
|
|
const [busy, setBusy] = useState(false)
|
|
const [error, setError] = useState('')
|
|
const [paying, setPaying] = useState(null) // rows being paid
|
|
const [rejecting, setRejecting] = useState(false)
|
|
|
|
if (!order) {
|
|
return (
|
|
<aside className="border-l border-line bg-surface flex items-center justify-center p-6" data-testid="online-panel">
|
|
<EmptyState compact title={t('pos.online.pick')} />
|
|
</aside>
|
|
)
|
|
}
|
|
|
|
const lines = orderLines(order)
|
|
const sums = orderSums(lines)
|
|
const step = nextStep(order)
|
|
const kind = kindOf(order)
|
|
const KindIcon = KIND_ICON[kind]
|
|
const finishWord = finishOf(order)
|
|
|
|
const run = async (fn) => {
|
|
setBusy(true)
|
|
setError('')
|
|
try { return await fn() } catch (err) { setError(errorMessage(err, t)); onChanged(); return null } finally { setBusy(false) }
|
|
}
|
|
const post = (path, body) => client.post(`/api/connect/orders/${order.id}/${path}`, body)
|
|
const accept = () => run(async () => { await post('accept'); onChanged(null, { list: 'in_progress' }) })
|
|
const advance = to => run(async () => { await post('status', { status: to }); onChanged() })
|
|
|
|
const payRows = lines.filter(l => !l.paid).map(l => ({ id: l.id, label: `${qtyLabel(l.quantity, l.unitType)} ${l.name}`, total: l.total }))
|
|
const submitPay = async ({ method, payments, cashGiven }) => (await client.post(`/api/pos/orders/${order.id}/pay`, {
|
|
payments: method === 'split' ? itemPayments(payments, paying) : [{ method, item_ids: paying.map(r => r.id) }],
|
|
cash_given: cashGiven,
|
|
})).data
|
|
|
|
return (
|
|
<aside className="border-l border-line bg-surface flex flex-col min-h-0" data-testid="online-panel" data-order={order.id} data-status={order.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">{order.ref}</span>
|
|
<span className="text-label font-medium text-ink-2 flex items-center gap-1">
|
|
<KindIcon size={16} aria-hidden="true" />
|
|
{t(`pos.online.type.${kind}`)}
|
|
</span>
|
|
</div>
|
|
<div className="text-caption text-ink-2">{t(`pos.online.status.${order.status}`)} · {formatTime(order.opened_at)}</div>
|
|
{kind !== 'dine_in' && <WhenChip when={order.requested_for} />}
|
|
</div>
|
|
|
|
<div className="flex-1 overflow-y-auto">
|
|
<section className="p-3 border-b border-line grid gap-1.5 text-body" data-testid="online-customer">
|
|
<div className="text-body-lg font-semibold">{order.customer.name}</div>
|
|
{order.customer.phone && <Detail icon={Phone}>{order.customer.phone}</Detail>}
|
|
{order.customer.address && <Detail icon={MapPin}>{order.customer.address}{order.area ? ` · ${order.area}` : ''}</Detail>}
|
|
{/* 6R.G: the customer's pin, opened in a map (the driver's phone or the browser) */}
|
|
{order.pin && (
|
|
<a href={`https://www.openstreetmap.org/?mlat=${order.pin.lat}&mlon=${order.pin.lng}#map=18/${order.pin.lat}/${order.pin.lng}`}
|
|
target="_blank" rel="noreferrer" className="text-label text-accent-ink underline" data-testid="online-pin">
|
|
{t('pos.online.pin')}
|
|
</a>
|
|
)}
|
|
{order.customer.notes && (
|
|
<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>{order.customer.notes}</span>
|
|
</div>
|
|
)}
|
|
</section>
|
|
<PriceCheck diff={order.price_diff} />
|
|
<section data-testid="online-lines">
|
|
{lines.map(l => (
|
|
<div key={l.id} className={`px-3 py-2 border-b border-line flex items-start gap-2 ${l.paid ? 'text-ink-3' : 'text-ink'}`}
|
|
data-testid={l.system ? 'online-fee' : 'online-line'}>
|
|
<span className="flex-1 min-w-0">
|
|
<span className={`text-body ${l.system ? 'text-ink-2' : 'font-medium'}`}>{l.system ? l.name : `${qtyLabel(l.quantity, l.unitType)} ${l.name}`}</span>
|
|
{(l.options.length > 0 || l.notes) && (
|
|
<span className="block text-caption text-ink-2">{[...l.options, l.notes && `“${l.notes}”`].filter(Boolean).join(' · ')}</span>
|
|
)}
|
|
</span>
|
|
<span className="text-body text-right">
|
|
{formatMoney(l.total)}
|
|
{l.paid && <span className="block text-caption text-success-ink">{t(`pos.table.paidBy.${l.method === 'card' ? 'card' : 'cash'}`)}</span>}
|
|
</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>}
|
|
<div className="flex items-baseline justify-between">
|
|
<span className="text-body-lg text-ink-2">{sums.unpaid > 0 ? t('pos.table.due') : t('pos.total')}</span>
|
|
<span className="text-display font-semibold" data-testid="online-total">{formatMoney(sums.unpaid > 0 ? sums.unpaid : sums.total)}</span>
|
|
</div>
|
|
{step?.kind === 'answer' && (
|
|
<>
|
|
<Button size="lg" icon={<Check size={20} />} loading={busy} onClick={accept} data-testid="online-accept">{t('pos.online.accept')}</Button>
|
|
<Button variant="secondary" icon={<Ban size={16} />} disabled={busy} onClick={() => setRejecting(true)} data-testid="online-reject">{t('pos.online.reject')}</Button>
|
|
</>
|
|
)}
|
|
{step?.kind === 'status' && (() => {
|
|
const Icon = STEP_ICON[step.to]
|
|
return <Button size="lg" icon={<Icon size={20} />} loading={busy} onClick={() => advance(step.to)} data-testid="online-next">{t(`pos.online.to.${step.to}`)}</Button>
|
|
})()}
|
|
{step?.kind === 'finish' && (step.pay ? (
|
|
<Button size="lg" icon={<Banknote size={20} />} disabled={busy} onClick={() => setPaying(payRows)} data-testid="online-finish">
|
|
{t(`pos.online.payAnd.${finishWord}`, { amount: formatMoney(sums.unpaid) })}
|
|
</Button>
|
|
) : (
|
|
<Button size="lg" icon={<PackageCheck size={20} />} loading={busy} onClick={() => advance('delivered')} data-testid="online-finish">
|
|
{t(`pos.online.mark.${finishWord}`)}
|
|
</Button>
|
|
))}
|
|
</div>
|
|
|
|
{paying && (
|
|
<PaymentDialog total={orderSums(paying).total} rows={paying} submit={submitPay}
|
|
onClose={() => setPaying(null)}
|
|
onPaid={result => {
|
|
setPaying(null)
|
|
run(async () => { await post('status', { status: 'delivered' }); onChanged(result) })
|
|
}} />
|
|
)}
|
|
|
|
{rejecting && <RejectDialog busy={busy} error={error} onClose={() => setRejecting(false)}
|
|
onReject={reason => run(async () => { await post('reject', { reason }); setRejecting(false); onChanged(null, { clear: true }) })} />}
|
|
</aside>
|
|
)
|
|
}
|
|
|
|
/** When the customer wants it: a time (with the day when it isn't today) or as soon as possible. */
|
|
function WhenChip({ when }) {
|
|
const { t } = useTranslation('terminal')
|
|
const { formatDate, formatDateTime, formatTime } = useFormat()
|
|
const label = !when ? t('pos.online.asap')
|
|
: t('pos.online.for', { when: formatDate(when) === formatDate(new Date()) ? formatTime(when) : formatDateTime(when, 'datetimeShort') })
|
|
return (
|
|
<div className={`justify-self-start mt-1 px-2 h-7 rounded-control flex items-center gap-1.5 text-label font-semibold
|
|
${when ? 'bg-info-soft text-info-ink' : 'bg-surface-2 text-ink-2'}`} data-testid="online-when">
|
|
<Clock size={14} aria-hidden="true" />{label}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/** The venue's own prices differ from what the customer was charged (S.5): what differs, line by line. */
|
|
function PriceCheck({ diff }) {
|
|
const { t } = useTranslation('terminal')
|
|
const { formatMoney } = useFormat()
|
|
const rows = diffRows(diff)
|
|
if (!rows.length) return null
|
|
const money = v => (v == null ? '—' : formatMoney(v))
|
|
return (
|
|
<section className="m-3 p-2.5 rounded-control bg-warning-soft text-warning-ink grid gap-1 text-label" role="note" data-testid="online-price-diff">
|
|
<p className="font-semibold flex items-center gap-1.5"><AlertTriangle size={15} aria-hidden="true" />{t('pos.online.diff.title')}</p>
|
|
<p>{t('pos.online.diff.body', { charged: formatMoney(diff.cloud_total), here: formatMoney(diff.site_total) })}</p>
|
|
<ul className="grid gap-0.5">
|
|
{rows.map(r => (
|
|
<li key={r.key}>
|
|
{r.name ?? t('pos.online.diff.fee')}: {t(`pos.online.diff.reason.${r.reason}`, { online: money(r.cloud), here: money(r.site) })}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</section>
|
|
)
|
|
}
|
|
|
|
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 RejectDialog({ busy, error, onClose, onReject }) {
|
|
const { t } = useTranslation('terminal')
|
|
const [choice, setChoice] = useState(null)
|
|
const [other, setOther] = useState('')
|
|
const options = REASONS.map(r => ({ value: r, label: t(`pos.online.reasons.${r}`) }))
|
|
const reason = reasonText(choice, options, other)
|
|
return (
|
|
<Dialog open size="sm" title={t('pos.online.rejectTitle')} 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={() => onReject(reason)} data-testid="reject-confirm">
|
|
{t('pos.online.rejectConfirm')}
|
|
</Button>
|
|
</>}>
|
|
<div className="grid gap-2">
|
|
<p className="text-body text-ink-2">{t('pos.online.rejectBody')}</p>
|
|
<ReasonPicker options={options} value={choice} onChange={setChoice} other={other} onOther={setOther}
|
|
otherLabel={t('pos.online.reasons.other')} ariaLabel={t('pos.online.reason')} testIdPrefix="reason" />
|
|
{error && <p role="alert" className="text-label text-danger-ink">{error}</p>}
|
|
</div>
|
|
</Dialog>
|
|
)
|
|
}
|