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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
import { useCarbon } from "@carbon/auth";
import { useRuleViolations } from "@carbon/ee/rules";
import {
Alert,
AlertDescription,
AlertTitle,
Button,
DatePicker,
FormControl,
FormLabel,
HStack,
Modal,
ModalBody,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
ModalTitle,
toast,
useMount,
useRouteData
} from "@carbon/react";
import type { TrackedEntityAttributes } from "@carbon/utils";
import { getItemReadableId } from "@carbon/utils";
import { getLocalTimeZone, parseDate, today } from "@internationalized/date";
import { Trans, useLingui } from "@lingui/react/macro";
import { useState } from "react";
import { LuTriangleAlert } from "react-icons/lu";
import { useNavigation, useParams } from "react-router";
import { DateTime } from "~/components";
import { useCompanyToday, useSettings, useUser } from "~/hooks";
import { useItems } from "~/stores";
import { path } from "~/utils/path";
import {
getShipmentTracking,
isShippableEntityStatus
} from "../../inventory.utils";
import type { Shipment, ShipmentLine } from "../../types";
type ExpiredEntityPolicy = "Warn" | "Block" | "BlockWithOverride";
const ShipmentPostModal = ({ onClose }: { onClose: () => void }) => {
const { shipmentId } = useParams();
if (!shipmentId) throw new Error("shipmentId not found");
const { t } = useLingui();
const [items] = useItems();
const routeData = useRouteData<{
shipment: Shipment;
shipmentLines: ShipmentLine[];
fixedAssetLines: {
id: string;
shipped: boolean;
}[];
rentalLines: { id: string; shipped: boolean }[];
}>(path.to.shipment(shipmentId));
const isRental = routeData?.shipment?.sourceDocument === "Rental Agreement";
const companyToday = useCompanyToday();
const [postingDate, setPostingDate] = useState(companyToday);
// Mirror the statuses lines.tracking accepted when the entity was assigned:
// On Hold on a return to a customer, Available or Rejected on a return to a
// supplier, Available otherwise.
const isShippable = (status: string | null | undefined) =>
isShippableEntityStatus(routeData?.shipment?.sourceDocument, status);
const navigation = useNavigation();
const [validated, setValidated] = useState(false);
const [validationErrors, setValidationErrors] = useState<
{
itemReadableId: string | null;
shippedQuantity: number;
shippedQuantityError: string;
}[]
>([]);
const { carbon } = useCarbon();
const {
company: { id: companyId }
} = useUser();
const settings = useSettings();
const expiredPolicy: ExpiredEntityPolicy =
(
(settings.inventoryShelfLife as {
expiredEntityPolicy?: ExpiredEntityPolicy;
} | null) ?? null
)?.expiredEntityPolicy ?? "Block";
const [expiredWarnings, setExpiredWarnings] = useState<
{
itemReadableId: string | null;
readableId: string;
expirationDate: string;
}[]
>([]);
const [expiredErrors, setExpiredErrors] = useState<
{
itemReadableId: string | null;
readableId: string;
expirationDate: string;
}[]
>([]);
const validateShipmentTracking = async () => {
const errors: {
itemReadableId: string | null;
shippedQuantity: number;
shippedQuantityError: string;
}[] = [];
if (!carbon) {
toast.error(t`Carbon client is not available`);
return;
}
const shipmentLineTracking = await getShipmentTracking(
carbon,
shipmentId,
companyId
);
const hasShipmentLines = routeData?.shipmentLines.some(
(line) => (line.shippedQuantity ?? 0) > 0
);
const hasFaLines = (routeData?.fixedAssetLines ?? []).some(
(line) => line.shipped
);
const hasRentalLines = (routeData?.rentalLines ?? []).some(
(line) => line.shipped
);
if (!hasShipmentLines && !hasFaLines && !hasRentalLines) {
setValidationErrors([
{
itemReadableId: null,
shippedQuantity: 0,
shippedQuantityError: "Shipment is empty"
}
]);
}
const todayLocal = today(getLocalTimeZone());
const isExpired = (expirationDate: string | null | undefined) => {
if (!expirationDate) return false;
try {
return parseDate(expirationDate).compare(todayLocal) < 0;
} catch {
return false;
}
};
const expiredCollected: {
itemReadableId: string | null;
readableId: string;
expirationDate: string;
}[] = [];
const expiredBlocked: {
itemReadableId: string | null;
readableId: string;
expirationDate: string;
}[] = [];
routeData?.shipmentLines.forEach((line: ShipmentLine) => {
if (line.requiresBatchTracking) {
const trackedEntity = shipmentLineTracking.data?.find((tracking) => {
const attributes = tracking.attributes as TrackedEntityAttributes;
return attributes["Shipment Line"] === line.id;
});
if (!isShippable(trackedEntity?.status)) {
errors.push({
itemReadableId: getItemReadableId(items, line.itemId) ?? null,
shippedQuantity: line.shippedQuantity ?? 0,
shippedQuantityError: "Tracked entity is not available"
});
}
if (trackedEntity && isExpired(trackedEntity.expirationDate)) {
const itemReadableId = getItemReadableId(items, line.itemId) ?? null;
const readableId = trackedEntity.readableId ?? trackedEntity.id;
const entry = {
itemReadableId,
readableId,
expirationDate: trackedEntity.expirationDate as string
};
if (
expiredPolicy === "Block" ||
expiredPolicy === "BlockWithOverride"
) {
expiredBlocked.push(entry);
} else {
expiredCollected.push(entry);
}
}
}
if (line.requiresSerialTracking) {
const trackedEntities = shipmentLineTracking.data?.filter(
(tracking) => {
const attributes = tracking.attributes as TrackedEntityAttributes;
return attributes["Shipment Line"] === line.id;
}
);
const quantityAvailable = trackedEntities?.reduce((acc, tracking) => {
const trackingQuantity = Number(tracking.quantity);
return acc + (isShippable(tracking.status) ? trackingQuantity : 0);
}, 0);
if (quantityAvailable !== line.shippedQuantity) {
errors.push({
itemReadableId: getItemReadableId(items, line.itemId) ?? null,
shippedQuantity: line.shippedQuantity ?? 0,
shippedQuantityError: "Serial numbers are missing or unavailable"
});
}
trackedEntities?.forEach((trackedEntity) => {
if (!isExpired(trackedEntity.expirationDate)) return;
const itemReadableId = getItemReadableId(items, line.itemId) ?? null;
const readableId = trackedEntity.readableId ?? trackedEntity.id;
const entry = {
itemReadableId,
readableId,
expirationDate: trackedEntity.expirationDate as string
};
if (
expiredPolicy === "Block" ||
expiredPolicy === "BlockWithOverride"
) {
expiredBlocked.push(entry);
} else {
expiredCollected.push(entry);
}
});
}
});
setValidationErrors(errors);
setExpiredWarnings(expiredCollected);
setExpiredErrors(expiredBlocked);
setValidated(true);
};
useMount(() => {
validateShipmentTracking();
});
const ruleViolations = useRuleViolations({
action: path.to.shipmentPost(shipmentId),
onSuccess: onClose
});
const { fetcher } = ruleViolations;
return (
<Modal
open={true}
onOpenChange={(open) => {
if (!open) onClose();
}}
>
<ModalOverlay />
<ModalContent>
<ModalHeader>
<ModalTitle>
<Trans>Post Shipment</Trans>
</ModalTitle>
</ModalHeader>
<ModalBody>
<p className="text-sm text-muted-foreground mb-4">
<Trans>Are you sure you want to post this shipment?</Trans>
</p>
{isRental && (
<FormControl className="mb-4">
<FormLabel>
<Trans>Delivered on</Trans>
</FormLabel>
<DatePicker
aria-label={t`Delivered on`}
value={parseDate(postingDate)}
maxValue={parseDate(companyToday)}
onChange={(d) => d && setPostingDate(d.toString())}
/>
</FormControl>
)}
{validationErrors.length > 0 && (
<Alert variant="destructive">
<LuTriangleAlert className="h-4 w-4" />
<AlertTitle>
<Trans>Missing Information</Trans>
</AlertTitle>
<AlertDescription>
<ul className="list-disc pl-4 mt-2 space-y-1">
{validationErrors.map((error, index) => (
<li key={index} className="text-sm font-medium">
<span className="font-mono">{error.itemReadableId}</span>
<span className="text-muted-foreground ml-2">
{error.shippedQuantity}
</span>
<span className="block mt-0.5 text-red-500 font-normal">
{error.shippedQuantityError}
</span>
</li>
))}
</ul>
</AlertDescription>
</Alert>
)}
{expiredErrors.length > 0 && (
<Alert variant="destructive" className="mt-4">
<LuTriangleAlert className="h-4 w-4" />
<AlertTitle>
<Trans>Expired Batches</Trans>
</AlertTitle>
<AlertDescription>
<Trans>
Cannot post — shipment contains expired tracked entities.
</Trans>
<ul className="list-disc pl-4 mt-2 space-y-1">
{expiredErrors.map((w, index) => (
<li key={index} className="text-sm font-medium">
<span className="font-mono">{w.itemReadableId}</span>
<span className="text-muted-foreground ml-2">
{w.readableId}
</span>
<span className="block mt-0.5 text-red-500 font-normal">
Expired on{" "}
<DateTime value={w.expirationDate} variant="date" />
</span>
</li>
))}
</ul>
</AlertDescription>
</Alert>
)}
{expiredWarnings.length > 0 && (
<Alert variant="warning" className="mt-4">
<LuTriangleAlert className="h-4 w-4" />
<AlertTitle>
<Trans>Expired Batches</Trans>
</AlertTitle>
<AlertDescription>
<ul className="list-disc pl-4 mt-2 space-y-1">
{expiredWarnings.map((w, index) => (
<li key={index} className="text-sm font-medium">
<span className="font-mono">{w.itemReadableId}</span>
<span className="text-muted-foreground ml-2">
{w.readableId}
</span>
<span className="block mt-0.5 text-amber-600 font-normal">
Expired on{" "}
<DateTime value={w.expirationDate} variant="date" />
</span>
</li>
))}
</ul>
</AlertDescription>
</Alert>
)}
</ModalBody>
<ModalFooter>
<HStack>
<Button variant="solid" onClick={onClose}>
<Trans>Cancel</Trans>
</Button>
<form
onSubmit={(e) => {
e.preventDefault();
const formData = new FormData();
if (isRental) formData.set("postingDate", postingDate);
ruleViolations.submit(formData);
}}
>
<Button
isLoading={fetcher.state !== "idle"}
isDisabled={
fetcher.state !== "idle" ||
navigation.state !== "idle" ||
!validated ||
validationErrors.length > 0 ||
expiredErrors.length > 0
}
type="submit"
>
Post Shipment
</Button>
</form>
</HStack>
</ModalFooter>
</ModalContent>
<ruleViolations.ViolationModal />
</Modal>
);
};
export default ShipmentPostModal;