Alter table "PharmacyIndentHeader" add column "LocationId" int references "Location"("LocationId");
--Select * from "Location"
--Update "PharmacyIndentHeader" set "LocationId" = 2;

Alter table "PharmacyRequestIndentHeader" add column "LocationId" int references "Location"("LocationId");
--Select * from "Location"

update "PharmacyRequestIndentHeader" set "LocationId" = 2;

DROP FUNCTION public."udf_DailySalesReportByMedication"(text, text, text, text, text, date, date);
DROP FUNCTION public."udf_DailySalesReportByMedication"(text, text, text, text, text, text, date, date);

CREATE OR REPLACE FUNCTION public."udf_DailySalesReportByMedication"(
	"productName" text DEFAULT NULL::text,
	"genericName" text DEFAULT NULL::text,
	"categoryName" text DEFAULT NULL::text,
	"companyName" text DEFAULT NULL::text,
	"supplierName" text DEFAULT NULL::text,
	"paidVia" text DEFAULT NULL::text,
	"fromDate" date DEFAULT NULL::date,
	"toDate" date DEFAULT NULL::date,
	"locationId" int default null)
    RETURNS TABLE("PharmacyProductId" integer, "ProductName" character varying, "BatchNumber" character varying, "GenericName" character varying, "CategoryName" character varying, "CompanyName" text, "SupplierName" text, "SaleQuantity" bigint, "Mrp" numeric, "Discount" numeric, "TotalAmount" numeric, "PaidVia" text, "SaleDate" timestamp without time zone, "PurchaseValue" numeric,
				  "PurchaseUnitQty" integer, "LocationName" character varying) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
Declare 
BEGIN
return query 
select A."PharmacyProductId",A."ProductName",A."BatchNumber",A."GenericName",A."CategoryName ",A."CompanyName"
,A."SupplierName"
,sum(A."Quantity") "SaleQuantity",A."Mrp",A."OverallDiscount" "Discount",sum(A."NetAmount") "TotalAmount"
,A."PaidVia",A."SaleDate" , TRUNC((A."PPD_NetAmount"/A."PPD_Quantity"),2)  as "PurchaseValue"
,A."PurchaseUnitQty",A."LocationName"
from (
select PS."PharmacyProductId",PP."ProductName",PRS."BatchNumber",pp."GenericName",ci."Name" "CategoryName ",c."Name" "CompanyName",s."Name" "SupplierName"
,PH."SaleDate",PS."Quantity",PRS."Mrp",Ps."Discount" "OverallDiscount",PS."NetAmount" , PH."PaidVia",
	  ppd."NetAmount" as "PPD_NetAmount",ppd."Quantity" as "PPD_Quantity",L."Name" as "LocationName"
	,pp."PurchaseUnitQty"
from "PharmacySaleHeader" PH
join "PharmacySaleDetail" PS on PH."PharmacySaleHeaderId"=PS."PharmacySaleHeaderId"
join "PharmacyProduct" pp on PS."PharmacyProductId"=PP."PharmacyProductId"
join "Company" c on c."CompanyId"=PP."CompanyId"
join "LookupValue" ci on ci."LookupValueId"=pp."CategoryId"
join "PharmacyRetailStock" PRS on   PRS."PharmacyRetailStockId"=PS."PharmacyRetailStockId" and PRS."PharmacyProductId"=PP."PharmacyProductId"
join "PharmacyPurchaseDetail" Ppd on  ppd."PharmacyStockId"=prs."PharmacyStockId" and  ppd."PharmacyProductId"=PRS."PharmacyProductId"
join "PharmacyPurchaseHeader" pph on pph."PharmacyPurchaseHeaderId"=Ppd."PharmacyPurchaseHeaderId"
join "Supplier" s on s."SupplierId"=pph."SupplierId" 
join "Location" L on L."LocationId" = PH."LocationId"	
	where case when "productName" ='' then 1=1   when "productName" is null then 1=1  else PP."ProductName" ilike  '%'||"productName"||'%' end and 
	case when "genericName" ='' then 1=1  when "genericName" is null then 1=1  else pp."GenericName" ilike  '%'||"genericName"||'%' end and 
  case when "categoryName" ='' then 1=1  when "categoryName" is null then 1=1  else ci."Name" ilike  '%'||"categoryName"||'%' end and 
	case when "companyName" ='' then 1=1  when "companyName" is null then 1=1  else c."Name" ilike  '%'||"companyName"||'%' end and 
  case when "supplierName" ='' then 1=1  when "supplierName" is null then 1=1  else s."Name" ilike  '%'||"supplierName"||'%' end  and
	   case when "paidVia" ='' then 1=1  when "paidVia" is null then 1=1  else PH."PaidVia" ilike  '%'||"paidVia"||'%' end  and

	case when "fromDate" is null then 1=1 else "fromDate" ::date <=PH."SaleDate"::date and PH."SaleDate"::date <="toDate" ::date end and
	case when "locationId" is null then 1=1 else PH."LocationId" = "locationId"::int end
	group by PS."PharmacyProductId",PP."ProductName",PRS."BatchNumber",PH."SaleDate"
	,pp."GenericName",ci."Name",c."Name" ,s."Name" ,PS."Quantity",PS."NetAmount",PRS."Mrp",Ps."Discount" ,PH."PaidVia" ,
 	ppd."NetAmount", ppd."Quantity",pp."PurchaseUnitQty",L."Name") A
	
group by A."PharmacyProductId",A."ProductName",A."BatchNumber",A."GenericName",A."CategoryName ",
A."CompanyName",A."SupplierName",A."Mrp",A."OverallDiscount",A."PaidVia",A."SaleDate", (A."PPD_NetAmount"/A."PPD_Quantity") , A."PurchaseUnitQty",
A."LocationName"
order  by A."ProductName"
;

END
$BODY$;

-----

DROP FUNCTION public."udf_PharmacySales_Report"(timestamp without time zone, timestamp without time zone, integer, character varying);

CREATE OR REPLACE FUNCTION public."udf_PharmacySales_Report"(
	"fromDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"toDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"createdBy" integer DEFAULT NULL::integer,
	"paidVia" character varying DEFAULT NULL::text,
    "locationId" text default null)
    RETURNS TABLE("SaleDate" timestamp without time zone, "TotalAmount" numeric, "TotalTaxes" numeric, "TotalDiscount" numeric, "TotalNetAmount" numeric, 
				  "PaidVia" text,"LocationName" character varying) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
BEGIN
return query

select PH."SaleDate" , sum(PH."Total") "Total", sum(PH."OverallTaxes") "OverallTaxes",
sum(PH."OverallDiscount") "OverallDiscount", 
sum(PH."OverallNetAmount") "OverallNetAmount", PH."PaidVia",L."Name" as "LocationName"

from "PharmacySaleHeader" PH
join "Location" L on L."LocationId" = PH."LocationId"
where case when "fromDate" is null then 1=1 else PH."SaleDate" >= "fromDate" end
and case when "toDate" is null then 1=1 else  PH."SaleDate" <= "toDate" end 
and case when "createdBy" is null then 1=1 else PH."CreatedBy"="createdBy" end   

and case when "paidVia" is null then 1=1 else PH."PaidVia" ilike'%'|| "paidVia"||'%' end
and case when "locationId" is null then 1=1 else PH."LocationId" = "locationId"::int end
group by PH."SaleDate",PH."PaidVia",L."Name" 
order by PH."SaleDate" desc;
END
$BODY$;

-----

DROP FUNCTION public."udf_PharmacySalesReport"(date, date);
DROP FUNCTION public."udf_PharmacySalesReport"(date, date, integer, character varying);
DROP FUNCTION public."udf_PharmacySalesReport"(date, date, character varying);

DROP FUNCTION public."udf_PharmacyBills_Report"(text, integer, character varying, character varying, character varying, integer, integer, timestamp without time zone, timestamp without time zone, text, integer);

CREATE OR REPLACE FUNCTION public."udf_PharmacyBills_Report"(
	"billNumber" text DEFAULT NULL::text,
	"patientId" integer DEFAULT NULL::integer,
	"patientMobile" character varying DEFAULT NULL::text,
	"uMRNo" character varying DEFAULT NULL::text,
	"paidVia" character varying DEFAULT NULL::text,
	"providerId" integer DEFAULT NULL::integer,
	"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,
	"retailName" text DEFAULT NULL::text,
	"retailPharmacyId" integer DEFAULT NULL::integer,
    "locationId" text default null::text)
    RETURNS TABLE("PharmacySaleHeaderId" integer, "BillNumber" character varying, "SaleDate" timestamp without time zone, "PatientName" character varying, "PatientMobile" character varying, "UMRNo" character varying, "PaidVia" text, "CreatedByName" text, "RoleName" character varying, "ProviderName" character varying, "TotalAmount" numeric, "RetailName" text, 
				  "OverallTaxes" numeric,"LocationName" character varying) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
BEGIN

return query
select PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."CreatedDate" "SaleDate",PH."PatientName",PH."Mobile" 
,Pa."UMRNo",PH."PaidVia",A."FullName" 
"CreatedByName",R."RoleName" "Role" ,
PH."ProviderName", PH."OverallNetAmount" ,RP."RetailName",PH."OverallTaxes",L."Name" as "LocationName"
from  "PharmacySaleHeader" PH
left join "Patient" Pa on Pa."PatientId"::text=PH."PatientId"::text
--join "Provider" pr on pr."ProviderId"::text=PH."ProviderId"::text
join "Account" A on A."AccountId"=PH."CreatedBy"
join "Role" R on R."RoleId"=A."RoleId"
	join "PharmacySaleDetail" PSD on PSD."PharmacySaleHeaderId"=PH."PharmacySaleHeaderId"
	join "PharmacyRetailStock" PRS on PRS."PharmacyRetailStockId"=PSD."PharmacyRetailStockId"
	join "RetailWareHouseLink" RWL on RWL."RetailWareHouseLinkId" = PRS."RetailWareHouseLinkId"
	join "RetailPharmacy" RP on RP."RetailPharmacyId"=RWL."RetailPharmacyId"
	join "Location" L on L."LocationId" = PH."LocationId"
where	case when "billNumber" is null then 1=1 else  PH."BillNumber" ilike '%' || "billNumber" ||'%' end and 
case when "patientId" is null then 1=1 else PH."PatientId"="patientId" end and
	
	case when "patientMobile" is null then 1=1 else  PH."Mobile" ilike '%' || "patientMobile" ||'%' end  and
	case when "uMRNo" is null then 1=1 else  Pa."UMRNo" ilike '%' || "uMRNo" ||'%' end  and
				case when "paidVia" is null then 1=1 else  PH."PaidVia" ilike '%' || "paidVia" ||'%' end  and
	case when "providerId" is null then 1=1 else  PH."ProviderId" ="providerId"  end  and
		case when "createdBy" is null then 1=1 else  A."AccountId"="createdBy" end and
 case when "fromDate" is null then 1=1 else PH."SaleDate" >= "fromDate" end and 
 case when "toDate" is null then 1=1 else PH."SaleDate" <= "toDate" end  and
 case when "retailName" is null then 1=1 else  RP."RetailName" ilike '%' || "retailName" ||'%' end and 
 	case when "retailPharmacyId" is null then 1=1 else  RP."RetailPharmacyId" = "retailPharmacyId" end and 
case when "locationId" is null then 1=1 else PH."LocationId" = "locationId"::int end
group by PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."SaleDate", PH."OverallNetAmount",PH."PatientName",PH."Mobile",Pa."UMRNo",PH."PaidVia",A."FullName" 
,R."RoleName" ,PH."ProviderName",RP."RetailName",PH."OverallTaxes",L."Name"
order by PH."SaleDate" desc;
END
$BODY$;
---------
DROP FUNCTION public."udf_PharmacyBills_FinalReport"(text, integer, integer, character varying, character varying, character varying, integer, integer, timestamp without time zone, timestamp without time zone, boolean);

 DROP FUNCTION public."udf_PharmacyBills_FinalReport"(text, integer, integer, character varying, character varying, character varying, integer, integer, timestamp without time zone, timestamp without time zone, text, integer, boolean);

CREATE OR REPLACE FUNCTION public."udf_PharmacyBills_FinalReport"(
	"billNumber" text DEFAULT NULL::text,
	"accountId" integer DEFAULT NULL::integer,
	"patientId" integer DEFAULT NULL::integer,
	"patientMobile" character varying DEFAULT NULL::text,
	"uMRNo" character varying DEFAULT NULL::text,
	"paidVia" character varying DEFAULT NULL::text,
	"providerId" integer DEFAULT NULL::integer,
	"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,
	"retailName" text DEFAULT NULL::text,
	"retailPharmacyId" integer DEFAULT NULL::integer,
	"locationId" text default null::text,
	"pharmacyBillType" boolean DEFAULT NULL::boolean)
    RETURNS TABLE("PharmacySaleHeaderId" integer, "BillNumber" character varying, "PaidVia" text, "SaleDate" timestamp without time zone, "PatientName" character varying, "PatientMobile" character varying, "UMRNo" character varying, "ProviderName" character varying, "CreatedByName" text, "RoleName" character varying, "TotalAmount" numeric, "SaleReturnHeaderId" integer, "OverallTaxes" numeric, "RetailName" text) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
BEGIN

return query

select PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",PH."SaleDate" "SaleDate",PH."PatientName",
PH."Mobile" ,Ph."UMRNo",PH."ProviderName",ph."CreatedByName",
ph."Role", PH."OverallNetAmount",PH."SaleReturnHeaderId" ,PH."OverallTaxes",PH."RetailName"
from 
(
select PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",PH."CreatedDate" "SaleDate",
	PH."PatientName",PH."Mobile" ,Pa."UMRNo",PH."ProviderName",A."FullName" 
"CreatedByName",R."RoleName" "Role" ,
	PH."OverallNetAmount",true "PharmacyBillType",null "SaleReturnHeaderId",PH."OverallTaxes",
	RP."RetailName"
from "PharmacySaleHeader" PH
	join "Account" A on A."AccountId"=PH."CreatedBy"
	join "Role" R on R."RoleId"=A."RoleId"
	join "PharmacySaleDetail" PSD on PSD."PharmacySaleHeaderId"=PH."PharmacySaleHeaderId"
	join "PharmacyRetailStock" PRS on PRS."PharmacyRetailStockId"=PSD."PharmacyRetailStockId"
	join "RetailWareHouseLink" RWL on RWL."RetailWareHouseLinkId" = PRS."RetailWareHouseLinkId"
	join "RetailPharmacy" RP on RP."RetailPharmacyId"=RWL."RetailPharmacyId"
left join "Patient" Pa on Pa."PatientId"::text=PH."PatientId"::text
		
where	case when "billNumber" is null then 1=1 else  PH."BillNumber" ilike '%' || "billNumber" ||'%' end and 
	case when "accountId" is null then 1=1 else PH."CreatedBy"="accountId" end and
	case when "patientId" is null then 1=1 else PH."PatientId"="patientId" end and
	case when "patientMobile" is null then 1=1 else  PH."Mobile" ilike '%' || "patientMobile" ||'%' end  and
	case when "uMRNo" is null then 1=1 else  Pa."UMRNo" ilike '%' || "uMRNo" ||'%' end  and
		case when "paidVia" is null then 1=1 else  PH."PaidVia" ilike '%' || "paidVia" ||'%' end  and
	case when "providerId" is null then 1=1 else  PH."ProviderId" = "providerId" end  and
		case when "createdBy" is null then 1=1 else  A."AccountId"="createdBy" end and
    case when "fromDate" is null then 1=1 else PH."SaleDate" >= "fromDate" end and
	case when "toDate" is null then 1=1 else PH."SaleDate" <= "toDate" end and
	case when "retailName" is null then 1=1 else  RP."RetailName" ilike '%' || "retailName" ||'%' end and
	case when "retailPharmacyId" is null then 1=1 else  RP."RetailPharmacyId" = "retailPharmacyId" end and
	case when "locationId" is null then 1=1 else PH."LocationId" = "locationId"::int end
group by PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",PH."SaleDate", PH."OverallNetAmount",
	PH."PatientName",PH."ProviderName",Pa."UMRNo",A."FullName" 
,R."RoleName",RP."RetailName"
union 
	
select PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",srh."ReturnDate" "SaleDate",PH."PatientName",
	Ph."Mobile" ,Pa."UMRNo",PH."ProviderName",A."FullName" 
"CreatedByName",R."RoleName" "Role", -srh."OverallNetAmount" "TotalAmount",false "PharmacyBillType"
,srh."SaleReturnHeaderId",srh."OverallTaxes",
	RP."RetailName"
	from "SaleReturnHeader" srh
Join "PharmacySaleHeader" ph on ph."PharmacySaleHeaderId"= srh."PharmacySaleHeaderId"
	join "Account" A on A."AccountId"=srh."CreatedBy"
		join "Role" R on R."RoleId"=A."RoleId"
	join "PharmacySaleDetail" PSD on PSD."PharmacySaleHeaderId"=PH."PharmacySaleHeaderId"
	join "PharmacyRetailStock" PRS on PRS."PharmacyRetailStockId"=PSD."PharmacyRetailStockId"
	join "RetailWareHouseLink" RWL on RWL."RetailWareHouseLinkId" = PRS."RetailWareHouseLinkId"
	join "RetailPharmacy" RP on RP."RetailPharmacyId"=RWL."RetailPharmacyId"
left join "Patient" Pa on Pa."PatientId"::text=PH."PatientId"::text
		
where case when "billNumber" is null then 1=1 else  PH."BillNumber" ilike '%' || "billNumber" ||'%' end  and
	case when "accountId" is null then 1=1 else srh."CreatedBy"="accountId" end and
	case when "patientId" is null then 1=1 else PH."PatientId"="patientId" end and
	case when "patientMobile" is null then 1=1 else  PH."Mobile" ilike '%' || "patientMobile" ||'%' end  and
	case when "uMRNo" is null then 1=1 else  Pa."UMRNo" ilike '%' || "uMRNo" ||'%' end  and
			case when "paidVia" is null then 1=1 else  PH."PaidVia" ilike '%' || "paidVia" ||'%' end  and
	case when "providerId" is null then 1=1 else  PH."ProviderId" = "providerId"  end  and
		case when "createdBy" is null then 1=1 else  A."AccountId"="createdBy" end and
 case when "fromDate" is null then 1=1 else srh."ReturnDate" >= "fromDate" end and 
	case when "toDate" is null then 1=1 else srh."ReturnDate" <= "toDate" end and
	case when "retailName" is null then 1=1 else  RP."RetailName" ilike '%' || "retailName" ||'%' end and
		case when "retailPharmacyId" is null then 1=1 else  RP."RetailPharmacyId" = "retailPharmacyId" end and
	case when "locationId" is null then 1=1 else ph."LocationId" = "locationId"::int end
group by PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",srh."ReturnDate", srh."OverallNetAmount",
	PH."PatientName",Pa."Mobile",Pa."UMRNo",PH."ProviderName",A."FullName" 
,R."RoleName",srh."SaleReturnHeaderId", RP."RetailName"
) 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."SaleDate" desc, PH."PharmacySaleHeaderId";
END
$BODY$;

------------


DROP FUNCTION 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,
    "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, "SupplierId" integer, "NetAmount" numeric, "WareHouseName" text,
				 "LocationName" character varying) 
    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",PH."LocationName" 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",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"
	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 
	 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 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",S."SupplierId", -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"
	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 
 		 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 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
$BODY$;

-----------------

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

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" character varying, "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 PWH."LocationId" = "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 PWH."LocationId" = "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$;
-----------

DROP FUNCTION public."udf_PatientMedicationReport"(integer, text, text, text);

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

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,
	"paidVia" text DEFAULT NULL::text,
	"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" character varying, "CategoryName" character varying, "CompanyName" text, "BatchNumber" character varying, "PaidVia" text, "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", PH."PaidVia" ,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" 
--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 "paidVia" ='' then 1=1   when "paidVia" is null then 1=1  else Ph."PaidVia"  ilike  '%'||"paidVia"||'%' 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",PH."PaidVia" ,PS."Quantity",PS."NetAmount" ),
					   ( P."FullName",Ph."BillNumber",PH."BillType"), ())  
order by Ph."CreatedDate" desc
;

END
$BODY$;

--------


-- Not Completed yet
--DROP FUNCTION public."udf_fetch_Admission"(text, integer, integer, date, date, integer, integer);
-- DROP FUNCTION public."udf_fetch_Admission"(text, integer, integer, boolean, date, date, integer, integer);
-- DROP FUNCTION public."udf_fetch_Admission"(text, integer, integer, boolean, text, date, date, date, text, integer, integer);
-- DROP FUNCTION public."udf_fetch_Admission"(text, integer, integer, boolean, text, date, date, date, text, boolean, integer, integer, integer);
-- DROP FUNCTION public."udf_fetch_Admission"(text, integer, integer, boolean, text, date, date, date, text, boolean, integer, integer);
-- DROP FUNCTION public."udf_fetch_Admission"(text, integer, integer, boolean, text, date, date, integer, integer);
-- DROP FUNCTION public."udf_fetch_Admission"(text, integer, integer, boolean, text, date, date, text, integer, integer);
