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: quantity on purchase order

Brad Barbinf558d787dac2
diff --git a/apps/carbon/app/modules/inventory/ui/Receipts/ReceiptLines.tsx b/apps/carbon/app/modules/inventory/ui/Receipts/ReceiptLines.tsx
index 45012f68f6..7331dc75e3 100644
--- a/apps/carbon/app/modules/inventory/ui/Receipts/ReceiptLines.tsx
+++ b/apps/carbon/app/modules/inventory/ui/Receipts/ReceiptLines.tsx
@@ -201,7 +201,7 @@ function ReceivedQuantity({
}}
>
<NumberInput
- className="border-0 -ml-3 shadow-none disabled:bg-transparent disabled:opacity-100"
+ className="border-0 -ml-3 shadow-none disabled:bg-transparent disabled:opacity-100 min-w-[100px]"
isDisabled={isReadOnly}
size="sm"
min={0}
diff --git a/apps/carbon/app/routes/x+/purchase-invoice+/new.tsx b/apps/carbon/app/routes/x+/purchase-invoice+/new.tsx
index 0eb2c6c519..23fe01b429 100644
--- a/apps/carbon/app/routes/x+/purchase-invoice+/new.tsx
+++ b/apps/carbon/app/routes/x+/purchase-invoice+/new.tsx
@@ -2,6 +2,7 @@ import { assertIsPost, error } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { validationError, validator } from "@carbon/form";
+import { getLocalTimeZone, today } from "@internationalized/date";
import type { FunctionsResponse } from "@supabase/functions-js";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "@vercel/remix";
import { redirect } from "@vercel/remix";
@@ -125,6 +126,7 @@ export default function PurchaseInvoiceNewRoute() {
invoiceId: undefined,
supplierId: supplierId ?? "",
locationId: defaults?.locationId ?? "",
+ dateIssued: today(getLocalTimeZone()).toString(),
};
return (
diff --git a/packages/database/supabase/migrations/20241218214231_quantity-on-purchase-order.sql b/packages/database/supabase/migrations/20241218214231_quantity-on-purchase-order.sql
new file mode 100644
index 0000000000..14d233ac70
--- /dev/null
+++ b/packages/database/supabase/migrations/20241218214231_quantity-on-purchase-order.sql
@@ -0,0 +1,86 @@
+CREATE OR REPLACE FUNCTION update_inventory_quantity_on_purchase_order_line()
+RETURNS TRIGGER AS $$
+BEGIN
+ -- Only proceed if quantityReceived has changed and is not 0
+ IF NEW."quantityReceived" != OLD."quantityReceived" AND NEW."quantityReceived" != 0 THEN
+ -- Check if an entry exists in itemInventory
+ IF EXISTS (
+ SELECT 1 FROM "itemInventory"
+ WHERE "itemId" = NEW."itemId"
+ AND "companyId" = NEW."companyId"
+ AND (("locationId" = NEW."locationId") OR (NEW."locationId" IS NULL AND "locationId" IS NULL))
+ AND (("shelfId" = NEW."shelfId") OR (NEW."shelfId" IS NULL AND "shelfId" IS NULL))
+ ) THEN
+ -- Update existing row
+ UPDATE "itemInventory"
+ SET "quantityOnPurchase" = "quantityOnPurchase" - ((NEW."quantityReceived" * COALESCE(NEW."conversionFactor", 1)) - (OLD."quantityReceived" * COALESCE(OLD."conversionFactor", 1)))
+ WHERE "itemId" = NEW."itemId"
+ AND "companyId" = NEW."companyId"
+ AND (("locationId" = NEW."locationId") OR (NEW."locationId" IS NULL AND "locationId" IS NULL))
+ AND (("shelfId" = NEW."shelfId") OR (NEW."shelfId" IS NULL AND "shelfId" IS NULL));
+ ELSE
+ -- Insert new row
+ INSERT INTO "itemInventory" ("itemId", "locationId", "shelfId", "companyId", "quantityOnPurchase")
+ VALUES (NEW."itemId", NEW."locationId", NEW."shelfId", NEW."companyId", NEW."quantityToReceive" * COALESCE(NEW."conversionFactor", 1));
+ END IF;
+ END IF;
+
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+-- Function to update inventory quantity on purchase order based on purchase order status changes
+CREATE OR REPLACE FUNCTION update_inventory_quantity_on_purchase_order()
+RETURNS TRIGGER AS $$
+DECLARE
+ line RECORD;
+BEGIN
+ -- When status changes to 'To Receive and Invoice'
+ IF (OLD.status = 'Draft' OR OLD.status = 'To Review') AND NEW.status = 'To Receive and Invoice' THEN
+ FOR line IN (SELECT * FROM "purchaseOrderLine" WHERE "purchaseOrderId" = NEW.id)
+ LOOP
+ IF EXISTS (
+ SELECT 1 FROM "itemInventory"
+ WHERE "itemId" = line."itemId"
+ AND "companyId" = line."companyId"
+ AND (("locationId" = line."locationId") OR (line."locationId" IS NULL AND "locationId" IS NULL))
+ AND (("shelfId" = line."shelfId") OR (line."shelfId" IS NULL AND "shelfId" IS NULL))
+ ) THEN
+ -- Update existing row
+ UPDATE "itemInventory"
+ SET "quantityOnPurchase" = "quantityOnPurchase" + (line."quantityToReceive" * COALESCE(line."conversionFactor", 1)) - (COALESCE(line."quantityReceived", 0) * COALESCE(line."conversionFactor", 1))
+ WHERE "itemId" = line."itemId"
+ AND "companyId" = line."companyId"
+ AND (("locationId" = line."locationId") OR (line."locationId" IS NULL AND "locationId" IS NULL))
+ AND (("shelfId" = line."shelfId") OR (line."shelfId" IS NULL AND "shelfId" IS NULL));
+ ELSE
+ -- Insert new row
+ INSERT INTO "itemInventory" ("itemId", "locationId", "shelfId", "companyId", "quantityOnPurchase")
+ VALUES (line."itemId", line."locationId", line."shelfId", line."companyId", (line."quantityToReceive" * COALESCE(line."conversionFactor", 1)) - (COALESCE(line."quantityReceived", 0) * COALESCE(line."conversionFactor", 1)));
+ END IF;
+ END LOOP;
+
+ -- When status changes back to 'Draft' or to 'Closed'
+ ELSIF (NEW.status = 'Draft' AND OLD.status != 'Draft') OR (NEW.status = 'Closed' AND OLD.status != 'Closed') THEN
+ FOR line IN (SELECT * FROM "purchaseOrderLine" WHERE "purchaseOrderId" = NEW.id)
+ LOOP
+ IF EXISTS (
+ SELECT 1 FROM "itemInventory"
+ WHERE "itemId" = line."itemId"
+ AND "companyId" = line."companyId"
+ AND (("locationId" = line."locationId") OR (line."locationId" IS NULL AND "locationId" IS NULL))
+ AND (("shelfId" = line."shelfId") OR (line."shelfId" IS NULL AND "shelfId" IS NULL))
+ ) THEN
+ UPDATE "itemInventory"
+ SET "quantityOnPurchase" = "quantityOnPurchase" - (line."quantityToReceive" * COALESCE(line."conversionFactor", 1)) - (COALESCE(line."quantityReceived", 0) * COALESCE(line."conversionFactor", 1))
+ WHERE "itemId" = line."itemId"
+ AND "companyId" = line."companyId"
+ AND (("locationId" = line."locationId") OR (line."locationId" IS NULL AND "locationId" IS NULL))
+ AND (("shelfId" = line."shelfId") OR (line."shelfId" IS NULL AND "shelfId" IS NULL));
+ END IF;
+ END LOOP;
+ END IF;
+
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
\ No newline at end of file