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 shipments to used in and don't post from background task

Brad Barbin664c8ae8e96e
diff --git a/apps/erp/app/modules/inventory/ui/Receipts/ReceiptPostModal.tsx b/apps/erp/app/modules/inventory/ui/Receipts/ReceiptPostModal.tsx
index 65b66bd771..a7b7ab0529 100644
--- a/apps/erp/app/modules/inventory/ui/Receipts/ReceiptPostModal.tsx
+++ b/apps/erp/app/modules/inventory/ui/Receipts/ReceiptPostModal.tsx
@@ -16,7 +16,7 @@ import {
toast,
useMount,
} from "@carbon/react";
-import { Form, useNavigation, useParams } from "@remix-run/react";
+import { useFetcher, useNavigation, useParams } from "@remix-run/react";
import { useState } from "react";
import { LuTriangleAlert } from "react-icons/lu";
import { path } from "~/utils/path";
@@ -109,6 +109,8 @@ const ReceiptPostModal = ({ onClose }: { onClose: () => void }) => {
getReceiptLines();
});
+ const fetcher = useFetcher<{}>();
+
return (
<Modal
open={true}
@@ -152,9 +154,11 @@ const ReceiptPostModal = ({ onClose }: { onClose: () => void }) => {
<Button variant="solid" onClick={onClose}>
Cancel
</Button>
- <Form action={path.to.receiptPost(receiptId)} method="post">
+ <fetcher.Form action={path.to.receiptPost(receiptId)} method="post">
<Button
+ isLoading={fetcher.state !== "idle"}
isDisabled={
+ fetcher.state !== "idle" ||
navigation.state !== "idle" ||
!validated ||
validationErrors.length > 0
@@ -163,7 +167,7 @@ const ReceiptPostModal = ({ onClose }: { onClose: () => void }) => {
>
Post Receipt
</Button>
- </Form>
+ </fetcher.Form>
</HStack>
</ModalFooter>
</ModalContent>
diff --git a/apps/erp/app/modules/inventory/ui/Shipments/ShipmentLines.tsx b/apps/erp/app/modules/inventory/ui/Shipments/ShipmentLines.tsx
index f2415fb51b..2cfcec3cf4 100644
--- a/apps/erp/app/modules/inventory/ui/Shipments/ShipmentLines.tsx
+++ b/apps/erp/app/modules/inventory/ui/Shipments/ShipmentLines.tsx
@@ -51,8 +51,9 @@ import { useUnitOfMeasure } from "~/components/Form/UnitOfMeasure";
import { TrackingTypeIcon } from "~/components/Icons";
import { useRouteData } from "~/hooks";
import {
+ splitValidator} from "~/modules/inventory";
+import type {
getBatchNumbersForItem,
- splitValidator,
type BatchProperty,
type getSerialNumbersForItem,
type Shipment,
@@ -320,7 +321,7 @@ function ShipmentLineItem({
</div>
</VStack>
<VStack spacing={1}>
- <label className="text-xs text-muted-foreground">Received</label>
+ <label className="text-xs text-muted-foreground">Shipped</label>
<NumberField
value={line.shippedQuantity}
diff --git a/apps/erp/app/modules/inventory/ui/Shipments/ShipmentPostModal.tsx b/apps/erp/app/modules/inventory/ui/Shipments/ShipmentPostModal.tsx
index e84a85b018..2d9d9b8a0b 100644
--- a/apps/erp/app/modules/inventory/ui/Shipments/ShipmentPostModal.tsx
+++ b/apps/erp/app/modules/inventory/ui/Shipments/ShipmentPostModal.tsx
@@ -16,7 +16,7 @@ import {
toast,
useMount,
} from "@carbon/react";
-import { Form, useNavigation, useParams } from "@remix-run/react";
+import { useFetcher, useNavigation, useParams } from "@remix-run/react";
import { useState } from "react";
import { LuTriangleAlert } from "react-icons/lu";
import { path } from "~/utils/path";
@@ -109,6 +109,8 @@ const ShipmentPostModal = ({ onClose }: { onClose: () => void }) => {
getShipmentLines();
});
+ const fetcher = useFetcher<{}>();
+
return (
<Modal
open={true}
@@ -152,9 +154,14 @@ const ShipmentPostModal = ({ onClose }: { onClose: () => void }) => {
<Button variant="solid" onClick={onClose}>
Cancel
</Button>
- <Form action={path.to.shipmentPost(shipmentId)} method="post">
+ <fetcher.Form
+ action={path.to.shipmentPost(shipmentId)}
+ method="post"
+ >
<Button
+ isLoading={fetcher.state !== "idle"}
isDisabled={
+ fetcher.state !== "idle" ||
navigation.state !== "idle" ||
!validated ||
validationErrors.length > 0
@@ -163,7 +170,7 @@ const ShipmentPostModal = ({ onClose }: { onClose: () => void }) => {
>
Post Shipment
</Button>
- </Form>
+ </fetcher.Form>
</HStack>
</ModalFooter>
</ModalContent>
diff --git a/apps/erp/app/modules/items/items.service.ts b/apps/erp/app/modules/items/items.service.ts
index c091837ad6..b122282724 100644
--- a/apps/erp/app/modules/items/items.service.ts
+++ b/apps/erp/app/modules/items/items.service.ts
@@ -490,6 +490,8 @@ export async function getMaterialUsedIn(
purchaseOrderLines,
receiptLines,
quoteMaterials,
+ salesOrderLines,
+ shipmentLines,
] = await Promise.all([
client
.from("jobMaterial")
@@ -522,6 +524,18 @@ export async function getMaterialUsedIn(
)
.eq("itemId", itemId)
.eq("companyId", companyId),
+ client
+ .from("salesOrderLine")
+ .select(
+ "id, methodType, ...salesOrder(documentReadableId:salesOrderId, documentId:id)"
+ )
+ .eq("itemId", itemId)
+ .eq("companyId", companyId),
+ client
+ .from("shipmentLine")
+ .select("id, ...shipment(documentReadableId:shipmentId, documentId:id)")
+ .eq("itemId", itemId)
+ .eq("companyId", companyId),
]);
return {
@@ -530,6 +544,8 @@ export async function getMaterialUsedIn(
purchaseOrderLines: purchaseOrderLines.data ?? [],
receiptLines: receiptLines.data ?? [],
quoteMaterials: quoteMaterials.data ?? [],
+ salesOrderLines: salesOrderLines.data ?? [],
+ shipmentLines: shipmentLines.data ?? [],
};
}
@@ -547,6 +563,7 @@ export async function getPartUsedIn(
quoteLines,
quoteMaterials,
salesOrderLines,
+ shipmentLines,
] = await Promise.all([
client
.from("jobMaterial")
@@ -598,6 +615,11 @@ export async function getPartUsedIn(
)
.eq("itemId", itemId)
.eq("companyId", companyId),
+ client
+ .from("shipmentLine")
+ .select("id, ...shipment(documentReadableId:shipmentId, documentId:id)")
+ .eq("itemId", itemId)
+ .eq("companyId", companyId),
]);
return {
@@ -609,6 +631,7 @@ export async function getPartUsedIn(
quoteLines: quoteLines.data ?? [],
quoteMaterials: quoteMaterials.data ?? [],
salesOrderLines: salesOrderLines.data ?? [],
+ shipmentLines: shipmentLines.data ?? [],
};
}
diff --git a/apps/erp/app/modules/items/ui/Item/UsedIn.tsx b/apps/erp/app/modules/items/ui/Item/UsedIn.tsx
index b988604155..45dfb8d0b2 100644
--- a/apps/erp/app/modules/items/ui/Item/UsedIn.tsx
+++ b/apps/erp/app/modules/items/ui/Item/UsedIn.tsx
@@ -9,11 +9,11 @@ import {
Count,
} from "@carbon/react";
import { useState } from "react";
-import { LuSearch, LuChevronRight } from "react-icons/lu";
+import { LuSearch, LuChevronRight, LuShip } from "react-icons/lu";
import { MethodIcon, Hyperlink } from "~/components";
import { LevelLine } from "~/components/TreeView";
import { usePermissions } from "~/hooks";
-import { MethodItemType } from "~/modules/shared";
+import type { MethodItemType } from "~/modules/shared";
import { path } from "~/utils/path";
import { getLinkToItemManufacturing } from "./ItemForm";
@@ -36,7 +36,8 @@ export type UsedInKey =
| "receiptLines"
| "quoteLines"
| "quoteMaterials"
- | "salesOrderLines";
+ | "salesOrderLines"
+ | "shipmentLines";
export type UsedInNode = {
key: UsedInKey;
@@ -147,10 +148,14 @@ export function UsedInItem({
className="flex h-8 cursor-pointer items-center overflow-hidden rounded-sm px-1 gap-4 text-sm hover:bg-muted/90 w-full font-medium"
>
<LevelLine isSelected={false} className="mr-2" />
- <MethodIcon
- type={child.methodType ?? "Method"}
- className="mr-2"
- />
+ {child.methodType === "Shipment" ? (
+ <LuShip className="mr-2" />
+ ) : (
+ <MethodIcon
+ type={child.methodType ?? "Method"}
+ className="mr-2"
+ />
+ )}
{child.documentReadableId}
</Hyperlink>
))
diff --git a/apps/erp/app/routes/x+/consumable+/$itemId.details.tsx b/apps/erp/app/routes/x+/consumable+/$itemId.details.tsx
index c49364d771..00d30ee6f3 100644
--- a/apps/erp/app/routes/x+/consumable+/$itemId.details.tsx
+++ b/apps/erp/app/routes/x+/consumable+/$itemId.details.tsx
@@ -23,12 +23,8 @@ import {
upsertConsumable,
} from "~/modules/items";
import { ItemDocuments, ItemNotes } from "~/modules/items/ui/Item";
-import type {
- UsedInNode} from "~/modules/items/ui/Item/UsedIn";
-import {
- UsedInSkeleton,
- UsedInTree,
-} from "~/modules/items/ui/Item/UsedIn";
+import type { UsedInNode } from "~/modules/items/ui/Item/UsedIn";
+import { UsedInSkeleton, UsedInTree } from "~/modules/items/ui/Item/UsedIn";
import { setCustomFields } from "~/utils/form";
import { path } from "~/utils/path";
@@ -116,6 +112,8 @@ export default function ConsumableDetailsRoute() {
purchaseOrderLines,
receiptLines,
quoteMaterials,
+ salesOrderLines,
+ shipmentLines,
} = resolvedUsedIn;
const tree: UsedInNode[] = [
@@ -159,6 +157,21 @@ export default function ConsumableDetailsRoute() {
documentReadableId: qm.documentReadableId ?? "",
})),
},
+ {
+ key: "salesOrderLines",
+ name: "Sales Orders",
+ module: "sales",
+ children: salesOrderLines,
+ },
+ {
+ key: "shipmentLines",
+ name: "Shipments",
+ module: "inventory",
+ children: shipmentLines.map((shipment) => ({
+ ...shipment,
+ methodType: "Shipment",
+ })),
+ },
];
return (
diff --git a/apps/erp/app/routes/x+/material+/$itemId.details.tsx b/apps/erp/app/routes/x+/material+/$itemId.details.tsx
index 1e2d37fa76..a4b04a8e5f 100644
--- a/apps/erp/app/routes/x+/material+/$itemId.details.tsx
+++ b/apps/erp/app/routes/x+/material+/$itemId.details.tsx
@@ -23,12 +23,8 @@ import {
upsertMaterial,
} from "~/modules/items";
import { ItemDocuments, ItemNotes } from "~/modules/items/ui/Item";
-import type {
- UsedInNode} from "~/modules/items/ui/Item/UsedIn";
-import {
- UsedInSkeleton,
- UsedInTree
-} from "~/modules/items/ui/Item/UsedIn";
+import type { UsedInNode } from "~/modules/items/ui/Item/UsedIn";
+import { UsedInSkeleton, UsedInTree } from "~/modules/items/ui/Item/UsedIn";
import { setCustomFields } from "~/utils/form";
import { path } from "~/utils/path";
@@ -116,6 +112,8 @@ export default function MaterialDetailsRoute() {
purchaseOrderLines,
receiptLines,
quoteMaterials,
+ salesOrderLines,
+ shipmentLines,
} = resolvedUsedIn;
const tree: UsedInNode[] = [
@@ -159,6 +157,21 @@ export default function MaterialDetailsRoute() {
documentReadableId: qm.documentReadableId ?? "",
})),
},
+ {
+ key: "salesOrderLines",
+ name: "Sales Orders",
+ module: "sales",
+ children: salesOrderLines,
+ },
+ {
+ key: "shipmentLines",
+ name: "Shipments",
+ module: "inventory",
+ children: shipmentLines.map((shipment) => ({
+ ...shipment,
+ methodType: "Shipment",
+ })),
+ },
];
return (
diff --git a/apps/erp/app/routes/x+/part+/$itemId.details.tsx b/apps/erp/app/routes/x+/part+/$itemId.details.tsx
index a4d0df37f7..bb285213a2 100644
--- a/apps/erp/app/routes/x+/part+/$itemId.details.tsx
+++ b/apps/erp/app/routes/x+/part+/$itemId.details.tsx
@@ -24,12 +24,8 @@ import { ItemDocuments, ItemNotes } from "~/modules/items/ui/Item";
import { setCustomFields } from "~/utils/form";
import { path } from "~/utils/path";
-import type {
- UsedInNode} from "~/modules/items/ui/Item/UsedIn";
-import {
- UsedInSkeleton,
- UsedInTree
-} from "~/modules/items/ui/Item/UsedIn";
+import type { UsedInNode } from "~/modules/items/ui/Item/UsedIn";
+import { UsedInSkeleton, UsedInTree } from "~/modules/items/ui/Item/UsedIn";
export async function loader({ request, params }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
@@ -117,6 +113,7 @@ export default function PartDetailsRoute() {
quoteLines,
quoteMaterials,
salesOrderLines,
+ shipmentLines,
} = resolvedUsedIn;
const tree: UsedInNode[] = [
@@ -181,6 +178,15 @@ export default function PartDetailsRoute() {
module: "sales",
children: salesOrderLines,
},
+ {
+ key: "shipmentLines",
+ name: "Shipments",
+ module: "inventory",
+ children: shipmentLines.map((shipment) => ({
+ ...shipment,
+ methodType: "Shipment",
+ })),
+ },
];
return (
diff --git a/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.post.tsx b/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.post.tsx
index 0e29cbeb9e..1cfed9021c 100644
--- a/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.post.tsx
+++ b/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.post.tsx
@@ -1,10 +1,8 @@
-import { error } from "@carbon/auth";
+import { error, getCarbonServiceRole } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
-import { tasks } from "@trigger.dev/sdk/v3";
import type { ActionFunctionArgs } from "@vercel/remix";
import { redirect } from "@vercel/remix";
-import type { postTransactionTask } from "~/trigger/post-transaction";
import { path } from "~/utils/path";
export const config = { runtime: "nodejs" };
@@ -34,12 +32,70 @@ export async function action({ request, params }: ActionFunctionArgs) {
);
}
- await tasks.trigger<typeof postTransactionTask>("post-transactions", {
- type: "purchase-invoice",
- documentId: invoiceId,
- userId,
- companyId,
- });
+ try {
+ const serviceRole = await getCarbonServiceRole();
+ const postPurchaseInvoice = await serviceRole.functions.invoke(
+ "post-purchase-invoice",
+ {
+ body: {
+ invoiceId: invoiceId,
+ userId: userId,
+ companyId: companyId,
+ },
+ }
+ );
+
+ if (postPurchaseInvoice.error) {
+ await client
+ .from("purchaseInvoice")
+ .update({
+ status: "Draft",
+ })
+ .eq("id", invoiceId);
+
+ throw redirect(
+ path.to.purchaseInvoices,
+ await flash(
+ request,
+ error(postPurchaseInvoice.error, "Failed to post purchase invoice")
+ )
+ );
+ }
+
+ const priceUpdate = await serviceRole.functions.invoke(
+ "update-purchased-prices",
+ {
+ body: {
+ invoiceId: invoiceId,
+ companyId: companyId,
+ },
+ }
+ );
+
+ if (priceUpdate.error) {
+ await client
+ .from("purchaseInvoice")
+ .update({
+ status: "Draft",
+ })
+ .eq("id", invoiceId);
+
+ throw redirect(
+ path.to.purchaseInvoices,
+ await flash(
+ request,
+ error(priceUpdate.error, "Failed to update prices")
+ )
+ );
+ }
+ } catch (error) {
+ await client
+ .from("purchaseInvoice")
+ .update({
+ status: "Draft",
+ })
+ .eq("id", invoiceId);
+ }
throw redirect(path.to.purchaseInvoices);
}
diff --git a/apps/erp/app/routes/x+/receipt+/$receiptId.post.tsx b/apps/erp/app/routes/x+/receipt+/$receiptId.post.tsx
index 0ce16e75d5..b16a27ad75 100644
--- a/apps/erp/app/routes/x+/receipt+/$receiptId.post.tsx
+++ b/apps/erp/app/routes/x+/receipt+/$receiptId.post.tsx
@@ -1,14 +1,10 @@
-import { error } from "@carbon/auth";
+import { error, getCarbonServiceRole } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
-import { tasks } from "@trigger.dev/sdk/v3";
import type { ActionFunctionArgs } from "@vercel/remix";
import { redirect } from "@vercel/remix";
-import type { postTransactionTask } from "~/trigger/post-transaction";
import { path } from "~/utils/path";
-export const config = { runtime: "nodejs" };
-
export async function action({ request, params }: ActionFunctionArgs) {
const { client, companyId, userId } = await requirePermissions(request, {
update: "inventory",
@@ -34,12 +30,37 @@ export async function action({ request, params }: ActionFunctionArgs) {
);
}
- await tasks.trigger<typeof postTransactionTask>("post-transactions", {
- type: "receipt",
- documentId: receiptId,
- userId,
- companyId,
- });
+ try {
+ const serviceRole = await getCarbonServiceRole();
+ const postReceipt = await serviceRole.functions.invoke("post-receipt", {
+ body: {
+ receiptId: receiptId,
+ userId: userId,
+ companyId: companyId,
+ },
+ });
+
+ if (postReceipt.error) {
+ await client
+ .from("receipt")
+ .update({
+ status: "Draft",
+ })
+ .eq("id", receiptId);
+
+ throw redirect(
+ path.to.receipts,
+ await flash(request, error(postReceipt.error, "Failed to post receipt"))
+ );
+ }
+ } catch (error) {
+ await client
+ .from("receipt")
+ .update({
+ status: "Draft",
+ })
+ .eq("id", receiptId);
+ }
throw redirect(path.to.receipts);
}
diff --git a/apps/erp/app/routes/x+/shipment+/$shipmentId.post.tsx b/apps/erp/app/routes/x+/shipment+/$shipmentId.post.tsx
index 7fe57a9bcb..25e10a883e 100644
--- a/apps/erp/app/routes/x+/shipment+/$shipmentId.post.tsx
+++ b/apps/erp/app/routes/x+/shipment+/$shipmentId.post.tsx
@@ -1,14 +1,10 @@
-import { error } from "@carbon/auth";
+import { error, getCarbonServiceRole } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
-import { tasks } from "@trigger.dev/sdk/v3";
import type { ActionFunctionArgs } from "@vercel/remix";
import { redirect } from "@vercel/remix";
-import type { postTransactionTask } from "~/trigger/post-transaction";
import { path } from "~/utils/path";
-export const config = { runtime: "nodejs" };
-
export async function action({ request, params }: ActionFunctionArgs) {
const { client, companyId, userId } = await requirePermissions(request, {
update: "inventory",
@@ -34,12 +30,40 @@ export async function action({ request, params }: ActionFunctionArgs) {
);
}
- await tasks.trigger<typeof postTransactionTask>("post-transactions", {
- type: "shipment",
- documentId: shipmentId,
- userId,
- companyId,
- });
+ try {
+ const serviceRole = await getCarbonServiceRole();
+ const postShipment = await serviceRole.functions.invoke("post-shipment", {
+ body: {
+ shipmentId: shipmentId,
+ userId: userId,
+ companyId: companyId,
+ },
+ });
+
+ if (postShipment.error) {
+ await client
+ .from("shipment")
+ .update({
+ status: "Draft",
+ })
+ .eq("id", shipmentId);
+
+ throw redirect(
+ path.to.shipments,
+ await flash(
+ request,
+ error(postShipment.error, "Failed to post shipment")
+ )
+ );
+ }
+ } catch (error) {
+ await client
+ .from("shipment")
+ .update({
+ status: "Draft",
+ })
+ .eq("id", shipmentId);
+ }
throw redirect(path.to.shipments);
}
diff --git a/apps/erp/app/routes/x+/tool+/$itemId.details.tsx b/apps/erp/app/routes/x+/tool+/$itemId.details.tsx
index ab991f034c..099baf53c9 100644
--- a/apps/erp/app/routes/x+/tool+/$itemId.details.tsx
+++ b/apps/erp/app/routes/x+/tool+/$itemId.details.tsx
@@ -21,12 +21,8 @@ import { getPartUsedIn, toolValidator, upsertTool } from "~/modules/items";
import { ItemDocuments, ItemNotes } from "~/modules/items/ui/Item";
import { setCustomFields } from "~/utils/form";
import { path } from "~/utils/path";
-import type {
- UsedInNode} from "~/modules/items/ui/Item/UsedIn";
-import {
- UsedInSkeleton,
- UsedInTree
-} from "~/modules/items/ui/Item/UsedIn";
+import type { UsedInNode } from "~/modules/items/ui/Item/UsedIn";
+import { UsedInSkeleton, UsedInTree } from "~/modules/items/ui/Item/UsedIn";
export async function loader({ request, params }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
@@ -109,6 +105,8 @@ export default function ToolDetailsRoute() {
purchaseOrderLines,
receiptLines,
quoteMaterials,
+ salesOrderLines,
+ shipmentLines,
} = resolvedUsedIn;
const tree: UsedInNode[] = [
@@ -161,6 +159,21 @@ export default function ToolDetailsRoute() {
documentReadableId: qm.documentReadableId ?? "",
})),
},
+ {
+ key: "salesOrderLines",
+ name: "Sales Orders",
+ module: "sales",
+ children: salesOrderLines,
+ },
+ {
+ key: "shipmentLines",
+ name: "Shipments",
+ module: "inventory",
+ children: shipmentLines.map((shipment) => ({
+ ...shipment,
+ methodType: "Shipment",
+ })),
+ },
];
return (