fixed bug in setting pid
[openemr.git] / library / report.inc
blob339f8aeae4a69a8be421664287fdb54897fb5fcb
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");
22 $history_data_array = array(
23 coffee => "Coffee Use: ",
24 tobacco => "Tobacco Use: ",
25 alcohol => "Alcohol Use: ",
26 sleep_patterns => "Sleep Patterns: ",
27 exercise_patterns => "Exercise Patterns: ",
28 seatbelt_use => "Seatbelt Use: ",
29 counseling => "Counseling: ",
30 hazardous_activities => "Hazardous Activities: ",
31 last_breast_exam => "Last Breast Exam: ",
32 last_mammogram => "Last Mammogram: ",
33 last_gynocological_exam => "Last Gyn. Exam: ",
34 last_rectal_exam => "Last Rectal Exam: ",
35 last_prostate_exam => "Last Prostate Exam: ",
36 last_physical_exam => "Last Physical Exam: ",
37 last_sigmoidoscopy_colonoscopy => "Last Sigmoid/Colonoscopy: ",
38 cataract_surgery => "Last Cataract Surgery: ",
39 tonsillectomy => "Last Tonsillectomy: ",
40 cholecystestomy => "Last Cholecystestomy: ",
41 heart_surgery => "Last Heart Surgery: ",
42 hysterectomy => "Last Hysterectomy: ",
43 hernia_repair => "Last Hernia Repair: ",
44 hip_replacement => "Last Hip Replacement: ",
45 knee_replacement => "Last Knee Replacement: ",
46 appendectomy => "Last Appendectomy: ",
47 history_mother => "Mother's History: ",
48 history_father => "Father's History: ",
49 history_siblings => "Sibling History: ",
50 history_offspring => "Offspring History: ",
51 history_spouse => "Spouse's History: ",
52 relatives_cancer => "Relatives Cancer: ",
53 relatives_tuberculosis => "Relatives Tuberculosis: ",
54 relatives_diabetes => "Relatives Diabetes: ",
55 relatives_high_blood_pressure => "Relatives Blood Pressure: ",
56 relatives_heart_problems => "Relatives Heart: ",
57 relatives_stroke => "Relatives Stroke: ",
58 relatives_epilepsy => "Relatives Epilepsy: ",
59 relatives_mental_illness => "Relatives Mental Illness: ",
60 relatives_suicide => "Relatives Suicide: ");
62 $employer_data_array = array(
63 name => "Employer: ",
64 street => "Address: ",
65 city => "City: ",
66 postal_code => "Zip: ",
67 state => "State",
68 country => "Country: ");
70 $insurance_data_array = array(
71 provider_name => "Provider: ",
72 plan_name => "Plan Name: ",
73 policy_number => "Policy Number: ",
74 group_number => "Group Number: ",
75 subscriber_fname => "Subscriber First Name: ",
76 subscriber_mname => "Subscriber Middle Name: ",
77 subscriber_lname => "Subscriber Last Name: ",
78 subscriber_relationship => "Subscriber Relationship: ",
79 subscriber_ss => "Subscriber SS: ",
80 subscriber_DOB => "Subscriber Date of Birth: ",
81 subscriber_phone => "Subscribter Phone: ",
82 subscriber_street => "Subscriber Address: ",
83 subscriber_postal_code => "Subscriber Zip: ",
84 subscriber_city => "Subscriber City: ",
85 subscriber_state => "Subscriber State: ",
86 subscriber_country => "Subscriber Country: ",
87 subscriber_employer => "Subscriber Employer: ",
88 subscriber_employer_street => "Subscriber Employer Street: ",
89 subscriber_employer_city => "Subscriber Employer City: ",
90 subscriber_employer_postal_code => "Subscriber Employer Zip: ",
91 subscriber_employer_state => "Subscriber Employer State: ",
92 subscriber_employer_country => "Subscriber Employer Country: "
104 function getPatientReport($pid)
106         $sql = "select * from patient_data where pid='$pid' order by date ASC";
107         $res = sqlStatement("$sql");
108         while($list = sqlFetchArray($res))
109         {
110                 while(list($key, $value) = each($list))
111                 {
112                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
113                         {
114                                 $ret[$key]['title'] = $key;
115                                 $ret[$key]['content'] = $value;
116                                 $ret[$key]['date'] = $list['date'];
117                         }
118                 }
119         }
120         return $ret;
123 function getHistoryReport($pid)
125         $sql = "select * from history_data where pid='$pid' order by date ASC";
126         $res = sqlStatement("$sql");
127         while($list = sqlFetchArray($res))
128         {
129                 while(list($key, $value) = each($list))
130                 {
131                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
132                         {
133                                 $ret[$key]['content'] = $value;
134                                 $ret[$key]['date'] = $list['date'];
135                         }
136                 }
137         }
138         return $ret;
141 function getInsuranceReport($pid, $type = "primary")
143         $sql = "select * from insurance_data where pid='$pid' and type='$type' order by date ASC";
144         $res = sqlStatement("$sql");
145         while($list = sqlFetchArray($res))
146         {
147                 while(list($key, $value) = each($list))
148                 {
149                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
150                         {
151                                 $ret[$key]['content'] = $value;
152                                 $ret[$key]['date'] = $list['date'];
153                         }
154                 }
155         }
156         return $ret;
159 function getEmployerReport($pid)
161         $sql = "select * from employer_data where pid='$pid' order by date ASC";
162         $res = sqlStatement("$sql");
163         while($list = sqlFetchArray($res))
164         {
165                 while(list($key, $value) = each($list))
166                 {
167                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
168                         {
169                                 $ret[$key]['content'] = $value;
170                                 $ret[$key]['date'] = $list['date'];
171                         }
172                 }
173         }
174         return $ret;
177 function getListsReport($pid)
179         $sql = "select * from lists where id='$id' order by date ASC";
180         $res = sqlStatement("$sql");
181         while($list = sqlFetchArray($res))
182         {
183                 while(list($key, $value) = each($list))
184                 {
185                         if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
186                         {
187                                 $ret[$key]['content'] = $value;
188                                 $ret[$key]['date'] = $list['date'];
189                         }
190                 }
191         }
192         return $ret;
195 function printListData($pid, $list_type, $list_activity = "%") {
196         $res = sqlStatement("select * from lists where pid='$pid' and type='$list_type' and activity like '$list_activity' order by date");
197         while($result = sqlFetchArray($res)) {
198                 print "<span class=bold>" . $result{"title"} . ":</span><span class=text> " . $result{"comments"} . "</span><br>\n";
199         }
202 function printPatientNotes($pid) {
203         $res = sqlStatement("select * from pnotes where pid='$pid' and activity=1 order by date");
204         while($result = sqlFetchArray($res)) {
205                 print "<span class=bold>" . date("Y-m-d",strtotime($result{"date"})) . ":</span><span class=text> " . stripslashes($result{"body"}) . "</span><br>\n";
206         }
209 function printPatientTransactions($pid) {
210         $res = sqlStatement("select * from transactions where pid='$pid' order by date");
211         while($result = sqlFetchArray($res)) {
212                 print "<span class=bold>" . date("Y-m-d",strtotime($result{"date"})) . ":</span><span class=text>(".$result{"title"}.") " . stripslashes($result{"body"}) . "</span><br>\n";
213         }
217 function printPatientBilling($pid) {
218         $res = sqlStatement("select * from billing 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{"code_type"}.") " . $result{"code"} . "</span><br>\n";
221         }
224 function getPatientBillingEncounter($pid,$encounter) {
225         $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";
226         $res = sqlStatement($sql);
227         $billings = array();
228         while($result = sqlFetchArray($res)) {
229                 $billings[] = $result;
230         }
231         return $billings;
235 function printPatientForms($pid, $cols) {
236         //this function takes a $pid
237         $inclookupres = sqlStatement("select distinct formdir from forms where pid='$pid'");
238         while($result = sqlFetchArray($inclookupres)) {
239                 include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
240         }
241         
242         $isfirst=1;
243         $res = sqlStatement("select * from forms where pid='$pid' order by date");
244         while($result = sqlFetchArray($res)) {
245                 if ($result{"form_name"} == "New Patient Encounter") {
246                         if ($isfirst==0) {
247                         print "</blockquote>\n\n";
248                         }
249                         $isfirst=0;
250                         print "<span class=bold>".$result{"form_name"}." </span><span class=text>(".date("Y-m-d",strtotime($result{"date"})).")</span><br><blockquote>\n";
251                 } else {
252                         print "<span class=bold>".$result{"form_name"}." </span><span class=text>(".date("Y-m-d",strtotime($result{"date"})).")</span><br>\n";
253                 }
254                 call_user_func($result{"formdir"} . "_report", $pid, $result{"encounter"}, $cols, $result{"form_id"});
255                 
256         }
260 function getRecHistoryData ($pid) {
261         //data is returned as a multi-level array:
262         //column name->dates->values
263         //$return{"lname"}[0..n]{"date"}
264         //$return{"lname"}[0..n]{"value"}
265         $res = sqlStatement("select * from history_data where pid='$pid' order by date");
266         
267         while($result = sqlFetchArray($res)) {
268                 foreach ($result as $key => $val) {
269                         if ($key == "pid" || $key == "date" || $key == "id") {
270                                 next;
271                         } else {
272                                 $curdate = $result{"date"};
273                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
274                                         $arcount{$key}++;
275                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
276                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
277                                 }
278                         }
279                 }
280         }
281         return $retar;
284 function getRecEmployerData ($pid) {
285         //data is returned as a multi-level array:
286         //column name->dates->values
287         //$return{"lname"}[0..n]{"date"}
288         //$return{"lname"}[0..n]{"value"}
289         $res = sqlStatement("select * from employer_data where pid='$pid' order by date");
290         
291         while($result = sqlFetchArray($res)) {
292                 foreach ($result as $key => $val) {
293                         if ($key == "pid" || $key == "date" || $key == "id") {
294                                 next;
295                         } else {
296                                 $curdate = $result{"date"};
297                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
298                                         $arcount{$key}++;
299                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
300                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
301                                 }
302                         }
303                 }
304         }
305         return $retar;
309 function getRecPatientData ($pid) {
310         //data is returned as a multi-level array:
311         //column name->dates->values
312         //$return{"lname"}[0..n]{"date"}
313         //$return{"lname"}[0..n]{"value"}
314         $res = sqlStatement("select * from patient_data where pid='$pid' order by date");
315         
316         while($result = sqlFetchArray($res)) {
317                 foreach ($result as $key => $val) {
318                         if ($key == "pid" || $key == "date" || $key == "id") {
319                                 next;
320                         } else {
321                                 $curdate = $result{"date"};
322                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
323                                         $arcount{$key}++;
324                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
325                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
326                                 }
327                         }
328                 }
329         }
330         return $retar;
334 function getRecInsuranceData ($pid, $ins_type) {
335         //data is returned as a multi-level array:
336         //column name->dates->values
337         //$return{"lname"}[0..n]{"date"}
338         //$return{"lname"}[0..n]{"value"}
339         $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");
340         
341         while($result = sqlFetchArray($res)) {
342                 foreach ($result as $key => $val) {
343                         if ($key == "pid" || $key == "date" || $key == "id") {
344                                 next;
345                         } else {
346                                 $curdate = $result{"date"};
347                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
348                                         $arcount{$key}++;
349                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
350                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
351                                 }
352                         }
353                 }
354         }
355         return $retar;
359 function printRecData($data_array, $recres, $N) {
360         //this function generates a formatted history of all changes to the data
361         //it is a multi-level recursive function that exhaustively displays all of
362         //the changes, with dates, of any data in the database under the given
363         //argument restrictions.
364         //$data_array is an array with table_column_name => "display name"
365         //$recres is the return from getRecPatientData for example
366         //$N is the number of items to display in one row
367         print "<table><tr>\n";
368         $count = 0;
369         foreach ($data_array as $akey => $aval) {
370                 if ($count == $N) {
371                         print "</tr><tr>\n";
372                         $count = 0;
373                 }
374                 print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
375                 printData($recres, $akey, "<br>", "Y-m-d");
376                 print "</span></td>\n";
377                 $count++;
378         }
379         print "</tr></table>\n";
385 function printData ($retar, $key, $sep, $date_format) {
386         //$retar{$key}
387         if (@array_key_exists($key,$retar)) {
388                 $length = sizeof($retar{$key});
389                 for ($iter = $length;$iter>=1;$iter--) {
390                         if ($retar{$key}[$iter]{"value"} != "0000-00-00 00:00:00") {
391                                 print $retar{$key}[$iter]{"value"} . " (" . date($date_format,strtotime($retar{$key}[$iter]{"date"})) . ")$sep";
392                         }
393                 }
394         }
397 function printRecDataOne($data_array, $recres, $N) {
398         //this function is like printRecData except it will only print out those elements that
399         //have values. when they do have values, this function will only print out the most recent
400         //value of each element.
401         //this may be considered a compressed data viewer.
402         //this function generates a formatted history of all changes to the data
403         //$data_array is an array with table_column_name => "display name"
404         //$recres is the return from getRecPatientData for example
405         //$N is the number of items to display in one row
406         print "<table><tr>\n";
407         $count = 0;
408         foreach ($data_array as $akey => $aval) {
409                 if (sizeof($recres{$akey})>0 && ($recres{$akey}[1]{"value"}!="0000-00-00 00:00:00")) {
410                         if ($count == $N) {
411                                 print "</tr><tr>\n";
412                                 $count = 0;
413                         }
414                         print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
415                         printDataOne($recres, $akey, "<br>", "Y-m-d");
416                         print "</span></td>\n";
417                         $count++;
418                 }
419         }
420         print "</tr></table>\n";
426 function printDataOne ($retar, $key, $sep, $date_format) {
427         //this function supports the printRecDataOne function above
428         if (@array_key_exists($key,$retar)) {
429                 $length = sizeof($retar{$key});
430                 if ($retar{$key}[$length]{"value"} != "0000-00-00 00:00:00") {
431                         //print $retar{$key}[$length]{"value"} . " (" . date($date_format,strtotime($retar{$key}[$length]{"date"})) . "$sep";
432                         print $retar{$key}[$length]{"value"} . "$sep";
433                 }
434         }