removed new acceptors from ma stats
[openemr.git] / library / report.inc
blob9917de3df96012343039a45997a3e6b56f6c69a5
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         $res = sqlStatement("select * from pnotes where pid='$pid' and activity=1 order by date");
211         while($result = sqlFetchArray($res)) {
212                 print "<span class=bold>" . date("Y-m-d",strtotime($result{"date"})) . ":</span><span class=text> " . stripslashes($result{"body"}) . "</span><br>\n";
213         }
216 function printPatientTransactions($pid) {
217         $res = sqlStatement("select * from transactions where pid='$pid' order by date");
218         while($result = sqlFetchArray($res)) {
219                 print "<span class=bold>" . date("Y-m-d",strtotime($result{"date"})) . ":</span><span class=text>(".$result{"title"}.") " . stripslashes($result{"body"}) . "</span><br>\n";
220         }
224 function printPatientBilling($pid) {
225         $res = sqlStatement("select * from billing where pid='$pid' order by date");
226         while($result = sqlFetchArray($res)) {
227                 echo "<span class=bold>" . date("Y-m-d",strtotime($result{"date"})) . " : </span>";
228                 echo "<span class=text>(".$result{"code_type"}.") " . $result{"code"} . " - ". $result['code_text']."</span>";
229                 echo "<br>\n";
230         }
233 function getPatientBillingEncounter($pid,$encounter) {
234         $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";
235         $res = sqlStatement($sql);
236         $billings = array();
237         while($result = sqlFetchArray($res)) {
238                 $billings[] = $result;
239         }
240         return $billings;
244 function printPatientForms($pid, $cols) {
245     //this function takes a $pid
246     $inclookupres = sqlStatement("select distinct formdir from forms where pid='$pid' AND deleted=0");
247     while($result = sqlFetchArray($inclookupres)) {
248         include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
249     }
250         
251     $res = sqlStatement("select * from forms where pid='$pid' AND deleted=0 order by date");
252     while($result = sqlFetchArray($res)) {
253         if ($result{"form_name"} == "New Patient Encounter") {
254             echo "<div class='text encounter'>\n";
255             echo "<h1>" . $result["form_name"] . "</h1>";
257             // display the provider info
258             $tmp = sqlQuery("SELECT u.title, u.fname, u.mname, u.lname " .
259                                     "FROM forms AS f, users AS u WHERE " .
260                                     "f.pid = '$pid' AND f.encounter = ".$result['encounter']." AND " .
261                                     "f.formdir = 'newpatient' AND u.username = f.user " .
262                                     " AND f.deleted=0 ". //--JRM--
263                                     "ORDER BY f.id LIMIT 1");
264             echo " ".xl('Provider').": ".$tmp['title']." ".
265                 $tmp['fname']." ".$tmp['mname']." ".$tmp['lname'];
266             echo "<br/>";
267         }
268         else {
269             echo "<div class='text encounter_form'>";
270             echo "<h1>" . $result["form_name"] . "</h1>";
271         }
272         echo "(" . date("Y-m-d",strtotime($result["date"])) . ") ";
274         if (acl_check('acct', 'rep') || acl_check('acct', 'eob') || acl_check('acct', 'bill')) {
275             if ($result{"form_name"} == "New Patient Encounter") {
276                 // display billing info
277                 echo "<br/>";
278                 $bres = sqlStatement("SELECT date, code, code_text FROM billing WHERE " .
279                                         "pid = '$pid' AND encounter = '".$result['encounter']."' AND activity = 1 AND " .
280                                         "( code_type = 'CPT4' OR code_type = 'OPCS' OR code_type = 'OSICS10' ) " .
281                                         "ORDER BY date");
282                 while ($brow=sqlFetchArray($bres)) {
283                     echo "<span class='bold'>&nbsp;".xl('Procedure').": </span><span class='text'>" .
284                         $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
285                 }
286             }
287         }
289         call_user_func($result{"formdir"} . "_report", $pid, $result{"encounter"}, $cols, $result{"form_id"});
291         echo "</div>";
292     }
296 function getRecHistoryData ($pid) {
297         //data is returned as a multi-level array:
298         //column name->dates->values
299         //$return{"lname"}[0..n]{"date"}
300         //$return{"lname"}[0..n]{"value"}
301         $res = sqlStatement("select * from history_data where pid='$pid' order by date");
302         
303         while($result = sqlFetchArray($res)) {
304                 foreach ($result as $key => $val) {
305                         if ($key == "pid" || $key == "date" || $key == "id") {
306                                 next;
307                         } else {
308                                 $curdate = $result{"date"};
309                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
310                                         $arcount{$key}++;
311                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
312                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
313                                 }
314                         }
315                 }
316         }
317         return $retar;
320 function getRecEmployerData ($pid) {
321         //data is returned as a multi-level array:
322         //column name->dates->values
323         //$return{"lname"}[0..n]{"date"}
324         //$return{"lname"}[0..n]{"value"}
325         $res = sqlStatement("select * from employer_data where pid='$pid' order by date");
326         
327         while($result = sqlFetchArray($res)) {
328                 foreach ($result as $key => $val) {
329                         if ($key == "pid" || $key == "date" || $key == "id") {
330                                 next;
331                         } else {
332                                 $curdate = $result{"date"};
333                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
334                                         $arcount{$key}++;
335                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
336                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
337                                 }
338                         }
339                 }
340         }
341         return $retar;
345 function getRecPatientData ($pid) {
346         //data is returned as a multi-level array:
347         //column name->dates->values
348         //$return{"lname"}[0..n]{"date"}
349         //$return{"lname"}[0..n]{"value"}
350         $res = sqlStatement("select * from patient_data where pid='$pid' order by date");
351         
352         while($result = sqlFetchArray($res)) {
353                 foreach ($result as $key => $val) {
354                         if ($key == "pid" || $key == "date" || $key == "id") {
355                                 next;
356                         } else {
357                                 $curdate = $result{"date"};
358                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
359                                         $arcount{$key}++;
360                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
361                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
362                                 }
363                         }
364                 }
365         }
366         return $retar;
370 function getRecInsuranceData ($pid, $ins_type) {
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 *, 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");
376         
377         while($result = sqlFetchArray($res)) {
378                 foreach ($result as $key => $val) {
379                         if ($key == "pid" || $key == "date" || $key == "id") {
380                                 next;
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         }
391         return $retar;
395 function printRecData($data_array, $recres, $N) {
396         //this function generates a formatted history of all changes to the data
397         //it is a multi-level recursive function that exhaustively displays all of
398         //the changes, with dates, of any data in the database under the given
399         //argument restrictions.
400         //$data_array is an array with table_column_name => "display name"
401         //$recres is the return from getRecPatientData for example
402         //$N is the number of items to display in one row
403         print "<table><tr>\n";
404         $count = 0;
405         foreach ($data_array as $akey => $aval) {
406                 if ($count == $N) {
407                         print "</tr><tr>\n";
408                         $count = 0;
409                 }
410                 print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
411                 printData($recres, $akey, "<br>", "Y-m-d");
412                 print "</span></td>\n";
413                 $count++;
414         }
415         print "</tr></table>\n";
421 function printData ($retar, $key, $sep, $date_format) {
422         //$retar{$key}
423         if (@array_key_exists($key,$retar)) {
424                 $length = sizeof($retar{$key});
425                 for ($iter = $length;$iter>=1;$iter--) {
426                         if ($retar{$key}[$iter]{"value"} != "0000-00-00 00:00:00") {
427                                 print $retar{$key}[$iter]{"value"} . " (" . date($date_format,strtotime($retar{$key}[$iter]{"date"})) . ")$sep";
428                         }
429                 }
430         }
433 function printRecDataOne($data_array, $recres, $N) {
434         //this function is like printRecData except it will only print out those elements that
435         //have values. when they do have values, this function will only print out the most recent
436         //value of each element.
437         //this may be considered a compressed data viewer.
438         //this function generates a formatted history of all changes to the data
439         //$data_array is an array with table_column_name => "display name"
440         //$recres is the return from getRecPatientData for example
441         //$N is the number of items to display in one row
442         print "<table><tr>\n";
443         $count = 0;
444         foreach ($data_array as $akey => $aval) {
445                 if (sizeof($recres{$akey})>0 && ($recres{$akey}[1]{"value"}!="0000-00-00 00:00:00")) {
446                         if ($count == $N) {
447                                 print "</tr><tr>\n";
448                                 $count = 0;
449                         }
450                         print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
451                         printDataOne($recres, $akey, "<br>", "Y-m-d");
452                         print "</span></td>\n";
453                         $count++;
454                 }
455         }
456         print "</tr></table>\n";
462 function printDataOne ($retar, $key, $sep, $date_format) {
463         //this function supports the printRecDataOne function above
464         if (@array_key_exists($key,$retar)) {
465                 $length = sizeof($retar{$key});
466                 if ($retar{$key}[$length]{"value"} != "0000-00-00 00:00:00") {
467                         //print $retar{$key}[$length]{"value"} . " (" . date($date_format,strtotime($retar{$key}[$length]{"date"})) . "$sep";
468                         print $retar{$key}[$length]{"value"} . "$sep";
469                 }
470         }