CREATE TYPE "MaterialStockKind" AS ENUM ('RECEIPT_ACCEPTED'); CREATE UNIQUE INDEX "goods_receipts_id_organization_id_key" ON "goods_receipts"("id", "organization_id"); CREATE TABLE "material_stock_items" ( "id" UUID NOT NULL, "organization_id" UUID NOT NULL, "material_id" UUID NOT NULL, "warehouse_id" UUID NOT NULL, "on_hand" DECIMAL(12,3) NOT NULL DEFAULT 0, CONSTRAINT "material_stock_items_pkey" PRIMARY KEY ("id"), CONSTRAINT "material_stock_items_balance" CHECK ("on_hand" BETWEEN 0 AND 999999999.999) ); CREATE UNIQUE INDEX "material_stock_items_material_id_warehouse_id_key" ON "material_stock_items"("material_id", "warehouse_id"); CREATE UNIQUE INDEX "material_stock_items_id_material_id_organization_id_key" ON "material_stock_items"("id", "material_id", "organization_id"); CREATE INDEX "material_stock_items_scope_idx" ON "material_stock_items"("organization_id", "warehouse_id", "material_id"); ALTER TABLE "material_stock_items" ADD CONSTRAINT "material_stock_items_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "material_stock_items" ADD CONSTRAINT "material_stock_items_material_id_organization_id_fkey" FOREIGN KEY ("material_id", "organization_id") REFERENCES "materials"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "material_stock_items" ADD CONSTRAINT "material_stock_items_warehouse_id_organization_id_fkey" FOREIGN KEY ("warehouse_id", "organization_id") REFERENCES "warehouses"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; CREATE TABLE "material_receipt_postings" ( "id" UUID NOT NULL, "organization_id" UUID NOT NULL, "receipt_id" UUID NOT NULL, "warehouse_id" UUID NOT NULL, "posted_by_id" UUID NOT NULL, "idempotency_key" UUID NOT NULL, "request_hash" CHAR(64) NOT NULL, "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "material_receipt_postings_pkey" PRIMARY KEY ("id"), CONSTRAINT "material_receipt_postings_hash" CHECK ("request_hash" ~ '^[a-f0-9]{64}$') ); CREATE UNIQUE INDEX "material_receipt_postings_receipt_id_key" ON "material_receipt_postings"("receipt_id"); CREATE UNIQUE INDEX "material_receipt_postings_receipt_id_organization_id_key" ON "material_receipt_postings"("receipt_id", "organization_id"); CREATE UNIQUE INDEX "material_receipt_postings_organization_id_idempotency_key_key" ON "material_receipt_postings"("organization_id", "idempotency_key"); CREATE UNIQUE INDEX "material_receipt_postings_id_organization_id_key" ON "material_receipt_postings"("id", "organization_id"); ALTER TABLE "material_receipt_postings" ADD CONSTRAINT "material_receipt_postings_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "material_receipt_postings" ADD CONSTRAINT "material_receipt_postings_receipt_id_organization_id_fkey" FOREIGN KEY ("receipt_id", "organization_id") REFERENCES "goods_receipts"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "material_receipt_postings" ADD CONSTRAINT "material_receipt_postings_warehouse_id_organization_id_fkey" FOREIGN KEY ("warehouse_id", "organization_id") REFERENCES "warehouses"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "material_receipt_postings" ADD CONSTRAINT "material_receipt_postings_posted_by_id_organization_id_fkey" FOREIGN KEY ("posted_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; CREATE TABLE "material_stock_ledger" ( "id" UUID NOT NULL, "organization_id" UUID NOT NULL, "stock_item_id" UUID NOT NULL, "material_id" UUID NOT NULL, "posting_id" UUID NOT NULL, "receipt_line_id" UUID NOT NULL, "kind" "MaterialStockKind" NOT NULL, "delta" DECIMAL(12,3) NOT NULL, "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "material_stock_ledger_pkey" PRIMARY KEY ("id"), CONSTRAINT "material_stock_ledger_quantity" CHECK ("kind" = 'RECEIPT_ACCEPTED' AND "delta" > 0) ); CREATE UNIQUE INDEX "material_stock_ledger_receipt_line_id_key" ON "material_stock_ledger"("receipt_line_id"); CREATE UNIQUE INDEX "material_stock_ledger_receipt_line_id_organization_id_key" ON "material_stock_ledger"("receipt_line_id", "organization_id"); CREATE INDEX "material_stock_ledger_item_created_idx" ON "material_stock_ledger"("organization_id", "stock_item_id", "created_at", "id"); ALTER TABLE "material_stock_ledger" ADD CONSTRAINT "material_stock_ledger_item_scope_fkey" FOREIGN KEY ("stock_item_id", "material_id", "organization_id") REFERENCES "material_stock_items"("id", "material_id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "material_stock_ledger" ADD CONSTRAINT "material_stock_ledger_posting_scope_fkey" FOREIGN KEY ("posting_id", "organization_id") REFERENCES "material_receipt_postings"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "material_stock_ledger" ADD CONSTRAINT "material_stock_ledger_receipt_scope_fkey" FOREIGN KEY ("receipt_line_id", "organization_id") REFERENCES "goods_receipt_lines"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; CREATE FUNCTION guard_material_receipt_posting() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Material receipt postings are immutable'; END IF; IF EXISTS ( SELECT 1 FROM goods_receipt_lines l WHERE l.receipt_id = NEW.receipt_id AND l.organization_id = NEW.organization_id AND NOT EXISTS ( SELECT 1 FROM receipt_inspections i WHERE i.receipt_line_id = l.id AND i.organization_id = NEW.organization_id ) ) THEN RAISE EXCEPTION 'Receipt requires complete QC before stock posting'; END IF; RETURN NEW; END; $$; CREATE TRIGGER material_receipt_postings_guard BEFORE INSERT OR UPDATE OR DELETE ON material_receipt_postings FOR EACH ROW EXECUTE FUNCTION guard_material_receipt_posting(); CREATE FUNCTION guard_material_stock_ledger() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Material stock ledger is immutable'; END IF; IF NOT EXISTS ( SELECT 1 FROM material_receipt_postings p JOIN goods_receipt_lines l ON l.receipt_id = p.receipt_id AND l.id = NEW.receipt_line_id AND l.organization_id = NEW.organization_id JOIN purchase_order_lines ol ON ol.id = l.purchase_order_line_id AND ol.organization_id = NEW.organization_id JOIN receipt_inspections i ON i.receipt_line_id = l.id AND i.organization_id = NEW.organization_id JOIN material_stock_items s ON s.id = NEW.stock_item_id AND s.organization_id = NEW.organization_id WHERE p.id = NEW.posting_id AND p.organization_id = NEW.organization_id AND s.warehouse_id = p.warehouse_id AND s.material_id = ol.material_id AND NEW.material_id = ol.material_id AND NEW.delta = i.accepted_quantity ) THEN RAISE EXCEPTION 'Material receipt stock entry does not match accepted QC'; END IF; UPDATE material_stock_items SET on_hand = on_hand + NEW.delta WHERE id = NEW.stock_item_id AND organization_id = NEW.organization_id AND on_hand + NEW.delta <= 999999999.999; IF NOT FOUND THEN RAISE EXCEPTION 'Material stock capacity exceeded'; END IF; RETURN NEW; END; $$; CREATE TRIGGER material_stock_ledger_guard BEFORE INSERT OR UPDATE OR DELETE ON material_stock_ledger FOR EACH ROW EXECUTE FUNCTION guard_material_stock_ledger();