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): never convert a quote to a sales order without a location (#1916)

Brad Barbin641c9c9d2673
diff --git a/apps/erp/app/modules/sales/ui/Quotes/QuoteProperties.tsx b/apps/erp/app/modules/sales/ui/Quotes/QuoteProperties.tsx
index f47563abf7..c24a6d7bf8 100644
--- a/apps/erp/app/modules/sales/ui/Quotes/QuoteProperties.tsx
+++ b/apps/erp/app/modules/sales/ui/Quotes/QuoteProperties.tsx
@@ -361,7 +361,6 @@ const QuoteProperties = () => {
onUpdate("locationId", value.value);
}
}}
- termId="quote"
/>
</ValidatedForm>
diff --git a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderProperties.tsx b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderProperties.tsx
index 0d99d6c8f7..6edf27e5e4 100644
--- a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderProperties.tsx
+++ b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderProperties.tsx
@@ -384,7 +384,6 @@ const SalesOrderProperties = () => {
onUpdate("locationId", value.value);
}
}}
- termId="sales-order"
/>
</ValidatedForm>
diff --git a/packages/jobs/src/inngest/functions/integrations/paperless-parts.ts b/packages/jobs/src/inngest/functions/integrations/paperless-parts.ts
index e713ea50f9..3711e7f21c 100644
--- a/packages/jobs/src/inngest/functions/integrations/paperless-parts.ts
+++ b/packages/jobs/src/inngest/functions/integrations/paperless-parts.ts
@@ -281,6 +281,7 @@ export const paperlessPartsFunction = inngest.createFunction(
customerId: quoteCustomerId,
customerContactId: quoteCustomerContactId,
quoteId: quoteReadableId,
+ locationId: quoteLocationId,
status: "Draft" as const,
currencyCode: company.data.baseCurrencyCode,
createdBy: quoteCreatedBy,
diff --git a/packages/server-functions/src/convert/index.ts b/packages/server-functions/src/convert/index.ts
index 54c8d685a8..a0da0c6e81 100644
--- a/packages/server-functions/src/convert/index.ts
+++ b/packages/server-functions/src/convert/index.ts
@@ -573,6 +573,25 @@ const convert = defineServerFn({
? "To Ship and Invoice"
: "To Invoice";
+ // An order always has a location. A quote can reach here without
+ // one (Paperless Parts imports never set it), so fall back to the
+ // quote's shipment location, then the company's first location.
+ const locationId =
+ quote.data.locationId ??
+ quoteShipping.data.locationId ??
+ (
+ await trx
+ .selectFrom("location")
+ .select("id")
+ .where("companyId", "=", companyId)
+ .orderBy("createdAt")
+ .orderBy("id")
+ .executeTakeFirst()
+ )?.id;
+ if (!locationId) {
+ throw new Error("The company has no location for the sales order");
+ }
+
const salesOrder = await trx
.insertInto("salesOrder")
.values([
@@ -586,7 +605,7 @@ const convert = defineServerFn({
quote.data.customerEngineeringContactId,
customerLocationId: quote.data.customerLocationId,
customerReference: purchaseOrderNumber ?? "",
- locationId: quote.data.locationId,
+ locationId,
salesPersonId: quote.data.salesPersonId ?? userId,
status: salesOrderStatus,
createdBy: userId,
@@ -622,12 +641,14 @@ const convert = defineServerFn({
// One read for every picked line's default bin, keyed by item and location.
const pickedLines = selectedQuoteLines.flatMap((line) => {
- const locationId = line.locationId ?? quote.data.locationId;
- return line.id &&
- line.itemId &&
- locationId &&
- line.methodType !== "Make to Order"
- ? [{ id: line.id, itemId: line.itemId, locationId }]
+ return line.id && line.itemId && line.methodType !== "Make to Order"
+ ? [
+ {
+ id: line.id,
+ itemId: line.itemId,
+ locationId: line.locationId ?? locationId
+ }
+ ]
: [];
});
const pickMethods = pickedLines.length
@@ -685,7 +706,7 @@ const convert = defineServerFn({
nonTaxableAddOnCost: addOn - taxableAddOn,
description: line.description,
itemId: line.itemId,
- locationId: line.locationId ?? quote.data.locationId,
+ locationId: line.locationId ?? locationId,
methodType: line.methodType,
storageUnitId: pickMethodDefaultsByLineId.get(line.id!) ?? null,
internalNotes: toJson(line.internalNotes),
@@ -732,6 +753,7 @@ const convert = defineServerFn({
.values({
...quoteShipping.data,
id: insertedSalesOrderId,
+ locationId: quoteShipping.data.locationId ?? locationId,
receiptRequestedDate:
quoteShipping.data.receiptRequestedDate ?? receiptPromisedDate,
receiptPromisedDate
diff --git a/packages/server-functions/src/convert/quote-location.test.ts b/packages/server-functions/src/convert/quote-location.test.ts
new file mode 100644
index 0000000000..a86fafafa5
--- /dev/null
+++ b/packages/server-functions/src/convert/quote-location.test.ts
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
+// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+
+// A quote with no location (Paperless Parts imports never set one) still
+// converts to a sales order with a location on the order, its lines and its
+// shipment: the company's first location.
+
+import { expect, vi } from "vitest";
+import {
+ alwaysPostFixture,
+ seed,
+ USER,
+ unwrap
+} from "../always-post-test-fixture";
+import { databaseTest } from "../local-database-test-fixture";
+import convert from "./index";
+
+// `convert` builds a service-role client up front; the quote path never uses
+// it, and the real one pulls in the browser UI package.
+vi.mock("@carbon/auth", () => ({ getCarbonClient: () => ({}) }));
+vi.mock("@carbon/env", () => ({ SUPABASE_SERVICE_ROLE_KEY: "test" }));
+
+databaseTest(
+ "a quote with no location converts to an order at the first location",
+ async () => {
+ const f = await alwaysPostFixture();
+ try {
+ const quoteId = `${f.prefix}-quote`;
+ const quoteLineId = `${f.prefix}-quote-line`;
+ await seed(f, async (trx) => {
+ await trx
+ .insertInto("sequence")
+ .values({
+ table: "salesOrder",
+ name: "Sales Order",
+ prefix: "SO-",
+ companyId: f.companyId
+ })
+ .execute();
+ await trx
+ .insertInto("quote")
+ .values({
+ id: quoteId,
+ quoteId: "Q-1",
+ customerId: f.customerId,
+ status: "Sent",
+ currencyCode: "USD",
+ exchangeRate: 1,
+ companyId: f.companyId,
+ createdBy: USER
+ })
+ .execute();
+ await trx
+ .insertInto("quotePayment")
+ .values({ id: quoteId, companyId: f.companyId })
+ .execute();
+ await trx
+ .insertInto("quoteShipment")
+ .values({ id: quoteId, companyId: f.companyId })
+ .execute();
+ await trx
+ .insertInto("quoteLine")
+ .values({
+ id: quoteLineId,
+ quoteId,
+ itemId: f.partId,
+ itemType: "Part",
+ description: "Bracket",
+ methodType: "Pull from Inventory",
+ unitOfMeasureCode: "EA",
+ quantity: [4],
+ companyId: f.companyId,
+ createdBy: USER
+ })
+ .execute();
+ await trx
+ .insertInto("quoteLinePrice")
+ .values({
+ quoteId,
+ quoteLineId,
+ quantity: 4,
+ unitPrice: 25,
+ leadTime: 0,
+ companyId: f.companyId,
+ createdBy: USER
+ })
+ .execute();
+ });
+
+ const { convertedId } = unwrap(
+ await convert(f.ctx, {
+ type: "quoteToSalesOrder",
+ id: quoteId,
+ selectedLines: { [quoteLineId]: { quantity: 4 } }
+ })
+ );
+
+ const order = await f.db
+ .selectFrom("salesOrder")
+ .select("locationId")
+ .where("id", "=", convertedId!)
+ .executeTakeFirstOrThrow();
+ const lines = await f.db
+ .selectFrom("salesOrderLine")
+ .select("locationId")
+ .where("salesOrderId", "=", convertedId!)
+ .execute();
+ const shipment = await f.db
+ .selectFrom("salesOrderShipment")
+ .select("locationId")
+ .where("id", "=", convertedId!)
+ .executeTakeFirstOrThrow();
+
+ expect(order.locationId).toBe(f.locationId);
+ expect(lines.map((line) => line.locationId)).toEqual([f.locationId]);
+ expect(shipment.locationId).toBe(f.locationId);
+ } finally {
+ await f.cleanup();
+ }
+ }
+);