datepicker in lab module
[openemr.git] / library / report.inc
blob92316784af413f71bd59b4f7a832c91e5c31ceec
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']}/acl.inc");
8 require_once($GLOBALS["srcdir"] . "/options.inc.php");
10 $patient_data_array = array(
11 title => xl('Title') . ": ",
12 fname => xl('First Name') . ": ",
13 mname => xl('Middle Name') . ": ",
14 lname => xl('Last Name') . ": ",
15 sex => xl('Sex') . ": ",
16 ss => xl('SS') . ": ",
17 DOB => xl('Date of Birth') . ": ",
18 street => xl('Street') . ": ",
19 city => xl('City') . ": ",
20 state => xl('State') . ": ",
21 postal_code => xl('Zip') . ": ",
22 country_code => xl('Country') . ": ",
23 occupation => xl('Occupation') . ": ",
24 phone_home => xl('Home Phone') . ": ",
25 phone_biz => xl('Business Phone') . ": ",
26 phone_contact => xl('Contact Phone') . ": ",
27 contact_relationship => xl('Contact Person') . ": ",
28 hipaa_mail => xl('Allows Mail') . ": ",
29 hipaa_voice => xl('Allows Voice msgs') . ": ",
30 hipaa_notice => xl('Notice Received') . ": ",
31 hipaa_message => xl('Leave Message With') . ": "
34 $history_data_array = array(
35 coffee => xl('Coffee Use') . ": ",
36 tobacco => xl('Tobacco Use') . ": ",
37 alcohol => xl('Alcohol Use') . ": ",
38 sleep_patterns => xl('Sleep Patterns') . ": ",
39 exercise_patterns => xl('Exercise Patterns') . ": ",
40 seatbelt_use => xl('Seatbelt Use') . ": ",
41 counseling => xl('Counseling') . ": ",
42 hazardous_activities => xl('Hazardous Activities') . ": ",
43 last_breast_exam => xl('Last Breast Exam') . ": ",
44 last_mammogram => xl('Last Mammogram') . ": ",
45 last_gynocological_exam => xl('Last Gyn. Exam') . ": ",
46 last_rectal_exam => xl('Last Rectal Exam') . ": ",
47 last_prostate_exam => xl('Last Prostate Exam') . ": ",
48 last_physical_exam => xl('Last Physical Exam') . ": ",
49 last_sigmoidoscopy_colonoscopy => xl('Last Sigmoid/Colonoscopy') . ": ",
50 cataract_surgery => xl('Last Cataract Surgery') . ": ",
51 tonsillectomy => xl('Last Tonsillectomy') . ": ",
52 cholecystestomy => xl('Last Cholecystestomy') . ": ",
53 heart_surgery => xl('Last Heart Surgery') . ": ",
54 hysterectomy => xl('Last Hysterectomy') . ": ",
55 hernia_repair => xl('Last Hernia Repair') . ": ",
56 hip_replacement => xl('Last Hip Replacement') . ": ",
57 knee_replacement => xl('Last Knee Replacement') . ": ",
58 appendectomy => xl('Last Appendectomy') . ": ",
59 history_mother => xl('Mothers History') . ": ",
60 history_father => xl('Fathers History') . ": ",
61 history_siblings => xl('Sibling History') . ": ",
62 history_offspring => xl('Offspring History') . ": ",
63 history_spouse => xl('Spouses History') . ": ",
64 relatives_cancer => xl('Relatives Cancer') . ": ",
65 relatives_tuberculosis => xl('Relatives Tuberculosis') . ": ",
66 relatives_diabetes => xl('Relatives Diabetes') . ": ",
67 relatives_high_blood_pressure => xl('Relatives Blood Pressure') . ": ",
68 relatives_heart_problems => xl('Relatives Heart') . ": ",
69 relatives_stroke => xl('Relatives Stroke') . ": ",
70 relatives_epilepsy => xl('Relatives Epilepsy') . ": ",
71 relatives_mental_illness => xl('Relatives Mental Illness') . ": ",
72 relatives_suicide => xl('Relatives Suicide') . ": "
75 $employer_data_array = array(
76 name => xl('Employer') . ": ",
77 street => xl('Address') . ": ",
78 city => xl('City') . ": ",
79 postal_code => xl('Zip') . ": ",
80 state => xl('State') . ": ",
81 country => xl('Country') . ": "
84 $insurance_data_array = array(
85 provider_name => xl('Provider') . ": ",
86 plan_name => xl('Plan Name') . ": ",
87 policy_number => xl('Policy Number') . ": ",
88 group_number => xl('Group Number') . ": ",
89 subscriber_fname => xl('Subscriber First Name') . ": ",
90 subscriber_mname => xl('Subscriber Middle Name') . ": ",
91 subscriber_lname => xl('Subscriber Last Name') . ": ",
92 subscriber_relationship => xl('Subscriber Relationship') . ": ",
93 subscriber_ss => xl('Subscriber SS') . ": ",
94 subscriber_DOB => xl('Subscriber Date of Birth') . ": ",
95 subscriber_phone => xl('Subscribter Phone') . ": ",
96 subscriber_street => xl('Subscriber Address') . ": ",
97 subscriber_postal_code => xl('Subscriber Zip') . ": ",
98 subscriber_city => xl('Subscriber City') . ": ",
99 subscriber_state => xl('Subscriber State') . ": ",
100 subscriber_country => xl('Subscriber Country') . ": ",
101 subscriber_employer => xl('Subscriber Employer') . ": ",
102 subscriber_employer_street => xl('Subscriber Employer Street') . ": ",
103 subscriber_employer_city => xl('Subscriber Employer City') . ": ",
104 subscriber_employer_postal_code => xl('Subscriber Employer Zip') . ": ",
105 subscriber_employer_state => xl('Subscriber Employer State') . ": ",
106 subscriber_employer_country => xl('Subscriber Employer Country') . ": "
109 function getPatientReport($pid)
111         $sql = "select * from patient_data where pid='$pid' order by date ASC";
112         $res = sqlStatement("$sql");
113         while($list = sqlFetchArray($res))
114         {
115                 while(list($key, $value) = each($list))
116                 {
117                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
118                         {
119                                 $ret[$key]['title'] = $key;
120                                 $ret[$key]['content'] = $value;
121                                 $ret[$key]['date'] = $list['date'];
122                         }
123                 }
124         }
125         return $ret;
128 function getHistoryReport($pid)
130         $sql = "select * from history_data where pid='$pid' order by date ASC";
131         $res = sqlStatement("$sql");
132         while($list = sqlFetchArray($res))
133         {
134                 while(list($key, $value) = each($list))
135                 {
136                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
137                         {
138                                 $ret[$key]['content'] = $value;
139                                 $ret[$key]['date'] = $list['date'];
140                         }
141                 }
142         }
143         return $ret;
146 function getInsuranceReport($pid, $type = "primary")
148         $sql = "select * from insurance_data where pid='$pid' and type='$type' order by date ASC";
149         $res = sqlStatement("$sql");
150         while($list = sqlFetchArray($res))
151         {
152                 while(list($key, $value) = each($list))
153                 {
154                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
155                         {
156                                 $ret[$key]['content'] = $value;
157                                 $ret[$key]['date'] = $list['date'];
158                         }
159                 }
160         }
161         return $ret;
164 function getEmployerReport($pid)
166         $sql = "select * from employer_data where pid='$pid' order by date ASC";
167         $res = sqlStatement("$sql");
168         while($list = sqlFetchArray($res))
169         {
170                 while(list($key, $value) = each($list))
171                 {
172                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
173                         {
174                                 $ret[$key]['content'] = $value;
175                                 $ret[$key]['date'] = $list['date'];
176                         }
177                 }
178         }
179         return $ret;
182 function getListsReport($pid)
184         $sql = "select * from lists where id='$id' order by date ASC";
185         $res = sqlStatement("$sql");
186         while($list = sqlFetchArray($res))
187         {
188                 while(list($key, $value) = each($list))
189                 {
190                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
191                         {
192                                 $ret[$key]['content'] = $value;
193                                 $ret[$key]['date'] = $list['date'];
194                         }
195                 }
196         }
197         return $ret;
200 function printListData($pid, $list_type, $list_activity = "%") {
201         $res = sqlStatement("select * from lists where pid='$pid' and type='$list_type' and activity like '$list_activity' order by date");
202         while($result = sqlFetchArray($res)) {
203                 print "<span class=bold>" . $result{"title"} . ":</span><span class=text> " . $result{"comments"} . "</span><br>\n";
204         }
207 function printPatientNotes($pid) {
208   // exclude ALL deleted notes
209   $res = sqlStatement("select * from pnotes where pid = '$pid' and deleted != 1 and activity = 1 order by date");
210   while($result = sqlFetchArray($res)) {
211     print "<span class=bold>" . oeFormatSDFT(strtotime($result{"date"})) .
212       ":</span><span class=text> " .
213       stripslashes(oeFormatPatientNote($result['body'])) . "</span><br>\n";
214   }
217 // Get the current value for a layout based transaction field.
219 function lbt_current_value($frow, $formid) {
220   $formname = $frow['form_id'];
221   $field_id = $frow['field_id'];
222   $currvalue = '';
223   if ($formid) {
224     $ldrow = sqlQuery("SELECT field_value FROM lbt_data WHERE " .
225       "form_id = ? AND field_id = ?", array($formid, $field_id) );
226     if (!empty($ldrow)) $currvalue = $ldrow['field_value'];
227   }
228   return $currvalue;
231 // Display a particular transaction.
233 function lbt_report($id, $formname) {
234   $arr = array();
235   $fres = sqlStatement("SELECT * FROM layout_options " .
236     "WHERE form_id = ? AND uor > 0 " .
237     "ORDER BY group_name, seq", array($formname));
238   while ($frow = sqlFetchArray($fres)) {
239     $field_id  = $frow['field_id'];
240     $currvalue = lbt_current_value($frow, $id);
241     // For brevity, skip fields without a value.
242     if ($currvalue === '') continue;
243     $arr[$field_id] = wordwrap($currvalue, 30, "\n", true);
244   }
245   echo "<table>\n";
246   display_layout_rows($formname, $arr);
247   echo "</table>\n";
250 // Display all transactions for the specified patient.
252 function printPatientTransactions($pid) {
253         $res = sqlStatement("SELECT * FROM transactions WHERE pid = ? ORDER BY date", array($pid));
254         while($row = sqlFetchArray($res)) {
255     echo "<p><span class='bold'>" .
256       text(oeFormatSDFT(strtotime($row['date']))) .
257       " (" .
258       generate_display_field(array('data_type'=>'1','list_id'=>'transactions'), $row['title']) .
259       ")</span><br />\n";
260     lbt_report($row['id'], $row['title']);
261     echo "</p>\n";
262         }
265 function printPatientBilling($pid) {
266   $res = sqlStatement("select * from billing where pid='$pid' order by date");
267   while($result = sqlFetchArray($res)) {
268     echo "<span class=bold>" . oeFormatSDFT(strtotime($result{"date"})) . " : </span>";
269     echo "<span class=text>(".$result{"code_type"}.") ";
270     echo $result['code_type'] == 'COPAY' ? oeFormatMoney($result['code']) : $result['code'];
271     echo " - ". wordwrap($result['code_text'], 70, "\n", true)."</span>";
272     echo "<br>\n";
273         }
276 function getPatientBillingEncounter($pid, $encounter) {
277   /*******************************************************************
278         $sql = "SELECT billing.*, u.id, u.fname, u.mname, u.lname, " .
279     "CONCAT(u.fname,' ', u.lname) AS provider_name, u.federaltaxid " .
280     "FROM billing LEFT JOIN users AS u ON u.id = billing.provider_id " .
281     "WHERE pid='" . add_escape_custom($pid) . "' AND " .
282     "encounter = '" . add_escape_custom($encounter) .
283     "' AND activity='1' ORDER BY date";
284   *******************************************************************/
285   $erow = sqlQuery("SELECT provider_id FROM form_encounter WHERE " .
286     "pid = '$pid' AND encounter = '$encounter' " .
287     "ORDER BY id DESC LIMIT 1");
288   $inv_provider = $erow['provider_id'] + 0;
289         $sql = "SELECT b.*, u.id, u.fname, u.mname, u.lname, " .
290     "CONCAT(u.fname,' ', u.lname) AS provider_name, u.federaltaxid " .
291     "FROM billing AS b " .
292     "LEFT JOIN users AS u ON " .
293     "( b.provider_id != 0 AND u.id = b.provider_id ) OR " .
294     "( b.provider_id  = 0 AND u.id = $inv_provider ) " .
295     "WHERE pid='" . add_escape_custom($pid) . "' AND " .
296     "encounter = '" . add_escape_custom($encounter) .
297     "' AND activity = '1' ORDER BY date";
298   /******************************************************************/
299         $res = sqlStatement($sql);
300         $billings = array();
301         while($result = sqlFetchArray($res)) {
302                 $billings[] = $result;
303         }
304         return $billings;
307 function printPatientForms($pid, $cols) {
308     //this function takes a $pid
309     $inclookupres = sqlStatement("select distinct formdir from forms where pid='$pid' AND deleted=0");
310     while($result = sqlFetchArray($inclookupres)) {
311         include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
312     }
314     $res = sqlStatement("select * from forms where pid='$pid' AND deleted=0 order by date");
315     while($result = sqlFetchArray($res)) {
316         if ($result{"form_name"} == "New Patient Encounter") {
317             echo "<div class='text encounter'>\n";
318             echo "<h1>" . $result["form_name"] . "</h1>";
320             // display the provider info
321             $tmp = sqlQuery("SELECT u.title, u.fname, u.mname, u.lname " .
322                                     "FROM forms AS f, users AS u WHERE " .
323                                     "f.pid = '$pid' AND f.encounter = ".$result['encounter']." AND " .
324                                     "f.formdir = 'newpatient' AND u.username = f.user " .
325                                     " AND f.deleted=0 ". //--JRM--
326                                     "ORDER BY f.id LIMIT 1");
327             echo " ".xl('Provider').": ".$tmp['title']." ".
328                 $tmp['fname']." ".$tmp['mname']." ".$tmp['lname'];
329             echo "<br/>";
330         }
331         else {
332             echo "<div class='text encounter_form'>";
333             echo "<h1>" . $result["form_name"] . "</h1>";
334         }
335         echo "(" . oeFormatSDFT(strtotime($result["date"])) . ") ";
337         if (acl_check('acct', 'rep') || acl_check('acct', 'eob') || acl_check('acct', 'bill')) {
338             if ($result{"form_name"} == "New Patient Encounter") {
339                 // display billing info
340                 echo "<br/>";
341                 $bres = sqlStatement("SELECT b.date, b.code, b.code_text " .
342                   "FROM billing AS b, code_types AS ct WHERE " .
343                   "b.pid = ? AND " .
344                   "b.encounter = ? AND " .
345                   "b.activity = 1 AND " .
346                   "b.code_type = ct.ct_key AND " .
347                   "ct.ct_diag = 0 " .
348                   "ORDER BY b.date",
349                   array($pid, $result['encounter']));
350                 while ($brow=sqlFetchArray($bres)) {
351                     echo "<span class='bold'>&nbsp;".xl('Procedure').": </span><span class='text'>" .
352                         $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
353                 }
354             }
355         }
357         call_user_func($result{"formdir"} . "_report", $pid, $result{"encounter"}, $cols, $result{"form_id"});
359         echo "</div>";
360     }
363 function getRecHistoryData ($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 history_data where pid=? order by date", array($pid) );
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 getRecEmployerData ($pid) {
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 * from employer_data where pid=? order by date", array($pid) );
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 getRecPatientData ($pid) {
412         //data is returned as a multi-level array:
413         //column name->dates->values
414         //$return{"lname"}[0..n]{"date"}
415         //$return{"lname"}[0..n]{"value"}
416         $res = sqlStatement("select * from patient_data where pid=? order by date", array($pid) );
418         while($result = sqlFetchArray($res)) {
419                 foreach ($result as $key => $val) {
420                         if ($key == "pid" || $key == "date" || $key == "id") {
421                                 next;
422                         } else {
423                                 $curdate = $result{"date"};
424                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
425                                         $arcount{$key}++;
426                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
427                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
428                                 }
429                         }
430                 }
431         }
432         return $retar;
435 function getRecInsuranceData ($pid, $ins_type) {
436         //data is returned as a multi-level array:
437         //column name->dates->values
438         //$return{"lname"}[0..n]{"date"}
439         //$return{"lname"}[0..n]{"value"}
440         $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) );
442         while($result = sqlFetchArray($res)) {
443                 foreach ($result as $key => $val) {
444                         if ($key == "pid" || $key == "date" || $key == "id") {
445                                 next;
446                         } else {
447                                 $curdate = $result{"date"};
448                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
449                                         $arcount{$key}++;
450                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
451                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
452                                 }
453                         }
454                 }
455         }
456         return $retar;
459 function printRecData($data_array, $recres, $N) {
460         //this function generates a formatted history of all changes to the data
461         //it is a multi-level recursive function that exhaustively displays all of
462         //the changes, with dates, of any data in the database under the given
463         //argument restrictions.
464         //$data_array is an array with table_column_name => "display name"
465         //$recres is the return from getRecPatientData for example
466         //$N is the number of items to display in one row
467         print "<table><tr>\n";
468         $count = 0;
469         foreach ($data_array as $akey => $aval) {
470                 if ($count == $N) {
471                         print "</tr><tr>\n";
472                         $count = 0;
473                 }
474                 print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
475                 printData($recres, $akey, "<br>", "Y-m-d");
476                 print "</span></td>\n";
477                 $count++;
478         }
479         print "</tr></table>\n";
482 function printData ($retar, $key, $sep, $date_format) {
483         //$retar{$key}
484         if (@array_key_exists($key,$retar)) {
485                 $length = sizeof($retar{$key});
486                 for ($iter = $length;$iter>=1;$iter--) {
487                         if ($retar{$key}[$iter]{"value"} != "0000-00-00 00:00:00") {
488                                 print $retar{$key}[$iter]{"value"} . " (" . oeFormatSDFT(strtotime($retar{$key}[$iter]{"date"})) . ")$sep";
489                         }
490                 }
491         }
494 function printRecDataOne($data_array, $recres, $N) {
495         //this function is like printRecData except it will only print out those elements that
496         //have values. when they do have values, this function will only print out the most recent
497         //value of each element.
498         //this may be considered a compressed data viewer.
499         //this function generates a formatted history of all changes to the data
500         //$data_array is an array with table_column_name => "display name"
501         //$recres is the return from getRecPatientData for example
502         //$N is the number of items to display in one row
503         print "<table><tr>\n";
504         $count = 0;
505         foreach ($data_array as $akey => $aval) {
506                 if (sizeof($recres{$akey})>0 && ($recres{$akey}[1]{"value"}!="0000-00-00 00:00:00")) {
507                         if ($count == $N) {
508                                 print "</tr><tr>\n";
509                                 $count = 0;
510                         }
511                         print "<td valign=top><span class=bold>" . text($aval) . "</span><br><span class=text>";
512                         printDataOne($recres, $akey, "<br>", "Y-m-d");
513                         print "</span></td>\n";
514                         $count++;
515                 }
516         }
517         print "</tr></table>\n";
520 function printDataOne ($retar, $key, $sep, $date_format) {
521         //this function supports the printRecDataOne function above
522         if (@array_key_exists($key,$retar)) {
523                 $length = sizeof($retar{$key});
524                 if ($retar{$key}[$length]{"value"} != "0000-00-00 00:00:00") {
525       $tmp = $retar{$key}[$length]{"value"};
526       if (strstr($key, 'DOB')) $tmp = oeFormatShortDate($tmp);
527                         print text($tmp) . $sep;
528                 }
529         }