DROP TABLE IF EXISTS public."PackageModuleDetail" cascade;

------------------------------------------------------
DROP TABLE IF EXISTS public."PackageModule" cascade;
------------------------------------------------------

CREATE TABLE IF NOT EXISTS public."PackageModule"
(
    "PackageModuleId" integer NOT NULL DEFAULT nextval('"PackageModule_PackageIdModule_Seq"'::regclass),
    "PackageName" character varying(50) COLLATE pg_catalog."default" NOT NULL,
    "PackageType" character varying(2) COLLATE pg_catalog."default" NOT NULL,
    "ModuleType" character varying(10) COLLATE pg_catalog."default" NOT NULL DEFAULT 'Package'::character varying,
    "LocationId" integer NOT NULL,
    "ProviderId" integer,
    "ChargeModuleTemplateId" integer NOT NULL,
    "ModulesMasterIds" text COLLATE pg_catalog."default" NOT NULL,
    "Notes" character varying(500) COLLATE pg_catalog."default",
    "Quantity" smallint NOT NULL DEFAULT 0,
    "FreeQuantity" smallint DEFAULT 0,
    "Exclusions" character varying(1000) COLLATE pg_catalog."default",
    "Active" boolean NOT NULL DEFAULT true,
    "CreatedBy" integer NOT NULL,
    "CreatedDate" timestamp without time zone NOT NULL DEFAULT now(),
    "ModifiedBy" integer,
    "ModifiedDate" timestamp without time zone,
    CONSTRAINT "PK_PackageModule_PackageModuleId" PRIMARY KEY ("PackageModuleId"),
    CONSTRAINT "FK_PackageModule_ChargeModuleTemplateId" FOREIGN KEY ("ChargeModuleTemplateId")
        REFERENCES public."ChargeModuleTemplate" ("ChargeModuleTemplateId") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT "FK_PackageModule_ProviderId" FOREIGN KEY ("ProviderId")
        REFERENCES public."Provider" ("ProviderId") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

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

CREATE TABLE IF NOT EXISTS public."PackageModuleDetail"
(
    "PackageModuleDetailId" integer NOT NULL DEFAULT nextval('"PackageModuleDetail_PackageIdModuleDetail_Seq"'::regclass),
    "PackageModuleId" integer NOT NULL,
    "ModulesMasterId" integer,
    "ReferenceId" integer NOT NULL,
    "Quantity" smallint NOT NULL DEFAULT 1,
    "IsFree" boolean NOT NULL DEFAULT false,
    "Amount" numeric(8,2),
    "Active" boolean NOT NULL DEFAULT true,
    "CreatedBy" numeric NOT NULL,
    "CreatedDate" timestamp without time zone NOT NULL DEFAULT now(),
    "ModifiedBy" numeric,
    "ModifiedDate" timestamp without time zone,
    CONSTRAINT "PackageModuleDetail_pkey" PRIMARY KEY ("PackageModuleDetailId"),
    CONSTRAINT "FK_PackageModuleDetail_PackageModuleId" FOREIGN KEY ("PackageModuleId")
        REFERENCES public."PackageModule" ("PackageModuleId") MATCH SIMPLE
        ON UPDATE CASCADE
        ON DELETE CASCADE
);

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

CREATE OR REPLACE FUNCTION public."udf_fetch_moduleChargeDetails"(
	"@modulesMasterId" integer DEFAULT NULL::integer,
	"@locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("LocationId" integer, "ModulesMasterId" integer, "ModuleName" character varying, "ModuleIcon" character varying, "ReferenceId" integer, "ChargeName" text, "DepartmentId" integer, "DepartmentName" text, "ChargeGroupId" integer, "ChargeGroupName" text, "RepeatTypeId" integer, "RepeatTypeName" text) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
BEGIN
	RETURN query SELECT distinct
-- 		CMD."ChargeModuleDetailsId",
-- 		CMD."ChargeModuleCategoryId",
		CMD."LocationId",
		CMC."ModulesMasterId",
		MM."ModuleName",
		MM."ModuleIcon",
-- 		CMD."Amount",
		CMD."ReferenceId",
		LMD."TestName" AS "ChargeName",
		NULL :: INT AS "DepartmentId",
		NULL :: TEXT AS "DepartmentName",
		NULL :: INT AS "ChargeGroupId",
		NULL :: TEXT AS "ChargeGroupName",
		NULL :: INT "RepeatTypeId",
		NULL :: TEXT "RepeatTypeName"
	FROM
		"ChargeModuleDetails" CMD
	JOIN "ChargeModuleCategory" CMC ON CMC."ChargeModuleCategoryId" = CMD."ChargeModuleCategoryId"
	JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = CMC."ModulesMasterId"
	JOIN "LabMainDetail" LMD ON LMD."LabMainDetailId" = CMD."ReferenceId"
	AND MM."ModuleName" = 'Lab'
	WHERE
		MM."ModulesMasterId" = "@modulesMasterId"
	AND CMD."LocationId" = "@locationId"
	UNION ALL
		SELECT DIstinct
-- 			CMD."ChargeModuleDetailsId",
-- 			CMD."ChargeModuleCategoryId",
			CMD."LocationId",
			CMC."ModulesMasterId",
			MM."ModuleName",
			MM."ModuleIcon",
-- 			CMD."Amount",
			CMD."ReferenceId",
			STM."ScanTestName" AS "ChargeName",
			NULL :: INT AS "DepartmentId",
			NULL :: TEXT AS "DepartmentName",
			NULL :: INT AS "ChargeGroupId",
			NULL :: TEXT AS "ChargeGroupName",
			NULL :: INT "RepeatTypeId",
			NULL :: TEXT "RepeatTypeName"
		FROM
			"ChargeModuleDetails" CMD
		JOIN "ChargeModuleCategory" CMC ON CMC."ChargeModuleCategoryId" = CMD."ChargeModuleCategoryId"
		JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = CMC."ModulesMasterId"
		JOIN "ScanTestMaster" STM ON STM."ScanTestMasterId" = CMD."ReferenceId"
		AND MM."ModuleName" = 'Scan'
		WHERE
			MM."ModulesMasterId" = "@modulesMasterId"
		AND CMD."LocationId" = "@locationId"
		UNION ALL
			SELECT distinct
-- 				CMD."ChargeModuleDetailsId",
-- 				CMD."ChargeModuleCategoryId",
				CMD."LocationId",
				CMC."ModulesMasterId",
				MM."ModuleName",
				MM."ModuleIcon",
-- 				CMD."Amount",
				CMD."ReferenceId",
				s."Name" AS "ChargeName",
				NULL :: INT AS "DepartmentId",
				NULL :: TEXT AS "DepartmentName",
				NULL :: INT AS "ChargeGroupId",
				NULL :: TEXT AS "ChargeGroupName",
				NULL :: INT "RepeatTypeId",
				NULL :: TEXT "RepeatTypeName"
			FROM
				"ChargeModuleDetails" CMD
			JOIN "ChargeModuleCategory" CMC ON CMC."ChargeModuleCategoryId" = CMD."ChargeModuleCategoryId"
			JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = CMC."ModulesMasterId"
			JOIN "Surgery" s ON s."SurgeryId" = CMD."ReferenceId"
			AND MM."ModuleName" = 'OT'
			WHERE
				MM."ModulesMasterId" = "@modulesMasterId"
			AND CMD."LocationId" = "@locationId"
			UNION ALL
				SELECT DISTINCT
-- 					CMD."ChargeModuleDetailsId",
-- 					CMD."ChargeModuleCategoryId",
					CMD."LocationId",
					CMC."ModulesMasterId",
					MM."ModuleName",
					MM."ModuleIcon",
-- 					CMD."Amount",
					CMD."ReferenceId",
					c."ChargeName",
					d."DepartmentId" AS "DepartmentId",
					d."DepartmentName" AS "DepartmentName",
					g."ChargeGroupId",
					g."ChargeGroupName",
					c."RepeatTypeId",
					t."RepeatTypeName" "Unit"
				FROM
					"ChargeModuleDetails" CMD
				JOIN "ChargeModuleCategory" CMC ON CMC."ChargeModuleCategoryId" = CMD."ChargeModuleCategoryId"
				JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = CMC."ModulesMasterId"
				JOIN "Charge" C ON C."ModulesMasterId" = CMC."ModulesMasterId"
				AND C."ChargeId" = CMD."ReferenceId"
				LEFT JOIN "ChargeGroup" G ON G."ChargeGroupId" = C."ChargeGroupId"
				LEFT JOIN "Department" d ON G."DepartmentId" = d."DepartmentId"
				LEFT JOIN "RepeatType" t ON t."RepeatTypeId" = c."RepeatTypeId"
				WHERE
					1 = 1
				AND MM."ModulesMasterId" = "@modulesMasterId"
				AND MM."ModuleName" = 'Services'
				AND CMD."LocationId" = "@locationId" ;
				END 
$BODY$;
