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