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.

4.4 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 { ValidatedForm } from "@carbon/form";
import {
  Button,
  HStack,
  ModalDrawer,
  ModalDrawerBody,
  ModalDrawerContent,
  ModalDrawerFooter,
  ModalDrawerHeader,
  ModalDrawerProvider,
  ModalDrawerTitle,
  VStack
} from "@carbon/react";
import { INPUT_FORMAT, INPUT_STEP } from "@carbon/utils";
import { Trans, useLingui } from "@lingui/react/macro";
import { useFetcher } from "react-router";
import type { z } from "zod";
import {
  Boolean,
  DatePicker,
  Input,
  Number,
  Select,
  Submit
} from "~/components/Form";
import { useCurrencyDecimals, usePermissions } from "~/hooks";
import { rentalAgreementChargeValidator } from "../../sales.models";

type RentalAgreementChargeFormProps = {
  initialValues: z.infer<typeof rentalAgreementChargeValidator>;
  currencyCode: string;
  /** The agreement's lines — a charge belongs to one fleet unit. */
  lineOptions: { value: string; label: string }[];
  /** Where the form posts when it is opened from a page rather than as its
   *  own route; it then also closes on submit. */
  action?: string;
  onClose: () => void;
};

const RentalAgreementChargeForm = ({
  initialValues,
  currencyCode,
  lineOptions,
  action,
  onClose
}: RentalAgreementChargeFormProps) => {
  const { t } = useLingui();
  const permissions = usePermissions();
  const fetcher = useFetcher<{}>();
  const currencyDecimals = useCurrencyDecimals(currencyCode);

  return (
    <ModalDrawerProvider type="modal">
      <ModalDrawer
        open
        onOpenChange={(open) => {
          if (!open) onClose();
        }}
      >
        <ModalDrawerContent>
          <ValidatedForm
            validator={rentalAgreementChargeValidator}
            method="post"
            action={action}
            fetcher={action ? fetcher : undefined}
            defaultValues={initialValues}
            className="flex flex-col h-full"
            onSubmit={action ? onClose : undefined}
          >
            <ModalDrawerHeader>
              <ModalDrawerTitle>
                <Trans>Add Charge</Trans>
              </ModalDrawerTitle>
            </ModalDrawerHeader>
            <ModalDrawerBody>
              <VStack spacing={4}>
                <p className="text-sm text-muted-foreground">
                  <Trans>
                    A variable charge — mileage overage, damage, delivery,
                    cleaning — is billed on the agreement's next invoice.
                  </Trans>
                </p>
                <Select
                  name="rentalAgreementLineId"
                  label={t`Unit`}
                  options={lineOptions}
                />
                <DatePicker name="chargeDate" label={t`Charge Date`} />
                <Input name="description" label={t`Description`} />
                <Number
                  name="amount"
                  label={t`Amount`}
                  step={INPUT_STEP.money(currencyDecimals)}
                  formatOptions={INPUT_FORMAT.money(
                    currencyCode,
                    currencyDecimals
                  )}
                />
                <Number
                  name="taxPercent"
                  label={t`Tax Percent`}
                  minValue={0}
                  maxValue={1}
                  step={INPUT_STEP.percent}
                  formatOptions={INPUT_FORMAT.percent}
                />
                <Boolean
                  name="invoiceSeparately"
                  label={t`Invoice Separately`}
                  description={t`Put this charge on its own invoice, held for review before it is posted. Otherwise it goes on the agreement's next invoice.`}
                />
              </VStack>
            </ModalDrawerBody>
            <ModalDrawerFooter>
              <HStack>
                <Submit isDisabled={!permissions.can("create", "sales")}>
                  <Trans>Save</Trans>
                </Submit>
                <Button size="md" variant="solid" onClick={onClose}>
                  <Trans>Cancel</Trans>
                </Button>
              </HStack>
            </ModalDrawerFooter>
          </ValidatedForm>
        </ModalDrawerContent>
      </ModalDrawer>
    </ModalDrawerProvider>
  );
};

export default RentalAgreementChargeForm;