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 shelf to jobs

Brad Barbin8c692a327515
diff --git a/apps/erp/app/modules/inventory/inventory.service.ts b/apps/erp/app/modules/inventory/inventory.service.ts
index b5a92b71e1..cac0a59e29 100644
--- a/apps/erp/app/modules/inventory/inventory.service.ts
+++ b/apps/erp/app/modules/inventory/inventory.service.ts
@@ -7,6 +7,7 @@ import type { StorageItem } from "~/types";
import type { GenericQueryFilters } from "~/utils/query";
import { setGenericQueryFilters } from "~/utils/query";
import { sanitize } from "~/utils/supabase";
+import { getItemShelfQuantities } from "../items/items.service";
import type {
batchPropertyOrderValidator,
batchPropertyValidator,
@@ -1147,3 +1148,42 @@ export async function upsertWarehouseTransferLine(
.single();
}
}
+
+export async function getDefaultShelfForJob(
+ client: SupabaseClient<Database>,
+ itemId: string,
+ locationId: string,
+ companyId: string
+): Promise<string | null> {
+ const pickMethod = await client
+ .from("pickMethod")
+ .select("defaultShelfId")
+ .eq("itemId", itemId)
+ .eq("locationId", locationId)
+ .eq("companyId", companyId)
+ .maybeSingle();
+
+ if (pickMethod.data?.defaultShelfId) {
+ return pickMethod.data.defaultShelfId;
+ }
+
+ const itemShelfQuantities = await getItemShelfQuantities(
+ client,
+ itemId,
+ companyId,
+ locationId
+ );
+
+ if (itemShelfQuantities.data?.length) {
+ // Find the shelf with the highest quantity
+ const shelfWithHighestQuantity = itemShelfQuantities.data.reduce(
+ (max, current) => {
+ return (current.quantity ?? 0) > (max.quantity ?? 0) ? current : max;
+ }
+ );
+
+ return shelfWithHighestQuantity.shelfId;
+ }
+
+ return null;
+}
diff --git a/apps/erp/app/modules/inventory/ui/useInventorySubmodules.tsx b/apps/erp/app/modules/inventory/ui/useInventorySubmodules.tsx
index 1ced89ebd4..552b76dd0c 100644
--- a/apps/erp/app/modules/inventory/ui/useInventorySubmodules.tsx
+++ b/apps/erp/app/modules/inventory/ui/useInventorySubmodules.tsx
@@ -1,6 +1,5 @@
import {
LuArrowRightLeft,
- LuFolderKanban,
LuHandCoins,
LuNetwork,
LuQrCode,
@@ -17,6 +16,12 @@ const inventoryRoutes: AuthenticatedRouteGroup[] = [
{
name: "Manage",
routes: [
+ {
+ name: "Kanbans",
+ to: path.to.kanbans,
+ role: "employee",
+ icon: <LuQrCode />,
+ },
{
name: "Receipts",
to: path.to.receipts,
@@ -64,12 +69,6 @@ const inventoryRoutes: AuthenticatedRouteGroup[] = [
{
name: "Configure",
routes: [
- {
- name: "Kanbans",
- to: path.to.kanbans,
- role: "employee",
- icon: <LuFolderKanban />,
- },
{
name: "Shelves",
to: path.to.shelves,
diff --git a/apps/erp/app/modules/production/production.service.ts b/apps/erp/app/modules/production/production.service.ts
index d6721b253f..864226de08 100644
--- a/apps/erp/app/modules/production/production.service.ts
+++ b/apps/erp/app/modules/production/production.service.ts
@@ -13,6 +13,7 @@ import type { StorageItem } from "~/types";
import type { GenericQueryFilters } from "~/utils/query";
import { setGenericQueryFilters } from "~/utils/query";
import { sanitize } from "~/utils/supabase";
+import { getDefaultShelfForJob } from "../inventory";
import type {
operationParameterValidator,
operationStepValidator,
@@ -146,6 +147,13 @@ export async function convertSalesOrderLinesToJobs(
}
}
+ const shelfId = await getDefaultShelfForJob(
+ serviceRole,
+ line.itemId,
+ locationId!,
+ companyId
+ );
+
const data = {
customerId: salesOrder.data?.customerId ?? undefined,
deadlineType: "Hard Deadline" as const,
@@ -164,6 +172,7 @@ export async function convertSalesOrderLinesToJobs(
salesOrderId: salesOrderId ?? undefined,
salesOrderLineId: line.id,
scrapQuantity: 0,
+ shelfId: shelfId ?? undefined,
unitOfMeasureCode: line.unitOfMeasureCode ?? "EA",
};
@@ -1585,6 +1594,7 @@ export async function upsertJob(
job:
| (Omit<z.infer<typeof jobValidator>, "id" | "jobId"> & {
jobId: string;
+ shelfId?: string;
startDate?: string;
companyId: string;
createdBy: string;
diff --git a/apps/erp/app/modules/production/ui/Jobs/JobHeader.tsx b/apps/erp/app/modules/production/ui/Jobs/JobHeader.tsx
index c7a3147634..b81ab6cec9 100644
--- a/apps/erp/app/modules/production/ui/Jobs/JobHeader.tsx
+++ b/apps/erp/app/modules/production/ui/Jobs/JobHeader.tsx
@@ -892,7 +892,7 @@ function JobCompleteModal({
salesOrderId: job.salesOrderId ?? undefined,
salesOrderLineId: job.salesOrderLineId ?? undefined,
locationId: job.locationId ?? undefined,
- shelfId: defaultShelfId ?? undefined,
+ shelfId: job.shelfId ?? defaultShelfId ?? undefined,
}}
fetcher={fetcher}
>
diff --git a/apps/erp/app/routes/api+/kanban.$id.tsx b/apps/erp/app/routes/api+/kanban.$id.tsx
index 07ead27575..ed17adc529 100644
--- a/apps/erp/app/routes/api+/kanban.$id.tsx
+++ b/apps/erp/app/routes/api+/kanban.$id.tsx
@@ -11,7 +11,7 @@ import { defer, type LoaderFunctionArgs } from "@vercel/remix";
import { Suspense } from "react";
import { Redirect } from "~/components/Redirect";
-import { getKanban } from "~/modules/inventory";
+import { getDefaultShelfForJob, getKanban } from "~/modules/inventory";
import { getItemReplenishment } from "~/modules/items";
import {
getActiveJobOperationByJobId,
@@ -71,9 +71,15 @@ async function handleKanban({
};
}
- const [nextSequence, manufacturing] = await Promise.all([
+ const [nextSequence, manufacturing, defaultShelf] = await Promise.all([
getNextSequence(client, "job", companyId),
getItemReplenishment(client, kanban.data.itemId!, companyId),
+ getDefaultShelfForJob(
+ client,
+ kanban.data.itemId!,
+ kanban.data.locationId!,
+ companyId
+ ),
]);
if (nextSequence.error) {
@@ -98,11 +104,15 @@ async function handleKanban({
};
}
+ // Use shelf from kanban if it exists, otherwise use default shelf
+ const shelfId = kanban.data.shelfId || defaultShelf || undefined;
+
const createdJob = await upsertJob(client, {
jobId: jobReadableId,
itemId: kanban.data.itemId!,
quantity: kanban.data.quantity!,
locationId: kanban.data.locationId!,
+ shelfId,
unitOfMeasureCode: kanban.data.purchaseUnitOfMeasureCode!,
deadlineType: "Hard Deadline",
scrapQuantity: 0,
diff --git a/apps/erp/app/routes/x+/job+/bulk.new.tsx b/apps/erp/app/routes/x+/job+/bulk.new.tsx
index 02973ecedc..090354884d 100644
--- a/apps/erp/app/routes/x+/job+/bulk.new.tsx
+++ b/apps/erp/app/routes/x+/job+/bulk.new.tsx
@@ -14,6 +14,7 @@ import {
} from "@internationalized/date";
import { tasks } from "@trigger.dev/sdk/v3";
import { redirect, type ActionFunctionArgs } from "@vercel/remix";
+import { getDefaultShelfForJob } from "~/modules/inventory";
import { getItemReplenishment } from "~/modules/items";
import {
bulkJobValidator,
@@ -106,6 +107,13 @@ export async function action({ request }: ActionFunctionArgs) {
}
}
+ const shelfId = await getDefaultShelfForJob(
+ serviceRole,
+ jobData.itemId,
+ jobData.locationId,
+ companyId
+ );
+
for await (const [i] of Array.from({ length: jobs }, (_, i) => [i])) {
const nextSequence = await getNextSequence(serviceRole, "job", companyId);
if (nextSequence.error) {
@@ -138,6 +146,7 @@ export async function action({ request }: ActionFunctionArgs) {
.subtract({ days: manufacturing.data?.leadTime ?? 7 })
.toString()
: undefined,
+ shelfId: shelfId ?? undefined,
configuration,
companyId,
createdBy: userId,
diff --git a/apps/erp/app/routes/x+/job+/new.tsx b/apps/erp/app/routes/x+/job+/new.tsx
index d92a46efef..df016ee5e7 100644
--- a/apps/erp/app/routes/x+/job+/new.tsx
+++ b/apps/erp/app/routes/x+/job+/new.tsx
@@ -8,6 +8,7 @@ import { tasks } from "@trigger.dev/sdk/v3";
import type { ActionFunctionArgs } from "@vercel/remix";
import { redirect } from "@vercel/remix";
import { useUrlParams, useUser } from "~/hooks";
+import { getDefaultShelfForJob } from "~/modules/inventory";
import { getItemReplenishment } from "~/modules/items";
import { jobValidator, upsertJob, upsertJobMethod } from "~/modules/production";
import { JobForm } from "~/modules/production/ui/Jobs";
@@ -80,10 +81,18 @@ export async function action({ request }: ActionFunctionArgs) {
}
}
+ const shelfId = await getDefaultShelfForJob(
+ client,
+ validation.data.itemId,
+ validation.data.locationId,
+ companyId
+ );
+
const createJob = await upsertJob(client, {
...data,
jobId,
configuration,
+ shelfId: shelfId ?? undefined,
startDate: data.dueDate
? parseDate(data.dueDate).subtract({ days: leadTime }).toString()
: undefined,
diff --git a/apps/erp/app/routes/x+/part+/$itemId.view.inventory.tsx b/apps/erp/app/routes/x+/part+/$itemId.view.inventory.tsx
index 0ee911c763..a05f7d2075 100644
--- a/apps/erp/app/routes/x+/part+/$itemId.view.inventory.tsx
+++ b/apps/erp/app/routes/x+/part+/$itemId.view.inventory.tsx
@@ -101,12 +101,10 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
}
}
- const quantities = await getItemQuantities(
- client,
- itemId,
- companyId,
- locationId
- );
+ const [quantities, itemShelfQuantities] = await Promise.all([
+ getItemQuantities(client, itemId, companyId, locationId),
+ getItemShelfQuantities(client, itemId, companyId, locationId),
+ ]);
if (quantities.error) {
throw redirect(
path.to.items,
@@ -114,12 +112,6 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
);
}
- const itemShelfQuantities = await getItemShelfQuantities(
- client,
- itemId,
- companyId,
- locationId
- );
if (itemShelfQuantities.error || !itemShelfQuantities.data) {
throw redirect(
path.to.items,
diff --git a/apps/erp/app/routes/x+/production+/planning.update.tsx b/apps/erp/app/routes/x+/production+/planning.update.tsx
index 0d2d23eea2..820bb6802f 100644
--- a/apps/erp/app/routes/x+/production+/planning.update.tsx
+++ b/apps/erp/app/routes/x+/production+/planning.update.tsx
@@ -1,6 +1,7 @@
import { requirePermissions } from "@carbon/auth/auth.server";
import { json, type ActionFunctionArgs } from "@vercel/remix";
import { z } from "zod";
+import { getDefaultShelfForJob } from "~/modules/inventory";
import {
productionOrderValidator,
recalculateJobRequirements,
@@ -178,6 +179,13 @@ export async function action({ request }: ActionFunctionArgs) {
continue;
}
+ const shelfId = await getDefaultShelfForJob(
+ client,
+ item.id,
+ locationId,
+ companyId
+ );
+
const createJob = await upsertJob(
client,
{
@@ -191,6 +199,7 @@ export async function action({ request }: ActionFunctionArgs) {
dueDate: order.dueDate ?? undefined,
deadlineType: order.isASAP ? "ASAP" : "Soft Deadline",
locationId,
+ shelfId: shelfId ?? undefined,
companyId,
createdBy: userId,
unitOfMeasureCode: "EA",
diff --git a/apps/erp/app/routes/x+/sales-order+/$orderId.$lineId.job.tsx b/apps/erp/app/routes/x+/sales-order+/$orderId.$lineId.job.tsx
index 1178fdd6ae..7b92b88e2b 100644
--- a/apps/erp/app/routes/x+/sales-order+/$orderId.$lineId.job.tsx
+++ b/apps/erp/app/routes/x+/sales-order+/$orderId.$lineId.job.tsx
@@ -7,6 +7,7 @@ import { parseDate } from "@internationalized/date";
import { tasks } from "@trigger.dev/sdk/v3";
import type { ActionFunctionArgs } from "@vercel/remix";
import { redirect } from "@vercel/remix";
+import { getDefaultShelfForJob } from "~/modules/inventory";
import { getItemReplenishment } from "~/modules/items";
import {
salesOrderToJobValidator,
@@ -72,9 +73,17 @@ export async function action({ request, params }: ActionFunctionArgs) {
if (!jobId) throw new Error("jobId is not defined");
const { id: _id, ...data } = validation.data;
+ const shelfId = await getDefaultShelfForJob(
+ serviceRole,
+ validation.data.itemId,
+ validation.data.locationId,
+ companyId
+ );
+
const createJob = await upsertJob(serviceRole, {
...data,
jobId,
+ shelfId: shelfId ?? undefined,
startDate: data.dueDate
? parseDate(data.dueDate).subtract({ days: leadTime }).toString()
: undefined,
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 0566bf6c42..2095f174e2 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -9251,6 +9251,7 @@ export type Database = {
salesOrderLineId: string | null
scrapQuantity: number
secondsToComplete: number | null
+ shelfId: string | null
startDate: string | null
status: Database["public"]["Enums"]["jobStatus"]
tags: string[] | null
@@ -9289,6 +9290,7 @@ export type Database = {
salesOrderLineId?: string | null
scrapQuantity?: number
secondsToComplete?: number | null
+ shelfId?: string | null
startDate?: string | null
status?: Database["public"]["Enums"]["jobStatus"]
tags?: string[] | null
@@ -9327,6 +9329,7 @@ export type Database = {
salesOrderLineId?: string | null
scrapQuantity?: number
secondsToComplete?: number | null
+ shelfId?: string | null
startDate?: string | null
status?: Database["public"]["Enums"]["jobStatus"]
tags?: string[] | null
@@ -9559,6 +9562,13 @@ export type Database = {
referencedRelation: "salesOrderLines"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "job_shelfId_fkey"
+ columns: ["shelfId"]
+ isOneToOne: false
+ referencedRelation: "shelf"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "job_unitOfMeasureCode_fkey"
columns: ["unitOfMeasureCode", "companyId"]
@@ -39546,6 +39556,7 @@ export type Database = {
salesOrderReadableId: string | null
scrapQuantity: number | null
secondsToComplete: number | null
+ shelfId: string | null
startDate: string | null
status: Database["public"]["Enums"]["jobStatus"] | null
tags: string[] | null
@@ -39779,6 +39790,13 @@ export type Database = {
referencedRelation: "salesOrderLines"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "job_shelfId_fkey"
+ columns: ["shelfId"]
+ isOneToOne: false
+ referencedRelation: "shelf"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "job_unitOfMeasureCode_fkey"
columns: ["unitOfMeasureCode", "companyId"]
@@ -45474,14 +45492,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["paymentCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["paymentCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 0566bf6c42..2095f174e2 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -9251,6 +9251,7 @@ export type Database = {
salesOrderLineId: string | null
scrapQuantity: number
secondsToComplete: number | null
+ shelfId: string | null
startDate: string | null
status: Database["public"]["Enums"]["jobStatus"]
tags: string[] | null
@@ -9289,6 +9290,7 @@ export type Database = {
salesOrderLineId?: string | null
scrapQuantity?: number
secondsToComplete?: number | null
+ shelfId?: string | null
startDate?: string | null
status?: Database["public"]["Enums"]["jobStatus"]
tags?: string[] | null
@@ -9327,6 +9329,7 @@ export type Database = {
salesOrderLineId?: string | null
scrapQuantity?: number
secondsToComplete?: number | null
+ shelfId?: string | null
startDate?: string | null
status?: Database["public"]["Enums"]["jobStatus"]
tags?: string[] | null
@@ -9559,6 +9562,13 @@ export type Database = {
referencedRelation: "salesOrderLines"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "job_shelfId_fkey"
+ columns: ["shelfId"]
+ isOneToOne: false
+ referencedRelation: "shelf"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "job_unitOfMeasureCode_fkey"
columns: ["unitOfMeasureCode", "companyId"]
@@ -39546,6 +39556,7 @@ export type Database = {
salesOrderReadableId: string | null
scrapQuantity: number | null
secondsToComplete: number | null
+ shelfId: string | null
startDate: string | null
status: Database["public"]["Enums"]["jobStatus"] | null
tags: string[] | null
@@ -39779,6 +39790,13 @@ export type Database = {
referencedRelation: "salesOrderLines"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "job_shelfId_fkey"
+ columns: ["shelfId"]
+ isOneToOne: false
+ referencedRelation: "shelf"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "job_unitOfMeasureCode_fkey"
columns: ["unitOfMeasureCode", "companyId"]
@@ -45474,14 +45492,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["paymentCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["paymentCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
diff --git a/packages/database/supabase/migrations/20251001133427_remove-production-event-trigger.sql b/packages/database/supabase/migrations/20251001133427_remove-production-event-trigger.sql
index a6b35f79fb..63a455820a 100644
--- a/packages/database/supabase/migrations/20251001133427_remove-production-event-trigger.sql
+++ b/packages/database/supabase/migrations/20251001133427_remove-production-event-trigger.sql
@@ -54,17 +54,12 @@ BEGIN
BEGIN
- SELECT "locationId"
- INTO job_location_id
+ SELECT "locationId", "shelfId"
+ INTO job_location_id, job_shelf_id
FROM "job"
WHERE "id" = NEW."jobId";
- SELECT "defaultShelfId"
- INTO job_shelf_id
- FROM "pickMethod"
- WHERE "itemId" = job_item_id
- AND "locationId" = job_location_id
- LIMIT 1;
+
-- Get sales order info
SELECT "salesOrderId" INTO sales_order_id FROM "job" WHERE "id" = NEW."jobId";