CREATE TABLE "rfq_last_attachment_revisions" (
	"id" serial PRIMARY KEY NOT NULL,
	"attachment_id" integer NOT NULL,
	"revision_no" varchar(10) NOT NULL,
	"revision_comment" text,
	"is_latest" boolean DEFAULT true NOT NULL,
	"file_name" varchar(255) NOT NULL,
	"original_file_name" varchar(255) NOT NULL,
	"file_path" varchar(512) NOT NULL,
	"file_size" integer,
	"file_type" varchar(100),
	"created_by" integer NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "rfq_last_attachments" (
	"id" serial PRIMARY KEY NOT NULL,
	"attachment_type" varchar(50) NOT NULL,
	"serial_no" varchar(50) NOT NULL,
	"rfq_id" integer NOT NULL,
	"current_revision" varchar(10) DEFAULT 'Rev.0' NOT NULL,
	"latest_revision_id" integer,
	"description" varchar(500),
	"created_by" integer NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "rfq_last_details" (
	"id" serial PRIMARY KEY NOT NULL,
	"procurement_rfqs_id" integer,
	"vendors_id" integer,
	"currency" varchar(10) DEFAULT 'USD',
	"payment_terms_code" varchar(50),
	"incoterms_code" varchar(20),
	"incoterms_detail" varchar(255),
	"delivery_date" date NOT NULL,
	"contract_duration" varchar(255),
	"tax_code" varchar(255) DEFAULT 'VV',
	"place_of_shipping" varchar(255),
	"place_of_destination" varchar(255),
	"remark" text,
	"cancel_reason" text,
	"updated_by" integer NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	"short_list" boolean DEFAULT false NOT NULL,
	"return_yn" boolean DEFAULT false NOT NULL,
	"returned_at" timestamp DEFAULT now(),
	"prject_gtc_yn" boolean DEFAULT false NOT NULL,
	"general_gtc_yn" boolean DEFAULT false NOT NULL,
	"nda_yn" boolean DEFAULT false NOT NULL,
	"agreement_yn" boolean DEFAULT false NOT NULL,
	"material_price_related_yn" boolean DEFAULT false,
	"sparepart_yn" boolean DEFAULT false,
	"first_yn" boolean DEFAULT false,
	"first_description" text,
	"sparepart_escription" text
);
--> statement-breakpoint
CREATE TABLE "rfq_pr_items" (
	"id" serial PRIMARY KEY NOT NULL,
	"rfqs_last_id" integer,
	"rfq_item" varchar(50),
	"pr_item" varchar(50),
	"pr_no" varchar(50),
	"material_code" varchar(255),
	"material_category" varchar(255),
	"acc" varchar(255),
	"material_description" varchar(255),
	"size" varchar(255),
	"delivery_date" date,
	"quantity" numeric(12, 2) DEFAULT 1,
	"uom" varchar(50),
	"gross_weight" numeric(12, 2) DEFAULT 1,
	"gw_uom" varchar(50),
	"spec_no" varchar(255),
	"spec_url" varchar(255),
	"tracking_no" varchar(255),
	"major_yn" boolean DEFAULT false,
	"project_def" varchar(255),
	"project_sc" varchar(255),
	"project_kl" varchar(255),
	"project_lc" varchar(255),
	"project_dl" varchar(255),
	"remark" text
);
--> statement-breakpoint
CREATE TABLE "rfqs_last" (
	"id" serial PRIMARY KEY NOT NULL,
	"rfq_code" varchar(50),
	"project_id" integer,
	"item_code" varchar(100),
	"item_name" varchar(255),
	"due_date" date,
	"rfq_send_date" date,
	"status" varchar(30) DEFAULT 'RFQ 생성' NOT NULL,
	"rfq_sealed_yn" boolean DEFAULT false,
	"eng_pic_name" varchar(50),
	"package_no" varchar(50),
	"package_name" varchar(255),
	"remark" text,
	"pic" integer,
	"pic_code" varchar(50),
	"pic_name" varchar(50),
	"sent_by" integer,
	"created_by" integer NOT NULL,
	"updated_by" integer NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	"rfq_type" varchar(255),
	"rfq_title" varchar(255),
	"project_company" varchar(255),
	"project_flag" varchar(255),
	"project_site" varchar(255),
	"sm_code" varchar(255),
	"pr_number" varchar(255),
	"pr_issue_date" date,
	"series" varchar(50),
	CONSTRAINT "rfqs_last_rfq_code_unique" UNIQUE("rfq_code")
);
--> statement-breakpoint
CREATE TABLE "rfq_last_vendor_attachments" (
	"id" serial PRIMARY KEY NOT NULL,
	"vendor_response_id" integer NOT NULL,
	"attachment_type" varchar(50) NOT NULL,
	"document_no" varchar(100),
	"file_name" varchar(255) NOT NULL,
	"original_file_name" varchar(255) NOT NULL,
	"file_path" varchar(512) NOT NULL,
	"file_size" integer,
	"file_type" varchar(100),
	"description" text,
	"valid_from" date,
	"valid_to" date,
	"uploaded_by" integer NOT NULL,
	"uploaded_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "rfq_last_vendor_quotation_items" (
	"id" serial PRIMARY KEY NOT NULL,
	"vendor_response_id" integer NOT NULL,
	"rfq_pr_item_id" integer NOT NULL,
	"pr_no" varchar(50),
	"material_code" varchar(255),
	"material_description" varchar(255),
	"quantity" numeric(12, 2) NOT NULL,
	"uom" varchar(50),
	"unit_price" numeric(15, 4) NOT NULL,
	"total_price" numeric(15, 2) NOT NULL,
	"currency" varchar(10) DEFAULT 'USD',
	"vendor_delivery_date" date,
	"lead_time" integer,
	"manufacturer" varchar(255),
	"manufacturer_country" varchar(100),
	"model_no" varchar(255),
	"technical_compliance" boolean DEFAULT true,
	"alternative_proposal" text,
	"discount_rate" numeric(5, 2),
	"discount_amount" numeric(15, 2),
	"item_remark" text,
	"deviation_reason" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "rfq_last_vendor_response_history" (
	"id" serial PRIMARY KEY NOT NULL,
	"vendor_response_id" integer NOT NULL,
	"action" varchar(50) NOT NULL,
	"previous_status" varchar(30),
	"new_status" varchar(30),
	"change_details" jsonb,
	"change_reason" text,
	"performed_by" integer NOT NULL,
	"performed_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "rfq_last_vendor_responses" (
	"id" serial PRIMARY KEY NOT NULL,
	"rfqs_last_id" integer NOT NULL,
	"rfq_last_details_id" integer NOT NULL,
	"vendors_id" integer NOT NULL,
	"response_version" integer DEFAULT 1 NOT NULL,
	"is_latest" boolean DEFAULT true NOT NULL,
	"status" varchar(30) DEFAULT '작성중' NOT NULL,
	"submitted_at" timestamp,
	"submitted_by" integer,
	"total_amount" numeric(15, 2),
	"currency" varchar(10) DEFAULT 'USD',
	"vendor_currency" varchar(10),
	"vendor_payment_terms_code" varchar(50),
	"vendor_incoterms_code" varchar(20),
	"vendor_incoterms_detail" varchar(255),
	"vendor_delivery_date" date,
	"vendor_contract_duration" varchar(255),
	"vendor_tax_code" varchar(255),
	"vendor_place_of_shipping" varchar(255),
	"vendor_place_of_destination" varchar(255),
	"vendor_first_yn" boolean,
	"vendor_first_description" text,
	"vendor_first_acceptance" varchar(50),
	"vendor_sparepart_yn" boolean,
	"vendor_sparepart_description" text,
	"vendor_sparepart_acceptance" varchar(50),
	"vendor_material_price_related_yn" boolean,
	"vendor_material_price_related_reason" text,
	"currency_reason" text,
	"payment_terms_reason" text,
	"delivery_date_reason" text,
	"incoterms_reason" text,
	"tax_reason" text,
	"shipping_reason" text,
	"general_remark" text,
	"technical_proposal" text,
	"created_by" integer NOT NULL,
	"updated_by" integer NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "rfq_last_attachment_revisions" ADD CONSTRAINT "rfq_last_attachment_revisions_attachment_id_rfq_last_attachments_id_fk" FOREIGN KEY ("attachment_id") REFERENCES "public"."rfq_last_attachments"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_attachment_revisions" ADD CONSTRAINT "rfq_last_attachment_revisions_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_attachments" ADD CONSTRAINT "rfq_last_attachments_rfq_id_rfqs_last_id_fk" FOREIGN KEY ("rfq_id") REFERENCES "public"."rfqs_last"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_attachments" ADD CONSTRAINT "rfq_last_attachments_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD CONSTRAINT "rfq_last_details_procurement_rfqs_id_rfqs_last_id_fk" FOREIGN KEY ("procurement_rfqs_id") REFERENCES "public"."rfqs_last"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD CONSTRAINT "rfq_last_details_vendors_id_vendors_id_fk" FOREIGN KEY ("vendors_id") REFERENCES "public"."vendors"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD CONSTRAINT "rfq_last_details_payment_terms_code_payment_terms_code_fk" FOREIGN KEY ("payment_terms_code") REFERENCES "public"."payment_terms"("code") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD CONSTRAINT "rfq_last_details_incoterms_code_incoterms_code_fk" FOREIGN KEY ("incoterms_code") REFERENCES "public"."incoterms"("code") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD CONSTRAINT "rfq_last_details_updated_by_users_id_fk" FOREIGN KEY ("updated_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_pr_items" ADD CONSTRAINT "rfq_pr_items_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
ALTER TABLE "rfqs_last" ADD CONSTRAINT "rfqs_last_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfqs_last" ADD CONSTRAINT "rfqs_last_pic_users_id_fk" FOREIGN KEY ("pic") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfqs_last" ADD CONSTRAINT "rfqs_last_sent_by_users_id_fk" FOREIGN KEY ("sent_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfqs_last" ADD CONSTRAINT "rfqs_last_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfqs_last" ADD CONSTRAINT "rfqs_last_updated_by_users_id_fk" FOREIGN KEY ("updated_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_attachments" ADD CONSTRAINT "rfq_last_vendor_attachments_vendor_response_id_rfq_last_vendor_responses_id_fk" FOREIGN KEY ("vendor_response_id") REFERENCES "public"."rfq_last_vendor_responses"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_attachments" ADD CONSTRAINT "rfq_last_vendor_attachments_uploaded_by_users_id_fk" FOREIGN KEY ("uploaded_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_quotation_items" ADD CONSTRAINT "rfq_last_vendor_quotation_items_vendor_response_id_rfq_last_vendor_responses_id_fk" FOREIGN KEY ("vendor_response_id") REFERENCES "public"."rfq_last_vendor_responses"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_quotation_items" ADD CONSTRAINT "rfq_last_vendor_quotation_items_rfq_pr_item_id_rfq_pr_items_id_fk" FOREIGN KEY ("rfq_pr_item_id") REFERENCES "public"."rfq_pr_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_response_history" ADD CONSTRAINT "rfq_last_vendor_response_history_vendor_response_id_rfq_last_vendor_responses_id_fk" FOREIGN KEY ("vendor_response_id") REFERENCES "public"."rfq_last_vendor_responses"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_response_history" ADD CONSTRAINT "rfq_last_vendor_response_history_performed_by_users_id_fk" FOREIGN KEY ("performed_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_responses" ADD CONSTRAINT "rfq_last_vendor_responses_rfqs_last_id_rfqs_last_id_fk" FOREIGN KEY ("rfqs_last_id") REFERENCES "public"."rfqs_last"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_responses" ADD CONSTRAINT "rfq_last_vendor_responses_rfq_last_details_id_rfq_last_details_id_fk" FOREIGN KEY ("rfq_last_details_id") REFERENCES "public"."rfq_last_details"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_responses" ADD CONSTRAINT "rfq_last_vendor_responses_vendors_id_vendors_id_fk" FOREIGN KEY ("vendors_id") REFERENCES "public"."vendors"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_responses" ADD CONSTRAINT "rfq_last_vendor_responses_submitted_by_users_id_fk" FOREIGN KEY ("submitted_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_responses" ADD CONSTRAINT "rfq_last_vendor_responses_vendor_payment_terms_code_payment_terms_code_fk" FOREIGN KEY ("vendor_payment_terms_code") REFERENCES "public"."payment_terms"("code") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_responses" ADD CONSTRAINT "rfq_last_vendor_responses_vendor_incoterms_code_incoterms_code_fk" FOREIGN KEY ("vendor_incoterms_code") REFERENCES "public"."incoterms"("code") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_responses" ADD CONSTRAINT "rfq_last_vendor_responses_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_responses" ADD CONSTRAINT "rfq_last_vendor_responses_updated_by_users_id_fk" FOREIGN KEY ("updated_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "rfq_last_latest_revision_idx" ON "rfq_last_attachment_revisions" USING btree ("attachment_id","is_latest") WHERE "rfq_last_attachment_revisions"."is_latest" = $1;--> statement-breakpoint
CREATE UNIQUE INDEX "rfq_last_attachment_revision_idx" ON "rfq_last_attachment_revisions" USING btree ("attachment_id","revision_no");--> statement-breakpoint
CREATE VIEW "public"."pr_items_last_view" AS (select "rfq_pr_items"."id", "rfq_pr_items"."rfqs_last_id", "rfq_pr_items"."rfq_item", "rfq_pr_items"."pr_item", "rfq_pr_items"."pr_no", "rfq_pr_items"."material_code", "rfq_pr_items"."material_category", "rfq_pr_items"."acc", "rfq_pr_items"."material_description", "rfq_pr_items"."size", "rfq_pr_items"."delivery_date", "rfq_pr_items"."quantity", "rfq_pr_items"."uom", "rfq_pr_items"."gross_weight", "rfq_pr_items"."gw_uom", "rfq_pr_items"."spec_no", "rfq_pr_items"."spec_url", "rfq_pr_items"."tracking_no", "rfq_pr_items"."major_yn", "rfq_pr_items"."project_def", "rfq_pr_items"."project_sc", "rfq_pr_items"."project_kl", "rfq_pr_items"."project_lc", "rfq_pr_items"."project_dl", "rfq_pr_items"."remark", "rfqs_last"."rfq_code", "rfqs_last"."rfq_type", "rfqs_last"."rfq_title", "rfqs_last"."item_code", "rfqs_last"."item_name", "rfqs_last"."pr_number", "rfqs_last"."pr_issue_date", "rfqs_last"."series", "rfqs_last"."status", "rfqs_last"."project_company", "rfqs_last"."project_flag", "rfqs_last"."project_site", "rfqs_last"."sm_code", "rfqs_last"."package_no", "rfqs_last"."package_name", "projects"."code", "projects"."name" from "rfq_pr_items" left join "rfqs_last" on "rfq_pr_items"."rfqs_last_id" = "rfqs_last"."id" left join "projects" on "rfqs_last"."project_id" = "projects"."id");--> statement-breakpoint
CREATE VIEW "public"."rfq_attachments_with_latest_revision" AS (
    SELECT 
      a.id as attachment_id,
      a.attachment_type,
      a.serial_no,
      a.rfq_id,
      a.description,
      a.current_revision,
      
      r.id as revision_id,
      r.file_name,
      r.original_file_name,
      r.file_path,
      r.file_size,
      r.file_type,
      r.revision_comment,
      
      a.created_by,
      u.name as created_by_name,
      a.created_at,
      a.updated_at
    FROM rfq_last_attachments a
    LEFT JOIN rfq_last_attachment_revisions r ON a.latest_revision_id = r.id
    LEFT JOIN users u ON a.created_by = u.id
  );--> 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 quotation_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 quotation_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 quotation_version DESC LIMIT 1
            ) as "quotation_submitted_at" from "rfq_last_details" "rfq_details" left join "rfqs_last" "rfqs" on "rfq_details"."procurement_rfqs_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");--> statement-breakpoint
CREATE VIEW "public"."rfqs_last_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", (
                SELECT MIN(submitted_at)
                FROM rfq_last_vendor_responses
                WHERE rfq_id = "rfqs_last"."id"
                AND submitted_at IS NOT NULL
            ) as "earliest_quotation_submitted_at", (
                SELECT COUNT(*)
                FROM rfq_last_details
                WHERE rfqs_last_id = "rfqs_last"."id"
            ) as "vendor_count", (
                SELECT COUNT(*)
                FROM rfq_last_details
                WHERE rfqs_last_id = "rfqs_last"."id"
                AND short_list = true
            ) as "short_listed_vendor_count", (
                SELECT COUNT(DISTINCT vendor_id)
                FROM rfq_last_vendor_responses
                WHERE rfq_id = "rfqs_last"."id"
                AND submitted_at IS NOT NULL
            ) as "quotation_received_count", "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", (
                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 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" 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");--> statement-breakpoint
CREATE VIEW "public"."vendor_quotation_items_detail_view" AS (select "quotation"."id" as "quotation_item_id", "quotation"."vendor_response_id" as "vendor_response_id", "rfq"."rfq_code" as "rfq_code", "pr_item"."pr_no" as "pr_no", "pr_item"."pr_item" as "pr_item", "pr_item"."material_code" as "material_code", "pr_item"."material_description" as "material_description", "pr_item"."quantity" as "original_quantity", "pr_item"."delivery_date" as "original_delivery_date", "quotation"."quantity" as "quoted_quantity", "quotation"."unit_price" as "unit_price", "quotation"."total_price" as "total_price", "quotation"."currency" as "currency", "quotation"."vendor_delivery_date" as "vendor_delivery_date", "quotation"."lead_time" as "lead_time", "quotation"."manufacturer" as "manufacturer", "quotation"."model_no" as "model_no", "quotation"."discount_rate" as "discount_rate", "quotation"."discount_amount" as "discount_amount", "quotation"."technical_compliance" as "technical_compliance", "quotation"."alternative_proposal" as "alternative_proposal", "quotation"."item_remark" as "item_remark", "quotation"."deviation_reason" as "deviation_reason" from "rfq_last_vendor_quotation_items" "quotation" left join "rfq_pr_items" "pr_item" on "quotation"."rfq_pr_item_id" = "pr_item"."id" left join "rfq_last_vendor_responses" "response" on "quotation"."vendor_response_id" = "response"."id" left join "rfqs_last" "rfq" on "response"."rfqs_last_id" = "rfq"."id");