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.3 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 { useAction } from "@carbon/query";
import {
  Button,
  HStack,
  ModalDrawer,
  ModalDrawerBody,
  ModalDrawerContent,
  ModalDrawerFooter,
  ModalDrawerHeader,
  ModalDrawerProvider,
  ModalDrawerTitle,
  toast,
  VStack
} from "@carbon/react";
import { Trans, useLingui } from "@lingui/react/macro";
import { Combobox, Hidden, Submit } from "~/components/Form";
import { usePermissions } from "~/hooks";
import { path } from "~/utils/path";
import { moveAccountValidator } from "../../accounting.models";

type MoveAccountFormProps = {
  accountId: string;
  accountName: string;
  groupAccounts: {
    id: string;
    name: string;
    incomeBalance: string;
    class: string | null;
  }[];
  currentParentId?: string | null;
  open?: boolean;
  onClose: () => void;
};

const MoveAccountForm = ({
  accountId,
  accountName,
  groupAccounts,
  currentParentId,
  open = true,
  onClose
}: MoveAccountFormProps) => {
  const { t } = useLingui();
  const permissions = usePermissions();
  // A refusal is toasted by useAction.
  const fetcher = useAction({
    onSuccess: () => {
      toast.success(t`Moved account`);
      onClose();
    }
  });

  const isDisabled = !permissions.can("update", "accounting");

  return (
    <ModalDrawerProvider type="modal">
      <ModalDrawer
        open={open}
        onOpenChange={(open) => {
          if (!open) onClose?.();
        }}
      >
        <ModalDrawerContent>
          <ValidatedForm
            validator={moveAccountValidator}
            method="post"
            action={path.to.moveChartOfAccount(accountId)}
            defaultValues={{
              id: accountId,
              parentId: currentParentId ?? undefined
            }}
            fetcher={fetcher}
            className="flex flex-col h-full"
          >
            <ModalDrawerHeader>
              <ModalDrawerTitle>
                <Trans>Move {accountName}</Trans>
              </ModalDrawerTitle>
            </ModalDrawerHeader>
            <ModalDrawerBody>
              <Hidden name="id" />
              <VStack spacing={4}>
                <p className="text-sm text-muted-foreground">
                  <Trans>Select a new parent group for this account.</Trans>
                </p>
                <Combobox
                  name="parentId"
                  label={t`Move to Group`}
                  options={groupAccounts.map((a) => ({
                    label: a.name,
                    value: a.id
                  }))}
                />
              </VStack>
            </ModalDrawerBody>
            <ModalDrawerFooter>
              <HStack>
                <Submit isDisabled={isDisabled}>
                  <Trans>Move</Trans>
                </Submit>
                <Button size="md" variant="solid" onClick={() => onClose?.()}>
                  <Trans>Cancel</Trans>
                </Button>
              </HStack>
            </ModalDrawerFooter>
          </ValidatedForm>
        </ModalDrawerContent>
      </ModalDrawer>
    </ModalDrawerProvider>
  );
};

export default MoveAccountForm;