-- FUNCTION: public.udf_ProductPurchaseReport(text, text, integer, text, text, date, date, boolean, text)

 DROP FUNCTION IF EXISTS public."udf_ProductPurchaseReport"(text, text, integer, text, text, date, date, boolean, text);

CREATE OR REPLACE FUNCTION public."udf_ProductPurchaseReport"(
	"billNumber" text DEFAULT NULL::text,
	"billType" text DEFAULT NULL::text,
	"createdBy" integer DEFAULT NULL::integer,
	"supplierName" text DEFAULT NULL::text,
	"productName" text DEFAULT NULL::text,
	"fromDate" date DEFAULT NULL::date,
	"toDate" date DEFAULT NULL::date,
	"pharmacyBillType" boolean DEFAULT NULL::boolean,
	"locationId" text DEFAULT NULL::text)
    RETURNS TABLE("ProductName" character varying, "GenericName" text, "CategoryName" character varying, "PurchaseRate" numeric, "Quantity" numeric, "Mrp" numeric, "TaxAmount" numeric, "PharmacyPurchaseHeaderId" integer, "BillNumber" character varying, "BillType" character varying, "BillDate" timestamp without time zone, "CreatedByName" text, "RoleName" character varying, "SupplierName" text, "NetAmount" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
BEGIN

return query
select  PH."ProductName",PH."GenericName",PH."CategoryName",PH."PurchaseRate",PH."Quantity",PH."Mrp",PH."TaxAmount", PH."PharmacyPurchaseHeaderId" ,PH."BillNumber",PH."BillType",PH."BillDate",ph."CreatedByName",
ph."Role",ph."SupplierName", PH."NetAmount" from 
(
select PP."ProductName",PP."GenericName",ci."Name" "CategoryName",PPD."PurchaseRate",PPD."Quantity",PPD."Mrp",PPD."TaxAmount", PH."PharmacyPurchaseHeaderId" ,PH."BillNumber",PH."BillType",PH."CreatedDate" "BillDate",A."FullName" 
"CreatedByName",R."RoleName" "Role",S."Name" "SupplierName", ppd."NetAmount" "NetAmount",true "PharmacyBillType"
 from "PharmacyPurchaseHeader" PH
	join "Supplier" s on s."SupplierId"=ph."SupplierId"
	join "PharmacyPurchaseDetail" ppd on ppd."PharmacyPurchaseHeaderId" = ph."PharmacyPurchaseHeaderId"
	join "PharmacyProduct" pp on pp."PharmacyProductId" = ppd."PharmacyProductId"
	join "LookupValue" ci on ci."LookupValueId"=pp."CategoryId"
	join "Account" A on A."AccountId"=ph."CreatedBy"
	join "Role" R on R."RoleId"=A."RoleId"
	join "PharmacyWareHouse" PWH on PWH."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 "productName" is null then 1=1 else pp."ProductName" ilike '%' || "productName" ||'%' end 
    and case when "fromDate" is null then 1=1 else PH."BillDate"::date >= "fromDate" end 
	and case when "toDate" is null then 1=1 else PH."BillDate"::date <= "toDate" end and
	case when "locationId" is null then 1=1 else coalesce(PWH."LocationId",PWH."CentralWarehouseLocationId") = "locationId"::int end
group by PP."ProductName",PP."GenericName",ci."Name",PPD."PurchaseRate",PPD."Quantity",PPD."Mrp",PPD."TaxAmount", PH."PharmacyPurchaseHeaderId" ,PH."BillNumber",PH."BillType",PH."CreatedDate", ppd."NetAmount",S."Name",
A."FullName" ,R."RoleName"
union 
select PP."ProductName",PP."GenericName",ci."Name" "CategoryName",PPD."PurchaseRate",
PrD."ReturnQuantity" "Quantity",PPD."Mrp",PrD."TaxAmount", PH."PharmacyPurchaseHeaderId" ,PH."BillNumber",PH."BillType",prh."CreatedDate" "BillDate",A."FullName" 
"CreatedByName",R."RoleName" "Role",S."Name" "SupplierName", -prd."NetAmount" "Netamount",false "PharmacyBillType"
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 "PharmacyWareHouse" PWH on PWH."PharmacyWareHouseId" = ph."PharmacyWareHouseId"
	join "PharmacyPurchaseDetail" ppd on ppd."PharmacyPurchaseHeaderId" = ph."PharmacyPurchaseHeaderId"
	join "PharmacyProduct" pp on pp."PharmacyProductId" = ppd."PharmacyProductId"
	join "LookupValue" ci on ci."LookupValueId"=pp."CategoryId"
	join "Supplier" s on s."SupplierId"=ph."SupplierId" 
	join  "PharmacyPurchaseReturnDetail" prd on prh."PharmacyPurchaseReturnHeaderId"= prd."PharmacyPurchaseReturnHeaderId"
	and  ppd."PharmacyProductId"=prd."PharmacyProductId"
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 "productName" is null then 1=1 else pp."ProductName" ilike '%' || "productName" ||'%' end 
    and case when "fromDate" is null then 1=1 else PH."BillDate"::date >= "fromDate" end 
	and case when "toDate" is null then 1=1 else PH."BillDate"::date <= "toDate" end and
	case when "locationId" is null then 1=1 else coalesce(PWH."LocationId",PWH."CentralWarehouseLocationId") = "locationId"::int end
group by PP."ProductName",PP."GenericName",ci."Name",PPD."PurchaseRate",PrD."ReturnQuantity",PPD."Mrp",PrD."TaxAmount",PH."PharmacyPurchaseHeaderId" ,
PH."BillNumber",PH."BillType",prh."CreatedDate",A."FullName" 
,R."RoleName" ,S."Name" , prd."NetAmount"
	) 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_ProductPurchaseReport"(text, text, integer, text, text, date, date, boolean, text)
    OWNER TO postgres;