Merge pull request #1128 from bradymiller/unique-insurance-report-cleanup_1
[openemr.git] / interface / billing / payment_pat_sel.inc.php
blobe369cbb90dd01288f3542f87ac31850a7ec76df5
1 <?php
2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2010 Z&H Consultancy Services Private Limited <sam@zhservices.com>
4 //
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
18 // A copy of the GNU General Public License is included along with this program:
19 // openemr/interface/login/GnuGPL.html
20 // For more information write to the Free Software
21 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 //
23 // Author: Eldho Chacko <eldho@zhservices.com>
24 // Paul Simon K <paul@zhservices.com>
26 // +------------------------------------------------------------------------------+
27 //===============================================================================
28 //Patient ajax section and listing of charges..Used in New Payment and Edit Payment screen.
29 //===============================================================================
30 if (isset($_POST["mode"])) {
31 if (($_POST["mode"] == "search" || $_POST["default_search_patient"] == "default_search_patient") && $_REQUEST['hidden_patient_code']*1>0) {
32 $hidden_patient_code=$_REQUEST['hidden_patient_code'];
33 $RadioPaid=$_REQUEST['RadioPaid'];
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 ";
42 $ResultSearchNew = sqlStatement("SELECT b.id,last_level_closed,b.encounter,fe.`date`,b.code_type,b.code,b.modifier,fee
43 FROM billing AS b,form_encounter AS fe, code_types AS ct
44 WHERE b.encounter=fe.encounter AND b.code_type=ct.ct_key AND ct.ct_diag=0
45 AND b.activity!=0 AND fe.pid ='$hidden_patient_code' AND b.pid ='$hidden_patient_code'
46 $StringForQuery ORDER BY fe.`date`, fe.encounter,b.code,b.modifier");
47 $res = sqlStatement("SELECT fname,lname,mname FROM patient_data
48 where pid ='".$_REQUEST['hidden_patient_code']."'");
49 $row = sqlFetchArray($res);
50 $fname=$row['fname'];
51 $lname=$row['lname'];
52 $mname=$row['mname'];
53 $NameNew=$lname.' '.$fname.' '.$mname;
57 //===============================================================================
59 <table width="1004" border="0" cellspacing="0" cellpadding="0" id="TablePatientPortion">
60 <tr height="5">
61 <td colspan="13" align="left" >
62 <table width="705" border="0" cellspacing="0" cellpadding="0" bgcolor="#DEDEDE">
63 <tr height="5">
64 <td class='title' width="700" ></td>
65 </tr>
66 <tr>
67 <td class='text'><table width="799" border="0" cellspacing="0" cellpadding="0" style="border:1px solid black" >
68 <tr>
69 <td width="45" align="left" class="text">&nbsp;<?php echo htmlspecialchars(xl('Patient'), ENT_QUOTES).':' ?>
70 </td>
71 <td width="265"><input type="hidden" id="hidden_ajax_patient_close_value" value="<?php echo $Message=='' ? htmlspecialchars($NameNew) : '' ;?>" />
72 <input name='patient_code' style="width:265px" id='patient_code' class="text" onKeyDown="PreventIt(event)"
73 value="<?php echo $Message=='' ? htmlspecialchars($NameNew) : '' ;?>" autocomplete="off" /></td> <!--onKeyUp="ajaxFunction(event,'patient','edit_payment.php');" -->
74 <td width="55" colspan="2" style="padding-left:5px;" ><div class="text" name="patient_name" id="patient_name"
75 style="border:1px solid black; ; padding-left:5px; width:55px; height:17px;"><?php echo $Message=='' ? htmlspecialchars(formData('hidden_patient_code')) : '' ;?></div>
76 </td>
77 <td width="84" class="text">&nbsp;<input type="radio" name="RadioPaid" onClick="SearchOnceMore()" <?php echo $_REQUEST['RadioPaid']=='Non_Paid'
78 || $_REQUEST['RadioPaid']=='' ? 'checked' : '' ; ?> value="Non_Paid" id="Non_Paid" /><?php echo htmlspecialchars(xl('Non Paid'), ENT_QUOTES) ?></td>
79 <td width="168" class="text"><input type="radio" name="RadioPaid" onClick="SearchOnceMore()"
80 <?php echo $_REQUEST['RadioPaid']=='Show_Primary_Complete' ? 'checked' : '' ; ?> value="Show_Primary_Complete"
81 id="Show_Primary_Complete" /><?php echo htmlspecialchars(xl('Show Primary Complete'), ENT_QUOTES) ?></td>
82 <td width="157" class="text"><input type="radio" name="RadioPaid" onClick="SearchOnceMore()"
83 <?php echo $_REQUEST['RadioPaid']=='Show_Paid' ? 'checked' : '' ; ?> value="Show_Paid" id="Show_Paid" /><?php echo htmlspecialchars(xl('Show All Transactions'), ENT_QUOTES) ?>
84 </td>
85 </tr>
86 <tr>
87 <td align="left" class="text"></td>
88 <td><div id='ajax_div_patient_section'>
89 <div id='ajax_div_patient_error'>
90 </div>
91 <div id="ajax_div_patient" style="display:none;"></div>
92 </div>
93 </div>
94 </td>
95 <td></td>
96 <td></td>
97 <td></td>
98 <td></td>
99 <td class="text"></td>
100 </tr>
101 </table> </td>
102 </tr>
103 </table>
105 </td>
106 </tr>
107 <tr>
108 <td colspan="13" align="left" >
110 <?php //New distribution section
111 //$CountIndex=0;
112 $CountIndexBelow=0;
113 $PreviousEncounter=0;
114 $PreviousPID=0;
115 if ($RowSearch = sqlFetchArray($ResultSearchNew)) {
117 <table width="1004" border="0" cellpadding="0" cellspacing="0" align="center" id="TableDistributePortion">
118 <tr class="text" height="10">
119 <td colspan="14"></td>
120 </tr>
121 <tr class="text" bgcolor="#dddddd">
122 <td width="55" class="left top" ><?php echo htmlspecialchars(xl('Post For'), ENT_QUOTES) ?></td>
123 <td width="80" class="left top" ><?php echo htmlspecialchars(xl('Service Date'), ENT_QUOTES) ?></td>
124 <td width="65" class="left top" ><?php echo htmlspecialchars(xl('Encounter'), ENT_QUOTES) ?></td>
125 <td width="70" class="left top" ><?php echo htmlspecialchars(xl('Service Code'), ENT_QUOTES) ?></td>
126 <td width="55" class="left top" ><?php echo htmlspecialchars(xl('Charge'), ENT_QUOTES) ?></td>
127 <td width="40" class="left top" ><?php echo htmlspecialchars(xl('Copay'), ENT_QUOTES) ?></td>
128 <td width="45" class="left top" ><?php echo htmlspecialchars(xl('Remdr'), ENT_QUOTES) ?></td>
129 <td width="60" class="left top" ><?php echo htmlspecialchars(xl('Allowed'), ENT_QUOTES) ?></td>
130 <td width="60" class="left top" ><?php echo htmlspecialchars(xl('Payment'), ENT_QUOTES) ?></td>
131 <td width="70" class="left top" ><?php echo htmlspecialchars(xl('Adj Amount'), ENT_QUOTES) ?></td>
132 <td width="60" class="left top" ><?php echo htmlspecialchars(xl('Deductible'), ENT_QUOTES) ?></td>
133 <td width="60" class="left top" ><?php echo htmlspecialchars(xl('Takeback'), ENT_QUOTES) ?></td>
134 <td width="60" class="left top" ><?php echo htmlspecialchars(xl('MSP Code'), ENT_QUOTES) ?></td>
135 <td width="60" class="left top" ><?php echo htmlspecialchars(xl('Follow Up'), ENT_QUOTES) ?></td>
136 <td width="209" class="left top right" ><?php echo htmlspecialchars(xl('Follow Up Reason'), ENT_QUOTES) ?></td>
137 </tr>
138 <?php
139 do {
140 $CountIndex++;
141 $CountIndexBelow++;
142 $Ins=0;
143 // Determine the next insurance level to be billed.
144 $ferow = sqlQuery("SELECT date, last_level_closed " .
145 "FROM form_encounter WHERE " .
146 "pid = '$hidden_patient_code' AND encounter = '".$RowSearch['encounter']."'");
147 $date_of_service = substr($ferow['date'], 0, 10);
148 $new_payer_type = 0 + $ferow['last_level_closed'];
149 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) || $new_payer_type == 0) {
150 ++$new_payer_type;
153 $new_payer_id = arGetPayerID($hidden_patient_code, $date_of_service, $new_payer_type);
155 if ($new_payer_id==0) {
156 $Ins=0;
157 } elseif ($new_payer_id>0) {
158 $Ins=$new_payer_type;
162 $ServiceDateArray=explode(' ', $RowSearch['date']);
163 $ServiceDate=oeFormatShortDate($ServiceDateArray[0]);
164 $Codetype=$RowSearch['code_type'];
165 $Code=$RowSearch['code'];
166 $Modifier =$RowSearch['modifier'];
167 if ($Modifier!='') {
168 $ModifierString=", $Modifier";
169 } else {
170 $ModifierString="";
173 $Fee=$RowSearch['fee'];
174 $Encounter=$RowSearch['encounter'];
176 //Always associating the copay to a particular charge.
177 $BillingId=$RowSearch['id'];
178 $resId = sqlStatement("SELECT b.id FROM billing AS b, code_types AS ct
179 WHERE b.code_type=ct.ct_key AND ct.ct_diag=0 AND
180 b.pid='$hidden_patient_code' AND b.encounter='$Encounter'
181 AND b.activity!=0 ORDER BY id");
182 $rowId = sqlFetchArray($resId);
183 $Id=$rowId['id'];
185 if ($BillingId!=$Id) {//multiple cpt in single encounter
186 $Copay=0.00;
187 } else {
188 $resCopay = sqlStatement("SELECT sum(fee) as copay FROM billing where code_type='COPAY' and
189 pid ='$hidden_patient_code' and encounter ='$Encounter' and billing.activity!=0");
190 $rowCopay = sqlFetchArray($resCopay);
191 $Copay=$rowCopay['copay']*-1;
193 $resMoneyGot = sqlStatement("SELECT sum(pay_amount) as PatientPay FROM ar_activity where
194 pid ='$hidden_patient_code' and encounter ='$Encounter' and payer_type=0 and
195 account_code='PCP'");//new fees screen copay gives account_code='PCP'
196 $rowMoneyGot = sqlFetchArray($resMoneyGot);
197 $PatientPay=$rowMoneyGot['PatientPay'];
199 $Copay=$Copay+$PatientPay;
202 //payer_type!=0, supports both mapped and unmapped code_type in ar_activity
203 $resMoneyGot = sqlStatement("SELECT sum(pay_amount) as MoneyGot FROM ar_activity where
204 pid ='$hidden_patient_code' and (code_type='$Codetype' or code_type='') and code='$Code' and modifier='$Modifier' and encounter ='$Encounter' and !(payer_type=0 and
205 account_code='PCP')");//new fees screen copay gives account_code='PCP'
206 $rowMoneyGot = sqlFetchArray($resMoneyGot);
207 $MoneyGot=$rowMoneyGot['MoneyGot'];
208 //supports both mapped and unmapped code_type in ar_activity
209 $resMoneyAdjusted = sqlStatement("SELECT sum(adj_amount) as MoneyAdjusted FROM ar_activity where
210 pid ='$hidden_patient_code' and (code_type='$Codetype' or code_type='') and code='$Code' and modifier='$Modifier' and encounter ='$Encounter'");
211 $rowMoneyAdjusted = sqlFetchArray($resMoneyAdjusted);
212 $MoneyAdjusted=$rowMoneyAdjusted['MoneyAdjusted'];
214 $Remainder=$Fee-$Copay-$MoneyGot-$MoneyAdjusted;
216 $TotalRows=sqlNumRows($ResultSearchNew);
217 if ($CountIndexBelow==sqlNumRows($ResultSearchNew)) {
218 $StringClass=' bottom left top ';
219 } else {
220 $StringClass=' left top ';
224 if ($Ins==1) {
225 $bgcolor='#ddddff';
226 } elseif ($Ins==2) {
227 $bgcolor='#ffdddd';
228 } elseif ($Ins==3) {
229 $bgcolor='#F2F1BC';
230 } elseif ($Ins==0) {
231 $bgcolor='#AAFFFF';
234 <tr class="text" bgcolor='<?php echo $bgcolor; ?>' id="trCharges<?php echo $CountIndex; ?>">
235 <td align="left" class="<?php echo $StringClass; ?>" ><input name="HiddenIns<?php echo $CountIndex; ?>" id="HiddenIns<?php echo $CountIndex; ?>"
236 value="<?php echo htmlspecialchars($Ins); ?>" type="hidden"/><?php echo generate_select_list("payment_ins$CountIndex", "payment_ins", "$Ins", "Insurance/Patient", '', '', 'ActionOnInsPat("'.$CountIndex.'")');?></td>
237 <td class="<?php echo $StringClass; ?>" ><?php echo htmlspecialchars($ServiceDate); ?></td>
238 <td align="right" class="<?php echo $StringClass; ?>" ><input name="HiddenEncounter<?php echo $CountIndex; ?>" value="<?php echo htmlspecialchars($Encounter); ?>"
239 type="hidden"/><?php echo htmlspecialchars($Encounter); ?></td>
240 <td class="<?php echo $StringClass; ?>" ><input name="HiddenCodetype<?php echo $CountIndex; ?>" value="<?php echo htmlspecialchars($Codetype); ?>" type="hidden"/><input name="HiddenCode<?php echo $CountIndex; ?>" value="<?php echo htmlspecialchars($Code); ?>"
241 type="hidden"/><?php echo htmlspecialchars($Codetype."-".$Code.$ModifierString); ?><input name="HiddenModifier<?php echo $CountIndex; ?>" value="<?php echo htmlspecialchars($Modifier); ?>"
242 type="hidden"/></td>
243 <td align="right" class="<?php echo $StringClass; ?>" ><input name="HiddenChargeAmount<?php echo $CountIndex; ?>"
244 id="HiddenChargeAmount<?php echo $CountIndex; ?>" value="<?php echo htmlspecialchars($Fee); ?>" type="hidden"/><?php echo htmlspecialchars($Fee); ?></td>
245 <td align="right" class="<?php echo $StringClass; ?>" ><input name="HiddenCopayAmount<?php echo $CountIndex; ?>"
246 id="HiddenCopayAmount<?php echo $CountIndex; ?>" value="<?php echo htmlspecialchars($Copay); ?>" type="hidden"/><?php echo htmlspecialchars(number_format($Copay, 2)); ?></td>
247 <td align="right" id="RemainderTd<?php echo $CountIndex; ?>" class="<?php echo $StringClass; ?>" ><?php echo htmlspecialchars(round($Remainder, 2)); ?></td>
248 <input name="HiddenRemainderTd<?php echo $CountIndex; ?>" id="HiddenRemainderTd<?php echo $CountIndex; ?>"
249 value="<?php echo htmlspecialchars(round($Remainder, 2)); ?>" type="hidden"/>
250 <td class="<?php echo $StringClass; ?>" ><input name="Allowed<?php echo $CountIndex; ?>" id="Allowed<?php echo $CountIndex; ?>"
251 onKeyDown="PreventIt(event)" autocomplete="off"
252 onChange="ValidateNumeric(this);ScreenAdjustment(this,<?php echo $CountIndex; ?>);UpdateTotalValues(<?php echo $CountIndexAbove*1+1; ?>,<?php echo $TotalRows; ?>,'Allowed','initialallowtotal');UpdateTotalValues(<?php echo $CountIndexAbove*1+1; ?>,<?php echo $TotalRows; ?>,'Payment','initialpaymenttotal');UpdateTotalValues(<?php echo $CountIndexAbove*1+1; ?>,<?php echo $TotalRows; ?>,'AdjAmount','initialAdjAmounttotal');RestoreValues(<?php echo $CountIndex; ?>)"
253 type="text" style="width:60px;text-align:right; font-size:12px" /></td>
254 <td class="<?php echo $StringClass; ?>" ><input type="text" name="Payment<?php echo $CountIndex; ?>"
255 onKeyDown="PreventIt(event)" autocomplete="off" id="Payment<?php echo $CountIndex; ?>"
256 onChange="ValidateNumeric(this);ScreenAdjustment(this,<?php echo $CountIndex; ?>);UpdateTotalValues(<?php echo $CountIndexAbove*1+1; ?>,<?php echo $TotalRows; ?>,'Payment','initialpaymenttotal');RestoreValues(<?php echo $CountIndex; ?>)"
257 style="width:60px;text-align:right; font-size:12px" /></td>
258 <td class="<?php echo $StringClass; ?>" ><input name="AdjAmount<?php echo $CountIndex; ?>" onKeyDown="PreventIt(event)"
259 autocomplete="off" id="AdjAmount<?php echo $CountIndex; ?>"
260 onChange="ValidateNumeric(this);ScreenAdjustment(this,<?php echo $CountIndex; ?>);UpdateTotalValues(<?php echo $CountIndexAbove*1+1; ?>,<?php echo $TotalRows; ?>,'AdjAmount','initialAdjAmounttotal');RestoreValues(<?php echo $CountIndex; ?>)"
261 type="text" style="width:70px;text-align:right; font-size:12px" /></td>
262 <td class="<?php echo $StringClass; ?>" ><input name="Deductible<?php echo $CountIndex; ?>" id="Deductible<?php echo $CountIndex; ?>"
263 onKeyDown="PreventIt(event)" onChange="ValidateNumeric(this);UpdateTotalValues(<?php echo $CountIndexAbove*1+1; ?>,<?php echo $TotalRows; ?>,'Deductible','initialdeductibletotal');" autocomplete="off" type="text"
264 style="width:60px;text-align:right; font-size:12px" /></td>
265 <td class="<?php echo $StringClass; ?>" ><input name="Takeback<?php echo $CountIndex; ?>" onKeyDown="PreventIt(event)" autocomplete="off"
266 id="Takeback<?php echo $CountIndex; ?>"
267 onChange="ValidateNumeric(this);ScreenAdjustment(this,<?php echo $CountIndex; ?>);UpdateTotalValues(<?php echo $CountIndexAbove*1+1; ?>,<?php echo $TotalRows; ?>,'Takeback','initialtakebacktotal');RestoreValues(<?php echo $CountIndex; ?>)"
268 type="text" style="width:60px;text-align:right; font-size:12px" /></td>
269 <td align="left" class="<?php echo $StringClass; ?>" ><input name="HiddenReasonCode<?php echo $CountIndex; ?>" id="HiddenReasonCode<?php echo $CountIndex; ?>" value="<?php echo htmlspecialchars($ReasonCodeDB); ?>" type="hidden"/><?php echo generate_select_list("ReasonCode$CountIndex", "msp_remit_codes", "", "MSP Code", "--"); ?></td>
270 <td align="center" class="<?php echo $StringClass; ?>" ><input type="checkbox" id="FollowUp<?php echo $CountIndex; ?>"
271 name="FollowUp<?php echo $CountIndex; ?>" value="y" onClick="ActionFollowUp(<?php echo $CountIndex; ?>)" /></td>
272 <td class="<?php echo $StringClass; ?> right" ><input onKeyDown="PreventIt(event)" id="FollowUpReason<?php echo $CountIndex; ?>"
273 name="FollowUpReason<?php echo $CountIndex; ?>" readonly="" type="text" style="width:209px;font-size:12px" /></td>
274 </tr>
275 <?php
276 } while ($RowSearch = sqlFetchArray($ResultSearchNew));
278 <tr class="text">
279 <td align="left" colspan="7">&nbsp;</td>
280 <td class="left bottom" bgcolor="#6699FF" id="initialallowtotal" align="right" >0</td>
281 <td class="left bottom" bgcolor="#6699FF" id="initialpaymenttotal" align="right" >0</td>
282 <td class="left bottom" bgcolor="#6699FF" id="initialAdjAmounttotal" align="right" >0</td>
283 <td class="left bottom" bgcolor="#6699FF" id="initialdeductibletotal" align="right">0</td>
284 <td class="left bottom right" bgcolor="#6699FF" id="initialtakebacktotal" align="right">0</td>
285 <td align="center">&nbsp;</td>
286 <td align="center">&nbsp;</td>
287 </tr>
288 </table>
289 <?php
290 }//if($RowSearch = sqlFetchArray($ResultSearchNew))
292 </td>
293 </tr>
294 </table>