CREATE TYPE "BatchCostLineKind" AS ENUM ('PRODUCTION', 'PACKAGING'); CREATE UNIQUE INDEX "product_bom_lines_cost_scope_key" ON "product_bom_lines"("id", "bom_id", "material_id", "organization_id"); CREATE UNIQUE INDEX "material_cost_rates_id_material_id_organization_id_key" ON "material_cost_rates"("id", "material_id", "organization_id"); CREATE TABLE "batch_cost_snapshots" ( "id" UUID NOT NULL, "organization_id" UUID NOT NULL, "batch_id" UUID NOT NULL, "created_by_id" UUID NOT NULL, "idempotency_key" UUID NOT NULL, "request_hash" CHAR(64) NOT NULL, "currency" CHAR(3) NOT NULL, "good_quantity" INTEGER NOT NULL, "production_total" DECIMAL(12,2) NOT NULL, "packaging_total" DECIMAL(12,2) NOT NULL, "total" DECIMAL(12,2) NOT NULL, "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "sealed_at" TIMESTAMPTZ(3), CONSTRAINT "batch_cost_snapshots_pkey" PRIMARY KEY ("id"), CONSTRAINT "batch_cost_snapshots_values" CHECK ( "request_hash" ~ '^[a-f0-9]{64}$' AND "currency" IN ('INR', 'USD', 'EUR', 'GBP') AND "good_quantity" >= 0 AND "production_total" >= 0 AND "packaging_total" >= 0 AND "total" = "production_total" + "packaging_total" ) ); CREATE UNIQUE INDEX "batch_cost_snapshots_batch_id_key" ON "batch_cost_snapshots"("batch_id"); CREATE UNIQUE INDEX "batch_cost_snapshots_batch_id_organization_id_key" ON "batch_cost_snapshots"("batch_id", "organization_id"); CREATE UNIQUE INDEX "batch_cost_snapshots_organization_id_idempotency_key_key" ON "batch_cost_snapshots"("organization_id", "idempotency_key"); CREATE UNIQUE INDEX "batch_cost_snapshots_id_organization_id_key" ON "batch_cost_snapshots"("id", "organization_id"); ALTER TABLE "batch_cost_snapshots" ADD CONSTRAINT "batch_cost_snapshots_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "batch_cost_snapshots" ADD CONSTRAINT "batch_cost_snapshots_batch_id_organization_id_fkey" FOREIGN KEY ("batch_id", "organization_id") REFERENCES "production_batches"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "batch_cost_snapshots" ADD CONSTRAINT "batch_cost_snapshots_created_by_id_organization_id_fkey" FOREIGN KEY ("created_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; CREATE TABLE "batch_cost_lines" ( "id" UUID NOT NULL, "organization_id" UUID NOT NULL, "snapshot_id" UUID NOT NULL, "kind" "BatchCostLineKind" NOT NULL, "bom_id" UUID NOT NULL, "bom_line_id" UUID NOT NULL, "material_id" UUID NOT NULL, "rate_id" UUID NOT NULL, "used_quantity" DECIMAL(12,3) NOT NULL, "wasted_quantity" DECIMAL(12,3) NOT NULL, "unit_cost" DECIMAL(12,4) NOT NULL, "line_cost" DECIMAL(12,2) NOT NULL, CONSTRAINT "batch_cost_lines_pkey" PRIMARY KEY ("id"), CONSTRAINT "batch_cost_lines_values" CHECK ( "used_quantity" >= 0 AND "wasted_quantity" >= 0 AND "unit_cost" > 0 AND "line_cost" >= 0 AND ROUND(("used_quantity" + "wasted_quantity") * "unit_cost", 2) = "line_cost" ) ); CREATE UNIQUE INDEX "batch_cost_lines_snapshot_id_bom_line_id_key" ON "batch_cost_lines"("snapshot_id", "bom_line_id"); ALTER TABLE "batch_cost_lines" ADD CONSTRAINT "batch_cost_lines_snapshot_id_organization_id_fkey" FOREIGN KEY ("snapshot_id", "organization_id") REFERENCES "batch_cost_snapshots"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "batch_cost_lines" ADD CONSTRAINT "batch_cost_lines_bom_scope_fkey" FOREIGN KEY ("bom_line_id", "bom_id", "material_id", "organization_id") REFERENCES "product_bom_lines"("id", "bom_id", "material_id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "batch_cost_lines" ADD CONSTRAINT "batch_cost_lines_rate_scope_fkey" FOREIGN KEY ("rate_id", "material_id", "organization_id") REFERENCES "material_cost_rates"("id", "material_id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; CREATE FUNCTION guard_batch_cost_snapshot() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP = 'UPDATE' THEN IF OLD.sealed_at IS NULL AND NEW.sealed_at IS NOT NULL AND (to_jsonb(NEW) - 'sealed_at') = (to_jsonb(OLD) - 'sealed_at') THEN RETURN NEW; END IF; RAISE EXCEPTION 'Batch cost snapshots are immutable'; END IF; IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Batch cost snapshots are immutable'; END IF; IF NOT EXISTS ( SELECT 1 FROM production_batches b JOIN production_batch_completions c ON c.batch_id = b.id AND c.organization_id = b.organization_id WHERE b.id = NEW.batch_id AND b.organization_id = NEW.organization_id AND b.status = 'COMPLETED' AND c.good_quantity = NEW.good_quantity ) THEN RAISE EXCEPTION 'Batch cost snapshot requires matching completion'; END IF; RETURN NEW; END; $$; CREATE TRIGGER batch_cost_snapshots_guard BEFORE INSERT OR UPDATE OR DELETE ON batch_cost_snapshots FOR EACH ROW EXECUTE FUNCTION guard_batch_cost_snapshot(); CREATE FUNCTION guard_batch_cost_line() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Batch cost lines are immutable'; END IF; IF NOT EXISTS ( SELECT 1 FROM batch_cost_snapshots WHERE id = NEW.snapshot_id AND organization_id = NEW.organization_id AND sealed_at IS NULL ) THEN RAISE EXCEPTION 'Batch cost snapshot is sealed'; END IF; RETURN NEW; END; $$; CREATE TRIGGER batch_cost_lines_guard BEFORE INSERT OR UPDATE OR DELETE ON batch_cost_lines FOR EACH ROW EXECUTE FUNCTION guard_batch_cost_line();