diff --git a/library/patient.inc.php b/library/patient.inc.php index c519ea174f4..687f04c167d 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 8b1caef6b94..0785f14dfbc 100644 --- a/src/Patient/Cards/BillingViewCard.php +++ b/src/Patient/Cards/BillingViewCard.php @@ -67,6 +67,7 @@ private function setupBillingData() $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 @@ private function setupBillingData() '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 67df0476d4b..30bea509f3c 100644 --- a/templates/patient/card/billing.html.twig +++ b/templates/patient/card/billing.html.twig @@ -22,6 +22,13 @@ {% endif %} + {% if collectionBalance > 0 %} +