Pa."FullName" "PatientName",Pa."UMRNo",Pa."Age" "PatientAge",Pa."Gender" as "PatientGender",pa."Mobile" "patientMobile",Pr."Age" "ProviderAge",Pr."Gender" as "ProviderGender",
sum(coalesce(Re."Cost",0))-coalesce((select sum(Rfd."Cost") from "Receipt" Rfd where Rfd."AdmissionId"=A."AdmissionId" and "ReceiptTypeId"=2 and Rfd."Active"=true and Rfd."IsRefunded" =true ),0) "PaidAmount"
,
FB."FinalAmount" ,
case when FB."FinalAmount"-(sum(coalesce(Re."Cost",0))-coalesce((select sum(Rfd."Cost") from "Receipt" Rfd where Rfd."AdmissionId"=A."AdmissionId" and "ReceiptTypeId"=2 and Rfd."Active"=true and Rfd."IsRefunded" =true ),0))=0 then 'Payment Cleared'
when FB."FinalAmount"-(sum(coalesce(Re."Cost",0))-coalesce((select sum(Rfd."Cost") from "Receipt" Rfd where Rfd."AdmissionId"=A."AdmissionId" and "ReceiptTypeId"=2 and Rfd."Active"=true and Rfd."IsRefunded" =true ),0))>0 then 'Payment Due'
when FB."FinalAmount"-(sum(coalesce(Re."Cost",0))-coalesce((select sum(Rfd."Cost") from "Receipt" Rfd where Rfd."AdmissionId"=A."AdmissionId" and "ReceiptTypeId"=2 and Rfd."Active"=true and Rfd."IsRefunded" =true ),0))<0 then 'Pending Refund' end "PaymentStatus",
case when FB."FinalBillId" is null then false else true end "IsFinalBill",(select T."TotalItems" from TotalItems T) "TotalItems",
A."Active",
A."ExpectedDischargeDate",
A."DischargedBy" ,
case when RL."RoleId" = '3' then DP."DepartmentName"::TEXT else RL."RoleName"::TEXT end as "DischargedByRole",
case when RL."RoleId" = '3' then PRO."FullName" else AC."FullName" end as "DischargedByName",
l."Name" "LocationName"
from "Admission" A
join "Location" l on l."LocationId" = A."LocationId"
join "Patient" Pa on A."PatientId"=Pa."PatientId"
join "Provider" Pr on A."ProviderId"=Pr."ProviderId"
join "Department" D on D."DepartmentId"=A."DepartmentId"
left join "Bed" B on B."BedId"=A."BedId"
left join "Room" R on B."RoomId"=R."RoomId"
left join "Ward" W on w."WardId"=R."WardId"
---left join "PatientFamily" pf on pf."PatientFamilyId"=A."PatientFamilyId"
left join "SurgeryType" st on st."SurgeryTypeId"=A."SurgeryTypeId"
left join "Discharge" ds on ds."AdmissionId"=A."AdmissionId" and ds."Active"=true
left join "DischargeStatus" dss on ds."DischargeStatusId"=dss."DischargeStatusId"
left join "Receipt" Re on A."AdmissionId" = Re."AdmissionId" and Re."ReceiptTypeId"=1 and Re."Active"=true
---left join "Receipt" Rf on Rf."ReceiptTypeId"=2 and A."AdmissionId"= Rf ."AdmissionId" and Rf."Active"=true and Rf."IsRefunded" =true
left join "FinalBill" FB on A."AdmissionId"= FB."AdmissionId" and FB."Active"=true
left join "Account" AC on AC."AccountId"=A."DischargedBy"
left join "Role" RL on RL."RoleId"= AC."RoleId"
left join "Provider" PRO on PRO."ProviderId"=AC."ReferenceId"
left join "Department" DP on DP."DepartmentId"=PRO."DepartmentId"
where l."LocationId" = "locationId"
And
case when "admissionNo" is null then 1=1
when "admissionNo"='' then 1=1
else A."AdmissionNo" ilike '%'||"admissionNo"||'%' end and
case when "patientMobileNo" is null then 1=1
when "patientMobileNo"='' then 1=1
else pa."Mobile" ilike '%'||"patientMobileNo"||'%' end and
case when "patientId" is null then 1=1
else Pa."PatientId"= "patientId" end and
case when "active" is null then 1=1
else A."Active"= "active" end and
case when "providerId" is null then 1=1
else Pr."ProviderId" = "providerId" end and
case when "isDischarged" is null then 1=1
when "isDischarged" =true then ds."AdmissionId"=A."AdmissionId"
when "isDischarged" =false then ds."AdmissionId" is null
end and
case when "fromDate" is null then 1=1 else "fromDate" <=A."AdmissionDate"::date and A."AdmissionDate"::date <="toDate" end
AND
case when "dischargeDate" is null then 1=1 else "dischargeDate" =ds."DischargeDate"::date end
and
case when "uMRNo" is null then 1=1
when "uMRNo"='' then 1=1
else Pa."UMRNo" ilike '%'||"uMRNo"||'%' end
group by A."AdmissionId",A."AdmissionNo",A."IsConvertedFromOPtoIp",A."AdmissionDate",A."AdmissionTime",
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)
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"
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)
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,
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)
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",
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)
Pa."FullName" "PatientName",Pa."UMRNo",Pa."Age" "PatientAge",Pa."Gender" as "PatientGender",pa."Mobile" "patientMobile",Pr."Age" "ProviderAge",Pr."Gender" as "ProviderGender",
sum(coalesce(Re."Cost",0))-coalesce((select sum(Rfd."Cost") from "Receipt" Rfd where Rfd."AdmissionId"=A."AdmissionId" and "ReceiptTypeId"=2 and Rfd."Active"=true and Rfd."IsRefunded" =true ),0) "PaidAmount"
,
FB."FinalAmount" ,
case when FB."FinalAmount"-(sum(coalesce(Re."Cost",0))-coalesce((select sum(Rfd."Cost") from "Receipt" Rfd where Rfd."AdmissionId"=A."AdmissionId" and "ReceiptTypeId"=2 and Rfd."Active"=true and Rfd."IsRefunded" =true ),0))=0 then 'Payment Cleared'
when FB."FinalAmount"-(sum(coalesce(Re."Cost",0))-coalesce((select sum(Rfd."Cost") from "Receipt" Rfd where Rfd."AdmissionId"=A."AdmissionId" and "ReceiptTypeId"=2 and Rfd."Active"=true and Rfd."IsRefunded" =true ),0))>0 then 'Payment Due'
when FB."FinalAmount"-(sum(coalesce(Re."Cost",0))-coalesce((select sum(Rfd."Cost") from "Receipt" Rfd where Rfd."AdmissionId"=A."AdmissionId" and "ReceiptTypeId"=2 and Rfd."Active"=true and Rfd."IsRefunded" =true ),0))<0 then 'Pending Refund' end "PaymentStatus",
case when FB."FinalBillId" is null then false else true end "IsFinalBill",(select T."TotalItems" from TotalItems T) "TotalItems",
A."Active",
A."ExpectedDischargeDate",
A."DischargedBy" ,
case when RL."RoleId" = '3' then DP."DepartmentName"::TEXT else RL."RoleName"::TEXT end as "DischargedByRole",
case when RL."RoleId" = '3' then PRO."FullName" else AC."FullName" end as "DischargedByName",
create table "DepartmentConsumption"("DepartmentConsumptionId" serial primary key,"PharmacyDepartmentalStockId" int references "PharmacyDepartmentalStock"("PharmacyDepartmentalStockId"),
"QuantityConsumed" int not null,"ConsumedBy" int references "Account"("AccountId"),"CreatedDate" timestamp without time zone,
"ReasonForConsumption" varchar(300));
alter table "DepartmentConsumption" add column "CreatedBy" int references "Account"("AccountId");
DROP FUNCTION public."udf_DailySalesReportByMedication"(text, text, text, text, text, text, 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" character varying, "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)
-- 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, text, 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, text, 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,
"payTypeId" integer DEFAULT NULL::integer,
"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" character varying, "PaymentNumber" character varying, "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)
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$;
--ALTER 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, text, boolean)
-- 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, text)
--
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, text);
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,
"payTypeId" integer DEFAULT NULL::integer,
"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" character varying, "PaymentNumber" character varying,
"CreatedByName" text, "RoleName" character varying, "ProviderName" character varying,
,Pa."UMRNo",PT."PayTypeName" as "PaidVia",PH."PaymentNumber",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"
left join "PayType" PT on PT."PayTypeId"=PH."PayTypeId"
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 "payTypeId" is null then 1=1 else PT."PayTypeId" = "payTypeId" 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",PT."PayTypeName", PH."PaymentNumber", A."FullName"
--ALTER 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, text)
alter table "LabTemplateDetail" add column "LabParameterHeaderId" int references "LabParameterHeader"("LabParameterHeaderId");
-----------------------
drop table "LabMainDetailCharge";
----------
CREATE OR REPLACE FUNCTION public."fetch_LabParameterDetails_on_conditions"(agetype text DEFAULT NULL::text, headerid integer DEFAULT NULL::integer, fromage integer DEFAULT NULL::integer, gendervalue text DEFAULT NULL::text)
RETURNS TABLE("LabParameterDetailId" bigint, "LabParameterHeaderId" integer, "Gender" character varying, "FromAgeType" character varying, "ToAge" integer, "ToAgeType" character varying, "MinValue" numeric, "MaxValue" numeric, "MinCriticalValue" numeric, "MaxCriticalValue" numeric, "UnitId" integer, "UnitName" character varying)
LANGUAGE plpgsql
AS $function$
declare labdata INTEGER := 0;
BEGIN
select count(*) into labdata
from "LabParameterDetail" a
where a."LabParameterHeaderId" = HeaderId and a."FromAgeType" = AgeType
and case when Gendervalue= '' then 1=1
when Gendervalue is null then 1=1
when Gendervalue= 'Male' then a."Gender" ='Male'
when Gendervalue= 'Female' then a."Gender" ='Female'
else coalesce(a."Gender",'All') = 'All' END
and (FromAge >= a."FromAge" and FromAge <= a."ToAge");
CREATE OR REPLACE FUNCTION public."udf_uiReport_fetch_Appointments_Location"(locationid integer DEFAULT NULL::integer, "appointmentNo" text DEFAULT NULL::text, "departmentId" integer[] DEFAULT NULL::integer[], "providerId" integer[] DEFAULT NULL::integer[], "patientId" integer[] DEFAULT NULL::integer[], "uMRNo" text DEFAULT NULL::text, "referredBy" text DEFAULT NULL::text, "referredByName" text DEFAULT NULL::text, mobile text DEFAULT NULL::text, "paymentType" text DEFAULT NULL::text, "fromDate" date DEFAULT NULL::date, "toDate" date DEFAULT NULL::date, "pageIndex" integer DEFAULT 0, "pageSize" integer DEFAULT 10)
RETURNS TABLE("DepartmentId" text, "DepartmentName" character varying, "ProviderId" text, "ProviderName" character varying, "FollowUpForAppointmentId" integer, "AppointmentDate" date, "AppointmentNo" character varying, "PatientId" integer, "ReferredBy" character varying, "ReferredByName" character varying, "PatientName" character varying, "PatientAge" smallint, "UMRNo" character varying, "Mobile" character varying, "PatientGender" character, "AppointmentTime" time without time zone, "VisitType" character, "PaymentType" character varying, "TotalAppointments" bigint, "TotalAmount" bigint, "ReceiptCreatedByName" text, "ReceiptDate" timestamp without time zone, "ReceiptId" integer, "StreetAdress" character varying, "City" character varying, "State" character varying, "FatherOrHusband" text)
LANGUAGE plpgsql
AS $function$
BEGIN
return query
with cts as (select coalesce(A."DepartmentId"::text,'GrandTotal') "DepartmentId",
Create table "HWCPatient" ("HWCPatientId" serial primary key, "HWCName" varchar(250), "Description" text, "LocationId" int references "Location"("LocationId"),
"Active" boolean default true,"CreatedBy" int references "Account"("AccountId"),"CreatedDate" timestamp without time zone,"ModifiedBy" int references "Account"("AccountId"),
"ModifiedDate" timestamp without time zone);
insert into "LogType"("LogTypeId","LogTypeName","Active") values(50,'HWCPatients',true);
alter table "Patient" add column "HWCPatientId" int references "HWCPatient"("HWCPatientId");
Alter table "HWCPatient" add column "RowColor" text;
------------------
drop function "udf_FetchPatients_For_QuickSchedule"(character varying,character varying,character varying);
CREATE OR REPLACE FUNCTION public."udf_FetchPatients_For_QuickSchedule"(filter character varying DEFAULT NULL::text, "patientMobile" character varying DEFAULT NULL::text, "patientName" character varying DEFAULT NULL::text)
select row_number() over(partition by a."PatientId" order by a."AppointmentId" desc, a."AppointmentDate" desc )"Max" ,a."PatientId",a."AppointmentId" from "Appointment" a
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
--and case when "filter" is null then 1=1 else (TRIM(UPPER(pat."FullName")) ILIKE '%' OR pat."UMRNo" ilike'%' OR pat."Mobile" ILIKE '%')|| "filter"||'%' end
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxapt as (
select a.* from "Appointment" a
join cts on a."AppointmentId"=cts."AppointmentId"
where "Max"=1)
,ctsadmission as (
select distinct max(a."AdmissionId") over(partition by a."PatientId" )"MaxAdmissionId" ,a."PatientId",
a."AppointmentId" from "Admission" a
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
--and case when "filter" is null then 1=1 else (TRIM(UPPER(pat."FullName")) ILIKE '%' OR pat."UMRNo" ilike'%' OR pat."Mobile" ILIKE '%') || "filter"||'%' end
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxadmision as (
select a.* from "Admission" a
join ctsadmission cts on a."AdmissionId"=cts."MaxAdmissionId"
)
SELECT DISTINCT pat."PatientId" ,
pat."Salutation" ,
pat."FirstName" ,
pat."MiddleName" ,
pat."LastName" ,
pat."FullName" ,
pat."DateOfBirth" ,
pat."Age" ,
pat."Gender" ,
pat."UMRNo" ,
pat."Email" ,
pat."Mobile" ,
pat."CountryId" ,
pat."ProfileImageUrl" ,
-- pat."ThumbnailUrl" ,
pat."Active" ,
(CASE WHEN pat."ThumbnailUrl" IS NOT NULL THEN CONCAT('https://hims-devv.s3.amazonaws.com/', pat."Guid", '/', pat."ThumbnailUrl") ELSE NULL END) AS "ThumbnailUrl"
,(max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId",apt."AppointmentDate")::text)::timestamp as "AppointmentDate",
(case when (max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId")::text)::timestamp > now() at time zone 'UTC-5:30' then true else false end)"isActiveAppointmentExists"
,(case when max(adm."AdmissionId")over(partition by adm."PatientId") is null then false
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is null then true
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is not null then false end
left join maxapt apt on apt."PatientId" = pat."PatientId" and apt."Status" <> 'C' and apt."Active"=true
left join maxadmision adm on adm."PatientId" = pat."PatientId" and adm."Active"=true
left join "Discharge" dc on dc."AdmissionId" = adm."AdmissionId"
left join "HWCPatient" HWC on HWC."HWCPatientId" = pat."HWCPatientId"
WHERE pat."Active" IS TRUE
--and case when "filter" is null then 1=1 else (TRIM(UPPER(pat."FullName")) ILIKE '%' OR pat."UMRNo" ilike'%' OR pat."Mobile" ILIKE '%')|| "filter"||'%' end
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
JOIN "Location" PL ON A."LocationId" = PL."LocationId" AND PL."Active" = TRUE
JOIN "Practice" P ON P."PracticeId" = PL."PracticeId" AND P."Active" = TRUE
where A."ProviderId"=providerid AND A."Active" IS TRUE
)
,Availability as (
select A."ProviderId",A."ProviderLocationId",A."LocationId",json_array_elements(case when (A."Availability" is null or A."Availability"='') then '[]' else (A."Availability"::json)end) "Availability"
from "ProviderLocation" A
JOIN "Location" PL ON A."LocationId" = PL."LocationId" AND PL."Active" = TRUE
JOIN "Practice" P ON P."PracticeId" = PL."PracticeId" AND P."Active" = TRUE
where A."ProviderId"=providerid AND A."Active" IS TRUE
--and case when providerLocationId is null then 1=1 else A."ProviderLocationId"=providerLocationId end
A."LocationId",extract(ISODOW from "Dates")"DateNo", CASE
WHEN extract(ISODOW from "Dates") =1 THEN 'Monday'
WHEN extract(ISODOW from "Dates") =2 THEN 'Tuesday'
WHEN extract(ISODOW from "Dates")=3 THEN 'Wednesday'
WHEN extract(ISODOW from "Dates")=4 THEN 'Thursday'
WHEN extract(ISODOW from "Dates")=5 THEN 'Friday'
WHEN extract(ISODOW from "Dates")=6 THEN 'Saturday'
WHEN extract(ISODOW from "Dates")=7 THEN 'Sunday' end "Day" ,
case when C."LeaveDate" is not null then 'L' when B."Day" is not null then 'A' else '' end "Status"
from TotalDates A
left join Avail B on B."Day"::text::int = extract(ISODOW from A."Dates") and A."ProviderId"=A."ProviderId" and A."ProviderLocationId"=B."ProviderLocationId"
and A."LocationId"=B."LocationId"
left join LeaveData C on C."LeaveDate"::date =A."Dates" and A."ProviderLocationId"=C."ProviderLocationId"
DROP FUNCTION public."udf_uiReport_fetch_Appointments_Location"(integer, text, integer[], integer[], integer[], text, text, text, text, integer, date, date, integer, integer);
CREATE OR REPLACE FUNCTION public."udf_uiReport_fetch_Appointments_Location"(
locationid integer DEFAULT NULL::integer,
"appointmentNo" text DEFAULT NULL::text,
"departmentId" integer[] DEFAULT NULL::integer[],
"providerId" integer[] DEFAULT NULL::integer[],
"patientId" integer[] DEFAULT NULL::integer[],
"uMRNo" text DEFAULT NULL::text,
"referredBy" text DEFAULT NULL::text,
"referredByName" text DEFAULT NULL::text,
mobile text DEFAULT NULL::text,
"payTypeId" integer DEFAULT NULL::integer,
"fromDate" date DEFAULT NULL::date,
"toDate" date DEFAULT NULL::date,
"pageIndex" integer DEFAULT 0,
"pageSize" integer DEFAULT 10)
RETURNS TABLE("DepartmentId" text, "DepartmentName" character varying, "ProviderId" text, "ProviderName" character varying, "FollowUpForAppointmentId" integer, "AppointmentDate" date, "AppointmentNo" character varying, "PatientId" integer, "ReferredBy" character varying, "ReferredByName" character varying, "PatientName" character varying, "PatientAge" smallint, "UMRNo" character varying, "Mobile" character varying, "PatientGender" character, "AppointmentTime" time without time zone, "VisitType" character, "PaymentType" character varying, "TotalAppointments" bigint,
ReceiptDate" timestamp without time zone, "ReceiptId" integer, "StreetAdress" character varying, "City" character varying, "State" character varying, "FatherOrHusband" text)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
with cts as (select coalesce(A."DepartmentId"::text,'GrandTotal') "DepartmentId",
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
--and case when "filter" is null then 1=1 else (TRIM(UPPER(pat."FullName")) ILIKE '%' OR pat."UMRNo" ilike'%' OR pat."Mobile" ILIKE '%')|| "filter"||'%' end
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxapt as (
select a.* from "Appointment" a
join cts on a."AppointmentId"=cts."AppointmentId"
where "Max"=1)
,ctsadmission as (
select distinct max(a."AdmissionId") over(partition by a."PatientId" )"MaxAdmissionId" ,a."PatientId",
a."AppointmentId" from "Admission" a
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
--and case when "filter" is null then 1=1 else (TRIM(UPPER(pat."FullName")) ILIKE '%' OR pat."UMRNo" ilike'%' OR pat."Mobile" ILIKE '%') || "filter"||'%' end
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxadmision as (
select a.* from "Admission" a
join ctsadmission cts on a."AdmissionId"=cts."MaxAdmissionId"
)
SELECT DISTINCT pat."PatientId" ,
pat."Salutation" ,
pat."FirstName" ,
pat."MiddleName" ,
pat."LastName" ,
pat."FullName" ,
pat."DateOfBirth" ,
pat."Age" ,
pat."Gender" ,
pat."UMRNo" ,
pat."Email" ,
pat."Mobile" ,
pat."CountryId" ,
pat."ProfileImageUrl" ,
-- pat."ThumbnailUrl" ,
pat."Active" ,
(CASE WHEN pat."ThumbnailUrl" IS NOT NULL THEN CONCAT("urlLink", pat."Guid", '/', pat."ThumbnailUrl") ELSE NULL END) AS "ThumbnailUrl"
,(max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId",apt."AppointmentDate")::text)::timestamp as "AppointmentDate",
(case when (max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId")::text)::timestamp > now() at time zone 'UTC-5:30' then true else false end)"isActiveAppointmentExists"
,(case when max(adm."AdmissionId")over(partition by adm."PatientId") is null then false
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is null then true
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is not null then false end
left join maxapt apt on apt."PatientId" = pat."PatientId" and apt."Status" <> 'C' and apt."Active"=true
left join maxadmision adm on adm."PatientId" = pat."PatientId" and adm."Active"=true
left join "Discharge" dc on dc."AdmissionId" = adm."AdmissionId"
left join "HWCPatient" HWC on HWC."HWCPatientId" = pat."HWCPatientId"
WHERE pat."Active" IS TRUE
--and case when "filter" is null then 1=1 else (TRIM(UPPER(pat."FullName")) ILIKE '%' OR pat."UMRNo" ilike'%' OR pat."Mobile" ILIKE '%')|| "filter"||'%' end
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
-- FUNCTION: public.udf_uiReport_Receipts_Admissions_Location(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
--
DROP FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone);
CREATE OR REPLACE FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(
"accountId" integer[] DEFAULT NULL::integer[],
"providerId" integer DEFAULT NULL::integer,
"locationId" integer DEFAULT NULL::integer,
"roleId" integer[] DEFAULT NULL::integer[],
"admissionNo" text DEFAULT NULL::text,
"patientId" integer DEFAULT NULL::integer,
"uMRNo" character varying DEFAULT NULL::text,
"patientMobile" character varying DEFAULT NULL::text,
"receiptId" integer DEFAULT NULL::integer,
"createdBy" integer DEFAULT NULL::integer,
"payTypeId" 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)
RETURNS TABLE("ReceiptCreatedBy" text, "RoleName" character varying, "ReceiptDate" timestamp without time zone, "ReceiptId" text, "AdmissionNo" text, "AdmissionDate" date, "AdmissionTime" text, "PatientName" text, "UMRNo" character varying, "PatientMobile" character varying, "ProviderName" character varying, "PayTypeName" character varying, "PaidAmount" numeric, "RefundAmount" numeric, "BalanceAmount" numeric)
--ALTER FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
-- FUNCTION: public.udf_uiReport_Receipts_Appointments_Location(integer[], integer, integer, integer[], character varying, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
-- DROP FUNCTION public."udf_uiReport_Receipts_Appointments_Location"(integer[], integer, integer, integer[], character varying, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone);
CREATE OR REPLACE FUNCTION public."udf_uiReport_Receipts_Appointments_Location"(
"accountId" integer[] DEFAULT NULL::integer[],
"providerId" integer DEFAULT NULL::integer,
"locationId" integer DEFAULT NULL::integer,
"roleId" integer[] DEFAULT NULL::integer[],
"appointmentNo" character varying DEFAULT NULL::text,
"patientId" integer DEFAULT NULL::integer,
"uMRNo" character varying DEFAULT NULL::text,
"patientMobile" character varying DEFAULT NULL::text,
"receiptId" integer DEFAULT NULL::integer,
"createdBy" integer DEFAULT NULL::integer,
"payTypeId" 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)
RETURNS TABLE("ReceiptCreatedBy" text, "RoleName" character varying, "ReceiptDate" timestamp without time zone, "ReceiptId" text, "AppointmentNo" character varying, "FollowUpForAppointmentId" integer, "AppointmentDate" date, "AppointmentTime" text, "PatientName" text, "UMRNo" character varying, "PatientMobile" character varying, "ProviderName" character varying, "PaymentType" character varying, "PayTypeName" character varying, "PaidAmount" numeric, "RefundAmount" numeric, "BalanceAmount" numeric, "AppointmentId" integer, "IsAppointmentReceipt" boolean)
ALTER FUNCTION public."udf_uiReport_Receipts_Appointments_Location"(integer[], integer, integer, integer[], character varying, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
ALter table "PharmacyProduct" add column if not exists "MedicationId" text;
--------------------------
Create table if not exists "ModuleDepartmentDetail"("ModuleDepartmentDetailId" serial primary key,"ModuleName" text,
"DepartmentId" int references "Department"("DepartmentId"),
"Active" boolean default true,
"CreatedBy" int references "Account"("AccountId"),"CreatedDate" timestamp without time zone,
"ModifiedBy" int references "Account"("AccountId"),"ModifiedDate" timestamp without time zone
);
Create table if not exists "ModuleDepartmentUser"("ModuleDepartmentUserId" serial primary key,"ModuleDepartmentDetailId" int references "ModuleDepartmentDetail"("ModuleDepartmentDetailId"),
"AccountId" int references "Account"("AccountId"));
--------------------
Alter table "ModuleDepartmentDetail" add column "LocationId" int references "Location"("LocationId");
JOIN "Location" PL ON A."LocationId" = PL."LocationId" AND PL."Active" = TRUE
JOIN "Practice" P ON P."PracticeId" = PL."PracticeId" AND P."Active" = TRUE
where A."ProviderId"=providerid AND A."Active" IS TRUE
)
,Availability as (
select A."ProviderId",A."ProviderLocationId",A."LocationId",json_array_elements(case when (A."Availability" is null or A."Availability"='') then '[]' else (A."Availability"::json)end) "Availability"
from "ProviderLocation" A
JOIN "Location" PL ON A."LocationId" = PL."LocationId" AND PL."Active" = TRUE
JOIN "Practice" P ON P."PracticeId" = PL."PracticeId" AND P."Active" = TRUE
where A."ProviderId"=providerid AND A."Active" IS TRUE
--and case when providerLocationId is null then 1=1 else A."ProviderLocationId"=providerLocationId end
A."LocationId",extract(ISODOW from "Dates")"DateNo", CASE
WHEN extract(ISODOW from "Dates") =1 THEN 'Monday'
WHEN extract(ISODOW from "Dates") =2 THEN 'Tuesday'
WHEN extract(ISODOW from "Dates")=3 THEN 'Wednesday'
WHEN extract(ISODOW from "Dates")=4 THEN 'Thursday'
WHEN extract(ISODOW from "Dates")=5 THEN 'Friday'
WHEN extract(ISODOW from "Dates")=6 THEN 'Saturday'
WHEN extract(ISODOW from "Dates")=7 THEN 'Sunday' end "Day" ,
case when C."LeaveDate" is not null then 'L' when B."Day" is not null then 'A' else '' end "Status"
from TotalDates A
left join Avail B on B."Day"::text::int = extract(ISODOW from A."Dates") and A."ProviderId"=A."ProviderId" and A."ProviderLocationId"=B."ProviderLocationId"
and A."LocationId"=B."LocationId"
left join LeaveData C on C."LeaveDate"::date =A."Dates" and A."ProviderLocationId"=C."ProviderLocationId"
alter table "PharmacyProduct" add column "Suppliers" text;
-------------
alter table "PharmacyProductDetail" add column "PharmacyWareHouseId" int references "PharmacyWareHouse"("PharmacyWareHouseId");
alter table "PharmacyProductRack" add column "PharmacyWareHouseId" int references "PharmacyWareHouse"("PharmacyWareHouseId");
--------------------------
----ImpQuery
with cts as(
select distinct count(ps."PharmacyProductId" ) over(partition by ps."PharmacyWareHouseId") as "Count",ps."PharmacyWareHouseId",ps."PharmacyProductId" from "PharmacyStock" ps
)
insert into "PharmacyProductDetail" ( "ROQ", "ROL", "PharmacyProductId", "CreatedBy", "CreatedDate", "PharmacyWareHouseId")
select coalesce(pp."MinQuantity",100) as "ROQ", coalesce(pp."RolQuantity",100) as "ROL",
c."PharmacyProductId",pp."CreatedBy",now() as "CreatedDate",c."PharmacyWareHouseId"
from "PharmacyProduct" pp
join cts c on c."PharmacyProductId" = pp."PharmacyProductId"
----Done---
create table "ProductRaiseStatus"(
"ProductRaiseStatusId" serial primary key,
"Status" varchar(250)
);
insert into "ProductRaiseStatus" ("Status") values
('Raised'),
('Rejected'),
('Completed');
--------
create table "ProductGroupForTender"(
"ProductGroupForTenderId" serial primary key,
"PharmacyProductId" int REFERENCES "PharmacyProduct"("PharmacyProductId"),
"ProductRaiseStatusId" int references "ProductRaiseStatus"("ProductRaiseStatusId"),
"PharmacyDepartmentId" int REFERENCES "PharmacyDepartment"("PharmacyDepartmentId"),
"RetailPharmacyId" int REFERENCES "RetailPharmacy"("RetailPharmacyId"),
"PharmacyWareHouseId" int REFERENCES "PharmacyWareHouse"("PharmacyWareHouseId"),
"Active" bool default true,
"CreatedBy" int REFERENCES "Account"("AccountId"),
CREATE OR REPLACE FUNCTION public."udf_PharmacyPurchaseReport1"("billNumber" text DEFAULT NULL::text, "billType" text DEFAULT NULL::text, "createdBy" integer DEFAULT NULL::integer, "supplierId" integer DEFAULT NULL::integer, "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, "NetAmount" numeric, "WareHouseName" text, "LocationName" character varying)
-- FUNCTION: public.udf_PharmacyPurchaseReport(text, text, integer, text, text, date, timestamp without time zone, timestamp without time zone, boolean, integer, text)
-- DROP FUNCTION IF EXISTS public."udf_PharmacyPurchaseReport1"(text, text, integer, integer, text, date, timestamp without time zone, timestamp without time zone, boolean, integer, text);
CREATE OR REPLACE FUNCTION public."udf_PharmacyPurchaseReport1"(
"billNumber" text DEFAULT NULL::text,
"billType" text DEFAULT NULL::text,
"createdBy" integer DEFAULT NULL::integer,
"supplierId" integer DEFAULT NULL::integer,
"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,
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, "NetAmount" numeric, "WareHouseName" text, "LocationName" character varying)
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_PharmacyPurchaseReport1"(text, text, integer, integer, text, date, timestamp without time zone, timestamp without time zone, boolean, integer, text)
INSERT INTO "Settings" ("Name", "Active", "Type", "Value", "ImageUrl", "Description", "Numeral") VALUES('Direct Stock Credit To Retail', true, 'Pharmacy', NULL, NULL, 'This setting will be used to enable direct stock credit to retail store whenever purchase bill will be added.', NULL);
INSERT INTO "Settings" ("Name", "Active", "Type", "Value", "ImageUrl", "Description", "Numeral") VALUES('Sales Bill Discount Display', true, 'Pharmacy', NULL, NULL, 'This will be used to show/hide discount option in Pharmacy', NULL);
-- FUNCTION: public.udf_Employee_Revenue_Location(integer, integer, integer, timestamp without time zone, timestamp without time zone)
-- DROP FUNCTION IF EXISTS public."udf_Employee_Revenue_Location"(integer, integer, integer, timestamp without time zone, timestamp without time zone);
CREATE OR REPLACE FUNCTION public."udf_Employee_Revenue_Location"(
accountid integer DEFAULT NULL::integer,
locationid integer DEFAULT NULL::integer,
roleid 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)
-- FUNCTION: public.udf_uiReport_fetch_Admissions_Locations(text, integer, integer[], integer[], date, integer, character varying, text, text, character varying, integer, date, integer, integer)
-- DROP FUNCTION IF EXISTS public."udf_uiReport_fetch_Admissions_Locations"(text, integer, integer[], integer[], date, integer, character varying, text, text, character varying, integer, date, integer, integer);
CREATE OR REPLACE FUNCTION public."udf_uiReport_fetch_Admissions_Locations"(
"admissionNo" text DEFAULT NULL::text,
locationid integer DEFAULT NULL::integer,
"departmentId" integer[] DEFAULT NULL::integer[],
"providerId" integer[] DEFAULT NULL::integer[],
"fromDate" date DEFAULT NULL::date,
"patientId" integer DEFAULT NULL::integer,
"uMRNo" character varying DEFAULT NULL::text,
"referredBy" text DEFAULT NULL::text,
"referredByName" text DEFAULT NULL::text,
"patientMobile" character varying DEFAULT NULL::text,
RETURNS TABLE("DepartmentName" text, "ProviderName" text, "AdmissionId" text, "AdmissionNo" text, "AdmissionDate" timestamp without time zone, "AdmissionTime" time without time zone, "PatientName" text, "ReferredBy" character varying, "ReferredByName" character varying, "UMRNo" character varying, "PatientMobile" character varying, "PatientAge" smallint, "PatientGender" character, "ProviderAge" smallint, "ProviderGender" character, "WardName" character varying, "RoomName" character varying, "BedNumber" character varying, "AttendantName" character varying, "AttendantRelationWithPatient" character varying, "AttendantContactNo" character varying, "IsDischarged" boolean, "DischargeDate" date, "DischargeTime" time without time zone, "DischargeStatus" character varying, "IsMaternity" boolean, "EncounterId" integer, "SurgeryName" character varying, "ProviderThumbnailUrl" text, "PatientThumbnailUrl" text, "TotalAdmission" bigint)
Pa."FullName" "PatientName",Pa."ReferredBy",Pa."ReferredByName",Pa."UMRNo",pa."Mobile",Pa."Age" "PatientAge",Pa."Gender" as "PatientGender",Pr."Age" "ProviderAge",Pr."Gender" as "ProviderGender",
(CASE WHEN pr."ThumbnailUrl" IS NOT NULL THEN CONCAT('{this.amazonS3Configuration.BucketURL}', pr."Guid", '/', pr."ThumbnailUrl") ELSE NULL END) AS "ProviderThumbnailUrl",
(CASE WHEN pa."ThumbnailUrl" IS NOT NULL THEN CONCAT('{this.amazonS3Configuration.BucketURL}', pa."Guid", '/', pa."ThumbnailUrl") ELSE NULL END) AS "PatientThumbnailUrl"
from "Admission" A
join "Patient" Pa on A."PatientId"=Pa."PatientId"
join "Provider" Pr on A."ProviderId"=Pr."ProviderId"
join "Department" D on D."DepartmentId"=A."DepartmentId"
left join "Bed" B on B."BedId"=A."BedId"
left join "Room" R on B."RoomId"=R."RoomId"
left join "Ward" W on w."WardId"=R."WardId"
---left join "PatientFamily" pf on pf."PatientFamilyId"=A."PatientFamilyId"
left join "SurgeryType" st on st."SurgeryTypeId"=A."SurgeryTypeId"
left join "Discharge" ds on ds."AdmissionId"=A."AdmissionId"
left join "DischargeStatus" dss on dss."DischargeStatusId"=ds."DischargeStatusId"
where
CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE A."LocationId"=locationId END AND
case when "departmentId" is null then 1=1 else A."DepartmentId" = any("departmentId") end and
case when "providerId" is null then 1=1 else A."ProviderId" = any("providerId") end and
case when "admissionNo" is null then 1=1 when "admissionNo" ='' then 1=1 else A."AdmissionNo" ilike'%'||"admissionNo" ||'%' end
and case when "patientId" is null then 1=1 else Pa."PatientId"="patientId" end and
case when "uMRNo" is null then 1=1 else Pa."UMRNo" ilike'%'||"uMRNo"||'%' end and
case when "referredBy" is null then 1=1 when "referredBy" ='' then 1=1 else Pa."ReferredBy" ilike'%'||"referredBy" ||'%' end and
case when "referredByName" is null then 1=1 when "referredByName" ='' then 1=1 else Pa."ReferredByName" ilike'%'||"referredByName" ||'%' end and
case when "patientMobile" is null then 1=1 else Pa."Mobile" ilike'%'||"patientMobile"||'%' end and
case when "dischargeStatusId" is null then 1=1 else dss."DischargeStatusId"="dischargeStatusId" end and
case when "fromDate" is null then 1=1 else "fromDate" <=A."AdmissionDate"::date and A."AdmissionDate"::date <="toDate" end
) A
GROUP BY GROUPING SETS((A."AdmissionId" , A."AdmissionNo" , A."AdmissionDate" , A."AdmissionTime",
ALTER FUNCTION public."udf_uiReport_fetch_Admissions_Locations"(text, integer, integer[], integer[], date, integer, character varying, text, text, character varying, integer, date, integer, integer)
-- DROP FUNCTION IF EXISTS public."udf_uiReport_fetch_Appointments_Location"(integer, text, integer[], integer[], integer[], text, text, text, text, text, date, date, integer, integer);
CREATE OR REPLACE FUNCTION public."udf_uiReport_fetch_Appointments_Location"(
locationid integer DEFAULT NULL::integer,
"appointmentNo" text DEFAULT NULL::text,
"departmentId" integer[] DEFAULT NULL::integer[],
"providerId" integer[] DEFAULT NULL::integer[],
"patientId" integer[] DEFAULT NULL::integer[],
"uMRNo" text DEFAULT NULL::text,
"referredBy" text DEFAULT NULL::text,
"referredByName" text DEFAULT NULL::text,
mobile text DEFAULT NULL::text,
"paymentType" text DEFAULT NULL::text,
"fromDate" date DEFAULT NULL::date,
"toDate" date DEFAULT NULL::date,
"pageIndex" integer DEFAULT 0,
"pageSize" integer DEFAULT 10)
RETURNS TABLE("DepartmentId" text, "DepartmentName" character varying, "ProviderId" text, "ProviderName" character varying, "FollowUpForAppointmentId" integer, "AppointmentDate" date, "AppointmentNo" character varying, "PatientId" integer, "ReferredBy" character varying, "ReferredByName" character varying, "PatientName" character varying, "PatientAge" smallint, "UMRNo" character varying, "Mobile" character varying, "PatientGender" character, "AppointmentTime" time without time zone, "VisitType" character, "PaymentType" character varying, "TotalAppointments" bigint, "TotalAmount" bigint, "ReceiptCreatedByName" text, "ReceiptDate" timestamp without time zone, "ReceiptId" integer, "StreetAdress" character varying, "City" character varying, "State" character varying, "FatherOrHusband" text)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
with cts as (select coalesce(A."DepartmentId"::text,'GrandTotal') "DepartmentId",
RETURNS TABLE("DepartmentId" text, "DepartmentName" character varying, "ProviderId" text, "ProviderName" character varying, "FollowUpForAppointmentId" integer, "AppointmentDate" date, "AppointmentNo" character varying, "PatientId" integer, "Name" character varying, "ReferredByName" character varying, "PatientName" character varying, "PatientAge" smallint, "UMRNo" character varying, "Mobile" character varying, "PatientGender" character, "AppointmentTime" time without time zone, "VisitType" character, "PaymentType" character varying, "PaymentNumber" character varying, "TotalAppointments" bigint, "TotalAmount" bigint, "TotalAmountStr" text, "ReceiptCreatedByName" text,
"ReceiptDate" timestamp without time zone, "ReceiptId" integer, "StreetAdress" character varying, "City" character varying, "State" character varying, "FatherOrHusband" text)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
with cts as (select coalesce(A."DepartmentId"::text,'GrandTotal') "DepartmentId",
sum(pa."PharmaSaleCash") over(partition by a."ProviderId") "PharmacySaleCash",
sum(pa."PharmaSaleCard") over(partition by a."ProviderId") "PharmacySaleCard",
sum(pa."PharmaSaleUPI") over(partition by a."ProviderId") "PharmacySaleUPI",
sum(pa."PharmaSaleOnline") over(partition by a."ProviderId") "PharmacySaleOnline",
sum(pa."PharmaSaleCheque") over(partition by a."ProviderId") "PharmacySaleCheque",
sum(pa."PharmaSalePaytm") over(partition by a."ProviderId") "PharmacySalePaytm",
sum(pa."PharmaAmount") over(partition by a."ProviderId") "PharmacyAmount",
coalesce(ap."AppointmentCashTotal",0)+ coalesce(ad."AdmissionCashTotal",0)+coalesce(lb."LabCash",0)+coalesce(sum(pa."PharmaSaleCash") over(partition by a."ProviderId") ,0) "TotalCash",
coalesce(ap."AppointmentCardTotal",0)+ coalesce(ad."AdmissionCardTotal",0)+coalesce(lb."LabCard",0)+coalesce(sum(pa."PharmaSaleCard") over(partition by a."ProviderId"),0) "TotalCard",
coalesce(ap."AppointmentUPITotal",0)+ coalesce(ad."AdmissionUPITotal",0)+coalesce(lb."LabUPI",0)+coalesce(sum(pa."PharmaSaleUPI") over(partition by a."ProviderId") ,0) "TotalUPI",
coalesce(ap."AppointmentOnlineTotal",0)+ coalesce(ad."AdmissionOnlineTotal",0)+coalesce(lb."LabOnline",0)+coalesce(sum(pa."PharmaSaleOnline") over(partition by a."ProviderId"),0) "TotalOnline",
coalesce(ap."AppointmentChequeTotal",0)+ coalesce(ad."AdmissionChequeTotal",0)+coalesce(lb."LabCheque",0)+coalesce(sum(pa."PharmaSaleCheque") over(partition by a."ProviderId") ,0) "TotalCheque",
coalesce(ap."AppointmentPaytmTotal",0)+ coalesce(ad."AdmissionPaytmTotal",0)+coalesce(lb."LabPaytm",0)+coalesce(sum(pa."PharmaSalePaytm") over(partition by a."ProviderId"),0) "TotalPaytm",
coalesce(ap."AppointmentAmount",0)+coalesce(ad."AdmissionAmount",0)+coalesce(lb."LabAmount",0)+coalesce(sum(pa."PharmaAmount") over(partition by a."ProviderId"),0) "Total"
from accountdata a
left join appointmentamount ap on ap."ProviderId"=a."ProviderId"
left join admissionamount ad on ad."ProviderId"=a."ProviderId"
left join LabAmount lb on lb."ProviderId"=a."ProviderId"
left join PharmaAmount pa on pa."ProviderId"=a."ProviderId"
;
end
$BODY$;
--ALTER FUNCTION public.udf_doctor_new_revenue(integer, integer, date, date)
-- FUNCTION: public.udf_pharmacypatientbills_finalreport(text, integer, integer, character varying, character varying, integer, integer, integer, date, date, text, integer, text, boolean)
-- DROP FUNCTION public.udf_pharmacypatientbills_finalreport(text, integer, integer, character varying, character varying, integer, integer, integer, date, date, text, integer, text, boolean);
CREATE OR REPLACE FUNCTION public.udf_pharmacypatientbills_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,
"payTypeId" integer DEFAULT NULL::integer,
"providerId" integer DEFAULT NULL::integer,
"createdBy" integer DEFAULT NULL::integer,
"fromDate" date DEFAULT NULL::date,
"toDate" date DEFAULT NULL::date,
"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" character varying, "PaymentNumber" character varying, "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)
RETURNS TABLE("DepartmentId" text, "DepartmentName" character varying, "ProviderId" text, "ProviderName" character varying, "AppointmentTypeName" character varying, "AppointmentDate" date, "AppointmentNo" character varying, "PatientId" integer, "Name" character varying, "ReferredByName" character varying, "FirstName" character varying, "MiddleName" character varying, "LastName" character varying, "PatientName" character varying, "PatientAge" smallint, "UMRNo" character varying, "Mobile" character varying, "PatientGender" character, "AppointmentTime" time without time zone, "VisitType" character, "PaymentType" character varying, "PaymentNumber" character varying, "TotalAppointments" bigint, "TotalAmount" bigint, "TotalAmountStr" text, "ReceiptCreatedByName" text, "ReceiptDate" timestamp without time zone, "ReceiptId" integer, "StreetAdress" character varying, "City" character varying, "State" character varying, "FatherOrHusband" text, "PaymentStatus" boolean, "EncounterType" encountertype_elements, "Status" text)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
with cts as (select coalesce(A."DepartmentId"::text,'GrandTotal') "DepartmentId",
-- FUNCTION: public.udf_uiReport_Receipts_Admissions_Location(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
--
DROP FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone);
CREATE OR REPLACE FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(
"accountId" integer[] DEFAULT NULL::integer[],
"providerId" integer DEFAULT NULL::integer,
"locationId" integer DEFAULT NULL::integer,
"roleId" integer[] DEFAULT NULL::integer[],
"admissionNo" text DEFAULT NULL::text,
"patientId" integer DEFAULT NULL::integer,
"uMRNo" character varying DEFAULT NULL::text,
"patientMobile" character varying DEFAULT NULL::text,
"receiptId" integer DEFAULT NULL::integer,
"createdBy" integer DEFAULT NULL::integer,
"payTypeId" 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)
RETURNS TABLE("ReceiptCreatedBy" text, "RoleName" character varying,
"ReceiptDate" timestamp without time zone, "ReceiptId" text, "AdmissionNo" text,
END-- FUNCTION: public.udf_uiReport_Receipts_Admissions_Location(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
--
DROP FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone);
CREATE OR REPLACE FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(
"accountId" integer[] DEFAULT NULL::integer[],
"providerId" integer DEFAULT NULL::integer,
"locationId" integer DEFAULT NULL::integer,
"roleId" integer[] DEFAULT NULL::integer[],
"admissionNo" text DEFAULT NULL::text,
"patientId" integer DEFAULT NULL::integer,
"uMRNo" character varying DEFAULT NULL::text,
"patientMobile" character varying DEFAULT NULL::text,
"receiptId" integer DEFAULT NULL::integer,
"createdBy" integer DEFAULT NULL::integer,
"payTypeId" 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)
RETURNS TABLE("ReceiptCreatedBy" text, "RoleName" character varying,
"ReceiptDate" timestamp without time zone, "ReceiptId" text, "AdmissionNo" text,
--ALTER FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
-- OWNER TO postgres;
$BODY$;
--ALTER FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
-- FUNCTION: public.udf_uiReport_Receipts_Appointments_Location(integer[], integer, integer, integer[], character varying, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
--
DROP FUNCTION public."udf_uiReport_Receipts_Appointments_Location"(integer[], integer, integer, integer[], character varying, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone);
CREATE OR REPLACE FUNCTION public."udf_uiReport_Receipts_Appointments_Location"(
"accountId" integer[] DEFAULT NULL::integer[],
"providerId" integer DEFAULT NULL::integer,
"locationId" integer DEFAULT NULL::integer,
"roleId" integer[] DEFAULT NULL::integer[],
"appointmentNo" character varying DEFAULT NULL::text,
"patientId" integer DEFAULT NULL::integer,
"uMRNo" character varying DEFAULT NULL::text,
"patientMobile" character varying DEFAULT NULL::text,
"receiptId" integer DEFAULT NULL::integer,
"createdBy" integer DEFAULT NULL::integer,
"payTypeId" 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)
RETURNS TABLE("ReceiptCreatedBy" text, "RoleName" character varying, "ReceiptDate" timestamp without time zone, "ReceiptId" text, "AppointmentNo" character varying, "FollowUpForAppointmentId" integer, "AppointmentDate" date, "AppointmentTime" text, "PatientName" text, "UMRNo" character varying, "PatientMobile" character varying, "ProviderName" character varying, "PaymentType" character varying, "PayTypeName" character varying, "PaymentDetails" character varying, "PaidAmount" numeric, "RefundAmount" numeric, "BalanceAmount" numeric, "AppointmentId" integer, "IsAppointmentReceipt" boolean)
ALTER FUNCTION public."udf_uiReport_Receipts_Appointments_Location"(integer[], integer, integer, integer[], character varying, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
-- FUNCTION: public.udf_uiReport_Receipts_Appointments_Location(integer[], integer, integer, integer[], character varying, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
--
DROP FUNCTION public."udf_uiReport_Receipts_Appointments_Location"(integer[], integer, integer, integer[], character varying, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone);
CREATE OR REPLACE FUNCTION public."udf_uiReport_Receipts_Appointments_Location"(
"accountId" integer[] DEFAULT NULL::integer[],
"providerId" integer DEFAULT NULL::integer,
"locationId" integer DEFAULT NULL::integer,
"roleId" integer[] DEFAULT NULL::integer[],
"appointmentNo" character varying DEFAULT NULL::text,
"patientId" integer DEFAULT NULL::integer,
"uMRNo" character varying DEFAULT NULL::text,
"patientMobile" character varying DEFAULT NULL::text,
"receiptId" integer DEFAULT NULL::integer,
"createdBy" integer DEFAULT NULL::integer,
"payTypeId" 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)
RETURNS TABLE("ReceiptCreatedBy" text, "RoleName" character varying,
"ReceiptDate" timestamp without time zone, "ReceiptId" text,
"AppointmentNo" character varying, "FollowUpForAppointmentId" integer,
--ALTER FUNCTION public."udf_uiReport_Receipts_Appointments_Location"(integer[], integer, integer, integer[], character varying, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
Pa."FullName" "PatientName",Pa."UMRNo",Pa."Age" "PatientAge",Pa."Gender" as "PatientGender",pa."Mobile" "patientMobile",Pr."Age" "ProviderAge",Pr."Gender" as "ProviderGender",
sum(coalesce(Re."Cost",0))-coalesce((select sum(Rfd."Cost") from "Receipt" Rfd where Rfd."AdmissionId"=A."AdmissionId" and "ReceiptTypeId"=2 and Rfd."Active"=true and Rfd."IsRefunded" =true ),0) "PaidAmount"
,
FB."FinalAmount" ,
case when FB."FinalAmount"-(sum(coalesce(Re."Cost",0))-coalesce((select sum(Rfd."Cost") from "Receipt" Rfd where Rfd."AdmissionId"=A."AdmissionId" and "ReceiptTypeId"=2 and Rfd."Active"=true and Rfd."IsRefunded" =true ),0))=0 then 'Payment Cleared'
when FB."FinalAmount"-(sum(coalesce(Re."Cost",0))-coalesce((select sum(Rfd."Cost") from "Receipt" Rfd where Rfd."AdmissionId"=A."AdmissionId" and "ReceiptTypeId"=2 and Rfd."Active"=true and Rfd."IsRefunded" =true ),0))>0 then 'Payment Due'
when FB."FinalAmount"-(sum(coalesce(Re."Cost",0))-coalesce((select sum(Rfd."Cost") from "Receipt" Rfd where Rfd."AdmissionId"=A."AdmissionId" and "ReceiptTypeId"=2 and Rfd."Active"=true and Rfd."IsRefunded" =true ),0))<0 then 'Pending Refund' end "PaymentStatus",
case when FB."FinalBillId" is null then false else true end "IsFinalBill",(select T."TotalItems" from TotalItems T) "TotalItems",
A."Active",
A."ExpectedDischargeDate",
A."DischargedBy" ,
case when RL."RoleId" = '3' then DP."DepartmentName"::TEXT else RL."RoleName"::TEXT end as "DischargedByRole",
case when RL."RoleId" = '3' then PRO."FullName" else AC."FullName" end as "DischargedByName"
from "Admission" A
join "Patient" Pa on A."PatientId"=Pa."PatientId"
join "Provider" Pr on A."ProviderId"=Pr."ProviderId"
join "Department" D on D."DepartmentId"=A."DepartmentId"
left join "Bed" B on B."BedId"=A."BedId"
left join "Room" R on B."RoomId"=R."RoomId"
left join "Ward" W on w."WardId"=R."WardId"
---left join "PatientFamily" pf on pf."PatientFamilyId"=A."PatientFamilyId"
left join "SurgeryType" st on st."SurgeryTypeId"=A."SurgeryTypeId"
left join "Discharge" ds on ds."AdmissionId"=A."AdmissionId" and ds."Active"=true
left join "DischargeStatus" dss on ds."DischargeStatusId"=dss."DischargeStatusId"
left join "Receipt" Re on A."AdmissionId" = Re."AdmissionId" and Re."ReceiptTypeId"=1 and Re."Active"=true
---left join "Receipt" Rf on Rf."ReceiptTypeId"=2 and A."AdmissionId"= Rf ."AdmissionId" and Rf."Active"=true and Rf."IsRefunded" =true
left join "FinalBill" FB on A."AdmissionId"= FB."AdmissionId" and FB."Active"=true
left join "Account" AC on AC."AccountId"=A."DischargedBy"
left join "Role" RL on RL."RoleId"= AC."RoleId"
left join "Provider" PRO on PRO."ProviderId"=AC."ReferenceId"
left join "Department" DP on DP."DepartmentId"=PRO."DepartmentId"
where
case when "admissionNo" is null then 1=1
when "admissionNo"='' then 1=1
else A."AdmissionNo" ilike '%'||"admissionNo"||'%' end and
CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE A."LocationId"=locationId END AND
case when "patientMobileNo" is null then 1=1
when "patientMobileNo"='' then 1=1
else pa."Mobile" ilike '%'||"patientMobileNo"||'%' end and
case when "patientId" is null then 1=1
else Pa."PatientId"= "patientId" end and
case when "active" is null then 1=1
else A."Active"= "active" end and
case when "providerId" is null then 1=1
else Pr."ProviderId" = "providerId" end and
case when "isDischarged" is null then 1=1
when "isDischarged" =true then ds."AdmissionId"=A."AdmissionId"
when "isDischarged" =false then ds."AdmissionId" is null
end AND
case when "fromDate" is null then 1=1 else "fromDate" <=A."AdmissionDate"::date and A."AdmissionDate"::date <="toDate" end
AND
case when "dischargeDate" is null then 1=1 else "dischargeDate" =ds."DischargeDate"::date end
and
case when "uMRNo" is null then 1=1
when "uMRNo"='' then 1=1
else Pa."UMRNo" ilike '%'||"uMRNo"||'%' end
group by A."AdmissionId",A."AdmissionNo",A."IsConvertedFromOPtoIp",A."AdmissionDate",A."AdmissionTime",
select string_agg(D."ProviderAvailabilityId"::text,',') "ProviderAvailabilityId" from "ProviderAvailability" D where D."ProviderId"= providerid and D."LocationId" = locationid
))
"ProviderAvailabilityId" from "ProviderLeave" A where A."ProviderId"=providerid and A."LocationId" = locationid
WHEN extract(ISODOW from "Dates") =1 THEN 'Monday'
WHEN extract(ISODOW from "Dates") =2 THEN 'Tuesday'
WHEN extract(ISODOW from "Dates")=3 THEN 'Wednesday'
WHEN extract(ISODOW from "Dates")=4 THEN 'Thursday'
WHEN extract(ISODOW from "Dates")=5 THEN 'Friday'
WHEN extract(ISODOW from "Dates")=6 THEN 'Saturday'
WHEN extract(ISODOW from "Dates")=7 THEN 'Sunday' end "Day" ,
case when C."LeaveDate" is not null then 'L' when "AvailableDate"::date is not null is not null then 'A' else '' end "Status"
from TotalDates A
left join Avail B on "AvailableDate"::date= A."Dates" ::date and A."ProviderId"=A."ProviderId" and A."ProviderAvailabilityId"=B."ProviderAvailabilityId"
--and A."LocationId"=B."LocationId"
left join LeaveData C on C."LeaveDate"::date =A."Dates" and A."ProviderAvailabilityId"=C."ProviderAvailabilityId"
where
A."Dates"::Date between startdate::Date and enddate::Date
-- DROP FUNCTION IF EXISTS public."UDF_Provider_Charges_From_Charge_Module"(integer, integer, integer, text, integer, integer);
CREATE OR REPLACE FUNCTION public."UDF_Provider_Charges_From_Charge_Module"(
providerid integer,
specializationid integer,
locationid integer,
appointmentdate text,
chargetypesid integer,
consultationtypeid integer)
RETURNS TABLE("DoctorSpecializationChargeModuleDetailsId" bigint, "ChargeTypesId" integer, "ChargeName" character varying, "Charge" numeric)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
begin
IF EXISTS (
Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSD."ProviderId" = providerid and DSD."SpecializationId" = specializationid and DSD."LocationId" = locationid
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid
--END IF; 1
) THEN return query
Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSD."ProviderId" = providerid and DSD."SpecializationId" = specializationid and DSD."LocationId" = locationid
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid;
ELSEIF EXISTS (
Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSD."SpecializationId" = specializationid and DSD."LocationId" = locationid and DSD."ProviderId" is null
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid
) THEN return query Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSD."SpecializationId" = specializationid and DSD."LocationId" = locationid and DSD."ProviderId" is null
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid;
--END IF; 2
ELSEIF EXISTS (Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSD."LocationId" = locationid and DSD."ProviderId" = providerid and DSD."SpecializationId" is null
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid
) THEN return query Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSD."LocationId" = locationid and DSD."ProviderId" = providerid and DSD."SpecializationId" is null
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid;
--END IF; 3
ELSEIF EXISTS (Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSD."LocationId" = locationid and DSD."ProviderId" is null and DSD."SpecializationId" is null
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid
) THEN return query Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSD."LocationId" = locationid and DSD."ProviderId" is null and DSD."SpecializationId" is null
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid;
END IF; -- 1
end
$BODY$;
ALTER FUNCTION public."UDF_Provider_Charges_From_Charge_Module"(integer, integer, integer, text, integer, integer)
"appointmentDate" character varying DEFAULT NULL::text)
RETURNS TABLE("ProviderAvailabilityId" integer, "FullName" character varying, "DepartmentName" character varying, "DepartmentId" integer, "ProviderId" integer, "SpecializationId" integer, "SpecializationName" character varying, "LocationId" integer, "ConsultationTypeId" integer)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
SELECT DISTINCT on (pr2."ProviderId", s."SpecializationId") prl."ProviderAvailabilityId" ,pr2."FullName"
, d."DepartmentName" , d."DepartmentId"
,pr2."ProviderId"
, s. "SpecializationId", s."SpecializationName"
, CMT."LocationId", prl."ConsultationTypeId"
FROM "ProviderAvailability" prl
Join "DoctorSpecializationMap" LSM on LSM."ProviderId" = prl."ProviderId" and LSM."SpecializationId" = prl."SpecializationId" and LSM."ConsultationTypeId" = prl."ConsultationTypeId"
join "DoctorSpecializationChargeModuleDetails" DSCD on DSCD."ReferenceId" = LSM."DoctorSpecializationMapId"
join "DoctorSpecializationChargeModuleCategory" DSCC on DSCC."DoctorSpecializationChargeModuleCategoryId" = DSCD."DoctorSpecializationChargeModuleCategoryId"
join "ChargeModuleTemplate" CMT on CMT."ChargeModuleTemplateId" = DSCC."ChargeModuleTemplateId"
JOIN "Provider" pr2 on pr2."ProviderId" = LSM."ProviderId" and pr2."Active" is true
join "Account" pa on pa."ReferenceId" = pr2."ProviderId" and pa."Active" is true and pa."RoleId" = 3
JOin "LocationAccountMap" LAM on LAM."AccountId" = pa."AccountId"
join "Specialization" s on s."SpecializationId" = ANY (pr2."Specializations")
JOIN "Location" pral on pral."LocationId" = CMT."LocationId" AND pral."Active" IS TRUE
JOIN "Practice" pra on pra."PracticeId" = pral."PracticeId" AND pra."Active" IS TRUE
JOIN "Department" d on d."DepartmentId" = pr2."DepartmentId"
where pr2."Active" is true and CMT."LocationId" = "locationId" and s."Active" = true and CMT."StartDate"::date <= "appointmentDate"::date and CMT."EndDate"::date >= "appointmentDate"::date and "IsInUse" is true
and case when "filter" is null then 1=1 else TRIM(UPPER(pr2."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(s."SpecializationName")) ilike'%'|| "filter"||'%' end
and case when "consultationTypeId" is null then 1=1 else prl."ConsultationTypeId" = "consultationTypeId" end
--and case when "locationId" is null then 1=1 else LAM."LocationId" = "LocationId" end
--order by "FullName" asc
;
END
$BODY$;
ALTER FUNCTION public."udf_FetchDoctor_With_Availability_Specialization"(character varying, integer, integer)
-- FUNCTION: public.udf_PharmacyBills_Report(text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone, text, integer, text)
-- DROP FUNCTION IF EXISTS public."udf_PharmacyBills_Report"(text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone, text, integer, text);
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,
"payTypeId" integer DEFAULT NULL::integer,
"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" character varying, "PaymentNumber" character varying, "CreatedByName" text, "RoleName" character varying, "ProviderName" character varying, "TotalAmount" numeric, "RetailName" text, "OverallTaxes" numeric, "LocationName" character varying)
,Pa."UMRNo",PT."PayTypeName" as "PaidVia",PH."PaymentNumber",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"
left join "PayType" PT on PT."PayTypeId"=PH."PayTypeId"
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 "payTypeId" is null then 1=1 else PT."PayTypeId" = "payTypeId" 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" :: date >= "fromDate" :: date end and
case when "toDate" is null then 1=1 else PH."SaleDate" :: date<= "toDate" :: date 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",PT."PayTypeName", PH."PaymentNumber", A."FullName"
ALTER FUNCTION public."udf_PharmacyBills_Report"(text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone, text, integer, text)
OWNER TO postgres;
--select * from udf_PharmacyBills_Report(text, integer,text, character varying, integer, integer, integer, '2023-02-28':: date, '2023-03-01':: date, text, integer, text)
-- FUNCTION: public.udf_PatientMedicationReport(integer, text, text, text, integer, timestamp without time zone, timestamp without time zone, text)
-- DROP FUNCTION IF EXISTS public."udf_PatientMedicationReport"(integer, text, text, text, integer, timestamp without time zone, timestamp without time zone, text);
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,
"payTypeId" 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,
"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" text, "CategoryName" character varying, "CompanyName" text, "BatchNumber" character varying, "PaidVia" character varying, "PaymentNumber" character varying, "Quantity" integer, "NetAmount" numeric, "TotalAmount" numeric)
ALTER FUNCTION public."udf_PatientMedicationReport"(integer, text, text, text, integer, timestamp without time zone, timestamp without time zone, text)
RETURNS TABLE("DepartmentId" text, "DepartmentName" character varying, "ProviderId" text, "ProviderName" character varying, "AppointmentTypeName" character varying, "AppointmentDate" date, "AppointmentNo" character varying, "PatientId" integer, "Name" character varying, "ReferredByName" character varying, "PatientName" character varying, "PatientAge" smallint, "UMRNo" character varying, "Mobile" character varying, "PatientGender" character, "AppointmentTime" time without time zone, "VisitType" character, "PaymentType" character varying, "PaymentNumber" character varying, "TotalAppointments" bigint, "TotalAmount" bigint, "TotalAmountStr" text, "ReceiptCreatedByName" text, "ReceiptDate" timestamp without time zone, "ReceiptId" integer, "StreetAdress" character varying, "City" character varying, "State" character varying, "FatherOrHusband" text)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
with cts as (select coalesce(A."DepartmentId"::text,'GrandTotal') "DepartmentId",
-- FUNCTION: public.udf_fetchpatientsfilter_for_appointment(character varying, character varying, character varying, character varying)
DROP FUNCTION public.udf_fetchpatientsfilter_for_appointment(character varying, character varying, character varying, character varying);
CREATE OR REPLACE FUNCTION public.udf_fetchpatientsfilter_for_appointment(
filter character varying DEFAULT NULL::text,
"patientMobile" character varying DEFAULT NULL::text,
"uMRNo" character varying DEFAULT NULL::text,
"patientName" character varying DEFAULT NULL::text)
RETURNS TABLE("PatientId" integer, "Salutation" character varying, "FirstName" text, "MiddleName" text, "LastName" text, "FullName" text, "DateOfBirth" date, "Age" smallint, "Gender" character, "UMRNo" character varying, "Email" character varying, "Mobile" character varying, "CountryId" integer, "ProfileImageUrl" text, "Active" boolean, "ThumbnailUrl" text, "AppointmentDate" timestamp without time zone, "isActiveAppointmentExists" boolean, "isActiveAdmissionExists" boolean)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
BEGIN
return query
with cts as (
select row_number() over(partition by a."PatientId" order by a."AppointmentId" desc, a."AppointmentDate" desc )"Max" ,a."PatientId",a."AppointmentId" from "Appointment" a
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "uMRNo" is null then 1=1 else pat."UMRNo" ilike'%'|| "uMRNo"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxapt as (
select a.* from "Appointment" a
join cts on a."AppointmentId"=cts."AppointmentId"
where "Max"=1)
,ctsadmission as (
select distinct max(a."AdmissionId") over(partition by a."PatientId" )"MaxAdmissionId" ,a."PatientId",
a."AppointmentId" from "Admission" a
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "uMRNo" is null then 1=1 else pat."UMRNo" ilike'%'|| "uMRNo"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxadmision as (
select a.* from "Admission" a
join ctsadmission cts on a."AdmissionId"=cts."MaxAdmissionId"
)
SELECT DISTINCT pat."PatientId" ,
pat."Salutation" ,
pat."FirstName" ,
pat."MiddleName" ,
pat."LastName" ,
pat."FullName" ,
pat."DateOfBirth" ,
pat."Age" ,
pat."Gender" ,
pat."UMRNo" ,
pat."Email" ,
pat."Mobile" ,
pat."CountryId" ,
pat."ProfileImageUrl" ,
-- pat."ThumbnailUrl" ,
pat."Active" ,
(CASE WHEN pat."ThumbnailUrl" IS NOT NULL THEN CONCAT('https://hims-qa.s3.amazonaws.com/', pat."Guid", '/', pat."ThumbnailUrl") ELSE NULL END) AS "ThumbnailUrl"
,(max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId",apt."AppointmentDate")::text)::timestamp as "AppointmentDate",
(case when (max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId")::text)::timestamp > now() at time zone 'UTC-5:30' then true else false end)"isActiveAppointmentExists"
,(case when max(adm."AdmissionId")over(partition by adm."PatientId") is null then false
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is null then true
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is not null then false end
)"isActiveAdmissionExists"
FROM "Patient" pat
left join maxapt apt on apt."PatientId" = pat."PatientId" and apt."Status" <> 'C' and apt."Active"=true
left join maxadmision adm on adm."PatientId" = pat."PatientId" and adm."Active"=true
left join "Discharge" dc on dc."AdmissionId" = adm."AdmissionId"
WHERE pat."Active" IS TRUE
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "uMRNo" is null then 1=1 else pat."UMRNo" ilike'%'|| "uMRNo"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
;
END
$BODY$;
ALTER FUNCTION public.udf_fetchpatientsfilter_for_appointment(character varying, character varying, character varying, character varying)
DROP FUNCTION IF EXISTS public."udf_FetchPatients_For_QuickScheduleFollowUp"(character varying, character varying, character varying, character varying);
-- FUNCTION: public.udf_FetchPatients_For_QuickScheduleFollowUp(character varying, character varying, character varying, character varying)
-- DROP FUNCTION IF EXISTS public."udf_FetchPatients_For_QuickScheduleFollowUp"(character varying, character varying, character varying, character varying);
CREATE OR REPLACE FUNCTION public."udf_FetchPatients_For_QuickScheduleFollowUp"(
filter character varying DEFAULT NULL::text,
"patientMobile" character varying DEFAULT NULL::text,
"patientName" character varying DEFAULT NULL::text,
"urlLink" character varying DEFAULT NULL::text)
RETURNS TABLE("PatientId" integer, "Salutation" character varying, "FirstName" text, "MiddleName" text, "LastName" text, "FullName" text, "DateOfBirth" date, "Age" smallint, "Gender" character, "UMRNo" character varying, "Email" character varying, "Mobile" character varying, "CountryId" integer, "ProfileImageUrl" text, "Active" boolean, "PaymentStatus" boolean,"HowDidYouKnowId" integer,"EducationId" integer,"OccupationId" integer, "ProviderId" integer, "ProviderName" character varying, "ThumbnailUrl" text, "AppointmentDate" timestamp without time zone, "isActiveAppointmentExists" boolean, "isActiveAdmissionExists" boolean, "HWCName" character varying, "HWCPatientId" integer, "Description" text, "RowColor" text)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
with cts as (
select row_number() over(partition by a."PatientId" order by a."AppointmentId" desc,
join "Patient" pat on a."PatientId" =pat."PatientId"
join "Provider" pr on a."ProviderId" = pr."ProviderId"
where a."Active"=true
--and case when "filter" is null then 1=1 else (TRIM(UPPER(pat."FullName")) ILIKE '%' OR pat."UMRNo" ilike'%' OR pat."Mobile" ILIKE '%')|| "filter"||'%' end
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxapt as (
select a.* from "Appointment" a
join cts on a."AppointmentId"=cts."AppointmentId"
where "Max"=1)
,ctsadmission as (
select distinct max(a."AdmissionId") over(partition by a."PatientId" )"MaxAdmissionId" ,a."PatientId",
a."AppointmentId" from "Admission" a
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
--and case when "filter" is null then 1=1 else (TRIM(UPPER(pat."FullName")) ILIKE '%' OR pat."UMRNo" ilike'%' OR pat."Mobile" ILIKE '%') || "filter"||'%' end
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxadmision as (
select a.* from "Admission" a
join ctsadmission cts on a."AdmissionId"=cts."MaxAdmissionId"
)
SELECT DISTINCT pat."PatientId" ,
pat."Salutation" ,
pat."FirstName" ,
pat."MiddleName" ,
pat."LastName" ,
pat."FullName" ,
pat."DateOfBirth" ,
pat."Age" ,
pat."Gender" ,
pat."UMRNo" ,
pat."Email" ,
pat."Mobile" ,
pat."CountryId" ,
pat."ProfileImageUrl" ,
-- pat."ThumbnailUrl" ,
pat."Active" ,
pat."PaymentStatus",
pat."HowDidYouKnowId",
pat."EducationId",
pat."OccupationId",
apt."ProviderId",
pr."FullName" as "ProviderName",
(CASE WHEN pat."ThumbnailUrl" IS NOT NULL THEN CONCAT("urlLink", pat."Guid", '/', pat."ThumbnailUrl") ELSE NULL END) AS "ThumbnailUrl"
,(max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId",apt."AppointmentDate")::text)::timestamp as "AppointmentDate",
(case when (max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId")::text)::timestamp > now() at time zone 'UTC-5:30' then true else false end)"isActiveAppointmentExists"
,(case when max(adm."AdmissionId")over(partition by adm."PatientId") is null then false
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is null then true
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is not null then false end
left join maxapt apt on apt."PatientId" = pat."PatientId" and apt."Status" <> 'C' and apt."Active"=true
left join "Provider" pr on pr."ProviderId" = apt."ProviderId"
left join maxadmision adm on adm."PatientId" = pat."PatientId" and adm."Active"=true
left join "Discharge" dc on dc."AdmissionId" = adm."AdmissionId"
left join "HWCPatient" HWC on HWC."HWCPatientId" = pat."HWCPatientId"
WHERE pat."Active" IS TRUE
--and case when "filter" is null then 1=1 else (TRIM(UPPER(pat."FullName")) ILIKE '%' OR pat."UMRNo" ilike'%' OR pat."Mobile" ILIKE '%')|| "filter"||'%' end
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
;
END
$BODY$;
ALTER FUNCTION public."udf_FetchPatients_For_QuickScheduleFollowUp"(character varying, character varying, character varying, character varying)
-- FUNCTION: public.udf_FetchPatientsFilter_For_Appointment(character varying,character varying, character varying, character varying)
-- DROP FUNCTION IF EXISTS public."udf_FetchPatientsFilter_For_Appointment"(character varying,character varying, character varying, character varying);
CREATE OR REPLACE FUNCTION public."udf_FetchPatientsFilter_For_Appointment"(
"filter" character varying DEFAULT NULL::text,
"patientMobile" character varying DEFAULT NULL::text,
"uMRNo" character varying DEFAULT NULL::text,
"patientName" character varying DEFAULT NULL::text)
RETURNS TABLE("PatientId" integer, "Salutation" character varying, "FirstName" text, "MiddleName" text, "LastName" text, "FullName" text, "DateOfBirth" date, "Age" smallint, "Gender" character, "UMRNo" character varying, "Email" character varying, "Mobile" character varying, "CountryId" integer, "ProfileImageUrl" text, "Active" boolean, "ThumbnailUrl" text, "AppointmentDate" timestamp without time zone, "isActiveAppointmentExists" boolean, "isActiveAdmissionExists" boolean)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
with cts as (
select row_number() over(partition by a."PatientId" order by a."AppointmentId" desc, a."AppointmentDate" desc )"Max" ,a."PatientId",a."AppointmentId" from "Appointment" a
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "uMRNo" is null then 1=1 else pat."UMRNo" ilike'%'|| "uMRNo"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxapt as (
select a.* from "Appointment" a
join cts on a."AppointmentId"=cts."AppointmentId"
where "Max"=1)
,ctsadmission as (
select distinct max(a."AdmissionId") over(partition by a."PatientId" )"MaxAdmissionId" ,a."PatientId",
a."AppointmentId" from "Admission" a
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "uMRNo" is null then 1=1 else pat."UMRNo" ilike'%'|| "uMRNo"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxadmision as (
select a.* from "Admission" a
join ctsadmission cts on a."AdmissionId"=cts."MaxAdmissionId"
)
SELECT DISTINCT pat."PatientId" ,
pat."Salutation" ,
pat."FirstName" ,
pat."MiddleName" ,
pat."LastName" ,
pat."FullName" ,
pat."DateOfBirth" ,
pat."Age" ,
pat."Gender" ,
pat."UMRNo" ,
pat."Email" ,
pat."Mobile" ,
pat."CountryId" ,
pat."ProfileImageUrl" ,
-- pat."ThumbnailUrl" ,
pat."Active" ,
(CASE WHEN pat."ThumbnailUrl" IS NOT NULL THEN CONCAT('https://hims-devv.s3.amazonaws.com/', pat."Guid", '/', pat."ThumbnailUrl") ELSE NULL END) AS "ThumbnailUrl"
,(max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId",apt."AppointmentDate")::text)::timestamp as "AppointmentDate",
(case when (max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId")::text)::timestamp > now() at time zone 'UTC-5:30' then true else false end)"isActiveAppointmentExists"
,(case when max(adm."AdmissionId")over(partition by adm."PatientId") is null then false
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is null then true
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is not null then false end
)"isActiveAdmissionExists"
FROM "Patient" pat
left join maxapt apt on apt."PatientId" = pat."PatientId" and apt."Status" <> 'C' and apt."Active"=true
left join maxadmision adm on adm."PatientId" = pat."PatientId" and adm."Active"=true
left join "Discharge" dc on dc."AdmissionId" = adm."AdmissionId"
WHERE pat."Active" IS TRUE
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "uMRNo" is null then 1=1 else pat."UMRNo" ilike'%'|| "uMRNo"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
;
END
$BODY$;
ALTER FUNCTION public."udf_FetchPatientsFilter_For_Appointment"(character varying,character varying, character varying, character varying)
-- DROP FUNCTION IF EXISTS public."UDF_Provider_Charges_From_Charge_Module"(integer, integer, integer, text, integer, integer);
CREATE OR REPLACE FUNCTION public."UDF_Provider_Charges_From_Charge_Module"(
providerid integer,
specializationid integer,
locationid integer,
appointmentdate text,
chargetypesid integer,
consultationtypeid integer)
RETURNS TABLE("DoctorSpecializationChargeModuleDetailsId" bigint, "ChargeTypesId" integer, "ChargeName" character varying, "Charge" numeric)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
begin
IF EXISTS (
Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationMap" DSM on DSM."DoctorSpecializationMapId" = DSD."ReferenceId"
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSM."ProviderId" = providerid and DSM."SpecializationId" = specializationid and DSD."LocationId" = locationid
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSM."ConsultationTypeId" = consultationtypeid
--END IF; 1
) THEN return query
Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationMap" DSM on DSM."DoctorSpecializationMapId" = DSD."ReferenceId"
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSM."ProviderId" = providerid and DSM."SpecializationId" = specializationid and DSD."LocationId" = locationid
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSM."ConsultationTypeId" = consultationtypeid;
ELSEIF EXISTS (
Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationMap" DSM on DSM."DoctorSpecializationMapId" = DSD."ReferenceId"
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSM."SpecializationId" = specializationid and DSD."LocationId" = locationid and DSM."ProviderId" is null
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSM."ConsultationTypeId" = consultationtypeid
) THEN return query Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationMap" DSM on DSM."DoctorSpecializationMapId" = DSD."ReferenceId"
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSM."SpecializationId" = specializationid and DSD."LocationId" = locationid and DSM."ProviderId" is null
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSM."ConsultationTypeId" = consultationtypeid;
--END IF; 2
ELSEIF EXISTS (Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationMap" DSM on DSM."DoctorSpecializationMapId" = DSD."ReferenceId"
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSD."LocationId" = locationid and DSM."ProviderId" = providerid and DSM."SpecializationId" is null
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSM."ConsultationTypeId" = consultationtypeid
) THEN return query Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationMap" DSM on DSM."DoctorSpecializationMapId" = DSD."ReferenceId"
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSD."LocationId" = locationid and DSM."ProviderId" = providerid and DSM."SpecializationId" is null
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSM."ConsultationTypeId" = consultationtypeid;
--END IF; 3
ELSEIF EXISTS (Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationMap" DSM on DSM."DoctorSpecializationMapId" = DSD."ReferenceId"
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSD."LocationId" = locationid and DSM."ProviderId" is null and DSM."SpecializationId" is null
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSM."ConsultationTypeId" = consultationtypeid
) THEN return query Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationMap" DSM on DSM."DoctorSpecializationMapId" = DSD."ReferenceId"
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId"
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true
where DSD."LocationId" = locationid and DSM."ProviderId" is null and DSM."SpecializationId" is null
AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
AND DSC."ChargeTypesId" = chargetypesid And DSM."ConsultationTypeId" = consultationtypeid;
END IF; -- 1
end
$BODY$;
ALTER FUNCTION public."UDF_Provider_Charges_From_Charge_Module"(integer, integer, integer, text, integer, integer)
"appointmentDate" character varying DEFAULT NULL::text)
RETURNS TABLE("ProviderAvailabilityId" integer, "FullName" character varying, "DepartmentName" character varying, "DepartmentId" integer, "ProviderId" integer, "SpecializationId" integer, "SpecializationName" character varying, "LocationId" integer, "ConsultationTypeId" integer)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
SELECT DISTINCT on (pr2."ProviderId", s."SpecializationId") prl."ProviderAvailabilityId" ,pr2."FullName"
, d."DepartmentName" , d."DepartmentId"
,pr2."ProviderId"
, s. "SpecializationId", s."SpecializationName"
, CMT."LocationId", prl."ConsultationTypeId"
FROM "ProviderAvailability" prl
Join "DoctorSpecializationMap" LSM on LSM."ProviderId" = prl."ProviderId" and LSM."SpecializationId" = prl."SpecializationId" and LSM."ConsultationTypeId" = prl."ConsultationTypeId"
join "DoctorSpecializationChargeModuleDetails" DSCD on DSCD."ReferenceId" = LSM."DoctorSpecializationMapId"
join "DoctorSpecializationChargeModuleCategory" DSCC on DSCC."DoctorSpecializationChargeModuleCategoryId" = DSCD."DoctorSpecializationChargeModuleCategoryId"
join "ChargeModuleTemplate" CMT on CMT."ChargeModuleTemplateId" = DSCC."ChargeModuleTemplateId"
JOIN "Provider" pr2 on pr2."ProviderId" = LSM."ProviderId" and pr2."Active" is true
join "Account" pa on pa."ReferenceId" = pr2."ProviderId" and pa."Active" is true and pa."RoleId" = 3
JOin "LocationAccountMap" LAM on LAM."AccountId" = pa."AccountId"
join "Specialization" s on s."SpecializationId" = ANY (pr2."Specializations")
JOIN "Location" pral on pral."LocationId" = CMT."LocationId" AND pral."Active" IS TRUE
JOIN "Practice" pra on pra."PracticeId" = pral."PracticeId" AND pra."Active" IS TRUE
JOIN "Department" d on d."DepartmentId" = pr2."DepartmentId"
where pr2."Active" is true and CMT."LocationId" = "locationId" and s."Active" = true
--and CMT."StartDate"::date <= "appointmentDate"::date and CMT."EndDate"::date >= "appointmentDate"::date
and "IsInUse" is true
and case when "filter" is null then 1=1 else TRIM(UPPER(pr2."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(s."SpecializationName")) ilike'%'|| "filter"||'%' end
and case when "consultationTypeId" is null then 1=1 else prl."ConsultationTypeId" = "consultationTypeId" end
and case when "appointmentDate" is null then CMT."StartDate"::date <= current_date::date and CMT."EndDate"::date >= current_date::date else CMT."StartDate"::date <= "appointmentDate"::date and CMT."EndDate"::date >= "appointmentDate"::date end
--and case when "locationId" is null then 1=1 else LAM."LocationId" = "LocationId" end
--order by "FullName" asc
;
END
$BODY$;
ALTER FUNCTION public."udf_FetchDoctor_With_Availability_Specialization_Op"(character varying, integer, integer, character varying)
-- FUNCTION: public.udf_FetchPatients_For_QuickSchedule(character varying, character varying, character varying)
DROP FUNCTION if exists public."udf_FetchPatients_For_QuickSchedule"(character varying, character varying, character varying);
CREATE OR REPLACE FUNCTION public."udf_FetchPatients_For_QuickSchedule"(
filter character varying DEFAULT NULL::text,
"patientMobile" character varying DEFAULT NULL::text,
"patientName" character varying DEFAULT NULL::text)
RETURNS TABLE("PatientId" integer, "Salutation" character varying, "FirstName" text, "MiddleName" text, "LastName" text, "FullName" text, "DateOfBirth" date, "Age" smallint, "Gender" character, "UMRNo" character varying, "Email" character varying, "Mobile" character varying, "CountryId" integer, "ProfileImageUrl" text, "Active" boolean, "ThumbnailUrl" text, "AppointmentDate" timestamp without time zone, "isActiveAppointmentExists" boolean, "isActiveAdmissionExists" boolean)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
with cts as (
select row_number() over(partition by a."PatientId" order by a."AppointmentId" desc, a."AppointmentDate" desc )"Max" ,a."PatientId",a."AppointmentId" from "Appointment" a
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
--and case when "filter" is null then 1=1 else (TRIM(UPPER(pat."FullName")) ILIKE '%' OR pat."UMRNo" ilike'%' OR pat."Mobile" ILIKE '%')|| "filter"||'%' end
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxapt as (
select a.* from "Appointment" a
join cts on a."AppointmentId"=cts."AppointmentId"
where "Max"=1)
,ctsadmission as (
select distinct max(a."AdmissionId") over(partition by a."PatientId" )"MaxAdmissionId" ,a."PatientId",
a."AppointmentId" from "Admission" a
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
--and case when "filter" is null then 1=1 else (TRIM(UPPER(pat."FullName")) ILIKE '%' OR pat."UMRNo" ilike'%' OR pat."Mobile" ILIKE '%') || "filter"||'%' end
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxadmision as (
select a.* from "Admission" a
join ctsadmission cts on a."AdmissionId"=cts."MaxAdmissionId"
)
SELECT DISTINCT pat."PatientId" ,
pat."Salutation" ,
pat."FirstName" ,
pat."MiddleName" ,
pat."LastName" ,
pat."FullName" ,
pat."DateOfBirth" ,
pat."Age" ,
pat."Gender" ,
pat."UMRNo" ,
pat."Email" ,
pat."Mobile" ,
pat."CountryId" ,
pat."ProfileImageUrl" ,
-- pat."ThumbnailUrl" ,
pat."Active" ,
(CASE WHEN pat."ThumbnailUrl" IS NOT NULL THEN CONCAT('https://hims-devv.s3.amazonaws.com/', pat."Guid", '/', pat."ThumbnailUrl") ELSE NULL END) AS "ThumbnailUrl"
,(max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId",apt."AppointmentDate")::text)::timestamp as "AppointmentDate",
(case when (max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId")::text)::timestamp > now() at time zone 'UTC-5:30' then true else false end)"isActiveAppointmentExists"
,(case when max(adm."AdmissionId")over(partition by adm."PatientId") is null then false
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is null then true
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is not null then false end
)"isActiveAdmissionExists"
FROM "Patient" pat
left join maxapt apt on apt."PatientId" = pat."PatientId" and apt."Status" <> 'C' and apt."Active"=true
left join maxadmision adm on adm."PatientId" = pat."PatientId" and adm."Active"=true
left join "Discharge" dc on dc."AdmissionId" = adm."AdmissionId"
WHERE pat."Active" IS TRUE
--and case when "filter" is null then 1=1 else (TRIM(UPPER(pat."FullName")) ILIKE '%' OR pat."UMRNo" ilike'%' OR pat."Mobile" ILIKE '%')|| "filter"||'%' end
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
;
END
$BODY$;
ALTER FUNCTION public."udf_FetchPatients_For_QuickSchedule"(character varying, character varying, character varying)
RETURNS TABLE("ProviderAvailabilityId" integer, "FullName" character varying, "DepartmentName" character varying, "DepartmentId" integer, "ProviderId" integer, "SpecializationId" integer, "SpecializationName" character varying, "LocationId" integer, "ConsultationTypeId" integer)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
SELECT DISTINCT on (pr2."ProviderId", s."SpecializationId") prl."ProviderAvailabilityId" ,pr2."FullName"
, d."DepartmentName" , d."DepartmentId"
,pr2."ProviderId"
, s. "SpecializationId", s."SpecializationName"
, prl."LocationId", prl."ConsultationTypeId"
FROM "ProviderAvailability" prl
JOIN "Provider" pr2 on pr2."ProviderId" = prl."ProviderId" and pr2."Active" is true
join "Account" pa on pa."ReferenceId" = pr2."ProviderId" and pa."Active" is true and pa."RoleId" = 3
JOin "LocationAccountMap" LAM on LAM."AccountId" = pa."AccountId"
join "Specialization" s on s."SpecializationId" = ANY (pr2."Specializations")
JOIN "Location" pral on pral."LocationId" = prl."LocationId" AND pral."Active" IS TRUE
JOIN "Practice" pra on pra."PracticeId" = pral."PracticeId" AND pra."Active" IS TRUE
JOIN "Department" d on d."DepartmentId" = pr2."DepartmentId"
where pr2."Active" is true and prl."LocationId" = "locationId" and s."Active" = true
and case when "filter" is null then 1=1 else TRIM(UPPER(pr2."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(s."SpecializationName")) ilike'%'|| "filter"||'%' end
and case when "consultationTypeId" is null then 1=1 else prl."ConsultationTypeId" = "consultationTypeId" end
--and case when "locationId" is null then 1=1 else LAM."LocationId" = "LocationId" end
--order by "FullName" asc
;
END
$BODY$;
ALTER FUNCTION public."udf_FetchDoctor_With_Availability_Specialization"(character varying, integer, integer)
-- FUNCTION: public.udf_Employee_Revenue_Location(integer, integer, integer, timestamp without time zone, timestamp without time zone)
-- DROP FUNCTION IF EXISTS public."udf_Employee_Revenue_Location"(integer, integer, integer, timestamp without time zone, timestamp without time zone);
CREATE OR REPLACE FUNCTION public."udf_Employee_Revenue_Location"(
accountid integer DEFAULT NULL::integer,
locationid integer DEFAULT NULL::integer,
roleid 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)
CREATE OR REPLACE FUNCTION public."udf_FetchPatientsFilter_For_Appointment1"(
filter character varying DEFAULT NULL::text,
"patientMobile" character varying DEFAULT NULL::text,
"uMRNo" character varying DEFAULT NULL::text,
"patientName" character varying DEFAULT NULL::text,
"billNumber" text DEFAULT NULL::text)
RETURNS TABLE("PatientId" integer, "Salutation" character varying, "FirstName" text, "MiddleName" text, "LastName" text, "FullName" text, "DateOfBirth" date, "Age" smallint, "Gender" character, "UMRNo" character varying, "Email" character varying, "Mobile" character varying, "CountryId" integer, "ProfileImageUrl" text, "Active" boolean, "ThumbnailUrl" text, "AppointmentDate" timestamp without time zone, "isActiveAppointmentExists" boolean, "isActiveAdmissionExists" boolean, "BillNumber" text)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
with cts as (
select row_number() over(partition by a."PatientId" order by a."AppointmentId" desc, a."AppointmentDate" desc )"Max" ,a."PatientId",a."AppointmentId"
from "Appointment" a
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "uMRNo" is null then 1=1 else pat."UMRNo" ilike'%'|| "uMRNo"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxapt as (
select a.* from "Appointment" a
join cts on a."AppointmentId"=cts."AppointmentId"
where "Max"=1)
,ctsadmission as (
select distinct max(a."AdmissionId") over(partition by a."PatientId" )"MaxAdmissionId" ,a."PatientId",
a."AppointmentId" from "Admission" a
join "Patient" pat on a."PatientId" =pat."PatientId"
where a."Active"=true
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "uMRNo" is null then 1=1 else pat."UMRNo" ilike'%'|| "uMRNo"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
)
, maxadmision as (
select a.* from "Admission" a
join ctsadmission cts on a."AdmissionId"=cts."MaxAdmissionId"
)
SELECT DISTINCT pat."PatientId" ,
pat."Salutation" ,
pat."FirstName" ,
pat."MiddleName" ,
pat."LastName" ,
pat."FullName" ,
pat."DateOfBirth" ,
pat."Age" ,
pat."Gender" ,
pat."UMRNo" ,
pat."Email" ,
pat."Mobile" ,
pat."CountryId" ,
pat."ProfileImageUrl" ,
-- pat."ThumbnailUrl" ,
pat."Active" ,
(CASE WHEN pat."ThumbnailUrl" IS NOT NULL THEN CONCAT('https://hims-devv.s3.amazonaws.com/', pat."Guid", '/', pat."ThumbnailUrl") ELSE NULL END) AS "ThumbnailUrl"
,(max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId",apt."AppointmentDate")::text)::timestamp as "AppointmentDate",
(case when (max(apt."AppointmentDate")over(partition by apt."PatientId")::text ||' ' || max(apt."AppointmentTime")over(partition by apt."PatientId")::text)::timestamp > now() at time zone 'UTC-5:30' then true else false end)"isActiveAppointmentExists"
,(case when max(adm."AdmissionId")over(partition by adm."PatientId") is null then false
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is null then true
when max(adm."AdmissionId")over(partition by adm."PatientId") is not null and max(dc."DischargeId")over(partition by pat."PatientId") is not null then false end
)"isActiveAdmissionExists"
,LBH."BillNumber"
FROM "Patient" pat
left join maxapt apt on apt."PatientId" = pat."PatientId" and apt."Status" <> 'C' and apt."Active"=true
left join maxadmision adm on adm."PatientId" = pat."PatientId" and adm."Active"=true
left join "Discharge" dc on dc."AdmissionId" = adm."AdmissionId"
left join "LabBookingHeader" LBH on LBH."PatientId"=pat."PatientId"
WHERE pat."Active" IS TRUE
and case when "filter" is null then 1=1 else TRIM(UPPER(pat."FullName")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."UMRNo")) ilike'%'|| "filter"||'%' OR TRIM(UPPER(pat."Mobile")) ilike'%'|| "filter"||'%' end
and case when "uMRNo" is null then 1=1 else pat."UMRNo" ilike'%'|| "uMRNo"||'%' end
and case when "patientMobile" is null then 1=1 else pat."Mobile" ilike'%'|| "patientMobile"||'%' end
and case when "patientName" is null then 1=1 else pat."FullName" ilike'%'|| "patientName"||'%' end
and case when "billNumber" is null then 1=1 else LBH."BillNumber" ilike'%'|| "billNumber"||'%' end
-- FUNCTION: public.udf_PharmacyBills_Report(text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone, text, integer, text)
-- DROP FUNCTION IF EXISTS public."udf_PharmacyBills_Report"(text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone, text, integer, text);
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,
"payTypeId" integer DEFAULT NULL::integer,
"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" character varying, "PaymentNumber" character varying, "CreatedByName" text, "RoleName" character varying, "ProviderName" character varying, "TotalAmount" numeric, "RetailName" text, "OverallTaxes" numeric, "LocationName" character varying)
,Pa."UMRNo",PT."PayTypeName" as "PaidVia",PH."PaymentNumber",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"
left join "PayType" PT on PT."PayTypeId"=PH."PayTypeId"
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 "payTypeId" is null then 1=1 else PT."PayTypeId" = "payTypeId" 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",PT."PayTypeName", PH."PaymentNumber", A."FullName"
ALTER FUNCTION public."udf_PharmacyBills_Report"(text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone, text, integer, text)
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",
RETURNS TABLE("DepartmentId" text, "DepartmentName" character varying, "ProviderId" text, "ProviderName" character varying, "AppointmentTypeName" character varying, "AppointmentDate" date, "AppointmentNo" character varying, "PatientId" integer, "Name" character varying, "ReferredByName" character varying, "PatientName" character varying, "PatientAge" smallint, "UMRNo" character varying, "Mobile" character varying, "PatientGender" character, "AppointmentTime" time without time zone, "VisitType" character, "PaymentType" character varying, "PaymentNumber" character varying, "TotalAppointments" bigint, "TotalAmount" bigint, "TotalAmountStr" text, "ReceiptCreatedByName" text, "ReceiptDate" timestamp without time zone, "ReceiptId" integer, "StreetAdress" character varying, "City" character varying, "State" character varying, "FatherOrHusband" text)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
return query
with cts as (select coalesce(A."DepartmentId"::text,'GrandTotal') "DepartmentId",