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.

fix: select itemId and shelfId in stock transfer wizard

Brad Barbinb9accd481a02
diff --git a/apps/erp/app/modules/inventory/ui/StockTransfers/StockTransferHeader.tsx b/apps/erp/app/modules/inventory/ui/StockTransfers/StockTransferHeader.tsx
index 3569ec9bb1..257e39fa10 100644
--- a/apps/erp/app/modules/inventory/ui/StockTransfers/StockTransferHeader.tsx
+++ b/apps/erp/app/modules/inventory/ui/StockTransfers/StockTransferHeader.tsx
@@ -188,8 +188,8 @@ const StockTransferHeader = () => {
<ConfirmDelete
action={path.to.deleteStockTransfer(id)}
isOpen={deleteModal.isOpen}
- name={routeData?.stockTransfer?.id ?? "stockTransfer"}
- text={`Are you sure you want to delete ${routeData?.stockTransfer?.id}? This cannot be undone.`}
+ name={routeData?.stockTransfer?.stockTransferId ?? "stockTransfer"}
+ text={`Are you sure you want to delete ${routeData?.stockTransfer?.stockTransferId}? This cannot be undone.`}
onCancel={() => {
deleteModal.onClose();
}}
diff --git a/apps/erp/app/modules/inventory/ui/StockTransfers/StockTransfersTable.tsx b/apps/erp/app/modules/inventory/ui/StockTransfers/StockTransfersTable.tsx
index e47ba8fff0..e034d0bfd4 100644
--- a/apps/erp/app/modules/inventory/ui/StockTransfers/StockTransfersTable.tsx
+++ b/apps/erp/app/modules/inventory/ui/StockTransfers/StockTransfersTable.tsx
@@ -87,13 +87,13 @@ import { usePermissions, useUrlParams, useUser } from "~/hooks";
import { useCustomColumns } from "~/hooks/useCustomColumns";
import {
addTransferLine,
- clearSelectedToShelves,
+ clearSelectedToItemShelves,
clearStockTransferWizard,
hasTransferLine,
- hasTransferLinesToShelf,
- isToShelfSelected,
+ hasTransferLinesToItemShelf,
+ isToItemShelfSelected,
removeTransferLine,
- toggleToShelfSelection,
+ toggleToItemShelfSelection,
updateTransferLineQuantity,
usePeople,
useStockTransferWizard,
@@ -474,7 +474,7 @@ function TransferGrid({ locationId }: { locationId: string }) {
}, [carbon, companyId, locationId]);
const transferFromQuery = useCallback(async () => {
- if (!carbon || wizard.selectedToShelfIds.size === 0) {
+ if (!carbon || wizard.selectedToItemShelfIds.size === 0) {
setAllTransferFromData([]);
return;
}
@@ -483,7 +483,7 @@ function TransferGrid({ locationId }: { locationId: string }) {
// Get the selected "to" items to extract their itemIds
const selectedToItems = allTransferToData.filter((item) =>
- wizard.selectedToShelfIds.has(item.shelfId!)
+ wizard.selectedToItemShelfIds.has(`${item.itemId}:${item.shelfId}`)
);
// Fetch data for each selected item
@@ -530,7 +530,7 @@ function TransferGrid({ locationId }: { locationId: string }) {
carbon,
companyId,
locationId,
- wizard.selectedToShelfIds,
+ wizard.selectedToItemShelfIds,
allTransferToData,
]);
@@ -551,7 +551,7 @@ function TransferGrid({ locationId }: { locationId: string }) {
// Deselect active item/shelf when "to" table page changes
useEffect(() => {
- clearSelectedToShelves();
+ clearSelectedToItemShelves();
}, [transferToOffset]);
// Client-side filtering and pagination for "to" table
@@ -756,8 +756,14 @@ function TransferGrid({ locationId }: { locationId: string }) {
{
id: "actions",
cell: ({ row }) => {
- const isSelected = isToShelfSelected(row.original.shelfId!);
- const hasTransfers = hasTransferLinesToShelf(row.original.shelfId!);
+ const isSelected = isToItemShelfSelected(
+ row.original.itemId,
+ row.original.shelfId!
+ );
+ const hasTransfers = hasTransferLinesToItemShelf(
+ row.original.itemId,
+ row.original.shelfId!
+ );
return (
<div className="flex justify-end">
<Button
@@ -765,11 +771,17 @@ function TransferGrid({ locationId }: { locationId: string }) {
onClick={() => {
if (isSelected) {
// If already selected, deselect it
- toggleToShelfSelection(row.original.shelfId!);
+ toggleToItemShelfSelection(
+ row.original.itemId,
+ row.original.shelfId!
+ );
} else {
// If not selected, clear selection and select only this one
- clearSelectedToShelves();
- toggleToShelfSelection(row.original.shelfId!);
+ clearSelectedToItemShelves();
+ toggleToItemShelfSelection(
+ row.original.itemId,
+ row.original.shelfId!
+ );
}
}}
>
@@ -799,7 +811,9 @@ function TransferGrid({ locationId }: { locationId: string }) {
const toItem = allTransferToData.find(
(item) =>
item.itemId === row.original.itemId &&
- wizard.selectedToShelfIds.has(item.shelfId!)
+ wizard.selectedToItemShelfIds.has(
+ `${item.itemId}:${item.shelfId}`
+ )
);
if (!toItem) return null;
@@ -864,7 +878,9 @@ function TransferGrid({ locationId }: { locationId: string }) {
const toItem = allTransferToData.find(
(item) =>
item.itemId === row.original.itemId &&
- wizard.selectedToShelfIds.has(item.shelfId!)
+ wizard.selectedToItemShelfIds.has(
+ `${item.itemId}:${item.shelfId}`
+ )
);
if (!toItem) return null;
@@ -945,7 +961,9 @@ function TransferGrid({ locationId }: { locationId: string }) {
const toItem = allTransferToData.find(
(item) =>
item.itemId === row.original.itemId &&
- wizard.selectedToShelfIds.has(item.shelfId!)
+ wizard.selectedToItemShelfIds.has(
+ `${item.itemId}:${item.shelfId}`
+ )
);
if (!toItem) return formatter.format(row.original.quantityOnHand);
@@ -990,7 +1008,9 @@ function TransferGrid({ locationId }: { locationId: string }) {
const toItem = allTransferToData.find(
(item) =>
item.itemId === row.original.itemId &&
- wizard.selectedToShelfIds.has(item.shelfId!)
+ wizard.selectedToItemShelfIds.has(
+ `${item.itemId}:${item.shelfId}`
+ )
);
if (!toItem) {
@@ -1067,7 +1087,12 @@ function TransferGrid({ locationId }: { locationId: string }) {
header: "Required",
},
];
- }, [formatter, allTransferToData, wizard.selectedToShelfIds, wizard.lines]);
+ }, [
+ formatter,
+ allTransferToData,
+ wizard.selectedToItemShelfIds,
+ wizard.lines,
+ ]);
return (
<>
@@ -1089,7 +1114,9 @@ function TransferGrid({ locationId }: { locationId: string }) {
setPageSize={setPageSize}
search={transferToSearch}
onSearchChange={setTransferToSearch}
- isRowSelected={(row) => isToShelfSelected(row.shelfId!)}
+ isRowSelected={(row) =>
+ isToItemShelfSelected(row.itemId, row.shelfId!)
+ }
/>
</div>
</div>
@@ -1114,7 +1141,9 @@ function TransferGrid({ locationId }: { locationId: string }) {
const toItem = allTransferToData.find(
(item) =>
item.itemId === row.itemId &&
- wizard.selectedToShelfIds.has(item.shelfId!)
+ wizard.selectedToItemShelfIds.has(
+ `${item.itemId}:${item.shelfId}`
+ )
);
return toItem
? hasTransferLine(row.itemId, row.shelfId!, toItem.shelfId!)
diff --git a/apps/erp/app/routes/api+/inventory.tracked-entities-by-shelf.ts b/apps/erp/app/routes/api+/inventory.tracked-entities-by-shelf.ts
new file mode 100644
index 0000000000..8fb30fce30
--- /dev/null
+++ b/apps/erp/app/routes/api+/inventory.tracked-entities-by-shelf.ts
@@ -0,0 +1,46 @@
+import { requirePermissions } from "@carbon/auth/auth.server";
+import type { LoaderFunctionArgs } from "@vercel/remix";
+import { json } from "@vercel/remix";
+import { getItemShelfQuantities } from "~/modules/items/items.service";
+
+export async function loader({ request }: LoaderFunctionArgs) {
+ const { client, companyId } = await requirePermissions(request, {});
+
+ const url = new URL(request.url);
+ const itemId = url.searchParams.get("itemId");
+ const shelfId = url.searchParams.get("shelfId");
+ const locationId = url.searchParams.get("locationId");
+
+ if (!itemId || !locationId) {
+ return json({
+ data: [],
+ error: null,
+ });
+ }
+
+ // Get all tracked entities for the item in the location
+ const result = await getItemShelfQuantities(
+ client,
+ itemId,
+ companyId,
+ locationId
+ );
+
+ if (result.error) {
+ return json({
+ data: [],
+ error: result.error,
+ });
+ }
+
+ // Filter to only include entities from the specific shelf
+ const shelfEntities =
+ result.data?.filter(
+ (entity) => entity.shelfId === shelfId && entity.trackedEntityId
+ ) || [];
+
+ return json({
+ data: shelfEntities,
+ error: null,
+ });
+}
diff --git a/apps/erp/app/stores/stock-transfer.ts b/apps/erp/app/stores/stock-transfer.ts
index 648708d41d..79586d948b 100644
--- a/apps/erp/app/stores/stock-transfer.ts
+++ b/apps/erp/app/stores/stock-transfer.ts
@@ -102,12 +102,12 @@ export type StockTransferWizardLine = {
};
export type StockTransferWizardState = {
- selectedToShelfIds: Set<string>; // Set of shelfIds selected in the "to" table
+ selectedToItemShelfIds: Set<string>; // Set of "itemId:shelfId" composite keys selected in the "to" table
lines: StockTransferWizardLine[];
};
const $wizardStore = atom<StockTransferWizardState>({
- selectedToShelfIds: new Set(),
+ selectedToItemShelfIds: new Set(),
lines: [],
});
@@ -122,32 +122,36 @@ export const useStockTransferWizardLinesCount = () =>
useValue($wizardLinesCount);
// Stock Transfer Wizard actions
-export const toggleToShelfSelection = (shelfId: string) => {
+export const toggleToItemShelfSelection = (itemId: string, shelfId: string) => {
const currentWizard = $wizardStore.get();
- const newSelectedToShelfIds = new Set(currentWizard.selectedToShelfIds);
+ const compositeKey = `${itemId}:${shelfId}`;
+ const newSelectedToItemShelfIds = new Set(
+ currentWizard.selectedToItemShelfIds
+ );
- if (newSelectedToShelfIds.has(shelfId)) {
- newSelectedToShelfIds.delete(shelfId);
- // Remove all lines that have this toShelfId
+ if (newSelectedToItemShelfIds.has(compositeKey)) {
+ newSelectedToItemShelfIds.delete(compositeKey);
+ // Remove all lines that have this itemId and toShelfId
const updatedLines = currentWizard.lines.filter(
- (line) => line.toShelfId !== shelfId
+ (line) => !(line.itemId === itemId && line.toShelfId === shelfId)
);
$wizardStore.set({
- selectedToShelfIds: newSelectedToShelfIds,
+ selectedToItemShelfIds: newSelectedToItemShelfIds,
lines: updatedLines,
});
} else {
- newSelectedToShelfIds.add(shelfId);
+ newSelectedToItemShelfIds.add(compositeKey);
$wizardStore.set({
...currentWizard,
- selectedToShelfIds: newSelectedToShelfIds,
+ selectedToItemShelfIds: newSelectedToItemShelfIds,
});
}
};
-export const isToShelfSelected = (shelfId: string) => {
+export const isToItemShelfSelected = (itemId: string, shelfId: string) => {
const currentWizard = $wizardStore.get();
- return currentWizard.selectedToShelfIds.has(shelfId);
+ const compositeKey = `${itemId}:${shelfId}`;
+ return currentWizard.selectedToItemShelfIds.has(compositeKey);
};
export const addTransferLine = (line: StockTransferWizardLine) => {
@@ -209,10 +213,16 @@ export const hasTransferLine = (
);
};
-export const hasTransferLinesToShelf = (shelfId: string) => {
+export const hasTransferLinesToItemShelf = (
+ itemId: string,
+ shelfId: string
+) => {
const currentWizard = $wizardStore.get();
return currentWizard.lines.some(
- (line) => line.toShelfId === shelfId && (line.quantity ?? 0) > 0
+ (line) =>
+ line.itemId === itemId &&
+ line.toShelfId === shelfId &&
+ (line.quantity ?? 0) > 0
);
};
@@ -242,15 +252,15 @@ export const updateTransferLineQuantity = (
export const clearStockTransferWizard = () => {
$wizardStore.set({
- selectedToShelfIds: new Set(),
+ selectedToItemShelfIds: new Set(),
lines: [],
});
};
-export const clearSelectedToShelves = () => {
+export const clearSelectedToItemShelves = () => {
const currentWizard = $wizardStore.get();
$wizardStore.set({
...currentWizard,
- selectedToShelfIds: new Set(),
+ selectedToItemShelfIds: new Set(),
});
};
diff --git a/apps/erp/app/utils/path.ts b/apps/erp/app/utils/path.ts
index f54cc493e5..b496040d46 100644
--- a/apps/erp/app/utils/path.ts
+++ b/apps/erp/app/utils/path.ts
@@ -19,6 +19,14 @@ export const path = {
assign: `${api}/assign`,
batchNumbers: (itemId: string) =>
generatePath(`${api}/inventory/batch-numbers?itemId=${itemId}`),
+ trackedEntitiesByShelf: (
+ itemId: string,
+ shelfId: string,
+ locationId: string
+ ) =>
+ generatePath(
+ `${api}/inventory/tracked-entities-by-shelf?itemId=${itemId}&shelfId=${shelfId}&locationId=${locationId}`
+ ),
billOfMaterials: (methodId: string, withOperations: boolean = false) =>
generatePath(
`${api}/items/methods/${methodId}/bom?withOperations=${withOperations}`