feat(terminal): quick sale at the counter: cash/card/split, change, cash drawer, fiscal (platform-v2 3.3b)
- POST /api/pos/sales: all checks first (shift, lines, payment split, cash given, discount → ordering.prices.modify or manager approval, fiscal VAT groups), then the existing open_order / add_items / pay_items functions, so tickets, KDS, price events, shift totals and the fiscal receipt work as for any order. - Cash drawer: printer_service.open_cash_drawer (ESC @ + ESC p 0 25 250) to the screen's receipt printer on cash, logged and audited (DRAWER_OPENED / DRAWER_FAILED). - orders._can_access_order: the people assigned to an order may act on it (a cashier's own takeaway order); pos_cart prices a batch with the lines before it, as add_items does. - Terminal: Charge → payment dialog (exact cash pre-selected, quick amounts, change, card, split by line), per-line discounts, sale summary and "Same again". - Tests: backend +6 (socket printer receives the drawer kick, fiscal file in the bridge folder), terminal unit +1, terminal.mjs +9. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
// The counter POS screen (plan step 3.3a): categories on the left, the product grid with a
|
||||
// search on top, the cart on the right. A tap adds a product (or opens its choices when it
|
||||
// has preferences to pick); the (i) button checks its price without adding it. Every price is
|
||||
// the backend's: the menu's and the cart's quote. Paying comes in 3.3b.
|
||||
// the backend's: the menu's and the cart's quote. Charge → PaymentDialog (3.3b); after a sale
|
||||
// the cart shows what happened and "Same again".
|
||||
import { useMemo, useState } from 'react'
|
||||
import { Info, Minus, Plus, Search, ShoppingBag, Trash2, Utensils, X } from 'lucide-react'
|
||||
import { Banknote, Info, Minus, Plus, Repeat, Search, ShoppingBag, Trash2, Utensils, X } from 'lucide-react'
|
||||
import { useFormat, useLanguage, useTranslation } from '@xenia/i18n'
|
||||
import { Button, EmptyState, IconButton, Input, Spinner } from '@xenia/ui'
|
||||
import { useCan } from '@xenia/access'
|
||||
import { Button, EmptyState, IconButton, Input, Select, Spinner, useToast } from '@xenia/ui'
|
||||
import {
|
||||
addLine, categoriesWithProducts, defaultSelection, needsChoices, productsIn, searchProducts, setLineQty, toLine, unitOf,
|
||||
DISCOUNTS, addLine, categoriesWithProducts, defaultSelection, needsChoices, productsIn, searchProducts, setLineQty, toLine, unitOf,
|
||||
} from './logic/cart'
|
||||
import PaymentDialog from './PaymentDialog'
|
||||
import ProductDialog from './ProductDialog'
|
||||
import { useMenu, useQuote } from './usePos'
|
||||
|
||||
@@ -27,7 +30,12 @@ function Counter({ menu, t }) {
|
||||
const [orderType, setOrderType] = useState(menu.settings.default_order_type)
|
||||
const [cart, setCart] = useState([])
|
||||
const [dialog, setDialog] = useState(null) // product
|
||||
const [paying, setPaying] = useState(false)
|
||||
const [lastSale, setLastSale] = useState(null) // { lines, orderType, result }
|
||||
const quote = useQuote(cart, orderType)
|
||||
const toast = useToast()
|
||||
const canDiscount = useCan('ordering.prices.modify')
|
||||
const ready = cart.length > 0 && !quote.pending && !quote.error && quote.quote?.lines?.length === cart.length
|
||||
|
||||
const byId = useMemo(() => new Map(menu.products.map(p => [p.id, p])), [menu.products])
|
||||
const categories = useMemo(() => categoriesWithProducts(menu.categories, menu.products), [menu])
|
||||
@@ -84,11 +92,13 @@ function Counter({ menu, t }) {
|
||||
))}
|
||||
</div>
|
||||
<div className="flex-1 overflow-y-auto">
|
||||
{cart.length === 0 && lastSale && <SaleDone sale={lastSale} onAgain={() => { setCart(lastSale.lines); setOrderType(lastSale.orderType); setLastSale(null) }} />}
|
||||
{cart.length === 0
|
||||
? <EmptyState compact title={t('pos.cartEmpty')} description={t('pos.cartEmptyBody')} />
|
||||
? !lastSale && <EmptyState compact title={t('pos.cartEmpty')} description={t('pos.cartEmptyBody')} />
|
||||
: cart.map((line, i) => (
|
||||
<CartLine key={i} line={line} priced={quote.quote?.lines?.[i]} product={byId.get(line.product_id)}
|
||||
onQty={q => setCart(c => setLineQty(c, i, q))} />
|
||||
<CartLine key={i} line={line} priced={quote.quote?.lines?.[i]} product={byId.get(line.product_id)} canDiscount={canDiscount}
|
||||
onQty={q => setCart(c => setLineQty(c, i, q))}
|
||||
onDiscount={pct => setCart(c => c.map((l, j) => (j === i ? { ...l, discount_pct: pct || undefined } : l)))} />
|
||||
))}
|
||||
</div>
|
||||
<div className="border-t border-line p-3 grid gap-3">
|
||||
@@ -99,10 +109,24 @@ function Counter({ menu, t }) {
|
||||
{formatMoney(quote.quote?.total ?? 0)}
|
||||
</span>
|
||||
</div>
|
||||
<Button size="lg" icon={<Banknote size={20} />} disabled={!ready} onClick={() => setPaying(true)} data-testid="charge">
|
||||
{t('pos.charge', { total: formatMoney(quote.quote?.total ?? 0) })}
|
||||
</Button>
|
||||
<Button variant="secondary" icon={<Trash2 size={16} />} disabled={!cart.length} onClick={() => setCart([])}>{t('pos.clearCart')}</Button>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
{paying && quote.quote && (
|
||||
<PaymentDialog cart={cart} quote={quote.quote} orderType={orderType} names={cart.map(l => byId.get(l.product_id)?.name ?? '')}
|
||||
onClose={() => setPaying(false)}
|
||||
onPaid={result => {
|
||||
setPaying(false)
|
||||
setLastSale({ lines: cart, orderType, result })
|
||||
setCart([])
|
||||
toast.success(result.change != null ? t('pos.done.toastChange', { change: formatMoney(result.change) }) : t('pos.done.toast'))
|
||||
}} />
|
||||
)}
|
||||
|
||||
{dialog && (
|
||||
<ProductDialog product={dialog} orderType={orderType} onClose={() => setDialog(null)}
|
||||
onAdd={line => setCart(c => addLine(c, line))} />
|
||||
@@ -145,7 +169,26 @@ function ProductCard({ product, onTap, onInfo }) {
|
||||
)
|
||||
}
|
||||
|
||||
function CartLine({ line, priced, product, onQty }) {
|
||||
function SaleDone({ sale, onAgain }) {
|
||||
const { t } = useTranslation('terminal')
|
||||
const { formatMoney } = useFormat()
|
||||
const r = sale.result
|
||||
return (
|
||||
<div className="m-3 p-4 rounded-card bg-success-soft text-success-ink grid gap-2" data-testid="sale-done" data-order={r.order_id}>
|
||||
<div className="text-title font-semibold">{t('pos.done.paid', { total: formatMoney(r.total) })}</div>
|
||||
{r.change != null && <div className="text-heading font-semibold" data-testid="sale-change">{t('pos.pay.change', { amount: formatMoney(r.change) })}</div>}
|
||||
<div className="text-label">
|
||||
{r.payments.map(p => t(`pos.done.${p.method}`, { amount: formatMoney(p.amount) })).join(' · ')}
|
||||
{r.drawer === 'opened' && ` · ${t('pos.done.drawerOpened')}`}
|
||||
{r.drawer === 'failed' && <span className="text-danger-ink"> · {t('pos.done.drawerFailed')}</span>}
|
||||
{r.fiscal === 'pending' && ` · ${t('pos.done.fiscal')}`}
|
||||
</div>
|
||||
<Button variant="secondary" icon={<Repeat size={16} />} onClick={onAgain} data-testid="same-again">{t('pos.done.again')}</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function CartLine({ line, priced, product, canDiscount, onQty, onDiscount }) {
|
||||
const { t } = useTranslation('terminal')
|
||||
const { formatMoney } = useFormat()
|
||||
const unit = product ? unitOf(product) : null
|
||||
@@ -169,7 +212,12 @@ function CartLine({ line, priced, product, onQty }) {
|
||||
<span className="min-w-12 text-center text-body font-semibold" data-testid="line-qty">{qtyText}</span>
|
||||
<IconButton size="sm" variant="secondary" label={t('pos.more')} onClick={() => onQty(line.quantity + step)}><Plus size={16} /></IconButton>
|
||||
{priced && line.quantity !== 1 && <span className="text-caption text-ink-3">{t('pos.each', { price: formatMoney(priced.unit_price) })}</span>}
|
||||
<span className="flex-1" />
|
||||
<Select size="sm" value={String(line.discount_pct ?? 0)} onChange={e => onDiscount(Number(e.target.value))} aria-label={t('pos.discount')}
|
||||
title={canDiscount ? undefined : t('pos.discountNeedsApproval')} data-testid="line-discount" className="w-36"
|
||||
options={DISCOUNTS.map(d => ({ value: String(d), label: d ? `−${d}%` : t('pos.noDiscount') }))} />
|
||||
</div>
|
||||
{priced?.discount ? <span className="text-label text-success-ink">{t('pos.discountLine', { pct: priced.discount_pct, amount: formatMoney(priced.discount) })}</span> : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user