|
|
|
|
@@ -24,6 +24,12 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|
|
|
|
const [activeLinePicker, setActiveLinePicker] = useState<number | null>(null);
|
|
|
|
|
const [isSaving, setIsSaving] = useState(false);
|
|
|
|
|
|
|
|
|
|
const subtotal = form.lines.reduce((sum, line) => sum + line.quantity * line.unitPrice, 0);
|
|
|
|
|
const discountAmount = subtotal * (form.discountPercent / 100);
|
|
|
|
|
const taxableSubtotal = subtotal - discountAmount;
|
|
|
|
|
const taxAmount = taxableSubtotal * (form.taxPercent / 100);
|
|
|
|
|
const total = taxableSubtotal + taxAmount + form.freightAmount;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!token) {
|
|
|
|
|
return;
|
|
|
|
|
@@ -46,6 +52,9 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|
|
|
|
status: document.status,
|
|
|
|
|
issueDate: document.issueDate,
|
|
|
|
|
expiresAt: entity === "quote" ? document.expiresAt : null,
|
|
|
|
|
discountPercent: document.discountPercent,
|
|
|
|
|
taxPercent: document.taxPercent,
|
|
|
|
|
freightAmount: document.freightAmount,
|
|
|
|
|
notes: document.notes,
|
|
|
|
|
lines: document.lines.map((line) => ({
|
|
|
|
|
itemId: line.itemId,
|
|
|
|
|
@@ -74,6 +83,10 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|
|
|
|
return customers.find((customer) => customer.id === customerId)?.name ?? "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSelectedCustomer(customerId: string) {
|
|
|
|
|
return customers.find((customer) => customer.id === customerId) ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateLine(index: number, nextLine: SalesLineInput) {
|
|
|
|
|
setForm((current: SalesDocumentInput) => ({
|
|
|
|
|
...current,
|
|
|
|
|
@@ -201,6 +214,7 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|
|
|
|
onMouseDown={(event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
updateField("customerId", customer.id);
|
|
|
|
|
updateField("discountPercent", customer.resellerDiscountPercent);
|
|
|
|
|
setCustomerSearchTerm(customer.name);
|
|
|
|
|
setCustomerPickerOpen(false);
|
|
|
|
|
}}
|
|
|
|
|
@@ -226,6 +240,11 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|
|
|
|
<div className="mt-2 min-h-5 text-xs text-muted">
|
|
|
|
|
{form.customerId ? getSelectedCustomerName(form.customerId) : "No customer selected"}
|
|
|
|
|
</div>
|
|
|
|
|
{form.customerId ? (
|
|
|
|
|
<div className="mt-1 text-xs text-muted">
|
|
|
|
|
Default reseller discount: {getSelectedCustomer(form.customerId)?.resellerDiscountPercent.toFixed(2) ?? "0.00"}%
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
</label>
|
|
|
|
|
<label className="block">
|
|
|
|
|
<span className="mb-2 block text-sm font-semibold text-text">Status</span>
|
|
|
|
|
@@ -271,6 +290,43 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|
|
|
|
className="w-full rounded-3xl border border-line/70 bg-page px-2 py-2 text-text outline-none transition focus:border-brand"
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
<div className="grid gap-3 xl:grid-cols-3">
|
|
|
|
|
<label className="block">
|
|
|
|
|
<span className="mb-2 block text-sm font-semibold text-text">Discount %</span>
|
|
|
|
|
<input
|
|
|
|
|
type="number"
|
|
|
|
|
min={0}
|
|
|
|
|
max={100}
|
|
|
|
|
step={0.01}
|
|
|
|
|
value={form.discountPercent}
|
|
|
|
|
onChange={(event) => updateField("discountPercent", Number(event.target.value) || 0)}
|
|
|
|
|
className="w-full rounded-2xl border border-line/70 bg-page px-2 py-2 text-text outline-none transition focus:border-brand"
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
<label className="block">
|
|
|
|
|
<span className="mb-2 block text-sm font-semibold text-text">Tax %</span>
|
|
|
|
|
<input
|
|
|
|
|
type="number"
|
|
|
|
|
min={0}
|
|
|
|
|
max={100}
|
|
|
|
|
step={0.01}
|
|
|
|
|
value={form.taxPercent}
|
|
|
|
|
onChange={(event) => updateField("taxPercent", Number(event.target.value) || 0)}
|
|
|
|
|
className="w-full rounded-2xl border border-line/70 bg-page px-2 py-2 text-text outline-none transition focus:border-brand"
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
<label className="block">
|
|
|
|
|
<span className="mb-2 block text-sm font-semibold text-text">Freight</span>
|
|
|
|
|
<input
|
|
|
|
|
type="number"
|
|
|
|
|
min={0}
|
|
|
|
|
step={0.01}
|
|
|
|
|
value={form.freightAmount}
|
|
|
|
|
onChange={(event) => updateField("freightAmount", Number(event.target.value) || 0)}
|
|
|
|
|
className="w-full rounded-2xl border border-line/70 bg-page px-2 py-2 text-text outline-none transition focus:border-brand"
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
<section className="rounded-[28px] border border-line/70 bg-surface/90 p-4 shadow-panel 2xl:p-5">
|
|
|
|
|
<div className="flex flex-col gap-3 lg:flex-row lg:items-start lg:justify-between">
|
|
|
|
|
@@ -290,7 +346,7 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|
|
|
|
<div className="mt-5 space-y-4">
|
|
|
|
|
{form.lines.map((line: SalesLineInput, index: number) => (
|
|
|
|
|
<div key={index} className="rounded-3xl border border-line/70 bg-page/60 p-3">
|
|
|
|
|
<div className="grid gap-3 xl:grid-cols-[1.25fr_1.4fr_0.55fr_0.55fr_0.75fr_auto]">
|
|
|
|
|
<div className="grid gap-3 xl:grid-cols-[1.15fr_1.25fr_0.5fr_0.55fr_0.7fr_0.75fr_auto]">
|
|
|
|
|
<label className="block">
|
|
|
|
|
<span className="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-muted">SKU</span>
|
|
|
|
|
<div className="relative">
|
|
|
|
|
@@ -357,6 +413,11 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|
|
|
|
<span className="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-muted">Unit Price</span>
|
|
|
|
|
<input type="number" min={0} step={0.01} value={line.unitPrice} onChange={(event) => updateLine(index, { ...line, unitPrice: Number(event.target.value) })} className="w-full rounded-2xl border border-line/70 bg-surface px-2 py-2 text-text outline-none transition focus:border-brand" />
|
|
|
|
|
</label>
|
|
|
|
|
<div className="flex items-end">
|
|
|
|
|
<div className="w-full rounded-2xl border border-line/70 bg-surface px-2 py-2 text-sm text-text">
|
|
|
|
|
${(line.quantity * line.unitPrice).toFixed(2)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-end">
|
|
|
|
|
<button type="button" onClick={() => removeLine(index)} className="rounded-2xl border border-rose-400/40 px-2 py-2 text-sm font-semibold text-rose-700 dark:text-rose-300">
|
|
|
|
|
Remove
|
|
|
|
|
@@ -367,6 +428,24 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<div className="mt-5 grid gap-3 md:grid-cols-2 xl:grid-cols-4">
|
|
|
|
|
<div className="rounded-2xl border border-line/70 bg-page/60 px-2 py-2 text-sm">
|
|
|
|
|
<div className="text-xs font-semibold uppercase tracking-[0.16em] text-muted">Subtotal</div>
|
|
|
|
|
<div className="mt-1 font-semibold text-text">${subtotal.toFixed(2)}</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="rounded-2xl border border-line/70 bg-page/60 px-2 py-2 text-sm">
|
|
|
|
|
<div className="text-xs font-semibold uppercase tracking-[0.16em] text-muted">Discount</div>
|
|
|
|
|
<div className="mt-1 font-semibold text-text">-${discountAmount.toFixed(2)}</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="rounded-2xl border border-line/70 bg-page/60 px-2 py-2 text-sm">
|
|
|
|
|
<div className="text-xs font-semibold uppercase tracking-[0.16em] text-muted">Tax + Freight</div>
|
|
|
|
|
<div className="mt-1 font-semibold text-text">${(taxAmount + form.freightAmount).toFixed(2)}</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="rounded-2xl border border-line/70 bg-page/60 px-2 py-2 text-sm">
|
|
|
|
|
<div className="text-xs font-semibold uppercase tracking-[0.16em] text-muted">Total</div>
|
|
|
|
|
<div className="mt-1 font-semibold text-text">${total.toFixed(2)}</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="mt-6 flex flex-col gap-3 rounded-2xl border border-line/70 bg-page/70 px-2 py-2 sm:flex-row sm:items-center sm:justify-between">
|
|
|
|
|
<span className="min-w-0 text-sm text-muted">{status}</span>
|
|
|
|
|
<button type="submit" disabled={isSaving} className="rounded-2xl bg-brand px-2 py-2 text-sm font-semibold text-white disabled:cursor-not-allowed disabled:opacity-60">
|
|
|
|
|
|