use default durations from categories
[openemr.git] / library / report.inc
blobfe38ea5d68e605ad55d326666e38fd3c176def01
1 <?php
2 require_once("{$GLOBALS['srcdir']}/sql.inc");
4 $patient_data_array = array(title => "Title: ",
5 fname => "First Name: ",
6 mname => "Middle Name: ",
7 lname => "Last Name: ",
8 sex => "Sex",
9 ss => "SS: ",
10 DOB => "Date of Birth: ",
11 street => "Street: ",
12 city => "City: ",
13 state => "State: ",
14 postal_code => "Zip: ",
15 country_code => "Country: ",
16 occupation => "Occupation: ",
17 phone_home => "Home Phone: ",
18 phone_biz => "Business Phone: ",
19 phone_contact => "Contact Phone: ",
20 contact_relationship => "Contact Person",
21 hipaa_mail => "Allows Mail: ",
22 hipaa_voice => "Allows Voice msgs: "
26 $history_data_array = array(
27 coffee => "Coffee Use: ",
28 tobacco => "Tobacco Use: ",
29 alcohol => "Alcohol Use: ",
30 sleep_patterns => "Sleep Patterns: ",
31 exercise_patterns => "Exercise Patterns: ",
32 seatbelt_use => "Seatbelt Use: ",
33 counseling => "Counseling: ",
34 hazardous_activities => "Hazardous Activities: ",
35 last_breast_exam => "Last Breast Exam: ",
36 last_mammogram => "Last Mammogram: ",
37 last_gynocological_exam => "Last Gyn. Exam: ",
38 last_rectal_exam => "Last Rectal Exam: ",
39 last_prostate_exam => "Last Prostate Exam: ",
40 last_physical_exam => "Last Physical Exam: ",
41 last_sigmoidoscopy_colonoscopy => "Last Sigmoid/Colonoscopy: ",
42 cataract_surgery => "Last Cataract Surgery: ",
43 tonsillectomy => "Last Tonsillectomy: ",
44 cholecystestomy => "Last Cholecystestomy: ",
45 heart_surgery => "Last Heart Surgery: ",
46 hysterectomy => "Last Hysterectomy: ",
47 hernia_repair => "Last Hernia Repair: ",
48 hip_replacement => "Last Hip Replacement: ",
49 knee_replacement => "Last Knee Replacement: ",
50 appendectomy => "Last Appendectomy: ",
51 history_mother => "Mother's History: ",
52 history_father => "Father's History: ",
53 history_siblings => "Sibling History: ",
54 history_offspring => "Offspring History: ",
55 history_spouse => "Spouse's History: ",
56 relatives_cancer => "Relatives Cancer: ",
57 relatives_tuberculosis => "Relatives Tuberculosis: ",
58 relatives_diabetes => "Relatives Diabetes: ",
59 relatives_high_blood_pressure => "Relatives Blood Pressure: ",
60 relatives_heart_problems => "Relatives Heart: ",
61 relatives_stroke => "Relatives Stroke: ",
62 relatives_epilepsy => "Relatives Epilepsy: ",
63 relatives_mental_illness => "Relatives Mental Illness: ",
64 relatives_suicide => "Relatives Suicide: ");
66 $employer_data_array = array(
67 name => "Employer: ",
68 street => "Address: ",
69 city => "City: ",
70 postal_code => "Zip: ",
71 state => "State",
72 country => "Country: ");
74 $insurance_data_array = array(
75 provider_name => "Provider: ",
76 plan_name => "Plan Name: ",
77 policy_number => "Policy Number: ",
78 group_number => "Group Number: ",
79 subscriber_fname => "Subscriber First Name: ",
80 subscriber_mname => "Subscriber Middle Name: ",
81 subscriber_lname => "Subscriber Last Name: ",
82 subscriber_relationship => "Subscriber Relationship: ",
83 subscriber_ss => "Subscriber SS: ",
84 subscriber_DOB => "Subscriber Date of Birth: ",
85 subscriber_phone => "Subscribter Phone: ",
86 subscriber_street => "Subscriber Address: ",
87 subscriber_postal_code => "Subscriber Zip: ",
88 subscriber_city => "Subscriber City: ",
89 subscriber_state => "Subscriber State: ",
90 subscriber_country => "Subscriber Country: ",
91 subscriber_employer => "Subscriber Employer: ",
92 subscriber_employer_street => "Subscriber Employer Street: ",
93 subscriber_employer_city => "Subscriber Employer City: ",
94 subscriber_employer_postal_code => "Subscriber Employer Zip: ",
95 subscriber_employer_state => "Subscriber Employer State: ",
96 subscriber_employer_country => "Subscriber Employer Country: "
108 function getPatientReport($pid)
110         $sql = "select * from patient_data where pid='$pid' order by date ASC";
111         $res = sqlStatement("$sql");
112         while($list = sqlFetchArray($res))
113         {
114                 while(list($key, $value) = each($list))
115                 {
116                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
117                         {
118                                 $ret[$key]['title'] = $key;
119                                 $ret[$key]['content'] = $value;
120                                 $ret[$key]['date'] = $list['date'];
121                         }
122                 }
123         }
124         return $ret;
127 function getHistoryReport($pid)
129         $sql = "select * from history_data where pid='$pid' order by date ASC";
130         $res = sqlStatement("$sql");
131         while($list = sqlFetchArray($res))
132         {
133                 while(list($key, $value) = each($list))
134                 {
135                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
136                         {
137                                 $ret[$key]['content'] = $value;
138                                 $ret[$key]['date'] = $list['date'];
139                         }
140                 }
141         }
142         return $ret;
145 function getInsuranceReport($pid, $type = "primary")
147         $sql = "select * from insurance_data where pid='$pid' and type='$type' order by date ASC";
148         $res = sqlStatement("$sql");
149         while($list = sqlFetchArray($res))
150         {
151                 while(list($key, $value) = each($list))
152                 {
153                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
154                         {
155                                 $ret[$key]['content'] = $value;
156                                 $ret[$key]['date'] = $list['date'];
157                         }
158                 }
159         }
160         return $ret;
163 function getEmployerReport($pid)
165         $sql = "select * from employer_data where pid='$pid' order by date ASC";
166         $res = sqlStatement("$sql");
167         while($list = sqlFetchArray($res))
168         {
169                 while(list($key, $value) = each($list))
170                 {
171                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
172                         {
173                                 $ret[$key]['content'] = $value;
174                                 $ret[$key]['date'] = $list['date'];
175                         }
176                 }
177         }
178         return $ret;
181 function getListsReport($pid)
183         $sql = "select * from lists where id='$id' order by date ASC";
184         $res = sqlStatement("$sql");
185         while($list = sqlFetchArray($res))
186         {
187                 while(list($key, $value) = each($list))
188                 {
189                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
190                         {
191                                 $ret[$key]['content'] = $value;
192                                 $ret[$key]['date'] = $list['date'];
193                         }
194                 }
195         }
196         return $ret;
199 function printListData($pid, $list_type, $list_activity = "%") {
200         $res = sqlStatement("select * from lists where pid='$pid' and type='$list_type' and activity like '$list_activity' order by date");
201         while($result = sqlFetchArray($res)) {
202                 print "<span class=bold>" . $result{"title"} . ":</span><span class=text> " . $result{"comments"} . "</span><br>\n";
203         }
206 function printPatientNotes($pid) {
207         $res = sqlStatement("select * from pnotes where pid='$pid' and activity=1 order by date");
208         while($result = sqlFetchArray($res)) {
209                 print "<span class=bold>" . date("Y-m-d",strtotime($result{"date"})) . ":</span><span class=text> " . stripslashes($result{"body"}) . "</span><br>\n";
210         }
213 function printPatientTransactions($pid) {
214         $res = sqlStatement("select * from transactions where pid='$pid' order by date");
215         while($result = sqlFetchArray($res)) {
216                 print "<span class=bold>" . date("Y-m-d",strtotime($result{"date"})) . ":</span><span class=text>(".$result{"title"}.") " . stripslashes($result{"body"}) . "</span><br>\n";
217         }
221 function printPatientBilling($pid) {
222         $res = sqlStatement("select * from billing where pid='$pid' order by date");
223         while($result = sqlFetchArray($res)) {
224                 print "<span class=bold>" . date("Y-m-d",strtotime($result{"date"})) . ":</span><span class=text>(".$result{"code_type"}.") " . $result{"code"} . "</span><br>\n";
225         }
228 function getPatientBillingEncounter($pid,$encounter) {
229         $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";
230         $res = sqlStatement($sql);
231         $billings = array();
232         while($result = sqlFetchArray($res)) {
233                 $billings[] = $result;
234         }
235         return $billings;
239 function printPatientForms($pid, $cols) {
240         //this function takes a $pid
241         $inclookupres = sqlStatement("select distinct formdir from forms where pid='$pid'");
242         while($result = sqlFetchArray($inclookupres)) {
243                 include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
244         }
245         
246         $isfirst=1;
247         $res = sqlStatement("select * from forms where pid='$pid' order by date");
248         while($result = sqlFetchArray($res)) {
249                 if ($result{"form_name"} == "New Patient Encounter") {
250                         if ($isfirst==0) {
251                         print "</blockquote>\n\n";
252                         }
253                         $isfirst=0;
254                         print "<span class=bold>".$result{"form_name"}." </span><span class=text>(".date("Y-m-d",strtotime($result{"date"})).")</span><br><blockquote>\n";
255                 } else {
256                         print "<span class=bold>".$result{"form_name"}." </span><span class=text>(".date("Y-m-d",strtotime($result{"date"})).")</span><br>\n";
257                 }
258                 call_user_func($result{"formdir"} . "_report", $pid, $result{"encounter"}, $cols, $result{"form_id"});
259                 
260         }
264 function getRecHistoryData ($pid) {
265         //data is returned as a multi-level array:
266         //column name->dates->values
267         //$return{"lname"}[0..n]{"date"}
268         //$return{"lname"}[0..n]{"value"}
269         $res = sqlStatement("select * from history_data where pid='$pid' order by date");
270         
271         while($result = sqlFetchArray($res)) {
272                 foreach ($result as $key => $val) {
273                         if ($key == "pid" || $key == "date" || $key == "id") {
274                                 next;
275                         } else {
276                                 $curdate = $result{"date"};
277                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
278                                         $arcount{$key}++;
279                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
280                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
281                                 }
282                         }
283                 }
284         }
285         return $retar;
288 function getRecEmployerData ($pid) {
289         //data is returned as a multi-level array:
290         //column name->dates->values
291         //$return{"lname"}[0..n]{"date"}
292         //$return{"lname"}[0..n]{"value"}
293         $res = sqlStatement("select * from employer_data where pid='$pid' order by date");
294         
295         while($result = sqlFetchArray($res)) {
296                 foreach ($result as $key => $val) {
297                         if ($key == "pid" || $key == "date" || $key == "id") {
298                                 next;
299                         } else {
300                                 $curdate = $result{"date"};
301                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
302                                         $arcount{$key}++;
303                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
304                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
305                                 }
306                         }
307                 }
308         }
309         return $retar;
313 function getRecPatientData ($pid) {
314         //data is returned as a multi-level array:
315         //column name->dates->values
316         //$return{"lname"}[0..n]{"date"}
317         //$return{"lname"}[0..n]{"value"}
318         $res = sqlStatement("select * from patient_data where pid='$pid' order by date");
319         
320         while($result = sqlFetchArray($res)) {
321                 foreach ($result as $key => $val) {
322                         if ($key == "pid" || $key == "date" || $key == "id") {
323                                 next;
324                         } else {
325                                 $curdate = $result{"date"};
326                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
327                                         $arcount{$key}++;
328                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
329                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
330                                 }
331                         }
332                 }
333         }
334         return $retar;
338 function getRecInsuranceData ($pid, $ins_type) {
339         //data is returned as a multi-level array:
340         //column name->dates->values
341         //$return{"lname"}[0..n]{"date"}
342         //$return{"lname"}[0..n]{"value"}
343         $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");
344         
345         while($result = sqlFetchArray($res)) {
346                 foreach ($result as $key => $val) {
347                         if ($key == "pid" || $key == "date" || $key == "id") {
348                                 next;
349                         } else {
350                                 $curdate = $result{"date"};
351                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
352                                         $arcount{$key}++;
353                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
354                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
355                                 }
356                         }
357                 }
358         }
359         return $retar;
363 function printRecData($data_array, $recres, $N) {
364         //this function generates a formatted history of all changes to the data
365         //it is a multi-level recursive function that exhaustively displays all of
366         //the changes, with dates, of any data in the database under the given
367         //argument restrictions.
368         //$data_array is an array with table_column_name => "display name"
369         //$recres is the return from getRecPatientData for example
370         //$N is the number of items to display in one row
371         print "<table><tr>\n";
372         $count = 0;
373         foreach ($data_array as $akey => $aval) {
374                 if ($count == $N) {
375                         print "</tr><tr>\n";
376                         $count = 0;
377                 }
378                 print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
379                 printData($recres, $akey, "<br>", "Y-m-d");
380                 print "</span></td>\n";
381                 $count++;
382         }
383         print "</tr></table>\n";
389 function printData ($retar, $key, $sep, $date_format) {
390         //$retar{$key}
391         if (@array_key_exists($key,$retar)) {
392                 $length = sizeof($retar{$key});
393                 for ($iter = $length;$iter>=1;$iter--) {
394                         if ($retar{$key}[$iter]{"value"} != "0000-00-00 00:00:00") {
395                                 print $retar{$key}[$iter]{"value"} . " (" . date($date_format,strtotime($retar{$key}[$iter]{"date"})) . ")$sep";
396                         }
397                 }
398         }
401 function printRecDataOne($data_array, $recres, $N) {
402         //this function is like printRecData except it will only print out those elements that
403         //have values. when they do have values, this function will only print out the most recent
404         //value of each element.
405         //this may be considered a compressed data viewer.
406         //this function generates a formatted history of all changes to the data
407         //$data_array is an array with table_column_name => "display name"
408         //$recres is the return from getRecPatientData for example
409         //$N is the number of items to display in one row
410         print "<table><tr>\n";
411         $count = 0;
412         foreach ($data_array as $akey => $aval) {
413                 if (sizeof($recres{$akey})>0 && ($recres{$akey}[1]{"value"}!="0000-00-00 00:00:00")) {
414                         if ($count == $N) {
415                                 print "</tr><tr>\n";
416                                 $count = 0;
417                         }
418                         print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
419                         printDataOne($recres, $akey, "<br>", "Y-m-d");
420                         print "</span></td>\n";
421                         $count++;
422                 }
423         }
424         print "</tr></table>\n";
430 function printDataOne ($retar, $key, $sep, $date_format) {
431         //this function supports the printRecDataOne function above
432         if (@array_key_exists($key,$retar)) {
433                 $length = sizeof($retar{$key});
434                 if ($retar{$key}[$length]{"value"} != "0000-00-00 00:00:00") {
435                         //print $retar{$key}[$length]{"value"} . " (" . date($date_format,strtotime($retar{$key}[$length]{"date"})) . "$sep";
436                         print $retar{$key}[$length]{"value"} . "$sep";
437                 }
438         }