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.7 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 { requirePermissions } from "@carbon/auth/auth.server";
import { getLogger } from "@carbon/logger";
import { RecordOutlet } from "@carbon/react";
import { redirect } from "@carbon/utils";
import { msg } from "@lingui/core/macro";
import { useLingui } from "@lingui/react/macro";
import type { LoaderFunctionArgs } from "react-router";
import { useLoaderData, useMatches } from "react-router";
import { SetupFrame } from "~/components/Setup";
import { getActivationCutover } from "~/modules/accounting/accounting.service";
import { hasAccountingCutover } from "~/modules/accounting/accounting.utils";
import {
  type ActivationStep,
  ActivationSteps,
  activationSteps
} from "~/modules/accounting/ui/Activation";
import { getCompanySettings } from "~/modules/settings/settings.service";
import type { Handle } from "~/utils/handle";
import { path } from "~/utils/path";

const logger = getLogger("erp", "accounting/activation");

export const handle: Handle = {
  breadcrumb: msg`Setup`,
  to: path.to.accountingActivation
};

/** The enable wizard is for a company with no cutover. Once accounting is set
 *  up it is one-way, so the wizard sends the user to the periods. */
export async function loader({ request }: LoaderFunctionArgs) {
  const { client, companyId } = await requirePermissions(request, {
    view: "accounting"
  });

  const settings = await getCompanySettings(client, companyId);
  if (settings.error) {
    logger.error("Failed to get company settings", {
      companyId,
      error: settings.error
    });
    throw new Response("The company settings could not be loaded.", {
      status: 500
    });
  }
  if (hasAccountingCutover(settings.data)) {
    throw redirect(path.to.accountingPeriods);
  }

  return getActivationCutover(client, companyId, request);
}

/** The current step, from the deepest matched activation route. */
function useCurrentStep(): ActivationStep {
  const matches = useMatches();
  for (const step of activationSteps) {
    if (matches.some((match) => match.id.endsWith(`activation.${step}`))) {
      return step;
    }
  }
  return "chart";
}

export default function AccountingActivationRoute() {
  const { t } = useLingui();
  const { cutoverDate } = useLoaderData<typeof loader>();
  const step = useCurrentStep();

  return (
    <SetupFrame
      title={t`Set Up Accounting`}
      subtitle={t`Open the books at a cutover date. Enabling accounting is one-way.`}
      step={step}
      steps={<ActivationSteps current={step} cutoverDate={cutoverDate} />}
    >
      <RecordOutlet />
    </SetupFrame>
  );
}