
jbe@87: BEGIN;
jbe@87:  
jbe@87: CREATE OR REPLACE VIEW "liquid_feedback_version" AS
jbe@87:   SELECT * FROM (VALUES ('1.2.9', 1, 2, 9))
jbe@87:   AS "subquery"("string", "major", "minor", "revision");
jbe@87: 
jbe@87: CREATE OR REPLACE FUNCTION "close_voting"("issue_id_p" "issue"."id"%TYPE)
jbe@87:   RETURNS VOID
jbe@87:   LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@87:     DECLARE
jbe@87:       "issue_row"   "issue"%ROWTYPE;
jbe@87:       "member_id_v" "member"."id"%TYPE;
jbe@87:     BEGIN
jbe@87:       PERFORM "lock_issue"("issue_id_p");
jbe@87:       SELECT * INTO "issue_row" FROM "issue" WHERE "id" = "issue_id_p";
jbe@87:       DELETE FROM "delegating_voter"
jbe@87:         WHERE "issue_id" = "issue_id_p";
jbe@87:       DELETE FROM "direct_voter"
jbe@87:         WHERE "issue_id" = "issue_id_p"
jbe@87:         AND "autoreject" = TRUE;
jbe@87:       DELETE FROM "direct_voter" USING "member"
jbe@87:         WHERE "direct_voter"."member_id" = "member"."id"
jbe@87:         AND "direct_voter"."issue_id" = "issue_id_p"
jbe@87:         AND "member"."active" = FALSE;
jbe@87:       UPDATE "direct_voter" SET "weight" = 1
jbe@87:         WHERE "issue_id" = "issue_id_p";
jbe@87:       PERFORM "add_vote_delegations"("issue_id_p");
jbe@87:       FOR "member_id_v" IN
jbe@87:         SELECT "interest"."member_id"
jbe@87:           FROM "interest"
jbe@87:           JOIN "member"
jbe@87:             ON "interest"."member_id" = "member"."id"
jbe@87:           LEFT JOIN "direct_voter"
jbe@87:             ON "interest"."member_id" = "direct_voter"."member_id"
jbe@87:             AND "interest"."issue_id" = "direct_voter"."issue_id"
jbe@87:           LEFT JOIN "delegating_voter"
jbe@87:             ON "interest"."member_id" = "delegating_voter"."member_id"
jbe@87:             AND "interest"."issue_id" = "delegating_voter"."issue_id"
jbe@87:           WHERE "interest"."issue_id" = "issue_id_p"
jbe@87:           AND "interest"."autoreject" = TRUE
jbe@87:           AND "member"."active"
jbe@87:           AND "direct_voter"."member_id" ISNULL
jbe@87:           AND "delegating_voter"."member_id" ISNULL
jbe@87:         UNION SELECT "membership"."member_id"
jbe@87:           FROM "membership"
jbe@87:           JOIN "member"
jbe@87:             ON "membership"."member_id" = "member"."id"
jbe@87:           LEFT JOIN "interest"
jbe@87:             ON "membership"."member_id" = "interest"."member_id"
jbe@87:             AND "interest"."issue_id" = "issue_id_p"
jbe@87:           LEFT JOIN "direct_voter"
jbe@87:             ON "membership"."member_id" = "direct_voter"."member_id"
jbe@87:             AND "direct_voter"."issue_id" = "issue_id_p"
jbe@87:           LEFT JOIN "delegating_voter"
jbe@87:             ON "membership"."member_id" = "delegating_voter"."member_id"
jbe@87:             AND "delegating_voter"."issue_id" = "issue_id_p"
jbe@87:           WHERE "membership"."area_id" = "issue_row"."area_id"
jbe@87:           AND "membership"."autoreject" = TRUE
jbe@87:           AND "member"."active"
jbe@87:           AND "interest"."autoreject" ISNULL
jbe@87:           AND "direct_voter"."member_id" ISNULL
jbe@87:           AND "delegating_voter"."member_id" ISNULL
jbe@87:       LOOP
jbe@87:         INSERT INTO "direct_voter"
jbe@87:           ("member_id", "issue_id", "weight", "autoreject") VALUES
jbe@87:           ("member_id_v", "issue_id_p", 1, TRUE);
jbe@87:         INSERT INTO "vote" (
jbe@87:           "member_id",
jbe@87:           "issue_id",
jbe@87:           "initiative_id",
jbe@87:           "grade"
jbe@87:           ) SELECT
jbe@87:             "member_id_v" AS "member_id",
jbe@87:             "issue_id_p"  AS "issue_id",
jbe@87:             "id"          AS "initiative_id",
jbe@87:             -1            AS "grade"
jbe@87:           FROM "initiative" WHERE "issue_id" = "issue_id_p";
jbe@87:       END LOOP;
jbe@87:       PERFORM "add_vote_delegations"("issue_id_p");
jbe@87:       UPDATE "issue" SET
jbe@87:         "closed" = now(),
jbe@87:         "voter_count" = (
jbe@87:           SELECT coalesce(sum("weight"), 0)
jbe@87:           FROM "direct_voter" WHERE "issue_id" = "issue_id_p"
jbe@87:         )
jbe@87:         WHERE "id" = "issue_id_p";
jbe@87:       UPDATE "initiative" SET
jbe@87:         "positive_votes" = "vote_counts"."positive_votes",
jbe@87:         "negative_votes" = "vote_counts"."negative_votes",
jbe@87:         "agreed" = CASE WHEN "majority_strict" THEN
jbe@87:           "vote_counts"."positive_votes" * "majority_den" >
jbe@87:           "majority_num" *
jbe@87:           ("vote_counts"."positive_votes"+"vote_counts"."negative_votes")
jbe@87:         ELSE
jbe@87:           "vote_counts"."positive_votes" * "majority_den" >=
jbe@87:           "majority_num" *
jbe@87:           ("vote_counts"."positive_votes"+"vote_counts"."negative_votes")
jbe@87:         END
jbe@87:         FROM
jbe@87:           ( SELECT
jbe@87:               "initiative"."id" AS "initiative_id",
jbe@87:               coalesce(
jbe@87:                 sum(
jbe@87:                   CASE WHEN "grade" > 0 THEN "direct_voter"."weight" ELSE 0 END
jbe@87:                 ),
jbe@87:                 0
jbe@87:               ) AS "positive_votes",
jbe@87:               coalesce(
jbe@87:                 sum(
jbe@87:                   CASE WHEN "grade" < 0 THEN "direct_voter"."weight" ELSE 0 END
jbe@87:                 ),
jbe@87:                 0
jbe@87:               ) AS "negative_votes"
jbe@87:             FROM "initiative"
jbe@87:             JOIN "issue" ON "initiative"."issue_id" = "issue"."id"
jbe@87:             JOIN "policy" ON "issue"."policy_id" = "policy"."id"
jbe@87:             LEFT JOIN "direct_voter"
jbe@87:               ON "direct_voter"."issue_id" = "initiative"."issue_id"
jbe@87:             LEFT JOIN "vote"
jbe@87:               ON "vote"."initiative_id" = "initiative"."id"
jbe@87:               AND "vote"."member_id" = "direct_voter"."member_id"
jbe@87:             WHERE "initiative"."issue_id" = "issue_id_p"
jbe@87:             AND "initiative"."admitted"  -- NOTE: NULL case is handled too
jbe@87:             GROUP BY "initiative"."id"
jbe@87:           ) AS "vote_counts",
jbe@87:           "issue",
jbe@87:           "policy"
jbe@87:         WHERE "vote_counts"."initiative_id" = "initiative"."id"
jbe@87:         AND "issue"."id" = "initiative"."issue_id"
jbe@87:         AND "policy"."id" = "issue"."policy_id";
jbe@87:       -- NOTE: "closed" column of issue must be set at this point
jbe@87:       DELETE FROM "battle" WHERE "issue_id" = "issue_id_p";
jbe@87:       INSERT INTO "battle" (
jbe@87:         "issue_id",
jbe@87:         "winning_initiative_id", "losing_initiative_id",
jbe@87:         "count"
jbe@87:       ) SELECT
jbe@87:         "issue_id",
jbe@87:         "winning_initiative_id", "losing_initiative_id",
jbe@87:         "count"
jbe@87:         FROM "battle_view" WHERE "issue_id" = "issue_id_p";
jbe@87:     END;
jbe@87:   $$;
jbe@87: 
jbe@87: COMMIT;




