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: add modal and routes for saving methods

Brad Barbin1eea25dc75f3
diff --git a/apps/carbon/app/components/CreateableCombobox.tsx b/apps/carbon/app/components/CreateableCombobox.tsx
index 01cfe77b40..f513ea220a 100644
--- a/apps/carbon/app/components/CreateableCombobox.tsx
+++ b/apps/carbon/app/components/CreateableCombobox.tsx
@@ -96,7 +96,8 @@ const CreatableCombobox = forwardRef<HTMLButtonElement, CreatableComboboxProps>(
<CommandItem
value={
typeof option.label === "string"
- ? option.label + option.helper
+ ? option.label.replace(/"/g, '\\"') +
+ option.helper?.replace(/"/g, '\\"')
: undefined
}
key={option.value}
@@ -131,7 +132,7 @@ const CreatableCombobox = forwardRef<HTMLButtonElement, CreatableComboboxProps>(
onSelect={() => {
props.onCreateOption?.(search);
}}
- value={search.trim() || ""}
+ value={search?.replace(/"/g, '\\"').trim() || ""}
className="cursor-pointer"
>
<span>Create</span>
diff --git a/apps/carbon/app/components/Form/CreatableCombobox.tsx b/apps/carbon/app/components/Form/CreatableCombobox.tsx
index 81df45a6b2..d95b667ed5 100644
--- a/apps/carbon/app/components/Form/CreatableCombobox.tsx
+++ b/apps/carbon/app/components/Form/CreatableCombobox.tsx
@@ -52,10 +52,10 @@ const CreatableCombobox = forwardRef<HTMLButtonElement, CreatableComboboxProps>(
<CreatableComboboxBase
ref={ref}
{...props}
- value={value}
+ value={value?.replace(/"/g, '\\"')}
onChange={(newValue) => {
- setValue(newValue ?? "");
- onChange(newValue ?? "");
+ setValue(newValue?.replace(/"/g, '\\"') ?? "");
+ onChange(newValue?.replace(/"/g, '\\"') ?? "");
}}
label={label}
className="w-full"
diff --git a/apps/carbon/app/components/Form/Item.tsx b/apps/carbon/app/components/Form/Item.tsx
index 3915ccafa2..64cada3b1b 100644
--- a/apps/carbon/app/components/Form/Item.tsx
+++ b/apps/carbon/app/components/Form/Item.tsx
@@ -37,11 +37,13 @@ const Item = ({ type, ...props }: ItemSelectProps) => {
(item.active === true || props?.includeInactive === true)
)
: items
- ).map((item) => ({
- value: item.id,
- label: item.readableId,
- helper: item.name,
- })) ?? [];
+ )
+ .filter((item) => item.type === type)
+ .map((item) => ({
+ value: item.id,
+ label: item.readableId,
+ helper: item.name,
+ })) ?? [];
if (props.disabledItems) {
return results.filter(
diff --git a/apps/carbon/app/modules/items/items.service.ts b/apps/carbon/app/modules/items/items.service.ts
index 1ef8e33040..3d790a9cc4 100644
--- a/apps/carbon/app/modules/items/items.service.ts
+++ b/apps/carbon/app/modules/items/items.service.ts
@@ -117,6 +117,7 @@ export async function getConsumables(
})
.eq("companyId", companyId);
+ // TODO: this does not handle the case where active is true and false
const includeInactive = args?.filters?.some(
(filter) => filter.column === "active" && filter.value === "false"
);
diff --git a/apps/carbon/app/modules/items/ui/Materials/MaterialForm.tsx b/apps/carbon/app/modules/items/ui/Materials/MaterialForm.tsx
index 1e775a7850..8acad0273c 100644
--- a/apps/carbon/app/modules/items/ui/Materials/MaterialForm.tsx
+++ b/apps/carbon/app/modules/items/ui/Materials/MaterialForm.tsx
@@ -18,6 +18,7 @@ import { useEffect, useMemo, useState } from "react";
import type { z } from "zod";
import {
Boolean,
+ Combobox,
CustomFormFields,
DefaultMethodType,
Hidden,
@@ -131,7 +132,7 @@ const MaterialForm = ({
)}
<Input name="name" label="Name" />
- <Select
+ <Combobox
name="materialSubstanceId"
label="Substance"
options={substances}
@@ -139,7 +140,7 @@ const MaterialForm = ({
{isEditing && (
<TextArea name="description" label="Description" />
)}
- <Select name="materialFormId" label="Form" options={forms} />
+ <Combobox name="materialFormId" label="Form" options={forms} />
<Input name="finish" label="Finish" />
<Input name="grade" label="Grade" />
<Input name="dimensions" label="Dimensions" />
diff --git a/apps/carbon/app/modules/sales/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.ts
index 495c0ee393..4953d30f50 100644
--- a/apps/carbon/app/modules/sales/sales.service.ts
+++ b/apps/carbon/app/modules/sales/sales.service.ts
@@ -1276,6 +1276,63 @@ export async function updateQuoteOperationOrder(
return Promise.all(updatePromises);
}
+export async function upsertMakeMethodFromQuoteLine(
+ client: SupabaseClient<Database>,
+ lineMethod: {
+ itemId: string;
+ quoteId: string;
+ quoteLineId: string;
+ companyId: string;
+ userId: string;
+ }
+) {
+ return client.functions.invoke("get-method", {
+ body: {
+ type: "quoteLineToItem",
+ sourceId: `${lineMethod.quoteId}:${lineMethod.quoteLineId}`,
+ targetId: lineMethod.itemId,
+ companyId: lineMethod.companyId,
+ userId: lineMethod.userId,
+ },
+ });
+}
+
+export async function upsertMakeMethodFromQuoteMethod(
+ client: SupabaseClient<Database>,
+ quoteMaterial: z.infer<typeof getMethodValidator> & {
+ companyId: string;
+ createdBy: string;
+ }
+) {
+ const makeMethod = await client
+ .from("quoteMakeMethod")
+ .select("id")
+ .eq("parentMaterialId", quoteMaterial.quoteMaterialId)
+ .single();
+ if (makeMethod.error) {
+ return makeMethod;
+ }
+
+ const { error } = await client.functions.invoke("get-method", {
+ body: {
+ type: "quoteMakeMethodToItem",
+ sourceId: makeMethod.data.id,
+ targetId: quoteMaterial.itemId,
+ companyId: quoteMaterial.companyId,
+ userId: quoteMaterial.createdBy,
+ },
+ });
+
+ if (error) {
+ return {
+ data: null,
+ error: { message: "Failed to save method" } as PostgrestError,
+ };
+ }
+
+ return { data: null, error: null };
+}
+
export async function upsertQuote(
client: SupabaseClient<Database>,
quote:
@@ -1479,8 +1536,6 @@ export async function upsertQuoteMaterialMakeMethod(
return makeMethod;
}
- console.log({ makeMethod });
-
const { error } = await client.functions.invoke("get-method", {
body: {
type: "itemToQuoteMakeMethod",
diff --git a/apps/carbon/app/modules/sales/ui/Quotes/QuoteBreadcrumbs.tsx b/apps/carbon/app/modules/sales/ui/Quotes/QuoteBreadcrumbs.tsx
index 59adb2d1ae..9d7f705ae7 100644
--- a/apps/carbon/app/modules/sales/ui/Quotes/QuoteBreadcrumbs.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotes/QuoteBreadcrumbs.tsx
@@ -1,5 +1,6 @@
import {
Alert,
+ AlertTitle,
Button,
Checkbox,
HStack,
@@ -19,7 +20,7 @@ import {
import { ValidatedForm } from "@carbon/remix-validated-form";
import { Link, useFetcher, useLocation, useParams } from "@remix-run/react";
import { useEffect, useState } from "react";
-import { LuDownload, LuUpload } from "react-icons/lu";
+import { LuAlertTriangle, LuDownload, LuUpload } from "react-icons/lu";
import { RiProgress4Line } from "react-icons/ri";
import { BreadcrumbItem, Breadcrumbs } from "~/components";
import { Hidden, Item, Submit } from "~/components/Form";
@@ -50,6 +51,8 @@ const QuoteBreadcrumbs = () => {
const isGetMethodLoading =
fetcher.state !== "idle" && fetcher.formAction === path.to.quoteMethodGet;
+ const isSaveMethodLoading =
+ fetcher.state !== "idle" && fetcher.formAction === path.to.quoteMethodSave;
useEffect(() => {
if (fetcher.data?.error) {
@@ -59,9 +62,11 @@ const QuoteBreadcrumbs = () => {
const [includeInactive, setIncludeInactive] = useState<
boolean | "indeterminate"
- >(false);
+ >(true);
const getMethodModal = useDisclosure();
+ const saveMethodModal = useDisclosure();
+
const isQuoteLineMethod =
pathname === path.to.quoteLineMethod(quoteId, lineId!, methodId!);
const isQuoteMakeMethod =
@@ -97,16 +102,22 @@ const QuoteBreadcrumbs = () => {
(isQuoteLineMethod || isQuoteMakeMethod) && (
<HStack spacing={0}>
<MenubarItem
- leftIcon={<LuDownload />}
isLoading={isGetMethodLoading}
isDisabled={isGetMethodLoading}
+ leftIcon={<LuDownload />}
onClick={getMethodModal.onOpen}
>
Get Method
</MenubarItem>
<MenubarItem
- isDisabled={!permissions.can("update", "parts")}
+ isDisabled={
+ !hasMethods ||
+ !permissions.can("update", "parts") ||
+ isSaveMethodLoading
+ }
+ isLoading={isSaveMethodLoading}
leftIcon={<LuUpload />}
+ onClick={saveMethodModal.onOpen}
>
Save Method
</MenubarItem>
@@ -176,7 +187,10 @@ const QuoteBreadcrumbs = () => {
</div>
{hasMethods && (
<Alert variant="destructive">
- This will overwrite the existing quote method
+ <LuAlertTriangle className="h-4 w-4" />
+ <AlertTitle>
+ This will overwrite the existing quote method
+ </AlertTitle>
</Alert>
)}
</VStack>
@@ -193,6 +207,95 @@ const QuoteBreadcrumbs = () => {
</ModalContent>
</Modal>
)}
+
+ {saveMethodModal.isOpen && (
+ <Modal
+ open
+ onOpenChange={(open) => {
+ if (!open) {
+ saveMethodModal.onClose();
+ }
+ }}
+ >
+ <ModalContent>
+ <ValidatedForm
+ method="post"
+ fetcher={fetcher}
+ action={path.to.quoteMethodSave}
+ validator={
+ isQuoteLineMethod ? getLineMethodValidator : getMethodValidator
+ }
+ defaultValues={{
+ itemId: isQuoteLineMethod
+ ? line?.itemId ?? undefined
+ : undefined,
+ }}
+ onSubmit={saveMethodModal.onClose}
+ >
+ <ModalHeader>
+ <ModalTitle>Save Method</ModalTitle>
+ <ModalDescription>
+ Overwrite the target manufacturing method with the quote
+ method
+ </ModalDescription>
+ </ModalHeader>
+ <ModalBody>
+ {isQuoteLineMethod ? (
+ <>
+ <Hidden name="type" value="line" />
+ <Hidden name="quoteId" value={quoteId} />
+ <Hidden name="quoteLineId" value={lineId} />
+ </>
+ ) : (
+ <>
+ <Hidden name="type" value="method" />
+ <Hidden name="quoteMaterialId" value={materialId!} />
+ </>
+ )}
+
+ <VStack spacing={4}>
+ <Item
+ name="itemId"
+ label="Target Method"
+ type={(line?.itemType ?? "Part") as "Part"}
+ includeInactive={includeInactive === true}
+ replenishmentSystem="Make"
+ />
+ <div className="flex items-center space-x-2">
+ <Checkbox
+ id="include-inactive"
+ checked={includeInactive}
+ onCheckedChange={setIncludeInactive}
+ />
+ <label
+ htmlFor="include-inactive"
+ className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
+ >
+ Include Inactive
+ </label>
+ </div>
+ {hasMethods && (
+ <Alert variant="destructive">
+ <LuAlertTriangle className="h-4 w-4" />
+ <AlertTitle>
+ This will overwrite the existing manufacturing method
+ </AlertTitle>
+ </Alert>
+ )}
+ </VStack>
+ </ModalBody>
+ <ModalFooter>
+ <Button onClick={saveMethodModal.onClose} variant="secondary">
+ Cancel
+ </Button>
+ <Submit variant={hasMethods ? "destructive" : "primary"}>
+ Confirm
+ </Submit>
+ </ModalFooter>
+ </ValidatedForm>
+ </ModalContent>
+ </Modal>
+ )}
</>
);
};
diff --git a/apps/carbon/app/routes/x+/quote+/methods.$quoteId.$lineId.material.$id.tsx b/apps/carbon/app/routes/x+/quote+/methods+/$quoteId.$lineId.material.$id.tsx
similarity index 100%
rename from apps/carbon/app/routes/x+/quote+/methods.$quoteId.$lineId.material.$id.tsx
rename to apps/carbon/app/routes/x+/quote+/methods+/$quoteId.$lineId.material.$id.tsx
diff --git a/apps/carbon/app/routes/x+/quote+/methods.$quoteId.$lineId.material.delete.$id.tsx b/apps/carbon/app/routes/x+/quote+/methods+/$quoteId.$lineId.material.delete.$id.tsx
similarity index 100%
rename from apps/carbon/app/routes/x+/quote+/methods.$quoteId.$lineId.material.delete.$id.tsx
rename to apps/carbon/app/routes/x+/quote+/methods+/$quoteId.$lineId.material.delete.$id.tsx
diff --git a/apps/carbon/app/routes/x+/quote+/methods.$quoteId.$lineId.material.new.tsx b/apps/carbon/app/routes/x+/quote+/methods+/$quoteId.$lineId.material.new.tsx
similarity index 100%
rename from apps/carbon/app/routes/x+/quote+/methods.$quoteId.$lineId.material.new.tsx
rename to apps/carbon/app/routes/x+/quote+/methods+/$quoteId.$lineId.material.new.tsx
diff --git a/apps/carbon/app/routes/x+/quote+/methods.$quoteId.$lineId.operation.$id.tsx b/apps/carbon/app/routes/x+/quote+/methods+/$quoteId.$lineId.operation.$id.tsx
similarity index 100%
rename from apps/carbon/app/routes/x+/quote+/methods.$quoteId.$lineId.operation.$id.tsx
rename to apps/carbon/app/routes/x+/quote+/methods+/$quoteId.$lineId.operation.$id.tsx
diff --git a/apps/carbon/app/routes/x+/quote+/methods.$quoteId.$lineId.operation.new.tsx b/apps/carbon/app/routes/x+/quote+/methods+/$quoteId.$lineId.operation.new.tsx
similarity index 100%
rename from apps/carbon/app/routes/x+/quote+/methods.$quoteId.$lineId.operation.new.tsx
rename to apps/carbon/app/routes/x+/quote+/methods+/$quoteId.$lineId.operation.new.tsx
diff --git a/apps/carbon/app/routes/x+/quote+/methods.get.tsx b/apps/carbon/app/routes/x+/quote+/methods+/get.tsx
similarity index 100%
rename from apps/carbon/app/routes/x+/quote+/methods.get.tsx
rename to apps/carbon/app/routes/x+/quote+/methods+/get.tsx
diff --git a/apps/carbon/app/routes/x+/quote+/methods.material.order.tsx b/apps/carbon/app/routes/x+/quote+/methods+/material.order.tsx
similarity index 100%
rename from apps/carbon/app/routes/x+/quote+/methods.material.order.tsx
rename to apps/carbon/app/routes/x+/quote+/methods+/material.order.tsx
diff --git a/apps/carbon/app/routes/x+/quote+/methods.operation.order.tsx b/apps/carbon/app/routes/x+/quote+/methods+/operation.order.tsx
similarity index 100%
rename from apps/carbon/app/routes/x+/quote+/methods.operation.order.tsx
rename to apps/carbon/app/routes/x+/quote+/methods+/operation.order.tsx
diff --git a/apps/carbon/app/routes/x+/quote+/methods+/save.tsx b/apps/carbon/app/routes/x+/quote+/methods+/save.tsx
new file mode 100644
index 0000000000..9029fcec75
--- /dev/null
+++ b/apps/carbon/app/routes/x+/quote+/methods+/save.tsx
@@ -0,0 +1,67 @@
+import { validationError, validator } from "@carbon/remix-validated-form";
+import { json, redirect, type ActionFunctionArgs } from "@remix-run/node";
+import { getSupabaseServiceRole } from "~/lib/supabase";
+import {
+ getLineMethodValidator,
+ getMethodValidator,
+ upsertMakeMethodFromQuoteLine,
+ upsertMakeMethodFromQuoteMethod,
+} from "~/modules/sales";
+import { requirePermissions } from "~/services/auth/auth.server";
+import { path, requestReferrer } from "~/utils/path";
+
+export async function action({ request }: ActionFunctionArgs) {
+ const { companyId, userId } = await requirePermissions(request, {
+ update: "sales",
+ });
+
+ const formData = await request.formData();
+ const type = formData.get("type") as string;
+
+ const serviceRole = getSupabaseServiceRole();
+ if (type === "line") {
+ const validation = await validator(getLineMethodValidator).validate(
+ formData
+ );
+ if (validation.error) {
+ return validationError(validation.error);
+ }
+
+ const lineMethod = await upsertMakeMethodFromQuoteLine(serviceRole, {
+ ...validation.data,
+ companyId,
+ userId,
+ });
+
+ return json({
+ error: lineMethod.error
+ ? "Failed to save quote method to make method"
+ : null,
+ });
+ }
+
+ if (type === "method") {
+ const validation = await validator(getMethodValidator).validate(formData);
+ if (validation.error) {
+ return validationError(validation.error);
+ }
+
+ const makeMethod = await upsertMakeMethodFromQuoteMethod(serviceRole, {
+ ...validation.data,
+ companyId,
+ createdBy: userId,
+ });
+
+ if (makeMethod.error) {
+ return json({
+ error: makeMethod.error
+ ? "Failed to save quote method to make method"
+ : null,
+ });
+ }
+
+ throw redirect(requestReferrer(request) ?? path.to.quotes);
+ }
+
+ return json({ error: "Invalid type" }, { status: 400 });
+}
diff --git a/apps/carbon/app/utils/path.ts b/apps/carbon/app/utils/path.ts
index a343a0d4f8..a4326dabae 100644
--- a/apps/carbon/app/utils/path.ts
+++ b/apps/carbon/app/utils/path.ts
@@ -581,6 +581,7 @@ export const path = {
generatePath(`${x}/quote/methods/${quoteId}/${lineId}/material/${id}`),
quoteMaterialsOrder: `${x}/quote/methods/material/order`,
quoteMethodGet: `${x}/quote/methods/get`,
+ quoteMethodSave: `${x}/quote/methods/save`,
quoteOperation: (quoteId: string, lineId: string, id: string) =>
generatePath(`${x}/quote/methods/${quoteId}/${lineId}/operation/${id}`),
quoteOperationsOrder: `${x}/quote/methods/operation/order`,
diff --git a/packages/utils/src/themes.ts b/packages/utils/src/themes.ts
index da87120830..8e5999f8da 100644
--- a/packages/utils/src/themes.ts
+++ b/packages/utils/src/themes.ts
@@ -487,10 +487,10 @@ export const themes = [
"card-foreground": "210 20% 98%",
popover: "244 41.4 8.9%",
"popover-foreground": "210 20% 98%",
- active: "253.4 70% 40.4%",
- "active-foreground": "210 20% 98%",
- primary: "263.4 70% 50.4%",
- "primary-foreground": "253.4 70% 98%",
+ primary: "253.4 70% 40.4%",
+ "primary-foreground": "210 20% 98%",
+ active: "253.4 70% 81.4%",
+ "active-foreground": "253.4 70% 30.4%",
secondary: "215 27.9% 16.9%",
"secondary-foreground": "210 20% 98%",
muted: "215 27.9% 16.9%",