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: selecting customer / supplier will populate currency on Sales / Purchase Order

Rob Carrington7678cd2ba9ec
diff --git a/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderForm.tsx b/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderForm.tsx
index 213dcf7fc0..8d516eeecb 100644
--- a/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderForm.tsx
+++ b/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderForm.tsx
@@ -1,3 +1,4 @@
+import { useCarbon } from "@carbon/auth";
import { ValidatedForm } from "@carbon/form";
import {
Card,
@@ -8,8 +9,10 @@ import {
CardTitle,
VStack,
cn,
+ toast,
} from "@carbon/react";
import { useState } from "react";
+import { flushSync } from "react-dom";
import type { z } from "zod";
import {
Currency,
@@ -39,9 +42,14 @@ type PurchaseOrderFormProps = {
const PurchaseOrderForm = ({ initialValues }: PurchaseOrderFormProps) => {
const permissions = usePermissions();
- const [supplier, setSupplier] = useState<string | undefined>(
- initialValues.supplierId
- );
+ const { carbon } = useCarbon();
+ const [supplier, setSupplier] = useState<{
+ id: string | undefined;
+ currencyCode: string | undefined;
+ }>({
+ id: initialValues.supplierId,
+ currencyCode: initialValues.presentationCurrencyCode,
+ });
const isEditing = initialValues.id !== undefined;
const isSupplier = permissions.is("supplier");
@@ -55,6 +63,47 @@ const PurchaseOrderForm = ({ initialValues }: PurchaseOrderFormProps) => {
value: type,
}));
+ const onSupplierChange = async (
+ newValue: {
+ value: string | undefined;
+ label: string;
+ } | null
+ ) => {
+ if (!carbon) {
+ toast.error("Carbon client not found");
+ return;
+ }
+
+ if (newValue?.value) {
+ flushSync(() => {
+ // update the supplier immediately
+ setSupplier({
+ id: newValue?.value,
+ currencyCode: undefined,
+ });
+ });
+
+ const { data, error } = await carbon
+ ?.from("supplier")
+ .select("currencyCode")
+ .eq("id", newValue.value)
+ .single();
+ if (error) {
+ toast.error("Error fetching supplier data");
+ } else {
+ setSupplier((prev) => ({
+ ...prev,
+ currencyCode: data.currencyCode ?? undefined,
+ }));
+ }
+ } else {
+ setSupplier({
+ id: undefined,
+ currencyCode: undefined,
+ });
+ }
+ };
+
return (
<ValidatedForm
method="post"
@@ -88,9 +137,7 @@ const PurchaseOrderForm = ({ initialValues }: PurchaseOrderFormProps) => {
autoFocus={!isEditing}
name="supplierId"
label="Supplier"
- onChange={(newValue) =>
- setSupplier(newValue?.value as string | undefined)
- }
+ onChange={onSupplierChange}
/>
<Input name="supplierReference" label="Supplier Order Number" />
{isEditing && permissions.can("delete", "purchasing") && (
@@ -105,12 +152,12 @@ const PurchaseOrderForm = ({ initialValues }: PurchaseOrderFormProps) => {
<SupplierLocation
name="supplierLocationId"
label="Supplier Location"
- supplier={supplier}
+ supplier={supplier.id}
/>
<SupplierContact
name="supplierContactId"
label="Supplier Contact"
- supplier={supplier}
+ supplier={supplier.id}
/>
<DatePicker
@@ -128,6 +175,15 @@ const PurchaseOrderForm = ({ initialValues }: PurchaseOrderFormProps) => {
<Currency
name="presentationCurrencyCode"
label="Presentation Currency"
+ value={supplier.currencyCode}
+ onChange={(newValue) => {
+ if (newValue?.value) {
+ setSupplier((prevSupplier) => ({
+ ...prevSupplier,
+ currencyCode: newValue.value,
+ }));
+ }
+ }}
/>
{isEditing && (
diff --git a/apps/carbon/app/modules/sales/ui/SalesOrder/SalesOrderForm.tsx b/apps/carbon/app/modules/sales/ui/SalesOrder/SalesOrderForm.tsx
index 4530f5a760..4e1ef90f2c 100644
--- a/apps/carbon/app/modules/sales/ui/SalesOrder/SalesOrderForm.tsx
+++ b/apps/carbon/app/modules/sales/ui/SalesOrder/SalesOrderForm.tsx
@@ -1,3 +1,4 @@
+import { useCarbon } from "@carbon/auth";
import { ValidatedForm } from "@carbon/form";
import {
Card,
@@ -8,8 +9,10 @@ import {
CardTitle,
VStack,
cn,
+ toast,
} from "@carbon/react";
import { useState } from "react";
+import { flushSync } from "react-dom";
import type { z } from "zod";
import {
Currency,
@@ -34,12 +37,58 @@ type SalesOrderFormProps = {
const SalesOrderForm = ({ initialValues }: SalesOrderFormProps) => {
const permissions = usePermissions();
- const [customer, setCustomer] = useState<string | undefined>(
- initialValues.customerId
- );
+ const { carbon } = useCarbon();
+ const [customer, setCustomer] = useState<{
+ id: string | undefined;
+ currencyCode: string | undefined;
+ }>({
+ id: initialValues.customerId,
+ currencyCode: initialValues.presentationCurrencyCode,
+ });
const isEditing = initialValues.id !== undefined;
const isCustomer = permissions.is("customer");
+ const onCustomerChange = async (
+ newValue: {
+ value: string | undefined;
+ label: string;
+ } | null
+ ) => {
+ if (!carbon) {
+ toast.error("Carbon client not found");
+ return;
+ }
+
+ if (newValue?.value) {
+ flushSync(() => {
+ // update the customer immediately
+ setCustomer({
+ id: newValue?.value,
+ currencyCode: undefined,
+ });
+ });
+
+ const { data, error } = await carbon
+ ?.from("customer")
+ .select("currencyCode")
+ .eq("id", newValue.value)
+ .single();
+ if (error) {
+ toast.error("Error fetching customer data");
+ } else {
+ setCustomer((prev) => ({
+ ...prev,
+ currencyCode: data.currencyCode ?? undefined,
+ }));
+ }
+ } else {
+ setCustomer({
+ id: undefined,
+ currencyCode: undefined,
+ });
+ }
+ };
+
return (
<Card>
<ValidatedForm
@@ -72,21 +121,19 @@ const SalesOrderForm = ({ initialValues }: SalesOrderFormProps) => {
autoFocus={!isEditing}
name="customerId"
label="Customer"
- onChange={(newValue) =>
- setCustomer(newValue?.value as string | undefined)
- }
+ onChange={onCustomerChange}
/>
<Input name="customerReference" label="Customer PO Number" />
<CustomerLocation
name="customerLocationId"
label="Customer Location"
- customer={customer}
+ customer={customer.id}
/>
<CustomerContact
name="customerContactId"
label="Customer Contact"
- customer={customer}
+ customer={customer.id}
/>
<DatePicker
@@ -100,6 +147,15 @@ const SalesOrderForm = ({ initialValues }: SalesOrderFormProps) => {
<Currency
name="presentationCurrencyCode"
label="Presentation Currency"
+ value={customer.currencyCode}
+ onChange={(newValue) => {
+ if (newValue?.value) {
+ setCustomer((prevCustomer) => ({
+ ...prevCustomer,
+ currencyCode: newValue.value,
+ }));
+ }
+ }}
/>
<CustomFormFields table="salesOrder" />