From 610b8117a0ea63d20af49e539ae930ff254b2b41 Mon Sep 17 00:00:00 2001 From: stephen waite Date: Fri, 24 May 2024 14:54:01 -0400 Subject: [PATCH] feat: show collection balance in billing widget (#7454) --- library/patient.inc.php | 21 ++++++++++++++++----- src/Patient/Cards/BillingViewCard.php | 2 ++ templates/patient/card/billing.html.twig | 7 +++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/library/patient.inc.php b/library/patient.inc.php index c519ea174..687f04c16 100644 --- a/library/patient.inc.php +++ b/library/patient.inc.php @@ -1598,7 +1598,7 @@ function getAllinsurances($pid) * @param int Optional encounter id. If value is passed, will fetch only bills from specified encounter. * @return number The balance. */ -function get_patient_balance($pid, $with_insurance = false, $eid = false) +function get_patient_balance($pid, $with_insurance = false, $eid = false, $in_collection = false) { $balance = 0; $bindarray = array($pid); @@ -1609,6 +1609,11 @@ function get_patient_balance($pid, $with_insurance = false, $eid = false) $sqlstatement .= " AND encounter = ?"; array_push($bindarray, $eid); } + + if ($in_collection) { + $sqlstatement .= " AND in_collection = ?"; + array_push($bindarray, 1); + } $feres = sqlStatement($sqlstatement, $bindarray); while ($ferow = sqlFetchArray($feres)) { $encounter = $ferow['encounter']; @@ -1641,10 +1646,16 @@ function get_patient_balance($pid, $with_insurance = false, $eid = false) $balance += $ptbal; } } else { - // Including insurance or not out to insurance, everything is due. - $brow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " . - "pid = ? AND encounter = ? AND " . - "activity = 1", array($pid, $encounter)); + if (!$with_insurance && $ferow['last_level_closed'] >= $inscount && $in_collection) { + $brow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " . + "pid = ? AND encounter = ? AND " . + "activity = 1", array($pid, $encounter)); + } else { + // Including insurance or not out to insurance, everything is due. + $brow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " . + "pid = ? AND encounter = ? AND " . + "activity = 1", array($pid, $encounter)); + } $drow = sqlQuery("SELECT SUM(pay_amount) AS payments, " . "SUM(adj_amount) AS adjustments FROM ar_activity WHERE " . "deleted IS NULL AND pid = ? AND encounter = ?", array($pid, $encounter)); diff --git a/src/Patient/Cards/BillingViewCard.php b/src/Patient/Cards/BillingViewCard.php index 8b1caef6b..0785f14df 100644 --- a/src/Patient/Cards/BillingViewCard.php +++ b/src/Patient/Cards/BillingViewCard.php @@ -67,6 +67,7 @@ class BillingViewCard extends CardModel $insurancebalance = get_patient_balance($pid, true) - $patientbalance; $totalbalance = $patientbalance + $insurancebalance; $unallocated_amt = get_unallocated_patient_balance($pid); + $collectionbalance = get_patient_balance($pid, false, false, true); $id = self::CARD_ID . "_ps_expand"; $dispatchResult = $ed->dispatch(new RenderEvent('billing'), RenderEvent::EVENT_HANDLE); @@ -79,6 +80,7 @@ class BillingViewCard extends CardModel 'patientBalance' => $patientbalance, 'insuranceBalance' => $insurancebalance, 'totalBalance' => $totalbalance, + 'collectionBalance' => $collectionbalance, 'unallocated' => $unallocated_amt, 'forceAlwaysOpen' => $forceBillingExpandAlways, 'prependedInjection' => $dispatchResult->getPrependedInjection(), diff --git a/templates/patient/card/billing.html.twig b/templates/patient/card/billing.html.twig index 67df0476d..30bea509f 100644 --- a/templates/patient/card/billing.html.twig +++ b/templates/patient/card/billing.html.twig @@ -22,6 +22,13 @@ {% endif %} + {% if collectionBalance > 0 %} +
+
{{ "Collection Balance"|xlt }}
+
{{ collectionBalance|money|text }}
+
+ {% endif %} + {% if billingNote %}
{{ "Billing Note"|xlt }}
-- 2.11.4.GIT