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