-- FUNCTION: public.udf_fetch_chargeModuleDetails(integer, integer)

-- DROP FUNCTION IF EXISTS public."udf_fetch_chargeModuleDetails"(integer, integer);

CREATE OR REPLACE FUNCTION public."udf_fetch_chargeModuleDetails"(
	"@chargeModuleCategoryId" integer DEFAULT NULL::integer,
	"@locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("ChargeModuleDetailsId" bigint, "ChargeModuleCategoryId" integer, "LocationId" integer, "ModulesMasterId" integer, "ModuleName" character varying, "ModuleIcon" character varying, "Amount" numeric, "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 
  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 
  CMC."ChargeModuleCategoryId" = "@chargeModuleCategoryId" 
  AND CMD."LocationId" = "@locationId"
 union all
 SELECT 
  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 
  CMC."ChargeModuleCategoryId" = "@chargeModuleCategoryId" 
  AND CMD."LocationId" = "@locationId"
 union all
 SELECT 
  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 
  CMC."ChargeModuleCategoryId" = "@chargeModuleCategoryId" 
  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 CMC."ChargeModuleCategoryId" = "@chargeModuleCategoryId"  AND MM."ModuleName" = 'Services'  and CMD."LocationId" = "@locationId"
  ;  
  
END 
$BODY$;

ALTER FUNCTION public."udf_fetch_chargeModuleDetails"(integer, integer)
    OWNER TO postgres;


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



-- FUNCTION: public.udf_fetch_packageModuleDetails(integer)

-- DROP FUNCTION IF EXISTS public."udf_fetch_packageModuleDetails"(integer);

CREATE OR REPLACE FUNCTION public."udf_fetch_packageModuleDetails"(
	"@packageModuleId" integer DEFAULT NULL::integer)
    RETURNS TABLE("PackageModuleDetailId" integer, "PackageModuleId" integer, "ChargeModuleDetailsId" bigint, "ChargeModuleCategoryId" integer, "ModulesMasterId" integer, "ModuleName" character varying, "ModuleIcon" character varying, "Quantity" smallint, "Amount" numeric,"IsFree" bool, "ReferenceId" integer, "ChargeName" text, "DepartmentId" integer, "Department" text, "ServiceId" integer, "ServiceName" text, "RepeatTypeId" integer, "RepeatTypeName" text) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
 BEGIN return query 
 SELECT 
  PMD."PackageModuleDetailId", PMD."PackageModuleId", PMD."ChargeModuleDetailsId", PMD."ChargeModuleCategoryId", 
  PMD."ModulesMasterId", MM."ModuleName", MM."ModuleIcon", PMD."Quantity", PMD."Amount", PMD."IsFree",
  CMD."ReferenceId", LMD."TestName" AS "ChargeName" 
  ,null::int  as "ServiceDepartmentId",
  null::text as "ServiceDepartment",
   null::int as "ServiceId",
	null::text  as "ServiceName",
	null::int "RepeatTypeId"
	,null::text "RepeatTypeName"
FROM 
  "PackageModuleDetail" PMD 
  JOIN "ChargeModuleCategory" CMC ON CMC."ChargeModuleCategoryId" = PMD."ChargeModuleCategoryId" AND CMC."Active" IS TRUE
  JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = PMD."ModulesMasterId"
  JOIN "ChargeModuleDetails" CMD ON CMD."ChargeModuleDetailsId" = PMD."ChargeModuleDetailsId"
  JOIN "LabMainDetail" LMD ON LMD."LabMainDetailId" = CMD."ReferenceId" AND LMD."Active" IS TRUE
WHERE 
  PMD."PackageModuleId" = "@packageModuleId" 
  
   
   union all 
  SELECT 
  PMD."PackageModuleDetailId", PMD."PackageModuleId", PMD."ChargeModuleDetailsId", PMD."ChargeModuleCategoryId", 
  PMD."ModulesMasterId", MM."ModuleName", MM."ModuleIcon", PMD."Quantity", PMD."Amount", PMD."IsFree",
  CMD."ReferenceId",STM."ScanTestName" AS "ChargeName" 
  ,null::int  as "ServiceDepartmentId",
  null::text as "ServiceDepartment",
   null::int as "ServiceId",
	null::text  as "ServiceName",
	null::int "RepeatTypeId"
	,null::text "RepeatTypeName"
FROM 
  "PackageModuleDetail" PMD 
  JOIN "ChargeModuleCategory" CMC ON CMC."ChargeModuleCategoryId" = PMD."ChargeModuleCategoryId" AND CMC."Active" IS TRUE
  JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = PMD."ModulesMasterId"
  JOIN "ChargeModuleDetails" CMD ON CMD."ChargeModuleDetailsId" = PMD."ChargeModuleDetailsId"
  JOIN "ScanTestMaster" STM ON STM."ScanTestMasterId" = CMD."ReferenceId" AND STM."Active" IS TRUE
WHERE 
  PMD."PackageModuleId" = "@packageModuleId" 
  
union all  
  SELECT 
  PMD."PackageModuleDetailId", PMD."PackageModuleId", PMD."ChargeModuleDetailsId", PMD."ChargeModuleCategoryId", 
  PMD."ModulesMasterId", MM."ModuleName", MM."ModuleIcon", PMD."Quantity", PMD."Amount", PMD."IsFree",
  CMD."ReferenceId", s."Name" AS "ChargeName" 
  ,null::int  as "ServiceDepartmentId",
  null::text as "ServiceDepartment",
   null::int as "ServiceId",
	null::text  as "ServiceName",
	null::int "RepeatTypeId"
	,null::text "RepeatTypeName"
FROM 
  "PackageModuleDetail" PMD 
  JOIN "ChargeModuleCategory" CMC ON CMC."ChargeModuleCategoryId" = PMD."ChargeModuleCategoryId" AND CMC."Active" IS TRUE
  JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = PMD."ModulesMasterId"
  JOIN "ChargeModuleDetails" CMD ON CMD."ChargeModuleDetailsId" = PMD."ChargeModuleDetailsId"
  JOIN "Surgery" s ON s."SurgeryId" = CMD."ReferenceId" 
WHERE 
  PMD."PackageModuleId" = "@packageModuleId" 
  
   union all 
  SELECT 
  PMD."PackageModuleDetailId", PMD."PackageModuleId", PMD."ChargeModuleDetailsId", PMD."ChargeModuleCategoryId", 
  PMD."ModulesMasterId", MM."ModuleName", MM."ModuleIcon", PMD."Quantity", PMD."Amount", PMD."IsFree",
  CMD."ReferenceId", c."ChargeName" ,
 d."DepartmentId"  as "DepartmentId",
   d."DepartmentName" as "Department",
   g."ChargeGroupId" ,
	g."ChargeGroupName"  ,
   c."RepeatTypeId"
, t."RepeatTypeName" "Unit"
FROM 
  "PackageModuleDetail" PMD 
  JOIN "ChargeModuleCategory" CMC ON CMC."ChargeModuleCategoryId" = PMD."ChargeModuleCategoryId" AND CMC."Active" IS TRUE
  JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = PMD."ModulesMasterId"
    JOIN "ChargeModuleDetails" CMD ON CMD."ChargeModuleDetailsId" = PMD."ChargeModuleDetailsId"
  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 
  PMD."PackageModuleId" = "@packageModuleId" 
 union all  
  SELECT 
  PMD."PackageModuleDetailId", PMD."PackageModuleId", PMD."ChargeModuleDetailsId", PMD."ChargeModuleCategoryId", 
  PMD."ModulesMasterId", MM."ModuleName", MM."ModuleIcon", PMD."Quantity", PMD."Amount", PMD."IsFree",
  null::int  as"ReferenceId", null::text AS "ChargeName" 
  ,null::int  as "ServiceDepartmentId",
  null::text as "ServiceDepartment",
   null::int as "ServiceId",
	null::text  as "ServiceName",
	null::int "RepeatTypeId"
	,null::text "RepeatTypeName"
FROM 
  "PackageModuleDetail" PMD 
  JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = PMD."ModulesMasterId" 
WHERE 
  
  PMD."PackageModuleId" = "@packageModuleId"  and PMD."ChargeModuleDetailsId" is null 
  
    ;
  
END 
$BODY$;

ALTER FUNCTION public."udf_fetch_packageModuleDetails"(integer)
    OWNER TO postgres;
