// A table's order lines in the manager (plan step 6.9): what was ordered with its choices, who // paid what and how, price changes; pick unpaid lines to pay, discount, cancel or put on the // customer's tab. The rules are the phones' (a discount needs pricing.discounts.apply, the // server checks every key again). import { useState } from 'react' import { useMutation } from '@tanstack/react-query' import toast from 'react-hot-toast' import { Ban, CreditCard, Percent, Printer, Receipt } from 'lucide-react' import { useCan, useModule } from '@xenia/access' import { errorMessage } from '@xenia/api' import { parseNumber, useFormat, useTranslation } from '@xenia/i18n' import { Button, Dialog, EmptyState, Field, Input, cx } from '@xenia/ui' import client from '../../api/client' import { Pill } from '../overview/parts' import { lineAmount, lineDetails, nameOf, pathText, selectable, selectedAmount, shownLines, toggleSelected, unitOf } from './orderLogic' const STATUS_TONE = { paid: 'success', cancelled: 'danger', tabbed: 'info', closed: 'warning' } const EVENT_TONE = { waiter_discount: 'text-accent-ink', free_item_added: 'text-success-ink', modifier_applied: 'text-info-ink', manual_price: 'text-warning-ink' } export default function OrderLines({ order, open, priceEvents, moves = {}, onPay, onChanged }) { const { t } = useTranslation('manager') const { formatMoney, formatTime } = useFormat() const canDiscount = useCan('pricing.discounts.apply') const tabsOn = useModule('tabs') const canTab = useCan('tabs.manage') && tabsOn // what the tab route checks (6.13a) const [selected, setSelected] = useState([]) const [cancelling, setCancelling] = useState(false) const [discounting, setDiscounting] = useState(false) const lines = shownLines(order) const picked = selected.filter(id => order.items.some(i => i.id === id && selectable(i))) const amount = selectedAmount(order, picked) const done = msg => { toast.success(msg); setSelected([]); onChanged() } const onError = err => toast.error(errorMessage(err, t)) const cancel = useMutation({ mutationFn: print => client.post(`/api/orders/${order.id}/cancel-items`, { item_ids: picked, print_cancellation: print }), onSuccess: () => { setCancelling(false); done(t('orders.lines.cancelled', { count: picked.length })) }, onError, }) const toTab = useMutation({ mutationFn: async () => { for (const id of picked) await client.post(`/api/orders/${order.id}/items/${id}/tab`) }, onSuccess: () => done(t('orders.lines.tabbed')), onError: err => { onError(err); onChanged() }, }) return (
{lines.length === 0 ? : ( )} {open && picked.length > 0 && (
{t('orders.lines.picked', { count: picked.length, amount: formatMoney(amount) })} {order.customer_id && canTab && } {canDiscount && }
)} {cancelling && ( setCancelling(false)} title={t('orders.lines.cancelTitle', { count: picked.length })} description={t('orders.lines.cancelBody')} footer={<> } /> )} {discounting && setDiscounting(false)} onDone={() => { setDiscounting(false); done(t('orders.discount.done')) }} />}
) } function DiscountDialog({ orderId, itemIds, amount, onClose, onDone }) { const { t } = useTranslation('manager') const { formatMoney } = useFormat() const [pct, setPct] = useState('10') const [note, setNote] = useState('') const value = parseNumber(String(pct)) ?? NaN const ok = value > 0 && value <= 100 const apply = useMutation({ mutationFn: () => client.post(`/api/orders/${orderId}/items/discount`, { item_ids: itemIds, discount_percent: value, note: note.trim() || null }), onSuccess: onDone, onError: err => toast.error(errorMessage(err, t)), }) return ( }>
{[5, 10, 15, 20, 50, 100].map(p => ( ))}
setPct(e.target.value)} data-testid="discount-pct" /> setNote(e.target.value)} />
) }