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.

- approval changes

Gaurav Bhattfc52debe13da
diff --git a/apps/erp/app/modules/settings/ui/Approvals/ApprovalRules.tsx b/apps/erp/app/modules/settings/ui/Approvals/ApprovalRules.tsx
index 85897b78e4..2a227d48ee 100644
--- a/apps/erp/app/modules/settings/ui/Approvals/ApprovalRules.tsx
+++ b/apps/erp/app/modules/settings/ui/Approvals/ApprovalRules.tsx
@@ -1,4 +1,7 @@
import {
+ Alert,
+ AlertDescription,
+ AlertTitle,
Button,
Card,
CardContent,
@@ -10,15 +13,16 @@ import {
VStack
} from "@carbon/react";
import { Trans } from "@lingui/react/macro";
-import { memo } from "react";
-import { LuPlus } from "react-icons/lu";
+import { memo, useMemo } from "react";
+import { LuPlus, LuTriangleAlert } from "react-icons/lu";
import { Link } from "react-router";
import { Empty } from "~/components";
-import { usePermissions } from "~/hooks";
+import { useCurrencyFormatter, usePermissions } from "~/hooks";
import {
type ApprovalRule,
approvalDocumentTypesWithAmounts
} from "~/modules/shared";
+import { topTierExplicitMax } from "~/modules/shared/approval-rules.coverage";
import { path } from "~/utils/path";
import ApprovalRuleCard from "./ApprovalRuleCard";
@@ -32,6 +36,11 @@ const ApprovalRules = memo(
({ poRules, qdRules, supplierRules }: ApprovalRulesProps) => {
const permissions = usePermissions();
const canCreate = permissions.can("update", "settings");
+ const currencyFormatter = useCurrencyFormatter();
+ const poTopTierGap = useMemo(
+ () => topTierExplicitMax(poRules, "purchaseOrder"),
+ [poRules]
+ );
return (
<ScrollArea className="h-full w-full">
@@ -67,6 +76,22 @@ const ApprovalRules = memo(
</div>
</CardHeader>
<CardContent>
+ {poTopTierGap != null && (
+ <Alert variant="warning" className="mb-4">
+ <LuTriangleAlert className="h-4 w-4" />
+ <AlertTitle>
+ <Trans>Highest-tier rule has a maximum</Trans>
+ </AlertTitle>
+ <AlertDescription>
+ <Trans>
+ Purchase orders above{" "}
+ {currencyFormatter.format(poTopTierGap)} will not
+ require approval. Edit the top-tier rule and clear the
+ Maximum Amount, or add a higher rule with no maximum.
+ </Trans>
+ </AlertDescription>
+ </Alert>
+ )}
{poRules.length === 0 ? (
<Empty className="my-4" />
) : (
diff --git a/apps/erp/app/modules/shared/approval-rules.coverage.ts b/apps/erp/app/modules/shared/approval-rules.coverage.ts
new file mode 100644
index 0000000000..d298548ef3
--- /dev/null
+++ b/apps/erp/app/modules/shared/approval-rules.coverage.ts
@@ -0,0 +1,96 @@
+// Tiered-approval coverage check.
+//
+// Each approval rule represents a half-open range [lowerBoundAmount,
+// upperBoundAmount). The "top tier" — the rule(s) with the highest
+// lowerBoundAmount for a given documentType — must have at least one rule
+// with a null upperBoundAmount so amounts above the top minimum still
+// route to an approver. Without this guarantee, a $3M PO can silently
+// bypass approval when all rules have explicit ceilings.
+//
+// Used by:
+// - approval-rules.new.tsx and approval-rules.$id.tsx (save-time block)
+// - ApprovalRules.tsx (list-view warning banner)
+
+import type { ApprovalRule } from "./types";
+
+type Candidate = {
+ id?: string | null;
+ lowerBoundAmount: number;
+ upperBoundAmount: number | null;
+};
+
+type CoverageInput = {
+ existingRules: Pick<
+ ApprovalRule,
+ "id" | "documentType" | "lowerBoundAmount" | "upperBoundAmount"
+ >[];
+ documentType: ApprovalRule["documentType"];
+ candidate: Candidate;
+};
+
+/**
+ * Returns true if, after applying the candidate save, at least one rule
+ * at the highest `lowerBoundAmount` for the document type has a null
+ * `upperBoundAmount`. Returns false when the save would leave a gap
+ * above the top tier.
+ */
+export function topTierWouldBeUnbounded({
+ existingRules,
+ documentType,
+ candidate
+}: CoverageInput): boolean {
+ const others = existingRules.filter(
+ (r) => r.documentType === documentType && r.id !== candidate.id
+ );
+
+ const merged: Pick<ApprovalRule, "lowerBoundAmount" | "upperBoundAmount">[] =
+ [
+ ...others.map((r) => ({
+ lowerBoundAmount: r.lowerBoundAmount ?? 0,
+ upperBoundAmount: r.upperBoundAmount ?? null
+ })),
+ {
+ lowerBoundAmount: candidate.lowerBoundAmount,
+ upperBoundAmount: candidate.upperBoundAmount
+ }
+ ];
+
+ const highestLower = merged.reduce(
+ (max, r) => Math.max(max, r.lowerBoundAmount ?? 0),
+ 0
+ );
+ return merged
+ .filter((r) => (r.lowerBoundAmount ?? 0) === highestLower)
+ .some((r) => r.upperBoundAmount == null);
+}
+
+/**
+ * Read-only variant used by the list-view banner. No candidate — just
+ * checks the current set of rules. Returns the maximum `upperBoundAmount`
+ * at the highest tier if coverage is incomplete, or null when the top is
+ * unbounded.
+ */
+export function topTierExplicitMax(
+ rules: Pick<
+ ApprovalRule,
+ "documentType" | "lowerBoundAmount" | "upperBoundAmount"
+ >[],
+ documentType: ApprovalRule["documentType"]
+): number | null {
+ const forType = rules.filter((r) => r.documentType === documentType);
+ if (forType.length === 0) return null;
+
+ const highestLower = forType.reduce(
+ (max, r) => Math.max(max, r.lowerBoundAmount ?? 0),
+ 0
+ );
+ const atHighest = forType.filter(
+ (r) => (r.lowerBoundAmount ?? 0) === highestLower
+ );
+ if (atHighest.some((r) => r.upperBoundAmount == null)) return null;
+
+ return atHighest.reduce(
+ (max, r) => Math.max(max, r.upperBoundAmount ?? 0),
+ 0
+ );
+}
diff --git a/apps/erp/app/routes/x+/settings+/approval-rules.$id.tsx b/apps/erp/app/routes/x+/settings+/approval-rules.$id.tsx
index 8fe9beed64..4428598d5c 100644
--- a/apps/erp/app/routes/x+/settings+/approval-rules.$id.tsx
+++ b/apps/erp/app/routes/x+/settings+/approval-rules.$id.tsx
@@ -13,6 +13,7 @@ import {
getApprovalRules,
upsertApprovalRule
} from "~/modules/shared";
+import { topTierWouldBeUnbounded } from "~/modules/shared/approval-rules.coverage";
import { getParams, path } from "~/utils/path";
@@ -76,6 +77,29 @@ export async function action({ request, params }: ActionFunctionArgs) {
);
}
+ // Highest-tier coverage check: enforce that the highest-lowerBoundAmount
+ // tier always has at least one rule with no upper bound.
+ if (validation.data.documentType === "purchaseOrder") {
+ if (
+ !topTierWouldBeUnbounded({
+ existingRules: rules.data ?? [],
+ documentType: "purchaseOrder",
+ candidate: {
+ id,
+ lowerBoundAmount: validation.data.lowerBoundAmount ?? 0,
+ upperBoundAmount: validation.data.upperBoundAmount ?? null
+ }
+ })
+ ) {
+ return validationError({
+ fieldErrors: {
+ upperBoundAmount:
+ "The highest tier must leave the maximum empty so it covers all amounts above its minimum."
+ }
+ });
+ }
+ }
+
const result = await upsertApprovalRule(serviceRole, {
id,
updatedBy: userId,
@@ -84,7 +108,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
approverGroupIds: validation.data.approverGroupIds || [],
defaultApproverId: validation.data.defaultApproverId,
lowerBoundAmount: validation.data.lowerBoundAmount ?? 0,
- upperBoundAmount: validation.data.upperBoundAmount ?? null
+ upperBoundAmount: validation.data.upperBoundAmount
});
if (result.error) {
diff --git a/apps/erp/app/routes/x+/settings+/approval-rules.new.tsx b/apps/erp/app/routes/x+/settings+/approval-rules.new.tsx
index 79e29eba64..46ccac3792 100644
--- a/apps/erp/app/routes/x+/settings+/approval-rules.new.tsx
+++ b/apps/erp/app/routes/x+/settings+/approval-rules.new.tsx
@@ -9,8 +9,10 @@ import { ApprovalRuleForm } from "~/modules/settings";
import {
type ApprovalDocumentType,
approvalRuleValidator,
+ getApprovalRules,
upsertApprovalRule
} from "~/modules/shared";
+import { topTierWouldBeUnbounded } from "~/modules/shared/approval-rules.coverage";
import { path } from "~/utils/path";
export async function loader({ request }: LoaderFunctionArgs) {
@@ -51,6 +53,31 @@ export async function action({ request }: ActionFunctionArgs) {
return validationError(validation.error);
}
+ // Highest-tier coverage check: if this rule would be at (or tied for)
+ // the highest lowerBoundAmount for its doc type AND it has a max set,
+ // make sure at least one rule at that tier remains unbounded above —
+ // otherwise amounts above the new top would silently bypass approval.
+ if (validation.data.documentType === "purchaseOrder") {
+ const existing = await getApprovalRules(serviceRole, companyId);
+ if (
+ !topTierWouldBeUnbounded({
+ existingRules: existing.data ?? [],
+ documentType: "purchaseOrder",
+ candidate: {
+ lowerBoundAmount: validation.data.lowerBoundAmount ?? 0,
+ upperBoundAmount: validation.data.upperBoundAmount ?? null
+ }
+ })
+ ) {
+ return validationError({
+ fieldErrors: {
+ upperBoundAmount:
+ "The highest tier must leave the maximum empty so it covers all amounts above its minimum."
+ }
+ });
+ }
+ }
+
const result = await upsertApprovalRule(serviceRole, {
createdBy: userId,
companyId,
@@ -59,7 +86,7 @@ export async function action({ request }: ActionFunctionArgs) {
approverGroupIds: validation.data.approverGroupIds || [],
defaultApproverId: validation.data.defaultApproverId,
lowerBoundAmount: validation.data.lowerBoundAmount ?? 0,
- upperBoundAmount: validation.data.upperBoundAmount ?? null
+ upperBoundAmount: validation.data.upperBoundAmount
});
if (result.error) {
diff --git a/packages/locale/locales/de/erp.po b/packages/locale/locales/de/erp.po
index 7b91bd861a..ceeeb9beb9 100644
--- a/packages/locale/locales/de/erp.po
+++ b/packages/locale/locales/de/erp.po
@@ -3744,6 +3744,9 @@ msgstr "Spalten ausblenden, fixieren und neu anordnen"
msgid "Higher priority wins ties and applies first for markups"
msgstr "Höhere Priorität gewinnt bei Gleichständen und wird zuerst für Aufschläge angewendet"
+msgid "Highest-tier rule has a maximum"
+msgstr ""
+
msgid "History"
msgstr "Verlauf"
@@ -5943,6 +5946,10 @@ msgstr "Bestellung erfolgreich entfernt"
msgid "Purchase Orders"
msgstr "Bestellungen"
+#. placeholder {0}: currencyFormatter.format(poTopTierGap)
+msgid "Purchase orders above {0} will not require approval. Edit the top-tier rule and clear the Maximum Amount, or add a higher rule with no maximum."
+msgstr ""
+
msgid "Purchase orders required"
msgstr "Bestellungen erforderlich"
diff --git a/packages/locale/locales/en/erp.po b/packages/locale/locales/en/erp.po
index ed4fde2831..6742421500 100644
--- a/packages/locale/locales/en/erp.po
+++ b/packages/locale/locales/en/erp.po
@@ -3744,6 +3744,9 @@ msgstr "Hide, pin and reorder columns"
msgid "Higher priority wins ties and applies first for markups"
msgstr "Higher priority wins ties and applies first for markups"
+msgid "Highest-tier rule has a maximum"
+msgstr "Highest-tier rule has a maximum"
+
msgid "History"
msgstr "History"
@@ -5943,6 +5946,10 @@ msgstr "Purchase order removed successfully"
msgid "Purchase Orders"
msgstr "Purchase Orders"
+#. placeholder {0}: currencyFormatter.format(poTopTierGap)
+msgid "Purchase orders above {0} will not require approval. Edit the top-tier rule and clear the Maximum Amount, or add a higher rule with no maximum."
+msgstr "Purchase orders above {0} will not require approval. Edit the top-tier rule and clear the Maximum Amount, or add a higher rule with no maximum."
+
msgid "Purchase orders required"
msgstr "Purchase orders required"
diff --git a/packages/locale/locales/es/erp.po b/packages/locale/locales/es/erp.po
index ac14e6c38e..f631cf15a8 100644
--- a/packages/locale/locales/es/erp.po
+++ b/packages/locale/locales/es/erp.po
@@ -3744,6 +3744,9 @@ msgstr "Ocultar, anclar y reordenar columnas"
msgid "Higher priority wins ties and applies first for markups"
msgstr "Mayor prioridad gana empates y se aplica primero para márgenes"
+msgid "Highest-tier rule has a maximum"
+msgstr ""
+
msgid "History"
msgstr "Historial"
@@ -5943,6 +5946,10 @@ msgstr "Orden de compra eliminada exitosamente"
msgid "Purchase Orders"
msgstr "Órdenes de Compra"
+#. placeholder {0}: currencyFormatter.format(poTopTierGap)
+msgid "Purchase orders above {0} will not require approval. Edit the top-tier rule and clear the Maximum Amount, or add a higher rule with no maximum."
+msgstr ""
+
msgid "Purchase orders required"
msgstr "Órdenes de compra requeridas"
diff --git a/packages/locale/locales/fr/erp.po b/packages/locale/locales/fr/erp.po
index 1fa1767a60..58dde670ac 100644
--- a/packages/locale/locales/fr/erp.po
+++ b/packages/locale/locales/fr/erp.po
@@ -3744,6 +3744,9 @@ msgstr "Masquer, épingler et réorganiser les colonnes"
msgid "Higher priority wins ties and applies first for markups"
msgstr "La priorité plus élevée gagne les égalités et s'applique en premier pour les majorations"
+msgid "Highest-tier rule has a maximum"
+msgstr ""
+
msgid "History"
msgstr "Historique"
@@ -5943,6 +5946,10 @@ msgstr "Bon de commande supprimé avec succès"
msgid "Purchase Orders"
msgstr "Bons de commande"
+#. placeholder {0}: currencyFormatter.format(poTopTierGap)
+msgid "Purchase orders above {0} will not require approval. Edit the top-tier rule and clear the Maximum Amount, or add a higher rule with no maximum."
+msgstr ""
+
msgid "Purchase orders required"
msgstr "Bons de commande requis"
diff --git a/packages/locale/locales/hi/erp.po b/packages/locale/locales/hi/erp.po
index 6b72c4fb66..c0f88452b9 100644
--- a/packages/locale/locales/hi/erp.po
+++ b/packages/locale/locales/hi/erp.po
@@ -3744,6 +3744,9 @@ msgstr "कॉलम को छिपाएं, पिन करें और
msgid "Higher priority wins ties and applies first for markups"
msgstr "उच्च प्राथमिकता टाई जीतती है और मार्कअप के लिए पहले लागू होती है"
+msgid "Highest-tier rule has a maximum"
+msgstr ""
+
msgid "History"
msgstr "इतिहास"
@@ -5943,6 +5946,10 @@ msgstr "क्रय आदेश सफलतापूर्वक हटा
msgid "Purchase Orders"
msgstr "क्रय आदेश"
+#. placeholder {0}: currencyFormatter.format(poTopTierGap)
+msgid "Purchase orders above {0} will not require approval. Edit the top-tier rule and clear the Maximum Amount, or add a higher rule with no maximum."
+msgstr ""
+
msgid "Purchase orders required"
msgstr "क्रय आदेश आवश्यक हैं"
diff --git a/packages/locale/locales/it/erp.po b/packages/locale/locales/it/erp.po
index 5583422141..cb46a5b4e0 100644
--- a/packages/locale/locales/it/erp.po
+++ b/packages/locale/locales/it/erp.po
@@ -3744,6 +3744,9 @@ msgstr "Nascondi, fissa e riordina le colonne"
msgid "Higher priority wins ties and applies first for markups"
msgstr "La priorità più alta vince i pareggi e si applica per prima per i ricarichi"
+msgid "Highest-tier rule has a maximum"
+msgstr ""
+
msgid "History"
msgstr "Cronologia"
@@ -5943,6 +5946,10 @@ msgstr "Ordine di acquisto rimosso con successo"
msgid "Purchase Orders"
msgstr "Ordini di Acquisto"
+#. placeholder {0}: currencyFormatter.format(poTopTierGap)
+msgid "Purchase orders above {0} will not require approval. Edit the top-tier rule and clear the Maximum Amount, or add a higher rule with no maximum."
+msgstr ""
+
msgid "Purchase orders required"
msgstr "Ordini di acquisto richiesti"
diff --git a/packages/locale/locales/ja/erp.po b/packages/locale/locales/ja/erp.po
index 2303d35d6e..0d1792ef21 100644
--- a/packages/locale/locales/ja/erp.po
+++ b/packages/locale/locales/ja/erp.po
@@ -3744,6 +3744,9 @@ msgstr "列を非表示、ピン留め、並び替え"
msgid "Higher priority wins ties and applies first for markups"
msgstr "優先度が高いほど同点を制し、マークアップに最初に適用されます"
+msgid "Highest-tier rule has a maximum"
+msgstr ""
+
msgid "History"
msgstr "履歴"
@@ -5943,6 +5946,10 @@ msgstr "購買注文が正常に削除されました"
msgid "Purchase Orders"
msgstr "購入注文"
+#. placeholder {0}: currencyFormatter.format(poTopTierGap)
+msgid "Purchase orders above {0} will not require approval. Edit the top-tier rule and clear the Maximum Amount, or add a higher rule with no maximum."
+msgstr ""
+
msgid "Purchase orders required"
msgstr "購入注文が必要です"
diff --git a/packages/locale/locales/pl/erp.po b/packages/locale/locales/pl/erp.po
index 45f4ec3726..26ea6331fc 100644
--- a/packages/locale/locales/pl/erp.po
+++ b/packages/locale/locales/pl/erp.po
@@ -3744,6 +3744,9 @@ msgstr "Ukryj, przypnij i zmień kolejność kolumn"
msgid "Higher priority wins ties and applies first for markups"
msgstr "Wyższy priorytet wygrywa remisy i jest stosowany jako pierwszy dla marż"
+msgid "Highest-tier rule has a maximum"
+msgstr ""
+
msgid "History"
msgstr "Historia"
@@ -5943,6 +5946,10 @@ msgstr "Zamówienie zakupu zostało pomyślnie usunięte"
msgid "Purchase Orders"
msgstr "Zamówienia Zakupu"
+#. placeholder {0}: currencyFormatter.format(poTopTierGap)
+msgid "Purchase orders above {0} will not require approval. Edit the top-tier rule and clear the Maximum Amount, or add a higher rule with no maximum."
+msgstr ""
+
msgid "Purchase orders required"
msgstr "Wymagane zamówienia zakupu"
diff --git a/packages/locale/locales/pt/erp.po b/packages/locale/locales/pt/erp.po
index cac4612d98..58b1398a43 100644
--- a/packages/locale/locales/pt/erp.po
+++ b/packages/locale/locales/pt/erp.po
@@ -3744,6 +3744,9 @@ msgstr "Ocultar, fixar e reordenar colunas"
msgid "Higher priority wins ties and applies first for markups"
msgstr "Prioridade mais alta vence empates e se aplica primeiro para margens"
+msgid "Highest-tier rule has a maximum"
+msgstr ""
+
msgid "History"
msgstr "Histórico"
@@ -5943,6 +5946,10 @@ msgstr "Ordem de compra removida com sucesso"
msgid "Purchase Orders"
msgstr "Pedidos de Compra"
+#. placeholder {0}: currencyFormatter.format(poTopTierGap)
+msgid "Purchase orders above {0} will not require approval. Edit the top-tier rule and clear the Maximum Amount, or add a higher rule with no maximum."
+msgstr ""
+
msgid "Purchase orders required"
msgstr "Ordens de compra necessárias"
diff --git a/packages/locale/locales/ru/erp.po b/packages/locale/locales/ru/erp.po
index abd21ef6a8..b13a8b04f5 100644
--- a/packages/locale/locales/ru/erp.po
+++ b/packages/locale/locales/ru/erp.po
@@ -3744,6 +3744,9 @@ msgstr "Скрывать, закреплять и переупорядочива
msgid "Higher priority wins ties and applies first for markups"
msgstr "Более высокий приоритет выигрывает при равенстве и применяется первым для наценок"
+msgid "Highest-tier rule has a maximum"
+msgstr ""
+
msgid "History"
msgstr "История"
@@ -5943,6 +5946,10 @@ msgstr "Заказ на покупку успешно удален"
msgid "Purchase Orders"
msgstr "Заказы на покупку"
+#. placeholder {0}: currencyFormatter.format(poTopTierGap)
+msgid "Purchase orders above {0} will not require approval. Edit the top-tier rule and clear the Maximum Amount, or add a higher rule with no maximum."
+msgstr ""
+
msgid "Purchase orders required"
msgstr "Требуются заказы на покупку"
diff --git a/packages/locale/locales/zh/erp.po b/packages/locale/locales/zh/erp.po
index ac3bf0a299..095b0a2593 100644
--- a/packages/locale/locales/zh/erp.po
+++ b/packages/locale/locales/zh/erp.po
@@ -3744,6 +3744,9 @@ msgstr "隐藏、固定和重新排序列"
msgid "Higher priority wins ties and applies first for markups"
msgstr "更高的优先级在平局时获胜,并首先应用于加价"
+msgid "Highest-tier rule has a maximum"
+msgstr ""
+
msgid "History"
msgstr "历史"
@@ -5943,6 +5946,10 @@ msgstr "采购订单已成功删除"
msgid "Purchase Orders"
msgstr "采购订单"
+#. placeholder {0}: currencyFormatter.format(poTopTierGap)
+msgid "Purchase orders above {0} will not require approval. Edit the top-tier rule and clear the Maximum Amount, or add a higher rule with no maximum."
+msgstr ""
+
msgid "Purchase orders required"
msgstr "采购订单必需"