CREATE OR REPLACE FUNCTION public."widget_Table_otAppointmentDashboard"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Salutation" character varying, "PatientName" text, "UMRNo" character varying, "AppointmentNo" character varying, "AppointmentTime" text, "AppointmentId" integer, "SpecializationName" character varying, "SurgeryId" integer, "SurgeryName" text) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select Pa."Salutation",Pa."FullName" as "PatientName",Pa."UMRNo",
apt."AppointmentNo" ,-- apt."AppointmentTime"::text
 TO_CHAR(ot."SignInDate", 'hh12:mi AM'),
 apt."AppointmentId",s."SpecializationName",ot."SurgeryId",s1."Name" as "SurgeryName"
from "OTRegister" ot
--join "AppointmentType" AT on AT."AppointmentTypeId" =apt."AppointmentTypeId"
join "Patient" Pa on Pa."PatientId"  = ot."PatientId" 
join "Provider" Pr on Pr."ProviderId"  = ot."ProviderId" 
 join "Specialization" s on s."SpecializationId" = ANY (Pr."Specializations")
 join "Appointment" apt on apt."PatientId"=ot."PatientId"
 join "Surgery" s1 on s1."SurgeryId"=ot."SurgeryId"
where 
ot."SignInDate"::date="fromDate" and apt."Active" is true 
and case when "referenceId" is null then 1=1 else ot."ProviderId"= "referenceId" end
and case when "locationId" is null then 1=1 else ot."LocationId"= "locationId" end 
;

end
$BODY$;

ALTER FUNCTION public."widget_Table_otAppointmentDashboard"(date, integer, integer)
    OWNER TO postgres;
-------------------------------------------------------------------------------------------
-- FUNCTION: public.widget_Table_WalkinAppointmentDashboard(date, integer, integer)

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

CREATE OR REPLACE FUNCTION public."widget_Table_WalkinAppointmentDashboard"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Salutation" character varying, "PatientName" text, "UMRNo" character varying, "AppointmentNo" character varying, "AppointmentTime" text, "AppointmentId" integer, "SpecializationName" character varying) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select Pa."Salutation",Pa."FullName" as "PatientName",Pa."UMRNo",
apt."AppointmentNo" ,-- apt."AppointmentTime"::text
 TO_CHAR(apt."AppointmentTime", 'hh12:mi AM'),
 apt."AppointmentId",s."SpecializationName"
from "Appointment" apt
--join "AppointmentType" AT on AT."AppointmentTypeId" =apt."AppointmentTypeId"
join "Patient" Pa on Pa."PatientId"  = apt."PatientId" 
join "Provider" Pr on Pr."ProviderId"  = apt."ProviderId" 
 join "Specialization" s on s."SpecializationId" = ANY (Pr."Specializations")
where 
apt."AppointmentDate"::date="fromDate" and apt."Active" is true and apt."VisitTypeId" =9
and case when "referenceId" is null then 1=1 else apt."ProviderId"= "referenceId" end
and case when "locationId" is null then 1=1 else apt."LocationId"= "locationId" end 
;

end
$BODY$;

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

--------------------------------------------------------------------------
-- FUNCTION: public.widget_Table_OnlineAppointmentDashboard(date, integer, integer)

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

CREATE OR REPLACE FUNCTION public."widget_Table_OnlineAppointmentDashboard"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Salutation" character varying, "PatientName" text, "UMRNo" character varying, "AppointmentNo" character varying, "AppointmentTime" text, "AppointmentId" integer, "SpecializationName" character varying) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select Pa."Salutation", Pa."FullName" as "PatientName",Pa."UMRNo",
apt."AppointmentNo" ,-- apt."AppointmentTime"::text
 TO_CHAR(apt."AppointmentTime", 'hh12:mi AM'),
 apt."AppointmentId",s."SpecializationName"
from "Appointment" apt
join "AppointmentType" AT on AT."AppointmentTypeId" =apt."AppointmentTypeId"
join "Patient" Pa on Pa."PatientId"  = apt."PatientId" 
join "Provider" Pr on Pr."ProviderId"  = apt."ProviderId" 
join "Specialization" s on s."SpecializationId" = ANY (pa."Specializations")
where apt."AppointmentDate"::date="fromDate" and apt."Active" is true and AT."AppointmentTypeId" =3
and case when "referenceId" is null then 1=1 else apt."ProviderId"= "referenceId" end
and case when "locationId" is null then 1=1 else apt."LocationId"= "locationId" end 
;

end
$BODY$;

ALTER FUNCTION public."widget_Table_OnlineAppointmentDashboard"(date, integer, integer)
    OWNER TO postgres;
------------------------------------------------------------------------
-- FUNCTION: public.widget_Table_NewPatientAppointmentDashboard(date, integer, integer)

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

CREATE OR REPLACE FUNCTION public."widget_Table_NewPatientAppointmentDashboard"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Salutation" character varying, "PatientName" text, "UMRNo" character varying, "AppointmentNo" character varying, "AppointmentTime" text, "AppointmentId" integer, "SpecializationName" character varying) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select  Pa."Salutation",Pa."FullName" as "PatientName",Pa."UMRNo",
apt."AppointmentNo" ,-- apt."AppointmentTime"::text
 TO_CHAR(apt."AppointmentTime", 'hh12:mi AM'),
 apt."AppointmentId",s."SpecializationName"
from "Appointment" apt
--join "AppointmentType" AT on AT."AppointmentTypeId" =apt."AppointmentTypeId"
join "Patient" Pa on Pa."PatientId"  = apt."PatientId" 
join "Provider" Pr on Pr."ProviderId"  = apt."ProviderId" 
join "Specialization" s on s."SpecializationId" = ANY (pr."Specializations")
where apt."AppointmentDate"::date="fromDate" and apt."Active" is true and apt."VisitTypeId" =43
and Pa."CreatedDate"::date="fromDate" and Pa."Active" is true
and case when "referenceId" is null then 1=1 else apt."ProviderId"= "referenceId" end
and case when "locationId" is null then 1=1 else apt."LocationId"= "locationId" end

;

end
$BODY$;

ALTER FUNCTION public."widget_Table_NewPatientAppointmentDashboard"(date, integer, integer)
    OWNER TO postgres;
-------------------------------------------------------------------------
-- FUNCTION: public.widget_Table_NewAppointmentDashboard(date, integer, integer)

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

CREATE OR REPLACE FUNCTION public."widget_Table_NewAppointmentDashboard"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Salutation" character varying, "PatientName" text, "UMRNo" character varying, "AppointmentNo" character varying, "AppointmentTime" text, "AppointmentId" integer, "SpecializationName" character varying) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query
select Pa."Salutation",Pa."FullName" as "PatientName",Pa."UMRNo",
apt."AppointmentNo" ,-- apt."AppointmentTime"::text
 TO_CHAR(apt."AppointmentTime", 'hh12:mi AM'),
 apt."AppointmentId",s."SpecializationName"
from "Appointment" apt
--left join "AppointmentType" AT on AT."AppointmentTypeId" =apt."AppointmentTypeId"
left join "Patient" Pa on Pa."PatientId"  = apt."PatientId" 
left join "Provider" Pr on Pr."ProviderId"  = apt."ProviderId" 
 join "Specialization" s on s."SpecializationId" = ANY (Pr."Specializations")
where apt."AppointmentDate"::date="fromDate" and apt."Active" is true and apt."VisitTypeId" <>10 
and case when "referenceId" is null then 1=1 else apt."ProviderId"="referenceId" end 
and case when "locationId" is null then 1=1 else apt."LocationId"= "locationId" end
--LIMIT 10 offset 0
;
end
$BODY$;

ALTER FUNCTION public."widget_Table_NewAppointmentDashboard"(date, integer, integer)
    OWNER TO postgres;
---------------------------------------------------------------------
-- FUNCTION: public.widget_Table_FollowUpAppointmentDashboard(date, integer, integer)

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

CREATE OR REPLACE FUNCTION public."widget_Table_FollowUpAppointmentDashboard"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Salutation" character varying, "PatientName" text, "UMRNo" character varying, "AppointmentNo" character varying, "AppointmentTime" text, "AppointmentId" integer, "SpecializationName" character varying) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select Pa."Salutation", Pa."FullName" as "PatientName",Pa."UMRNo",
apt."AppointmentNo" ,-- apt."AppointmentTime"::text
 TO_CHAR(apt."AppointmentTime", 'hh12:mi AM'),
 apt."AppointmentId",s."SpecializationName"
from "Appointment" apt
--join "AppointmentType" AT on AT."AppointmentTypeId" =apt."AppointmentTypeId"
join "Patient" Pa on Pa."PatientId"  = apt."PatientId" 
join "Provider" Pr on Pr."ProviderId"  = apt."ProviderId" 
 join "Specialization" s on s."SpecializationId" = ANY (Pr."Specializations")
where apt."AppointmentDate"::date="fromDate" and apt."VisitTypeId" =10
and case when "referenceId" is null then 1=1 else apt."ProviderId"= "referenceId" end
and case when "locationId" is null then 1=1 else apt."LocationId"= "locationId" end 
;

end
$BODY$;

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