Translation project - incorporate xl() and generate_form_field() for superbill list
[openemr.git] / library / report.inc
blob33aa3590992f69c6db729a09fae51f37cb47418d
1 <?php
2 require_once("{$GLOBALS['srcdir']}/sql.inc");
3 require_once("{$GLOBALS['srcdir']}/acl.inc");
5 $patient_data_array = array(title => "Title: ",
6 fname => "First Name: ",
7 mname => "Middle Name: ",
8 lname => "Last Name: ",
9 sex => "Sex",
10 ss => "SS: ",
11 DOB => "Date of Birth: ",
12 street => "Street: ",
13 city => "City: ",
14 state => "State: ",
15 postal_code => "Zip: ",
16 country_code => "Country: ",
17 occupation => "Occupation: ",
18 phone_home => "Home Phone: ",
19 phone_biz => "Business Phone: ",
20 phone_contact => "Contact Phone: ",
21 contact_relationship => "Contact Person",
22 hipaa_mail => "Allows Mail: ",
23 hipaa_voice => "Allows Voice msgs: ",
24 hipaa_notice => "Notice Received: ",
25 hipaa_message => "Leave Message With: "
29 $history_data_array = array(
30 coffee => "Coffee Use: ",
31 tobacco => "Tobacco Use: ",
32 alcohol => "Alcohol Use: ",
33 sleep_patterns => "Sleep Patterns: ",
34 exercise_patterns => "Exercise Patterns: ",
35 seatbelt_use => "Seatbelt Use: ",
36 counseling => "Counseling: ",
37 hazardous_activities => "Hazardous Activities: ",
38 last_breast_exam => "Last Breast Exam: ",
39 last_mammogram => "Last Mammogram: ",
40 last_gynocological_exam => "Last Gyn. Exam: ",
41 last_rectal_exam => "Last Rectal Exam: ",
42 last_prostate_exam => "Last Prostate Exam: ",
43 last_physical_exam => "Last Physical Exam: ",
44 last_sigmoidoscopy_colonoscopy => "Last Sigmoid/Colonoscopy: ",
45 cataract_surgery => "Last Cataract Surgery: ",
46 tonsillectomy => "Last Tonsillectomy: ",
47 cholecystestomy => "Last Cholecystestomy: ",
48 heart_surgery => "Last Heart Surgery: ",
49 hysterectomy => "Last Hysterectomy: ",
50 hernia_repair => "Last Hernia Repair: ",
51 hip_replacement => "Last Hip Replacement: ",
52 knee_replacement => "Last Knee Replacement: ",
53 appendectomy => "Last Appendectomy: ",
54 history_mother => "Mother's History: ",
55 history_father => "Father's History: ",
56 history_siblings => "Sibling History: ",
57 history_offspring => "Offspring History: ",
58 history_spouse => "Spouse's History: ",
59 relatives_cancer => "Relatives Cancer: ",
60 relatives_tuberculosis => "Relatives Tuberculosis: ",
61 relatives_diabetes => "Relatives Diabetes: ",
62 relatives_high_blood_pressure => "Relatives Blood Pressure: ",
63 relatives_heart_problems => "Relatives Heart: ",
64 relatives_stroke => "Relatives Stroke: ",
65 relatives_epilepsy => "Relatives Epilepsy: ",
66 relatives_mental_illness => "Relatives Mental Illness: ",
67 relatives_suicide => "Relatives Suicide: ");
69 $employer_data_array = array(
70 name => "Employer: ",
71 street => "Address: ",
72 city => "City: ",
73 postal_code => "Zip: ",
74 state => "State",
75 country => "Country: ");
77 $insurance_data_array = array(
78 provider_name => "Provider: ",
79 plan_name => "Plan Name: ",
80 policy_number => "Policy Number: ",
81 group_number => "Group Number: ",
82 subscriber_fname => "Subscriber First Name: ",
83 subscriber_mname => "Subscriber Middle Name: ",
84 subscriber_lname => "Subscriber Last Name: ",
85 subscriber_relationship => "Subscriber Relationship: ",
86 subscriber_ss => "Subscriber SS: ",
87 subscriber_DOB => "Subscriber Date of Birth: ",
88 subscriber_phone => "Subscribter Phone: ",
89 subscriber_street => "Subscriber Address: ",
90 subscriber_postal_code => "Subscriber Zip: ",
91 subscriber_city => "Subscriber City: ",
92 subscriber_state => "Subscriber State: ",
93 subscriber_country => "Subscriber Country: ",
94 subscriber_employer => "Subscriber Employer: ",
95 subscriber_employer_street => "Subscriber Employer Street: ",
96 subscriber_employer_city => "Subscriber Employer City: ",
97 subscriber_employer_postal_code => "Subscriber Employer Zip: ",
98 subscriber_employer_state => "Subscriber Employer State: ",
99 subscriber_employer_country => "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>" . date("Y-m-d",strtotime($result{"date"})) . ":</span><span class=text> " . stripslashes($result{"body"}) . "</span><br>\n";
214         }
217 function printPatientTransactions($pid) {
218         $res = sqlStatement("select * from transactions where pid='$pid' order by date");
219         while($result = sqlFetchArray($res)) {
220                 print "<span class=bold>" . date("Y-m-d",strtotime($result{"date"})) . ":</span><span class=text>(".$result{"title"}.") " . stripslashes($result{"body"}) . "</span><br>\n";
221         }
225 function printPatientBilling($pid) {
226         $res = sqlStatement("select * from billing where pid='$pid' order by date");
227         while($result = sqlFetchArray($res)) {
228                 echo "<span class=bold>" . date("Y-m-d",strtotime($result{"date"})) . " : </span>";
229                 echo "<span class=text>(".$result{"code_type"}.") " . $result{"code"} . " - ". $result['code_text']."</span>";
230                 echo "<br>\n";
231         }
234 function getPatientBillingEncounter($pid,$encounter) {
235         $sql = "select billing.*, u.id, u.fname, u.mname, u.lname,CONCAT(u.fname,' ', u.lname) as provider_name, u.federaltaxid  from billing LEFT JOIN users as u on u.id = billing.provider_id where pid='" . mysql_real_escape_string($pid) . "' and encounter = '" . mysql_real_escape_string($encounter) . "' and activity='1' order by date";
236         $res = sqlStatement($sql);
237         $billings = array();
238         while($result = sqlFetchArray($res)) {
239                 $billings[] = $result;
240         }
241         return $billings;
245 function printPatientForms($pid, $cols) {
246     //this function takes a $pid
247     $inclookupres = sqlStatement("select distinct formdir from forms where pid='$pid' AND deleted=0");
248     while($result = sqlFetchArray($inclookupres)) {
249         include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
250     }
251         
252     $res = sqlStatement("select * from forms where pid='$pid' AND deleted=0 order by date");
253     while($result = sqlFetchArray($res)) {
254         if ($result{"form_name"} == "New Patient Encounter") {
255             echo "<div class='text encounter'>\n";
256             echo "<h1>" . $result["form_name"] . "</h1>";
258             // display the provider info
259             $tmp = sqlQuery("SELECT u.title, u.fname, u.mname, u.lname " .
260                                     "FROM forms AS f, users AS u WHERE " .
261                                     "f.pid = '$pid' AND f.encounter = ".$result['encounter']." AND " .
262                                     "f.formdir = 'newpatient' AND u.username = f.user " .
263                                     " AND f.deleted=0 ". //--JRM--
264                                     "ORDER BY f.id LIMIT 1");
265             echo " ".xl('Provider').": ".$tmp['title']." ".
266                 $tmp['fname']." ".$tmp['mname']." ".$tmp['lname'];
267             echo "<br/>";
268         }
269         else {
270             echo "<div class='text encounter_form'>";
271             echo "<h1>" . $result["form_name"] . "</h1>";
272         }
273         echo "(" . date("Y-m-d",strtotime($result["date"])) . ") ";
275         if (acl_check('acct', 'rep') || acl_check('acct', 'eob') || acl_check('acct', 'bill')) {
276             if ($result{"form_name"} == "New Patient Encounter") {
277                 // display billing info
278                 echo "<br/>";
279                 $bres = sqlStatement("SELECT date, code, code_text FROM billing WHERE " .
280                                         "pid = '$pid' AND encounter = '".$result['encounter']."' AND activity = 1 AND " .
281                                         "( code_type = 'CPT4' OR code_type = 'OPCS' OR code_type = 'OSICS10' ) " .
282                                         "ORDER BY date");
283                 while ($brow=sqlFetchArray($bres)) {
284                     echo "<span class='bold'>&nbsp;".xl('Procedure').": </span><span class='text'>" .
285                         $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
286                 }
287             }
288         }
290         call_user_func($result{"formdir"} . "_report", $pid, $result{"encounter"}, $cols, $result{"form_id"});
292         echo "</div>";
293     }
297 function getRecHistoryData ($pid) {
298         //data is returned as a multi-level array:
299         //column name->dates->values
300         //$return{"lname"}[0..n]{"date"}
301         //$return{"lname"}[0..n]{"value"}
302         $res = sqlStatement("select * from history_data where pid='$pid' order by date");
303         
304         while($result = sqlFetchArray($res)) {
305                 foreach ($result as $key => $val) {
306                         if ($key == "pid" || $key == "date" || $key == "id") {
307                                 next;
308                         } else {
309                                 $curdate = $result{"date"};
310                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
311                                         $arcount{$key}++;
312                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
313                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
314                                 }
315                         }
316                 }
317         }
318         return $retar;
321 function getRecEmployerData ($pid) {
322         //data is returned as a multi-level array:
323         //column name->dates->values
324         //$return{"lname"}[0..n]{"date"}
325         //$return{"lname"}[0..n]{"value"}
326         $res = sqlStatement("select * from employer_data where pid='$pid' order by date");
327         
328         while($result = sqlFetchArray($res)) {
329                 foreach ($result as $key => $val) {
330                         if ($key == "pid" || $key == "date" || $key == "id") {
331                                 next;
332                         } else {
333                                 $curdate = $result{"date"};
334                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
335                                         $arcount{$key}++;
336                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
337                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
338                                 }
339                         }
340                 }
341         }
342         return $retar;
346 function getRecPatientData ($pid) {
347         //data is returned as a multi-level array:
348         //column name->dates->values
349         //$return{"lname"}[0..n]{"date"}
350         //$return{"lname"}[0..n]{"value"}
351         $res = sqlStatement("select * from patient_data where pid='$pid' order by date");
352         
353         while($result = sqlFetchArray($res)) {
354                 foreach ($result as $key => $val) {
355                         if ($key == "pid" || $key == "date" || $key == "id") {
356                                 next;
357                         } else {
358                                 $curdate = $result{"date"};
359                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
360                                         $arcount{$key}++;
361                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
362                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
363                                 }
364                         }
365                 }
366         }
367         return $retar;
371 function getRecInsuranceData ($pid, $ins_type) {
372         //data is returned as a multi-level array:
373         //column name->dates->values
374         //$return{"lname"}[0..n]{"date"}
375         //$return{"lname"}[0..n]{"value"}
376         $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");
377         
378         while($result = sqlFetchArray($res)) {
379                 foreach ($result as $key => $val) {
380                         if ($key == "pid" || $key == "date" || $key == "id") {
381                                 next;
382                         } else {
383                                 $curdate = $result{"date"};
384                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
385                                         $arcount{$key}++;
386                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
387                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
388                                 }
389                         }
390                 }
391         }
392         return $retar;
396 function printRecData($data_array, $recres, $N) {
397         //this function generates a formatted history of all changes to the data
398         //it is a multi-level recursive function that exhaustively displays all of
399         //the changes, with dates, of any data in the database under the given
400         //argument restrictions.
401         //$data_array is an array with table_column_name => "display name"
402         //$recres is the return from getRecPatientData for example
403         //$N is the number of items to display in one row
404         print "<table><tr>\n";
405         $count = 0;
406         foreach ($data_array as $akey => $aval) {
407                 if ($count == $N) {
408                         print "</tr><tr>\n";
409                         $count = 0;
410                 }
411                 print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
412                 printData($recres, $akey, "<br>", "Y-m-d");
413                 print "</span></td>\n";
414                 $count++;
415         }
416         print "</tr></table>\n";
422 function printData ($retar, $key, $sep, $date_format) {
423         //$retar{$key}
424         if (@array_key_exists($key,$retar)) {
425                 $length = sizeof($retar{$key});
426                 for ($iter = $length;$iter>=1;$iter--) {
427                         if ($retar{$key}[$iter]{"value"} != "0000-00-00 00:00:00") {
428                                 print $retar{$key}[$iter]{"value"} . " (" . date($date_format,strtotime($retar{$key}[$iter]{"date"})) . ")$sep";
429                         }
430                 }
431         }
434 function printRecDataOne($data_array, $recres, $N) {
435         //this function is like printRecData except it will only print out those elements that
436         //have values. when they do have values, this function will only print out the most recent
437         //value of each element.
438         //this may be considered a compressed data viewer.
439         //this function generates a formatted history of all changes to the data
440         //$data_array is an array with table_column_name => "display name"
441         //$recres is the return from getRecPatientData for example
442         //$N is the number of items to display in one row
443         print "<table><tr>\n";
444         $count = 0;
445         foreach ($data_array as $akey => $aval) {
446                 if (sizeof($recres{$akey})>0 && ($recres{$akey}[1]{"value"}!="0000-00-00 00:00:00")) {
447                         if ($count == $N) {
448                                 print "</tr><tr>\n";
449                                 $count = 0;
450                         }
451                         print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
452                         printDataOne($recres, $akey, "<br>", "Y-m-d");
453                         print "</span></td>\n";
454                         $count++;
455                 }
456         }
457         print "</tr></table>\n";
463 function printDataOne ($retar, $key, $sep, $date_format) {
464         //this function supports the printRecDataOne function above
465         if (@array_key_exists($key,$retar)) {
466                 $length = sizeof($retar{$key});
467                 if ($retar{$key}[$length]{"value"} != "0000-00-00 00:00:00") {
468                         //print $retar{$key}[$length]{"value"} . " (" . date($date_format,strtotime($retar{$key}[$length]{"date"})) . "$sep";
469                         print $retar{$key}[$length]{"value"} . "$sep";
470                 }
471         }