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




