 
 
 
----------------------
--- Freezing issues --
----------------------
-
-
-CREATE FUNCTION "issue_admission"
-  ( "issue_id_p" "issue"."id"%TYPE )
-  RETURNS VOID
-  LANGUAGE 'plpgsql' VOLATILE AS $$
-    DECLARE
-      "issue_row"  "issue"%ROWTYPE;
-      "policy_row" "policy"%ROWTYPE;
-    BEGIN
-      PERFORM "require_transaction_isolation"();
-      SELECT * INTO "issue_row" FROM "issue"
-        WHERE "id" = "issue_id_p" FOR UPDATE;
-      SELECT * INTO "policy_row"
-        FROM "policy" WHERE "id" = "issue_row"."policy_id";
-      IF EXISTS (
-        SELECT NULL FROM "initiative"
-        WHERE "issue_id" = "issue_id_p"
-        AND "supporter_count" > 0
-        AND "supporter_count" * "policy_row"."issue_quorum_den"
-        >= "issue_row"."population" * "policy_row"."issue_quorum_num"
-      ) THEN
-        UPDATE "issue" SET
-          "state"          = 'discussion',
-          "accepted"       = coalesce("phase_finished", now()),
-          "phase_finished" = NULL
-          WHERE "id" = "issue_id_p";
-      ELSIF "issue_row"."phase_finished" NOTNULL THEN
-        UPDATE "issue" SET
-          "state"          = 'canceled_issue_not_accepted',
-          "closed"         = "phase_finished",
-          "phase_finished" = NULL
-          WHERE "id" = "issue_id_p";
-      END IF;
-      RETURN;
-    END;
-  $$;
-
-COMMENT ON FUNCTION "issue_admission"
-  ( "issue"."id"%TYPE )
-  IS 'Checks admission of an issue, and either sets "accepted" to TRUE, or cancels the issue if "phase_finished" is set';
-
-
-CREATE FUNCTION "initiative_admission"
-  ( "issue_id_p" "issue"."id"%TYPE )
-  RETURNS VOID
-  LANGUAGE 'plpgsql' VOLATILE AS $$
-    DECLARE
-      "issue_row"      "issue"%ROWTYPE;
-      "policy_row"     "policy"%ROWTYPE;
-      "initiative_row" "initiative"%ROWTYPE;
-    BEGIN
-      PERFORM "require_transaction_isolation"();
-      SELECT * INTO "issue_row" FROM "issue"
-        WHERE "id" = "issue_id_p";
-      SELECT * INTO "policy_row" FROM "policy"
-        WHERE "id" = "issue_row"."policy_id";
-      FOR "initiative_row" IN
-        SELECT * FROM "initiative"
-        WHERE "issue_id" = "issue_id_p" AND "revoked" ISNULL
-        FOR UPDATE
-      LOOP
-        IF
-          "initiative_row"."polling" OR (
-            "initiative_row"."satisfied_supporter_count" > 0 AND
-            "initiative_row"."satisfied_supporter_count" *
-            "policy_row"."initiative_quorum_den" >=
-            "issue_row"."population" * "policy_row"."initiative_quorum_num"
-          )
-        THEN
-          UPDATE "initiative" SET "admitted" = TRUE
-            WHERE "id" = "initiative_row"."id";
-        ELSE
-          UPDATE "initiative" SET "admitted" = FALSE
-            WHERE "id" = "initiative_row"."id";
-        END IF;
-      END LOOP;
-      RETURN;
-    END;
-  $$;
-
-COMMENT ON FUNCTION "initiative_admission"
-  ( "issue"."id"%TYPE )
-  IS 'Sets the "admitted" flag of all initiatives in an issue, according to their supporter count fulfilling the necessary "initiative_quorum"';
-
-
-CREATE FUNCTION "freeze_after_snapshot"
-  ( "issue_id_p" "issue"."id"%TYPE )
-  RETURNS VOID
-  LANGUAGE 'plpgsql' VOLATILE AS $$
-    DECLARE
-      "issue_row"      "issue"%ROWTYPE;
-      "policy_row"     "policy"%ROWTYPE;
-      "initiative_row" "initiative"%ROWTYPE;
-    BEGIN
-      PERFORM "require_transaction_isolation"();
-      SELECT * INTO "issue_row" FROM "issue"
-        WHERE "id" = "issue_id_p" FOR UPDATE;
-      SELECT * INTO "policy_row" FROM "policy"
-        WHERE "id" = "issue_row"."policy_id";
-      IF EXISTS (
-        SELECT NULL FROM "initiative"
-        WHERE "issue_id" = "issue_id_p" AND "admitted" = TRUE
-      ) THEN
-        UPDATE "issue" SET
-          "state"         = 'voting',
-          "accepted"      = coalesce("accepted", "phase_finished"),
-          "half_frozen"   = coalesce("half_frozen", "phase_finished"),
-          "fully_frozen"  = "phase_finished",
-          "phase_finished" = NULL
-          WHERE "id" = "issue_id_p";
-      ELSE
-        UPDATE "issue" SET
-          "state"           = 'canceled_no_initiative_admitted',
-          "accepted"        = coalesce("accepted", "phase_finished"),
-          "half_frozen"     = coalesce("half_frozen", "phase_finished"),
-          "fully_frozen"    = "phase_finished",
-          "closed"          = "phase_finished",
-          "phase_finished"  = NULL
-          WHERE "id" = "issue_id_p";
-        -- NOTE: The following DELETE statements have effect only when
-        --       issue state has been manipulated
-        DELETE FROM "direct_voter"     WHERE "issue_id" = "issue_id_p";
-        DELETE FROM "delegating_voter" WHERE "issue_id" = "issue_id_p";
-        DELETE FROM "battle"           WHERE "issue_id" = "issue_id_p";
-      END IF;
-      RETURN;
-    END;
-  $$;
-
-COMMENT ON FUNCTION "freeze_after_snapshot"
-  ( "issue"."id"%TYPE )
-  IS 'This function freezes an issue (fully) and starts voting, but must only be called after all other preparations have been made.';
-
-
-CREATE FUNCTION "manual_freeze"("issue_id_p" "issue"."id"%TYPE)
-  RETURNS VOID
-  LANGUAGE 'plpgsql' VOLATILE AS $$
-    DECLARE
-      "issue_row" "issue"%ROWTYPE;
-    BEGIN
-      PERFORM "create_snapshot"("issue_id_p");
-      PERFORM "set_snapshot_event"("issue_id_p", 'full_freeze');
-      PERFORM "initiative_admission"("issue_id_p");
-      PERFORM "set_harmonic_initiative_weights"("issue_id_p");
-      PERFORM "freeze_after_snapshot"("issue_id_p");
-      RETURN;
-    END;
-  $$;
-
-COMMENT ON FUNCTION "manual_freeze"
-  ( "issue"."id"%TYPE )
-  IS 'Freeze an issue manually (fully) and start voting';
-
-
-
 -----------------------
 -- Counting of votes --
 -----------------------

    DECLARE
      "member_id_v" "member"."id"%TYPE;
    BEGIN
      DELETE FROM "direct_interest_snapshot"
        WHERE "issue_id" = "issue_id_p"
        AND "event" = 'periodic';

        RETURN NULL;
      END IF;
      IF "persist"."state" = 'admission' THEN
        SELECT * INTO "issue_row" FROM "issue" WHERE "id" = "issue_id_p"
          FOR UPDATE;
        SELECT * INTO "policy_row"
          FROM "policy" WHERE "id" = "issue_row"."policy_id";
        IF EXISTS (
          SELECT NULL FROM "initiative"
          WHERE "issue_id" = "issue_id_p"
          AND "supporter_count" > 0
          AND "supporter_count" * "policy_row"."issue_quorum_den"
          >= "issue_row"."population" * "policy_row"."issue_quorum_num"
        ) THEN
          UPDATE "issue" SET
            "state"          = 'discussion',
            "accepted"       = coalesce("phase_finished", now()),
            "phase_finished" = NULL
            WHERE "id" = "issue_id_p";
        ELSIF "issue_row"."phase_finished" NOTNULL THEN
          UPDATE "issue" SET
            "state"          = 'canceled_issue_not_accepted',
            "closed"         = "phase_finished",
            "phase_finished" = NULL
            WHERE "id" = "issue_id_p";
        END IF;
        RETURN NULL;
      END IF;
      IF "persist"."phase_finished" THEN
