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: disable receipt and shipment until change

Brad Barbinb4f4995f93d6
diff --git a/apps/erp/app/components/Form/index.ts b/apps/erp/app/components/Form/index.ts
index e8117fc422..7233bc595a 100644
--- a/apps/erp/app/components/Form/index.ts
+++ b/apps/erp/app/components/Form/index.ts
@@ -7,6 +7,7 @@ import {
CreatableMultiSelect,
DatePicker,
DateTimePicker,
+ DefaultDisabledSubmit,
Hidden,
Input,
InputControlled,
@@ -96,6 +97,7 @@ export {
CustomFormFields,
DatePicker,
DateTimePicker,
+ DefaultDisabledSubmit,
DefaultMethodType,
Department,
Employee,
diff --git a/apps/erp/app/modules/inventory/ui/Receipts/ReceiptForm/ReceiptForm.tsx b/apps/erp/app/modules/inventory/ui/Receipts/ReceiptForm/ReceiptForm.tsx
index f939d063ac..f57bac76ef 100644
--- a/apps/erp/app/modules/inventory/ui/Receipts/ReceiptForm/ReceiptForm.tsx
+++ b/apps/erp/app/modules/inventory/ui/Receipts/ReceiptForm/ReceiptForm.tsx
@@ -1,4 +1,4 @@
-import { ValidatedForm } from "@carbon/form";
+import { DefaultDisabledSubmit, ValidatedForm } from "@carbon/form";
import {
Card,
CardAction,
@@ -26,7 +26,6 @@ import {
Input,
Location,
Select,
- Submit,
} from "~/components/Form";
import { ConfirmDelete } from "~/components/Modals";
import { usePermissions } from "~/hooks";
@@ -161,7 +160,8 @@ const ReceiptForm = ({ initialValues, status }: ReceiptFormProps) => {
</VStack>
</CardContent>
<CardFooter>
- <Submit
+ <DefaultDisabledSubmit
+ formId={formId}
isDisabled={
isEditing
? !permissions.can("update", "inventory")
@@ -169,7 +169,7 @@ const ReceiptForm = ({ initialValues, status }: ReceiptFormProps) => {
}
>
Save
- </Submit>
+ </DefaultDisabledSubmit>
</CardFooter>
</ValidatedForm>
</Card>
diff --git a/apps/erp/app/modules/inventory/ui/Shipments/ShipmentForm/ShipmentForm.tsx b/apps/erp/app/modules/inventory/ui/Shipments/ShipmentForm/ShipmentForm.tsx
index 989a561c16..c48651b169 100644
--- a/apps/erp/app/modules/inventory/ui/Shipments/ShipmentForm/ShipmentForm.tsx
+++ b/apps/erp/app/modules/inventory/ui/Shipments/ShipmentForm/ShipmentForm.tsx
@@ -22,12 +22,12 @@ import type { z } from "zod";
import {
Combobox,
CustomFormFields,
+ DefaultDisabledSubmit,
Hidden,
Input,
Location,
Select,
ShippingMethod,
- Submit,
} from "~/components/Form";
import { ConfirmDelete } from "~/components/Modals";
import { usePermissions } from "~/hooks";
@@ -162,7 +162,8 @@ const ShipmentForm = ({ initialValues, status }: ShipmentFormProps) => {
</VStack>
</CardContent>
<CardFooter>
- <Submit
+ <DefaultDisabledSubmit
+ formId={formId}
isDisabled={
isEditing
? !permissions.can("update", "inventory")
@@ -170,7 +171,7 @@ const ShipmentForm = ({ initialValues, status }: ShipmentFormProps) => {
}
>
Save
- </Submit>
+ </DefaultDisabledSubmit>
</CardFooter>
</ValidatedForm>
</Card>
diff --git a/apps/erp/app/modules/production/production.service.ts b/apps/erp/app/modules/production/production.service.ts
index 409ad0718d..811922fd0e 100644
--- a/apps/erp/app/modules/production/production.service.ts
+++ b/apps/erp/app/modules/production/production.service.ts
@@ -292,45 +292,17 @@ export async function getJobMaterialsWithQuantityOnHand(
locationId: string,
args?: { search: string | null } & GenericQueryFilters
) {
- let query = client
- .from("jobMaterial")
- .select("id, ...item(readableIdWithRevision)", {
+ return client.rpc(
+ "get_job_quantity_on_hand",
+ {
+ job_id: jobId,
+ company_id: companyId,
+ location_id: locationId,
+ },
+ {
count: "exact",
- })
- .eq("jobId", jobId);
-
- if (args?.search) {
- query = query.or(
- `item.readableIdWithRevision.ilike.%${args.search}%,description.ilike.%${args.search}%`
- );
- }
-
- if (args) {
- query = setGenericQueryFilters(query, args, [
- { column: "createdAt", ascending: true },
- ]);
- }
-
- const { data: jobMaterials, count, error } = await query;
- const jobMaterialIds = new Set(jobMaterials?.map((material) => material.id));
-
- if (error) return { error };
-
- const jobQuantities = await client.rpc("get_job_quantity_on_hand", {
- job_id: jobId,
- company_id: companyId,
- location_id: locationId,
- });
-
- return {
- data: jobQuantities.error
- ? null
- : jobQuantities.data?.filter((material) =>
- jobMaterialIds.has(material.id)
- ) ?? [],
- count: count,
- error: jobQuantities.error,
- };
+ }
+ );
}
export async function getJobMethodTree(
diff --git a/packages/form/src/components/Submit.tsx b/packages/form/src/components/Submit.tsx
index 9dec0c9f2c..7c07d307b5 100644
--- a/packages/form/src/components/Submit.tsx
+++ b/packages/form/src/components/Submit.tsx
@@ -12,12 +12,31 @@ import {
import { useBlocker, useNavigation } from "@remix-run/react";
import { forwardRef } from "react";
import { useIsSubmitting } from "../hooks";
+import { useFormContext } from "../userFacingFormContext";
type SubmitProps = ButtonProps & {
formId?: string;
withBlocker?: boolean;
};
+export function DefaultDisabledSubmit({
+ children,
+ formId,
+ isDisabled,
+}: {
+ children: React.ReactNode;
+ formId: string;
+ isDisabled: boolean;
+}) {
+ const { touchedFields } = useFormContext(formId);
+ const isTouched = Object.keys(touchedFields).length > 0;
+ return (
+ <Submit formId={formId} isDisabled={!isTouched || isDisabled}>
+ {children}
+ </Submit>
+ );
+}
+
export const Submit = forwardRef<HTMLButtonElement, SubmitProps>(
({ formId, children, isDisabled, withBlocker = true, ...props }, ref) => {
const isSubmitting = useIsSubmitting(formId);
diff --git a/packages/form/src/components/index.ts b/packages/form/src/components/index.ts
index 29d205e39d..ca0a836dc6 100644
--- a/packages/form/src/components/index.ts
+++ b/packages/form/src/components/index.ts
@@ -23,7 +23,7 @@ import Radios from "./Radios";
import type { SelectProps } from "./Select";
import Select from "./Select";
import SelectControlled from "./SelectControlled";
-import Submit from "./Submit";
+import Submit, { DefaultDisabledSubmit } from "./Submit";
import TextArea from "./Textarea";
import TextAreaControlled from "./TextareaControlled";
import TimePicker from "./TimePicker";
@@ -38,6 +38,7 @@ export {
CreatableMultiSelect,
DatePicker,
DateTimePicker,
+ DefaultDisabledSubmit,
Hidden,
Input,
InputControlled,