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.

2.0 KB
Raw Blame
// 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 { useNavigate, useParams } from "react-router";
import { useRouteData } from "~/hooks";
import type { AffectedItemDraft } from "~/modules/items/ui/ChangeNotice";
import { SupplierPartForm } from "~/modules/items/ui/Item";
import { path } from "~/utils/path";

// Create drawer for a supplier part on a CO line's draft item (Buy Revision /
// New Part) — reached relatively from the Supplier Parts grid embedded in
// AffectedItemDetail. No action here: the form posts to the part purchasing
// create action (path.to.newPartSupplier(draftItemId)), which returns
// { success } without redirecting, so the CO stays put and loader revalidation
// refreshes the grid in place.
export default function ChangeNoticeNewSupplierPartRoute() {
  const { id, affectedId } = useParams();
  if (!id) throw new Error("Could not find id");
  if (!affectedId) throw new Error("Could not find affectedId");

  const routeData = useRouteData<{ affectedItems: AffectedItemDraft[] }>(
    path.to.changeNotice(id)
  );
  const affected =
    routeData?.affectedItems.find((a) => a.affectedItem.id === affectedId) ??
    null;

  const navigate = useNavigate();
  const onClose = () =>
    navigate(path.to.changeNoticeAffectedItem(id, affectedId));

  // The grid only renders (and links here) for Part-type lines with partData.
  if (!affected?.partData) return null;

  const initialValues = {
    itemId: affected.draftItemId,
    supplierId: "",
    supplierPartId: "",
    supplierUnitPrice: 0,
    supplierUnitOfMeasureCode: "EA",
    minimumOrderQuantity: 1,
    orderMultiple: 1,
    conversionFactor: 1
  };

  return (
    <SupplierPartForm
      type="Part"
      initialValues={initialValues}
      unitOfMeasureCode={affected.partData.partSummary?.unitOfMeasureCode ?? ""}
      onClose={onClose}
    />
  );
}