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 ui for ordering production parts from planning submodule

Brad Barbin7e1547d1ee58
diff --git a/apps/erp/app/hooks/useModules.tsx b/apps/erp/app/hooks/useModules.tsx
index 1cd363688e..413f399809 100644
--- a/apps/erp/app/hooks/useModules.tsx
+++ b/apps/erp/app/hooks/useModules.tsx
@@ -64,7 +64,7 @@ export function useModules() {
},
{
permission: "accounting",
- name: "Accounting",
+ name: "Finance",
to: path.to.currencies,
icon: LuLandmark,
},
diff --git a/apps/erp/app/modules/items/ui/Item/ItemReorderPolicy.tsx b/apps/erp/app/modules/items/ui/Item/ItemReorderPolicy.tsx
index 5c3fa5bddb..df220f17eb 100644
--- a/apps/erp/app/modules/items/ui/Item/ItemReorderPolicy.tsx
+++ b/apps/erp/app/modules/items/ui/Item/ItemReorderPolicy.tsx
@@ -57,15 +57,17 @@ export function getReorderPolicyDescription(
}
}
-export function getOrdersFromProductionPlanning(
- itemPlanning: ProductionPlanningItem,
- periods: { startDate: string; id: string }[]
-): {
+export type Order = {
startDate: string;
dueDate: string;
quantity: number;
isASAP: boolean;
-}[] {
+};
+
+export function getOrdersFromProductionPlanning(
+ itemPlanning: ProductionPlanningItem,
+ periods: { startDate: string; id: string }[]
+): Order[] {
if (itemPlanning.reorderingPolicy === "Manual Reorder") {
return [];
}
diff --git a/apps/erp/app/modules/production/ui/Planning/PlanningTable.tsx b/apps/erp/app/modules/production/ui/Planning/PlanningTable.tsx
index 28b8b60f83..adf226d8d5 100644
--- a/apps/erp/app/modules/production/ui/Planning/PlanningTable.tsx
+++ b/apps/erp/app/modules/production/ui/Planning/PlanningTable.tsx
@@ -1,24 +1,53 @@
import {
Button,
Combobox,
+ DatePicker,
+ DropdownMenuContent,
+ DropdownMenuIcon,
+ DropdownMenuItem,
+ DropdownMenuLabel,
+ DropdownMenuSeparator,
HStack,
+ Modal,
+ ModalBody,
+ ModalContent,
+ ModalDescription,
+ ModalFooter,
+ ModalHeader,
+ ModalTitle,
+ NumberDecrementStepper,
+ NumberField,
+ NumberIncrementStepper,
+ NumberInput,
+ NumberInputGroup,
+ NumberInputStepper,
PulsingDot,
+ Table as TableBase,
+ Tbody,
+ Td,
+ Th,
+ Thead,
Tooltip,
TooltipContent,
TooltipTrigger,
+ Tr,
VStack,
} from "@carbon/react";
import { getLocalTimeZone, parseDate } from "@internationalized/date";
import { useDateFormatter, useNumberFormatter } from "@react-aria/i18n";
import { useFetcher } from "@remix-run/react";
import type { ColumnDef } from "@tanstack/react-table";
-import { memo, useMemo } from "react";
+import { memo, useCallback, useMemo, useState } from "react";
import {
LuBookMarked,
LuBox,
+ LuCalendar,
+ LuChevronDown,
+ LuChevronUp,
LuCircleCheck,
LuCirclePlay,
LuPackage,
+ LuSquareChartGantt,
} from "react-icons/lu";
import {
Hyperlink,
@@ -29,15 +58,17 @@ import {
import { Enumerable } from "~/components/Enumerable";
import { useLocations } from "~/components/Form/Location";
import { useUnitOfMeasure } from "~/components/Form/UnitOfMeasure";
-import { useUrlParams } from "~/hooks";
+import { usePermissions, useUrlParams } from "~/hooks";
import { itemTypes } from "~/modules/inventory/inventory.models";
import { itemReorderingPolicies } from "~/modules/items/items.models";
+import type { Order } from "~/modules/items/ui/Item/ItemReorderPolicy";
import {
getOrdersFromProductionPlanning,
getReorderPolicyDescription,
ItemReorderPolicy,
} from "~/modules/items/ui/Item/ItemReorderPolicy";
import type { action as mrpAction } from "~/routes/api+/mrp";
+import type { action as bulkUpdateAction } from "~/routes/x+/production+/planning.update";
import { path } from "~/utils/path";
import type { ProductionPlanningItem } from "../../types";
@@ -48,8 +79,118 @@ type PlanningTableProps = {
periods: { id: string; startDate: string; endDate: string }[];
};
+const OrderModal = memo(
+ ({
+ row,
+ orders,
+ setOrders,
+ isOpen,
+ onClose,
+ }: {
+ row: ProductionPlanningItem;
+ orders: Order[];
+ setOrders: (item: ProductionPlanningItem, orders: Order[]) => void;
+ isOpen: boolean;
+ onClose: () => void;
+ }) => {
+ return (
+ <Modal open={isOpen} onOpenChange={(open) => !open && onClose()}>
+ <ModalContent>
+ <ModalHeader>
+ <ModalTitle>{row.readableIdWithRevision}</ModalTitle>
+ <ModalDescription>{row.name}</ModalDescription>
+ </ModalHeader>
+ <ModalBody>
+ <TableBase full>
+ <Thead>
+ <Th className="pl-0">
+ <div className="flex items-center gap-2">
+ <LuPackage />
+ <span>Quantity</span>
+ </div>
+ </Th>
+ <Th className="pr-0">
+ <div className="justify-end flex items-center gap-2">
+ <LuCalendar />
+ <span>Due Date</span>
+ </div>
+ </Th>
+ </Thead>
+ <Tbody>
+ {orders.map((order, index) => (
+ <Tr key={index}>
+ <Td className="pl-0 pr-1">
+ <NumberField
+ value={order.quantity}
+ onChange={(value) => {
+ if (row.id && value) {
+ const newOrders = [...orders];
+ newOrders[index] = {
+ ...order,
+ quantity: value,
+ };
+ setOrders(row, newOrders);
+ }
+ }}
+ >
+ <NumberInputGroup className="relative">
+ <NumberInput />
+ <NumberInputStepper>
+ <NumberIncrementStepper>
+ <LuChevronUp size="1em" strokeWidth="3" />
+ </NumberIncrementStepper>
+ <NumberDecrementStepper>
+ <LuChevronDown size="1em" strokeWidth="3" />
+ </NumberDecrementStepper>
+ </NumberInputStepper>
+ </NumberInputGroup>
+ </NumberField>
+ </Td>
+ <Td className="text-right pr-0 pl-1">
+ <HStack className="justify-end">
+ <DatePicker
+ value={parseDate(order.dueDate)}
+ onChange={(date) => {
+ if (row.id) {
+ const newOrders = [...orders];
+ newOrders[index] = {
+ ...order,
+ dueDate: date ? date.toString() : order.dueDate,
+ };
+ setOrders(row, newOrders);
+ }
+ }}
+ />
+ </HStack>
+ </Td>
+ </Tr>
+ ))}
+ </Tbody>
+ </TableBase>
+ </ModalBody>
+ <ModalFooter>
+ <Button variant="secondary" onClick={onClose}>
+ Close
+ </Button>
+ <Button
+ variant="primary"
+ onClick={() => console.log(row.id, orders)}
+ >
+ Order
+ </Button>
+ </ModalFooter>
+ </ModalContent>
+ </Modal>
+ );
+ }
+);
+
+OrderModal.displayName = "OrderModal";
+
const PlanningTable = memo(
({ data, count, locationId, periods }: PlanningTableProps) => {
+ const permissions = usePermissions();
+ const isDisabled = !permissions.can("create", "production");
const [params] = useUrlParams();
const dateFormatter = useDateFormatter({
month: "short",
@@ -60,6 +201,57 @@ const PlanningTable = memo(
const locations = useLocations();
const unitOfMeasures = useUnitOfMeasure();
+ const bulkUpdateFetcher = useFetcher<typeof bulkUpdateAction>();
+
+ // Store orders in a map keyed by item id
+ const [ordersMap, setOrderssMap] = useState<Record<string, Order[]>>(() => {
+ const initialMap: Record<string, Order[]> = {};
+ data.forEach((item) => {
+ if (item.id) {
+ initialMap[item.id] = getOrdersFromProductionPlanning(item, periods);
+ }
+ });
+ return initialMap;
+ });
+
+ const onBulkUpdate = useCallback(
+ (selectedRows: typeof data, action: "order") => {
+ const payload = {
+ locationId,
+ items: selectedRows
+ .filter((row) => row.id)
+ .map((row) => {
+ return {
+ id: row.id,
+ orders: ordersMap[row.id!] || [],
+ };
+ }),
+ action: action,
+ };
+ bulkUpdateFetcher.submit(payload, {
+ method: "post",
+ action: path.to.bulkUpdateProductionPlanning,
+ encType: "application/json",
+ });
+ },
+ [bulkUpdateFetcher, locationId, ordersMap]
+ );
+
+ const [selectedItem, setSelectedItem] =
+ useState<ProductionPlanningItem | null>(null);
+
+ const setOrders = useCallback(
+ (item: ProductionPlanningItem, orders: Order[]) => {
+ if (item.id) {
+ setOrderssMap((prev) => ({
+ ...prev,
+ [item.id!]: orders,
+ }));
+ }
+ },
+ []
+ );
+
const columns = useMemo<ColumnDef<ProductionPlanningItem>[]>(() => {
const periodColumns: ColumnDef<ProductionPlanningItem>[] = periods.map(
(period, index) => {
@@ -102,7 +294,7 @@ const PlanningTable = memo(
return [
{
accessorKey: "readableIdWithRevision",
- header: "Item ID",
+ header: "Part ID",
cell: ({ row }) => (
<HStack className="py-1">
<ItemThumbnail
@@ -141,10 +333,6 @@ const PlanningTable = memo(
accessorKey: "reorderingPolicy",
header: "Reorder Policy",
cell: ({ row }) => {
- const orders = getOrdersFromProductionPlanning(
- row.original,
- periods
- );
return (
<HStack>
<Tooltip>
@@ -157,8 +345,6 @@ const PlanningTable = memo(
{getReorderPolicyDescription(row.original)}
</TooltipContent>
</Tooltip>
-
- {orders.length > 0 && <PulsingDot />}
</HStack>
);
},
@@ -215,26 +401,67 @@ const PlanningTable = memo(
id: "Order",
header: "",
cell: ({ row }) => {
- const orders = getOrdersFromProductionPlanning(
- row.original,
- periods
+ const orders = row.original.id
+ ? ordersMap[row.original.id] || []
+ : [];
+ const hasOrders = orders.length > 0;
+ const orderQuantity = orders.reduce(
+ (acc, order) => acc + order.quantity,
+ 0
);
return (
- <Button
- isDisabled={orders.length === 0}
- variant="secondary"
- leftIcon={<LuCircleCheck />}
- onClick={() => {
- console.log(orders);
- }}
- >
- Order
- </Button>
+ <div className="flex justify-end">
+ <Button
+ variant="secondary"
+ leftIcon={hasOrders ? undefined : <LuCircleCheck />}
+ isDisabled={isDisabled}
+ onClick={() => {
+ setSelectedItem(row.original);
+ }}
+ >
+ {hasOrders ? (
+ <HStack>
+ <PulsingDot />
+ <span>Order {orderQuantity}</span>
+ </HStack>
+ ) : (
+ "Order"
+ )}
+ </Button>
+ </div>
);
},
},
];
- }, [periods, dateFormatter, numberFormatter, params, unitOfMeasures]);
+ }, [
+ periods,
+ dateFormatter,
+ numberFormatter,
+ params,
+ unitOfMeasures,
+ isDisabled,
+ ordersMap,
+ ]);
+
+ const renderActions = useCallback(
+ (selectedRows: typeof data) => {
+ return (
+ <DropdownMenuContent align="end" className="min-w-[200px]">
+ <DropdownMenuLabel>Bulk Update</DropdownMenuLabel>
+ <DropdownMenuSeparator />
+
+ <DropdownMenuItem
+ onSelect={() => onBulkUpdate(data, "order")}
+ disabled={bulkUpdateFetcher.state !== "idle"}
+ >
+ <DropdownMenuIcon icon={<LuSquareChartGantt />} />
+ Order Parts
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ );
+ },
+ [bulkUpdateFetcher.state, onBulkUpdate, data]
+ );
const defaultColumnVisibility = {
active: false,
@@ -249,49 +476,66 @@ const PlanningTable = memo(
const mrpFetcher = useFetcher<typeof mrpAction>();
return (
- <Table<ProductionPlanningItem>
- count={count}
- columns={columns}
- data={data}
- defaultColumnVisibility={defaultColumnVisibility}
- defaultColumnPinning={defaultColumnPinning}
- primaryAction={
- <div className="flex items-center gap-2">
- <Combobox
- asButton
- size="sm"
- value={locationId}
- options={locations}
- onChange={(selected) => {
- // hard refresh because initialValues update has no effect otherwise
- window.location.href = getLocationPath(selected);
- }}
- />
- <mrpFetcher.Form method="post" action={path.to.api.mrp(locationId)}>
- <Tooltip>
- <TooltipTrigger>
- <Button
- type="submit"
- variant="secondary"
- rightIcon={<LuCirclePlay />}
- isDisabled={mrpFetcher.state !== "idle"}
- isLoading={mrpFetcher.state !== "idle"}
- >
- Recalculate
- </Button>
- </TooltipTrigger>
- <TooltipContent>
- MRP runs automatically every 3 hours, but you can run it
- manually here.
- </TooltipContent>
- </Tooltip>
- </mrpFetcher.Form>
- </div>
- }
- title="Planning"
- table="inventory"
- withSavedView
- />
+ <>
+ <Table<ProductionPlanningItem>
+ count={count}
+ columns={columns}
+ data={data}
+ defaultColumnVisibility={defaultColumnVisibility}
+ defaultColumnPinning={defaultColumnPinning}
+ primaryAction={
+ <div className="flex items-center gap-2">
+ <Combobox
+ asButton
+ size="sm"
+ value={locationId}
+ options={locations}
+ onChange={(selected) => {
+ // hard refresh because initialValues update has no effect otherwise
+ window.location.href = getLocationPath(selected);
+ }}
+ />
+ <mrpFetcher.Form
+ method="post"
+ action={path.to.api.mrp(locationId)}
+ >
+ <Tooltip>
+ <TooltipTrigger>
+ <Button
+ type="submit"
+ variant="secondary"
+ rightIcon={<LuCirclePlay />}
+ isDisabled={mrpFetcher.state !== "idle"}
+ isLoading={mrpFetcher.state !== "idle"}
+ >
+ Recalculate
+ </Button>
+ </TooltipTrigger>
+ <TooltipContent>
+ MRP runs automatically every 3 hours, but you can run it
+ manually here.
+ </TooltipContent>
+ </Tooltip>
+ </mrpFetcher.Form>
+ </div>
+ }
+ renderActions={renderActions}
+ title="Planning"
+ table="planning"
+ withSavedView
+ withSelectableRows
+ />
+
+ {selectedItem && (
+ <OrderModal
+ row={selectedItem}
+ orders={selectedItem.id ? ordersMap[selectedItem.id] || [] : []}
+ setOrders={setOrders}
+ isOpen={!!selectedItem}
+ onClose={() => setSelectedItem(null)}
+ />
+ )}
+ </>
);
}
);
diff --git a/apps/erp/app/modules/production/ui/useProductionSubmodules.tsx b/apps/erp/app/modules/production/ui/useProductionSubmodules.tsx
index d2c072bc96..b473090459 100644
--- a/apps/erp/app/modules/production/ui/useProductionSubmodules.tsx
+++ b/apps/erp/app/modules/production/ui/useProductionSubmodules.tsx
@@ -29,6 +29,7 @@ const productionRoutes: AuthenticatedRouteGroup[] = [
name: "Planning",
to: path.to.productionPlanning,
icon: <LuSquareChartGantt />,
+ table: "planning",
},
{
name: "Schedule",
diff --git a/apps/erp/app/routes/x+/_index.tsx b/apps/erp/app/routes/x+/_index.tsx
index 1e020744db..bfc298173f 100644
--- a/apps/erp/app/routes/x+/_index.tsx
+++ b/apps/erp/app/routes/x+/_index.tsx
@@ -61,7 +61,7 @@ const ModuleCard = ({ module }: { module: Authenticated<NavItem> }) => (
<Link
to={module.to}
prefetch="intent"
- className="aspect-video flex flex-col gap-3 items-center justify-center py-8 shadow-button-base bg-gradient-to-bl from-card from-50% to-background rounded-lg text-center group ring-2 ring-transparent hover:ring-white/10 cursor-pointer"
+ className="aspect-video flex flex-col gap-3 items-center justify-center py-8 shadow-button-base bg-gradient-to-bl from-card from-50% to-background rounded-lg text-center group ring-2 ring-transparent hover:ring-white/10 cursor-pointer hover:scale-105 transition-all duration-300"
>
<div className="p-4 rounded-full border">
<module.icon className="text-2xl" />
diff --git a/apps/erp/app/routes/x+/accounting+/_layout.tsx b/apps/erp/app/routes/x+/accounting+/_layout.tsx
index 2da9e23142..7d6593d0df 100644
--- a/apps/erp/app/routes/x+/accounting+/_layout.tsx
+++ b/apps/erp/app/routes/x+/accounting+/_layout.tsx
@@ -13,7 +13,7 @@ import type { Handle } from "~/utils/handle";
import { path } from "~/utils/path";
export const meta: MetaFunction = () => {
- return [{ title: "Carbon | Accounting" }];
+ return [{ title: "Carbon | Finance" }];
};
export const config = {
@@ -21,7 +21,7 @@ export const config = {
};
export const handle: Handle = {
- breadcrumb: "Accounting",
+ breadcrumb: "Finance",
to: path.to.currencies,
module: "accounting",
};
diff --git a/apps/erp/app/routes/x+/production+/planning.update.tsx b/apps/erp/app/routes/x+/production+/planning.update.tsx
new file mode 100644
index 0000000000..24d043baf9
--- /dev/null
+++ b/apps/erp/app/routes/x+/production+/planning.update.tsx
@@ -0,0 +1,21 @@
+import { requirePermissions } from "@carbon/auth/auth.server";
+import { json, type ActionFunctionArgs } from "@vercel/remix";
+
+export async function action({ request }: ActionFunctionArgs) {
+ const { client, companyId, userId } = await requirePermissions(request, {
+ update: "production",
+ });
+
+ const { items, action, locationId } = await request.json();
+
+ if (typeof action !== "string") {
+ return json({ error: { message: "Invalid form data" }, data: null });
+ }
+
+ switch (action) {
+ case "order":
+ console.log({ items, action, locationId });
+ default:
+ return json({ error: { message: "Invalid field" }, data: null });
+ }
+}
diff --git a/apps/erp/app/utils/path.ts b/apps/erp/app/utils/path.ts
index 8aab083252..1e4146bb61 100644
--- a/apps/erp/app/utils/path.ts
+++ b/apps/erp/app/utils/path.ts
@@ -9,6 +9,10 @@ const mes = SUPABASE_URL.includes("localhost")
? "http://localhost:3001"
: "https://mes.carbonos.dev";
+const mes = SUPABASE_URL.includes("localhost")
+ ? "http://localhost:3001"
+ : "https://mes.carbonos.dev";
+
export const path = {
to: {
api: {
@@ -318,6 +322,7 @@ export const path = {
generatePath(`${x}/inventory/batch-property/${itemId}/property/order`),
bulkEditPermissions: `${x}/users/bulk-edit-permissions`,
bulkUpdateItems: `${x}/items/update`,
+ bulkUpdateProductionPlanning: `${x}/production/planning/update`,
bulkUpdateProcedure: `${x}/procedure/update`,
bulkUpdateJob: `${x}/job/update`,
bulkUpdateNonConformance: `${x}/non-conformance/update`,