changes to prior commit
[openemr.git] / interface / billing / payment_pat_sel.inc.php
blobd4476c89c765a409dd71695b80f061626b159c34
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"]))
32 if (($_POST["mode"] == "search" || $_POST["default_search_patient"] == "default_search_patient") && $_REQUEST['hidden_patient_code']*1>0)
34 $hidden_patient_code=$_REQUEST['hidden_patient_code'];
35 $RadioPaid=$_REQUEST['RadioPaid'];
36 if($RadioPaid=='Show_Paid')
38 $StringForQuery='';
40 elseif($RadioPaid=='Non_Paid')
42 $StringForQuery=" and last_level_closed = 0 ";
44 elseif($RadioPaid=='Show_Primary_Complete')
46 $StringForQuery=" and last_level_closed >= 1 ";
48 $ResultSearchNew = sqlStatement("SELECT b.id,last_level_closed,b.encounter,fe.`date`,b.code_type,b.code,b.modifier,fee
49 FROM billing AS b,form_encounter AS fe, code_types AS ct
50 WHERE b.encounter=fe.encounter AND b.code_type=ct.ct_key AND ct.ct_diag=0
51 AND b.activity!=0 AND fe.pid ='$hidden_patient_code' AND b.pid ='$hidden_patient_code'
52 $StringForQuery ORDER BY fe.`date`, fe.encounter,b.code,b.modifier");
53 $res = sqlStatement("SELECT fname,lname,mname FROM patient_data
54 where pid ='".$_REQUEST['hidden_patient_code']."'");
55 $row = sqlFetchArray($res);
56 $fname=$row['fname'];
57 $lname=$row['lname'];
58 $mname=$row['mname'];
59 $NameNew=$lname.' '.$fname.' '.$mname;
62 //===============================================================================
64 <table width="1004" border="0" cellspacing="0" cellpadding="0" id="TablePatientPortion">
65 <tr height="5">
66 <td colspan="13" align="left" >
67 <table width="705" border="0" cellspacing="0" cellpadding="0" bgcolor="#DEDEDE">
68 <tr height="5">
69 <td class='title' width="700" ></td>
70 </tr>
71 <tr>
72 <td class='text'><table width="799" border="0" cellspacing="0" cellpadding="0" style="border:1px solid black" >
73 <tr>
74 <td width="45" align="left" class="text">&nbsp;<?php echo htmlspecialchars( xl('Patient'), ENT_QUOTES).':' ?>
75 </td>
76 <td width="265"><input type="hidden" id="hidden_ajax_patient_close_value" value="<?php echo $Message=='' ? htmlspecialchars($NameNew) : '' ;?>" />
77 <input name='patient_code' style="width:265px" id='patient_code' class="text" onKeyDown="PreventIt(event)"
78 value="<?php echo $Message=='' ? htmlspecialchars($NameNew) : '' ;?>" autocomplete="off" /></td> <!--onKeyUp="ajaxFunction(event,'patient','edit_payment.php');" -->
79 <td width="55" colspan="2" style="padding-left:5px;" ><div class="text" name="patient_name" id="patient_name"
80 style="border:1px solid black; ; padding-left:5px; width:55px; height:17px;"><?php echo $Message=='' ? htmlspecialchars(formData('hidden_patient_code')) : '' ;?></div>
81 </td>
82 <td width="84" class="text">&nbsp;<input type="radio" name="RadioPaid" onClick="SearchOnceMore()" <?php echo $_REQUEST['RadioPaid']=='Non_Paid'
83 || $_REQUEST['RadioPaid']=='' ? 'checked' : '' ; ?> value="Non_Paid" id="Non_Paid" /><?php echo htmlspecialchars( xl('Non Paid'), ENT_QUOTES) ?></td>
84 <td width="168" class="text"><input type="radio" name="RadioPaid" onClick="SearchOnceMore()"
85 <?php echo $_REQUEST['RadioPaid']=='Show_Primary_Complete' ? 'checked' : '' ; ?> value="Show_Primary_Complete"
86 id="Show_Primary_Complete" /><?php echo htmlspecialchars( xl('Show Primary Complete'), ENT_QUOTES) ?></td>
87 <td width="157" class="text"><input type="radio" name="RadioPaid" onClick="SearchOnceMore()"
88 <?php echo $_REQUEST['RadioPaid']=='Show_Paid' ? 'checked' : '' ; ?> value="Show_Paid" id="Show_Paid" /><?php echo htmlspecialchars( xl('Show All Transactions'), ENT_QUOTES) ?>
89 </td>
90 </tr>
91 <tr>
92 <td align="left" class="text"></td>
93 <td><div id='ajax_div_patient_section'>
94 <div id='ajax_div_patient_error'>
95 </div>
96 <div id="ajax_div_patient" style="display:none;"></div>
97 </div>
98 </div>
99 </td>
100 <td></td>
101 <td></td>
102 <td></td>
103 <td></td>
104 <td class="text"></td>
105 </tr>
106 </table> </td>
107 </tr>
108 </table>
110 </td>
111 </tr>
112 <tr>
113 <td colspan="13" align="left" >
115 <?php //New distribution section
116 //$CountIndex=0;
117 $CountIndexBelow=0;
118 $PreviousEncounter=0;
119 $PreviousPID=0;
120 if($RowSearch = sqlFetchArray($ResultSearchNew))
123 <table width="1004" border="0" cellpadding="0" cellspacing="0" align="center" id="TableDistributePortion">
124 <tr class="text" height="10">
125 <td colspan="14"></td>
126 </tr>
127 <tr class="text" bgcolor="#dddddd">
128 <td width="55" class="left top" ><?php echo htmlspecialchars( xl('Post For'), ENT_QUOTES) ?></td>
129 <td width="80" class="left top" ><?php echo htmlspecialchars( xl('Service Date'), ENT_QUOTES) ?></td>
130 <td width="65" class="left top" ><?php echo htmlspecialchars( xl('Encounter'), ENT_QUOTES) ?></td>
131 <td width="70" class="left top" ><?php echo htmlspecialchars( xl('Service Code'), ENT_QUOTES) ?></td>
132 <td width="55" class="left top" ><?php echo htmlspecialchars( xl('Charge'), ENT_QUOTES) ?></td>
133 <td width="40" class="left top" ><?php echo htmlspecialchars( xl('Copay'), ENT_QUOTES) ?></td>
134 <td width="45" class="left top" ><?php echo htmlspecialchars( xl('Remdr'), ENT_QUOTES) ?></td>
135 <td width="60" class="left top" ><?php echo htmlspecialchars( xl('Allowed'), ENT_QUOTES) ?></td>
136 <td width="60" class="left top" ><?php echo htmlspecialchars( xl('Payment'), ENT_QUOTES) ?></td>
137 <td width="70" class="left top" ><?php echo htmlspecialchars( xl('Adj Amount'), ENT_QUOTES) ?></td>
138 <td width="60" class="left top" ><?php echo htmlspecialchars( xl('Deductible'), ENT_QUOTES) ?></td>
139 <td width="60" class="left top" ><?php echo htmlspecialchars( xl('Takeback'), ENT_QUOTES) ?></td>
140 <td width="60" class="left top" ><?php echo htmlspecialchars( xl('MSP Code'), ENT_QUOTES) ?></td>
141 <td width="60" class="left top" ><?php echo htmlspecialchars( xl('Follow Up'), ENT_QUOTES) ?></td>
142 <td width="209" class="left top right" ><?php echo htmlspecialchars( xl('Follow Up Reason'), ENT_QUOTES) ?></td>
143 </tr>
144 <?php
148 $CountIndex++;
149 $CountIndexBelow++;
150 $Ins=0;
151 // Determine the next insurance level to be billed.
152 $ferow = sqlQuery("SELECT date, last_level_closed " .
153 "FROM form_encounter WHERE " .
154 "pid = '$hidden_patient_code' AND encounter = '".$RowSearch['encounter']."'");
155 $date_of_service = substr($ferow['date'], 0, 10);
156 $new_payer_type = 0 + $ferow['last_level_closed'];
157 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) || $new_payer_type == 0)
158 ++$new_payer_type;
159 $new_payer_id = arGetPayerID($hidden_patient_code, $date_of_service, $new_payer_type);
161 if($new_payer_id==0)
163 $Ins=0;
165 elseif($new_payer_id>0)
167 $Ins=$new_payer_type;
171 $ServiceDateArray=explode(' ',$RowSearch['date']);
172 $ServiceDate=oeFormatShortDate($ServiceDateArray[0]);
173 $Codetype=$RowSearch['code_type'];
174 $Code=$RowSearch['code'];
175 $Modifier =$RowSearch['modifier'];
176 if($Modifier!='')
177 $ModifierString=", $Modifier";
178 else
179 $ModifierString="";
180 $Fee=$RowSearch['fee'];
181 $Encounter=$RowSearch['encounter'];
183 //Always associating the copay to a particular charge.
184 $BillingId=$RowSearch['id'];
185 $resId = sqlStatement("SELECT b.id FROM billing AS b, code_types AS ct
186 WHERE b.code_type=ct.ct_key AND ct.ct_diag=0 AND
187 b.pid='$hidden_patient_code' AND b.encounter='$Encounter'
188 AND b.activity!=0 ORDER BY id");
189 $rowId = sqlFetchArray($resId);
190 $Id=$rowId['id'];
192 if($BillingId!=$Id)//multiple cpt in single encounter
194 $Copay=0.00;
196 else
198 $resCopay = sqlStatement("SELECT sum(fee) as copay FROM billing where code_type='COPAY' and
199 pid ='$hidden_patient_code' and encounter ='$Encounter' and billing.activity!=0");
200 $rowCopay = sqlFetchArray($resCopay);
201 $Copay=$rowCopay['copay']*-1;
203 $resMoneyGot = sqlStatement("SELECT sum(pay_amount) as PatientPay FROM ar_activity where
204 pid ='$hidden_patient_code' 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 $PatientPay=$rowMoneyGot['PatientPay'];
209 $Copay=$Copay+$PatientPay;
211 //payer_type!=0, supports both mapped and unmapped code_type in ar_activity
212 $resMoneyGot = sqlStatement("SELECT sum(pay_amount) as MoneyGot FROM ar_activity where
213 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
214 account_code='PCP')");//new fees screen copay gives account_code='PCP'
215 $rowMoneyGot = sqlFetchArray($resMoneyGot);
216 $MoneyGot=$rowMoneyGot['MoneyGot'];
217 //supports both mapped and unmapped code_type in ar_activity
218 $resMoneyAdjusted = sqlStatement("SELECT sum(adj_amount) as MoneyAdjusted FROM ar_activity where
219 pid ='$hidden_patient_code' and (code_type='$Codetype' or code_type='') and code='$Code' and modifier='$Modifier' and encounter ='$Encounter'");
220 $rowMoneyAdjusted = sqlFetchArray($resMoneyAdjusted);
221 $MoneyAdjusted=$rowMoneyAdjusted['MoneyAdjusted'];
223 $Remainder=$Fee-$Copay-$MoneyGot-$MoneyAdjusted;
225 $TotalRows=sqlNumRows($ResultSearchNew);
226 if($CountIndexBelow==sqlNumRows($ResultSearchNew))
228 $StringClass=' bottom left top ';
230 else
232 $StringClass=' left top ';
236 if($Ins==1)
238 $bgcolor='#ddddff';
240 elseif($Ins==2)
242 $bgcolor='#ffdddd';
244 elseif($Ins==3)
246 $bgcolor='#F2F1BC';
248 elseif($Ins==0)
250 $bgcolor='#AAFFFF';
253 <tr class="text" bgcolor='<?php echo $bgcolor; ?>' id="trCharges<?php echo $CountIndex; ?>">
254 <td align="left" class="<?php echo $StringClass; ?>" ><input name="HiddenIns<?php echo $CountIndex; ?>" id="HiddenIns<?php echo $CountIndex; ?>"
255 value="<?php echo htmlspecialchars($Ins); ?>" type="hidden"/><?php echo generate_select_list("payment_ins$CountIndex", "payment_ins", "$Ins", "Insurance/Patient",'','','ActionOnInsPat("'.$CountIndex.'")');?></td>
256 <td class="<?php echo $StringClass; ?>" ><?php echo htmlspecialchars($ServiceDate); ?></td>
257 <td align="right" class="<?php echo $StringClass; ?>" ><input name="HiddenEncounter<?php echo $CountIndex; ?>" value="<?php echo htmlspecialchars($Encounter); ?>"
258 type="hidden"/><?php echo htmlspecialchars($Encounter); ?></td>
259 <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); ?>"
260 type="hidden"/><?php echo htmlspecialchars($Codetype."-".$Code.$ModifierString); ?><input name="HiddenModifier<?php echo $CountIndex; ?>" value="<?php echo htmlspecialchars($Modifier); ?>"
261 type="hidden"/></td>
262 <td align="right" class="<?php echo $StringClass; ?>" ><input name="HiddenChargeAmount<?php echo $CountIndex; ?>"
263 id="HiddenChargeAmount<?php echo $CountIndex; ?>" value="<?php echo htmlspecialchars($Fee); ?>" type="hidden"/><?php echo htmlspecialchars($Fee); ?></td>
264 <td align="right" class="<?php echo $StringClass; ?>" ><input name="HiddenCopayAmount<?php echo $CountIndex; ?>"
265 id="HiddenCopayAmount<?php echo $CountIndex; ?>" value="<?php echo htmlspecialchars($Copay); ?>" type="hidden"/><?php echo htmlspecialchars(number_format($Copay,2)); ?></td>
266 <td align="right" id="RemainderTd<?php echo $CountIndex; ?>" class="<?php echo $StringClass; ?>" ><?php echo htmlspecialchars(round($Remainder,2)); ?></td>
267 <input name="HiddenRemainderTd<?php echo $CountIndex; ?>" id="HiddenRemainderTd<?php echo $CountIndex; ?>"
268 value="<?php echo htmlspecialchars(round($Remainder,2)); ?>" type="hidden"/>
269 <td class="<?php echo $StringClass; ?>" ><input name="Allowed<?php echo $CountIndex; ?>" id="Allowed<?php echo $CountIndex; ?>"
270 onKeyDown="PreventIt(event)" autocomplete="off"
271 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; ?>)"
272 type="text" style="width:60px;text-align:right; font-size:12px" /></td>
273 <td class="<?php echo $StringClass; ?>" ><input type="text" name="Payment<?php echo $CountIndex; ?>"
274 onKeyDown="PreventIt(event)" autocomplete="off" id="Payment<?php echo $CountIndex; ?>"
275 onChange="ValidateNumeric(this);ScreenAdjustment(this,<?php echo $CountIndex; ?>);UpdateTotalValues(<?php echo $CountIndexAbove*1+1; ?>,<?php echo $TotalRows; ?>,'Payment','initialpaymenttotal');RestoreValues(<?php echo $CountIndex; ?>)"
276 style="width:60px;text-align:right; font-size:12px" /></td>
277 <td class="<?php echo $StringClass; ?>" ><input name="AdjAmount<?php echo $CountIndex; ?>" onKeyDown="PreventIt(event)"
278 autocomplete="off" id="AdjAmount<?php echo $CountIndex; ?>"
279 onChange="ValidateNumeric(this);ScreenAdjustment(this,<?php echo $CountIndex; ?>);UpdateTotalValues(<?php echo $CountIndexAbove*1+1; ?>,<?php echo $TotalRows; ?>,'AdjAmount','initialAdjAmounttotal');RestoreValues(<?php echo $CountIndex; ?>)"
280 type="text" style="width:70px;text-align:right; font-size:12px" /></td>
281 <td class="<?php echo $StringClass; ?>" ><input name="Deductible<?php echo $CountIndex; ?>" id="Deductible<?php echo $CountIndex; ?>"
282 onKeyDown="PreventIt(event)" onChange="ValidateNumeric(this);UpdateTotalValues(<?php echo $CountIndexAbove*1+1; ?>,<?php echo $TotalRows; ?>,'Deductible','initialdeductibletotal');" autocomplete="off" type="text"
283 style="width:60px;text-align:right; font-size:12px" /></td>
284 <td class="<?php echo $StringClass; ?>" ><input name="Takeback<?php echo $CountIndex; ?>" onKeyDown="PreventIt(event)" autocomplete="off"
285 id="Takeback<?php echo $CountIndex; ?>"
286 onChange="ValidateNumeric(this);ScreenAdjustment(this,<?php echo $CountIndex; ?>);UpdateTotalValues(<?php echo $CountIndexAbove*1+1; ?>,<?php echo $TotalRows; ?>,'Takeback','initialtakebacktotal');RestoreValues(<?php echo $CountIndex; ?>)"
287 type="text" style="width:60px;text-align:right; font-size:12px" /></td>
288 <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>
289 <td align="center" class="<?php echo $StringClass; ?>" ><input type="checkbox" id="FollowUp<?php echo $CountIndex; ?>"
290 name="FollowUp<?php echo $CountIndex; ?>" value="y" onClick="ActionFollowUp(<?php echo $CountIndex; ?>)" /></td>
291 <td class="<?php echo $StringClass; ?> right" ><input onKeyDown="PreventIt(event)" id="FollowUpReason<?php echo $CountIndex; ?>"
292 name="FollowUpReason<?php echo $CountIndex; ?>" readonly="" type="text" style="width:209px;font-size:12px" /></td>
293 </tr>
294 <?php
296 }while($RowSearch = sqlFetchArray($ResultSearchNew));
298 <tr class="text">
299 <td align="left" colspan="7">&nbsp;</td>
300 <td class="left bottom" bgcolor="#6699FF" id="initialallowtotal" align="right" >0</td>
301 <td class="left bottom" bgcolor="#6699FF" id="initialpaymenttotal" align="right" >0</td>
302 <td class="left bottom" bgcolor="#6699FF" id="initialAdjAmounttotal" align="right" >0</td>
303 <td class="left bottom" bgcolor="#6699FF" id="initialdeductibletotal" align="right">0</td>
304 <td class="left bottom right" bgcolor="#6699FF" id="initialtakebacktotal" align="right">0</td>
305 <td align="center">&nbsp;</td>
306 <td align="center">&nbsp;</td>
307 </tr>
308 </table>
309 <?php
310 }//if($RowSearch = sqlFetchArray($ResultSearchNew))
312 </td>
313 </tr>
314 </table>