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.

feat: pull customer and supplier locations and contacts automatically

Brad Barbin4c410504f364
diff --git a/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceForm.tsx b/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceForm.tsx
index bde780aa44..de83f5ef9b 100644
--- a/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceForm.tsx
+++ b/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceForm.tsx
@@ -65,7 +65,6 @@ const PurchaseInvoiceForm = ({ initialValues }: PurchaseInvoiceFormProps) => {
const onSupplierChange = async (
newValue: {
value: string | undefined;
- label: string;
} | null
) => {
setSupplier({ id: newValue?.value });
@@ -77,7 +76,6 @@ const PurchaseInvoiceForm = ({ initialValues }: PurchaseInvoiceFormProps) => {
const onInvoiceSupplierChange = async (
newValue: {
value: string | undefined;
- label: string;
} | null
) => {
if (!carbon) {
@@ -100,7 +98,9 @@ const PurchaseInvoiceForm = ({ initialValues }: PurchaseInvoiceFormProps) => {
const [supplierData, paymentTermData] = await Promise.all([
carbon
?.from("supplier")
- .select("currencyCode")
+ .select(
+ "currencyCode, purchasingContactId, supplierShipping!supplierId(shippingSupplierLocationId)"
+ )
.eq("id", newValue.value)
.single(),
carbon
@@ -117,9 +117,13 @@ const PurchaseInvoiceForm = ({ initialValues }: PurchaseInvoiceFormProps) => {
...prev,
id: newValue.value,
invoiceSupplierContactId:
- paymentTermData.data.invoiceSupplierContactId ?? undefined,
+ paymentTermData.data.invoiceSupplierContactId ??
+ supplierData.data.purchasingContactId ??
+ undefined,
invoiceSupplierLocationId:
- paymentTermData.data.invoiceSupplierLocationId ?? undefined,
+ paymentTermData.data.invoiceSupplierLocationId ??
+ supplierData.data.supplierShipping?.shippingSupplierLocationId ??
+ undefined,
currencyCode: supplierData.data.currencyCode ?? undefined,
paymentTermId: paymentTermData.data.paymentTermId ?? undefined
}));
diff --git a/apps/erp/app/modules/invoicing/ui/SalesInvoice/SalesInvoiceForm.tsx b/apps/erp/app/modules/invoicing/ui/SalesInvoice/SalesInvoiceForm.tsx
index 4e9690dd8f..f404135630 100644
--- a/apps/erp/app/modules/invoicing/ui/SalesInvoice/SalesInvoiceForm.tsx
+++ b/apps/erp/app/modules/invoicing/ui/SalesInvoice/SalesInvoiceForm.tsx
@@ -65,7 +65,6 @@ const SalesInvoiceForm = ({ initialValues }: SalesInvoiceFormProps) => {
const onCustomerChange = async (
newValue: {
value: string | undefined;
- label: string;
} | null
) => {
setCustomer({ id: newValue?.value });
@@ -77,7 +76,6 @@ const SalesInvoiceForm = ({ initialValues }: SalesInvoiceFormProps) => {
const onInvoiceCustomerChange = async (
newValue: {
value: string | undefined;
- label: string;
} | null
) => {
if (!carbon) {
@@ -100,7 +98,9 @@ const SalesInvoiceForm = ({ initialValues }: SalesInvoiceFormProps) => {
const [customerData, paymentTermData] = await Promise.all([
carbon
?.from("customer")
- .select("currencyCode")
+ .select(
+ "currencyCode, salesContactId, customerShipping!customerId(shippingCustomerLocationId)"
+ )
.eq("id", newValue.value)
.single(),
carbon
@@ -117,9 +117,13 @@ const SalesInvoiceForm = ({ initialValues }: SalesInvoiceFormProps) => {
...prev,
id: newValue.value,
invoiceCustomerContactId:
- paymentTermData.data.invoiceCustomerContactId ?? undefined,
+ paymentTermData.data.invoiceCustomerContactId ??
+ customerData.data.salesContactId ??
+ undefined,
invoiceCustomerLocationId:
- paymentTermData.data.invoiceCustomerLocationId ?? undefined,
+ paymentTermData.data.invoiceCustomerLocationId ??
+ customerData.data.customerShipping?.shippingCustomerLocationId ??
+ undefined,
currencyCode: customerData.data.currencyCode ?? undefined,
paymentTermId: paymentTermData.data.paymentTermId ?? undefined
}));
diff --git a/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderForm.tsx b/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderForm.tsx
index f2f0a97899..6275e2cebd 100644
--- a/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderForm.tsx
+++ b/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderForm.tsx
@@ -43,16 +43,17 @@ const PurchaseOrderForm = ({ initialValues }: PurchaseOrderFormProps) => {
const [supplier, setSupplier] = useState<{
id: string | undefined;
currencyCode: string | undefined;
+ supplierContactId: string | undefined;
}>({
id: initialValues.supplierId,
- currencyCode: initialValues.currencyCode
+ currencyCode: initialValues.currencyCode,
+ supplierContactId: initialValues.supplierContactId
});
const isEditing = initialValues.id !== undefined;
const onSupplierChange = async (
newValue: {
value: string | undefined;
- label: string;
} | null
) => {
if (!carbon) {
@@ -65,13 +66,14 @@ const PurchaseOrderForm = ({ initialValues }: PurchaseOrderFormProps) => {
// update the supplier immediately
setSupplier({
id: newValue?.value,
- currencyCode: undefined
+ currencyCode: undefined,
+ supplierContactId: undefined
});
});
const { data, error } = await carbon
?.from("supplier")
- .select("currencyCode")
+ .select("currencyCode, purchasingContactId")
.eq("id", newValue.value)
.single();
if (error) {
@@ -79,13 +81,15 @@ const PurchaseOrderForm = ({ initialValues }: PurchaseOrderFormProps) => {
} else {
setSupplier((prev) => ({
...prev,
- currencyCode: data.currencyCode ?? undefined
+ currencyCode: data.currencyCode ?? undefined,
+ supplierContactId: data.purchasingContactId ?? undefined
}));
}
} else {
setSupplier({
id: undefined,
- currencyCode: undefined
+ currencyCode: undefined,
+ supplierContactId: undefined
});
}
};
@@ -143,6 +147,7 @@ const PurchaseOrderForm = ({ initialValues }: PurchaseOrderFormProps) => {
name="supplierContactId"
label="Supplier Contact"
supplier={supplier.id}
+ value={supplier.supplierContactId}
/>
<Currency
diff --git a/apps/erp/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteForm.tsx b/apps/erp/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteForm.tsx
index c9f60c4b71..c5b39559a7 100644
--- a/apps/erp/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteForm.tsx
+++ b/apps/erp/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteForm.tsx
@@ -50,9 +50,11 @@ const SupplierQuoteForm = ({ initialValues }: SupplierQuoteFormProps) => {
const [supplier, setSupplier] = useState<{
id: string | undefined;
currencyCode: string | undefined;
+ supplierContactId: string | undefined;
}>({
id: initialValues.supplierId,
- currencyCode: initialValues.currencyCode
+ currencyCode: initialValues.currencyCode,
+ supplierContactId: initialValues.supplierContactId
});
const isDisabled = initialValues?.status !== "Draft";
@@ -63,7 +65,6 @@ const SupplierQuoteForm = ({ initialValues }: SupplierQuoteFormProps) => {
const onSupplierChange = async (
newValue: {
value: string | undefined;
- label: string;
} | null
) => {
if (!carbon) {
@@ -76,13 +77,14 @@ const SupplierQuoteForm = ({ initialValues }: SupplierQuoteFormProps) => {
// update the supplier immediately
setSupplier({
id: newValue?.value,
- currencyCode: undefined
+ currencyCode: undefined,
+ supplierContactId: undefined
});
});
const { data, error } = await carbon
?.from("supplier")
- .select("currencyCode")
+ .select("currencyCode, purchasingContactId")
.eq("id", newValue.value)
.single();
if (error) {
@@ -90,13 +92,15 @@ const SupplierQuoteForm = ({ initialValues }: SupplierQuoteFormProps) => {
} else {
setSupplier((prev) => ({
...prev,
- currencyCode: data.currencyCode ?? undefined
+ currencyCode: data.currencyCode ?? undefined,
+ supplierContactId: data.purchasingContactId ?? undefined
}));
}
} else {
setSupplier({
id: undefined,
- currencyCode: undefined
+ currencyCode: undefined,
+ supplierContactId: undefined
});
}
};
@@ -156,6 +160,7 @@ const SupplierQuoteForm = ({ initialValues }: SupplierQuoteFormProps) => {
label="Supplier Contact"
isOptional
supplier={supplier.id}
+ value={supplier.supplierContactId}
/>
<DatePicker name="quotedDate" label="Quoted Date" />
<DatePicker
diff --git a/apps/erp/app/modules/sales/ui/Quotes/QuoteForm.tsx b/apps/erp/app/modules/sales/ui/Quotes/QuoteForm.tsx
index dbfa4f103e..3766504e7b 100644
--- a/apps/erp/app/modules/sales/ui/Quotes/QuoteForm.tsx
+++ b/apps/erp/app/modules/sales/ui/Quotes/QuoteForm.tsx
@@ -48,9 +48,13 @@ const QuoteForm = ({ initialValues }: QuoteFormProps) => {
const [customer, setCustomer] = useState<{
id: string | undefined;
currencyCode: string | undefined;
+ customerContactId: string | undefined;
+ customerLocationId: string | undefined;
}>({
id: initialValues.customerId,
- currencyCode: initialValues.currencyCode
+ currencyCode: initialValues.currencyCode,
+ customerContactId: initialValues.customerContactId,
+ customerLocationId: initialValues.customerLocationId
});
const isCustomer = permissions.is("customer");
const isDisabled = initialValues?.status !== "Draft";
@@ -61,7 +65,6 @@ const QuoteForm = ({ initialValues }: QuoteFormProps) => {
const onCustomerChange = async (
newValue: {
value: string | undefined;
- label: string;
} | null
) => {
if (!carbon) {
@@ -74,13 +77,17 @@ const QuoteForm = ({ initialValues }: QuoteFormProps) => {
// update the customer immediately
setCustomer({
id: newValue?.value,
- currencyCode: undefined
+ currencyCode: undefined,
+ customerContactId: undefined,
+ customerLocationId: undefined
});
});
const { data, error } = await carbon
?.from("customer")
- .select("currencyCode")
+ .select(
+ "currencyCode, salesContactId, customerShipping!customerId(shippingCustomerLocationId)"
+ )
.eq("id", newValue.value)
.single();
if (error) {
@@ -88,13 +95,18 @@ const QuoteForm = ({ initialValues }: QuoteFormProps) => {
} else {
setCustomer((prev) => ({
...prev,
- currencyCode: data.currencyCode ?? undefined
+ currencyCode: data.currencyCode ?? undefined,
+ customerContactId: data.salesContactId ?? undefined,
+ customerLocationId:
+ data.customerShipping?.shippingCustomerLocationId ?? undefined
}));
}
} else {
setCustomer({
id: undefined,
- currencyCode: undefined
+ currencyCode: undefined,
+ customerContactId: undefined,
+ customerLocationId: undefined
});
}
};
@@ -136,7 +148,11 @@ const QuoteForm = ({ initialValues }: QuoteFormProps) => {
autoFocus={!isEditing}
name="customerId"
label="Customer"
- onChange={onCustomerChange}
+ onChange={(newValue) => {
+ if (newValue?.value) {
+ onCustomerChange(newValue);
+ }
+ }}
/>
<Input name="customerReference" label="Customer RFQ" />
<CustomerContact
@@ -144,6 +160,7 @@ const QuoteForm = ({ initialValues }: QuoteFormProps) => {
label="Purchasing Contact"
isOptional
customer={customer.id}
+ value={customer.customerContactId}
/>
<CustomerContact
name="customerEngineeringContactId"
@@ -156,11 +173,11 @@ const QuoteForm = ({ initialValues }: QuoteFormProps) => {
label="Customer Location"
isOptional
customer={customer.id}
+ value={customer.customerLocationId}
/>
<Employee name="salesPersonId" label="Sales Person" isOptional />
<Employee name="estimatorId" label="Estimator" isOptional />
<Location name="locationId" label="Quote Location" />
-
<DatePicker
name="dueDate"
label="Due Date"
@@ -171,7 +188,6 @@ const QuoteForm = ({ initialValues }: QuoteFormProps) => {
label="Expiration Date"
isDisabled={isCustomer}
/>
-
<Currency
name="currencyCode"
label="Currency"
@@ -190,7 +206,6 @@ const QuoteForm = ({ initialValues }: QuoteFormProps) => {
}
}}
/>
-
{isEditing &&
!!customer.currencyCode &&
customer.currencyCode !== company.baseCurrencyCode && (
@@ -214,7 +229,6 @@ const QuoteForm = ({ initialValues }: QuoteFormProps) => {
}}
/>
)}
-
<CustomFormFields table="quote" />
</div>
</VStack>
diff --git a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderForm.tsx b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderForm.tsx
index 7dd8cf555e..bfe459f011 100644
--- a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderForm.tsx
+++ b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderForm.tsx
@@ -51,9 +51,13 @@ const SalesOrderForm = ({ initialValues }: SalesOrderFormProps) => {
const [customer, setCustomer] = useState<{
id: string | undefined;
currencyCode: string | undefined;
+ customerContactId: string | undefined;
+ customerLocationId: string | undefined;
}>({
id: initialValues.customerId,
- currencyCode: initialValues.currencyCode
+ currencyCode: initialValues.currencyCode,
+ customerContactId: initialValues.customerContactId,
+ customerLocationId: initialValues.customerLocationId
});
const isEditing = initialValues.id !== undefined;
const isCustomer = permissions.is("customer");
@@ -63,7 +67,6 @@ const SalesOrderForm = ({ initialValues }: SalesOrderFormProps) => {
const onCustomerChange = async (
newValue: {
value: string | undefined;
- label: string;
} | null
) => {
if (!carbon) {
@@ -76,13 +79,17 @@ const SalesOrderForm = ({ initialValues }: SalesOrderFormProps) => {
// update the customer immediately
setCustomer({
id: newValue?.value,
- currencyCode: undefined
+ currencyCode: undefined,
+ customerContactId: undefined,
+ customerLocationId: undefined
});
});
const { data, error } = await carbon
?.from("customer")
- .select("currencyCode")
+ .select(
+ "currencyCode, salesContactId, customerShipping!customerId(shippingCustomerLocationId)"
+ )
.eq("id", newValue.value)
.single();
if (error) {
@@ -90,13 +97,18 @@ const SalesOrderForm = ({ initialValues }: SalesOrderFormProps) => {
} else {
setCustomer((prev) => ({
...prev,
- currencyCode: data.currencyCode ?? undefined
+ currencyCode: data.currencyCode ?? undefined,
+ customerContactId: data.salesContactId ?? undefined,
+ customerLocationId:
+ data.customerShipping?.shippingCustomerLocationId ?? undefined
}));
}
} else {
setCustomer({
id: undefined,
- currencyCode: undefined
+ currencyCode: undefined,
+ customerContactId: undefined,
+ customerLocationId: undefined
});
}
};
@@ -148,6 +160,7 @@ const SalesOrderForm = ({ initialValues }: SalesOrderFormProps) => {
name="customerContactId"
label="Purchasing Contact"
customer={customer.id}
+ value={customer.customerContactId}
/>
<CustomerContact
name="customerEngineeringContactId"
@@ -158,6 +171,7 @@ const SalesOrderForm = ({ initialValues }: SalesOrderFormProps) => {
name="customerLocationId"
label="Customer Location"
customer={customer.id}
+ value={customer.customerLocationId}
/>
{initialValues.originatedFromQuote &&
diff --git a/apps/erp/app/modules/sales/ui/SalesRFQ/SalesRFQForm.tsx b/apps/erp/app/modules/sales/ui/SalesRFQ/SalesRFQForm.tsx
index be7cf4e9d1..3bbd0644d0 100644
--- a/apps/erp/app/modules/sales/ui/SalesRFQ/SalesRFQForm.tsx
+++ b/apps/erp/app/modules/sales/ui/SalesRFQ/SalesRFQForm.tsx
@@ -1,3 +1,4 @@
+import { useCarbon } from "@carbon/auth";
import { ValidatedForm } from "@carbon/form";
import {
Card,
@@ -7,9 +8,11 @@ import {
CardHeader,
CardTitle,
cn,
+ toast,
VStack
} from "@carbon/react";
import { useState } from "react";
+import { flushSync } from "react-dom";
import type { z } from "zod";
import {
Customer,
@@ -35,15 +38,67 @@ type SalesRFQFormProps = {
const SalesRFQForm = ({ initialValues }: SalesRFQFormProps) => {
const permissions = usePermissions();
- const [customer, setCustomer] = useState<string | undefined>(
- initialValues.customerId
- );
+ const { carbon } = useCarbon();
+ const [customer, setCustomer] = useState<{
+ id: string | undefined;
+ customerContactId: string | undefined;
+ customerLocationId: string | undefined;
+ }>({
+ id: initialValues.customerId,
+ customerContactId: initialValues.customerContactId,
+ customerLocationId: initialValues.customerLocationId
+ });
const isEditing = initialValues.id !== undefined;
const isCustomer = permissions.is("customer");
const isDraft = ["Draft", "Ready to Quote"].includes(
initialValues.status ?? ""
);
+ const onCustomerChange = async (
+ newValue: {
+ value: string | undefined;
+ } | null
+ ) => {
+ if (!carbon) {
+ toast.error("Carbon client not found");
+ return;
+ }
+
+ if (newValue?.value) {
+ flushSync(() => {
+ setCustomer({
+ id: newValue?.value,
+ customerContactId: undefined,
+ customerLocationId: undefined
+ });
+ });
+
+ const { data, error } = await carbon
+ ?.from("customer")
+ .select(
+ "salesContactId, customerShipping!customerId(shippingCustomerLocationId)"
+ )
+ .eq("id", newValue.value)
+ .single();
+ if (error) {
+ toast.error("Error fetching customer data");
+ } else {
+ setCustomer((prev) => ({
+ ...prev,
+ customerContactId: data.salesContactId ?? undefined,
+ customerLocationId:
+ data.customerShipping?.shippingCustomerLocationId ?? undefined
+ }));
+ }
+ } else {
+ setCustomer({
+ id: undefined,
+ customerContactId: undefined,
+ customerLocationId: undefined
+ });
+ }
+ };
+
return (
<Card>
<ValidatedForm
@@ -82,25 +137,25 @@ const SalesRFQForm = ({ initialValues }: SalesRFQFormProps) => {
autoFocus={!isEditing}
name="customerId"
label="Customer"
- onChange={(newValue) =>
- setCustomer(newValue?.value as string | undefined)
- }
+ onChange={onCustomerChange}
/>
<Input name="customerReference" label="Customer RFQ" />
<CustomerContact
name="customerContactId"
label="Purchasing Contact"
- customer={customer}
+ customer={customer.id}
+ value={customer.customerContactId}
/>
<CustomerContact
name="customerEngineeringContactId"
label="Engineering Contact"
- customer={customer}
+ customer={customer.id}
/>
<CustomerLocation
name="customerLocationId"
label="Customer Location"
- customer={customer}
+ customer={customer.id}
+ value={customer.customerLocationId}
/>
<DatePicker
name="rfqDate"