"use client"; import React from "react"; import { Plus, Trash2 } from "lucide-react"; import { centsFrom, money, randInput, type Line } from "@/lib/billing"; /** A line as it's typed: the unit price as text in rand. */ export type EditLine = { description: string; quantity: string; unit: string }; export const toEdit = (lines: Line[]): EditLine[] => lines.map(l => ({ description: l.description, quantity: String(l.quantity), unit: randInput(l.unitCents) })); export const fromEdit = (lines: EditLine[]) => lines .filter(l => l.description.trim()) .map(l => ({ description: l.description.trim(), quantity: Number(l.quantity) || 1, unitCents: centsFrom(l.unit) ?? 0 })); export const editTotal = (lines: EditLine[]) => fromEdit(lines).reduce((s, l) => s + Math.round(l.quantity * l.unitCents), 0); /** Invoice lines: description, quantity and unit price (negative for a discount). */ export function LinesEditor({ lines, onChange }: { lines: EditLine[]; onChange: (lines: EditLine[]) => void }) { const set = (i: number, patch: Partial) => onChange(lines.map((l, j) => (j === i ? { ...l, ...patch } : l))); return (
{lines.map((l, i) => { const amount = Math.round((Number(l.quantity) || 0) * (centsFrom(l.unit) ?? 0)); return ( ); })}
DescriptionQtyUnit price (R)Amount
set(i, { description: e.target.value })} /> set(i, { quantity: e.target.value })} /> set(i, { unit: e.target.value })} /> {money(amount)}
Total {money(editTotal(lines))}

Use a negative unit price for a discount.

); }