summaryrefslogtreecommitdiff
path: root/db/migrations/0300_tearful_silverclaw.sql
blob: 96b862eab4f991e7d85f7e5709f3bdd026dbb63f (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
DROP VIEW "public"."vendor_detail_view";--> statement-breakpoint
--vendors_with_types drop 시에, initial_rfq_detail 가 먼저 drop 되어야 함. 따라서 수동 추가하였습니다.(최겸)
DROP VIEW "public"."initial_rfq_detail";--> statement-breakpoint
DROP VIEW "public"."vendors_with_types";--> statement-breakpoint
ALTER TABLE "vendors" ADD COLUMN "address_detail" text;--> statement-breakpoint
ALTER TABLE "vendors" ADD COLUMN "postal_code" varchar(20);--> statement-breakpoint
CREATE VIEW "public"."vendor_detail_view" AS (select "id", "vendor_name", "vendor_code", "tax_id", "address", "address_detail", "postal_code", "business_size", "country", "phone", "email", "website", "status", "representative_name", "representative_birth", "representative_email", "representative_phone", "corporate_registration_number", "credit_agency", "credit_rating", "cash_flow_rating", "created_at", "updated_at", 
        (SELECT COALESCE(
          json_agg(
            json_build_object(
              'id', c.id,
              'contactName', c.contact_name,
              'contactPosition', c.contact_position,
              'contactEmail', c.contact_email,
              'contactPhone', c.contact_phone,
              'isPrimary', c.is_primary
            )
          ),
          '[]'::json
        )
        FROM vendor_contacts c
        WHERE c.vendor_id = vendors.id)
       as "contacts", 
        (SELECT COALESCE(
          json_agg(
            json_build_object(
              'id', a.id,
              'fileName', a.file_name,
              'filePath', a.file_path,
              'attachmentType', a.attachment_type,
              'createdAt', a.created_at
            )
            ORDER BY a.attachment_type, a.created_at DESC
          ),
          '[]'::json
        )
        FROM vendor_attachments a
        WHERE a.vendor_id = vendors.id)
       as "attachments", 
        (SELECT COUNT(*)
        FROM vendor_attachments a
        WHERE a.vendor_id = vendors.id)
       as "attachment_count", 
        (SELECT COUNT(*) 
        FROM vendor_contacts c
        WHERE c.vendor_id = vendors.id)
       as "contact_count" from "vendors");--> statement-breakpoint
CREATE VIEW "public"."vendors_with_types" AS (select "vendors"."id" as "id", "vendors"."vendor_name" as "vendor_name", "vendors"."vendor_code" as "vendor_code", "vendors"."tax_id" as "tax_id", "vendors"."address" as "address", "vendors"."address_detail" as "address_detail", "vendors"."postal_code" as "postal_code", "vendors"."country" as "country", "vendors"."phone" as "phone", "vendors"."email" as "email", "vendors"."business_size" as "business_size", "vendors"."website" as "website", "vendors"."status" as "status", "vendors"."vendor_type_id" as "vendor_type_id", "vendors"."representative_name" as "representative_name", "vendors"."representative_birth" as "representative_birth", "vendors"."representative_email" as "representative_email", "vendors"."representative_phone" as "representative_phone", "vendors"."corporate_registration_number" as "corporate_registration_number", "vendors"."items" as "items", "vendors"."credit_agency" as "credit_agency", "vendors"."credit_rating" as "credit_rating", "vendors"."cash_flow_rating" as "cash_flow_rating", "vendors"."created_at" as "created_at", "vendors"."updated_at" as "updated_at", "vendor_types"."name_ko" as "vendor_type_name", "vendor_types"."name_en" as "vendor_type_name_en", "vendor_types"."code" as "vendor_type_code", 
        CASE
          WHEN "vendors"."status" = 'ACTIVE' THEN '정규업체'
          WHEN "vendors"."status" IN ('INACTIVE', 'BLACKLISTED', 'REJECTED') THEN ''
          ELSE '잠재업체'
        END
       as "vendor_category" from "vendors" left join "vendor_types" on "vendors"."vendor_type_id" = "vendor_types"."id");

CREATE VIEW "public"."initial_rfq_detail" AS--> statement-breakpoint
SELECT 
    br.id AS rfq_id,
    br.rfq_code,
    br.status AS rfq_status,
    ir.id AS initial_rfq_id,
    ir.initial_rfq_status,
    ir.vendor_id,
    v.vendor_code,
    v.vendor_name,
    v.vendor_category,
    v.country AS vendor_country,
    v.business_size AS vendor_business_size,
    ir.due_date,
    ir.valid_date,
    ir.incoterms_code,
    inc.description AS incoterms_description,
    ir.short_list,
    ir.return_yn,
    ir.cp_request_yn,
    ir.prject_gtc_yn,
    ir.return_revision,
    ir.rfq_revision,
    ir.gtc,
    ir.gtc_valid_date,
    ir.classification,
    ir.sparepart,
    ir.created_at,
    ir.updated_at
FROM b_rfqs br
JOIN initial_rfq ir 
  ON br.id = ir.rfq_id
LEFT JOIN vendors_with_types v 
  ON ir.vendor_id = v.id
LEFT JOIN incoterms inc 
  ON ir.incoterms_code = inc.code;