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.

Merge remote-tracking branch 'origin/main' into naveenkash/invoice-customer-field-fix

naveenkash7b73e3bd834f
diff --cc apps/erp/app/routes/x+/purchase-invoice+/update.tsx
index 9cf867b11b,f08fcdb399..d4c954d39c
--- a/apps/erp/app/routes/x+/purchase-invoice+/update.tsx
+++ b/apps/erp/app/routes/x+/purchase-invoice+/update.tsx
@@@ -47,37 -46,36 +47,39 @@@ export async function action({ request
}
switch (field) {
- case "invoiceSupplierId":
- let currencyCode: string | undefined;
- if (value && ids.length === 1) {
- const supplier = await client
- ?.from("supplier")
- .select("currencyCode")
- .eq("id", value)
- .single();
+ case "invoiceSupplierId": {
+ if (!value) {
+ return {
+ error: { message: "Invoice supplier is required" },
+ data: null
+ };
+ }
+
+ const supplier = await client
+ .from("supplier")
+ .select("currencyCode")
+ .eq("id", value)
+ .eq("companyId", companyId)
+ .single();
+ if (supplier.error) return supplier;
- if (supplier.data?.currencyCode) {
- currencyCode = supplier.data.currencyCode;
- const rate = await getExchangeRate(client, companyId, currencyCode);
- if (rate.error) return rate;
- return await client
- .from("purchaseInvoice")
- .update({
- invoiceSupplierId: value ?? undefined,
- invoiceSupplierContactId: null,
- invoiceSupplierLocationId: null,
- currencyCode: currencyCode ?? undefined,
- exchangeRate: rate.data,
- updatedBy: userId,
- updatedAt: new Date().toISOString()
- })
- .in("id", ids as string[]);
- }
+ let currency: { currencyCode: string; exchangeRate: number } | null =
+ null;
+ if (supplier.data.currencyCode) {
+ const rate = await getExchangeRate(
+ client,
+ companyId,
+ supplier.data.currencyCode
+ );
+ if (rate.error) return rate;
+ currency = {
+ currencyCode: supplier.data.currencyCode,
+ exchangeRate: rate.data
+ };
}
+ // A supplier with no currency keeps the invoice's own. The contact and
+ // location belonged to the previous invoice supplier.
return await client
.from("purchaseInvoice")
.update({
diff --cc apps/erp/app/routes/x+/sales-invoice+/update.tsx
index dda805024d,fda0753a0a..62ee8b8bbf
--- a/apps/erp/app/routes/x+/sales-invoice+/update.tsx
+++ b/apps/erp/app/routes/x+/sales-invoice+/update.tsx
@@@ -46,37 -45,36 +46,39 @@@ export async function action({ request
}
switch (field) {
- case "invoiceCustomerId":
- let currencyCode: string | undefined;
- if (value && ids.length === 1) {
- const customer = await client
- ?.from("customer")
- .select("currencyCode")
- .eq("id", value)
- .single();
+ case "invoiceCustomerId": {
+ if (!value) {
+ return {
+ error: { message: "Invoice customer is required" },
+ data: null
+ };
+ }
+
+ const customer = await client
+ .from("customer")
+ .select("currencyCode")
+ .eq("id", value)
+ .eq("companyId", companyId)
+ .single();
+ if (customer.error) return customer;
- if (customer.data?.currencyCode) {
- currencyCode = customer.data.currencyCode;
- const rate = await getExchangeRate(client, companyId, currencyCode);
- if (rate.error) return rate;
- return await client
- .from("salesInvoice")
- .update({
- invoiceCustomerId: value ?? undefined,
- invoiceCustomerContactId: null,
- invoiceCustomerLocationId: null,
- currencyCode: currencyCode ?? undefined,
- exchangeRate: rate.data,
- updatedBy: userId,
- updatedAt: new Date().toISOString()
- })
- .in("id", ids as string[]);
- }
+ let currency: { currencyCode: string; exchangeRate: number } | null =
+ null;
+ if (customer.data.currencyCode) {
+ const rate = await getExchangeRate(
+ client,
+ companyId,
+ customer.data.currencyCode
+ );
+ if (rate.error) return rate;
+ currency = {
+ currencyCode: customer.data.currencyCode,
+ exchangeRate: rate.data
+ };
}
+ // A customer with no currency keeps the invoice's own. The contact and
+ // location belonged to the previous invoice customer.
return await client
.from("salesInvoice")
.update({