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: shipped quantity and received quantity must be a number

Brad Barbin96fd9d23773d
diff --git a/apps/erp/app/modules/inventory/ui/Receipts/ReceiptLines.tsx b/apps/erp/app/modules/inventory/ui/Receipts/ReceiptLines.tsx
index fe937668dd..c5c0ed3744 100644
--- a/apps/erp/app/modules/inventory/ui/Receipts/ReceiptLines.tsx
+++ b/apps/erp/app/modules/inventory/ui/Receipts/ReceiptLines.tsx
@@ -384,25 +384,27 @@ function ReceiptLineItem({
<NumberField
value={line.receivedQuantity ?? 0}
onChange={(value) => {
+ // Default to 0 if value is NaN, null, or undefined
+ const safeValue = isNaN(value) || value == null ? 0 : value;
onUpdate({
lineId: line.id!,
field: "receivedQuantity",
- value,
+ value: safeValue,
});
// Adjust serial numbers array size while preserving existing values
- if (value > serialNumbers.length) {
+ if (safeValue > serialNumbers.length) {
onSerialNumbersChange([
...serialNumbers,
...Array.from(
- { length: value - serialNumbers.length },
+ { length: safeValue - serialNumbers.length },
() => ({
index: serialNumbers.length,
number: "",
})
),
]);
- } else if (value < serialNumbers.length) {
- onSerialNumbersChange(serialNumbers.slice(0, value));
+ } else if (safeValue < serialNumbers.length) {
+ onSerialNumbersChange(serialNumbers.slice(0, safeValue));
}
}}
>
diff --git a/apps/erp/app/modules/inventory/ui/Shipments/ShipmentLines.tsx b/apps/erp/app/modules/inventory/ui/Shipments/ShipmentLines.tsx
index df797f4829..0b108fdddf 100644
--- a/apps/erp/app/modules/inventory/ui/Shipments/ShipmentLines.tsx
+++ b/apps/erp/app/modules/inventory/ui/Shipments/ShipmentLines.tsx
@@ -422,25 +422,27 @@ function ShipmentLineItem({
<NumberField
value={line.shippedQuantity || 0}
onChange={(value) => {
+ // Default to 0 if value is NaN, null, or undefined
+ const safeValue = isNaN(value) || value == null ? 0 : value;
onUpdate({
lineId: line.id!,
field: "shippedQuantity",
- value,
+ value: safeValue,
});
// Adjust serial numbers array size while preserving existing values
- if (value > serialNumbers.length) {
+ if (safeValue > serialNumbers.length) {
onSerialNumbersChange([
...serialNumbers,
...Array.from(
- { length: value - serialNumbers.length },
+ { length: safeValue - serialNumbers.length },
(_, i) => ({
index: i,
id: "",
})
),
]);
- } else if (value < serialNumbers.length) {
- onSerialNumbersChange(serialNumbers.slice(0, value));
+ } else if (safeValue < serialNumbers.length) {
+ onSerialNumbersChange(serialNumbers.slice(0, safeValue));
}
}}
>
diff --git a/apps/erp/app/routes/x+/_index.tsx b/apps/erp/app/routes/x+/_index.tsx
index ae5f8b30a5..e7adb165ed 100644
--- a/apps/erp/app/routes/x+/_index.tsx
+++ b/apps/erp/app/routes/x+/_index.tsx
@@ -27,8 +27,6 @@ export default function AppIndexRoute() {
logo = mode === "dark" ? "/carbon-word-dark.svg" : "/carbon-word-light.svg";
}
- console.log({ logo });
-
return (
<div className="p-8 w-full flex flex-col h-[calc(100dvh-var(--header-height)*2)] bg-muted">
<Heading size="h3">Hello, {user.firstName}</Heading>
diff --git a/packages/database/supabase/functions/post-receipt/index.ts b/packages/database/supabase/functions/post-receipt/index.ts
index d2d2285ca6..700c7a15ba 100644
--- a/packages/database/supabase/functions/post-receipt/index.ts
+++ b/packages/database/supabase/functions/post-receipt/index.ts
@@ -121,8 +121,9 @@ serve(async (req: Request) => {
(purchaseOrder.data?.exchangeRate ?? 1);
const totalLinesCost = receiptLines.data.reduce((acc, receiptLine) => {
+ const safeReceivedQuantity = isNaN(receiptLine.receivedQuantity) || receiptLine.receivedQuantity == null ? 0 : receiptLine.receivedQuantity;
const lineCost =
- Math.abs(receiptLine.receivedQuantity ?? 0) *
+ Math.abs(safeReceivedQuantity) *
(receiptLine.unitPrice ?? 0);
return acc + lineCost;
}, 0);
@@ -172,9 +173,10 @@ serve(async (req: Request) => {
]?.toString()
);
+ const safeReceivedQuantity = isNaN(receiptLine?.receivedQuantity) || receiptLine?.receivedQuantity == null ? 0 : receiptLine.receivedQuantity;
const quantity = receiptLine?.requiresSerialTracking
? 1
- : receiptLine?.receivedQuantity ?? itemTracking.quantity;
+ : safeReceivedQuantity || itemTracking.quantity;
acc[itemTracking.id] = {
status: "Available",
@@ -202,7 +204,8 @@ serve(async (req: Request) => {
) {
const recivedQuantityInPurchaseUnit =
receiptLines.reduce((acc, receiptLine) => {
- return acc + (receiptLine.receivedQuantity ?? 0);
+ const safeReceivedQuantity = isNaN(receiptLine.receivedQuantity) || receiptLine.receivedQuantity == null ? 0 : receiptLine.receivedQuantity;
+ return acc + safeReceivedQuantity;
}, 0) / (receiptLines[0].conversionFactor ?? 1);
const receivedComplete =
@@ -239,7 +242,8 @@ serve(async (req: Request) => {
) {
const recivedQuantityInPurchaseUnit =
receiptLines.reduce((acc, receiptLine) => {
- return acc + (receiptLine.receivedQuantity ?? 0);
+ const safeReceivedQuantity = isNaN(receiptLine.receivedQuantity) || receiptLine.receivedQuantity == null ? 0 : receiptLine.receivedQuantity;
+ return acc + safeReceivedQuantity;
}, 0) / (receiptLines[0].conversionFactor ?? 1);
const newQuantityReceived =
@@ -438,10 +442,11 @@ serve(async (req: Request) => {
(purchaseOrderLine?.quantityInvoiced ?? 0) *
(purchaseOrderLine?.conversionFactor ?? 1);
+ const safeReceiptLineQuantity = isNaN(receiptLine.receivedQuantity) || receiptLine.receivedQuantity == null ? 0 : receiptLine.receivedQuantity;
const quantityToReverse = Math.max(
0,
Math.min(
- Math.abs(receiptLine.receivedQuantity ?? 0),
+ Math.abs(safeReceiptLineQuantity),
quantityInvoiced - quantityReceived
)
);
@@ -657,7 +662,7 @@ serve(async (req: Request) => {
});
}
- const receivedQuantity = receiptLine.receivedQuantity ?? 0;
+ const receivedQuantity = isNaN(receiptLine.receivedQuantity) || receiptLine.receivedQuantity == null ? 0 : receiptLine.receivedQuantity;
const isNegativeReceipt = receivedQuantity < 0;
const absReceivedQuantity = Math.abs(receivedQuantity);
@@ -798,9 +803,8 @@ serve(async (req: Request) => {
] === receiptLine.id
);
- const absReceivedQuantity = Math.abs(
- receiptLine.receivedQuantity || 0
- );
+ const safeReceiptLineQuantity = isNaN(receiptLine.receivedQuantity) || receiptLine.receivedQuantity == null ? 0 : receiptLine.receivedQuantity;
+ const absReceivedQuantity = Math.abs(safeReceiptLineQuantity);
const entryType =
receivedQuantity < 0 ? "Negative Adjmt." : "Positive Adjmt.";
const quantityPerEntry = receivedQuantity < 0 ? -1 : 1;
@@ -1058,7 +1062,7 @@ serve(async (req: Request) => {
if (!warehouseTransferLine) continue;
- const receivedQuantity = receiptLine.receivedQuantity ?? 0;
+ const receivedQuantity = isNaN(receiptLine.receivedQuantity) || receiptLine.receivedQuantity == null ? 0 : receiptLine.receivedQuantity;
if (receivedQuantity === 0) continue;
// Update warehouse transfer line received quantity
diff --git a/packages/database/supabase/functions/post-shipment/index.ts b/packages/database/supabase/functions/post-shipment/index.ts
index 471fe17a36..58b45af15d 100644
--- a/packages/database/supabase/functions/post-shipment/index.ts
+++ b/packages/database/supabase/functions/post-shipment/index.ts
@@ -253,8 +253,8 @@ serve(async (req: Request) => {
items.data.find((item) => item.id === shipmentLine.itemId)
?.itemTrackingType ?? "Inventory";
- // Default shippedQuantity to 0 if not defined
- const shippedQuantity = shipmentLine.shippedQuantity ?? 0;
+ // Default shippedQuantity to 0 if not defined or NaN
+ const shippedQuantity = isNaN(shipmentLine.shippedQuantity) || shipmentLine.shippedQuantity == null ? 0 : shipmentLine.shippedQuantity;
if (itemTrackingType === "Inventory") {
itemLedgerInserts.push({
@@ -353,7 +353,8 @@ serve(async (req: Request) => {
) {
const shippedQuantity = shipmentLines.reduce(
(acc, shipmentLine) => {
- return acc + (shipmentLine.shippedQuantity ?? 0);
+ const safeShippedQuantity = isNaN(shipmentLine.shippedQuantity) || shipmentLine.shippedQuantity == null ? 0 : shipmentLine.shippedQuantity;
+ return acc + safeShippedQuantity;
},
0
);
@@ -849,7 +850,8 @@ serve(async (req: Request) => {
) {
const shippedQuantity = shipmentLines.reduce(
(acc, shipmentLine) => {
- return acc + (shipmentLine.shippedQuantity ?? 0);
+ const safeShippedQuantity = isNaN(shipmentLine.shippedQuantity) || shipmentLine.shippedQuantity == null ? 0 : shipmentLine.shippedQuantity;
+ return acc + safeShippedQuantity;
},
0
);
@@ -1178,7 +1180,7 @@ serve(async (req: Request) => {
if (!warehouseTransferLine) continue;
- const shippedQuantity = shipmentLine.shippedQuantity ?? 0;
+ const shippedQuantity = isNaN(shipmentLine.shippedQuantity) || shipmentLine.shippedQuantity == null ? 0 : shipmentLine.shippedQuantity;
// Update warehouse transfer line shipped quantity
const newShippedQuantity =
diff --git a/packages/database/supabase/migrations/20250828093616_non-nullable-quantities-for-shipment-and-receipt.sql b/packages/database/supabase/migrations/20250828093616_non-nullable-quantities-for-shipment-and-receipt.sql
new file mode 100644
index 0000000000..aac9a7f45e
--- /dev/null
+++ b/packages/database/supabase/migrations/20250828093616_non-nullable-quantities-for-shipment-and-receipt.sql
@@ -0,0 +1,11 @@
+-- Make shippedQuantity in shipmentLine non-nullable with default 0
+UPDATE "shipmentLine" SET "shippedQuantity" = 0 WHERE "shippedQuantity" IS NULL;
+ALTER TABLE "shipmentLine" ALTER COLUMN "shippedQuantity" SET DEFAULT 0;
+ALTER TABLE "shipmentLine" ALTER COLUMN "shippedQuantity" SET NOT NULL;
+ALTER TABLE "shipmentLine" ADD CONSTRAINT "shipmentLine_shippedQuantity_not_nan" CHECK ("shippedQuantity" = "shippedQuantity");
+
+-- Make receivedQuantity in receiptLine non-nullable with default 0
+UPDATE "receiptLine" SET "receivedQuantity" = 0 WHERE "receivedQuantity" IS NULL;
+ALTER TABLE "receiptLine" ALTER COLUMN "receivedQuantity" SET DEFAULT 0;
+ALTER TABLE "receiptLine" ALTER COLUMN "receivedQuantity" SET NOT NULL;
+ALTER TABLE "receiptLine" ADD CONSTRAINT "receiptLine_receivedQuantity_not_nan" CHECK ("receivedQuantity" = "receivedQuantity");