DROP VIEW "public"."procurement_rfqs_view";--> statement-breakpoint
ALTER TABLE "item_offshore_hull" DROP CONSTRAINT "item_offshore_hull_item_id_items_id_fk";
--> statement-breakpoint
ALTER TABLE "item_offshore_top" DROP CONSTRAINT "item_offshore_top_item_id_items_id_fk";
--> statement-breakpoint
ALTER TABLE "item_shipbuilding" DROP CONSTRAINT "item_shipbuilding_item_id_items_id_fk";
--> statement-breakpoint
ALTER TABLE "item_offshore_hull" ADD COLUMN "item_code" varchar(100) NOT NULL;--> statement-breakpoint
ALTER TABLE "item_offshore_top" ADD COLUMN "item_code" varchar(100) NOT NULL;--> statement-breakpoint
ALTER TABLE "item_shipbuilding" ADD COLUMN "item_code" varchar(100) NOT NULL;--> statement-breakpoint
ALTER TABLE "item_offshore_hull" ADD CONSTRAINT "item_offshore_hull_item_code_items_item_code_fk" FOREIGN KEY ("item_code") REFERENCES "public"."items"("item_code") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "item_offshore_top" ADD CONSTRAINT "item_offshore_top_item_code_items_item_code_fk" FOREIGN KEY ("item_code") REFERENCES "public"."items"("item_code") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "item_shipbuilding" ADD CONSTRAINT "item_shipbuilding_item_code_items_item_code_fk" FOREIGN KEY ("item_code") REFERENCES "public"."items"("item_code") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "item_offshore_hull" DROP COLUMN "item_id";--> statement-breakpoint
ALTER TABLE "item_offshore_top" DROP COLUMN "item_id";--> statement-breakpoint
ALTER TABLE "item_shipbuilding" DROP COLUMN "item_id";--> statement-breakpoint
CREATE VIEW "public"."procurement_rfqs_view" AS (select "procurement_rfqs"."id" as "id", "procurement_rfqs"."rfq_code" as "rfqCode", "procurement_rfqs"."series" as "series", "procurement_rfqs"."rfq_sealed_yn" as "rfqSealedYn", "projects"."code" as "projectCode", "projects"."name" as "projectName", "items"."item_code" as "itemCode", "items"."item_name" as "itemName", "procurement_rfqs"."status" as "status", "procurement_rfqs"."pic_code" as "picCode", "procurement_rfqs"."rfq_send_date" as "rfqSendDate", "procurement_rfqs"."due_date" as "dueDate", (
        SELECT MIN(submitted_at)
        FROM procurement_vendor_quotations
        WHERE rfq_id = "procurement_rfqs"."id"
        AND submitted_at IS NOT NULL
      ) as "earliestQuotationSubmittedAt", "created_by_user"."name" as "createdByUserName", "sent_by_user"."name" as "sentByUserName", "procurement_rfqs"."updated_at" as "updatedAt", "updated_by_user"."name" as "updatedByUserName", "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 "majorItemMaterialCode", (
        SELECT pr_no 
        FROM pr_items 
        WHERE procurement_rfqs_id = "procurement_rfqs"."id"
        AND major_yn = true
        LIMIT 1
      ) as "poNo", (
        SELECT COUNT(*) 
        FROM pr_items 
        WHERE procurement_rfqs_id = "procurement_rfqs"."id"
      ) as "prItemsCount" 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");