Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(sales): reject repeated quantity breaks on a quote line so each quantity keeps its own price

aashu0d8dc6910dd3
diff --git a/apps/erp/app/modules/sales/sales.models.test.ts b/apps/erp/app/modules/sales/sales.models.test.ts
index eb3fdb9324..fb221d7bcb 100644
--- a/apps/erp/app/modules/sales/sales.models.test.ts
+++ b/apps/erp/app/modules/sales/sales.models.test.ts
@@ -18,7 +18,7 @@ vi.mock("@lingui/core/macro", () => ({
: String(strings)
}));
-const { quoteValidator } = await import("./sales.models");
+const { quoteLineValidator, quoteValidator } = await import("./sales.models");
// `quote.internalNotes` is a `json` column that the sales-order conversion
// copies through Kysely. A bare string stored there (which `notes: z.any()`
@@ -54,3 +54,21 @@ describe("quoteValidator.notes", () => {
);
});
});
+
+// quoteLinePrice is keyed by (quoteLineId, quantity), so two equal breaks on a
+// line would share one price row and edit together.
+describe("quoteLineValidator.quantity", () => {
+ it("rejects repeated quantity breaks", () => {
+ const result = quoteLineValidator.shape.quantity.safeParse([10, 10, 25]);
+ expect(result.success).toBe(false);
+ expect(result.error?.issues[0]?.message).toBe(
+ "Each quantity must be different"
+ );
+ });
+
+ it("accepts distinct quantity breaks", () => {
+ expect(
+ quoteLineValidator.shape.quantity.safeParse([10, 20, 25]).success
+ ).toBe(true);
+ });
+});
diff --git a/apps/erp/app/modules/sales/sales.models.ts b/apps/erp/app/modules/sales/sales.models.ts
index 316b5f9fff..d93b8ca772 100644
--- a/apps/erp/app/modules/sales/sales.models.ts
+++ b/apps/erp/app/modules/sales/sales.models.ts
@@ -490,9 +490,13 @@ export const quoteLineValidator = z.object({
unitOfMeasureCode: zfd.text(
z.string().min(1, { message: "Unit of measure is required" })
),
- quantity: z.array(
- zfd.numeric(z.number().min(0.00001, { message: "Quantity is required" }))
- ),
+ quantity: z
+ .array(
+ zfd.numeric(z.number().min(0.00001, { message: "Quantity is required" }))
+ )
+ .refine((quantities) => new Set(quantities).size === quantities.length, {
+ message: "Each quantity must be different"
+ }),
modelUploadId: zfd.text(z.string().optional()),
noQuoteReason: zfd.text(z.string().optional()),
taxPercent: zfd.numeric(
diff --git a/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.details.tsx b/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
index c1ba04b82d..e264a3edeb 100644
--- a/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
+++ b/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
@@ -160,9 +160,10 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
...line.data,
// Present quantity breaks least-to-greatest everywhere they're consumed
// (line form, costing grid, pricing grid). Preserve null so the `?? [1]`
- // fallbacks downstream still apply.
+ // fallbacks downstream still apply. Distinct: a line saved with a
+ // repeated break must not render twice (one price row per quantity).
quantity: line.data.quantity
- ? [...line.data.quantity].sort((a, b) => a - b)
+ ? [...new Set(line.data.quantity)].sort((a, b) => a - b)
: line.data.quantity
},
operations: operations?.data ?? [],