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.

16.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 { assertIsPost, error, notFound, success } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { validationError, validator } from "@carbon/form";
import type { FundingSource } from "@carbon/utils";
import { fundingScopeOf, getErrorMessage, redirect } from "@carbon/utils";
import { msg } from "@lingui/core/macro";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import { data, useLoaderData } from "react-router";
import { DocumentPage, DocumentSidebar } from "~/components/DocumentPage";
import {
  isPaymentLocked,
  paymentValidator
} from "~/modules/invoicing/invoicing.models";
import {
  checkDepositDocument,
  getAvailableCreditsForParty,
  getAvailableOnAccountCreditSources,
  getDepositDocuments,
  getInvoiceSettlements,
  getOpenPurchaseInvoicesForSupplier,
  getOpenReimbursementsForEmployee,
  getOpenSalesInvoicesForCustomer,
  getPayment,
  getPaymentCurrencyConfiguration,
  getSettlementRelatedItems,
  getStagedCreditsForPayment,
  upsertPayment
} from "~/modules/invoicing/invoicing.service";
import {
  AvailableCreditsTable,
  PaymentApplications,
  PaymentApplyTable,
  PaymentDocuments,
  PaymentForm,
  PaymentHeader
} from "~/modules/invoicing/ui";
import { setCustomFields } from "~/utils/form";
import { detailBreadcrumb, type Handle } from "~/utils/handle";
import { path } from "~/utils/path";

export const handle: Handle = {
  breadcrumb: detailBreadcrumb(
    { breadcrumb: msg`Payments`, to: path.to.payments },
    (data) => data?.payment?.paymentId
  ),
  module: "invoicing"
};

export async function loader({ request, params }: LoaderFunctionArgs) {
  const { client, companyId } = await requirePermissions(request, {
    view: "invoicing"
  });
  const { paymentId } = params;
  if (!paymentId) throw notFound("Missing paymentId");
  const [payment, applications] = await Promise.all([
    getPayment(client, paymentId, companyId),
    getInvoiceSettlements(client, companyId, paymentId)
  ]);
  if (payment.error || !payment.data)
    throw redirect(
      path.to.payments,
      await flash(request, error(payment.error, "Failed to load payment"))
    );
  try {
    if (applications.error) throw new Error(applications.error.message);
    const [configuration, depositDocuments] = await Promise.all([
      getPaymentCurrencyConfiguration(
        client,
        companyId,
        payment.data.currencyCode
      ),
      // The payment's own document stays listed even once it is closed.
      getDepositDocuments(client, companyId, [payment.data.salesOrderId])
    ]);
    let openInvoices: NonNullable<
      Awaited<ReturnType<typeof getOpenSalesInvoicesForCustomer>>["data"]
    > = [];
    let funding = {
      sources: [] as FundingSource[],
      availableDocumentAmount: 0,
      availableBaseAmount: 0
    };
    let availableCredits: NonNullable<
      Awaited<ReturnType<typeof getAvailableCreditsForParty>>["data"]
    > = [];
    let stagedCredits: NonNullable<
      Awaited<ReturnType<typeof getStagedCreditsForPayment>>["data"]
    > = [];
    // An employee payee settles Posted reimbursements, never invoices or memos:
    // it is resolved BEFORE the customer-XOR-supplier check below, which a
    // three-way party can no longer satisfy. There is no on-account credit and
    // no credit memo to draw on, so `funding`, `availableCredits` and
    // `stagedCredits` all stay empty — exactly what
    // `replaceInvoiceSettlements`' employee arm assumes.
    if (payment.data.status === "Draft" && payment.data.employeeId) {
      const reimbursements = await getOpenReimbursementsForEmployee(
        client,
        companyId,
        payment.data.employeeId,
        payment.data.currencyCode
      );
      if (reimbursements.error) throw reimbursements.error;
      openInvoices = (reimbursements.data ?? []).map((reimbursement) => ({
        id: reimbursement.id,
        invoiceId: reimbursement.reimbursementId,
        dateDue: null,
        dateIssued: null,
        paymentTermId: null,
        currencyCode: reimbursement.currencyCode,
        exchangeRate: reimbursement.exchangeRate,
        totalAmount: reimbursement.totalAmount,
        balance: reimbursement.balance,
        remainingDocument: reimbursement.remainingDocument,
        status: "Posted",
        rentalAgreementIds: [],
        salesOrderIds: []
      }));
    } else if (payment.data.status === "Draft") {
      const isAR = Boolean(payment.data.customerId);
      const isRefund = isAR !== (payment.data.paymentType === "Receipt");
      const partyId = isAR ? payment.data.customerId : payment.data.supplierId;
      if (
        !partyId ||
        Boolean(payment.data.customerId) === Boolean(payment.data.supplierId)
      )
        throw new Error("Payment requires exactly one customer or supplier");
      if (isRefund) {
        const memos = await getAvailableCreditsForParty(
          client,
          companyId,
          isAR
            ? { side: "sales", customerId: partyId }
            : { side: "purchase", supplierId: partyId },
          paymentId,
          payment.data.currencyCode
        );
        if (memos.error) throw memos.error;
        openInvoices = (memos.data ?? []).map((memo) => ({
          id: memo.id,
          invoiceId: memo.memoId,
          dateDue: null,
          dateIssued: null,
          paymentTermId: null,
          currencyCode: memo.currencyCode,
          exchangeRate: memo.exchangeRate,
          totalAmount: memo.amount,
          balance: memo.remaining,
          remainingDocument: memo.remainingDocument,
          status: "Posted",
          rentalAgreementIds: [],
          salesOrderIds: []
        }));
      } else {
        const [invoices, credit, credits, staged] = await Promise.all([
          isAR
            ? getOpenSalesInvoicesForCustomer(
                client,
                companyId,
                partyId,
                payment.data.currencyCode
              )
            : getOpenPurchaseInvoicesForSupplier(
                client,
                companyId,
                partyId,
                payment.data.currencyCode
              ),
          getAvailableOnAccountCreditSources(
            client,
            companyId,
            isAR
              ? { paymentType: "Receipt", customerId: partyId }
              : { paymentType: "Disbursement", supplierId: partyId },
            payment.data.currencyCode
          ),
          getAvailableCreditsForParty(
            client,
            companyId,
            isAR
              ? { side: "sales", customerId: partyId }
              : { side: "purchase", supplierId: partyId },
            paymentId,
            payment.data.currencyCode
          ),
          getStagedCreditsForPayment(
            client,
            paymentId,
            isAR ? "sales" : "purchase",
            companyId
          )
        ]);
        const loadError =
          invoices.error ?? credit.error ?? credits.error ?? staged.error;
        if (loadError) throw loadError;
        if (!credit.data) throw new Error("Unable to load payment funding");
        openInvoices = invoices.data ?? [];
        funding = credit.data;
        availableCredits = credits.data ?? [];
        stagedCredits = staged.data ?? [];
      }
    }
    return {
      payment: payment.data,
      applications: applications.data ?? [],
      openInvoices,
      funding,
      availableCredits,
      stagedCredits,
      depositDocuments,
      ...configuration,
      relatedItems: getSettlementRelatedItems(client, companyId, {
        journalId: payment.data.journalId,
        targets: applications.data ?? [],
        appliedViaPaymentId: payment.data.id,
        appliedViaPaymentStaged: payment.data.status === "Draft"
      })
    };
  } catch (e) {
    throw redirect(
      path.to.payments,
      await flash(
        request,
        // The log keeps the fixed text so occurrences group as one error;
        // the person sees the cause, which a service returns as a plain
        // `{ message }` object rather than an `Error`.
        {
          ...error(e, "Failed to load payment balances"),
          message: getErrorMessage(e, "Failed to load payment balances")
        }
      )
    );
  }
}

export async function action({ request, params }: ActionFunctionArgs) {
  assertIsPost(request);
  const { client, companyId, userId } = await requirePermissions(request, {
    update: "invoicing"
  });
  const { paymentId } = params;
  if (!paymentId) throw notFound("Missing paymentId");

  const formData = await request.formData();
  const validation = await validator(paymentValidator).validate(formData);
  if (validation.error) {
    return validationError(validation.error);
  }

  // Only Draft payments are editable; Posted/Voided are immutable.
  const existing = await getPayment(client, paymentId, companyId);
  if (existing.error || !existing.data) {
    throw redirect(
      path.to.payments,
      await flash(request, error(existing.error, "Failed to load payment"))
    );
  }
  if (existing.data.status !== "Draft") {
    throw redirect(
      path.to.payment(paymentId),
      await flash(request, error(null, "Only draft payments can be edited"))
    );
  }

  // The deposit ids arrive as hidden fields: never trust them to name this
  // company's, this customer's, open document. The payment's own saved link
  // stays valid after its document closes.
  const depositError = await checkDepositDocument(
    client,
    companyId,
    validation.data,
    existing.data
  );
  if (depositError) {
    return validationError({
      fieldErrors: { depositDocument: depositError }
    });
  }

  try {
    await getPaymentCurrencyConfiguration(
      client,
      companyId,
      validation.data.currencyCode
    );
  } catch (e) {
    throw redirect(
      path.to.payment(paymentId),
      await flash(request, {
        ...error(e, "Invalid currency configuration"),
        message: getErrorMessage(e, "Invalid currency configuration")
      })
    );
  }

  const { id: _omitId, ...paymentData } = validation.data;
  const update = await upsertPayment(client, {
    ...paymentData,
    id: paymentId,
    updatedBy: userId,
    customFields: setCustomFields(formData)
  });
  if (update.error) {
    return data(
      {},
      await flash(request, error(update.error, "Failed to update payment"))
    );
  }

  throw redirect(
    path.to.payment(paymentId),
    await flash(request, success("Payment updated"))
  );
}

export default function PaymentDetailRoute() {
  const {
    payment,
    applications,
    openInvoices,
    funding,
    baseCurrencyCode,
    currencyDecimals,
    availableCredits,
    stagedCredits,
    depositDocuments
  } = useLoaderData<typeof loader>();
  const locked = isPaymentLocked(payment.status);
  const isReimbursement = Boolean(payment.employeeId);
  const side: "sales" | "purchase" = payment.customerId ? "sales" : "purchase";
  // A reimbursement payout is always cash OUT against the employee-payable
  // control account — never a refund, whatever the trade-side arithmetic says.
  const isRefund =
    !isReimbursement &&
    (side === "sales") !== (payment.paymentType === "Receipt");
  // A deposit receipt applies only to its own document's invoices; a deposit
  // REFUND (a disbursement) targets memos and is not scoped.
  const depositScope =
    side === "sales" && !isRefund ? fundingScopeOf(payment) : null;
  const paymentScope = depositScope && {
    ...depositScope,
    readableId:
      depositDocuments.find((document) => document.id === depositScope.id)
        ?.readableId ?? null
  };

  const initialValues = {
    id: payment.id,
    paymentId: payment.paymentId,
    paymentType: payment.paymentType,
    customerId: payment.customerId ?? "",
    supplierId: payment.supplierId ?? "",
    employeeId: payment.employeeId ?? "",
    paymentDate: payment.paymentDate,
    currencyCode: payment.currencyCode ?? "",
    exchangeRate: Number(payment.exchangeRate),
    totalAmount: Number(payment.totalAmount ?? 0),
    bankAccount: payment.bankAccount ?? "",
    reference: payment.reference ?? "",
    memo: payment.memo ?? "",
    salesOrderId: payment.salesOrderId ?? "",
    rentalAgreementId: payment.rentalAgreementId ?? "",
    status: payment.status ?? undefined
  };

  return (
    <DocumentPage
      header={<PaymentHeader />}
      sidebar={
        <DocumentSidebar
          documents={<PaymentDocuments />}
          activity={{
            entityType: "payment",
            entityId: payment.id,
            refreshKey: `${payment.updatedAt ?? ""}:${payment.status}`
          }}
        />
      }
    >
      <PaymentForm
        key={payment.id}
        initialValues={initialValues}
        depositDocuments={depositDocuments}
      />
      <PaymentApplications
        applications={applications}
        paymentTotal={Number(payment.totalAmount)}
        paymentCurrency={payment.currencyCode}
        baseCurrency={baseCurrencyCode}
        isRefund={isRefund}
        isReimbursement={isReimbursement}
      />

      {!locked && (
        <PaymentApplyTable
          key={`${payment.id}:${side}:${payment.customerId ?? payment.supplierId ?? payment.employeeId}:${payment.currencyCode}:${payment.paymentType}`}
          isRefund={isRefund}
          isReimbursement={isReimbursement}
          paymentId={payment.id}
          paymentType={payment.paymentType}
          paymentCurrency={payment.currencyCode}
          baseCurrency={baseCurrencyCode}
          currencyDecimals={currencyDecimals}
          priorSources={funding.sources}
          paymentScope={paymentScope}
          paymentTotal={Number(payment.totalAmount)}
          paymentExchangeRate={Number(payment.exchangeRate)}
          availableCredit={funding.availableDocumentAmount}
          openInvoices={(openInvoices ?? []).map((inv) => ({
            id: inv.id,
            invoiceId: inv.invoiceId ?? inv.id,
            dateDue: inv.dateDue,
            currencyCode: inv.currencyCode,
            exchangeRate: Number(inv.exchangeRate),
            totalAmount: Number(inv.totalAmount ?? 0),
            balance: Number(inv.balance ?? 0),
            remainingDocument: inv.remainingDocument,
            status: inv.status,
            rentalAgreementIds: inv.rentalAgreementIds,
            salesOrderIds: inv.salesOrderIds
          }))}
          existingApplications={applications.map((a) => ({
            targetSalesInvoiceId: a.targetSalesInvoiceId,
            targetPurchaseInvoiceId: a.targetPurchaseInvoiceId,
            targetMemoId: a.targetMemoId,
            targetReimbursementId: a.targetReimbursementId,
            sourceAmount: a.sourceAmount,
            sourcePaymentId: a.sourcePaymentId,
            appliedAmount: Number(a.appliedAmount),
            discountAmount: Number(a.discountAmount),
            writeOffAmount: Number(a.writeOffAmount),
            targetExchangeRate: Number(a.targetExchangeRate),
            sourceExchangeRate: Number(a.sourceExchangeRate),
            appliedDate: a.appliedDate
          }))}
        />
      )}

      {!locked && !isRefund && availableCredits.length > 0 && (
        <AvailableCreditsTable
          // The rows are the user's draft over these credits: another party,
          // currency or set of credits on the same payment starts a new one.
          key={`${side}:${payment.customerId ?? payment.supplierId ?? payment.employeeId}:${payment.currencyCode}:${availableCredits.map((c) => `${c.id}=${c.remaining}`).join(",")}`}
          paymentId={payment.id}
          side={side}
          currency={baseCurrencyCode}
          documentCurrency={payment.currencyCode}
          documentDecimals={currencyDecimals}
          credits={availableCredits.map((c) => ({
            id: c.id,
            memoId: c.memoId,
            direction: c.direction,
            currencyCode: c.currencyCode,
            exchangeRate: Number(c.exchangeRate),
            remaining: Number(c.remaining),
            remainingDocument: c.remainingDocument
          }))}
          openInvoices={(openInvoices ?? []).map((inv) => ({
            id: inv.id,
            invoiceId: inv.invoiceId ?? inv.id,
            exchangeRate: Number(inv.exchangeRate),
            balance: Number(inv.balance ?? 0),
            remainingDocument: inv.remainingDocument
          }))}
          staged={stagedCredits}
        />
      )}
    </DocumentPage>
  );
}