Skip to content
120 changes: 120 additions & 0 deletions onprc_ehr/resources/queries/onprc_ehr/BillingDiscrepancyforR_L.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
=====================================================================
Query Name : GJ_BillingDiscrepanciesRL
Schema : onprc_ehr
Purpose : Identify procedure charges where the billed project
does not match the animal’s current project,
using project.displayName for clarity
Parameters : @DaysBack (integer, 1–30)
Issue / Ticket : EHR Issue 11870
Author : jonesga
Last Modified : 2026-04-17
=====================================================================
*/
/*
=====================================================================
Query Name : GJ_BillingDiscrepanciesRL
Schema : onprc_ehr
Purpose : Identify procedure charges where the billed project
does not match the animal’s current project and
highlight animals with dual active assignments.
Issue / Ticket : EHR Issue 11870
Author : jonesga
Last Modified : 2026-04-21
=====================================================================
*/

WITH ProcedureFees AS (
SELECT
pfr.id,
pfr.date,
pfr.project.displayName AS ProjectBilledTo,
pfr.project AS billedProjectObj,
pfr.chargeType,
pfr.procedureId,
pfr.chargeId,
pfr.unitCost,
pfr.matchesProject,
pfr.taskId
FROM onprc_billing.procedureFeeRates pfr
),

/*-------------------------------------------------------------------
Active assignment context
-------------------------------------------------------------------*/
ActiveAssignments AS (
SELECT
a.Id,
a.project,
a.project.displayName AS ProjectName
FROM study.assignment a
WHERE
a.enddate IS NULL
OR a.enddate >= CURDATE()
),

AssignmentCounts AS (
SELECT
Id,
COUNT(DISTINCT project) AS AssignmentCount
FROM ActiveAssignments
GROUP BY Id
),

AnimalAssignmentStatus AS (
SELECT
aa.Id,
aa.ProjectName AS CurrentProject,
ac.AssignmentCount,
CASE
WHEN ac.AssignmentCount > 1 THEN true
ELSE false
END AS IsDualAssigned
FROM ActiveAssignments aa
LEFT JOIN AssignmentCounts ac
ON aa.Id = ac.Id
)

/*-------------------------------------------------------------------
Final result
-------------------------------------------------------------------*/
SELECT
pf.id,
pf.date,

pf.ProjectBilledTo,
aas.CurrentProject,

CASE
WHEN pf.ProjectBilledTo = aas.CurrentProject
THEN '✅ Billing is Correct'
ELSE '❌ Billing Needs Review'
END AS ChargeReview,

CASE
WHEN aas.IsDualAssigned = true
THEN '⚠️ Dual Assigned'
ELSE 'Single Assignment'
END AS AssignmentStatus,

aas.AssignmentCount,

pf.chargeType,
pf.procedureId,
pf.chargeId,
pf.unitCost,
pf.matchesProject,
pf.taskId

FROM ProcedureFees pf
LEFT JOIN AnimalAssignmentStatus aas
ON pf.id = aas.Id

WHERE
pf.ProjectBilledTo NOT LIKE '0492-%'
--or
--AND aas.IsDualAssigned = true)

ORDER BY
pf.date DESC,
pf.id;
79 changes: 79 additions & 0 deletions onprc_ehr/resources/queries/onprc_ehr/BillingDiscrepancyforR_L.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<metadata>

<!-- ========================= -->
<!-- Parameters -->
<!-- ========================= -->
<parameters>
<parameter>
<name>DaysBack</name>
<label>Number of Days Back to Review</label>
<type>int</type>
<required>true</required>
<defaultValue>1</defaultValue>
<description>
Number of prior days to include in the review window.
Valid values are 1 through 30.
</description>
</parameter>
</parameters>

<!-- ========================= -->
<!-- Columns -->
<!-- ========================= -->
<columns>

<column>
<name>ProjectBilledTo</name>
<label>Project Billed To</label>
<description>
The project to which the procedure charge was billed.
</description>
</column>

<column>
<name>CurrentProject</name>
<label>Current Animal Project</label>
<description>
The animal’s current active project at the time of review.
</description>
</column>

<!-- ✅ Charge Review with Conditional Formatting -->
<column>
<name>ChargeReview</name>
<label>Billing Review Status</label>

<conditionalFormats>

<!-- Billing is Correct -->
<conditionalFormat>
<operator>equals</operator>
<value>Billing is Correct</value>
<backgroundColor>#2E7D32</backgroundColor> <!-- Green -->
<textColor>#FFD700</textColor> <!-- Yellow -->
</conditionalFormat>

<!-- Billing Needs Review -->
<conditionalFormat>
<operator>equals</operator>
<value>Billing Needs Review</value>
<backgroundColor>#C62828</backgroundColor> <!-- Red -->
<textColor>#FFFFFF</textColor> <!-- White -->
<fontWeight>bold</fontWeight>
</conditionalFormat>

</conditionalFormats>

</column>



<column>
<name>unitCost</name>
<label>Unit Cost ($)</label>
<format>$#,##0.00</format>
</column>

</columns>

</metadata>
124 changes: 124 additions & 0 deletions onprc_ehr/resources/queries/onprc_ehr/gdj_BillingDiscrepancyforR_L.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<metadata>
<!--

&lt;!&ndash; =====================================================
Query-Level Metadata
===================================================== &ndash;&gt;
<description>
Identifies procedure charges where the billed project
does not match the animal’s current project.
Intended for R&amp;L review and reconciliation.
</description>

<title>
Billing Discrepancy Review – Project Mismatch
</title>

<notes>
Created for EHR Issue 11870.
Uses a configurable lookback window defined by the DaysBack parameter.
Project comparisons are based on project.displayName for clarity.
</notes>
-->


<!-- =====================================================
Parameters
===================================================== -->
<parameters>
<parameter>
<name>DaysBack</name>
<label>Number of Days Back to Review</label>
<type>int</type>
<required>true</required>
<defaultValue>1</defaultValue>
<description>
Number of prior days to include in the review window.
Valid values are 1 through 30.
Example: 7 = review the last 7 days.
</description>
</parameter>
</parameters>


<!-- =====================================================
Column Metadata
===================================================== -->
<columns>

<column>
<name>ProjectBilledTo</name>
<label>Project Billed To</label>
<description>
The project to which the procedure charge will be billed.
</description>
</column>

<column>
<name>CurrentProject</name>
<label>Current Animal Project</label>
<description>
The animal’s current active project at the time of review.
</description>
</column>

<column>
<name>ChargeReview</name>
<label>Billing Review Status</label>
<description>
Indicates whether the billed project matches the animal’s
current project. Records marked with a red X require review.
</description>
</column>

<column>
<name>DaysBackUsed</name>
<label>Days Back Applied</label>
<description>
The actual number of days included in this run after
validation and bounds enforcement.
</description>
</column>

<column>
<name>unitCost</name>
<label>Unit Cost ($)</label>
<format>$#,##0.00</format>
</column>

</columns>


<!-- =====================================================
Conditional Formatting (Optional but Powerful)
===================================================== -->
<conditionalFormats>

<conditionalFormat>
<columnName>ChargeReview</columnName>
<operator>equals</operator>
<value>❌ Billing Needs Review</value>
<backgroundColor>#F8D7DA</backgroundColor>
<textColor>#721C24</textColor>
</conditionalFormat>

<conditionalFormat>
<columnName>ChargeReview</columnName>
<operator>equals</operator>
<value>✅ Billing is Correct</value>
<backgroundColor>#D4EDDA</backgroundColor>
<textColor>#155724</textColor>
</conditionalFormat>

</conditionalFormats>


<!-- =====================================================
Visibility / Usability Controls (Optional)
===================================================== -->
<hiddenColumns>
<!-- Example: hide technical IDs if not useful to end users -->
<!-- <columnName>id</columnName> -->
</hiddenColumns>

</metadata>
Loading