// 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 (
{t('orders.split.invalid')}
}{t('orders.split.hint')}
{error &&{error}
}