summaryrefslogtreecommitdiff
path: root/db/migrations/0227_serious_ben_grimm.sql
blob: cfeb171deb131a2c09915c80dae385cfd34188dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
DROP VIEW "public"."periodic_evaluations_view";--> statement-breakpoint
CREATE VIEW "public"."periodic_evaluations_view" AS (select "periodic_evaluations"."id", "periodic_evaluations"."evaluation_target_id", "evaluation_targets"."evaluation_year", "evaluation_targets"."division", "evaluation_targets"."vendor_id", "evaluation_targets"."vendor_code", "evaluation_targets"."vendor_name", "evaluation_targets"."domestic_foreign", "evaluation_targets"."material_type", "periodic_evaluations"."evaluation_period", "periodic_evaluations"."documents_submitted", "periodic_evaluations"."submission_date", "periodic_evaluations"."submission_deadline", "periodic_evaluations"."final_score", "periodic_evaluations"."final_grade", (
        SELECT COALESCE(SUM(CAST(red.score AS DECIMAL(5,2))), 0)
        FROM "reviewer_evaluation_details" red
        JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id
        JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id
        JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND rec.category2 = 'processScore'
        AND re.is_completed = true
      ) as "process_score", (
        SELECT COALESCE(SUM(CAST(red.score AS DECIMAL(5,2))), 0)
        FROM "reviewer_evaluation_details" red
        JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id
        JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id
        JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND rec.category2 = 'priceScore'
        AND re.is_completed = true
      ) as "price_score", (
        SELECT COALESCE(SUM(CAST(red.score AS DECIMAL(5,2))), 0)
        FROM "reviewer_evaluation_details" red
        JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id
        JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id
        JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND rec.category2 = 'deliveryScore'
        AND re.is_completed = true
      ) as "delivery_score", (
        SELECT COALESCE(SUM(CAST(red.score AS DECIMAL(5,2))), 0)
        FROM "reviewer_evaluation_details" red
        JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id
        JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id
        JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND rec.category2 = 'selfEvaluationScore'
        AND re.is_completed = true
      ) as "self_evaluation_score", (
        SELECT COALESCE(SUM(CAST(red.score AS DECIMAL(5,2))), 0)
        FROM "reviewer_evaluation_details" red
        JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id
        JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id
        JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND rec.category2 = 'bonus'
        AND re.is_completed = true
      ) as "participation_bonus", (
        SELECT COALESCE(SUM(CAST(red.score AS DECIMAL(5,2))), 0)
        FROM "reviewer_evaluation_details" red
        JOIN "reg_eval_criteria_details" recd ON red.reg_eval_criteria_details_id = recd.id
        JOIN "reg_eval_criteria" rec ON recd.criteria_id = rec.id
        JOIN "reviewer_evaluations" re ON red.reviewer_evaluation_id = re.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND rec.category2 = 'penalty'
        AND re.is_completed = true
      ) as "quality_deduction", "periodic_evaluations"."status", "periodic_evaluations"."review_completed_at", "periodic_evaluations"."finalized_at", "periodic_evaluations"."finalized_by", "periodic_evaluations"."evaluation_note", "periodic_evaluations"."created_at", "periodic_evaluations"."updated_at", "evaluation_targets"."admin_comment", "evaluation_targets"."consolidated_comment", "evaluation_targets"."consensus_status", "evaluation_targets"."confirmed_at", (
        SELECT CASE
          WHEN re.id IS NULL THEN 'NOT_ASSIGNED'
          WHEN re.is_completed = true THEN 'COMPLETED'
          WHEN (
            SELECT COALESCE(SUM(CAST(red.score AS DECIMAL(5,2))), 0)
            FROM "reviewer_evaluation_details" red
            WHERE red.reviewer_evaluation_id = re.id
          ) > 0 THEN 'IN_PROGRESS'
          ELSE 'NOT_STARTED'
        END
        FROM "reviewer_evaluations" re
        JOIN "evaluation_target_reviewers" etr ON re.evaluation_target_reviewer_id = etr.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND etr.department_code = 'ORDER_EVAL'
        LIMIT 1
      ) as "order_eval_status", (
        SELECT CASE
          WHEN re.id IS NULL THEN 'NOT_ASSIGNED'
          WHEN re.is_completed = true THEN 'COMPLETED'
          WHEN (
            SELECT COALESCE(SUM(CAST(red.score AS DECIMAL(5,2))), 0)
            FROM "reviewer_evaluation_details" red
            WHERE red.reviewer_evaluation_id = re.id
          ) > 0 THEN 'IN_PROGRESS'
          ELSE 'NOT_STARTED'
        END
        FROM "reviewer_evaluations" re
        JOIN "evaluation_target_reviewers" etr ON re.evaluation_target_reviewer_id = etr.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND etr.department_code = 'PROCUREMENT_EVAL'
        LIMIT 1
      ) as "procurement_eval_status", (
        SELECT CASE
          WHEN re.id IS NULL THEN 'NOT_ASSIGNED'
          WHEN re.is_completed = true THEN 'COMPLETED'
          WHEN (
            SELECT COALESCE(SUM(CAST(red.score AS DECIMAL(5,2))), 0)
            FROM "reviewer_evaluation_details" red
            WHERE red.reviewer_evaluation_id = re.id
          ) > 0 THEN 'IN_PROGRESS'
          ELSE 'NOT_STARTED'
        END
        FROM "reviewer_evaluations" re
        JOIN "evaluation_target_reviewers" etr ON re.evaluation_target_reviewer_id = etr.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND etr.department_code = 'QUALITY_EVAL'
        LIMIT 1
      ) as "quality_eval_status", (
        SELECT CASE
          WHEN re.id IS NULL THEN 'NOT_ASSIGNED'
          WHEN re.is_completed = true THEN 'COMPLETED'
          WHEN (
            SELECT COALESCE(SUM(CAST(red.score AS DECIMAL(5,2))), 0)
            FROM "reviewer_evaluation_details" red
            WHERE red.reviewer_evaluation_id = re.id
          ) > 0 THEN 'IN_PROGRESS'
          ELSE 'NOT_STARTED'
        END
        FROM "reviewer_evaluations" re
        JOIN "evaluation_target_reviewers" etr ON re.evaluation_target_reviewer_id = etr.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND etr.department_code = 'DESIGN_EVAL'
        LIMIT 1
      ) as "design_eval_status", (
        SELECT CASE
          WHEN re.id IS NULL THEN 'NOT_ASSIGNED'
          WHEN re.is_completed = true THEN 'COMPLETED'
          WHEN (
            SELECT COALESCE(SUM(CAST(red.score AS DECIMAL(5,2))), 0)
            FROM "reviewer_evaluation_details" red
            WHERE red.reviewer_evaluation_id = re.id
          ) > 0 THEN 'IN_PROGRESS'
          ELSE 'NOT_STARTED'
        END
        FROM "reviewer_evaluations" re
        JOIN "evaluation_target_reviewers" etr ON re.evaluation_target_reviewer_id = etr.id
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND etr.department_code = 'CS_EVAL'
        LIMIT 1
      ) as "cs_eval_status", (
              SELECT CASE
                WHEN re.id IS NULL THEN 'NOT_ASSIGNED'
                WHEN re.is_completed = true THEN 'COMPLETED'
                WHEN (
                  SELECT COALESCE(SUM(CAST(red.score AS DECIMAL(5,2))), 0)
                  FROM "reviewer_evaluation_details" red
                  WHERE red.reviewer_evaluation_id = re.id
                ) > 0 THEN 'IN_PROGRESS'
                ELSE 'NOT_STARTED'
              END
              FROM "reviewer_evaluations" re
              JOIN "evaluation_target_reviewers" etr ON re.evaluation_target_reviewer_id = etr.id
              WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
              AND etr.department_code = 'admin'
              LIMIT 1
            ) as "admin_eval_status", (
        SELECT COUNT(*)::int
        FROM "reviewer_evaluations" re
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
      ) as "total_reviewers", (
        SELECT COUNT(*)::int
        FROM "reviewer_evaluations" re
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND re.is_completed = true
      ) as "completed_reviewers", (
        SELECT COUNT(*)::int
        FROM "reviewer_evaluations" re
        WHERE re.periodic_evaluation_id = "periodic_evaluations"."id"
        AND re.is_completed = false
      ) as "pending_reviewers", "users"."name", "users"."email" from "periodic_evaluations" left join "evaluation_targets" on "periodic_evaluations"."evaluation_target_id" = "evaluation_targets"."id" left join "users" on "periodic_evaluations"."finalized_by" = "users"."id" order by "periodic_evaluations"."created_at");