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




