DROP VIEW "public"."rfq_last_details_view";--> statement-breakpoint
ALTER TABLE "rfq_last_details" RENAME COLUMN "procurement_rfqs_id" TO "rfqs_last_id";--> statement-breakpoint
ALTER TABLE "rfq_last_details" DROP CONSTRAINT "rfq_last_details_procurement_rfqs_id_rfqs_last_id_fk";
--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD CONSTRAINT "rfq_last_details_rfqs_last_id_rfqs_last_id_fk" FOREIGN KEY ("rfqs_last_id") REFERENCES "public"."rfqs_last"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE VIEW "public"."rfq_last_details_view" AS (select "rfq_details"."id" as "detail_id", "rfqs"."id" as "rfq_id", "rfqs"."rfq_code" as "rfq_code", "rfqs"."rfq_type" as "rfq_type", "rfqs"."rfq_title" as "rfq_title", "rfqs"."project_company" as "project_company", "rfqs"."project_flag" as "project_flag", "rfqs"."project_site" as "project_site", "rfqs"."sm_code" as "sm_code", "rfqs"."pr_number" as "pr_number", "rfqs"."pr_issue_date" as "pr_issue_date", "rfqs"."series" as "series", "projects"."code" as "project_code", "projects"."name" as "project_name", "rfqs"."item_code" as "item_code", "rfqs"."item_name" as "item_name", "vendors"."id" as "vendor_id", "vendors"."vendor_name" as "vendor_name", "vendors"."vendor_code" as "vendor_code", "vendors"."country" as "vendor_country", "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"."contract_duration" as "contract_duration", "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"."short_list" as "short_list", "rfq_details"."return_yn" as "return_yn", "rfq_details"."returned_at" as "returned_at", "rfq_details"."prject_gtc_yn" as "prject_gtc_yn", "rfq_details"."general_gtc_yn" as "general_gtc_yn", "rfq_details"."nda_yn" as "nda_yn", "rfq_details"."agreement_yn" as "agreement_yn", "rfq_details"."material_price_related_yn" as "material_price_related_yn", "rfq_details"."sparepart_yn" as "sparepart_yn", "rfq_details"."first_yn" as "first_yn", "rfq_details"."remark" as "remark", "rfq_details"."cancel_reason" as "cancel_reason", "rfq_details"."first_description" as "first_description", "rfq_details"."sparepart_escription" as "sparepart_description", "rfq_details"."updated_by" as "updated_by", "updated_by_user"."name" as "updated_by_user_name", "rfq_details"."updated_at" as "updated_at", (
                SELECT COUNT(*) 
                FROM rfq_pr_items 
                WHERE rfqs_last_id = "rfqs"."id"
            ) as "pr_items_count", (
                SELECT COUNT(*) 
                FROM rfq_pr_items 
                WHERE rfqs_last_id = "rfqs"."id" 
                AND major_yn = true
            ) as "major_items_count", (
                SELECT COUNT(*) > 0
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
            ) as "has_quotation", (
                SELECT status
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY created_at DESC LIMIT 1
            ) as "quotation_status", (
                SELECT total_amount
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY created_at DESC LIMIT 1
            ) as "quotation_total_price", (
                SELECT response_version
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY response_version DESC LIMIT 1
            ) as "quotation_version", (
                SELECT COUNT(DISTINCT response_version)
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
            ) as "quotation_version_count", (
                SELECT created_at
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY response_version DESC LIMIT 1
            ) as "last_quotation_date", (
                SELECT submitted_at
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY response_version DESC LIMIT 1
            ) as "quotation_submitted_at" from "rfq_last_details" "rfq_details" left join "rfqs_last" "rfqs" on "rfq_details"."rfqs_last_id" = "rfqs"."id" left join "projects" on "rfqs"."project_id" = "projects"."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");