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.

fix: guard against infinite loops in reorder policies

Brad Barbin78cde0fc4419
diff --git a/apps/erp/app/modules/items/ui/Item/ItemReorderPolicy.tsx b/apps/erp/app/modules/items/ui/Item/ItemReorderPolicy.tsx
index befc7be4f1..3ca29bd5b7 100644
--- a/apps/erp/app/modules/items/ui/Item/ItemReorderPolicy.tsx
+++ b/apps/erp/app/modules/items/ui/Item/ItemReorderPolicy.tsx
@@ -169,7 +169,8 @@ function calculateOrders({ itemPlanning, periods }: BaseOrderParams): {
reorderPoint - (projectedQuantity + orderedQuantity);
let day = 0;
- while (remainingQuantityNeeded > 0 && day < 5) {
+ let maxIterations = 100; // Safety counter to prevent infinite loops
+ while (remainingQuantityNeeded > 0 && day < 5 && maxIterations-- > 0) {
const dueDate = parseDate(period.startDate).add({ days: day });
const startDate = dueDate.subtract({
days: leadTime,
@@ -201,7 +202,8 @@ function calculateOrders({ itemPlanning, periods }: BaseOrderParams): {
reorderPoint - (projectedQuantity + orderedQuantity);
let day = 0;
- while (remainingQuantityNeeded > 0 && day < 5) {
+ let maxIterations = 100; // Safety counter to prevent infinite loops
+ while (remainingQuantityNeeded > 0 && day < 5 && maxIterations-- > 0) {
const dueDate = parseDate(period.startDate).add({ days: day });
const startDate = dueDate.subtract({
days: leadTime,
@@ -214,6 +216,11 @@ function calculateOrders({ itemPlanning, periods }: BaseOrderParams): {
// Adjust quantity based on order constraints
let orderQuantity = Math.max(minimumOrderQuantity, requiredQuantity);
+ // Ensure orderQuantity is positive to prevent infinite loop
+ if (orderQuantity <= 0) {
+ break;
+ }
+
// Round to nearest multiple if specified
if (orderMultiple && orderMultiple > 1) {
orderQuantity =