DROP FUNCTION IF EXISTS public."udf_PatientMedicationReport"(integer, text, text, text, integer, timestamp without time zone, timestamp without time zone, text);

CREATE OR REPLACE FUNCTION public."udf_PatientMedicationReport"(
	"patientId" integer DEFAULT NULL::integer,
	"uMRNo" text DEFAULT NULL::text,
	"patientMobile" text DEFAULT NULL::text,
	"billNumber" text DEFAULT NULL::text,
	"payTypeId" integer DEFAULT NULL::integer,
	"fromDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"toDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"locationId" text DEFAULT NULL::text)
    RETURNS TABLE("PatientName" text, "UMRNo" character varying, "PatientMobile" character varying, "BillNumber" character varying, "BillType" character varying, "SaleDate" timestamp without time zone, "OverallNetAmount" numeric, "ProductName" character varying, "GenericName" text, "CategoryName" character varying, "CompanyName" text, "BatchNumber" character varying, "PaidVia" character varying, "PaymentNumber" character varying, "Quantity" integer, "NetAmount" numeric, "TotalAmount" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
Declare 
BEGIN
return query

select P."FullName" "PatientName",P."UMRNo",p."Mobile" "PatientMobile" ,Ph."BillNumber",PH."BillType",
PH."CreatedDate" "SaleDate",PH."OverallNetAmount",PP."ProductName",pp."GenericName",
ci."Name" "CategoryName ",c."Name" "CompanyName",PRS."BatchNumber", PT."PayTypeName" as "PaidVia",PH."PaymentNumber" ,sum(PS."Quantity")::int "Quantity",
PS."NetAmount" ,sum(PS."NetAmount") "TotalAmount"

 from "PharmacySaleHeader" PH 
 join "PharmacySaleDetail" PS on PH."PharmacySaleHeaderId"=PS."PharmacySaleHeaderId"
 join "PharmacyProduct" pp on PS."PharmacyProductId"=PP."PharmacyProductId"
 join "PharmacyRetailStock" PRS on   PRS."PharmacyRetailStockId"=PS."PharmacyRetailStockId" and PRS."PharmacyProductId"=PP."PharmacyProductId"
 join "Company" c on c."CompanyId"=PP."CompanyId"
 join "LookupValue" ci on ci."LookupValueId"=pp."CategoryId"
 join "Patient" P on P."PatientId"=ph."PatientId" 
left join "PayType" PT on PT."PayTypeId"=PH."PayTypeId"
--where p."UMRNo" ='UMR21050206'
where case when "uMRNo" ='' then 1=1   when "uMRNo" is null then 1=1  else p."UMRNo" ilike  '%'||"uMRNo"||'%' end and 
--case when "patientName" ='' then 1=1   when "patientName" is null then 1=1  else P."FullName" ilike  '%'||"patientName"||'%' end and 
	case when "patientId" is null then 1=1 else P."PatientId"="patientId" end and
case when "patientMobile" ='' then 1=1   when "patientMobile" is null then 1=1  else p."Mobile"  ilike  '%'||"patientMobile"||'%' end and 
case when "billNumber" ='' then 1=1   when "billNumber" is null then 1=1  else Ph."BillNumber"  ilike  '%'||"billNumber"||'%' end  
and case  when "payTypeId" is null then 1=1  else PT."PayTypeId" ="payTypeId" end  
and case when "fromDate" is null then 1=1 else  "fromDate" <= PH."CreatedDate" and PH."CreatedDate" <= "toDate" end
and case when "locationId" is null then 1=1 else PH."LocationId" = "locationId"::int end
GROUP BY GROUPING SETS(( P."FullName",P."UMRNo",p."Mobile"  ,Ph."BillNumber",PH."CreatedDate",PH."BillType",
						PH."OverallNetAmount",PP."ProductName",pp."GenericName",
ci."Name" ,c."Name" ,PRS."BatchNumber",PT."PayTypeName", PH."PaymentNumber" ,PS."Quantity",PS."NetAmount" ),
					   ( P."FullName",Ph."BillNumber",PH."BillType"), ())  
order by Ph."CreatedDate" desc
;

END
$BODY$;

ALTER FUNCTION public."udf_PatientMedicationReport"(integer, text, text, text, integer, timestamp without time zone, timestamp without time zone, text)
    OWNER TO postgres;