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 ability to issue serialized parts to jobs

Brad Barbincb7aa915a736
diff --git a/apps/mes/app/components/AdjustInventory.tsx b/apps/mes/app/components/AdjustInventory.tsx
index d00c6e6769..941a8bb6de 100644
--- a/apps/mes/app/components/AdjustInventory.tsx
+++ b/apps/mes/app/components/AdjustInventory.tsx
@@ -132,7 +132,8 @@ export function AdjustInventory({ add }: { add: boolean }) {
<ModalHeader>
<ModalTitle>{add ? "Add" : "Remove"} Inventory</ModalTitle>
<ModalDescription>
- Manually {add ? "add" : "remove"} items to inventory
+ Manually {add ? "add" : "remove"} items {add ? "to" : "from"}{" "}
+ inventory
</ModalDescription>
</ModalHeader>
<ModalBody>
diff --git a/apps/mes/app/components/JobOperation/JobOperation.tsx b/apps/mes/app/components/JobOperation/JobOperation.tsx
index 3e9820c8fe..2d5066ece7 100644
--- a/apps/mes/app/components/JobOperation/JobOperation.tsx
+++ b/apps/mes/app/components/JobOperation/JobOperation.tsx
@@ -103,7 +103,6 @@ import type {
import { useItems } from "~/stores";
import { path } from "~/utils/path";
import ItemThumbnail from "../ItemThumbnail";
-import { BatchIssueModal } from "./components/BatchIssueModal";
import { OperationChat } from "./components/Chat";
import {
Controls,
@@ -112,11 +111,10 @@ import {
Times,
WorkTypeToggle
} from "./components/Controls";
-import { IssueModal } from "./components/IssueModal";
+import { IssueMaterialModal } from "./components/IssueMaterialModal";
import { MaintenanceDispatch } from "./components/MaintenanceDispatch";
import { ParametersListItem } from "./components/Parameter";
import { QuantityModal } from "./components/QuantityModal";
-import { SerialIssueModal } from "./components/SerialIssueModal";
import { SerialSelectorModal } from "./components/SerialSelectorModal";
import {
DeleteStepRecordModal,
@@ -1270,57 +1268,23 @@ export const JobOperation = ({
)}
</Tbody>
</Table>
- {issueModal.isOpen &&
- selectedMaterial?.requiresBatchTracking !== true &&
- selectedMaterial?.requiresSerialTracking !==
- true && (
- <IssueModal
- operationId={operation.id}
- material={selectedMaterial ?? undefined}
- onClose={() => {
- setSelectedMaterial(null);
- issueModal.onClose();
- }}
- />
- )}
- {issueModal.isOpen &&
- selectedMaterial?.requiresBatchTracking ===
- true && (
- <BatchIssueModal
- parentId={trackedEntityId ?? ""}
- parentIdIsSerialized={
- method?.requiresSerialTracking ?? false
- }
- operationId={operation.id}
- material={selectedMaterial ?? undefined}
- trackedInputs={
- resolvedMaterials?.trackedInputs ?? []
- }
- onClose={() => {
- setSelectedMaterial(null);
- issueModal.onClose();
- }}
- />
- )}
- {issueModal.isOpen &&
- selectedMaterial?.requiresSerialTracking ===
- true && (
- <SerialIssueModal
- operationId={operation.id}
- material={selectedMaterial ?? undefined}
- parentId={trackedEntityId ?? ""}
- parentIdIsSerialized={
- method?.requiresSerialTracking ?? false
- }
- trackedInputs={
- resolvedMaterials?.trackedInputs ?? []
- }
- onClose={() => {
- setSelectedMaterial(null);
- issueModal.onClose();
- }}
- />
- )}
+ {issueModal.isOpen && (
+ <IssueMaterialModal
+ operationId={operation.id}
+ material={selectedMaterial ?? undefined}
+ parentId={trackedEntityId ?? ""}
+ parentIdIsSerialized={
+ method?.requiresSerialTracking ?? false
+ }
+ trackedInputs={
+ resolvedMaterials?.trackedInputs ?? []
+ }
+ onClose={() => {
+ setSelectedMaterial(null);
+ issueModal.onClose();
+ }}
+ />
+ )}
</>
);
}}
diff --git a/apps/mes/app/components/JobOperation/components/BatchIssueModal.tsx b/apps/mes/app/components/JobOperation/components/BatchIssueModal.tsx
deleted file mode 100644
index 4d71c8d92e..0000000000
--- a/apps/mes/app/components/JobOperation/components/BatchIssueModal.tsx
+++ /dev/null
@@ -1,943 +0,0 @@
-import {
- Input as FormInput,
- Number as FormNumberInput,
- Hidden,
- ValidatedForm
-} from "@carbon/form";
-import {
- Alert,
- AlertDescription,
- AlertTitle,
- Button,
- Combobox as ComboboxBase,
- cn,
- IconButton,
- Input,
- InputGroup,
- InputRightElement,
- Modal,
- ModalBody,
- ModalContent,
- ModalDescription,
- ModalFooter,
- ModalHeader,
- ModalTitle,
- NumberDecrementStepper,
- NumberField,
- NumberIncrementStepper,
- NumberInput,
- NumberInputGroup,
- NumberInputStepper,
- Tabs,
- TabsContent,
- TabsList,
- TabsTrigger,
- toast,
- useDisclosure
-} from "@carbon/react";
-import type { TrackedEntityAttributes } from "@carbon/utils";
-import { getItemReadableId } from "@carbon/utils";
-import { useNumberFormatter } from "@react-aria/i18n";
-import { useCallback, useEffect, useMemo, useRef, useState } from "react";
-import {
- LuArrowRightLeft,
- LuCheck,
- LuChevronDown,
- LuChevronUp,
- LuCirclePlus,
- LuGitBranch,
- LuList,
- LuPrinter,
- LuQrCode,
- LuScale,
- LuTrash,
- LuUndo2,
- LuX
-} from "react-icons/lu";
-import { useFetcher } from "react-router";
-import type { getBatchNumbersForItem } from "~/services/inventory.service";
-import { convertEntityValidator } from "~/services/models";
-import type { JobMaterial, TrackedInput } from "~/services/types";
-import { useItems } from "~/stores";
-import { path } from "~/utils/path";
-
-export function BatchIssueModal({
- parentId,
- parentIdIsSerialized,
- operationId,
- material,
- trackedInputs,
- onClose
-}: {
- parentId: string;
- parentIdIsSerialized: boolean;
- operationId: string;
- material?: JobMaterial;
- trackedInputs: TrackedInput[];
- onClose: () => void;
-}) {
- const { data: batchNumbers } = useBatchNumbers(material?.itemId ?? "");
-
- const [errors, setErrors] = useState<Record<number, string>>({});
-
- const options = useMemo(() => {
- return (
- batchNumbers?.data
- ?.filter((batchNumber) => batchNumber.status === "Available")
- .map((batchNumber) => {
- const attributes = batchNumber.attributes as TrackedEntityAttributes;
- return {
- label: batchNumber.sourceDocumentReadableId ?? "",
- value: batchNumber.id,
- helper: batchNumber.readableId
- ? `${batchNumber.id.slice(0, 10)} - ${
- batchNumber.quantity
- } Available of Batch ${batchNumber.readableId}`
- : `${batchNumber.id.slice(0, 10)} - ${
- batchNumber.quantity
- } Available`,
- availableQuantity: batchNumber.quantity
- };
- }) ?? []
- );
- }, [batchNumbers]);
-
- const unconsumeOptions = useMemo(() => {
- return trackedInputs.map((input) => ({
- label: input.id,
- value: input.id,
- helper: `${input.quantity} ${
- input.readableId ? `of Batch ${input.readableId}` : ""
- }`
- }));
- }, [trackedInputs]);
-
- const initialQuantity = parentIdIsSerialized
- ? (material?.quantity ?? 1)
- : (material?.estimatedQuantity ?? 1);
-
- const [selectedBatchNumbers, setSelectedBatchNumbers] = useState<
- Array<{
- index: number;
- id: string;
- quantity: number;
- }>
- >(
- Array(1)
- .fill("")
- .map((_, index) => ({ index, id: "", quantity: initialQuantity }))
- );
-
- const validateBatchNumber = useCallback(
- (value: string, quantity: number, index: number) => {
- if (!value) return "Batch number is required";
-
- // Check for duplicates
- const isDuplicate = selectedBatchNumbers.some(
- (bn, i) => bn.id === value && i !== index
- );
- if (isDuplicate) return "Duplicate batch number";
-
- // Check if batch number exists in options
- const batchOption = options.find((option) => option.value === value);
- if (!batchOption) {
- const batchNumber = batchNumbers?.data?.find((bn) => bn.id === value);
- if (batchNumber) return `Batch number is ${batchNumber.status}`;
- return "Batch number is not available";
- }
-
- // Check if quantity is valid
- if (quantity <= 0) return "Quantity must be greater than 0";
- if (quantity > batchOption.availableQuantity)
- return `Quantity cannot exceed available quantity (${batchOption.availableQuantity})`;
-
- return null;
- },
- [selectedBatchNumbers, options, batchNumbers?.data]
- );
-
- const updateBatchNumber = useCallback(
- (batchNumber: { index: number; id: string; quantity: number }) => {
- setSelectedBatchNumbers((prev) => {
- const newBatchNumbers = [...prev];
- newBatchNumbers[batchNumber.index] = batchNumber;
- return newBatchNumbers;
- });
- },
- []
- );
-
- const addBatchNumber = useCallback(() => {
- setSelectedBatchNumbers((prev) => {
- const newIndex = prev.length;
- return [...prev, { index: newIndex, id: "", quantity: 1 }];
- });
- }, []);
-
- const removeBatchNumber = useCallback((indexToRemove: number) => {
- setSelectedBatchNumbers((prev) => {
- // Remove the item at the specified index
- const filtered = prev.filter((_, i) => i !== indexToRemove);
-
- // Reindex the remaining items
- return filtered.map((item, i) => ({ ...item, index: i }));
- });
-
- // Clean up any errors for the removed index
- setErrors((prev) => {
- const newErrors = { ...prev };
- delete newErrors[indexToRemove];
-
- // Reindex the errors for indices greater than the removed one
- const reindexedErrors: Record<number, string> = {};
- Object.entries(newErrors).forEach(([key, value]) => {
- const keyNum = parseInt(key);
- if (keyNum > indexToRemove) {
- reindexedErrors[keyNum - 1] = value;
- } else {
- reindexedErrors[keyNum] = value;
- }
- });
-
- return reindexedErrors;
- });
- }, []);
-
- const fetcher = useFetcher<{
- success: boolean;
- message: string;
- splitEntities?: Array<{
- originalId: string;
- newId: string;
- quantity: number;
- readableId?: string;
- }>;
- }>();
-
- // biome-ignore lint/correctness/useExhaustiveDependencies: suppressed due to migration
- const handleSubmit = useCallback(() => {
- // Validate all batch numbers
- let hasErrors = false;
- const newErrors: Record<number, string> = {};
-
- selectedBatchNumbers.forEach((bn) => {
- const error = validateBatchNumber(bn.id, bn.quantity, bn.index);
- if (error) {
- newErrors[bn.index] = error;
- hasErrors = true;
- }
- });
-
- setErrors(newErrors);
-
- if (!hasErrors) {
- // Submit to the API
- const payload = {
- materialId: material?.id!,
- parentTrackedEntityId: parentId,
- children: selectedBatchNumbers.map((bn) => ({
- trackedEntityId: bn.id,
- quantity: bn.quantity
- }))
- };
-
- fetcher.submit(JSON.stringify(payload), {
- method: "post",
- action: path.to.issueTrackedEntity,
- encType: "application/json"
- });
- }
- }, [
- selectedBatchNumbers,
- validateBatchNumber,
- operationId,
- parentId,
- onClose,
- material?.id
- ]);
-
- const [splitEntitiesResult, setSplitEntitiesResult] = useState<
- {
- newId: string;
- originalId: string;
- quantity: number;
- readableId?: string;
- }[]
- >([]);
-
- const processedFetcherData = useRef<typeof fetcher.data | null>(null);
-
- useEffect(() => {
- // Only process if we have new data that we haven't processed yet
- if (
- fetcher.state === "idle" &&
- fetcher.data &&
- fetcher.data !== processedFetcherData.current
- ) {
- processedFetcherData.current = fetcher.data;
-
- if (fetcher.data.success) {
- if (
- fetcher.data.splitEntities &&
- fetcher.data.splitEntities.length > 0
- ) {
- // Map the split entities to our state format (use newId as the id)
- setSplitEntitiesResult(
- fetcher.data.splitEntities.map((entity) => ({
- newId: entity.newId,
- originalId: entity.originalId,
- readableId: entity.readableId,
- quantity: entity.quantity
- }))
- );
- toast.success(fetcher.data.message);
- } else {
- onClose();
- if (fetcher.data.message) {
- toast.success(fetcher.data.message);
- }
- }
- } else if (fetcher.data.message) {
- toast.error(fetcher.data.message);
- }
- }
- }, [fetcher.state, fetcher.data, onClose]);
-
- const validateBatchInput = useCallback(
- (value: string, index: number) => {
- if (!value) {
- setErrors((prev) => ({ ...prev, [index]: "Batch number is required" }));
- return false;
- }
-
- // Check for duplicates by comparing with all other batch numbers
- // This ensures we catch duplicates even after adding new batch numbers
- const duplicateIndices = selectedBatchNumbers
- .map((bn, i) => (bn.id === value && i !== index ? i : -1))
- .filter((i) => i !== -1);
-
- if (duplicateIndices.length > 0) {
- setErrors((prev) => ({ ...prev, [index]: "Duplicate batch number" }));
- return false;
- }
-
- // Check if batch number exists in options
- const batchOption = options.find((option) => option.value === value);
- if (!batchOption) {
- setErrors((prev) => ({
- ...prev,
- [index]: "Batch number is not available"
- }));
- return false;
- }
-
- // Check if the requested quantity exceeds available quantity
- const currentBatchNumber = selectedBatchNumbers[index];
- if (currentBatchNumber.quantity > batchOption.availableQuantity) {
- // If we need more than available, create a new batch row with remaining quantity
- const remainingQuantity =
- currentBatchNumber.quantity - batchOption.availableQuantity;
-
- // Update current row to use maximum available quantity
- updateBatchNumber({
- ...currentBatchNumber,
- id: value,
- quantity: batchOption.availableQuantity
- });
-
- // Add a new row for the remaining quantity
- setSelectedBatchNumbers((prev) => {
- const newIndex = prev.length;
- return [
- ...prev,
- { index: newIndex, id: "", quantity: remainingQuantity }
- ];
- });
- }
-
- // Clear errors if valid
- setErrors((prev) => {
- const newErrors = { ...prev };
- delete newErrors[index];
- return newErrors;
- });
- return true;
- },
- [selectedBatchNumbers, options, updateBatchNumber]
- );
-
- const [activeTab, setActiveTab] = useState("scan");
-
- const [unconsumedBatch, setUnconsumedBatch] = useState("");
- // biome-ignore lint/correctness/useExhaustiveDependencies: suppressed due to migration
- const handleUnconsume = useCallback(() => {
- const payload = {
- materialId: material?.id!,
- parentTrackedEntityId: parentId,
- children: [
- {
- trackedEntityId: unconsumedBatch,
- quantity:
- trackedInputs.find((input) => input.id === unconsumedBatch)
- ?.quantity ?? 0
- }
- ]
- };
-
- fetcher.submit(JSON.stringify(payload), {
- method: "post",
- action: path.to.unconsume,
- encType: "application/json"
- });
- // fetcher is not needed to be in the dependency array
- }, [unconsumedBatch, material?.id, parentId, trackedInputs]);
-
- const [items] = useItems();
- const numberFormatter = useNumberFormatter({
- maximumFractionDigits: 4
- });
-
- const convertDisclosure = useDisclosure();
- const scrapDisclosure = useDisclosure();
- const [trackedEntity, setTrackedEntity] = useState<string | null>(null);
-
- return (
- <>
- <Modal open onOpenChange={onClose}>
- <ModalContent>
- <ModalHeader>
- <ModalTitle>{material?.description}</ModalTitle>
- </ModalHeader>
- <ModalBody>
- {splitEntitiesResult.length > 0 ? (
- <Alert variant="default" className="mb-4">
- <LuGitBranch className="mr-2" />
- <AlertTitle>Batch Split Occurred</AlertTitle>
- <AlertDescription>
- <div className="flex flex-col gap-2">
- <p>A new batch entity was created from a split:</p>
- <ul className="list-disc list-inside space-y-1">
- {splitEntitiesResult.map((split) => (
- <li key={split.newId} className="flex flex-col text-sm">
- <span className="text-md font-semibold">
- {split.readableId ??
- getItemReadableId(items, material?.itemId) ??
- "Material"}
- </span>
- <div className="flex gap-2 items-center">
- <span className="font-mono flex gap-1 items-center">
- <LuQrCode />
- {split.newId}
- </span>
- <span className="font-mono text-xs text-muted-foreground flex gap-1 items-center truncate">
- <LuScale />
- {numberFormatter.format(split.quantity)}
- </span>
- </div>
- <div className="flex gap-2 mt-4">
- <Button
- variant="primary"
- leftIcon={<LuPrinter />}
- onClick={() => {
- window.open(
- window.location.origin +
- path.to.file.trackedEntityLabelPdf(
- split.newId
- ),
- "_blank"
- );
- }}
- >
- Print Label
- </Button>
-
- <Button
- variant="secondary"
- leftIcon={<LuArrowRightLeft />}
- onClick={() => {
- setTrackedEntity(split.newId);
- convertDisclosure.onOpen();
- }}
- >
- Convert
- </Button>
- <Button
- variant="secondary"
- leftIcon={<LuTrash />}
- onClick={() => {
- setTrackedEntity(split.newId);
- scrapDisclosure.onOpen();
- }}
- >
- Scrap
- </Button>
- </div>
- </li>
- ))}
- </ul>
- </div>
- </AlertDescription>
- </Alert>
- ) : (
- <Tabs value={activeTab} onValueChange={setActiveTab}>
- <TabsList
- className={cn(
- "grid w-full grid-cols-2 mb-4",
- trackedInputs.length > 0 && "grid-cols-3"
- )}
- >
- <TabsTrigger value="scan">
- <LuQrCode className="mr-2" />
- Scan
- </TabsTrigger>
- <TabsTrigger value="select">
- <LuList className="mr-2" />
- Select
- </TabsTrigger>
- {trackedInputs.length > 0 && (
- <TabsTrigger value="unconsume">
- <LuUndo2 className="mr-2" />
- Unconsume
- </TabsTrigger>
- )}
- </TabsList>
- <TabsContent value="scan">
- <div className="flex flex-col gap-4">
- {selectedBatchNumbers.map((batchNumber, index) => (
- <div key={index} className="flex flex-col gap-2">
- <div className="flex items-center gap-2">
- <div className="flex-1">
- <InputGroup>
- <Input
- value={batchNumber.id}
- onChange={(e) => {
- const newValue = e.target.value;
- updateBatchNumber({
- ...batchNumber,
- id: newValue
- });
- }}
- onBlur={(e) => {
- validateBatchInput(e.target.value, index);
- }}
- placeholder="Scan batch number"
- />
- <InputRightElement className="pl-2">
- {!errors[index] && batchNumber.id ? (
- <LuCheck className="text-emerald-500" />
- ) : (
- <LuQrCode />
- )}
- </InputRightElement>
- </InputGroup>
- </div>
- <div className="w-24">
- <NumberField
- id={`quantity-${index}`}
- value={batchNumber.quantity}
- onChange={(value) =>
- updateBatchNumber({
- ...batchNumber,
- quantity: value
- })
- }
- minValue={0.01}
- maxValue={
- options.find((o) => o.value === batchNumber.id)
- ?.availableQuantity ?? 999999
- }
- >
- <NumberInputGroup className="relative">
- <NumberInput />
- <NumberInputStepper>
- <NumberIncrementStepper>
- <LuChevronUp size="1em" strokeWidth="3" />
- </NumberIncrementStepper>
- <NumberDecrementStepper>
- <LuChevronDown size="1em" strokeWidth="3" />
- </NumberDecrementStepper>
- </NumberInputStepper>
- </NumberInputGroup>
- </NumberField>
- </div>
- {index > 0 && (
- <IconButton
- aria-label="Remove Batch Number"
- icon={<LuX />}
- variant="ghost"
- onClick={() => removeBatchNumber(index)}
- />
- )}
- </div>
- {errors[index] && (
- <span className="text-xs text-destructive">
- {errors[index]}
- </span>
- )}
- </div>
- ))}
- <div>
- <Button
- type="button"
- variant="secondary"
- leftIcon={<LuCirclePlus />}
- onClick={addBatchNumber}
- >
- Add
- </Button>
- </div>
- </div>
- </TabsContent>
- <TabsContent value="select">
- <div className="flex flex-col gap-4">
- {selectedBatchNumbers.map((batchNumber, index) => (
- <div key={index} className="flex flex-col gap-2">
- <div className="flex items-center gap-2">
- <div className="flex-1">
- <ComboboxBase
- value={batchNumber.id}
- onChange={(value) => {
- updateBatchNumber({
- ...batchNumber,
- id: value
- });
- validateBatchInput(value, index);
- }}
- options={options}
- placeholder="Select batch number"
- />
- </div>
- <div className="w-24">
- <NumberField
- value={batchNumber.quantity}
- onChange={(value) =>
- updateBatchNumber({
- ...batchNumber,
- quantity: value
- })
- }
- minValue={0.01}
- maxValue={
- options.find((o) => o.value === batchNumber.id)
- ?.availableQuantity ?? 999999
- }
- >
- <NumberInputGroup className="relative">
- <NumberInput />
- <NumberInputStepper>
- <NumberIncrementStepper>
- <LuChevronUp size="1em" strokeWidth="3" />
- </NumberIncrementStepper>
- <NumberDecrementStepper>
- <LuChevronDown size="1em" strokeWidth="3" />
- </NumberDecrementStepper>
- </NumberInputStepper>
- </NumberInputGroup>
- </NumberField>
- </div>
- {index > 0 && (
- <IconButton
- aria-label="Remove Batch Number"
- icon={<LuX />}
- variant="ghost"
- onClick={() => removeBatchNumber(index)}
- />
- )}
- </div>
- {errors[index] && (
- <span className="text-xs text-destructive">
- {errors[index]}
- </span>
- )}
- </div>
- ))}
- <div>
- <Button
- type="button"
- variant="secondary"
- leftIcon={<LuCirclePlus />}
- onClick={addBatchNumber}
- >
- Add Batch
- </Button>
- </div>
- </div>
- </TabsContent>
- {trackedInputs.length > 0 && (
- <TabsContent value="unconsume">
- <div className="flex flex-col gap-4">
- <div className="flex gap-2">
- <div className="flex-1">
- <ComboboxBase
- value={unconsumedBatch}
- onChange={(value) => {
- setUnconsumedBatch(value);
- }}
- options={unconsumeOptions}
- placeholder="Select batch to unconsume"
- />
- </div>
- {unconsumedBatch && (
- <div className="w-24">
- <Input
- isReadOnly
- value={
- trackedInputs
- .find((input) => input.id === unconsumedBatch)
- ?.quantity.toString() ?? "0"
- }
- />
- </div>
- )}
- </div>
-
- <div className="h-8" />
- </div>
- </TabsContent>
- )}
- </Tabs>
- )}
- </ModalBody>
- <ModalFooter>
- {splitEntitiesResult.length > 0 ? (
- <Button variant="primary" onClick={onClose}>
- Close
- </Button>
- ) : (
- <>
- <Button variant="secondary" onClick={onClose}>
- Cancel
- </Button>
- {activeTab === "unconsume" ? (
- <Button
- variant="destructive"
- onClick={handleUnconsume}
- isLoading={fetcher.state !== "idle"}
- isDisabled={fetcher.state !== "idle" || !unconsumedBatch}
- >
- Unconsume
- </Button>
- ) : (
- <Button
- variant="primary"
- onClick={handleSubmit}
- isLoading={fetcher.state !== "idle"}
- isDisabled={fetcher.state !== "idle"}
- >
- Issue
- </Button>
- )}
- </>
- )}
- </ModalFooter>
- </ModalContent>
- </Modal>
- {convertDisclosure.isOpen && (
- <ConvertSplitModal
- trackedEntity={trackedEntity}
- itemType={material?.itemType ?? "Part"}
- onCancel={() => {
- convertDisclosure.onClose();
- setTrackedEntity(null);
- }}
- onSuccess={(convertedEntity) => {
- setSplitEntitiesResult((prev) =>
- prev.map((entity) =>
- entity.newId === convertedEntity.trackedEntityId
- ? {
- ...entity,
- readableId: convertedEntity.readableId,
- quantity: convertedEntity.quantity
- }
- : entity
- )
- );
- convertDisclosure.onClose();
- setTrackedEntity(null);
- }}
- />
- )}
- {scrapDisclosure.isOpen && (
- <ScrapSplitModal
- materialId={material?.id!}
- parentTrackedEntityId={parentId}
- trackedEntity={trackedEntity}
- onCancel={() => {
- scrapDisclosure.onClose();
- setTrackedEntity(null);
- }}
- onSuccess={() => {
- scrapDisclosure.onClose();
- setTrackedEntity(null);
- onClose();
- }}
- />
- )}
- </>
- );
-}
-
-function ConvertSplitModal({
- trackedEntity,
- itemType,
- onCancel,
- onSuccess
-}: {
- trackedEntity: string | null;
- itemType: string | null;
- onCancel: () => void;
- onSuccess: (convertedEntity: {
- trackedEntityId: string;
- readableId: string;
- quantity: number;
- }) => void;
-}) {
- const fetcher = useFetcher<{
- success: boolean;
- message: string;
- convertedEntity?: {
- trackedEntityId: string;
- readableId: string;
- quantity: number;
- };
- }>();
-
- useEffect(() => {
- if (fetcher.data?.success && fetcher.data.convertedEntity) {
- toast.success("Entity converted successfully");
- onSuccess(fetcher.data.convertedEntity);
- } else if (fetcher.data?.success === false) {
- toast.error(fetcher.data.message || "Failed to convert entity");
- }
- }, [fetcher.data, onSuccess]);
-
- if (!trackedEntity) return null;
- return (
- <Modal open onOpenChange={onCancel}>
- <ModalContent>
- <ModalHeader>
- <ModalTitle>
- Convert to New {itemType === "Material" ? "Size" : "Revision"}
- </ModalTitle>
- <ModalDescription>
- Convert this tracked entity into a quantity of 1 of a new size.
- </ModalDescription>
- </ModalHeader>
- <ValidatedForm
- method="post"
- action={path.to.convertEntity(trackedEntity)}
- defaultValues={{
- trackedEntityId: trackedEntity,
- newRevision: "",
- quantity: 1
- }}
- validator={convertEntityValidator}
- fetcher={fetcher}
- >
- <Hidden name="trackedEntityId" />
- <ModalBody>
- <div className="flex flex-col gap-4">
- <FormInput
- name="newRevision"
- label={`New ${itemType === "Material" ? "Size" : "Revision"}`}
- autoFocus
- />
- <FormNumberInput
- name="quantity"
- label="Quantity"
- minValue={0.001}
- />
- </div>
- </ModalBody>
- <ModalFooter>
- <Button variant="secondary" onClick={onCancel}>
- Cancel
- </Button>
- <Button
- isLoading={fetcher.state !== "idle"}
- isDisabled={fetcher.state !== "idle"}
- type="submit"
- variant="primary"
- >
- Convert
- </Button>
- </ModalFooter>
- </ValidatedForm>
- </ModalContent>
- </Modal>
- );
-}
-
-function ScrapSplitModal({
- materialId,
- parentTrackedEntityId,
- trackedEntity,
- onCancel,
- onSuccess
-}: {
- materialId: string;
- parentTrackedEntityId: string;
- trackedEntity: string | null;
- onCancel: () => void;
- onSuccess: () => void;
-}) {
- const fetcher = useFetcher<{ success: boolean; message: string }>();
-
- useEffect(() => {
- if (fetcher.data?.success) {
- onSuccess();
- }
- }, [fetcher.data?.success, onSuccess]);
-
- if (!trackedEntity) return null;
-
- return (
- <Modal open onOpenChange={onCancel}>
- <ModalContent>
- <ModalHeader>
- <ModalTitle>Are you sure you want to scrap this batch?</ModalTitle>
- <ModalDescription>
- The remaining quantity will be removed from inventory and issued to
- the job
- </ModalDescription>
- </ModalHeader>
- <ModalFooter>
- <Button variant="secondary" onClick={onCancel}>
- Cancel
- </Button>
- <fetcher.Form
- method="post"
- action={path.to.scrapEntity(
- materialId,
- trackedEntity,
- parentTrackedEntityId
- )}
- >
- <Button
- isLoading={fetcher.state !== "idle"}
- isDisabled={fetcher.state !== "idle"}
- type="submit"
- variant="destructive"
- >
- Scrap
- </Button>
- </fetcher.Form>
- </ModalFooter>
- </ModalContent>
- </Modal>
- );
-}
-
-function useBatchNumbers(itemId?: string) {
- const batchNumbersFetcher =
- useFetcher<Awaited<ReturnType<typeof getBatchNumbersForItem>>>();
-
- // biome-ignore lint/correctness/useExhaustiveDependencies: suppressed due to migration
- useEffect(() => {
- if (itemId) {
- batchNumbersFetcher.load(path.to.api.batchNumbers(itemId));
- }
- }, [itemId]);
-
- return { data: batchNumbersFetcher.data };
-}
diff --git a/apps/mes/app/components/JobOperation/components/IssueMaterialModal.tsx b/apps/mes/app/components/JobOperation/components/IssueMaterialModal.tsx
new file mode 100644
index 0000000000..fb1e53f9c4
--- /dev/null
+++ b/apps/mes/app/components/JobOperation/components/IssueMaterialModal.tsx
@@ -0,0 +1,1712 @@
+import { useCarbon } from "@carbon/auth";
+import {
+ Input as FormInput,
+ Number as FormNumberInput,
+ Hidden,
+ ValidatedForm
+} from "@carbon/form";
+import {
+ Alert,
+ AlertDescription,
+ AlertTitle,
+ Button,
+ Checkbox,
+ Combobox as ComboboxBase,
+ cn,
+ IconButton,
+ Input,
+ InputGroup,
+ InputRightElement,
+ Modal,
+ ModalBody,
+ ModalContent,
+ ModalDescription,
+ ModalFooter,
+ ModalHeader,
+ ModalTitle,
+ NumberDecrementStepper,
+ NumberField,
+ NumberIncrementStepper,
+ NumberInput,
+ NumberInputGroup,
+ NumberInputStepper,
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+ Tabs,
+ TabsContent,
+ TabsList,
+ TabsTrigger,
+ toast,
+ useDisclosure
+} from "@carbon/react";
+
+import { getItemReadableId } from "@carbon/utils";
+import { useNumberFormatter } from "@react-aria/i18n";
+import { useCallback, useEffect, useMemo, useRef, useState } from "react";
+import {
+ LuArrowRightLeft,
+ LuCheck,
+ LuChevronDown,
+ LuChevronUp,
+ LuCirclePlus,
+ LuGitBranch,
+ LuList,
+ LuPrinter,
+ LuQrCode,
+ LuScale,
+ LuTrash,
+ LuUndo2,
+ LuX
+} from "react-icons/lu";
+import { useFetcher } from "react-router";
+import type {
+ getBatchNumbersForItem,
+ getSerialNumbersForItem
+} from "~/services/inventory.service";
+import { convertEntityValidator, issueValidator } from "~/services/models";
+import type { JobMaterial, TrackedInput } from "~/services/types";
+import { useItems } from "~/stores";
+import { path } from "~/utils/path";
+
+type TrackingType = "Serial" | "Batch" | "Inventory" | null;
+
+interface ItemDetails {
+ id: string;
+ name: string;
+ unitOfMeasureCode: string;
+ itemTrackingType: TrackingType;
+}
+
+export function IssueMaterialModal({
+ operationId,
+ material,
+ parentId,
+ parentIdIsSerialized,
+ trackedInputs = [],
+ onClose
+}: {
+ operationId: string;
+ material?: JobMaterial;
+ parentId?: string;
+ parentIdIsSerialized?: boolean;
+ trackedInputs?: TrackedInput[];
+ onClose: () => void;
+}) {
+ const { carbon } = useCarbon();
+ const [items] = useItems();
+ const numberFormatter = useNumberFormatter({ maximumFractionDigits: 4 });
+
+ // Item selection state
+ const [selectedItemId, setSelectedItemId] = useState<string>(
+ material?.itemId ?? ""
+ );
+ const [itemDetails, setItemDetails] = useState<ItemDetails | null>(null);
+ const [isLoadingItem, setIsLoadingItem] = useState(false);
+
+ // Determine tracking type from material or item details
+ const trackingType: TrackingType = useMemo(() => {
+ if (material) {
+ if (material.requiresSerialTracking) return "Serial";
+ if (material.requiresBatchTracking) return "Batch";
+ return "Inventory";
+ }
+ return itemDetails?.itemTrackingType ?? null;
+ }, [material, itemDetails]);
+
+ // Item options for the combobox
+ const itemOptions = useMemo(() => {
+ return items.map((item) => ({
+ label: item.readableIdWithRevision,
+ helper: item.name,
+ value: item.id
+ }));
+ }, [items]);
+
+ // Serial number state and options
+ const { data: serialNumbers } = useSerialNumbers(
+ trackingType === "Serial" ? selectedItemId : undefined
+ );
+ const serialOptions = useMemo(() => {
+ return (
+ serialNumbers?.data?.map((sn) => ({
+ label: sn.id ?? "",
+ value: sn.id,
+ helper: sn.readableId ? `Serial ${sn.readableId}` : undefined
+ })) ?? []
+ );
+ }, [serialNumbers]);
+
+ // Batch number state and options
+ const { data: batchNumbers } = useBatchNumbers(
+ trackingType === "Batch" ? selectedItemId : undefined
+ );
+ const batchOptions = useMemo(() => {
+ return (
+ batchNumbers?.data
+ ?.filter((bn) => bn.status === "Available")
+ .map((bn) => {
+ return {
+ label: bn.sourceDocumentReadableId ?? "",
+ value: bn.id,
+ helper: bn.readableId
+ ? `${bn.id.slice(0, 10)} - ${bn.quantity} Available of Batch ${bn.readableId}`
+ : `${bn.id.slice(0, 10)} - ${bn.quantity} Available`,
+ availableQuantity: bn.quantity
+ };
+ }) ?? []
+ );
+ }, [batchNumbers]);
+
+ // Unconsume options for batch
+ const unconsumeOptions = useMemo(() => {
+ return trackedInputs.map((input) => ({
+ label: input.id,
+ value: input.id,
+ helper: `${input.quantity} ${input.readableId ? `of Batch ${input.readableId}` : ""}`
+ }));
+ }, [trackedInputs]);
+
+ // Quantity for inventory items
+ const initialQuantity = useMemo(() => {
+ if (!material) return 1;
+ return parentIdIsSerialized
+ ? (material.quantity ?? material.estimatedQuantity ?? 1)
+ : (material.estimatedQuantity ?? material.quantity ?? 1);
+ }, [material, parentIdIsSerialized]);
+
+ const [quantity, setQuantity] = useState(initialQuantity);
+
+ // Serial numbers selection state
+ const [selectedSerialNumbers, setSelectedSerialNumbers] = useState<
+ Array<{ index: number; id: string }>
+ >(
+ Array(Math.max(1, initialQuantity))
+ .fill("")
+ .map((_, index) => ({ index, id: "" }))
+ );
+ const [serialErrors, setSerialErrors] = useState<Record<number, string>>({});
+ const [selectedTrackedInputs, setSelectedTrackedInputs] = useState<string[]>(
+ []
+ );
+
+ // Batch numbers selection state
+ const [selectedBatchNumbers, setSelectedBatchNumbers] = useState<
+ Array<{ index: number; id: string; quantity: number }>
+ >([{ index: 0, id: "", quantity: initialQuantity }]);
+ const [batchErrors, setBatchErrors] = useState<Record<number, string>>({});
+ const [unconsumedBatch, setUnconsumedBatch] = useState("");
+
+ // Tab state
+ const [activeTab, setActiveTab] = useState("scan");
+
+ // Split entities result state (for batch splitting)
+ const [splitEntitiesResult, setSplitEntitiesResult] = useState<
+ {
+ newId: string;
+ originalId: string;
+ quantity: number;
+ readableId?: string;
+ }[]
+ >([]);
+
+ // Fetchers
+ const fetcher = useFetcher<{
+ success: boolean;
+ message: string;
+ splitEntities?: Array<{
+ originalId: string;
+ newId: string;
+ quantity: number;
+ readableId?: string;
+ }>;
+ }>();
+ const unconsumeFetcher = useFetcher<{ success: boolean; message: string }>();
+ const inventoryFetcher = useFetcher<{ success: boolean; message: string }>();
+
+ // Sub-modals for batch splitting
+ const convertDisclosure = useDisclosure();
+ const scrapDisclosure = useDisclosure();
+ const [trackedEntity, setTrackedEntity] = useState<string | null>(null);
+
+ // Fetch item details when item is selected (only when no material provided)
+ const handleItemChange = useCallback(
+ async (itemId: string) => {
+ setSelectedItemId(itemId);
+ setItemDetails(null);
+ setQuantity(1);
+ setSelectedSerialNumbers([{ index: 0, id: "" }]);
+ setSelectedBatchNumbers([{ index: 0, id: "", quantity: 1 }]);
+ setSerialErrors({});
+ setBatchErrors({});
+
+ if (itemId && carbon && !material) {
+ setIsLoadingItem(true);
+ const { data } = await carbon
+ .from("item")
+ .select("id, name, unitOfMeasureCode, itemTrackingType")
+ .eq("id", itemId)
+ .single();
+
+ if (data) {
+ setItemDetails(data as ItemDetails);
+ }
+ setIsLoadingItem(false);
+ }
+ },
+ [carbon, material]
+ );
+
+ // Validation functions
+ const validateSerialNumber = useCallback(
+ (value: string, index: number) => {
+ if (!value) return "Serial number is required";
+ const isDuplicate = selectedSerialNumbers.some(
+ (sn, i) => sn.id === value && i !== index
+ );
+ if (isDuplicate) return "Duplicate serial number";
+ const isValid = serialOptions.some((opt) => opt.value === value);
+ if (!isValid) {
+ const sn = serialNumbers?.data?.find((s) => s.id === value);
+ if (sn) return `Serial number is ${sn.status}`;
+ return "Serial number is not available";
+ }
+ return null;
+ },
+ [selectedSerialNumbers, serialOptions, serialNumbers?.data]
+ );
+
+ const validateBatchNumber = useCallback(
+ (value: string, qty: number, index: number) => {
+ if (!value) return "Batch number is required";
+ const isDuplicate = selectedBatchNumbers.some(
+ (bn, i) => bn.id === value && i !== index
+ );
+ if (isDuplicate) return "Duplicate batch number";
+ const batchOption = batchOptions.find((opt) => opt.value === value);
+ if (!batchOption) {
+ const bn = batchNumbers?.data?.find((b) => b.id === value);
+ if (bn) return `Batch number is ${bn.status}`;
+ return "Batch number is not available";
+ }
+ if (qty <= 0) return "Quantity must be greater than 0";
+ if (qty > batchOption.availableQuantity)
+ return `Quantity cannot exceed available quantity (${batchOption.availableQuantity})`;
+ return null;
+ },
+ [selectedBatchNumbers, batchOptions, batchNumbers?.data]
+ );
+
+ // Update functions for serial numbers
+ const updateSerialNumber = useCallback(
+ (serialNumber: { index: number; id: string }) => {
+ setSelectedSerialNumbers((prev) => {
+ const newSerialNumbers = [...prev];
+ newSerialNumbers[serialNumber.index] = serialNumber;
+ return newSerialNumbers;
+ });
+ },
+ []
+ );
+
+ const addSerialNumber = useCallback(() => {
+ setSelectedSerialNumbers((prev) => {
+ const newIndex = prev.length;
+ return [...prev, { index: newIndex, id: "" }];
+ });
+ }, []);
+
+ const removeSerialNumber = useCallback((indexToRemove: number) => {
+ setSelectedSerialNumbers((prev) => {
+ const filtered = prev.filter((_, i) => i !== indexToRemove);
+ return filtered.map((item, i) => ({ ...item, index: i }));
+ });
+ setSerialErrors((prev) => {
+ const newErrors = { ...prev };
+ delete newErrors[indexToRemove];
+ const reindexedErrors: Record<number, string> = {};
+ Object.entries(newErrors).forEach(([key, value]) => {
+ const keyNum = parseInt(key);
+ if (keyNum > indexToRemove) {
+ reindexedErrors[keyNum - 1] = value;
+ } else {
+ reindexedErrors[keyNum] = value;
+ }
+ });
+ return reindexedErrors;
+ });
+ }, []);
+
+ // Update functions for batch numbers
+ const updateBatchNumber = useCallback(
+ (batchNumber: { index: number; id: string; quantity: number }) => {
+ setSelectedBatchNumbers((prev) => {
+ const newBatchNumbers = [...prev];
+ newBatchNumbers[batchNumber.index] = batchNumber;
+ return newBatchNumbers;
+ });
+ },
+ []
+ );
+
+ const addBatchNumber = useCallback(() => {
+ setSelectedBatchNumbers((prev) => {
+ const newIndex = prev.length;
+ return [...prev, { index: newIndex, id: "", quantity: 1 }];
+ });
+ }, []);
+
+ const removeBatchNumber = useCallback((indexToRemove: number) => {
+ setSelectedBatchNumbers((prev) => {
+ const filtered = prev.filter((_, i) => i !== indexToRemove);
+ return filtered.map((item, i) => ({ ...item, index: i }));
+ });
+ setBatchErrors((prev) => {
+ const newErrors = { ...prev };
+ delete newErrors[indexToRemove];
+ const reindexedErrors: Record<number, string> = {};
+ Object.entries(newErrors).forEach(([key, value]) => {
+ const keyNum = parseInt(key);
+ if (keyNum > indexToRemove) {
+ reindexedErrors[keyNum - 1] = value;
+ } else {
+ reindexedErrors[keyNum] = value;
+ }
+ });
+ return reindexedErrors;
+ });
+ }, []);
+
+ const validateBatchInput = useCallback(
+ (value: string, index: number) => {
+ if (!value) {
+ setBatchErrors((prev) => ({
+ ...prev,
+ [index]: "Batch number is required"
+ }));
+ return false;
+ }
+
+ const duplicateIndices = selectedBatchNumbers
+ .map((bn, i) => (bn.id === value && i !== index ? i : -1))
+ .filter((i) => i !== -1);
+
+ if (duplicateIndices.length > 0) {
+ setBatchErrors((prev) => ({
+ ...prev,
+ [index]: "Duplicate batch number"
+ }));
+ return false;
+ }
+
+ const batchOption = batchOptions.find((opt) => opt.value === value);
+ if (!batchOption) {
+ setBatchErrors((prev) => ({
+ ...prev,
+ [index]: "Batch number is not available"
+ }));
+ return false;
+ }
+
+ const currentBatchNumber = selectedBatchNumbers[index];
+ if (currentBatchNumber.quantity > batchOption.availableQuantity) {
+ const remainingQuantity =
+ currentBatchNumber.quantity - batchOption.availableQuantity;
+
+ updateBatchNumber({
+ ...currentBatchNumber,
+ id: value,
+ quantity: batchOption.availableQuantity
+ });
+
+ setSelectedBatchNumbers((prev) => {
+ const newIndex = prev.length;
+ return [
+ ...prev,
+ { index: newIndex, id: "", quantity: remainingQuantity }
+ ];
+ });
+ }
+
+ setBatchErrors((prev) => {
+ const newErrors = { ...prev };
+ delete newErrors[index];
+ return newErrors;
+ });
+ return true;
+ },
+ [selectedBatchNumbers, batchOptions, updateBatchNumber]
+ );
+
+ const toggleTrackedInput = useCallback((id: string) => {
+ setSelectedTrackedInputs((prev) => {
+ if (prev.includes(id)) {
+ return prev.filter((item) => item !== id);
+ }
+ return [...prev, id];
+ });
+ }, []);
+
+ // Submit handlers
+ const handleSubmitSerial = useCallback(() => {
+ if (!parentId) {
+ toast.error("Parent tracking ID is required for serial tracked items.");
+ return;
+ }
+
+ // Either material.id or (operationId + selectedItemId) must be provided
+ if (!material?.id && !selectedItemId) {
+ toast.error("Please select an item to issue.");
+ return;
+ }
+
+ let hasErrors = false;
+ const newErrors: Record<number, string> = {};
+
+ selectedSerialNumbers.forEach((sn) => {
+ const error = validateSerialNumber(sn.id, sn.index);
+ if (error) {
+ newErrors[sn.index] = error;
+ hasErrors = true;
+ }
+ });
+
+ setSerialErrors(newErrors);
+
+ if (!hasErrors) {
+ const payload = material?.id
+ ? {
+ materialId: material.id,
+ parentTrackedEntityId: parentId,
+ children: selectedSerialNumbers.map((sn) => ({
+ trackedEntityId: sn.id,
+ quantity: 1
+ }))
+ }
+ : {
+ jobOperationId: operationId,
+ itemId: selectedItemId,
+ parentTrackedEntityId: parentId,
+ children: selectedSerialNumbers.map((sn) => ({
+ trackedEntityId: sn.id,
+ quantity: 1
+ }))
+ };
+
+ fetcher.submit(JSON.stringify(payload), {
+ method: "post",
+ action: path.to.issueTrackedEntity,
+ encType: "application/json"
+ });
+ }
+ }, [
+ selectedSerialNumbers,
+ validateSerialNumber,
+ parentId,
+ material?.id,
+ operationId,
+ selectedItemId,
+ fetcher
+ ]);
+
+ const handleSubmitBatch = useCallback(() => {
+ if (!parentId) {
+ toast.error("Parent tracking ID is required for batch tracked items.");
+ return;
+ }
+
+ // Either material.id or (operationId + selectedItemId) must be provided
+ if (!material?.id && !selectedItemId) {
+ toast.error("Please select an item to issue.");
+ return;
+ }
+
+ let hasErrors = false;
+ const newErrors: Record<number, string> = {};
+
+ selectedBatchNumbers.forEach((bn) => {
+ const error = validateBatchNumber(bn.id, bn.quantity, bn.index);
+ if (error) {
+ newErrors[bn.index] = error;
+ hasErrors = true;
+ }
+ });
+
+ setBatchErrors(newErrors);
+
+ if (!hasErrors) {
+ const payload = material?.id
+ ? {
+ materialId: material.id,
+ parentTrackedEntityId: parentId,
+ children: selectedBatchNumbers.map((bn) => ({
+ trackedEntityId: bn.id,
+ quantity: bn.quantity
+ }))
+ }
+ : {
+ jobOperationId: operationId,
+ itemId: selectedItemId,
+ parentTrackedEntityId: parentId,
+ children: selectedBatchNumbers.map((bn) => ({
+ trackedEntityId: bn.id,
+ quantity: bn.quantity
+ }))
+ };
+
+ fetcher.submit(JSON.stringify(payload), {
+ method: "post",
+ action: path.to.issueTrackedEntity,
+ encType: "application/json"
+ });
+ }
+ }, [
+ selectedBatchNumbers,
+ validateBatchNumber,
+ parentId,
+ material?.id,
+ operationId,
+ selectedItemId,
+ fetcher
+ ]);
+
+ const handleUnconsumeSerial = useCallback(() => {
+ if (selectedTrackedInputs.length === 0) {
+ toast.error("Please select at least one item to unconsume");
+ return;
+ }
+
+ if (!material?.id || !parentId) {
+ toast.error("Material and parent ID are required to unconsume");
+ return;
+ }
+
+ const payload = {
+ materialId: material.id,
+ parentTrackedEntityId: parentId,
+ children: selectedTrackedInputs.map((id) => ({
+ trackedEntityId: id,
+ quantity: 1
+ }))
+ };
+
+ unconsumeFetcher.submit(JSON.stringify(payload), {
+ method: "post",
+ action: path.to.unconsume,
+ encType: "application/json"
+ });
+ }, [selectedTrackedInputs, material?.id, parentId, unconsumeFetcher]);
+
+ const handleUnconsumeBatch = useCallback(() => {
+ if (!unconsumedBatch) {
+ toast.error("Please select a batch to unconsume");
+ return;
+ }
+
+ if (!material?.id || !parentId) {
+ toast.error("Material and parent ID are required to unconsume");
+ return;
+ }
+
+ const payload = {
+ materialId: material.id,
+ parentTrackedEntityId: parentId,
+ children: [
+ {
+ trackedEntityId: unconsumedBatch,
+ quantity:
+ trackedInputs.find((input) => input.id === unconsumedBatch)
+ ?.quantity ?? 0
+ }
+ ]
+ };
+
+ unconsumeFetcher.submit(JSON.stringify(payload), {
+ method: "post",
+ action: path.to.unconsume,
+ encType: "application/json"
+ });
+ }, [
+ unconsumedBatch,
+ material?.id,
+ parentId,
+ trackedInputs,
+ unconsumeFetcher
+ ]);
+
+ // Handle fetcher responses
+ const processedFetcherData = useRef<typeof fetcher.data | null>(null);
+
+ useEffect(() => {
+ if (
+ fetcher.state === "idle" &&
+ fetcher.data &&
+ fetcher.data !== processedFetcherData.current
+ ) {
+ processedFetcherData.current = fetcher.data;
+
+ if (fetcher.data.success) {
+ if (
+ fetcher.data.splitEntities &&
+ fetcher.data.splitEntities.length > 0
+ ) {
+ setSplitEntitiesResult(
+ fetcher.data.splitEntities.map((entity) => ({
+ newId: entity.newId,
+ originalId: entity.originalId,
+ readableId: entity.readableId,
+ quantity: entity.quantity
+ }))
+ );
+ toast.success(fetcher.data.message);
+ } else {
+ onClose();
+ if (fetcher.data.message) {
+ toast.success(fetcher.data.message);
+ }
+ }
+ } else if (fetcher.data.message) {
+ toast.error(fetcher.data.message);
+ }
+ }
+ }, [fetcher.state, fetcher.data, onClose]);
+
+ useEffect(() => {
+ if (unconsumeFetcher.data?.success) {
+ onClose();
+ if (unconsumeFetcher.data.message) {
+ toast.success(unconsumeFetcher.data.message);
+ }
+ } else if (unconsumeFetcher.data?.message) {
+ toast.error(unconsumeFetcher.data.message);
+ }
+ }, [unconsumeFetcher.data, onClose]);
+
+ useEffect(() => {
+ if (inventoryFetcher.data?.success) {
+ onClose();
+ }
+ }, [inventoryFetcher.data, onClose]);
+
+ // Determine what to render based on state
+ const showItemSelector = !material?.itemId;
+ const showContent = material?.itemId || itemDetails;
+
+ const hasTrackedInputs = trackedInputs.length > 0;
+
+ return (
+ <>
+ <Modal open onOpenChange={onClose}>
+ <ModalContent>
+ <ModalHeader>
+ <ModalTitle>
+ {material?.description ??
+ getItemReadableId(items, selectedItemId) ??
+ "Issue Material"}
+ </ModalTitle>
+ {!material && (
+ <ModalDescription>
+ Select an item and specify the quantity to issue
+ </ModalDescription>
+ )}
+ </ModalHeader>
+
+ {splitEntitiesResult.length > 0 ? (
+ // Show split entities result
+ <ModalBody>
+ <Alert variant="default" className="mb-4">
+ <LuGitBranch className="mr-2" />
+ <AlertTitle>Batch Split Occurred</AlertTitle>
+ <AlertDescription>
+ <div className="flex flex-col gap-2">
+ <p>A new batch entity was created from a split:</p>
+ <ul className="list-disc list-inside space-y-1">
+ {splitEntitiesResult.map((split) => (
+ <li key={split.newId} className="flex flex-col text-sm">
+ <span className="text-md font-semibold">
+ {split.readableId ??
+ getItemReadableId(items, material?.itemId) ??
+ "Material"}
+ </span>
+ <div className="flex gap-2 items-center">
+ <span className="font-mono flex gap-1 items-center">
+ <LuQrCode />
+ {split.newId}
+ </span>
+ <span className="font-mono text-xs text-muted-foreground flex gap-1 items-center truncate">
+ <LuScale />
+ {numberFormatter.format(split.quantity)}
+ </span>
+ </div>
+ <div className="flex gap-2 mt-4">
+ <Button
+ variant="primary"
+ leftIcon={<LuPrinter />}
+ onClick={() => {
+ window.open(
+ window.location.origin +
+ path.to.file.trackedEntityLabelPdf(
+ split.newId
+ ),
+ "_blank"
+ );
+ }}
+ >
+ Print Label
+ </Button>
+ <Button
+ variant="secondary"
+ leftIcon={<LuArrowRightLeft />}
+ onClick={() => {
+ setTrackedEntity(split.newId);
+ convertDisclosure.onOpen();
+ }}
+ >
+ Convert
+ </Button>
+ <Button
+ variant="secondary"
+ leftIcon={<LuTrash />}
+ onClick={() => {
+ setTrackedEntity(split.newId);
+ scrapDisclosure.onOpen();
+ }}
+ >
+ Scrap
+ </Button>
+ </div>
+ </li>
+ ))}
+ </ul>
+ </div>
+ </AlertDescription>
+ </Alert>
+ </ModalBody>
+ ) : trackingType === "Inventory" || trackingType === null ? (
+ // Inventory item - use ValidatedForm
+ <ValidatedForm
+ method="post"
+ action={path.to.issue}
+ onSubmit={onClose}
+ validator={issueValidator}
+ defaultValues={{
+ materialId: material?.id ?? "",
+ jobOperationId: operationId,
+ itemId: selectedItemId,
+ quantity:
+ (material?.estimatedQuantity ?? 0) -
+ (material?.quantityIssued ?? 0),
+ adjustmentType: "Negative Adjmt."
+ }}
+ fetcher={inventoryFetcher}
+ >
+ <ModalBody>
+ <Hidden name="jobOperationId" />
+ <Hidden name="materialId" />
+ {material?.id && (
+ <Hidden name="adjustmentType" value="Negative Adjmt." />
+ )}
+ <div className="flex flex-col gap-4">
+ {showItemSelector && (
+ <div>
+ <label className="block text-sm font-medium mb-1">
+ Item
+ </label>
+ <ComboboxBase
+ placeholder="Select an item..."
+ value={selectedItemId}
+ onChange={(value) => {
+ handleItemChange(value);
+ }}
+ options={itemOptions}
+ />
+ <input
+ type="hidden"
+ name="itemId"
+ value={selectedItemId}
+ />
+ </div>
+ )}
+ {material?.id && (
+ <Hidden name="itemId" value={selectedItemId} />
+ )}
+
+ {isLoadingItem && (
+ <div className="text-sm text-muted-foreground">
+ Loading item details...
+ </div>
+ )}
+
+ {showContent && trackingType === "Inventory" && (
+ <>
+ {!material?.id && (
+ <div>
+ <label className="block text-sm font-medium mb-1">
+ Adjustment Type
+ </label>
+ <Select
+ name="adjustmentType"
+ defaultValue="Negative Adjmt."
+ >
+ <SelectTrigger>
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ <SelectItem value="Positive Adjmt.">
+ Add to Inventory
+ </SelectItem>
+ <SelectItem value="Negative Adjmt.">
+ Pull from Inventory
+ </SelectItem>
+ </SelectContent>
+ </Select>
+ </div>
+ )}
+ <div>
+ <label className="block text-sm font-medium mb-1">
+ Quantity
+ </label>
+
+ <NumberField
+ value={quantity}
+ onChange={setQuantity}
+ minValue={0.01}
+ >
+ <NumberInputGroup className="relative">
+ <NumberInput name="quantity" />
+ <NumberInputStepper>
+ <NumberIncrementStepper>
+ <LuChevronUp size="1em" strokeWidth="3" />
+ </NumberIncrementStepper>
+ <NumberDecrementStepper>
+ <LuChevronDown size="1em" strokeWidth="3" />
+ </NumberDecrementStepper>
+ </NumberInputStepper>
+ </NumberInputGroup>
+ </NumberField>
+ </div>
+ </>
+ )}
+ </div>
+ </ModalBody>
+ <ModalFooter>
+ <Button variant="secondary" onClick={onClose}>
+ Cancel
+ </Button>
+ <Button
+ type="submit"
+ variant="primary"
+ isLoading={inventoryFetcher.state !== "idle"}
+ isDisabled={
+ inventoryFetcher.state !== "idle" ||
+ !selectedItemId ||
+ isLoadingItem
+ }
+ >
+ Issue
+ </Button>
+ </ModalFooter>
+ </ValidatedForm>
+ ) : (
+ // Tracked items (Serial or Batch)
+ <>
+ <ModalBody>
+ <div className="flex flex-col gap-4">
+ {showItemSelector && (
+ <div>
+ <label className="block text-sm font-medium mb-1">
+ Item
+ </label>
+ <ComboboxBase
+ placeholder="Select an item..."
+ value={selectedItemId}
+ onChange={handleItemChange}
+ options={itemOptions}
+ />
+ </div>
+ )}
+
+ {isLoadingItem && (
+ <div className="text-sm text-muted-foreground">
+ Loading item details...
+ </div>
+ )}
+
+ {showContent && trackingType === "Serial" && (
+ <Tabs value={activeTab} onValueChange={setActiveTab}>
+ <TabsList
+ className={cn(
+ "grid w-full grid-cols-2 mb-4",
+ hasTrackedInputs && "grid-cols-3"
+ )}
+ >
+ <TabsTrigger value="scan">
+ <LuQrCode className="mr-2" />
+ Scan
+ </TabsTrigger>
+ <TabsTrigger value="select">
+ <LuList className="mr-2" />
+ Select
+ </TabsTrigger>
+ {hasTrackedInputs && (
+ <TabsTrigger value="unconsume">
+ <LuUndo2 className="mr-2" />
+ Unconsume
+ </TabsTrigger>
+ )}
+ </TabsList>
+
+ <TabsContent value="scan">
+ <div className="flex flex-col gap-4">
+ {selectedSerialNumbers.map((sn, index) => (
+ <div
+ key={`${index}-serial-scan`}
+ className="flex flex-col gap-1"
+ >
+ <div className="flex items-center gap-2">
+ <div className="flex-1">
+ <InputGroup>
+ <Input
+ placeholder={`Serial Number ${index + 1}`}
+ value={sn.id}
+ onChange={(e) => {
+ const newValue = e.target.value;
+ const newSerialNumbers = [
+ ...selectedSerialNumbers
+ ];
+ newSerialNumbers[index] = {
+ index,
+ id: newValue
+ };
+ setSelectedSerialNumbers(
+ newSerialNumbers
+ );
+ }}
+ onBlur={(e) => {
+ const newValue = e.target.value;
+ const error = validateSerialNumber(
+ newValue,
+ index
+ );
+ setSerialErrors((prev) => {
+ const newErrors = { ...prev };
+ if (error) {
+ newErrors[index] = error;
+ } else {
+ delete newErrors[index];
+ }
+ return newErrors;
+ });
+ if (!error) {
+ updateSerialNumber({
+ index,
+ id: newValue
+ });
+ } else {
+ const newSerialNumbers = [
+ ...selectedSerialNumbers
+ ];
+ newSerialNumbers[index] = {
+ index,
+ id: ""
+ };
+ setSelectedSerialNumbers(
+ newSerialNumbers
+ );
+ }
+ }}
+ className={cn(
+ serialErrors[index] &&
+ "border-destructive"
+ )}
+ />
+ <InputRightElement className="pl-2">
+ {!serialErrors[index] && sn.id ? (
+ <LuCheck className="text-emerald-500" />
+ ) : (
+ <LuQrCode />
+ )}
+ </InputRightElement>
+ </InputGroup>
+ </div>
+ {index > 0 && (
+ <IconButton
+ aria-label="Remove Serial Number"
+ icon={<LuX />}
+ variant="ghost"
+ onClick={() => removeSerialNumber(index)}
+ className="flex-shrink-0"
+ />
+ )}
+ </div>
+ {serialErrors[index] && (
+ <span className="text-xs text-destructive">
+ {serialErrors[index]}
+ </span>
+ )}
+ </div>
+ ))}
+ <div>
+ <Button
+ type="button"
+ variant="secondary"
+ leftIcon={<LuCirclePlus />}
+ onClick={addSerialNumber}
+ >
+ Add
+ </Button>
+ </div>
+ </div>
+ </TabsContent>
+
+ <TabsContent value="select">
+ <div className="flex flex-col gap-4">
+ {selectedSerialNumbers.map((sn, index) => (
+ <div
+ key={`${index}-serial-select`}
+ className="flex flex-col gap-1"
+ >
+ <div className="flex items-center gap-2">
+ <div className="flex-1">
+ <ComboboxBase
+ placeholder={`Select Serial Number ${index + 1}`}
+ value={sn.id}
+ onChange={(value) => {
+ const newSerialNumbers = [
+ ...selectedSerialNumbers
+ ];
+ newSerialNumbers[index] = {
+ index,
+ id: value
+ };
+ setSelectedSerialNumbers(
+ newSerialNumbers
+ );
+ const error = validateSerialNumber(
+ value,
+ index
+ );
+ setSerialErrors((prev) => {
+ const newErrors = { ...prev };
+ if (error) {
+ newErrors[index] = error;
+ } else {
+ delete newErrors[index];
+ }
+ return newErrors;
+ });
+ }}
+ options={serialOptions}
+ />
+ </div>
+ {index > 0 && (
+ <IconButton
+ aria-label="Remove Serial Number"
+ icon={<LuX />}
+ variant="ghost"
+ onClick={() => removeSerialNumber(index)}
+ className="flex-shrink-0"
+ />
+ )}
+ </div>
+ {serialErrors[index] && (
+ <span className="text-xs text-destructive">
+ {serialErrors[index]}
+ </span>
+ )}
+ </div>
+ ))}
+ <div>
+ <Button
+ type="button"
+ variant="secondary"
+ leftIcon={<LuCirclePlus />}
+ onClick={addSerialNumber}
+ >
+ Add
+ </Button>
+ </div>
+ </div>
+ </TabsContent>
+
+ {hasTrackedInputs && (
+ <TabsContent value="unconsume">
+ <div className="flex flex-col gap-4">
+ {trackedInputs.map((input) => (
+ <div
+ key={input.id}
+ className="flex items-center gap-3 p-2 border rounded-md"
+ >
+ <Checkbox
+ id={`unconsume-${input.id}`}
+ checked={selectedTrackedInputs.includes(
+ input.id
+ )}
+ onCheckedChange={() =>
+ toggleTrackedInput(input.id)
+ }
+ />
+ <label
+ htmlFor={`unconsume-${input.id}`}
+ className="flex-1 cursor-pointer"
+ >
+ <div className="font-medium text-sm">
+ {input.id}
+ </div>
+ {input.readableId && (
+ <div className="text-xs text-muted-foreground">
+ Serial: {input.readableId}
+ </div>
+ )}
+ </label>
+ </div>
+ ))}
+ {trackedInputs.length === 0 && (
+ <Alert variant="warning">
+ <AlertTitle>No consumed materials</AlertTitle>
+ <AlertDescription>
+ There are no consumed materials to unconsume.
+ </AlertDescription>
+ </Alert>
+ )}
+ </div>
+ </TabsContent>
+ )}
+ </Tabs>
+ )}
+
+ {showContent && trackingType === "Batch" && (
+ <Tabs value={activeTab} onValueChange={setActiveTab}>
+ <TabsList
+ className={cn(
+ "grid w-full grid-cols-2 mb-4",
+ hasTrackedInputs && "grid-cols-3"
+ )}
+ >
+ <TabsTrigger value="scan">
+ <LuQrCode className="mr-2" />
+ Scan
+ </TabsTrigger>
+ <TabsTrigger value="select">
+ <LuList className="mr-2" />
+ Select
+ </TabsTrigger>
+ {hasTrackedInputs && (
+ <TabsTrigger value="unconsume">
+ <LuUndo2 className="mr-2" />
+ Unconsume
+ </TabsTrigger>
+ )}
+ </TabsList>
+
+ <TabsContent value="scan">
+ <div className="flex flex-col gap-4">
+ {selectedBatchNumbers.map((batch, index) => (
+ <div key={index} className="flex flex-col gap-2">
+ <div className="flex items-center gap-2">
+ <div className="flex-1">
+ <InputGroup>
+ <Input
+ value={batch.id}
+ onChange={(e) => {
+ const newValue = e.target.value;
+ updateBatchNumber({
+ ...batch,
+ id: newValue
+ });
+ }}
+ onBlur={(e) => {
+ validateBatchInput(
+ e.target.value,
+ index
+ );
+ }}
+ placeholder="Scan batch number"
+ />
+ <InputRightElement className="pl-2">
+ {!batchErrors[index] && batch.id ? (
+ <LuCheck className="text-emerald-500" />
+ ) : (
+ <LuQrCode />
+ )}
+ </InputRightElement>
+ </InputGroup>
+ </div>
+ <div className="w-24">
+ <NumberField
+ id={`quantity-${index}`}
+ value={batch.quantity}
+ onChange={(value) =>
+ updateBatchNumber({
+ ...batch,
+ quantity: value
+ })
+ }
+ minValue={0.01}
+ maxValue={
+ batchOptions.find(
+ (o) => o.value === batch.id
+ )?.availableQuantity ?? 999999
+ }
+ >
+ <NumberInputGroup className="relative">
+ <NumberInput />
+ <NumberInputStepper>
+ <NumberIncrementStepper>
+ <LuChevronUp
+ size="1em"
+ strokeWidth="3"
+ />
+ </NumberIncrementStepper>
+ <NumberDecrementStepper>
+ <LuChevronDown
+ size="1em"
+ strokeWidth="3"
+ />
+ </NumberDecrementStepper>
+ </NumberInputStepper>
+ </NumberInputGroup>
+ </NumberField>
+ </div>
+ {index > 0 && (
+ <IconButton
+ aria-label="Remove Batch Number"
+ icon={<LuX />}
+ variant="ghost"
+ onClick={() => removeBatchNumber(index)}
+ />
+ )}
+ </div>
+ {batchErrors[index] && (
+ <span className="text-xs text-destructive">
+ {batchErrors[index]}
+ </span>
+ )}
+ </div>
+ ))}
+ <div>
+ <Button
+ type="button"
+ variant="secondary"
+ leftIcon={<LuCirclePlus />}
+ onClick={addBatchNumber}
+ >
+ Add
+ </Button>
+ </div>
+ </div>
+ </TabsContent>
+
+ <TabsContent value="select">
+ <div className="flex flex-col gap-4">
+ {selectedBatchNumbers.map((batch, index) => (
+ <div key={index} className="flex flex-col gap-2">
+ <div className="flex items-center gap-2">
+ <div className="flex-1">
+ <ComboboxBase
+ value={batch.id}
+ onChange={(value) => {
+ updateBatchNumber({
+ ...batch,
+ id: value
+ });
+ validateBatchInput(value, index);
+ }}
+ options={batchOptions}
+ placeholder="Select batch number"
+ />
+ </div>
+ <div className="w-24">
+ <NumberField
+ value={batch.quantity}
+ onChange={(value) =>
+ updateBatchNumber({
+ ...batch,
+ quantity: value
+ })
+ }
+ minValue={0.01}
+ maxValue={
+ batchOptions.find(
+ (o) => o.value === batch.id
+ )?.availableQuantity ?? 999999
+ }
+ >
+ <NumberInputGroup className="relative">
+ <NumberInput />
+ <NumberInputStepper>
+ <NumberIncrementStepper>
+ <LuChevronUp
+ size="1em"
+ strokeWidth="3"
+ />
+ </NumberIncrementStepper>
+ <NumberDecrementStepper>
+ <LuChevronDown
+ size="1em"
+ strokeWidth="3"
+ />
+ </NumberDecrementStepper>
+ </NumberInputStepper>
+ </NumberInputGroup>
+ </NumberField>
+ </div>
+ {index > 0 && (
+ <IconButton
+ aria-label="Remove Batch Number"
+ icon={<LuX />}
+ variant="ghost"
+ onClick={() => removeBatchNumber(index)}
+ />
+ )}
+ </div>
+ {batchErrors[index] && (
+ <span className="text-xs text-destructive">
+ {batchErrors[index]}
+ </span>
+ )}
+ </div>
+ ))}
+ <div>
+ <Button
+ type="button"
+ variant="secondary"
+ leftIcon={<LuCirclePlus />}
+ onClick={addBatchNumber}
+ >
+ Add Batch
+ </Button>
+ </div>
+ </div>
+ </TabsContent>
+
+ {hasTrackedInputs && (
+ <TabsContent value="unconsume">
+ <div className="flex flex-col gap-4">
+ <div className="flex gap-2">
+ <div className="flex-1">
+ <ComboboxBase
+ value={unconsumedBatch}
+ onChange={setUnconsumedBatch}
+ options={unconsumeOptions}
+ placeholder="Select batch to unconsume"
+ />
+ </div>
+ {unconsumedBatch && (
+ <div className="w-24">
+ <Input
+ isReadOnly
+ value={
+ trackedInputs
+ .find(
+ (input) =>
+ input.id === unconsumedBatch
+ )
+ ?.quantity.toString() ?? "0"
+ }
+ />
+ </div>
+ )}
+ </div>
+ <div className="h-8" />
+ </div>
+ </TabsContent>
+ )}
+ </Tabs>
+ )}
+ </div>
+ </ModalBody>
+ <ModalFooter>
+ {splitEntitiesResult.length > 0 ? (
+ <Button variant="primary" onClick={onClose}>
+ Close
+ </Button>
+ ) : (
+ <>
+ <Button variant="secondary" onClick={onClose}>
+ Cancel
+ </Button>
+ {activeTab === "unconsume" ? (
+ <Button
+ variant="destructive"
+ onClick={
+ trackingType === "Serial"
+ ? handleUnconsumeSerial
+ : handleUnconsumeBatch
+ }
+ isLoading={unconsumeFetcher.state !== "idle"}
+ isDisabled={
+ unconsumeFetcher.state !== "idle" ||
+ (trackingType === "Serial"
+ ? selectedTrackedInputs.length === 0
+ : !unconsumedBatch)
+ }
+ >
+ Unconsume
+ </Button>
+ ) : (
+ <Button
+ variant="primary"
+ onClick={
+ trackingType === "Serial"
+ ? handleSubmitSerial
+ : handleSubmitBatch
+ }
+ isLoading={fetcher.state !== "idle"}
+ isDisabled={
+ fetcher.state !== "idle" ||
+ !selectedItemId ||
+ isLoadingItem
+ }
+ >
+ Issue
+ </Button>
+ )}
+ </>
+ )}
+ </ModalFooter>
+ </>
+ )}
+
+ {/* Footer for split entities result */}
+ {splitEntitiesResult.length > 0 && (
+ <ModalFooter>
+ <Button variant="primary" onClick={onClose}>
+ Close
+ </Button>
+ </ModalFooter>
+ )}
+ </ModalContent>
+ </Modal>
+
+ {/* Sub-modals for batch splitting */}
+ {convertDisclosure.isOpen && (
+ <ConvertSplitModal
+ trackedEntity={trackedEntity}
+ itemType={material?.itemType ?? "Part"}
+ onCancel={() => {
+ convertDisclosure.onClose();
+ setTrackedEntity(null);
+ }}
+ onSuccess={(convertedEntity) => {
+ setSplitEntitiesResult((prev) =>
+ prev.map((entity) =>
+ entity.newId === convertedEntity.trackedEntityId
+ ? {
+ ...entity,
+ readableId: convertedEntity.readableId,
+ quantity: convertedEntity.quantity
+ }
+ : entity
+ )
+ );
+ convertDisclosure.onClose();
+ setTrackedEntity(null);
+ }}
+ />
+ )}
+ {scrapDisclosure.isOpen && (
+ <ScrapSplitModal
+ materialId={material?.id!}
+ parentTrackedEntityId={parentId ?? ""}
+ trackedEntity={trackedEntity}
+ onCancel={() => {
+ scrapDisclosure.onClose();
+ setTrackedEntity(null);
+ }}
+ onSuccess={() => {
+ scrapDisclosure.onClose();
+ setTrackedEntity(null);
+ onClose();
+ }}
+ />
+ )}
+ </>
+ );
+}
+
+// Sub-modal for converting split batch entities
+function ConvertSplitModal({
+ trackedEntity,
+ itemType,
+ onCancel,
+ onSuccess
+}: {
+ trackedEntity: string | null;
+ itemType: string | null;
+ onCancel: () => void;
+ onSuccess: (convertedEntity: {
+ trackedEntityId: string;
+ readableId: string;
+ quantity: number;
+ }) => void;
+}) {
+ const fetcher = useFetcher<{
+ success: boolean;
+ message: string;
+ convertedEntity?: {
+ trackedEntityId: string;
+ readableId: string;
+ quantity: number;
+ };
+ }>();
+
+ useEffect(() => {
+ if (fetcher.data?.success && fetcher.data.convertedEntity) {
+ toast.success("Entity converted successfully");
+ onSuccess(fetcher.data.convertedEntity);
+ } else if (fetcher.data?.success === false) {
+ toast.error(fetcher.data.message || "Failed to convert entity");
+ }
+ }, [fetcher.data, onSuccess]);
+
+ if (!trackedEntity) return null;
+
+ return (
+ <Modal open onOpenChange={onCancel}>
+ <ModalContent>
+ <ModalHeader>
+ <ModalTitle>
+ Convert to New {itemType === "Material" ? "Size" : "Revision"}
+ </ModalTitle>
+ <ModalDescription>
+ Convert this tracked entity into a quantity of 1 of a new size.
+ </ModalDescription>
+ </ModalHeader>
+ <ValidatedForm
+ method="post"
+ action={path.to.convertEntity(trackedEntity)}
+ defaultValues={{
+ trackedEntityId: trackedEntity,
+ newRevision: "",
+ quantity: 1
+ }}
+ validator={convertEntityValidator}
+ fetcher={fetcher}
+ >
+ <Hidden name="trackedEntityId" />
+ <ModalBody>
+ <div className="flex flex-col gap-4">
+ <FormInput
+ name="newRevision"
+ label={`New ${itemType === "Material" ? "Size" : "Revision"}`}
+ autoFocus
+ />
+ <FormNumberInput
+ name="quantity"
+ label="Quantity"
+ minValue={0.001}
+ />
+ </div>
+ </ModalBody>
+ <ModalFooter>
+ <Button variant="secondary" onClick={onCancel}>
+ Cancel
+ </Button>
+ <Button
+ isLoading={fetcher.state !== "idle"}
+ isDisabled={fetcher.state !== "idle"}
+ type="submit"
+ variant="primary"
+ >
+ Convert
+ </Button>
+ </ModalFooter>
+ </ValidatedForm>
+ </ModalContent>
+ </Modal>
+ );
+}
+
+// Sub-modal for scrapping split batch entities
+function ScrapSplitModal({
+ materialId,
+ parentTrackedEntityId,
+ trackedEntity,
+ onCancel,
+ onSuccess
+}: {
+ materialId: string;
+ parentTrackedEntityId: string;
+ trackedEntity: string | null;
+ onCancel: () => void;
+ onSuccess: () => void;
+}) {
+ const fetcher = useFetcher<{ success: boolean; message: string }>();
+
+ useEffect(() => {
+ if (fetcher.data?.success) {
+ onSuccess();
+ }
+ }, [fetcher.data?.success, onSuccess]);
+
+ if (!trackedEntity) return null;
+
+ return (
+ <Modal open onOpenChange={onCancel}>
+ <ModalContent>
+ <ModalHeader>
+ <ModalTitle>Are you sure you want to scrap this batch?</ModalTitle>
+ <ModalDescription>
+ The remaining quantity will be removed from inventory and issued to
+ the job
+ </ModalDescription>
+ </ModalHeader>
+ <ModalFooter>
+ <Button variant="secondary" onClick={onCancel}>
+ Cancel
+ </Button>
+ <fetcher.Form
+ method="post"
+ action={path.to.scrapEntity(
+ materialId,
+ trackedEntity,
+ parentTrackedEntityId
+ )}
+ >
+ <Button
+ isLoading={fetcher.state !== "idle"}
+ isDisabled={fetcher.state !== "idle"}
+ type="submit"
+ variant="destructive"
+ >
+ Scrap
+ </Button>
+ </fetcher.Form>
+ </ModalFooter>
+ </ModalContent>
+ </Modal>
+ );
+}
+
+// Hook for fetching serial numbers
+function useSerialNumbers(itemId?: string) {
+ const serialNumbersFetcher =
+ useFetcher<Awaited<ReturnType<typeof getSerialNumbersForItem>>>();
+
+ useEffect(() => {
+ if (itemId) {
+ serialNumbersFetcher.load(path.to.api.serialNumbers(itemId));
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [itemId]);
+
+ return { data: serialNumbersFetcher.data };
+}
+
+// Hook for fetching batch numbers
+function useBatchNumbers(itemId?: string) {
+ const batchNumbersFetcher =
+ useFetcher<Awaited<ReturnType<typeof getBatchNumbersForItem>>>();
+
+ useEffect(() => {
+ if (itemId) {
+ batchNumbersFetcher.load(path.to.api.batchNumbers(itemId));
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [itemId]);
+
+ return { data: batchNumbersFetcher.data };
+}
diff --git a/apps/mes/app/components/JobOperation/components/IssueModal.tsx b/apps/mes/app/components/JobOperation/components/IssueModal.tsx
deleted file mode 100644
index 373f9b0c20..0000000000
--- a/apps/mes/app/components/JobOperation/components/IssueModal.tsx
+++ /dev/null
@@ -1,104 +0,0 @@
-// biome-ignore lint/suspicious/noShadowRestrictedNames: suppressed due to migration
-import { Combobox, Hidden, Number, Select, ValidatedForm } from "@carbon/form";
-import {
- Button,
- Modal,
- ModalBody,
- ModalContent,
- ModalDescription,
- ModalFooter,
- ModalHeader,
- ModalTitle,
- VStack
-} from "@carbon/react";
-import { useMemo } from "react";
-import { issueValidator } from "~/services/models";
-import type { JobMaterial } from "~/services/types";
-import { useItems } from "~/stores";
-import { path } from "~/utils/path";
-
-export function IssueModal({
- operationId,
- material,
- onClose
-}: {
- operationId: string;
- material?: JobMaterial;
- onClose: () => void;
-}) {
- const [items] = useItems();
- const itemOptions = useMemo(() => {
- return items
- .filter((i) => !["Batch", "Serial"].includes(i.itemTrackingType))
- .map((item) => ({
- label: item.readableIdWithRevision,
- helper: item.name,
- value: item.id
- }));
- }, [items]);
-
- return (
- <Modal open onOpenChange={onClose}>
- <ModalContent>
- <ModalHeader>
- <ModalTitle>Issue Material</ModalTitle>
- <ModalDescription>
- Manually add or subtract material from the required quantities.
- </ModalDescription>
- </ModalHeader>
-
- <ValidatedForm
- method="post"
- action={path.to.issue}
- onSubmit={onClose}
- validator={issueValidator}
- defaultValues={{
- materialId: material?.id ?? "",
- jobOperationId: operationId,
- itemId: material?.itemId ?? "",
- quantity:
- (material?.estimatedQuantity ?? 0) -
- (material?.quantityIssued ?? 0),
- adjustmentType: "Negative Adjmt."
- }}
- >
- <ModalBody>
- <Hidden name="jobOperationId" />
- <Hidden name="materialId" />
- {material?.id && (
- <Hidden name="adjustmentType" value="Negative Adjmt." />
- )}
- <VStack spacing={4}>
- <Combobox name="itemId" label="Item" options={itemOptions} />
- {!material?.id && (
- <Select
- name="adjustmentType"
- label="Adjustment Type"
- options={[
- {
- label: "Add to Inventory",
- value: "Positive Adjmt."
- },
- {
- label: "Pull from Inventory",
- value: "Negative Adjmt."
- }
- ]}
- />
- )}
- <Number name="quantity" label="Quantity" />
- </VStack>
- </ModalBody>
- <ModalFooter>
- <Button variant="secondary" onClick={onClose}>
- Cancel
- </Button>
- <Button type="submit" variant="primary">
- Issue
- </Button>
- </ModalFooter>
- </ValidatedForm>
- </ModalContent>
- </Modal>
- );
-}
diff --git a/apps/mes/app/components/JobOperation/components/SerialIssueModal.tsx b/apps/mes/app/components/JobOperation/components/SerialIssueModal.tsx
deleted file mode 100644
index a1eb019abc..0000000000
--- a/apps/mes/app/components/JobOperation/components/SerialIssueModal.tsx
+++ /dev/null
@@ -1,548 +0,0 @@
-import {
- Alert,
- AlertDescription,
- AlertTitle,
- Button,
- Checkbox,
- Combobox as ComboboxBase,
- cn,
- IconButton,
- Input,
- InputGroup,
- InputRightElement,
- Modal,
- ModalBody,
- ModalContent,
- ModalDescription,
- ModalFooter,
- ModalTitle,
- Tabs,
- TabsContent,
- TabsList,
- TabsTrigger,
- toast
-} from "@carbon/react";
-import { getItemReadableId } from "@carbon/utils";
-import { useCallback, useEffect, useMemo, useState } from "react";
-import {
- LuCheck,
- LuCirclePlus,
- LuList,
- LuQrCode,
- LuUndo2,
- LuX
-} from "react-icons/lu";
-import { useFetcher } from "react-router";
-import type { getSerialNumbersForItem } from "~/services/inventory.service";
-import type { JobMaterial, TrackedInput } from "~/services/types";
-import { useItems } from "~/stores";
-import { path } from "~/utils/path";
-
-export function SerialIssueModal({
- operationId,
- material,
- parentId,
- parentIdIsSerialized,
- trackedInputs,
- onClose
-}: {
- parentId: string;
- parentIdIsSerialized: boolean;
- operationId: string;
- material?: JobMaterial;
- trackedInputs: TrackedInput[];
- onClose: () => void;
-}) {
- const fetcher = useFetcher<{ success: boolean; message: string }>();
- const unconsumeFetcher = useFetcher<{ success: boolean; message: string }>();
- const { data: serialNumbers } = useSerialNumbers(material?.itemId ?? "");
-
- const [errors, setErrors] = useState<Record<number, string>>({});
-
- const options = useMemo(() => {
- return (
- serialNumbers?.data?.map((serialNumber) => ({
- label: serialNumber.id ?? "",
- value: serialNumber.id,
- helper: serialNumber.readableId
- ? `Serial ${serialNumber.readableId}`
- : undefined
- })) ?? []
- );
- }, [serialNumbers]);
-
- const initialQuantity = Math.max(
- 1,
- parentIdIsSerialized
- ? (material?.quantity ?? material?.estimatedQuantity ?? 1)
- : (material?.estimatedQuantity ?? material?.quantity ?? 1)
- );
-
- const [selectedSerialNumbers, setSelectedSerialNumbers] = useState<
- Array<{
- index: number;
- id: string;
- }>
- >(
- Array(initialQuantity)
- .fill("")
- .map((_, index) => ({ index, id: "" }))
- );
-
- const [selectedTrackedInputs, setSelectedTrackedInputs] = useState<string[]>(
- []
- );
-
- const validateSerialNumber = useCallback(
- (value: string, index: number) => {
- if (!value) return "Serial number is required";
-
- // Check for duplicates
- const isDuplicate = selectedSerialNumbers.some(
- (sn, i) => sn.id === value && i !== index
- );
- if (isDuplicate) return "Duplicate serial number";
-
- // Check if serial number exists in options
- const isValid = options.some((option) => option.value === value);
- if (!isValid) {
- const serialNumber = serialNumbers?.data?.find((sn) => sn.id === value);
- if (serialNumber) return `Serial number is ${serialNumber.status}`;
- return "Serial number is not available";
- }
-
- return null;
- },
- [selectedSerialNumbers, options, serialNumbers?.data]
- );
-
- const updateSerialNumber = useCallback(
- (serialNumber: { index: number; id: string }) => {
- setSelectedSerialNumbers((prev) => {
- const newSerialNumbers = [...prev];
- newSerialNumbers[serialNumber.index] = serialNumber;
- return newSerialNumbers;
- });
- },
- []
- );
-
- const addSerialNumber = useCallback(() => {
- setSelectedSerialNumbers((prev) => {
- const newIndex = prev.length;
- return [...prev, { index: newIndex, id: "" }];
- });
- }, []);
-
- const removeSerialNumber = useCallback((indexToRemove: number) => {
- setSelectedSerialNumbers((prev) => {
- // Remove the item at the specified index
- const filtered = prev.filter((_, i) => i !== indexToRemove);
-
- // Reindex the remaining items
- return filtered.map((item, i) => ({ ...item, index: i }));
- });
-
- // Clean up any errors for the removed index
- setErrors((prev) => {
- const newErrors = { ...prev };
- delete newErrors[indexToRemove];
-
- // Reindex the errors for indices greater than the removed one
- const reindexedErrors: Record<number, string> = {};
- Object.entries(newErrors).forEach(([key, value]) => {
- const keyNum = parseInt(key);
- if (keyNum > indexToRemove) {
- reindexedErrors[keyNum - 1] = value;
- } else {
- reindexedErrors[keyNum] = value;
- }
- });
-
- return reindexedErrors;
- });
- }, []);
-
- // biome-ignore lint/correctness/useExhaustiveDependencies: suppressed due to migration
- const handleSubmit = useCallback(() => {
- // Validate all serial numbers
- let hasErrors = false;
- const newErrors: Record<number, string> = {};
-
- selectedSerialNumbers.forEach((sn) => {
- const error = validateSerialNumber(sn.id, sn.index);
- if (error) {
- newErrors[sn.index] = error;
- hasErrors = true;
- }
- });
-
- setErrors(newErrors);
-
- if (!hasErrors) {
- // Submit to the API
- const payload = {
- materialId: material?.id!,
- parentTrackedEntityId: parentId,
- children: selectedSerialNumbers.map((sn) => ({
- trackedEntityId: sn.id,
- quantity: 1
- }))
- };
-
- fetcher.submit(JSON.stringify(payload), {
- method: "post",
- action: path.to.issueTrackedEntity,
- encType: "application/json"
- });
- }
- }, [
- selectedSerialNumbers,
- validateSerialNumber,
- operationId,
- parentId,
- onClose,
- material?.id
- ]);
-
- const handleUnconsume = useCallback(() => {
- if (selectedTrackedInputs.length === 0) {
- toast.error("Please select at least one item to unconsume");
- return;
- }
-
- const payload = {
- materialId: material?.id!,
- parentTrackedEntityId: parentId,
- children: selectedTrackedInputs.map((id) => ({
- trackedEntityId: id,
- quantity: 1
- }))
- };
-
- unconsumeFetcher.submit(JSON.stringify(payload), {
- method: "post",
- action: path.to.unconsume,
- encType: "application/json"
- });
- }, [selectedTrackedInputs, material?.id, parentId, unconsumeFetcher]);
-
- useEffect(() => {
- if (fetcher.data?.success) {
- onClose();
- if (fetcher.data.message) {
- toast.success(fetcher.data.message);
- }
- } else if (fetcher.data?.message) {
- toast.error(fetcher.data.message);
- }
- }, [fetcher.data, onClose]);
-
- useEffect(() => {
- if (unconsumeFetcher.data?.success) {
- onClose();
- if (unconsumeFetcher.data.message) {
- toast.success(unconsumeFetcher.data.message);
- }
- } else if (unconsumeFetcher.data?.message) {
- toast.error(unconsumeFetcher.data.message);
- }
- }, [unconsumeFetcher.data, onClose]);
-
- const [activeTab, setActiveTab] = useState("scan");
-
- const toggleTrackedInput = useCallback((id: string) => {
- setSelectedTrackedInputs((prev) => {
- if (prev.includes(id)) {
- return prev.filter((item) => item !== id);
- } else {
- return [...prev, id];
- }
- });
- }, []);
-
- const [items] = useItems();
-
- return (
- <Modal open onOpenChange={onClose}>
- <ModalContent>
- <ModalTitle>
- {getItemReadableId(items, material?.itemId) ?? "Material"}
- </ModalTitle>
- <ModalDescription>{material?.description}</ModalDescription>
- <ModalBody>
- <Tabs value={activeTab} onValueChange={setActiveTab}>
- <TabsList
- className={cn(
- "grid w-full grid-cols-2 mb-4",
- trackedInputs.length > 0 && "grid-cols-3"
- )}
- >
- <TabsTrigger value="scan">
- <LuQrCode className="mr-2" />
- Scan
- </TabsTrigger>
- <TabsTrigger value="select">
- <LuList className="mr-2" />
- Select
- </TabsTrigger>
- {trackedInputs.length > 0 && (
- <TabsTrigger value="unconsume">
- <LuUndo2 className="mr-2" />
- Unconsume
- </TabsTrigger>
- )}
- </TabsList>
- <TabsContent value="scan">
- <div className="flex flex-col gap-4">
- {selectedSerialNumbers.map((serialNumber, index) => (
- <div
- key={`${index}-serial-scan`}
- className="flex flex-col gap-1"
- >
- <div className="flex items-center gap-2">
- <div className="flex-1">
- <InputGroup>
- <Input
- placeholder={`Serial Number ${index + 1}`}
- value={serialNumber.id}
- onChange={(e) => {
- const newValue = e.target.value;
- const newSerialNumbers = [
- ...selectedSerialNumbers
- ];
- newSerialNumbers[index] = {
- index,
- id: newValue
- };
- setSelectedSerialNumbers(newSerialNumbers);
- }}
- onBlur={(e) => {
- const newValue = e.target.value;
- const error = validateSerialNumber(
- newValue,
- index
- );
-
- setErrors((prev) => {
- const newErrors = { ...prev };
- if (error) {
- newErrors[index] = error;
- } else {
- delete newErrors[index];
- }
- return newErrors;
- });
-
- if (!error) {
- updateSerialNumber({
- index,
- id: newValue
- });
- } else {
- // Clear the input value but keep the error message
- const newSerialNumbers = [
- ...selectedSerialNumbers
- ];
- newSerialNumbers[index] = {
- index,
- id: ""
- };
- setSelectedSerialNumbers(newSerialNumbers);
- }
- }}
- className={cn(
- errors[index] && "border-destructive"
- )}
- />
- <InputRightElement className="pl-2">
- {!errors[index] && serialNumber.id ? (
- <LuCheck className="text-emerald-500" />
- ) : (
- <LuQrCode />
- )}
- </InputRightElement>
- </InputGroup>
- </div>
- {index > 0 && (
- <IconButton
- aria-label="Remove Serial Number"
- icon={<LuX />}
- variant="ghost"
- onClick={() => removeSerialNumber(index)}
- className="flex-shrink-0"
- />
- )}
- </div>
- {errors[index] && (
- <span className="text-xs text-destructive">
- {errors[index]}
- </span>
- )}
- </div>
- ))}
- <div>
- <Button
- type="button"
- variant="secondary"
- leftIcon={<LuCirclePlus />}
- onClick={addSerialNumber}
- >
- Add
- </Button>
- </div>
- </div>
- </TabsContent>
- <TabsContent value="select">
- <div className="flex flex-col gap-4">
- {selectedSerialNumbers.map((serialNumber, index) => (
- <div
- key={`${index}-serial-select`}
- className="flex flex-col gap-1"
- >
- <div className="flex items-center gap-2">
- <div className="flex-1">
- <ComboboxBase
- placeholder={`Select Serial Number ${index + 1}`}
- value={serialNumber.id}
- onChange={(value) => {
- const newSerialNumbers = [...selectedSerialNumbers];
- newSerialNumbers[index] = {
- index,
- id: value
- };
- setSelectedSerialNumbers(newSerialNumbers);
-
- // Validate on change for select
- const error = validateSerialNumber(value, index);
- setErrors((prev) => {
- const newErrors = { ...prev };
- if (error) {
- newErrors[index] = error;
- } else {
- delete newErrors[index];
- }
- return newErrors;
- });
- }}
- options={options}
- />
- </div>
- {index > 0 && (
- <IconButton
- aria-label="Remove Serial Number"
- icon={<LuX />}
- variant="ghost"
- onClick={() => removeSerialNumber(index)}
- className="flex-shrink-0"
- />
- )}
- </div>
- {errors[index] && (
- <span className="text-xs text-destructive">
- {errors[index]}
- </span>
- )}
- </div>
- ))}
- <div>
- <Button
- type="button"
- variant="secondary"
- leftIcon={<LuCirclePlus />}
- onClick={addSerialNumber}
- >
- Add
- </Button>
- </div>
- </div>
- </TabsContent>
- {trackedInputs.length > 0 && (
- <TabsContent value="unconsume">
- <div className="flex flex-col gap-4">
- {trackedInputs.map((input) => {
- const serialNumber = input.readableId;
-
- return (
- <div
- key={input.id}
- className="flex items-center gap-3 p-2 border rounded-md"
- >
- <Checkbox
- id={`unconsume-${input.id}`}
- checked={selectedTrackedInputs.includes(input.id)}
- onCheckedChange={() => toggleTrackedInput(input.id)}
- />
- <label
- htmlFor={`unconsume-${input.id}`}
- className="flex-1 cursor-pointer"
- >
- <div className="font-medium text-sm">{input.id}</div>
- {serialNumber && (
- <div className="text-xs text-muted-foreground">
- Serial: {serialNumber}
- </div>
- )}
- </label>
- </div>
- );
- })}
-
- {trackedInputs.length === 0 && (
- <Alert variant="warning">
- <AlertTitle>No consumed materials</AlertTitle>
- <AlertDescription>
- There are no consumed materials to unconsume.
- </AlertDescription>
- </Alert>
- )}
- </div>
- </TabsContent>
- )}
- </Tabs>
- </ModalBody>
- <ModalFooter>
- <Button variant="secondary" onClick={onClose}>
- Cancel
- </Button>
- {activeTab === "unconsume" ? (
- <Button
- variant="destructive"
- onClick={handleUnconsume}
- isLoading={unconsumeFetcher.state !== "idle"}
- isDisabled={
- unconsumeFetcher.state !== "idle" ||
- selectedTrackedInputs.length === 0
- }
- >
- Unconsume
- </Button>
- ) : (
- <Button
- variant="primary"
- onClick={handleSubmit}
- isLoading={fetcher.state !== "idle"}
- isDisabled={fetcher.state !== "idle"}
- >
- Issue
- </Button>
- )}
- </ModalFooter>
- </ModalContent>
- </Modal>
- );
-}
-
-function useSerialNumbers(itemId?: string) {
- const serialNumbersFetcher =
- useFetcher<Awaited<ReturnType<typeof getSerialNumbersForItem>>>();
-
- // biome-ignore lint/correctness/useExhaustiveDependencies: suppressed due to migration
- useEffect(() => {
- if (itemId) {
- serialNumbersFetcher.load(path.to.api.serialNumbers(itemId));
- }
- }, [itemId]);
-
- return { data: serialNumbersFetcher.data };
-}
diff --git a/apps/mes/app/routes/x+/issue-tracked-entity.tsx b/apps/mes/app/routes/x+/issue-tracked-entity.tsx
index 2028495e54..c907bf57f4 100644
--- a/apps/mes/app/routes/x+/issue-tracked-entity.tsx
+++ b/apps/mes/app/routes/x+/issue-tracked-entity.tsx
@@ -19,13 +19,21 @@ export async function action({ request }: ActionFunctionArgs) {
);
}
- const { materialId, parentTrackedEntityId, children } = validation.data;
+ const {
+ materialId,
+ jobOperationId,
+ itemId,
+ parentTrackedEntityId,
+ children
+ } = validation.data;
const serviceRole = await getCarbonServiceRole();
const issue = await serviceRole.functions.invoke("issue", {
body: {
type: "trackedEntitiesToOperation",
materialId,
+ jobOperationId,
+ itemId,
parentTrackedEntityId,
children,
companyId,
diff --git a/apps/mes/app/services/inventory.service.ts b/apps/mes/app/services/inventory.service.ts
index 19592a1400..3f3c87b0b8 100644
--- a/apps/mes/app/services/inventory.service.ts
+++ b/apps/mes/app/services/inventory.service.ts
@@ -25,7 +25,7 @@ export async function getBatchNumbersForItem(
.select("*")
.eq("id", args.itemId)
.single();
- if (item.data?.type === "Material" && item.data.revision !== "0") {
+ if (item.data?.type === "Material") {
const items = await client
.from("item")
.select("id")
@@ -63,11 +63,28 @@ export async function getSerialNumbersForItem(
companyId: string;
}
) {
+ let itemIds = [args.itemId];
+ const item = await client
+ .from("item")
+ .select("*")
+ .eq("id", args.itemId)
+ .single();
+ if (item.data?.type === "Material") {
+ const items = await client
+ .from("item")
+ .select("id")
+ .eq("readableId", item.data.readableId)
+ .eq("companyId", args.companyId);
+ if (items.data?.length) {
+ itemIds = items.data.map((item) => item.id);
+ }
+ }
+
return client
.from("trackedEntity")
.select("*")
.eq("sourceDocument", "Item")
- .eq("sourceDocumentId", args.itemId)
+ .in("sourceDocumentId", itemIds)
.eq("companyId", args.companyId)
.eq("status", "Available")
.gt("quantity", 0);
diff --git a/apps/mes/app/services/models.ts b/apps/mes/app/services/models.ts
index 381cb7fe05..b3145b2b87 100644
--- a/apps/mes/app/services/models.ts
+++ b/apps/mes/app/services/models.ts
@@ -134,7 +134,9 @@ export const finishValidator = z.object({
});
export const issueTrackedEntityValidator = z.object({
- materialId: z.string(),
+ materialId: z.string().optional(),
+ jobOperationId: z.string().optional(),
+ itemId: z.string().optional(),
parentTrackedEntityId: z.string(),
children: z.array(
z.object({
diff --git a/packages/database/supabase/functions/issue/index.ts b/packages/database/supabase/functions/issue/index.ts
index 0500ca4852..6ebea8629b 100644
--- a/packages/database/supabase/functions/issue/index.ts
+++ b/packages/database/supabase/functions/issue/index.ts
@@ -96,7 +96,9 @@ const payloadValidator = z.discriminatedUnion("type", [
}),
z.object({
type: z.literal("trackedEntitiesToOperation"),
- materialId: z.string(),
+ materialId: z.string().optional(),
+ jobOperationId: z.string().optional(),
+ itemId: z.string().optional(),
parentTrackedEntityId: z.string(),
children: z.array(
z.object({
@@ -1137,6 +1139,8 @@ serve(async (req: Request) => {
case "trackedEntitiesToOperation": {
const {
materialId,
+ jobOperationId,
+ itemId,
parentTrackedEntityId,
children,
companyId,
@@ -1151,6 +1155,13 @@ serve(async (req: Request) => {
throw new Error("Children are required");
}
+ // Either materialId or (jobOperationId + itemId) must be provided
+ if (!materialId && (!jobOperationId || !itemId)) {
+ throw new Error(
+ "Either materialId or both jobOperationId and itemId must be provided"
+ );
+ }
+
const splitEntities = await db.transaction().execute(async (trx) => {
const trackedEntities = await trx
.selectFrom("trackedEntity")
@@ -1179,29 +1190,99 @@ serve(async (req: Request) => {
throw new Error("Tracked entities are not available");
}
- let jobMaterial = await trx
- .selectFrom("jobMaterial")
- .where("id", "=", materialId)
- .selectAll()
- .executeTakeFirst();
-
- // Check if any tracked entity has a different sourceDocumentId than the material's itemId
+ let jobMaterial: Awaited<
+ ReturnType<
+ ReturnType<typeof trx.selectFrom<"jobMaterial">>["selectAll"]
+ >
+ >[0] | undefined;
+ let actualMaterialId: string | undefined = materialId;
const firstTrackedEntity = trackedEntities[0];
- let actualMaterialId = materialId;
- if (
- firstTrackedEntity &&
- jobMaterial &&
- firstTrackedEntity.sourceDocumentId !== jobMaterial.itemId
- ) {
- // Create a new jobMaterial for the tracked entity's item
+ if (materialId) {
+ // Existing behavior: fetch the jobMaterial
+ jobMaterial = await trx
+ .selectFrom("jobMaterial")
+ .where("id", "=", materialId)
+ .selectAll()
+ .executeTakeFirst();
+
+ // Check if any tracked entity has a different sourceDocumentId than the material's itemId
+ if (
+ firstTrackedEntity &&
+ jobMaterial &&
+ firstTrackedEntity.sourceDocumentId !== jobMaterial.itemId
+ ) {
+ // Create a new jobMaterial for the tracked entity's item
+ const totalChildQuantity = children.reduce((sum, child) => {
+ return sum + Number(child.quantity);
+ }, 0);
+
+ const itemCost = await trx
+ .selectFrom("itemCost")
+ .where("itemId", "=", firstTrackedEntity.sourceDocumentId!)
+ .select("unitCost")
+ .executeTakeFirst();
+
+ const newJobMaterial = await trx
+ .insertInto("jobMaterial")
+ .values({
+ companyId,
+ createdBy: userId,
+ description: firstTrackedEntity.sourceDocumentReadableId ?? "",
+ estimatedQuantity: 0,
+ itemId: firstTrackedEntity.sourceDocumentId!,
+ jobId: jobMaterial.jobId!,
+ jobMakeMethodId: jobMaterial.jobMakeMethodId,
+ jobOperationId: jobMaterial.jobOperationId,
+ itemType: jobMaterial.itemType,
+ methodType: jobMaterial.methodType,
+ quantity: 0,
+ quantityIssued: totalChildQuantity,
+ requiresBatchTracking: jobMaterial.requiresBatchTracking,
+ requiresSerialTracking: jobMaterial.requiresSerialTracking,
+ unitCost: itemCost?.unitCost,
+ })
+ .returning("id")
+ .executeTakeFirstOrThrow();
+
+ actualMaterialId = newJobMaterial.id!;
+
+ // Fetch the newly created jobMaterial
+ jobMaterial = await trx
+ .selectFrom("jobMaterial")
+ .where("id", "=", actualMaterialId)
+ .selectAll()
+ .executeTakeFirstOrThrow();
+ }
+ } else if (jobOperationId && itemId) {
+ // New behavior: create a jobMaterial on the fly
+ const jobOperation = await trx
+ .selectFrom("jobOperation")
+ .where("id", "=", jobOperationId)
+ .select(["jobId", "jobMakeMethodId"])
+ .executeTakeFirst();
+
+ if (!jobOperation) {
+ throw new Error("Job operation not found");
+ }
+
+ const item = await trx
+ .selectFrom("item")
+ .where("id", "=", itemId)
+ .select(["name", "type", "itemTrackingType", "defaultMethodType"])
+ .executeTakeFirst();
+
+ if (!item) {
+ throw new Error("Item not found");
+ }
+
const totalChildQuantity = children.reduce((sum, child) => {
return sum + Number(child.quantity);
}, 0);
const itemCost = await trx
.selectFrom("itemCost")
- .where("itemId", "=", firstTrackedEntity.sourceDocumentId!)
+ .where("itemId", "=", itemId)
.select("unitCost")
.executeTakeFirst();
@@ -1210,18 +1291,18 @@ serve(async (req: Request) => {
.values({
companyId,
createdBy: userId,
- description: firstTrackedEntity.sourceDocumentReadableId ?? "",
+ description: item.name ?? "",
estimatedQuantity: 0,
- itemId: firstTrackedEntity.sourceDocumentId!,
- jobId: jobMaterial.jobId!,
- jobMakeMethodId: jobMaterial.jobMakeMethodId,
- jobOperationId: jobMaterial.jobOperationId,
- itemType: jobMaterial.itemType,
- methodType: jobMaterial.methodType,
+ itemId: itemId,
+ jobId: jobOperation.jobId!,
+ jobMakeMethodId: jobOperation.jobMakeMethodId,
+ jobOperationId: jobOperationId,
+ itemType: item.type ?? "Part",
+ methodType: item.defaultMethodType ?? "Pick",
quantity: 0,
quantityIssued: totalChildQuantity,
- requiresBatchTracking: jobMaterial.requiresBatchTracking,
- requiresSerialTracking: jobMaterial.requiresSerialTracking,
+ requiresBatchTracking: item.itemTrackingType === "Batch",
+ requiresSerialTracking: item.itemTrackingType === "Serial",
unitCost: itemCost?.unitCost,
})
.returning("id")
@@ -1237,6 +1318,10 @@ serve(async (req: Request) => {
.executeTakeFirstOrThrow();
}
+ if (!jobMaterial) {
+ throw new Error("Job material not found");
+ }
+
// Get item details
const item = await trx
.selectFrom("item")
@@ -1276,7 +1361,7 @@ serve(async (req: Request) => {
id: activityId,
type: "Consume",
sourceDocument: "Job Material",
- sourceDocumentId: materialId,
+ sourceDocumentId: actualMaterialId,
sourceDocumentReadableId: item?.readableIdWithRevision ?? "",
attributes: {
Job: job?.id!,
@@ -1350,7 +1435,7 @@ serve(async (req: Request) => {
id: splitActivityId,
type: "Split",
sourceDocument: "Job Material",
- sourceDocumentId: materialId,
+ sourceDocumentId: actualMaterialId,
attributes: {
"Original Quantity": Number(trackedEntity.quantity),
"Consumed Quantity": quantity,