sql-injection fix in demographics
[openemr.git] / library / report.inc
blob7d903a34784b4c6a24b149274a6f5899d858ff35
1 <?php
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 require_once("{$GLOBALS['srcdir']}/sql.inc");
8 require_once("{$GLOBALS['srcdir']}/acl.inc");
9 require_once("{$GLOBALS['srcdir']}/formatting.inc.php");
10 require_once("{$GLOBALS['srcdir']}/formdata.inc.php");
12 $patient_data_array = array(
13 title => xl('Title') . ": ",
14 fname => xl('First Name') . ": ",
15 mname => xl('Middle Name') . ": ",
16 lname => xl('Last Name') . ": ",
17 sex => xl('Sex') . ": ",
18 ss => xl('SS') . ": ",
19 DOB => xl('Date of Birth') . ": ",
20 street => xl('Street') . ": ",
21 city => xl('City') . ": ",
22 state => xl('State') . ": ",
23 postal_code => xl('Zip') . ": ",
24 country_code => xl('Country') . ": ",
25 occupation => xl('Occupation') . ": ",
26 phone_home => xl('Home Phone') . ": ",
27 phone_biz => xl('Business Phone') . ": ",
28 phone_contact => xl('Contact Phone') . ": ",
29 contact_relationship => xl('Contact Person') . ": ",
30 hipaa_mail => xl('Allows Mail') . ": ",
31 hipaa_voice => xl('Allows Voice msgs') . ": ",
32 hipaa_notice => xl('Notice Received') . ": ",
33 hipaa_message => xl('Leave Message With') . ": "
36 $history_data_array = array(
37 coffee => xl('Coffee Use') . ": ",
38 tobacco => xl('Tobacco Use') . ": ",
39 alcohol => xl('Alcohol Use') . ": ",
40 sleep_patterns => xl('Sleep Patterns') . ": ",
41 exercise_patterns => xl('Exercise Patterns') . ": ",
42 seatbelt_use => xl('Seatbelt Use') . ": ",
43 counseling => xl('Counseling') . ": ",
44 hazardous_activities => xl('Hazardous Activities') . ": ",
45 last_breast_exam => xl('Last Breast Exam') . ": ",
46 last_mammogram => xl('Last Mammogram') . ": ",
47 last_gynocological_exam => xl('Last Gyn. Exam') . ": ",
48 last_rectal_exam => xl('Last Rectal Exam') . ": ",
49 last_prostate_exam => xl('Last Prostate Exam') . ": ",
50 last_physical_exam => xl('Last Physical Exam') . ": ",
51 last_sigmoidoscopy_colonoscopy => xl('Last Sigmoid/Colonoscopy') . ": ",
52 cataract_surgery => xl('Last Cataract Surgery') . ": ",
53 tonsillectomy => xl('Last Tonsillectomy') . ": ",
54 cholecystestomy => xl('Last Cholecystestomy') . ": ",
55 heart_surgery => xl('Last Heart Surgery') . ": ",
56 hysterectomy => xl('Last Hysterectomy') . ": ",
57 hernia_repair => xl('Last Hernia Repair') . ": ",
58 hip_replacement => xl('Last Hip Replacement') . ": ",
59 knee_replacement => xl('Last Knee Replacement') . ": ",
60 appendectomy => xl('Last Appendectomy') . ": ",
61 history_mother => xl('Mothers History') . ": ",
62 history_father => xl('Fathers History') . ": ",
63 history_siblings => xl('Sibling History') . ": ",
64 history_offspring => xl('Offspring History') . ": ",
65 history_spouse => xl('Spouses History') . ": ",
66 relatives_cancer => xl('Relatives Cancer') . ": ",
67 relatives_tuberculosis => xl('Relatives Tuberculosis') . ": ",
68 relatives_diabetes => xl('Relatives Diabetes') . ": ",
69 relatives_high_blood_pressure => xl('Relatives Blood Pressure') . ": ",
70 relatives_heart_problems => xl('Relatives Heart') . ": ",
71 relatives_stroke => xl('Relatives Stroke') . ": ",
72 relatives_epilepsy => xl('Relatives Epilepsy') . ": ",
73 relatives_mental_illness => xl('Relatives Mental Illness') . ": ",
74 relatives_suicide => xl('Relatives Suicide') . ": "
77 $employer_data_array = array(
78 name => xl('Employer') . ": ",
79 street => xl('Address') . ": ",
80 city => xl('City') . ": ",
81 postal_code => xl('Zip') . ": ",
82 state => xl('State') . ": ",
83 country => xl('Country') . ": "
86 $insurance_data_array = array(
87 provider_name => xl('Provider') . ": ",
88 plan_name => xl('Plan Name') . ": ",
89 policy_number => xl('Policy Number') . ": ",
90 group_number => xl('Group Number') . ": ",
91 subscriber_fname => xl('Subscriber First Name') . ": ",
92 subscriber_mname => xl('Subscriber Middle Name') . ": ",
93 subscriber_lname => xl('Subscriber Last Name') . ": ",
94 subscriber_relationship => xl('Subscriber Relationship') . ": ",
95 subscriber_ss => xl('Subscriber SS') . ": ",
96 subscriber_DOB => xl('Subscriber Date of Birth') . ": ",
97 subscriber_phone => xl('Subscribter Phone') . ": ",
98 subscriber_street => xl('Subscriber Address') . ": ",
99 subscriber_postal_code => xl('Subscriber Zip') . ": ",
100 subscriber_city => xl('Subscriber City') . ": ",
101 subscriber_state => xl('Subscriber State') . ": ",
102 subscriber_country => xl('Subscriber Country') . ": ",
103 subscriber_employer => xl('Subscriber Employer') . ": ",
104 subscriber_employer_street => xl('Subscriber Employer Street') . ": ",
105 subscriber_employer_city => xl('Subscriber Employer City') . ": ",
106 subscriber_employer_postal_code => xl('Subscriber Employer Zip') . ": ",
107 subscriber_employer_state => xl('Subscriber Employer State') . ": ",
108 subscriber_employer_country => xl('Subscriber Employer Country') . ": "
111 function getPatientReport($pid)
113         $sql = "select * from patient_data where pid='$pid' order by date ASC";
114         $res = sqlStatement("$sql");
115         while($list = sqlFetchArray($res))
116         {
117                 while(list($key, $value) = each($list))
118                 {
119                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
120                         {
121                                 $ret[$key]['title'] = $key;
122                                 $ret[$key]['content'] = $value;
123                                 $ret[$key]['date'] = $list['date'];
124                         }
125                 }
126         }
127         return $ret;
130 function getHistoryReport($pid)
132         $sql = "select * from history_data where pid='$pid' order by date ASC";
133         $res = sqlStatement("$sql");
134         while($list = sqlFetchArray($res))
135         {
136                 while(list($key, $value) = each($list))
137                 {
138                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
139                         {
140                                 $ret[$key]['content'] = $value;
141                                 $ret[$key]['date'] = $list['date'];
142                         }
143                 }
144         }
145         return $ret;
148 function getInsuranceReport($pid, $type = "primary")
150         $sql = "select * from insurance_data where pid='$pid' and type='$type' order by date ASC";
151         $res = sqlStatement("$sql");
152         while($list = sqlFetchArray($res))
153         {
154                 while(list($key, $value) = each($list))
155                 {
156                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
157                         {
158                                 $ret[$key]['content'] = $value;
159                                 $ret[$key]['date'] = $list['date'];
160                         }
161                 }
162         }
163         return $ret;
166 function getEmployerReport($pid)
168         $sql = "select * from employer_data where pid='$pid' order by date ASC";
169         $res = sqlStatement("$sql");
170         while($list = sqlFetchArray($res))
171         {
172                 while(list($key, $value) = each($list))
173                 {
174                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
175                         {
176                                 $ret[$key]['content'] = $value;
177                                 $ret[$key]['date'] = $list['date'];
178                         }
179                 }
180         }
181         return $ret;
184 function getListsReport($pid)
186         $sql = "select * from lists where id='$id' order by date ASC";
187         $res = sqlStatement("$sql");
188         while($list = sqlFetchArray($res))
189         {
190                 while(list($key, $value) = each($list))
191                 {
192                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
193                         {
194                                 $ret[$key]['content'] = $value;
195                                 $ret[$key]['date'] = $list['date'];
196                         }
197                 }
198         }
199         return $ret;
202 function printListData($pid, $list_type, $list_activity = "%") {
203         $res = sqlStatement("select * from lists where pid='$pid' and type='$list_type' and activity like '$list_activity' order by date");
204         while($result = sqlFetchArray($res)) {
205                 print "<span class=bold>" . $result{"title"} . ":</span><span class=text> " . $result{"comments"} . "</span><br>\n";
206         }
209 function printPatientNotes($pid) {
210   // exclude ALL deleted notes
211   $res = sqlStatement("select * from pnotes where pid = '$pid' and deleted != 1 and activity = 1 order by date");
212   while($result = sqlFetchArray($res)) {
213     print "<span class=bold>" . oeFormatSDFT(strtotime($result{"date"})) .
214       ":</span><span class=text> " .
215       stripslashes(oeFormatPatientNote($result['body'])) . "</span><br>\n";
216   }
219 function printPatientTransactions($pid) {
220         $res = sqlStatement("select * from transactions where pid=? order by date", array($pid) );
221         while($result = sqlFetchArray($res)) {
222                 print "<span class=bold>" .
223                 htmlspecialchars( oeFormatSDFT(strtotime($result{"date"})), ENT_NOQUOTES) .
224                 ":</span><span class=text>(" .
225                 generate_display_field(array('data_type'=>'1','list_id'=>'transactions'), $result{"title"}) .
226                 ") " . htmlspecialchars( $result{"body"}, ENT_NOQUOTES) .
227                 "</span><br>\n";
228         }
231 function printPatientBilling($pid) {
232   $res = sqlStatement("select * from billing where pid='$pid' order by date");
233   while($result = sqlFetchArray($res)) {
234     echo "<span class=bold>" . oeFormatSDFT(strtotime($result{"date"})) . " : </span>";
235     echo "<span class=text>(".$result{"code_type"}.") ";
236     echo $result['code_type'] == 'COPAY' ? oeFormatMoney($result['code']) : $result['code'];
237     echo " - ". $result['code_text']."</span>";
238     echo "<br>\n";
239         }
242 function getPatientBillingEncounter($pid, $encounter) {
243   /*******************************************************************
244         $sql = "SELECT billing.*, u.id, u.fname, u.mname, u.lname, " .
245     "CONCAT(u.fname,' ', u.lname) AS provider_name, u.federaltaxid " .
246     "FROM billing LEFT JOIN users AS u ON u.id = billing.provider_id " .
247     "WHERE pid='" . mysql_real_escape_string($pid) . "' AND " .
248     "encounter = '" . mysql_real_escape_string($encounter) .
249     "' AND activity='1' ORDER BY date";
250   *******************************************************************/
251   $erow = sqlQuery("SELECT provider_id FROM form_encounter WHERE " .
252     "pid = '$pid' AND encounter = '$encounter' " .
253     "ORDER BY id DESC LIMIT 1");
254   $inv_provider = $erow['provider_id'] + 0;
255         $sql = "SELECT b.*, u.id, u.fname, u.mname, u.lname, " .
256     "CONCAT(u.fname,' ', u.lname) AS provider_name, u.federaltaxid " .
257     "FROM billing AS b " .
258     "LEFT JOIN users AS u ON " .
259     "( b.provider_id != 0 AND u.id = b.provider_id ) OR " .
260     "( b.provider_id  = 0 AND u.id = $inv_provider ) " .
261     "WHERE pid='" . mysql_real_escape_string($pid) . "' AND " .
262     "encounter = '" . mysql_real_escape_string($encounter) .
263     "' AND activity = '1' ORDER BY date";
264   /******************************************************************/
265         $res = sqlStatement($sql);
266         $billings = array();
267         while($result = sqlFetchArray($res)) {
268                 $billings[] = $result;
269         }
270         return $billings;
273 function printPatientForms($pid, $cols) {
274     //this function takes a $pid
275     $inclookupres = sqlStatement("select distinct formdir from forms where pid='$pid' AND deleted=0");
276     while($result = sqlFetchArray($inclookupres)) {
277         include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
278     }
279         
280     $res = sqlStatement("select * from forms where pid='$pid' AND deleted=0 order by date");
281     while($result = sqlFetchArray($res)) {
282         if ($result{"form_name"} == "New Patient Encounter") {
283             echo "<div class='text encounter'>\n";
284             echo "<h1>" . $result["form_name"] . "</h1>";
286             // display the provider info
287             $tmp = sqlQuery("SELECT u.title, u.fname, u.mname, u.lname " .
288                                     "FROM forms AS f, users AS u WHERE " .
289                                     "f.pid = '$pid' AND f.encounter = ".$result['encounter']." AND " .
290                                     "f.formdir = 'newpatient' AND u.username = f.user " .
291                                     " AND f.deleted=0 ". //--JRM--
292                                     "ORDER BY f.id LIMIT 1");
293             echo " ".xl('Provider').": ".$tmp['title']." ".
294                 $tmp['fname']." ".$tmp['mname']." ".$tmp['lname'];
295             echo "<br/>";
296         }
297         else {
298             echo "<div class='text encounter_form'>";
299             echo "<h1>" . $result["form_name"] . "</h1>";
300         }
301         echo "(" . oeFormatSDFT(strtotime($result["date"])) . ") ";
303         if (acl_check('acct', 'rep') || acl_check('acct', 'eob') || acl_check('acct', 'bill')) {
304             if ($result{"form_name"} == "New Patient Encounter") {
305                 // display billing info
306                 echo "<br/>";
307                 $bres = sqlStatement("SELECT b.date, b.code, b.code_text " .
308                   "FROM billing AS b, code_types AS ct WHERE " .
309                   "b.pid = ? AND " .
310                   "b.encounter = ? AND " .
311                   "b.activity = 1 AND " .
312                   "b.code_type = ct.ct_key AND " .
313                   "ct.ct_diag = 0 " .
314                   "ORDER BY b.date",
315                   array($pid, $result['encounter']));
316                 while ($brow=sqlFetchArray($bres)) {
317                     echo "<span class='bold'>&nbsp;".xl('Procedure').": </span><span class='text'>" .
318                         $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
319                 }
320             }
321         }
323         call_user_func($result{"formdir"} . "_report", $pid, $result{"encounter"}, $cols, $result{"form_id"});
325         echo "</div>";
326     }
329 function getRecHistoryData ($pid) {
330         //data is returned as a multi-level array:
331         //column name->dates->values
332         //$return{"lname"}[0..n]{"date"}
333         //$return{"lname"}[0..n]{"value"}
334         $res = sqlStatement("select * from history_data where pid=? order by date", array($pid) );
335         
336         while($result = sqlFetchArray($res)) {
337                 foreach ($result as $key => $val) {
338                         if ($key == "pid" || $key == "date" || $key == "id") {
339                                 next;
340                         } else {
341                                 $curdate = $result{"date"};
342                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
343                                         $arcount{$key}++;
344                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
345                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
346                                 }
347                         }
348                 }
349         }
350         return $retar;
353 function getRecEmployerData ($pid) {
354         //data is returned as a multi-level array:
355         //column name->dates->values
356         //$return{"lname"}[0..n]{"date"}
357         //$return{"lname"}[0..n]{"value"}
358         $res = sqlStatement("select * from employer_data where pid=? order by date", array($pid) );
359         
360         while($result = sqlFetchArray($res)) {
361                 foreach ($result as $key => $val) {
362                         if ($key == "pid" || $key == "date" || $key == "id") {
363                                 next;
364                         } else {
365                                 $curdate = $result{"date"};
366                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
367                                         $arcount{$key}++;
368                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
369                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
370                                 }
371                         }
372                 }
373         }
374         return $retar;
377 function getRecPatientData ($pid) {
378         //data is returned as a multi-level array:
379         //column name->dates->values
380         //$return{"lname"}[0..n]{"date"}
381         //$return{"lname"}[0..n]{"value"}
382         $res = sqlStatement("select * from patient_data where pid=? order by date", array($pid) );
383         
384         while($result = sqlFetchArray($res)) {
385                 foreach ($result as $key => $val) {
386                         if ($key == "pid" || $key == "date" || $key == "id") {
387                                 next;
388                         } else {
389                                 $curdate = $result{"date"};
390                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
391                                         $arcount{$key}++;
392                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
393                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
394                                 }
395                         }
396                 }
397         }
398         return $retar;
401 function getRecInsuranceData ($pid, $ins_type) {
402         //data is returned as a multi-level array:
403         //column name->dates->values
404         //$return{"lname"}[0..n]{"date"}
405         //$return{"lname"}[0..n]{"value"}
406         $res = sqlStatement("select *, ic.name as provider_name from insurance_data left join insurance_companies as ic on ic.id = provider where pid=? and type=? order by date", array($pid,$ins_type) );
407         
408         while($result = sqlFetchArray($res)) {
409                 foreach ($result as $key => $val) {
410                         if ($key == "pid" || $key == "date" || $key == "id") {
411                                 next;
412                         } else {
413                                 $curdate = $result{"date"};
414                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
415                                         $arcount{$key}++;
416                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
417                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
418                                 }
419                         }
420                 }
421         }
422         return $retar;
425 function printRecData($data_array, $recres, $N) {
426         //this function generates a formatted history of all changes to the data
427         //it is a multi-level recursive function that exhaustively displays all of
428         //the changes, with dates, of any data in the database under the given
429         //argument restrictions.
430         //$data_array is an array with table_column_name => "display name"
431         //$recres is the return from getRecPatientData for example
432         //$N is the number of items to display in one row
433         print "<table><tr>\n";
434         $count = 0;
435         foreach ($data_array as $akey => $aval) {
436                 if ($count == $N) {
437                         print "</tr><tr>\n";
438                         $count = 0;
439                 }
440                 print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
441                 printData($recres, $akey, "<br>", "Y-m-d");
442                 print "</span></td>\n";
443                 $count++;
444         }
445         print "</tr></table>\n";
448 function printData ($retar, $key, $sep, $date_format) {
449         //$retar{$key}
450         if (@array_key_exists($key,$retar)) {
451                 $length = sizeof($retar{$key});
452                 for ($iter = $length;$iter>=1;$iter--) {
453                         if ($retar{$key}[$iter]{"value"} != "0000-00-00 00:00:00") {
454                                 print $retar{$key}[$iter]{"value"} . " (" . oeFormatSDFT(strtotime($retar{$key}[$iter]{"date"})) . ")$sep";
455                         }
456                 }
457         }
460 function printRecDataOne($data_array, $recres, $N) {
461         //this function is like printRecData except it will only print out those elements that
462         //have values. when they do have values, this function will only print out the most recent
463         //value of each element.
464         //this may be considered a compressed data viewer.
465         //this function generates a formatted history of all changes to the data
466         //$data_array is an array with table_column_name => "display name"
467         //$recres is the return from getRecPatientData for example
468         //$N is the number of items to display in one row
469         print "<table><tr>\n";
470         $count = 0;
471         foreach ($data_array as $akey => $aval) {
472                 if (sizeof($recres{$akey})>0 && ($recres{$akey}[1]{"value"}!="0000-00-00 00:00:00")) {
473                         if ($count == $N) {
474                                 print "</tr><tr>\n";
475                                 $count = 0;
476                         }
477                         print "<td valign=top><span class=bold>" . text($aval) . "</span><br><span class=text>";
478                         printDataOne($recres, $akey, "<br>", "Y-m-d");
479                         print "</span></td>\n";
480                         $count++;
481                 }
482         }
483         print "</tr></table>\n";
486 function printDataOne ($retar, $key, $sep, $date_format) {
487         //this function supports the printRecDataOne function above
488         if (@array_key_exists($key,$retar)) {
489                 $length = sizeof($retar{$key});
490                 if ($retar{$key}[$length]{"value"} != "0000-00-00 00:00:00") {
491       $tmp = $retar{$key}[$length]{"value"};
492       if (strstr($key, 'DOB')) $tmp = oeFormatShortDate($tmp);
493                         print text($tmp) . $sep;
494                 }
495         }