CREATE OR REPLACE FUNCTION public."udf_PharmacyPurchaseReport1"("billNumber" text DEFAULT NULL::text, "billType" text DEFAULT NULL::text, "createdBy" integer DEFAULT NULL::integer, "supplierId" integer DEFAULT NULL::integer, "paidVia" text DEFAULT NULL::text, "dueDate" date DEFAULT NULL::date, "fromDate" timestamp without time zone DEFAULT (now())::timestamp without time zone, "toDate" timestamp without time zone DEFAULT (now())::timestamp without time zone, "pharmacyBillType" boolean DEFAULT NULL::boolean, "pharmacyWareHouseId" integer DEFAULT NULL::integer, "locationId" text DEFAULT NULL::text)
 RETURNS TABLE("PharmacyPurchaseHeaderId" integer, "DueDate" timestamp without time zone, "BillNumber" character varying, "BillType" character varying, "BillDate" timestamp without time zone, "CreatedByName" text, "RoleName" character varying, "SupplierName" text, "NetAmount" numeric, "WareHouseName" text, "LocationName" character varying)
 LANGUAGE plpgsql
AS $function$

BEGIN

return query
select  PH."PharmacyPurchaseHeaderId" ,PH."DueDate",PH."BillNumber",PH."BillType",PH."BillDate",ph."CreatedByName",
ph."Role",ph."SupplierName", PH."NetAmount",PH."WareHouseName",PH."LocationName" from 
(
select PH."PharmacyPurchaseHeaderId" ,PH."DueDate",PH."BillNumber",PH."BillType",PH."CreatedDate" "BillDate",A."FullName" 
"CreatedByName",R."RoleName" "Role",S."Name" "SupplierName",PH."Netamount" "NetAmount",true "PharmacyBillType"
,PW."WareHouseName",L."Name" as  "LocationName"
 from "PharmacyPurchaseHeader" PH
	join "Supplier" s on s."SupplierId"=ph."SupplierId"  
	join "Account" A on A."AccountId"=ph."CreatedBy"
	join "Role" R on R."RoleId"=A."RoleId"
	join "PharmacyWareHouse" PW on PW."PharmacyWareHouseId"=PH."PharmacyWareHouseId"
	left join "Location" L on L."LocationId" = PW."LocationId"
	left join "PharmacyPurchaseReturnHeader" pr on pr."PharmacyPurchaseHeaderId" = PH."PharmacyPurchaseHeaderId"
 where
 case when "billNumber" is null then 1=1 else  PH."BillNumber" ilike '%' || "billNumber" ||'%' end and
	case when "billType" is null then 1=1 else  PH."BillType" ilike '%' ||"billType"||'%' end and
 	case when "createdBy" is null then 1=1 else  A."AccountId"="createdBy" end and
  --  case when "supplierName" is null then 1=1 else S."Name" ilike '%' || "supplierName" ||'%' end 
	case when "supplierId" is null then 1=1 else ph."SupplierId" = "supplierId"::int end and
	    case when "paidVia" is null then 1=1 else PH."BillType" ilike '%' || "paidVia" ||'%' end and
 	case when "dueDate" is null then 1=1 else "dueDate" =PH."DueDate"::date end 
     and case when "fromDate" is null then 1=1 
 	else (PH."CreatedDate" >= "fromDate" and PH."CreatedDate" <= "toDate")  end and
	case when "pharmacyWareHouseId" is null then 1=1 else  PW."PharmacyWareHouseId" = "pharmacyWareHouseId" end and 
	case when "locationId" is null then 1=1 else (PW."LocationId" = "locationId"::int or PW."LocationId" is null) end
group by PH."PharmacyPurchaseHeaderId" ,PH."BillNumber",PH."BillType",PH."CreatedDate", PH."Netamount",S."Name",S."SupplierId",
A."FullName" ,R."RoleName",PW."WareHouseName",L."Name"

union

select PH."PharmacyPurchaseHeaderId" ,PH."DueDate",PH."BillNumber",PH."BillType",prh."CreatedDate" "BillDate",A."FullName" 
"CreatedByName",R."RoleName" "Role",S."Name" "SupplierName", -prh."OverallNetamount" "Netamount",false "PharmacyBillType"
,PW."WareHouseName",L."Name" as "LocationName"
from "PharmacyPurchaseReturnHeader" prh
	join "Account" A on A."AccountId"=prh."CreatedBy"
	join "Role" R on R."RoleId"=A."RoleId"
	Join "PharmacyPurchaseHeader" ph on ph."PharmacyPurchaseHeaderId"= prh."PharmacyPurchaseHeaderId"
	join "Supplier" s on s."SupplierId"=ph."SupplierId"  
	join "PharmacyWareHouse" PW on PW."PharmacyWareHouseId" =ph."PharmacyWareHouseId"
	left join "Location" L on L."LocationId" = PW."LocationId"
where	
 case when "billNumber" is null then 1=1 else  PH."BillNumber" ilike '%' || "billNumber" ||'%' end and
 	case when "billType" is null then 1=1 else  PH."BillType" ilike '%' ||"billType"||'%' end and
 	case when "createdBy" is null then 1=1 else  A."AccountId"="createdBy" end and
    -- case when "supplierName" is null then 1=1 else S."Name" ilike '%' || "supplierName" ||'%' end 
	case when "supplierId" is null then 1=1 else ph."SupplierId" = "supplierId"::int end 
 		 and   case when "paidVia" is null then 1=1 else PH."BillType" ilike '%' || "paidVia" ||'%' end and
 	case when "dueDate" is null then 1=1 else "dueDate" =PH."DueDate"::date end 
     --and case when "fromDate" is null then 1=1 else PH."BillDate" >= "fromDate" end 
 	--and case when "toDate" is null then 1=1 else PH."BillDate" <= "toDate" end 
 	and case when "fromDate" is null then 1=1 
 	else (PH."CreatedDate" >= "fromDate" and PH."CreatedDate" <= "toDate")  end and
	case when "pharmacyWareHouseId" is null then 1=1 else  PW."PharmacyWareHouseId" = "pharmacyWareHouseId" end and
	case when "locationId" is null then 1=1 else (PW."LocationId" = "locationId"::int or PW."LocationId" is null) end
group by PH."PharmacyPurchaseHeaderId" ,PH."DueDate",PH."BillNumber",PH."BillType",prh."CreatedDate",A."FullName" 
,R."RoleName" ,S."Name" ,s."SupplierId", prh."OverallNetamount",PW."WareHouseName",L."Name"
	) Ph
where case when "pharmacyBillType" is null then 1=1 
when "pharmacyBillType" = true then "PharmacyBillType" = true
when "pharmacyBillType" = false then "PharmacyBillType" = false
end 
order by PH."BillDate" desc, PH."PharmacyPurchaseHeaderId";
END
$function$
;
