summaryrefslogtreecommitdiff
path: root/db/migrations/0056_tricky_enchantress.sql
blob: 20ffbe65f74bd6e080e5c1123c83fa28083f659f (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
DROP VIEW "public"."procurement_rfq_details_view";--> statement-breakpoint
CREATE VIEW "public"."procurement_rfq_details_view" AS (select "rfq_details"."id" as "detail_id", "rfqs"."id" as "rfq_id", "rfqs"."rfq_code" as "rfq_code", "projects"."code" as "project_code", "projects"."name" as "project_name", "items"."item_code" as "item_code", "items"."item_name" as "item_name", "vendors"."vendor_name" as "vendor_name", "vendors"."vendor_code" as "vendor_code", "rfq_details"."currency" as "currency", "payment_terms"."code" as "payment_terms_code", "payment_terms"."description" as "payment_terms_description", "incoterms"."code" as "incoterms_code", "incoterms"."description" as "incoterms_description", "rfq_details"."incoterms_detail" as "incoterms_detail", "rfq_details"."delivery_date" as "delivery_date", "rfq_details"."tax_code" as "tax_code", "rfq_details"."place_of_shipping" as "place_of_shipping", "rfq_details"."place_of_destination" as "place_of_destination", "rfq_details"."material_price_related_yn" as "material_price_related_yn", "updated_by_user"."name" as "updated_by_user_name", "rfq_details"."updated_at" as "updated_at", (
              SELECT COUNT(*) 
              FROM pr_items 
              WHERE procurement_rfqs_id = "rfqs"."id"
          ) as "pr_items_count", (
              SELECT COUNT(*) 
              FROM pr_items 
              WHERE procurement_rfqs_id = "rfqs"."id" 
              AND major_yn = true
          ) as "major_items_count", (
                SELECT COUNT(*) 
                FROM procurement_rfq_comments 
                WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
            ) as "comment_count", (
                SELECT created_at 
                FROM procurement_rfq_comments 
                WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY created_at DESC LIMIT 1
            ) as "last_comment_date", (
                SELECT created_at 
                FROM procurement_rfq_comments 
                WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id" AND is_vendor_comment = true
                ORDER BY created_at DESC LIMIT 1
            ) as "last_vendor_comment_date", (
                SELECT COUNT(*) 
                FROM procurement_rfq_attachments 
                WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
            ) as "attachment_count", (
                SELECT COUNT(*) > 0
                FROM procurement_vendor_quotations
                WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
            ) as "has_quotation", (
                SELECT status
                FROM procurement_vendor_quotations
                WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY created_at DESC LIMIT 1
            ) as "quotation_status", (
                SELECT total_price
                FROM procurement_vendor_quotations
                WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY created_at DESC LIMIT 1
            ) as "quotation_total_price" from "procurement_rfq_details" "rfq_details" left join "procurement_rfqs" "rfqs" on "rfq_details"."procurement_rfqs_id" = "rfqs"."id" left join "projects" on "rfqs"."project_id" = "projects"."id" left join "items" on "rfqs"."item_id" = "items"."id" left join "vendors" on "rfq_details"."vendors_id" = "vendors"."id" left join "payment_terms" on "rfq_details"."payment_terms_code" = "payment_terms"."code" left join "incoterms" on "rfq_details"."incoterms_code" = "incoterms"."code" left join "users" "updated_by_user" on "rfq_details"."updated_by" = "updated_by_user"."id");