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.

7.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 { error } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { RecordOutlet, VStack } from "@carbon/react";
import { datetime, defaultReportRange, redirect } from "@carbon/utils";
import { msg } from "@lingui/core/macro";
import { useState } from "react";
import type { LoaderFunctionArgs } from "react-router";
import { useLoaderData } from "react-router";
import { financialReportParamsValidator } from "~/modules/accounting/accounting.models";
import {
  getCompaniesInGroup,
  getConsolidatedBalancesForReport,
  getFinancialStatementBalances,
  getFiscalYearSettings,
  translateCompanyBalances
} from "~/modules/accounting/accounting.service";
import { selectReportCompanyIds } from "~/modules/accounting/accounting.utils";
import type { Chart } from "~/modules/accounting/types";
import {
  exportTrialBalance,
  ReportFilters,
  TrialBalanceTree
} from "~/modules/accounting/ui/Reports";
import { months } from "~/modules/shared/shared.models";
import { getCompanyTimeZone } from "~/modules/shared/shared.service";
import type { Handle } from "~/utils/handle";
import { path } from "~/utils/path";
import { revalidateOnSearchChange } from "~/utils/revalidate";

export const handle: Handle = {
  breadcrumb: msg`Trial Balance`,
  to: path.to.trialBalance
};

export const shouldRevalidate = revalidateOnSearchChange;

export async function loader({ request }: LoaderFunctionArgs) {
  const { client, companyId, companyGroupId } = await requirePermissions(
    request,
    {
      view: "accounting",
      role: "employee"
    }
  );

  const url = new URL(request.url);
  // Invalid params fall back to defaults — a bad bookmark must not 500
  const parsed = financialReportParamsValidator.safeParse(
    Object.fromEntries(url.searchParams.entries())
  );
  const companiesParam = parsed.success
    ? (parsed.data.companies ?? null)
    : null;
  const startDateParam = parsed.success
    ? (parsed.data.startDate ?? null)
    : null;
  const endDateParam = parsed.success ? (parsed.data.endDate ?? null) : null;
  const showTranslatedParam = parsed.success
    ? parsed.data.showTranslated
    : false;

  const [companies, fiscalYearSettings] = await Promise.all([
    getCompaniesInGroup(client, companyGroupId),
    getFiscalYearSettings(client, companyId)
  ]);
  const fiscalStartMonth =
    months.indexOf(fiscalYearSettings.data?.startMonth ?? "January") + 1;
  if (companies.error) {
    throw redirect(
      path.to.accounting,
      await flash(
        request,
        error(companies.error, "Failed to load report companies")
      )
    );
  }
  const companiesList = companies.data ?? [];
  const parentCompany = companiesList.find((c) => !c.parentCompanyId);
  const parentCurrency = parentCompany?.baseCurrencyCode ?? null;

  const selectedCompanyIds = selectReportCompanyIds(
    companiesParam,
    companiesList.map((c) => c.id),
    companyId
  );
  if (!selectedCompanyIds) {
    throw new Response("Company not found", { status: 404 });
  }
  const isMultiCompany = selectedCompanyIds.length > 1;

  // Default to the trailing six months, matching every other range report
  // (previously trial balance alone defaulted to all-time).
  const range = defaultReportRange(
    endDateParam ??
      datetime.today(await getCompanyTimeZone(client, companyId)).toString()
  );
  const startDate = startDateParam ?? range.startDate;
  const endDate = endDateParam ?? range.endDate;

  if (isMultiCompany && parentCurrency) {
    const consolidated = await getConsolidatedBalancesForReport(
      client,
      companyGroupId,
      selectedCompanyIds,
      parentCurrency,
      endDate,
      startDate
    );

    if (consolidated.error || !consolidated.data) {
      throw redirect(
        path.to.accounting,
        await flash(
          request,
          error(
            consolidated.error,
            "Failed to translate a subsidiary's balances"
          )
        )
      );
    }

    return {
      trialBalance: consolidated.data as (Chart & {
        translatedBalance?: number;
        exchangeRate?: number;
      })[],
      companies: companiesList,
      selectedCompanyIds,
      showTranslated: true,
      isMultiCompany: true,
      isForeignCurrency: false,
      parentCurrency,
      fiscalStartMonth
    };
  }

  // Single company
  const selectedCompanyId = selectedCompanyIds[0];
  const balances = await getFinancialStatementBalances(
    client,
    companyGroupId,
    selectedCompanyId,
    { startDate, endDate }
  );

  if (balances.error) {
    throw redirect(
      path.to.accounting,
      await flash(
        request,
        error(balances.error, "Failed to load trial balance")
      )
    );
  }

  const selectedCompany = companiesList.find((c) => c.id === selectedCompanyId);
  const isForeignCurrency =
    !!parentCurrency &&
    !!selectedCompany?.baseCurrencyCode &&
    selectedCompany.baseCurrencyCode !== parentCurrency;

  let accounts = (balances.data ?? []) as (Chart & {
    translatedBalance?: number;
    exchangeRate?: number;
  })[];

  if (showTranslatedParam && isForeignCurrency && parentCurrency) {
    const translation = await translateCompanyBalances(
      client,
      companyGroupId,
      selectedCompanyId!,
      parentCurrency,
      endDate,
      startDate,
      balances.data ?? []
    );

    // Rendering untranslated numbers under a translated header with no signal
    // is worse than refusing — surface the failure.
    if (translation.error) {
      throw redirect(
        path.to.accounting,
        await flash(
          request,
          error(translation.error, "Failed to translate balances")
        )
      );
    }

    if (translation.data) {
      const translationMap = new Map(
        translation.data.map((t) => [t.accountId, t])
      );

      accounts = accounts.map((account) => {
        const t = translationMap.get(account.id);
        if (t) {
          return {
            ...account,
            translatedBalance: Number(t.translatedBalance),
            exchangeRate: Number(t.exchangeRate)
          };
        }
        return account;
      });
    }
  }

  return {
    trialBalance: accounts,
    companies: companiesList,
    selectedCompanyIds,
    showTranslated: showTranslatedParam && isForeignCurrency,
    isMultiCompany: false,
    isForeignCurrency,
    parentCurrency,
    fiscalStartMonth
  };
}

export default function TrialBalanceRoute() {
  const {
    trialBalance,
    companies,
    selectedCompanyIds,
    showTranslated,
    isMultiCompany,
    isForeignCurrency,
    parentCurrency,
    fiscalStartMonth
  } = useLoaderData<typeof loader>();
  const [search, setSearch] = useState("");

  return (
    <VStack spacing={0} className="h-full">
      <ReportFilters
        companies={companies}
        selectedCompanyIds={selectedCompanyIds}
        isMultiCompany={isMultiCompany}
        isForeignCurrency={isForeignCurrency}
        parentCurrency={parentCurrency}
        fiscalStartMonth={fiscalStartMonth}
        onDownload={() =>
          exportTrialBalance({
            accounts: trialBalance,
            showTranslated,
            parentCurrency,
            search,
            filename: "trial-balance.csv"
          })
        }
        search={search}
        onSearchChange={setSearch}
      />
      <TrialBalanceTree
        data={trialBalance}
        showTranslated={showTranslated}
        parentCurrency={parentCurrency}
        search={search}
        ledgerPath={path.to.trialBalanceLedger}
      />
      <RecordOutlet />
    </VStack>
  );
}