-- FUNCTION: public.udf_DailySalesReportByMedication(text, text, text, text, text, integer, date, date, integer)

-- DROP FUNCTION IF EXISTS public."udf_DailySalesReportByMedication"(text, text, text, text, text, integer, date, date, integer);

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,
	"payTypeId" integer DEFAULT NULL::integer,
	"fromDate" date DEFAULT NULL::date,
	"toDate" date DEFAULT NULL::date,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("PharmacyProductId" integer, "ProductName" character varying, "BatchNumber" character varying, "GenericName" text, "CategoryName" character varying, "CompanyName" text, "SupplierName" text, "SaleQuantity" bigint, "Mrp" numeric, "Discount" numeric, "TotalAmount" numeric, "PaidVia" character varying, "PaymentNumber" character varying, "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."PaymentNumber",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" , PT."PayTypeName" as "PaidVia", PH."PaymentNumber",
	  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"	
left join "PayType" PT on PT."PayTypeId"=PH."PayTypeId"	
	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 "payTypeId" is null then 1=1  else PT."PayTypeId" ="payTypeId"  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" ,PT."PayTypeName" , PH."PaymentNumber",
 	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."PaymentNumber",A."SaleDate", (A."PPD_NetAmount"/A."PPD_Quantity") , A."PurchaseUnitQty",
A."LocationName"
order  by A."ProductName"
;

END
$BODY$;

ALTER FUNCTION public."udf_DailySalesReportByMedication"(text, text, text, text, text, integer, date, date, integer)
    OWNER TO postgres;