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: search by text-based custom fields

Brad Barbin61bee7c63dcf
diff --git a/apps/erp/app/hooks/useCustomColumns.tsx b/apps/erp/app/hooks/useCustomColumns.tsx
index 52c2775f55..be39c698f6 100644
--- a/apps/erp/app/hooks/useCustomColumns.tsx
+++ b/apps/erp/app/hooks/useCustomColumns.tsx
@@ -13,6 +13,7 @@ import { Avatar } from "~/components";
import { Enumerable } from "~/components/Enumerable";
import { DataType } from "~/modules/shared";
import { usePeople } from "~/stores";
+import { path } from "~/utils/path";
import { useCustomFieldsSchema } from "./useCustomFieldsSchema";
export function useCustomColumns<T extends { customFields: Json }>(
@@ -48,6 +49,19 @@ export function useCustomColumns<T extends { customFields: Json }>(
label: <Enumerable value={option} />,
})) || [],
}
+ : field.dataTypeId === DataType.User
+ ? {
+ type: "static",
+ options: people.map((person) => ({
+ value: person.id,
+ label: person.name,
+ })),
+ }
+ : field.dataTypeId === DataType.Text
+ ? {
+ type: "fetcher",
+ endpoint: path.to.api.customFieldOptions(table, field.id),
+ }
: undefined,
},
cell: (item) => {
diff --git a/apps/erp/app/routes/api+/settings.custom-fields.$table.$id.tsx b/apps/erp/app/routes/api+/settings.custom-fields.$table.$id.tsx
new file mode 100644
index 0000000000..025e03af22
--- /dev/null
+++ b/apps/erp/app/routes/api+/settings.custom-fields.$table.$id.tsx
@@ -0,0 +1,38 @@
+import { error } from "@carbon/auth";
+import { requirePermissions } from "@carbon/auth/auth.server";
+import { flash } from "@carbon/auth/session.server";
+import type { LoaderFunctionArgs } from "@vercel/remix";
+import { json } from "@vercel/remix";
+
+export async function loader({ request, params }: LoaderFunctionArgs) {
+ const { client, companyId } = await requirePermissions(request, {});
+
+ const { table, id } = params;
+
+ if (!table || !id)
+ return json({
+ data: [],
+ });
+
+ const values = await client.rpc("get_custom_field_unique_values", {
+ table_name: table,
+ field_key: id,
+ company_id: companyId,
+ });
+ if (values.error) {
+ return json(
+ [],
+ await flash(
+ request,
+ error(values.error, "Failed to get unique values for custom field")
+ )
+ );
+ }
+
+ const options = values.data.map((value) => ({
+ id: value.value,
+ name: value.value,
+ }));
+
+ return json({ data: options, error: null });
+}
diff --git a/apps/erp/app/utils/path.ts b/apps/erp/app/utils/path.ts
index 3bb61bf2b6..2e1f48cacf 100644
--- a/apps/erp/app/utils/path.ts
+++ b/apps/erp/app/utils/path.ts
@@ -25,6 +25,8 @@ export const path = {
generatePath(`${api}/sales/customer-locations/${id}`),
customerStatuses: `${api}/sales/customer-statuses`,
customerTypes: `${api}/sales/customer-types`,
+ customFieldOptions: (table: string, fieldId: string) =>
+ generatePath(`${api}/settings/custom-fields/${table}/${fieldId}`),
departments: `${api}/people/departments`,
digitalQuote: (id: string) =>
generatePath(`${api}/sales/digital-quote/${id}`),
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index fe6f8b6c75..eba37fc055 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -17602,6 +17602,7 @@ export type Database = {
internalNotes: Json | null
invoiced: boolean | null
locationId: string | null
+ postedBy: string | null
postingDate: string | null
receiptId: string
sourceDocument:
@@ -17627,6 +17628,7 @@ export type Database = {
internalNotes?: Json | null
invoiced?: boolean | null
locationId?: string | null
+ postedBy?: string | null
postingDate?: string | null
receiptId: string
sourceDocument?:
@@ -17652,6 +17654,7 @@ export type Database = {
internalNotes?: Json | null
invoiced?: boolean | null
locationId?: string | null
+ postedBy?: string | null
postingDate?: string | null
receiptId?: string
sourceDocument?:
@@ -17772,6 +17775,41 @@ export type Database = {
referencedRelation: "location"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "receipt_postedBy_fkey"
+ columns: ["postedBy"]
+ isOneToOne: false
+ referencedRelation: "employees"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "receipt_postedBy_fkey"
+ columns: ["postedBy"]
+ isOneToOne: false
+ referencedRelation: "employeesAcrossCompanies"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "receipt_postedBy_fkey"
+ columns: ["postedBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "receipt_postedBy_fkey"
+ columns: ["postedBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "receipt_postedBy_fkey"
+ columns: ["postedBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
{
foreignKeyName: "receipt_supplierId_fkey"
columns: ["supplierId"]
@@ -32499,6 +32537,16 @@ export type Database = {
}
Returns: string
}
+ get_custom_field_unique_values: {
+ Args: {
+ table_name: string
+ field_key: string
+ company_id: string
+ }
+ Returns: {
+ value: Json
+ }[]
+ }
get_item_quantities: {
Args: {
location_id: string
@@ -33022,7 +33070,7 @@ export type Database = {
| "Fixed Reorder Quantity"
| "Maximum Quantity"
itemReplenishmentSystem: "Buy" | "Make" | "Buy and Make"
- itemTrackingType: "Inventory" | "Non-Inventory"
+ itemTrackingType: "Inventory" | "Non-Inventory" | "Serial" | "Lot"
itemType:
| "Part"
| "Material"
@@ -33030,6 +33078,8 @@ export type Database = {
| "Service"
| "Consumable"
| "Fixture"
+ | "Serial"
+ | "Lot"
jobOperationStatus:
| "Canceled"
| "Done"
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index fe6f8b6c75..eba37fc055 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -17602,6 +17602,7 @@ export type Database = {
internalNotes: Json | null
invoiced: boolean | null
locationId: string | null
+ postedBy: string | null
postingDate: string | null
receiptId: string
sourceDocument:
@@ -17627,6 +17628,7 @@ export type Database = {
internalNotes?: Json | null
invoiced?: boolean | null
locationId?: string | null
+ postedBy?: string | null
postingDate?: string | null
receiptId: string
sourceDocument?:
@@ -17652,6 +17654,7 @@ export type Database = {
internalNotes?: Json | null
invoiced?: boolean | null
locationId?: string | null
+ postedBy?: string | null
postingDate?: string | null
receiptId?: string
sourceDocument?:
@@ -17772,6 +17775,41 @@ export type Database = {
referencedRelation: "location"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "receipt_postedBy_fkey"
+ columns: ["postedBy"]
+ isOneToOne: false
+ referencedRelation: "employees"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "receipt_postedBy_fkey"
+ columns: ["postedBy"]
+ isOneToOne: false
+ referencedRelation: "employeesAcrossCompanies"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "receipt_postedBy_fkey"
+ columns: ["postedBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "receipt_postedBy_fkey"
+ columns: ["postedBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "receipt_postedBy_fkey"
+ columns: ["postedBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
{
foreignKeyName: "receipt_supplierId_fkey"
columns: ["supplierId"]
@@ -32499,6 +32537,16 @@ export type Database = {
}
Returns: string
}
+ get_custom_field_unique_values: {
+ Args: {
+ table_name: string
+ field_key: string
+ company_id: string
+ }
+ Returns: {
+ value: Json
+ }[]
+ }
get_item_quantities: {
Args: {
location_id: string
@@ -33022,7 +33070,7 @@ export type Database = {
| "Fixed Reorder Quantity"
| "Maximum Quantity"
itemReplenishmentSystem: "Buy" | "Make" | "Buy and Make"
- itemTrackingType: "Inventory" | "Non-Inventory"
+ itemTrackingType: "Inventory" | "Non-Inventory" | "Serial" | "Lot"
itemType:
| "Part"
| "Material"
@@ -33030,6 +33078,8 @@ export type Database = {
| "Service"
| "Consumable"
| "Fixture"
+ | "Serial"
+ | "Lot"
jobOperationStatus:
| "Canceled"
| "Done"
diff --git a/packages/database/supabase/migrations/20250125115137_get-unique-values-for-custom-field.sql b/packages/database/supabase/migrations/20250125115137_get-unique-values-for-custom-field.sql
new file mode 100644
index 0000000000..2c5fcad410
--- /dev/null
+++ b/packages/database/supabase/migrations/20250125115137_get-unique-values-for-custom-field.sql
@@ -0,0 +1,18 @@
+CREATE OR REPLACE FUNCTION get_custom_field_unique_values(
+ table_name text,
+ field_key text,
+ company_id text
+)
+RETURNS TABLE (value jsonb) AS $$
+BEGIN
+ RETURN QUERY EXECUTE format(
+ 'SELECT DISTINCT jsonb_extract_path("customFields", $1) as value
+ FROM %I
+ WHERE "companyId" = $2
+ AND "customFields" ? $1
+ AND jsonb_extract_path("customFields", $1) IS NOT NULL',
+ table_name
+ ) USING field_key, company_id;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+