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