Fully responsive globals.php with vertical menu (#2460)
[openemr.git] / library / report.inc
blobe39891fe8c7a7920e9142991e2f738b5c377a27b
1 <?php
2 /**
3  * report.inc
4  *
5  * @package   OpenEMR
6  * @link      http://www.open-emr.org
7  * @author    Brady Miller <brady.g.miller@gmail.com>
8  * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
9  * @license   https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
10  */
13 require_once("{$GLOBALS['srcdir']}/acl.inc");
14 require_once($GLOBALS["srcdir"] . "/options.inc.php");
16 $patient_data_array = array(
17 'title' => xl('Title') . ": ",
18 'fname' => xl('First Name') . ": ",
19 'mname' => xl('Middle Name') . ": ",
20 'lname' => xl('Last Name') . ": ",
21 'sex' => xl('Sex') . ": ",
22 'ss' => xl('SS') . ": ",
23 'DOB' => xl('Date of Birth') . ": ",
24 'street' => xl('Street') . ": ",
25 'city' => xl('City') . ": ",
26 'state' => xl('State') . ": ",
27 'postal_code' => xl('Zip') . ": ",
28 'country_code' => xl('Country') . ": ",
29 'occupation' => xl('Occupation') . ": ",
30 'phone_home' => xl('Home Phone') . ": ",
31 'phone_biz' => xl('Business Phone') . ": ",
32 'phone_contact' => xl('Contact Phone') . ": ",
33 'contact_relationship' => xl('Contact Person') . ": ",
34 'hipaa_mail' => xl('Allows Mail') . ": ",
35 'hipaa_voice' => xl('Allows Voice msgs') . ": ",
36 'hipaa_notice' => xl('Notice Received') . ": ",
37 'hipaa_message' => xl('Leave Message With') . ": "
40 $history_data_array = array(
41 'coffee' => xl('Coffee Use') . ": ",
42 'tobacco' => xl('Tobacco Use') . ": ",
43 'alcohol' => xl('Alcohol Use') . ": ",
44 'sleep_patterns' => xl('Sleep Patterns') . ": ",
45 'exercise_patterns' => xl('Exercise Patterns') . ": ",
46 'seatbelt_use' => xl('Seatbelt Use') . ": ",
47 'counseling' => xl('Counseling') . ": ",
48 'hazardous_activities' => xl('Hazardous Activities') . ": ",
49 'last_breast_exam' => xl('Last Breast Exam') . ": ",
50 'last_mammogram' => xl('Last Mammogram') . ": ",
51 'last_gynocological_exam' => xl('Last Gyn. Exam') . ": ",
52 'last_rectal_exam' => xl('Last Rectal Exam') . ": ",
53 'last_prostate_exam' => xl('Last Prostate Exam') . ": ",
54 'last_physical_exam' => xl('Last Physical Exam') . ": ",
55 'last_sigmoidoscopy_colonoscopy' => xl('Last Sigmoid/Colonoscopy') . ": ",
56 'cataract_surgery' => xl('Last Cataract Surgery') . ": ",
57 'tonsillectomy' => xl('Last Tonsillectomy') . ": ",
58 'cholecystestomy' => xl('Last Cholecystestomy') . ": ",
59 'heart_surgery' => xl('Last Heart Surgery') . ": ",
60 'hysterectomy' => xl('Last Hysterectomy') . ": ",
61 'hernia_repair' => xl('Last Hernia Repair') . ": ",
62 'hip_replacement' => xl('Last Hip Replacement') . ": ",
63 'knee_replacement' => xl('Last Knee Replacement') . ": ",
64 'appendectomy' => xl('Last Appendectomy') . ": ",
65 'history_mother' => xl('Mothers History') . ": ",
66 'history_father' => xl('Fathers History') . ": ",
67 'history_siblings' => xl('Sibling History') . ": ",
68 'history_offspring' => xl('Offspring History') . ": ",
69 'history_spouse' => xl('Spouses History') . ": ",
70 'relatives_cancer' => xl('Relatives Cancer') . ": ",
71 'relatives_tuberculosis' => xl('Relatives Tuberculosis') . ": ",
72 'relatives_diabetes' => xl('Relatives Diabetes') . ": ",
73 'relatives_high_blood_pressure' => xl('Relatives Blood Pressure') . ": ",
74 'relatives_heart_problems' => xl('Relatives Heart') . ": ",
75 'relatives_stroke' => xl('Relatives Stroke') . ": ",
76 'relatives_epilepsy' => xl('Relatives Epilepsy') . ": ",
77 'relatives_mental_illness' => xl('Relatives Mental Illness') . ": ",
78 'relatives_suicide' => xl('Relatives Suicide') . ": "
81 $employer_data_array = array(
82 'name' => xl('Employer') . ": ",
83 'street' => xl('Address') . ": ",
84 'city' => xl('City') . ": ",
85 'postal_code' => xl('Zip') . ": ",
86 'state' => xl('State') . ": ",
87 'country' => xl('Country') . ": "
90 $insurance_data_array = array(
91 'provider_name' => xl('Provider') . ": ",
92 'plan_name' => xl('Plan Name') . ": ",
93 'policy_number' => xl('Policy Number') . ": ",
94 'group_number' => xl('Group Number') . ": ",
95 'subscriber_fname' => xl('Subscriber First Name') . ": ",
96 'subscriber_mname' => xl('Subscriber Middle Name') . ": ",
97 'subscriber_lname' => xl('Subscriber Last Name') . ": ",
98 'subscriber_relationship' => xl('Subscriber Relationship') . ": ",
99 'subscriber_ss' => xl('Subscriber SS') . ": ",
100 'subscriber_DOB' => xl('Subscriber Date of Birth') . ": ",
101 'subscriber_phone' => xl('Subscribter Phone') . ": ",
102 'subscriber_street' => xl('Subscriber Address') . ": ",
103 'subscriber_postal_code' => xl('Subscriber Zip') . ": ",
104 'subscriber_city' => xl('Subscriber City') . ": ",
105 'subscriber_state' => xl('Subscriber State') . ": ",
106 'subscriber_country' => xl('Subscriber Country') . ": ",
107 'subscriber_employer' => xl('Subscriber Employer') . ": ",
108 'subscriber_employer_street' => xl('Subscriber Employer Street') . ": ",
109 'subscriber_employer_city' => xl('Subscriber Employer City') . ": ",
110 'subscriber_employer_postal_code' => xl('Subscriber Employer Zip') . ": ",
111 'subscriber_employer_state' => xl('Subscriber Employer State') . ": ",
112 'subscriber_employer_country' => xl('Subscriber Employer Country') . ": "
115 function getPatientReport($pid)
117     $sql = "select * from patient_data where pid=? order by date ASC";
118     $res = sqlStatement($sql, array($pid));
119     while ($list = sqlFetchArray($res)) {
120         while (list($key, $value) = each($list)) {
121             if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date']) {
122                 $ret[$key]['title'] = $key;
123                 $ret[$key]['content'] = $value;
124                 $ret[$key]['date'] = $list['date'];
125             }
126         }
127     }
129     return $ret;
132 function getHistoryReport($pid)
134     $sql = "select * from history_data where pid=? order by date ASC";
135     $res = sqlStatement($sql, array($pid));
136     while ($list = sqlFetchArray($res)) {
137         while (list($key, $value) = each($list)) {
138             if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date']) {
139                 $ret[$key]['content'] = $value;
140                 $ret[$key]['date'] = $list['date'];
141             }
142         }
143     }
145     return $ret;
148 function getInsuranceReport($pid, $type = "primary")
150     $sql = "select * from insurance_data where pid=? and type=? order by date ASC";
151     $res = sqlStatement($sql, array($pid, $type));
152     while ($list = sqlFetchArray($res)) {
153         while (list($key, $value) = each($list)) {
154             if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date']) {
155                 $ret[$key]['content'] = $value;
156                 $ret[$key]['date'] = $list['date'];
157             }
158         }
159     }
161     return $ret;
164 function getEmployerReport($pid)
166     $sql = "select * from employer_data where pid=? order by date ASC";
167     $res = sqlStatement($sql, array($pid));
168     while ($list = sqlFetchArray($res)) {
169         while (list($key, $value) = each($list)) {
170             if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date']) {
171                 $ret[$key]['content'] = $value;
172                 $ret[$key]['date'] = $list['date'];
173             }
174         }
175     }
177     return $ret;
180 function getListsReport($id)
182     $sql = "select * from lists where id=? order by date ASC";
183     $res = sqlStatement($sql, array($id));
184     while ($list = sqlFetchArray($res)) {
185         while (list($key, $value) = each($list)) {
186             if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date']) {
187                 $ret[$key]['content'] = $value;
188                 $ret[$key]['date'] = $list['date'];
189             }
190         }
191     }
193     return $ret;
196 function printListData($pid, $list_type, $list_activity = "%")
198     $res = sqlStatement("select * from lists where pid=? and type=? and activity like ? order by date", array($pid, $list_type, $list_activity));
199     while ($result = sqlFetchArray($res)) {
200         print "<span class=bold>" . text($result{"title"}) . ":</span><span class=text> " . text($result{"comments"}) . "</span><br>\n";
201     }
204 function printPatientNotes($pid)
206   // exclude ALL deleted notes
207     $res = sqlStatement("select * from pnotes where pid = ? and deleted != 1 and activity = 1 order by date", array($pid));
208     while ($result = sqlFetchArray($res)) {
209         print "<span class=bold>" . text(oeFormatSDFT(strtotime($result{"date"}))) .
210         ":</span><span class=text> " .
211             nl2br(text(oeFormatPatientNote($result['body']))) . "</span><br>\n";
212     }
215 // Get the current value for a layout based transaction field.
217 function lbt_current_value($frow, $formid)
219     $formname = $frow['form_id'];
220     $field_id = $frow['field_id'];
221     $currvalue = '';
222     if ($formid) {
223         $ldrow = sqlQuery("SELECT field_value FROM lbt_data WHERE " .
224         "form_id = ? AND field_id = ?", array($formid, $field_id));
225         if (!empty($ldrow)) {
226             $currvalue = $ldrow['field_value'];
227         }
228     }
230     return $currvalue;
233 // Display a particular transaction.
235 function lbt_report($id, $formname)
237     $arr = array();
238     $fres = sqlStatement("SELECT * FROM layout_options " .
239     "WHERE form_id = ? AND uor > 0 " .
240     "ORDER BY group_id, seq", array($formname));
241     while ($frow = sqlFetchArray($fres)) {
242         $field_id  = $frow['field_id'];
243         $currvalue = lbt_current_value($frow, $id);
244         // For brevity, skip fields without a value.
245         if ($currvalue === '') {
246             continue;
247         }
249         $arr[$field_id] = wordwrap($currvalue, 30, "\n", true);
250     }
252     echo "<table>\n";
253     display_layout_rows($formname, $arr);
254     echo "</table>\n";
257 // Display all transactions for the specified patient.
259 function printPatientTransactions($pid)
261     $res = sqlStatement("SELECT * FROM transactions WHERE pid = ? ORDER BY date", array($pid));
262     while ($row = sqlFetchArray($res)) {
263         echo "<p><span class='bold'>" .
264         text(oeFormatSDFT(strtotime($row['date']))) .
265         " (" .
266         generate_display_field(array('data_type'=>'1','list_id'=>'transactions'), $row['title']) .
267         ")</span><br />\n";
268         lbt_report($row['id'], $row['title']);
269         echo "</p>\n";
270     }
273 function printPatientBilling($pid)
275     $res = sqlStatement("select * from billing where pid=? order by date", array($pid));
276     while ($result = sqlFetchArray($res)) {
277         echo "<span class=bold>" . text(oeFormatSDFT(strtotime($result{"date"}))) . " : </span>";
278         echo "<span class=text>(" . text($result{"code_type"}) . ") ";
279         echo $result['code_type'] == 'COPAY' ? text(oeFormatMoney($result['code'])) : text($result['code']);
280         echo " - ". wordwrap(text($result['code_text']), 70, "\n", true)."</span>";
281         echo "<br>\n";
282     }
285 function getPatientBillingEncounter($pid, $encounter)
287     $erow = sqlQuery("SELECT provider_id FROM form_encounter WHERE " .
288     "pid = ? AND encounter = ? " .
289     "ORDER BY id DESC LIMIT 1", array($pid, $encounter));
290     $inv_provider = $erow['provider_id'] + 0;
291     $sql = "SELECT b.*, u.id, u.fname, u.mname, u.lname, " .
292     "CONCAT(u.fname,' ', u.lname) AS provider_name, u.federaltaxid " .
293     "FROM billing AS b " .
294     "LEFT JOIN users AS u ON " .
295     "( b.provider_id != 0 AND u.id = b.provider_id ) OR " .
296     "( b.provider_id  = 0 AND u.id = ? ) " .
297     "WHERE pid= ? AND " .
298     "encounter = ? " .
299     "AND activity = '1' ORDER BY date";
301     $res = sqlStatement($sql, array($inv_provider, $pid, $encounter));
302     $billings = array();
303     while ($result = sqlFetchArray($res)) {
304         $billings[] = $result;
305     }
307     return $billings;
310 function printPatientForms($pid, $cols)
312     //this function takes a $pid
313     $inclookupres = sqlStatement("select distinct formdir from forms where pid=? AND deleted=0", array($pid));
314     while ($result = sqlFetchArray($inclookupres)) {
315         include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
316     }
318     $res = sqlStatement("select * from forms where pid=? AND deleted=0 order by date", array($pid));
319     while ($result = sqlFetchArray($res)) {
320         if ($result{"form_name"} == "New Patient Encounter") {
321             echo "<div class='text encounter'>\n";
322             echo "<h1>" . text($result["form_name"]) . "</h1>";
324             // display the provider info
325             $tmp = sqlQuery("SELECT u.title, u.fname, u.mname, u.lname " .
326                                     "FROM forms AS f, users AS u WHERE " .
327                                     "f.pid = ? AND f.encounter = ? AND " .
328                                     "f.formdir = 'newpatient' AND u.username = f.user " .
329                                     " AND f.deleted=0 ". //--JRM--
330                                     "ORDER BY f.id LIMIT 1", array($pid, $result['encounter']));
331             echo " ".xlt('Provider') . ": " . text($tmp['title']) . " " .
332                 text($tmp['fname']) . " " . text($tmp['mname']) . " " . text($tmp['lname']);
333             echo "<br/>";
334         } else {
335             echo "<div class='text encounter_form'>";
336             echo "<h1>" . text($result["form_name"]) . "</h1>";
337         }
339         echo "(" . text(oeFormatSDFT(strtotime($result["date"]))) . ") ";
341         if (acl_check('acct', 'rep') || acl_check('acct', 'eob') || acl_check('acct', 'bill')) {
342             if ($result{"form_name"} == "New Patient Encounter") {
343                 // display billing info
344                 echo "<br/>";
345                 $bres = sqlStatement(
346                     "SELECT b.date, b.code, b.code_text " .
347                     "FROM billing AS b, code_types AS ct WHERE " .
348                     "b.pid = ? AND " .
349                     "b.encounter = ? AND " .
350                     "b.activity = 1 AND " .
351                     "b.code_type = ct.ct_key AND " .
352                     "ct.ct_diag = 0 " .
353                     "ORDER BY b.date",
354                     array($pid, $result['encounter'])
355                 );
356                 while ($brow=sqlFetchArray($bres)) {
357                     echo "<span class='bold'>&nbsp;" . xlt('Procedure') . ": </span><span class='text'>" .
358                         text($brow['code']) . " " . text($brow['code_text']) . "</span><br>\n";
359                 }
360             }
361         }
363         call_user_func($result{"formdir"} . "_report", $pid, $result{"encounter"}, $cols, $result{"form_id"});
365         echo "</div>";
366     }
369 function getRecHistoryData($pid)
371     //data is returned as a multi-level array:
372     //column name->dates->values
373     //$return{"lname"}[0..n]{"date"}
374     //$return{"lname"}[0..n]{"value"}
375     $res = sqlStatement("select * from history_data where pid=? order by date", array($pid));
377     while ($result = sqlFetchArray($res)) {
378         foreach ($result as $key => $val) {
379             if ($key == "pid" || $key == "date" || $key == "id") {
380                 continue;
381             } else {
382                 $curdate = $result{"date"};
383                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
384                     $arcount{$key}++;
385                     $retar{$key}[$arcount{$key}]{"value"} = $val;
386                     $retar{$key}[$arcount{$key}]{"date"} = $curdate;
387                 }
388             }
389         }
390     }
392     return $retar;
395 function getRecEmployerData($pid)
397     //data is returned as a multi-level array:
398     //column name->dates->values
399     //$return{"lname"}[0..n]{"date"}
400     //$return{"lname"}[0..n]{"value"}
401     $res = sqlStatement("select * from employer_data where pid=? order by date", array($pid));
403     while ($result = sqlFetchArray($res)) {
404         foreach ($result as $key => $val) {
405             if ($key == "pid" || $key == "date" || $key == "id") {
406                 continue;
407             } else {
408                 $curdate = $result{"date"};
409                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
410                     $arcount{$key}++;
411                     $retar{$key}[$arcount{$key}]{"value"} = $val;
412                     $retar{$key}[$arcount{$key}]{"date"} = $curdate;
413                 }
414             }
415         }
416     }
418     return $retar;
421 function getRecPatientData($pid)
423     //data is returned as a multi-level array:
424     //column name->dates->values
425     //$return{"lname"}[0..n]{"date"}
426     //$return{"lname"}[0..n]{"value"}
427     $res = sqlStatement("select * from patient_data where pid=? order by date", array($pid));
429     while ($result = sqlFetchArray($res)) {
430         foreach ($result as $key => $val) {
431             if ($key == "pid" || $key == "date" || $key == "id") {
432                 continue;
433             } else {
434                 $curdate = $result{"date"};
435                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
436                     $arcount{$key}++;
437                     $retar{$key}[$arcount{$key}]{"value"} = $val;
438                     $retar{$key}[$arcount{$key}]{"date"} = $curdate;
439                 }
440             }
441         }
442     }
444     return $retar;
447 function getRecInsuranceData($pid, $ins_type)
449     //data is returned as a multi-level array:
450     //column name->dates->values
451     //$return{"lname"}[0..n]{"date"}
452     //$return{"lname"}[0..n]{"value"}
453     $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));
455     while ($result = sqlFetchArray($res)) {
456         foreach ($result as $key => $val) {
457             if ($key == "pid" || $key == "date" || $key == "id") {
458                 continue;
459             } else {
460                 $curdate = $result{"date"};
461                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
462                     $arcount{$key}++;
463                     $retar{$key}[$arcount{$key}]{"value"} = $val;
464                     $retar{$key}[$arcount{$key}]{"date"} = $curdate;
465                 }
466             }
467         }
468     }
470     return $retar;
473 function printRecData($data_array, $recres, $N)
475     //this function generates a formatted history of all changes to the data
476     //it is a multi-level recursive function that exhaustively displays all of
477     //the changes, with dates, of any data in the database under the given
478     //argument restrictions.
479     //$data_array is an array with table_column_name => "display name"
480     //$recres is the return from getRecPatientData for example
481     //$N is the number of items to display in one row
482     print "<table><tr>\n";
483     $count = 0;
484     foreach ($data_array as $akey => $aval) {
485         if ($count == $N) {
486             print "</tr><tr>\n";
487             $count = 0;
488         }
490         print "<td valign=top><span class=bold>" . text($aval) . "</span><br><span class=text>";
491         printData($recres, $akey, "<br>", "Y-m-d");
492         print "</span></td>\n";
493         $count++;
494     }
496     print "</tr></table>\n";
499 function printData($retar, $key, $sep, $date_format)
501     //$retar{$key}
502     if (@array_key_exists($key, $retar)) {
503         $length = sizeof($retar{$key});
504         for ($iter = $length; $iter>=1; $iter--) {
505             if ($retar{$key}[$iter]{"value"} != "0000-00-00 00:00:00") {
506                 print text($retar{$key}[$iter]{"value"}) . " (" . text(oeFormatSDFT(strtotime($retar{$key}[$iter]{"date"}))) . ")$sep";
507             }
508         }
509     }
512 function printRecDataOne($data_array, $recres, $N)
514     //this function is like printRecData except it will only print out those elements that
515     //have values. when they do have values, this function will only print out the most recent
516     //value of each element.
517     //this may be considered a compressed data viewer.
518     //this function generates a formatted history of all changes to the data
519     //$data_array is an array with table_column_name => "display name"
520     //$recres is the return from getRecPatientData for example
521     //$N is the number of items to display in one row
522     print "<table><tr>\n";
523     $count = 0;
524     foreach ($data_array as $akey => $aval) {
525         if (!empty($recres{$akey}) && sizeof($recres{$akey})>0 && ($recres{$akey}[1]{"value"}!="0000-00-00 00:00:00")) {
526             if ($count == $N) {
527                 print "</tr><tr>\n";
528                 $count = 0;
529             }
531             print "<td valign=top><span class=bold>" . text($aval) . "</span><br><span class=text>";
532             printDataOne($recres, $akey, "<br>", "Y-m-d");
533             print "</span></td>\n";
534             $count++;
535         }
536     }
538     print "</tr></table>\n";
541 function printDataOne($retar, $key, $sep, $date_format)
543     //this function supports the printRecDataOne function above
544     if (@array_key_exists($key, $retar)) {
545         $length = sizeof($retar{$key});
546         if ($retar{$key}[$length]{"value"} != "0000-00-00 00:00:00") {
547             $tmp = $retar{$key}[$length]{"value"};
548             if (strstr($key, 'DOB')) {
549                 $tmp = oeFormatShortDate($tmp);
550             }
552             print text($tmp) . $sep;
553         }
554     }