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.

3.6 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 { useFormState, ValidatedForm } from "@carbon/form";
import {
  Card,
  CardContent,
  CardFooter,
  CardHeader,
  CardTitle,
  useViewport
} from "@carbon/react";
import { INPUT_FORMAT } from "@carbon/utils";
import { Trans, useLingui } from "@lingui/react/macro";
import { useFetcher, useParams } from "react-router";
import type { z } from "zod";
import {
  Boolean,
  CustomFormFields,
  Hidden,
  Number,
  Submit
} from "~/components/Form";
import { usePermissions } from "~/hooks";

import type { action } from "~/routes/x+/part+/$itemId.details";
import { itemManufacturingValidator } from "../../items.models";

type ItemManufacturingFormProps = {
  initialValues: z.infer<typeof itemManufacturingValidator>;
  withConfiguration?: boolean;
};

const ItemManufacturingForm = ({
  initialValues,
  withConfiguration = true
}: ItemManufacturingFormProps) => {
  const fetcher = useFetcher<typeof action>();
  const permissions = usePermissions();
  const { t } = useLingui();
  const { itemId } = useParams();

  if (!itemId) throw new Error("Could not find itemId");

  return (
    <Card>
      <ValidatedForm
        method="post"
        validator={itemManufacturingValidator}
        defaultValues={initialValues}
        fetcher={fetcher}
      >
        <CardHeader>
          <CardTitle>
            <Trans>Manufacturing</Trans>
          </CardTitle>
        </CardHeader>
        <CardContent>
          <Hidden name="intent" value="manufacturing" />
          <Hidden name="itemId" />
          <div className="grid grid-cols-1 lg:grid-cols-3 gap-x-8 gap-y-4 w-full">
            <Number
              name="lotSize"
              label={t`Batch Size`}
              termId="part-batch-size"
            />
            <Number
              name="scrapPercentage"
              label={t`Scrap Percent`}
              termId="item-scrap-percent"
              formatOptions={INPUT_FORMAT.percent}
            />
            <Number
              name="leadTime"
              label={t`Lead Time (Days)`}
              termId="item-manufacturing-lead-time"
            />
            {/* <Boolean
              name="manufacturingBlocked"
              label={t`Manufacturing Blocked`}
            /> */}

            {withConfiguration && (
              <Boolean
                name="requiresConfiguration"
                label={t`Configured`}
                termId="item-configured"
                bordered
                description={t`Part is configured for manufacturing`}
                className="col-span-3 max-md:col-span-1"
              />
            )}
            <CustomFormFields table="partReplenishment" />
          </div>
        </CardContent>
        <CardFooter className="flex justify-between">
          <ManufacturingSubmit
            isDisabled={!permissions.can("update", "parts")}
          />
        </CardFooter>
      </ValidatedForm>
    </Card>
  );
};

/**
 * The card's Save. Phones: full width, and disabled until a field in the
 * card changes (precedent: DefaultDisabledSubmit).
 */
function ManufacturingSubmit({ isDisabled }: { isDisabled: boolean }) {
  const { isPhone } = useViewport();
  const { touchedFields } = useFormState();
  const isTouched = Object.keys(touchedFields).length > 0;
  return (
    <Submit
      withBlocker={false}
      isDisabled={isDisabled || (isPhone && !isTouched)}
      className="max-md:w-full"
    >
      <Trans>Save</Trans>
    </Submit>
  );
}

export default ItemManufacturingForm;