DROP VIEW "public"."pr_items_view";--> statement-breakpoint
DROP VIEW "public"."procurement_rfqs_view";--> statement-breakpoint
ALTER TABLE "pr_items" RENAME COLUMN "prject_sc" TO "project_sc";--> statement-breakpoint
ALTER TABLE "pr_items" RENAME COLUMN "prject_kl" TO "project_kl";--> statement-breakpoint
ALTER TABLE "pr_items" RENAME COLUMN "prject_lc" TO "project_lc";--> statement-breakpoint
ALTER TABLE "pr_items" RENAME COLUMN "prject_dl" TO "project_dl";--> statement-breakpoint
ALTER TABLE "procurement_rfqs" ADD COLUMN "sent_by" integer;--> statement-breakpoint
ALTER TABLE "procurement_rfqs" ADD CONSTRAINT "procurement_rfqs_sent_by_users_id_fk" FOREIGN KEY ("sent_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE VIEW "public"."pr_items_view" AS (select "pr_items"."id", "pr_items"."procurement_rfqs_id", "pr_items"."rfq_item", "pr_items"."pr_item", "pr_items"."pr_no", "pr_items"."item_id", "pr_items"."material_code", "pr_items"."material_category", "pr_items"."acc", "pr_items"."material_description", "pr_items"."size", "pr_items"."delivery_date", "pr_items"."quantity", "pr_items"."uom", "pr_items"."gross_weight", "pr_items"."gw_uom", "pr_items"."spec_no", "pr_items"."spec_url", "pr_items"."tracking_no", "pr_items"."major_yn", "pr_items"."project_def", "pr_items"."project_sc", "pr_items"."project_kl", "pr_items"."project_lc", "pr_items"."project_dl", "pr_items"."remark", "procurement_rfqs"."rfq_code", "items"."item_code", "items"."item_name" from "pr_items" left join "procurement_rfqs" on "pr_items"."procurement_rfqs_id" = "procurement_rfqs"."id" left join "items" on "pr_items"."item_id" = "items"."id");--> statement-breakpoint
CREATE VIEW "public"."procurement_rfqs_view" AS (select "procurement_rfqs"."id" as "id", "procurement_rfqs"."rfq_code" as "rfq_code", "procurement_rfqs"."series" as "series", "procurement_rfqs"."rfq_sealed_yn" as "rfq_sealed_yn", "projects"."code" as "project_code", "projects"."name" as "project_name", "items"."item_code" as "item_code", "items"."item_name" as "item_name", "procurement_rfqs"."status" as "status", "procurement_rfqs"."pic_code" as "pic_code", "procurement_rfqs"."rfq_send_date" as "rfq_send_date", "procurement_rfqs"."due_date" as "due_date", (
        SELECT MIN(submitted_at)
        FROM procurement_vendor_quotations
        WHERE rfq_id = "procurement_rfqs"."id"
        AND submitted_at IS NOT NULL
      ) as "earliest_quotation_submitted_at", "created_by_user"."name" as "created_by_user_name", "sent_by_user"."name" as "sent_by_user_name", "procurement_rfqs"."updated_at" as "updated_at", "updated_by_user"."name" as "updated_by_user_name", "procurement_rfqs"."remark" as "remark", (
        SELECT material_code 
        FROM pr_items 
        WHERE procurement_rfqs_id = "procurement_rfqs"."id"
        AND major_yn = true
        LIMIT 1
      ) as "major_item_material_code", (
        SELECT pr_no 
        FROM pr_items 
        WHERE procurement_rfqs_id = "procurement_rfqs"."id"
        AND major_yn = true
        LIMIT 1
      ) as "po_no", (
        SELECT COUNT(*) 
        FROM pr_items 
        WHERE procurement_rfqs_id = "procurement_rfqs"."id"
      ) as "pr_items_count" from "procurement_rfqs" left join "projects" on "procurement_rfqs"."project_id" = "projects"."id" left join "items" on "procurement_rfqs"."item_id" = "items"."id" left join "users" "created_by_user" on "procurement_rfqs"."created_by" = "created_by_user"."id" left join "users" "updated_by_user" on "procurement_rfqs"."updated_by" = "updated_by_user"."id" left join "users" "sent_by_user" on "procurement_rfqs"."sent_by" = "sent_by_user"."id");