// Splitting a bill by amount in the manager (plan 6F.6), as the phones and the counter do: the chosen // lines are settled into a split bill and cut into equal parts (pre-filled from the customers) or, for // two parts, any amounts; each part is then paid like any line. Undo while no part is paid. import { useState } from 'react' import { Minus, Plus, Undo2 } from 'lucide-react' import { errorMessage } from '@xenia/api' import { parseNumber, useFormat, useTranslation } from '@xenia/i18n' import { defaultParts, splitAmounts } from '@xenia/ordering/split' import { Button, IconButton, Input } from '@xenia/ui' import toast from 'react-hot-toast' import client from '../../api/client' /** The part of the pay dialog that splits instead of paying (choices from splitChoices()). */ export function SplitAmountForm({ orderId, itemIds, amount, choices, customerCount, onDone, onCancel }) { const { t } = useTranslation('manager') const { formatMoney } = useFormat() const [parts, setParts] = useState(() => defaultParts(customerCount, choices.maxParts)) const [mode, setMode] = useState('equal') const [first, setFirst] = useState('') const [busy, setBusy] = useState(false) const [error, setError] = useState(null) const uneven = mode === 'custom' && parts === 2 const typed = first.trim() ? parseNumber(first, { decimals: 2, allowNegative: false }) : null const amounts = splitAmounts(amount, uneven ? 'custom' : 'equal', parts, typed) const step = d => { setParts(p => Math.max(2, Math.min(choices.maxParts, p + d))); setMode('equal') } async function split() { setBusy(true) setError(null) try { await client.post(`/api/orders/${orderId}/split-bills`, uneven ? { item_ids: itemIds, mode: 'custom', parts: 2, first_amount: amounts[0] } : { item_ids: itemIds, mode: 'equal', parts }) onDone() } catch (err) { setError(errorMessage(err, t)) } finally { setBusy(false) } } return (
{choices.maxParts > 2 && (
step(-1)}> {parts} = choices.maxParts} onClick={() => step(1)} data-testid="split-more">
)} {choices.custom && parts === 2 && (
{['equal', 'custom'].map(m => ( ))}
)} {uneven && ( )} {amounts ? ( ) :

{t('orders.split.invalid')}

}

{t('orders.split.hint')}

{error &&

{error}

}
) } /** The order's split bills: parts, paid so far, and Undo while nothing is paid. */ export function SplitBillsCard({ order, canUndo, onChanged }) { const { t } = useTranslation('manager') const { formatMoney } = useFormat() const [busy, setBusy] = useState(null) const bills = order.split_bills ?? [] if (!bills.length) return null async function undo(bill) { setBusy(bill.id) try { await client.post(`/api/orders/${order.id}/split-bills/${bill.id}/undo`) toast.success(t('orders.split.undone')) onChanged() } catch (err) { toast.error(errorMessage(err, t)) } finally { setBusy(null) } } return (
{bills.map(b => (
{t('orders.split.bill', { count: b.parts, total: formatMoney(b.total) })} {t('orders.split.paid', { paid: formatMoney(b.paid), due: formatMoney(Math.max(0, b.total - b.paid)) })} {canUndo && b.can_undo && ( )}
))}
) }