CREATE VIEW "public"."vendor_quotation_view" AS (select "rfqs_last"."id" as "id", "rfqs_last"."rfq_code" as "rfq_code", "rfqs_last"."series" as "series", "rfqs_last"."rfq_sealed_yn" as "rfq_sealed_yn", "rfqs_last"."rfq_type" as "rfq_type", "rfqs_last"."rfq_title" as "rfq_title", "rfqs_last"."project_company" as "project_company", "rfqs_last"."project_flag" as "project_flag", "rfqs_last"."project_site" as "project_site", "rfqs_last"."sm_code" as "sm_code", "rfqs_last"."pr_number" as "pr_number", "rfqs_last"."pr_issue_date" as "pr_issue_date", "rfqs_last"."project_id" as "project_id", "projects"."code" as "project_code", "projects"."name" as "project_name", "rfqs_last"."item_code" as "item_code", "rfqs_last"."item_name" as "item_name", "rfqs_last"."package_no" as "package_no", "rfqs_last"."package_name" as "package_name", "rfqs_last"."eng_pic_name" as "eng_pic_name", "rfqs_last"."status" as "status", "rfqs_last"."rfq_send_date" as "rfq_send_date", "rfqs_last"."due_date" as "due_date", "rfqs_last"."pic" as "pic_id", "rfqs_last"."pic_code" as "pic_code", "rfqs_last"."pic_name" as "pic_name", "pic_user"."name" as "pic_user_name", "rfqs_last"."created_by" as "created_by", "created_by_user"."name" as "created_by_user_name", "rfqs_last"."created_at" as "created_at", "rfqs_last"."sent_by" as "sent_by", "sent_by_user"."name" as "sent_by_user_name", "rfqs_last"."updated_by" as "updated_by", "updated_by_user"."name" as "updated_by_user_name", "rfqs_last"."updated_at" as "updated_at", "rfqs_last"."remark" as "remark", "vendors"."id" as "vendor_id", "vendors"."vendor_name" as "vendor_name", "vendors"."vendor_code" as "vendor_code", "rfq_last_details"."id" as "rfq_last_details_id", "rfq_last_details"."email_sent_at" as "email_sent_at", "rfq_last_details"."email_status" as "email_status", "rfq_last_details"."short_list" as "short_list", "rfq_last_vendor_responses"."id" as "vendor_response_id", "rfq_last_vendor_responses"."participation_status" as "participation_status", "rfq_last_vendor_responses"."participation_replied_at" as "participation_replied_at", "rfq_last_vendor_responses"."non_participation_reason" as "non_participation_reason", "rfq_last_vendor_responses"."status" as "response_status", "rfq_last_vendor_responses"."response_version" as "response_version", "rfq_last_vendor_responses"."submitted_at" as "submitted_at", "rfq_last_vendor_responses"."total_amount" as "total_amount", "rfq_last_vendor_responses"."vendor_currency" as "vendor_currency", "rfq_last_vendor_responses"."vendor_payment_terms_code" as "vendor_payment_terms_code", "rfq_last_vendor_responses"."vendor_incoterms_code" as "vendor_incoterms_code", "rfq_last_vendor_responses"."vendor_delivery_date" as "vendor_delivery_date", CASE WHEN "rfq_last_vendor_responses"."participation_status" = '불참' THEN '불참' WHEN "rfq_last_vendor_responses"."participation_status" = '참여' THEN COALESCE("rfq_last_vendor_responses"."status", '작성중') WHEN "rfq_last_vendor_responses"."participation_status" = '미응답' OR "rfq_last_vendor_responses"."participation_status" IS NULL THEN CASE WHEN "rfq_last_details"."email_sent_at" IS NOT NULL THEN '미응답' ELSE NULL END ELSE '미응답' END as "display_status", ( SELECT COUNT(*) FROM rfq_last_details d WHERE d.rfqs_last_id = "rfqs_last"."id" AND d.is_latest = true ) as "vendor_count", ( SELECT COUNT(*) FROM rfq_last_details d WHERE d.rfqs_last_id = "rfqs_last"."id" AND d.short_list = true AND d.is_latest = true ) as "short_listed_vendor_count", ( SELECT COUNT(DISTINCT r.vendor_id) FROM rfq_last_vendor_responses r WHERE r.rfqs_last_id = "rfqs_last"."id" AND r.submitted_at IS NOT NULL AND r.is_latest = true ) as "quotation_received_count", ( SELECT MIN(r.submitted_at) FROM rfq_last_vendor_responses r WHERE r.rfqs_last_id = "rfqs_last"."id" AND r.submitted_at IS NOT NULL AND r.is_latest = true ) as "earliest_quotation_submitted_at", ( SELECT material_code FROM rfq_pr_items WHERE rfqs_last_id = "rfqs_last"."id" AND major_yn = true LIMIT 1 ) as "major_item_material_code", ( SELECT material_description FROM rfq_pr_items WHERE rfqs_last_id = "rfqs_last"."id" AND major_yn = true LIMIT 1 ) as "major_item_material_description", ( SELECT material_category FROM rfq_pr_items WHERE rfqs_last_id = "rfqs_last"."id" AND major_yn = true LIMIT 1 ) as "major_item_material_category", ( SELECT pr_no FROM rfq_pr_items WHERE rfqs_last_id = "rfqs_last"."id" AND major_yn = true LIMIT 1 ) as "major_item_pr_no", ( SELECT COUNT(*) FROM rfq_pr_items WHERE rfqs_last_id = "rfqs_last"."id" ) as "pr_items_count", ( SELECT COUNT(*) FROM rfq_pr_items WHERE rfqs_last_id = "rfqs_last"."id" AND major_yn = true ) as "major_items_count" from "rfqs_last" inner join "rfq_last_details" on ("rfq_last_details"."rfqs_last_id" = "rfqs_last"."id" and "rfq_last_details"."is_latest" = true) left join "vendors" on "rfq_last_details"."vendors_id" = "vendors"."id" left join "rfq_last_vendor_responses" on ("rfq_last_vendor_responses"."rfqs_last_id" = "rfqs_last"."id" and "rfq_last_vendor_responses"."vendor_id" = "vendors"."id" and "rfq_last_vendor_responses"."is_latest" = true) left join "projects" on "rfqs_last"."project_id" = "projects"."id" left join "users" "created_by_user" on "rfqs_last"."created_by" = "created_by_user"."id" left join "users" "updated_by_user" on "rfqs_last"."updated_by" = "updated_by_user"."id" left join "users" "sent_by_user" on "rfqs_last"."sent_by" = "sent_by_user"."id" left join "users" "pic_user" on "rfqs_last"."pic" = "pic_user"."id");