-- FUNCTION: public.udf_PharmacyPurchaseReport(text, text, integer, text, text, date, timestamp without time zone, timestamp without time zone, boolean, integer)

-- DROP FUNCTION IF EXISTS public."udf_PharmacyPurchaseReport"(text, text, integer, text, text, date, timestamp without time zone, timestamp without time zone, boolean, integer);

CREATE OR REPLACE FUNCTION public."udf_PharmacyPurchaseReport"(
	"billNumber" text DEFAULT NULL::text,
	"billType" text DEFAULT NULL::text,
	"createdBy" integer DEFAULT NULL::integer,
	"supplierName" text DEFAULT NULL::text,
	"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)
    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, "SupplierId" integer, "NetAmount" numeric, "WareHouseName" text) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
BEGIN

return query
select  PH."PharmacyPurchaseHeaderId" ,PH."DueDate",PH."BillNumber",PH."BillType",PH."BillDate",ph."CreatedByName",
ph."Role",ph."SupplierName",PH."SupplierId", PH."NetAmount",PH."WareHouseName" from 
(
select PH."PharmacyPurchaseHeaderId" ,PH."DueDate",PH."BillNumber",PH."BillType",PH."CreatedDate" "BillDate",A."FullName" 
"CreatedByName",R."RoleName" "Role",S."Name" "SupplierName",S."SupplierId" ,PH."Netamount" "NetAmount",true "PharmacyBillType"
,PW."WareHouseName"
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 "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 
	 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
group by PH."PharmacyPurchaseHeaderId" ,PH."BillNumber",PH."BillType",PH."CreatedDate", PH."Netamount",S."Name",S."SupplierId",
A."FullName" ,R."RoleName",PW."WareHouseName"

union

select PH."PharmacyPurchaseHeaderId" ,PH."DueDate",PH."BillNumber",PH."BillType",prh."CreatedDate" "BillDate",A."FullName" 
"CreatedByName",R."RoleName" "Role",S."Name" "SupplierName",S."SupplierId", -prh."OverallNetamount" "Netamount",false "PharmacyBillType"
,PW."WareHouseName"
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"
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 
 		 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
group by PH."PharmacyPurchaseHeaderId" ,PH."DueDate",PH."BillNumber",PH."BillType",prh."CreatedDate",A."FullName" 
,R."RoleName" ,S."Name" ,s."SupplierId", prh."OverallNetamount",PW."WareHouseName"
	) 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
$BODY$;

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