DROP FUNCTION public."udf_InventoryPurchase_Report"(integer, text, text, integer, date, integer, timestamp without time zone, timestamp without time zone, boolean);


CREATE OR REPLACE FUNCTION public."udf_InventoryPurchase_Report"(
	"supplierId" integer DEFAULT NULL::integer,
	"purchaseBillNo" text DEFAULT NULL::text,
	"billType" text DEFAULT NULL::text,
	"wareHouseId" integer DEFAULT NULL::integer,
	"dueDate" date DEFAULT NULL::date,
	"createdBy" 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,
	"pharmacyBillType" boolean DEFAULT NULL::boolean,
        "locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("BillDate" timestamp without time zone, "WareHouseName" text, "PurchaseBillNo" character varying, "SupplierName" text, "DueDate" date, "BillAmount" numeric, "Discount" numeric, "Taxes" numeric, "NetAmount" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
BEGIN

return query
Select final."BillDate",final."WareHouseName",final."BillNumber" as "PurchaseBillNo",final."SupplierName",
final."DueDate",final."BillAmount",final."Discount",final."Taxes",final."NetAmount" from
(select PH."CreatedDate" "BillDate" ,IH."Name" as "WareHouseName", 
 PH."BillNumber",MP."Name" "SupplierName",PH."DueDate"::date as "DueDate",
	PH."BillAmount" "BillAmount",PH."Discount" "Discount" ,PH."Taxes" "Taxes",PH."NetAmount" "NetAmount", 
	true "PharmacyBillType"
 from "InventoryPurchaseHeader" PH
INNER JOIN "Supplier" MP on MP."SupplierId"=PH."SupplierId"  
 
left join "InventoryWareHouse" IH on IH."InventoryWareHouseId"=PH."InventoryWareHouseId"
where case when "purchaseBillNo" is not null then  PH."BillNumber" ilike '%'||"purchaseBillNo" ||'%' else 1=1 end 
and case when "supplierId" is null then 1=1 else MP."SupplierId"="supplierId" end 
and case when "billType" is null then 1=1 else  PH."BillType" ilike '%' ||"billType"||'%' end 
and case when "wareHouseId" is null then 1=1 else  PH."InventoryWareHouseId"="wareHouseId" end
and case when "locationId" is null then 1=1 else  PH."LocationId"="locationId" end
and case when "createdBy" is null then 1=1 else PH."CreatedBy"="createdBy" end 
and case when "dueDate" is null then 1=1 else PH."DueDate"="dueDate" end and
case when "fromDate" is null then 1=1 else "fromDate" <=PH."CreatedDate" and PH."CreatedDate"<="toDate" end 
group by PH."CreatedDate" ,PH."BillNumber",IH."Name",MP."Name", PH."BillAmount",PH."Discount",PH."Taxes", PH."NetAmount",PH."DueDate"::date
union   
select srh."ReturnDate" "BillDate",''::text as "WareHouseName", srh."BillNumber",MP."Name" "SupplierName",null::date as "DueDate",srh."ReturnAmount" "BillAmount",
	srh."Discount" "Discount" ,srh."Taxes" "Taxes", -srh."NetAmount" "TotalAmount",false "PharmacyBillType"
 from "InventoryPurchaseReturnHeader" srh
--Join "InventoryPurchaseHeader" ph on ph."InventoryPurchaseHeaderId"= srh."InventoryPurchaseHeaderId"
inner JOIN "Supplier" MP on MP."SupplierId"=srh."SupplierId"  
where case when "purchaseBillNo" is not null then  srh."BillNumber" ilike '%'||"purchaseBillNo" ||'%' else 1=1 end 
and case when "supplierId" is null then 1=1 else MP."SupplierId"="supplierId" end and  
-- case when "billType" is null then 1=1 else  srh."BillType" ilike '%' ||"billType"||'%' end 
 
case when "fromDate" is null then 1=1 else "fromDate" <= srh."CreatedDate" and srh."CreatedDate"
<="toDate" end 
group by srh."ReturnDate" ,srh."BillNumber",MP."Name", srh."ReturnAmount",srh."Discount"
	,srh."Taxes" ,srh."NetAmount" ) final
	
 where case when "pharmacyBillType" is null then 1=1 
when "pharmacyBillType" = true then "PharmacyBillType" = true
when "pharmacyBillType" = false then "PharmacyBillType" = false
  end and  final."WareHouseName"!=''
  order by final."BillDate" desc;
END
$BODY$;

	
	
	
