drop TABLE if exists "PharmacyProductDetail";
drop TABLE if exists "PharmacyDepartmentalStock";
DROP TABLE if exists "ModuleDepartmentUser";
DROP TABLE if exists "ModuleDepartmentDetail";

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


Create table "PharmacyDepartment"("PharmacyDepartmentId" serial Primary Key,"Name" varchar(300),"Active" boolean default true,
								 "CreatedBy" int references "Account"("AccountId"),
								 "CreatedDate" timestamp without time zone,
								  "ModifiedBy" int references "Account"("AccountId"),
								 "ModifiedDate" timestamp without time zone,
								  "LocationId" int references "Location"("LocationId")
								 );

CREATE TABLE "PharmacyDepartmentUser"
(
    "PharmacyDepartmentUserId" serial primary key,
    "PharmacyDepartmentId" int references "PharmacyDepartment"("PharmacyDepartmentId"),
    "AccountId" int
);
-----------------

Create table "PharmacyProductRack"("PharmacyProductRackId" serial Primary Key,
								   "RackName" text,"LocationId" int references "Location"("LocationId"),
								   "CreatedBy" int references "Account"("AccountId"),
								   "CreatedDate" timestamp without time zone,
								   "ModifiedBy" int references "Account"("AccountId"),
								   "ModifiedDate" timestamp without time zone
								  );
--------------

CREATE TABLE "PharmacyDepartmentalStock"
(
    "PharmacyDepartmentalStockId" serial primary key,
    "PharmacyProductId" integer,
    "TaxId" integer,
    "QuantityIn" integer,
    "QuantityOut" integer,
    "BatchNumber" character varying(50) COLLATE pg_catalog."default",
    "ExpiryDate" date,
    "PurchaseRate" numeric(8,2),
    "Mrp" numeric(8,2),
    "Barcode" character varying(50) COLLATE pg_catalog."default",
    "PharmacyStockId" integer,
	"PharmacyRetailStockId" integer references "PharmacyRetailStock"("PharmacyRetailStockId"),
    "CreatedBy" integer,
    "CreatedDate" timestamp without time zone,
    "ModifiedBy" integer,
    "ModifiedDate" timestamp without time zone,
    "PharmacyDepartmentId" int references "PharmacyDepartment"("PharmacyDepartmentId"),    
    CONSTRAINT "PharmacyDepartmentalStock_CreatedBy_fkey" FOREIGN KEY ("CreatedBy")
        REFERENCES public."Account" ("AccountId") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT "PharmacyDepartmentalStock_ModifiedBy_fkey" FOREIGN KEY ("ModifiedBy")
        REFERENCES public."Account" ("AccountId") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT "PharmacyDepartmentalStock_PharmacyProductId_fkey" FOREIGN KEY ("PharmacyProductId")
        REFERENCES public."PharmacyProduct" ("PharmacyProductId") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT "PharmacyDepartmentalStock_PharmacyStockId_fkey" FOREIGN KEY ("PharmacyStockId")
        REFERENCES public."PharmacyStock" ("PharmacyStockId") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT "PharmacyDepartmentalStock_TaxId_fkey" FOREIGN KEY ("TaxId")
        REFERENCES public."LookupValue" ("LookupValueId") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

------------------------------------------
Create table "PharmacyProductDetail"("PharmacyProductDetailId" serial primary key,
									"PharmacyProductRackId" int references "PharmacyProductRack"("PharmacyProductRackId"),
									"ROQ" int,"ROL" int,"PharmacyProductId" int references "PharmacyProduct"("PharmacyProductId"),
									"PharmacyStockId" int references "PharmacyStock"("PharmacyStockId"),
									"PharmacyRetailStockId" int references "PharmacyRetailStock"("PharmacyRetailStockId"),
									"PharmacyDepartmentalStockId" int references "PharmacyDepartmentalStock"("PharmacyDepartmentalStockId"),
									"CreatedBy" int references "Account"("AccountId"),
								    "CreatedDate" timestamp without time zone 
									);
-----------------------

Alter table "IndentHeader" add column "PharmacyDepartmentId" int references "PharmacyDepartment"("PharmacyDepartmentId");

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

