sales
This commit is contained in:
@@ -162,6 +162,26 @@ export function SalesDetailPage({ entity }: { entity: SalesDocumentEntity }) {
|
|||||||
<div className="mt-2 text-base font-bold text-text">${activeDocument.subtotal.toFixed(2)}</div>
|
<div className="mt-2 text-base font-bold text-text">${activeDocument.subtotal.toFixed(2)}</div>
|
||||||
</article>
|
</article>
|
||||||
</section>
|
</section>
|
||||||
|
<section className="grid gap-3 xl:grid-cols-4">
|
||||||
|
<article className="rounded-[24px] border border-line/70 bg-surface/90 px-3 py-3 shadow-panel">
|
||||||
|
<p className="text-xs font-semibold uppercase tracking-[0.18em] text-muted">Discount</p>
|
||||||
|
<div className="mt-2 text-base font-bold text-text">-${activeDocument.discountAmount.toFixed(2)}</div>
|
||||||
|
<div className="mt-1 text-xs text-muted">{activeDocument.discountPercent.toFixed(2)}%</div>
|
||||||
|
</article>
|
||||||
|
<article className="rounded-[24px] border border-line/70 bg-surface/90 px-3 py-3 shadow-panel">
|
||||||
|
<p className="text-xs font-semibold uppercase tracking-[0.18em] text-muted">Tax</p>
|
||||||
|
<div className="mt-2 text-base font-bold text-text">${activeDocument.taxAmount.toFixed(2)}</div>
|
||||||
|
<div className="mt-1 text-xs text-muted">{activeDocument.taxPercent.toFixed(2)}%</div>
|
||||||
|
</article>
|
||||||
|
<article className="rounded-[24px] border border-line/70 bg-surface/90 px-3 py-3 shadow-panel">
|
||||||
|
<p className="text-xs font-semibold uppercase tracking-[0.18em] text-muted">Freight</p>
|
||||||
|
<div className="mt-2 text-base font-bold text-text">${activeDocument.freightAmount.toFixed(2)}</div>
|
||||||
|
</article>
|
||||||
|
<article className="rounded-[24px] border border-line/70 bg-surface/90 px-3 py-3 shadow-panel">
|
||||||
|
<p className="text-xs font-semibold uppercase tracking-[0.18em] text-muted">Total</p>
|
||||||
|
<div className="mt-2 text-base font-bold text-text">${activeDocument.total.toFixed(2)}</div>
|
||||||
|
</article>
|
||||||
|
</section>
|
||||||
<div className="grid gap-3 xl:grid-cols-[minmax(0,1.05fr)_minmax(320px,0.95fr)]">
|
<div className="grid gap-3 xl:grid-cols-[minmax(0,1.05fr)_minmax(320px,0.95fr)]">
|
||||||
<article className="rounded-[28px] border border-line/70 bg-surface/90 p-4 shadow-panel 2xl:p-5">
|
<article className="rounded-[28px] border border-line/70 bg-surface/90 p-4 shadow-panel 2xl:p-5">
|
||||||
<p className="text-xs font-semibold uppercase tracking-[0.24em] text-muted">Customer</p>
|
<p className="text-xs font-semibold uppercase tracking-[0.24em] text-muted">Customer</p>
|
||||||
|
|||||||
@@ -24,6 +24,12 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|||||||
const [activeLinePicker, setActiveLinePicker] = useState<number | null>(null);
|
const [activeLinePicker, setActiveLinePicker] = useState<number | null>(null);
|
||||||
const [isSaving, setIsSaving] = useState(false);
|
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(() => {
|
useEffect(() => {
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return;
|
return;
|
||||||
@@ -46,6 +52,9 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|||||||
status: document.status,
|
status: document.status,
|
||||||
issueDate: document.issueDate,
|
issueDate: document.issueDate,
|
||||||
expiresAt: entity === "quote" ? document.expiresAt : null,
|
expiresAt: entity === "quote" ? document.expiresAt : null,
|
||||||
|
discountPercent: document.discountPercent,
|
||||||
|
taxPercent: document.taxPercent,
|
||||||
|
freightAmount: document.freightAmount,
|
||||||
notes: document.notes,
|
notes: document.notes,
|
||||||
lines: document.lines.map((line) => ({
|
lines: document.lines.map((line) => ({
|
||||||
itemId: line.itemId,
|
itemId: line.itemId,
|
||||||
@@ -74,6 +83,10 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|||||||
return customers.find((customer) => customer.id === customerId)?.name ?? "";
|
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) {
|
function updateLine(index: number, nextLine: SalesLineInput) {
|
||||||
setForm((current: SalesDocumentInput) => ({
|
setForm((current: SalesDocumentInput) => ({
|
||||||
...current,
|
...current,
|
||||||
@@ -201,6 +214,7 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|||||||
onMouseDown={(event) => {
|
onMouseDown={(event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
updateField("customerId", customer.id);
|
updateField("customerId", customer.id);
|
||||||
|
updateField("discountPercent", customer.resellerDiscountPercent);
|
||||||
setCustomerSearchTerm(customer.name);
|
setCustomerSearchTerm(customer.name);
|
||||||
setCustomerPickerOpen(false);
|
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">
|
<div className="mt-2 min-h-5 text-xs text-muted">
|
||||||
{form.customerId ? getSelectedCustomerName(form.customerId) : "No customer selected"}
|
{form.customerId ? getSelectedCustomerName(form.customerId) : "No customer selected"}
|
||||||
</div>
|
</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>
|
||||||
<label className="block">
|
<label className="block">
|
||||||
<span className="mb-2 block text-sm font-semibold text-text">Status</span>
|
<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"
|
className="w-full rounded-3xl border border-line/70 bg-page px-2 py-2 text-text outline-none transition focus:border-brand"
|
||||||
/>
|
/>
|
||||||
</label>
|
</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>
|
||||||
<section className="rounded-[28px] border border-line/70 bg-surface/90 p-4 shadow-panel 2xl:p-5">
|
<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">
|
<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">
|
<div className="mt-5 space-y-4">
|
||||||
{form.lines.map((line: SalesLineInput, index: number) => (
|
{form.lines.map((line: SalesLineInput, index: number) => (
|
||||||
<div key={index} className="rounded-3xl border border-line/70 bg-page/60 p-3">
|
<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">
|
<label className="block">
|
||||||
<span className="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-muted">SKU</span>
|
<span className="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-muted">SKU</span>
|
||||||
<div className="relative">
|
<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>
|
<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" />
|
<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>
|
</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">
|
<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">
|
<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
|
Remove
|
||||||
@@ -367,6 +428,24 @@ export function SalesFormPage({ entity, mode }: { entity: SalesDocumentEntity; m
|
|||||||
))}
|
))}
|
||||||
</div>
|
</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">
|
<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>
|
<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">
|
<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">
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ export function SalesListPage({ entity }: { entity: SalesDocumentEntity }) {
|
|||||||
<SalesStatusBadge status={document.status} />
|
<SalesStatusBadge status={document.status} />
|
||||||
</td>
|
</td>
|
||||||
<td className="px-2 py-2 text-muted">{new Date(document.issueDate).toLocaleDateString()}</td>
|
<td className="px-2 py-2 text-muted">{new Date(document.issueDate).toLocaleDateString()}</td>
|
||||||
<td className="px-2 py-2 text-muted">${document.subtotal.toFixed(2)}</td>
|
<td className="px-2 py-2 text-muted">${document.total.toFixed(2)}</td>
|
||||||
<td className="px-2 py-2 text-muted">{document.lineCount}</td>
|
<td className="px-2 py-2 text-muted">{document.lineCount}</td>
|
||||||
</tr>
|
</tr>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -54,6 +54,9 @@ export const emptySalesDocumentInput: SalesDocumentInput = {
|
|||||||
status: "DRAFT",
|
status: "DRAFT",
|
||||||
issueDate: new Date().toISOString(),
|
issueDate: new Date().toISOString(),
|
||||||
expiresAt: null,
|
expiresAt: null,
|
||||||
|
discountPercent: 0,
|
||||||
|
taxPercent: 0,
|
||||||
|
freightAmount: 0,
|
||||||
notes: "",
|
notes: "",
|
||||||
lines: [],
|
lines: [],
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
ALTER TABLE "SalesQuote" ADD COLUMN "discountPercent" REAL NOT NULL DEFAULT 0;
|
||||||
|
ALTER TABLE "SalesQuote" ADD COLUMN "taxPercent" REAL NOT NULL DEFAULT 0;
|
||||||
|
ALTER TABLE "SalesQuote" ADD COLUMN "freightAmount" REAL NOT NULL DEFAULT 0;
|
||||||
|
|
||||||
|
ALTER TABLE "SalesOrder" ADD COLUMN "discountPercent" REAL NOT NULL DEFAULT 0;
|
||||||
|
ALTER TABLE "SalesOrder" ADD COLUMN "taxPercent" REAL NOT NULL DEFAULT 0;
|
||||||
|
ALTER TABLE "SalesOrder" ADD COLUMN "freightAmount" REAL NOT NULL DEFAULT 0;
|
||||||
@@ -290,6 +290,9 @@ model SalesQuote {
|
|||||||
status String
|
status String
|
||||||
issueDate DateTime
|
issueDate DateTime
|
||||||
expiresAt DateTime?
|
expiresAt DateTime?
|
||||||
|
discountPercent Float @default(0)
|
||||||
|
taxPercent Float @default(0)
|
||||||
|
freightAmount Float @default(0)
|
||||||
notes String
|
notes String
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
@@ -320,6 +323,9 @@ model SalesOrder {
|
|||||||
customerId String
|
customerId String
|
||||||
status String
|
status String
|
||||||
issueDate DateTime
|
issueDate DateTime
|
||||||
|
discountPercent Float @default(0)
|
||||||
|
taxPercent Float @default(0)
|
||||||
|
freightAmount Float @default(0)
|
||||||
notes String
|
notes String
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ const quoteSchema = z.object({
|
|||||||
status: z.enum(salesDocumentStatuses),
|
status: z.enum(salesDocumentStatuses),
|
||||||
issueDate: z.string().datetime(),
|
issueDate: z.string().datetime(),
|
||||||
expiresAt: z.string().datetime().nullable(),
|
expiresAt: z.string().datetime().nullable(),
|
||||||
|
discountPercent: z.number().min(0).max(100),
|
||||||
|
taxPercent: z.number().min(0).max(100),
|
||||||
|
freightAmount: z.number().nonnegative(),
|
||||||
notes: z.string(),
|
notes: z.string(),
|
||||||
lines: z.array(salesLineSchema),
|
lines: z.array(salesLineSchema),
|
||||||
});
|
});
|
||||||
@@ -38,6 +41,9 @@ const orderSchema = z.object({
|
|||||||
customerId: z.string().trim().min(1),
|
customerId: z.string().trim().min(1),
|
||||||
status: z.enum(salesDocumentStatuses),
|
status: z.enum(salesDocumentStatuses),
|
||||||
issueDate: z.string().datetime(),
|
issueDate: z.string().datetime(),
|
||||||
|
discountPercent: z.number().min(0).max(100),
|
||||||
|
taxPercent: z.number().min(0).max(100),
|
||||||
|
freightAmount: z.number().nonnegative(),
|
||||||
notes: z.string(),
|
notes: z.string(),
|
||||||
lines: z.array(salesLineSchema),
|
lines: z.array(salesLineSchema),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ type SalesDocumentRecord = {
|
|||||||
status: string;
|
status: string;
|
||||||
issueDate: Date;
|
issueDate: Date;
|
||||||
expiresAt?: Date | null;
|
expiresAt?: Date | null;
|
||||||
|
discountPercent: number;
|
||||||
|
taxPercent: number;
|
||||||
|
freightAmount: number;
|
||||||
notes: string;
|
notes: string;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
@@ -60,6 +63,31 @@ const documentConfig = {
|
|||||||
},
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
|
function roundMoney(value: number) {
|
||||||
|
return Math.round(value * 100) / 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
function calculateTotals(subtotal: number, discountPercent: number, taxPercent: number, freightAmount: number) {
|
||||||
|
const normalizedSubtotal = roundMoney(subtotal);
|
||||||
|
const normalizedDiscountPercent = Number.isFinite(discountPercent) ? discountPercent : 0;
|
||||||
|
const normalizedTaxPercent = Number.isFinite(taxPercent) ? taxPercent : 0;
|
||||||
|
const normalizedFreight = roundMoney(Number.isFinite(freightAmount) ? freightAmount : 0);
|
||||||
|
const discountAmount = roundMoney(normalizedSubtotal * (normalizedDiscountPercent / 100));
|
||||||
|
const taxableSubtotal = roundMoney(normalizedSubtotal - discountAmount);
|
||||||
|
const taxAmount = roundMoney(taxableSubtotal * (normalizedTaxPercent / 100));
|
||||||
|
const total = roundMoney(taxableSubtotal + taxAmount + normalizedFreight);
|
||||||
|
|
||||||
|
return {
|
||||||
|
subtotal: normalizedSubtotal,
|
||||||
|
discountPercent: normalizedDiscountPercent,
|
||||||
|
discountAmount,
|
||||||
|
taxPercent: normalizedTaxPercent,
|
||||||
|
taxAmount,
|
||||||
|
freightAmount: normalizedFreight,
|
||||||
|
total,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function normalizeLines(lines: SalesLineInput[]) {
|
function normalizeLines(lines: SalesLineInput[]) {
|
||||||
return lines
|
return lines
|
||||||
.map((line, index) => ({
|
.map((line, index) => ({
|
||||||
@@ -117,6 +145,12 @@ function mapDocument(record: SalesDocumentRecord): SalesDocumentDetailDto {
|
|||||||
lineTotal: line.quantity * line.unitPrice,
|
lineTotal: line.quantity * line.unitPrice,
|
||||||
position: line.position,
|
position: line.position,
|
||||||
}));
|
}));
|
||||||
|
const totals = calculateTotals(
|
||||||
|
lines.reduce((sum, line) => sum + line.lineTotal, 0),
|
||||||
|
record.discountPercent,
|
||||||
|
record.taxPercent,
|
||||||
|
record.freightAmount
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: record.id,
|
id: record.id,
|
||||||
@@ -125,7 +159,13 @@ function mapDocument(record: SalesDocumentRecord): SalesDocumentDetailDto {
|
|||||||
customerName: record.customer.name,
|
customerName: record.customer.name,
|
||||||
customerEmail: record.customer.email,
|
customerEmail: record.customer.email,
|
||||||
status: record.status as SalesDocumentStatus,
|
status: record.status as SalesDocumentStatus,
|
||||||
subtotal: lines.reduce((sum, line) => sum + line.lineTotal, 0),
|
subtotal: totals.subtotal,
|
||||||
|
discountPercent: totals.discountPercent,
|
||||||
|
discountAmount: totals.discountAmount,
|
||||||
|
taxPercent: totals.taxPercent,
|
||||||
|
taxAmount: totals.taxAmount,
|
||||||
|
freightAmount: totals.freightAmount,
|
||||||
|
total: totals.total,
|
||||||
issueDate: record.issueDate.toISOString(),
|
issueDate: record.issueDate.toISOString(),
|
||||||
expiresAt: "expiresAt" in record && record.expiresAt ? record.expiresAt.toISOString() : null,
|
expiresAt: "expiresAt" in record && record.expiresAt ? record.expiresAt.toISOString() : null,
|
||||||
notes: record.notes,
|
notes: record.notes,
|
||||||
@@ -198,6 +238,7 @@ export async function listSalesCustomerOptions(): Promise<SalesCustomerOptionDto
|
|||||||
id: true,
|
id: true,
|
||||||
name: true,
|
name: true,
|
||||||
email: true,
|
email: true,
|
||||||
|
resellerDiscountPercent: true,
|
||||||
},
|
},
|
||||||
orderBy: [{ name: "asc" }],
|
orderBy: [{ name: "asc" }],
|
||||||
});
|
});
|
||||||
@@ -232,6 +273,12 @@ export async function listSalesDocuments(type: SalesDocumentType, filters: { q?:
|
|||||||
customerName: detail.customerName,
|
customerName: detail.customerName,
|
||||||
status: detail.status,
|
status: detail.status,
|
||||||
subtotal: detail.subtotal,
|
subtotal: detail.subtotal,
|
||||||
|
discountPercent: detail.discountPercent,
|
||||||
|
discountAmount: detail.discountAmount,
|
||||||
|
taxPercent: detail.taxPercent,
|
||||||
|
taxAmount: detail.taxAmount,
|
||||||
|
freightAmount: detail.freightAmount,
|
||||||
|
total: detail.total,
|
||||||
issueDate: detail.issueDate,
|
issueDate: detail.issueDate,
|
||||||
updatedAt: detail.updatedAt,
|
updatedAt: detail.updatedAt,
|
||||||
lineCount: detail.lineCount,
|
lineCount: detail.lineCount,
|
||||||
@@ -274,6 +321,9 @@ export async function createSalesDocument(type: SalesDocumentType, payload: Sale
|
|||||||
status: payload.status,
|
status: payload.status,
|
||||||
issueDate: new Date(payload.issueDate),
|
issueDate: new Date(payload.issueDate),
|
||||||
...(type === "QUOTE" ? { expiresAt: payload.expiresAt ? new Date(payload.expiresAt) : null } : {}),
|
...(type === "QUOTE" ? { expiresAt: payload.expiresAt ? new Date(payload.expiresAt) : null } : {}),
|
||||||
|
discountPercent: payload.discountPercent,
|
||||||
|
taxPercent: payload.taxPercent,
|
||||||
|
freightAmount: payload.freightAmount,
|
||||||
notes: payload.notes,
|
notes: payload.notes,
|
||||||
lines: {
|
lines: {
|
||||||
create: validatedLines.lines,
|
create: validatedLines.lines,
|
||||||
@@ -317,6 +367,9 @@ export async function updateSalesDocument(type: SalesDocumentType, documentId: s
|
|||||||
status: payload.status,
|
status: payload.status,
|
||||||
issueDate: new Date(payload.issueDate),
|
issueDate: new Date(payload.issueDate),
|
||||||
...(type === "QUOTE" ? { expiresAt: payload.expiresAt ? new Date(payload.expiresAt) : null } : {}),
|
...(type === "QUOTE" ? { expiresAt: payload.expiresAt ? new Date(payload.expiresAt) : null } : {}),
|
||||||
|
discountPercent: payload.discountPercent,
|
||||||
|
taxPercent: payload.taxPercent,
|
||||||
|
freightAmount: payload.freightAmount,
|
||||||
notes: payload.notes,
|
notes: payload.notes,
|
||||||
lines: {
|
lines: {
|
||||||
deleteMany: {},
|
deleteMany: {},
|
||||||
@@ -369,6 +422,9 @@ export async function convertQuoteToSalesOrder(quoteId: string) {
|
|||||||
customerId: mappedQuote.customerId,
|
customerId: mappedQuote.customerId,
|
||||||
status: "DRAFT",
|
status: "DRAFT",
|
||||||
issueDate: new Date(),
|
issueDate: new Date(),
|
||||||
|
discountPercent: mappedQuote.discountPercent,
|
||||||
|
taxPercent: mappedQuote.taxPercent,
|
||||||
|
freightAmount: mappedQuote.freightAmount,
|
||||||
notes: mappedQuote.notes ? `Converted from ${mappedQuote.documentNumber}\n\n${mappedQuote.notes}` : `Converted from ${mappedQuote.documentNumber}`,
|
notes: mappedQuote.notes ? `Converted from ${mappedQuote.documentNumber}\n\n${mappedQuote.notes}` : `Converted from ${mappedQuote.documentNumber}`,
|
||||||
lines: {
|
lines: {
|
||||||
create: mappedQuote.lines.map((line) => ({
|
create: mappedQuote.lines.map((line) => ({
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ export interface SalesCustomerOptionDto {
|
|||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
email: string;
|
email: string;
|
||||||
|
resellerDiscountPercent: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SalesLineDto {
|
export interface SalesLineDto {
|
||||||
@@ -40,6 +41,12 @@ export interface SalesDocumentSummaryDto {
|
|||||||
customerName: string;
|
customerName: string;
|
||||||
status: SalesDocumentStatus;
|
status: SalesDocumentStatus;
|
||||||
subtotal: number;
|
subtotal: number;
|
||||||
|
discountPercent: number;
|
||||||
|
discountAmount: number;
|
||||||
|
taxPercent: number;
|
||||||
|
taxAmount: number;
|
||||||
|
freightAmount: number;
|
||||||
|
total: number;
|
||||||
issueDate: string;
|
issueDate: string;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
lineCount: number;
|
lineCount: number;
|
||||||
@@ -58,6 +65,9 @@ export interface SalesDocumentInput {
|
|||||||
status: SalesDocumentStatus;
|
status: SalesDocumentStatus;
|
||||||
issueDate: string;
|
issueDate: string;
|
||||||
expiresAt: string | null;
|
expiresAt: string | null;
|
||||||
|
discountPercent: number;
|
||||||
|
taxPercent: number;
|
||||||
|
freightAmount: number;
|
||||||
notes: string;
|
notes: string;
|
||||||
lines: SalesLineInput[];
|
lines: SalesLineInput[];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user