chore(deps): bump twig/twig from 3.17.1 to 3.19.0 (#7951)
[openemr.git] / interface / billing / payment_pat_sel.inc.php
blobfb60d01eb069c3cdd61787420509570e2c27ab31
1 <?php
3 /**
4 * payment_pat_sel.inc.php
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Eldho Chacko <eldho@zhservices.com>
9 * @author Paul Simon K <paul@zhservices.com>
10 * @author Stephen Waite <stephen.waite@cmsvt.com>
11 * @author Brady Miller <brady.g.miller@gmail.com>
12 * @author Rod Roark <rod@sunsetsystems.com>
13 * @copyright Copyright (c) 2010 Z&H Consultancy Services Private Limited <sam@zhservices.com>
14 * @copyright Copyright (c) 2018 Stephen Waite <stephen.waite@cmsvt.com>
15 * @copyright Copyright (c) 2019-2020 Brady Miller <brady.g.miller@gmail.com>
16 * @copyright Copyright (c) 2020 Rod Roark <rod@sunsetsystems.com>
17 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
20 use OpenEMR\Billing\SLEOB;
22 //-----------------------------------------------------------------------+
23 //===============================================================================
24 //Patient ajax section and listing of charges..Used in New Payment and Edit Payment screen.
25 //===============================================================================
26 if (isset($_POST["mode"])) {
27 if (
28 ($_POST["mode"] == "search") || (($_POST["default_search_patient"] ?? null) == "default_search_patient") &&
29 isset($_REQUEST['hidden_patient_code']) &&
30 (int)$_REQUEST['hidden_patient_code'] > 0
31 ) {
32 $hidden_patient_code = $_REQUEST['hidden_patient_code'];
33 $RadioPaid = $_REQUEST['RadioPaid'] ?? null;
34 if ($RadioPaid == 'Show_Paid') {
35 $StringForQuery = '';
36 } elseif ($RadioPaid == 'Non_Paid') {
37 $StringForQuery = " and last_level_closed = 0 ";
38 } elseif ($RadioPaid == 'Show_Primary_Complete') {
39 $StringForQuery = " and last_level_closed >= 1 ";
41 $ResultSearchNew = sqlStatement("SELECT b.id,last_level_closed,b.encounter,fe.`date`,b.code_type,b.code,b.modifier,fee
42 FROM billing AS b,form_encounter AS fe, code_types AS ct
43 WHERE b.encounter=fe.encounter AND b.code_type=ct.ct_key AND ct.ct_diag=0
44 AND b.activity!=0 AND fe.pid =? AND b.pid =?
45 " . ($StringForQuery ?? '') . " ORDER BY fe.`date`, fe.encounter,b.code,b.modifier", array($hidden_patient_code, $hidden_patient_code));
46 $res = sqlStatement("SELECT fname,lname,mname FROM patient_data
47 where pid =?", array($hidden_patient_code));
48 $row = sqlFetchArray($res);
49 $fname = $row['fname'] ?? '';
50 $lname = $row['lname'] ?? '';
51 $mname = $row['mname'] ?? '';
52 $NameNew = $lname . ' ' . $fname . ' ' . $mname;
55 //===============================================================================
57 <fieldset>
58 <legend><?php echo xlt('Distribute')?></legend>
59 <div class="row pb-2" id="TablePatientPortion">
60 <div class="frames col-3">
61 <input id="hidden_ajax_patient_close_value" type="hidden" value="<?php echo (empty($Message)) ? attr($NameNew ?? '') : '' ;?>" />
62 <label class="control-label" for="patient_code"><?php echo xlt('Patient'); ?>:</label>
63 <input class="form-control" name='patient_code' class="text" id='patient_code' onKeyDown="PreventIt(event)" value="<?php echo (empty($Message)) ? attr($NameNew ?? '') : '' ;?>" autocomplete="off" />
64 </div>
65 <div class="frames col-2">
66 <label class="control-label" for="patient_name"><?php echo xlt('Patient Id'); ?>:</label>
67 <div class="form-control" name="patient_name" id="patient_name">
68 <?php echo (empty($Message)) ? text($hidden_patient_code ?? '') : ''; ?>
69 </div>
70 </div>
71 <div class="frames col">
72 <label class="control-label" for="type_code"><?php echo xlt('Select'); ?>:</label>
73 <div>
74 <label class="radio-inline">
75 <input type="radio" id="Non_Paid" name="RadioPaid" onclick="SearchOnceMore()" <?php echo (empty($_REQUEST['RadioPaid']) || ($_REQUEST['RadioPaid'] == 'Non_Paid')) ? 'checked' : '' ; ?> value="Non_Paid" /><?php echo xlt('Non Paid'); ?>
76 </label>
77 <label class="radio-inline">
78 <input type="radio" id="Show_Primary_Complete" name="RadioPaid" onclick="SearchOnceMore()" <?php echo (!empty($_REQUEST['RadioPaid']) && ($_REQUEST['RadioPaid'] == 'Show_Primary_Complete')) ? 'checked' : '' ; ?> value="Show_Primary_Complete" /><?php echo xlt('Show Primary Complete'); ?>
79 </label>
80 <label class="radio-inline">
81 <input type="radio" id="Show_Paid" name="RadioPaid" onclick="SearchOnceMore()" <?php echo (!empty($_REQUEST['RadioPaid']) && ($_REQUEST['RadioPaid'] == 'Show_Paid')) ? 'checked' : '' ; ?> value="Show_Paid" /><?php echo xlt('Show All Transactions'); ?>
82 </label>
83 </div>
84 </div>
85 </div>
86 <div id='ajax_div_patient_section'>
87 <div id='ajax_div_patient_error'>
88 </div>
89 <div id="ajax_div_patient" style="display: none;">
90 </div>
91 </div>
92 </fieldset>
93 <?php //New distribution section
94 //$CountIndex=0;
95 $CountIndexBelow = 0;
96 $PreviousEncounter = 0;
97 $PreviousPID = 0;
98 if (!empty($ResultSearchNew)) {
99 if ($RowSearch = sqlFetchArray($ResultSearchNew)) { ?>
100 <div class="overflow-auto">
101 <div class="table-responsive-lg">
102 <table class="table table-sm table-bordered table-light" id="TableDistributePortion">
103 <thead class="bg-dark text-light">
104 <tr>
105 <th><?php echo xlt('Post For'); ?></th>
106 <th><?php echo xlt('Service Date'); ?></th>
107 <th><?php echo xlt('Enc#'); ?></th>
108 <th><?php echo xlt('Service Code'); ?></th>
109 <th><?php echo xlt('Charge'); ?></th>
110 <th><?php echo xlt('Copay'); ?></th>
111 <th><?php echo xlt('Bal Due'); ?></th>
112 <th><?php echo xlt('Allowed'); ?></th>
113 <th><?php echo xlt('Payment'); ?></th>
114 <th><?php echo xlt('Adj Amount'); ?></th>
115 <th><?php echo xlt('Deductible'); ?></th>
116 <th><?php echo xlt('Takeback'); ?></th>
117 <th><?php echo xlt('MSP'); ?></th>
118 <th><?php echo xlt('Follow Up'); ?></th>
119 <th><?php echo xlt('Follow Up Reason'); ?></th>
120 </tr>
121 </thead>
122 <?php
123 do {
124 $CountIndex = $CountIndex ?? null;
125 $CountIndex++;
126 $CountIndexBelow++;
127 $Ins = 0;
128 // Determine the next insurance level to be billed.
129 $ferow = sqlQuery("SELECT date, last_level_closed " .
130 "FROM form_encounter WHERE " .
131 "pid = ? AND encounter = ?", array($hidden_patient_code, $RowSearch['encounter']));
132 $date_of_service = substr($ferow['date'], 0, 10);
133 $new_payer_type = 0 + $ferow['last_level_closed'];
134 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) || $new_payer_type == 0) {
135 ++$new_payer_type;
137 $new_payer_id = SLEOB::arGetPayerID($hidden_patient_code, $date_of_service, $new_payer_type);
139 if ($new_payer_id == 0) {
140 $Ins = 0;
141 } elseif ($new_payer_id > 0) {
142 $Ins = $new_payer_type;
146 $ServiceDateArray = explode(' ', $RowSearch['date']);
147 $ServiceDate = oeFormatShortDate($ServiceDateArray[0]);
148 $Codetype = $RowSearch['code_type'];
149 $Code = $RowSearch['code'];
150 $Modifier = $RowSearch['modifier'];
151 if ($Modifier != '') {
152 $ModifierString = ", $Modifier";
153 } else {
154 $ModifierString = "";
156 $Fee = $RowSearch['fee'];
157 $Encounter = $RowSearch['encounter'];
159 //Always associating the copay to a particular charge.
160 $BillingId = $RowSearch['id'];
161 $resId = sqlStatement("SELECT b.id FROM billing AS b, code_types AS ct
162 WHERE b.code_type=ct.ct_key AND ct.ct_diag=0 AND
163 b.pid=? AND b.encounter=?
164 AND b.activity!=0 ORDER BY id", array($hidden_patient_code, $Encounter));
165 $rowId = sqlFetchArray($resId);
166 $Id = $rowId['id'];
168 if ($BillingId != $Id) {//multiple cpt in single encounter
169 $Copay = 0.00;
170 } else {
171 $resCopay = sqlStatement("SELECT sum(fee) as copay FROM billing where code_type='COPAY' and
172 pid =? and encounter =? and billing.activity!=0", array($hidden_patient_code, $Encounter));
173 $rowCopay = sqlFetchArray($resCopay);
174 $Copay = $rowCopay['copay'] * -1;
176 $resMoneyGot = sqlStatement(
177 "SELECT sum(pay_amount) as PatientPay FROM ar_activity where " .
178 "deleted IS NULL AND pid = ? and encounter = ? and payer_type = 0 and " .
179 "account_code = 'PCP'",
180 array($hidden_patient_code, $Encounter)
181 );//new fees screen copay gives account_code='PCP'
182 $rowMoneyGot = sqlFetchArray($resMoneyGot);
183 $PatientPay = $rowMoneyGot['PatientPay'];
185 $Copay = $Copay + $PatientPay;
187 //payer_type!=0, supports both mapped and unmapped code_type in ar_activity
188 $resMoneyGot = sqlStatement(
189 "SELECT sum(pay_amount) as MoneyGot FROM ar_activity where " .
190 "deleted IS NULL AND pid = ? and (code_type = ? or code_type = '') and " .
191 "code = ? and modifier = ? and encounter = ? and ! (payer_type = 0 and " .
192 "account_code = 'PCP')",
193 array($hidden_patient_code, $Codetype, $Code, $Modifier, $Encounter)
194 );//new fees screen copay gives account_code='PCP'
195 $rowMoneyGot = sqlFetchArray($resMoneyGot);
196 $MoneyGot = $rowMoneyGot['MoneyGot'];
197 //supports both mapped and unmapped code_type in ar_activity
198 $resMoneyAdjusted = sqlStatement(
199 "SELECT sum(adj_amount) as MoneyAdjusted FROM ar_activity where " .
200 "deleted IS NULL AND pid = ? and (code_type = ? or code_type = '') and " .
201 "code = ? and modifier = ? and encounter = ?",
202 array($hidden_patient_code, $Codetype, $Code, $Modifier, $Encounter)
204 $rowMoneyAdjusted = sqlFetchArray($resMoneyAdjusted);
205 $MoneyAdjusted = $rowMoneyAdjusted['MoneyAdjusted'];
207 $Remainder = $Fee - $Copay - $MoneyGot - $MoneyAdjusted;
209 $TotalRows = sqlNumRows($ResultSearchNew);
211 if ($Ins == 1) {
212 $bgcolor = '#ddddff';
213 } elseif ($Ins == 2) {
214 $bgcolor = '#ffdddd';
215 } elseif ($Ins == 3) {
216 $bgcolor = '#F2F1BC';
217 } elseif ($Ins == 0) {
218 $bgcolor = '#AAFFFF';
221 <tr class="text" bgcolor='<?php echo attr($bgcolor); ?>' id="trCharges<?php echo attr($CountIndex); ?>">
222 <td class="text-left">
223 <input name="HiddenIns<?php echo attr($CountIndex); ?>" id="HiddenIns<?php echo attr($CountIndex); ?>" value="<?php echo attr($Ins); ?>" type="hidden"/>
224 <?php echo generate_select_list("payment_ins$CountIndex", "payment_ins", "$Ins", "Insurance/Patient", '', 'oe-payment-select form-input-sm', 'ActionOnInsPat("' . $CountIndex . '")');?>
225 </td>
226 <td>
227 <?php echo text($ServiceDate); ?>
228 </td>
229 <td class="text-right">
230 <input name="HiddenEncounter<?php echo attr($CountIndex); ?>" value="<?php echo attr($Encounter); ?>" type="hidden" />
231 <?php echo text($Encounter); ?>
232 </td>
233 <td>
234 <input name="HiddenCodetype<?php echo attr($CountIndex); ?>" value="<?php echo attr($Codetype); ?>" type="hidden" />
235 <input name="HiddenCode<?php echo attr($CountIndex); ?>" value="<?php echo attr($Code); ?>" type="hidden" />
236 <?php echo text($Codetype . "-" . $Code . $ModifierString); ?>
237 <input name="HiddenModifier<?php echo attr($CountIndex); ?>" value="<?php echo attr($Modifier); ?>" type="hidden" />
238 </td>
239 <td class="text-right">
240 <input name="HiddenChargeAmount<?php echo attr($CountIndex); ?>" id="HiddenChargeAmount<?php echo attr($CountIndex); ?>" value="<?php echo attr($Fee); ?>" type="hidden"/>
241 <?php echo text($Fee); ?>
242 </td>
243 <td class="text-right">
244 <input name="HiddenCopayAmount<?php echo attr($CountIndex); ?>" id="HiddenCopayAmount<?php echo attr($CountIndex); ?>" value="<?php echo attr($Copay); ?>" type="hidden" />
245 <?php echo text(number_format($Copay, 2)); ?>
246 </td>
247 <td class="text-right" id="RemainderTd<?php echo attr($CountIndex); ?>">
248 <?php echo text(round($Remainder, 2)); ?>
249 </td>
250 <input name="HiddenRemainderTd<?php echo attr($CountIndex); ?>" id="HiddenRemainderTd<?php echo attr($CountIndex); ?>" value="<?php echo attr(round($Remainder, 2)); ?>" type="hidden" />
251 <td>
252 <input name="Allowed<?php echo attr($CountIndex); ?>" id="Allowed<?php echo attr($CountIndex); ?>" onKeyDown="PreventIt(event)" autocomplete="off" onChange="ValidateNumeric(this);ScreenAdjustment(this,<?php echo attr_js($CountIndex); ?>);UpdateTotalValues(<?php echo attr_js(($CountIndexAbove ?? null) * 1 + 1); ?>,<?php echo attr_js($TotalRows); ?>,'Allowed','initialallowtotal');UpdateTotalValues(<?php echo attr_js(($CountIndexAbove ?? null) * 1 + 1); ?>,<?php echo attr_js($TotalRows); ?>,'Payment','initialpaymenttotal');UpdateTotalValues(<?php echo attr_js(($CountIndexAbove ?? null) * 1 + 1); ?>,<?php echo attr_js($TotalRows); ?>,'AdjAmount','initialAdjAmounttotal');RestoreValues(<?php echo attr_js($CountIndex); ?>)" type="text" class="text-right amt_input" />
253 </td>
254 <td>
255 <input type="text" name="Payment<?php echo attr($CountIndex); ?>" onKeyDown="PreventIt(event)" autocomplete="off" id="Payment<?php echo attr($CountIndex); ?>" onChange="ValidateNumeric(this);ScreenAdjustment(this,<?php echo attr_js($CountIndex); ?>);UpdateTotalValues(<?php echo attr_js(($CountIndexAbove ?? null) * 1 + 1); ?>,<?php echo attr_js($TotalRows); ?>,'Payment','initialpaymenttotal');RestoreValues(<?php echo attr_js($CountIndex); ?>)" class="text-right amt_input" />
256 </td>
257 <td>
258 <input name="AdjAmount<?php echo attr($CountIndex); ?>" onKeyDown="PreventIt(event)" autocomplete="off" id="AdjAmount<?php echo attr($CountIndex); ?>" onChange="ValidateNumeric(this);ScreenAdjustment(this,<?php echo attr_js($CountIndex); ?>);UpdateTotalValues(<?php echo attr_js(($CountIndexAbove ?? null) * 1 + 1); ?>,<?php echo attr_js($TotalRows); ?>,'AdjAmount','initialAdjAmounttotal');RestoreValues(<?php echo attr_js($CountIndex); ?>)" type="text" class="text-right amt_input" />
259 </td>
260 <td>
261 <input name="Deductible<?php echo attr($CountIndex); ?>" id="Deductible<?php echo attr($CountIndex); ?>" onKeyDown="PreventIt(event)" onChange="ValidateNumeric(this);UpdateTotalValues(<?php echo attr_js(($CountIndexAbove ?? null) * 1 + 1); ?>,<?php echo attr_js($TotalRows); ?>,'Deductible','initialdeductibletotal');" autocomplete="off" type="text" class="text-right amt_input" />
262 </td>
263 <td>
264 <input name="Takeback<?php echo attr($CountIndex); ?>" onKeyDown="PreventIt(event)" autocomplete="off" id="Takeback<?php echo attr($CountIndex); ?>" onChange="ValidateNumeric(this);ScreenAdjustment(this,<?php echo attr_js($CountIndex); ?>);UpdateTotalValues(<?php echo attr_js(($CountIndexAbove ?? null) * 1 + 1); ?>,<?php echo attr_js($TotalRows); ?>,'Takeback','initialtakebacktotal');RestoreValues(<?php echo attr_js($CountIndex); ?>)" type="text" class="text-right amt_input" />
265 </td>
266 <td class="text-left">
267 <input name="HiddenReasonCode<?php echo attr($CountIndex); ?>" id="HiddenReasonCode<?php echo attr($CountIndex); ?>" value="<?php echo attr($ReasonCodeDB ?? ''); ?>" type="hidden" />
268 <?php echo generate_select_list("ReasonCode$CountIndex", "msp_remit_codes", "", "MSP Code", "--", "oe-payment-select"); ?>
269 </td>
270 <td class="text-center">
271 <input type="checkbox" id="FollowUp<?php echo attr($CountIndex); ?>" name="FollowUp<?php echo attr($CountIndex); ?>" value="y" onClick="ActionFollowUp(<?php echo attr_js($CountIndex); ?>)" />
272 </td>
273 <td>
274 <input name="FollowUpReason<?php echo attr($CountIndex); ?>" onKeyDown="PreventIt(event)" id="FollowUpReason<?php echo attr($CountIndex); ?>" readonly />
275 </td>
276 </tr>
277 <?php
278 } while ($RowSearch = sqlFetchArray($ResultSearchNew)); ?>
279 <tr class="text">
280 <td class="text-right text-dark text-left" colspan="7"><b><?php echo (xlt("Totals") . ": ") ?></b></td>
281 <td class="bg-dark text-secondary text-center" id="initialallowtotal">0</td>
282 <td class="bg-dark text-secondary text-center" id="initialpaymenttotal">0</td>
283 <td class="bg-dark text-secondary text-center" id="initialAdjAmounttotal" >0</td>
284 <td class="bg-dark text-secondary text-center" id="initialdeductibletotal">0</td>
285 <td class="bg-dark text-secondary text-center" id="initialtakebacktotal">0</td>
286 <td class="text-center">&nbsp;</td>
287 <td class="text-center">&nbsp;</td>
288 </tr>
289 </table>
290 </div>
291 </div>
292 <?php } //if($RowSearch = sqlFetchArray($ResultSearchNew))
293 } //if(!empty($ResultSearchNew)) ?>