
drop table if exists vendors."IncomingQuotationDetail";
drop table if exists vendors."IncomingQuotationHeader";

------------
drop table if exists vendors."TenderRequestSupplierDetail";
drop table if exists vendors."TenderRequestSupplierHeader";

---------------------

create table if not exists vendors."IncomingQuotationHeader"(
				"IncomingQuotationHeaderId" bigserial primary key,
				 "ProductForQuotationHeaderId" bigint references vendors."ProductForQuotationHeader",
				 "SupplierId" int references public."Supplier"("SupplierId"),
				 "CreatedBy" int references "Account"("AccountId"),
				 "CreatedDate" timestamp without time zone,
				 "ModifiedBy" int references "Account"("AccountId"),
				 "ModifiedDate" timestamp without time zone,
				 "IsFinalized" boolean default false,
				 "FinalizedBy" int references "Account"("AccountId"),
				 "FinalizedDate" timestamp without time zone,
				 "DeliveryDate" date,
				 "TotalAmount" numeric,
				 "TotalTax" numeric,
				 "TotalNetAmount" numeric,
				 "TotalDiscount" numeric
);
            
     
create table if not exists vendors."IncomingQuotationDetail"(
					"IncomingQuotationDetailId" bigserial primary key,
					"PharmacyProductApprovalId" bigint references vendors."PharmacyProductApproval"("PharmacyProductApprovalId"),
					"IncomingQuotationHeaderId" bigint references vendors."IncomingQuotationHeader"("IncomingQuotationHeaderId"),
					"ProductForQuotationDetailId" bigint references vendors."ProductForQuotationDetail"("ProductForQuotationDetailId"),
					"PharmacyProductId" int references public."PharmacyProduct"("PharmacyProductId"),
					"Tax" int,
					"PurchaseRate" numeric,
					"MRP" numeric,
					"Amount" numeric,
					"TaxAmount" numeric,
					"DiscountPercentage" numeric,
					"DiscountAmount" numeric,
					"NetAmount" numeric,
					"Quantity" int,
					"Free" int					
);

----------


insert into vendors."TendorStatus" ("Status") values ('Quoatation Received');

alter table  vendors."SupplierProductFormulation" drop column if exists "SupplierProductHeaderId";
alter table  vendors."SupplierProductFormulation"  add column if not exists  "SupplierProductDetailId" int references vendors."SupplierProductDetail"("SupplierProductDetailId");
-----
alter table vendors."SupplierProductHeader" add column if not exists "RateCardUrl" text;

------

drop TABLE if exists public."InventoryRequest";
drop TABLE if exists public."CpxRelatedToDepartment";
drop TABLE if exists public."RequestDepartment";
drop TABLE if exists public."TicketType";

----------

create table vendors."InventoryProductRequest"(
				"InventoryProductRequestId" serial primary key,
				"PharmacyProductId" int references "PharmacyProduct"("PharmacyProductId"),
				"RequestProductName" text,
				"ReasonForRequest" text,
				"RequestedQuantity" int,
				"RequestType" varchar(50),
				"CreatedBy" int references "Account"("AccountId"),
				"CreatedDate" timestamp without time zone,
				"PharmacyProductRequestId" bigint references vendors."PharmacyProductRequest"("PharmacyProductRequestId"),
				"RejectedBy" int references "Account"("AccountId"),
				"RejectedDate" timestamp without time zone,
				"RejectReason" text
);
alter table vendors."InventoryProductRequest" add column if not exists "ModifiedBy" int references "Account"("AccountId"),
add column if not exists "ModifiedDate" timestamp without time zone,add column if not exists "DocumentUrl" text,
add column if not exists "LocationId" int references "Location"("LocationId");


alter table vendors."PharmacyProductRequest" add column if not exists "RequestedQuantity" int default null;
----
INSERT INTO vendors."TendorStatus" ("Status")
SELECT 'Quotation Selected' as "Status"
WHERE
NOT EXISTS (
SELECT "Status" FROM vendors."TendorStatus" WHERE "Status" = 'Quotation Selected'
);
-----

alter table vendors."PharmacyProductApproval" add column if not exists "ProductForQuotationHeaderId" bigint references vendors."ProductForQuotationHeader"("ProductForQuotationHeaderId"); 
