DROP VIEW "public"."pr_items_view";--> statement-breakpoint DROP VIEW "public"."procurement_rfq_details_view";--> statement-breakpoint DROP VIEW "public"."procurement_rfqs_view";--> statement-breakpoint ALTER TABLE "pr_items" DROP CONSTRAINT "pr_items_item_id_items_id_fk"; --> statement-breakpoint ALTER TABLE "procurement_rfqs" DROP CONSTRAINT "procurement_rfqs_item_id_items_id_fk"; --> statement-breakpoint ALTER TABLE "procurement_rfqs" ADD COLUMN "item_code" varchar(100);--> statement-breakpoint ALTER TABLE "procurement_rfqs" ADD COLUMN "item_name" varchar(255);--> statement-breakpoint ALTER TABLE "pr_items" DROP COLUMN "item_id";--> statement-breakpoint ALTER TABLE "procurement_rfqs" DROP COLUMN "item_id";--> 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"."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", "procurement_rfqs"."item_code", "procurement_rfqs"."item_name" from "pr_items" left join "procurement_rfqs" on "pr_items"."procurement_rfqs_id" = "procurement_rfqs"."id");--> statement-breakpoint CREATE VIEW "public"."procurement_rfq_details_view" AS (select "rfq_details"."id" as "detail_id", "rfqs"."id" as "rfq_id", "rfqs"."rfq_code" as "rfq_code", "projects"."code" as "project_code", "projects"."name" as "project_name", "rfqs"."item_code" as "item_code", "rfqs"."item_name" as "item_name", "vendors"."vendor_name" as "vendor_name", "vendors"."vendor_code" as "vendor_code", "vendors"."id" as "vendor_id", "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"."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"."material_price_related_yn" as "material_price_related_yn", "updated_by_user"."name" as "updated_by_user_name", "rfq_details"."updated_at" as "updated_at", ( SELECT COUNT(*) FROM pr_items WHERE procurement_rfqs_id = "rfqs"."id" ) as "pr_items_count", ( SELECT COUNT(*) FROM pr_items WHERE procurement_rfqs_id = "rfqs"."id" AND major_yn = true ) as "major_items_count", ( SELECT COUNT(*) FROM procurement_rfq_comments WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id" ) as "comment_count", ( SELECT created_at FROM procurement_rfq_comments WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id" ORDER BY created_at DESC LIMIT 1 ) as "last_comment_date", ( SELECT created_at FROM procurement_rfq_comments WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id" AND is_vendor_comment = true ORDER BY created_at DESC LIMIT 1 ) as "last_vendor_comment_date", ( SELECT COUNT(*) FROM procurement_rfq_attachments WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id" ) as "attachment_count", ( SELECT COUNT(*) > 0 FROM procurement_vendor_quotations WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id" ) as "has_quotation", ( SELECT status FROM procurement_vendor_quotations WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id" ORDER BY created_at DESC LIMIT 1 ) as "quotation_status", ( SELECT total_price FROM procurement_vendor_quotations WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id" ORDER BY created_at DESC LIMIT 1 ) as "quotation_total_price", ( SELECT quotation_version FROM procurement_vendor_quotations WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id" ORDER BY quotation_version DESC LIMIT 1 ) as "quotation_version", ( SELECT COUNT(DISTINCT quotation_version) FROM procurement_vendor_quotations WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id" ) as "quotation_version_count", ( SELECT created_at FROM procurement_vendor_quotations WHERE rfq_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id" ORDER BY quotation_version DESC LIMIT 1 ) as "last_quotation_date" from "procurement_rfq_details" "rfq_details" left join "procurement_rfqs" "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"."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", "procurement_rfqs"."item_code" as "item_code", "procurement_rfqs"."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 "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");