more internationalization and input validation project
[openemr.git] / library / report.inc
blob9e1e705c568aa13a27ed383d5ace12126e3ed369
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   /*******************************************************************
236         $sql = "SELECT billing.*, u.id, u.fname, u.mname, u.lname, " .
237     "CONCAT(u.fname,' ', u.lname) AS provider_name, u.federaltaxid " .
238     "FROM billing LEFT JOIN users AS u ON u.id = billing.provider_id " .
239     "WHERE pid='" . mysql_real_escape_string($pid) . "' AND " .
240     "encounter = '" . mysql_real_escape_string($encounter) .
241     "' AND activity='1' ORDER BY date";
242   *******************************************************************/
243   $erow = sqlQuery("SELECT provider_id FROM form_encounter WHERE " .
244     "pid = '$pid' AND encounter = '$encounter' " .
245     "ORDER BY id DESC LIMIT 1");
246   $inv_provider = $erow['provider_id'] + 0;
247         $sql = "SELECT b.*, u.id, u.fname, u.mname, u.lname, " .
248     "CONCAT(u.fname,' ', u.lname) AS provider_name, u.federaltaxid " .
249     "FROM billing AS b " .
250     "LEFT JOIN users AS u ON " .
251     "( b.provider_id != 0 AND u.id = b.provider_id ) OR " .
252     "( b.provider_id  = 0 AND u.id = $inv_provider ) " .
253     "WHERE pid='" . mysql_real_escape_string($pid) . "' AND " .
254     "encounter = '" . mysql_real_escape_string($encounter) .
255     "' AND activity = '1' ORDER BY date";
256   /******************************************************************/
257         $res = sqlStatement($sql);
258         $billings = array();
259         while($result = sqlFetchArray($res)) {
260                 $billings[] = $result;
261         }
262         return $billings;
265 function printPatientForms($pid, $cols) {
266     //this function takes a $pid
267     $inclookupres = sqlStatement("select distinct formdir from forms where pid='$pid' AND deleted=0");
268     while($result = sqlFetchArray($inclookupres)) {
269         include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
270     }
271         
272     $res = sqlStatement("select * from forms where pid='$pid' AND deleted=0 order by date");
273     while($result = sqlFetchArray($res)) {
274         if ($result{"form_name"} == "New Patient Encounter") {
275             echo "<div class='text encounter'>\n";
276             echo "<h1>" . $result["form_name"] . "</h1>";
278             // display the provider info
279             $tmp = sqlQuery("SELECT u.title, u.fname, u.mname, u.lname " .
280                                     "FROM forms AS f, users AS u WHERE " .
281                                     "f.pid = '$pid' AND f.encounter = ".$result['encounter']." AND " .
282                                     "f.formdir = 'newpatient' AND u.username = f.user " .
283                                     " AND f.deleted=0 ". //--JRM--
284                                     "ORDER BY f.id LIMIT 1");
285             echo " ".xl('Provider').": ".$tmp['title']." ".
286                 $tmp['fname']." ".$tmp['mname']." ".$tmp['lname'];
287             echo "<br/>";
288         }
289         else {
290             echo "<div class='text encounter_form'>";
291             echo "<h1>" . $result["form_name"] . "</h1>";
292         }
293         echo "(" . date("Y-m-d",strtotime($result["date"])) . ") ";
295         if (acl_check('acct', 'rep') || acl_check('acct', 'eob') || acl_check('acct', 'bill')) {
296             if ($result{"form_name"} == "New Patient Encounter") {
297                 // display billing info
298                 echo "<br/>";
299                 $bres = sqlStatement("SELECT date, code, code_text FROM billing WHERE " .
300                                         "pid = '$pid' AND encounter = '".$result['encounter']."' AND activity = 1 AND " .
301                                         "( code_type = 'CPT4' OR code_type = 'OPCS' OR code_type = 'OSICS10' ) " .
302                                         "ORDER BY date");
303                 while ($brow=sqlFetchArray($bres)) {
304                     echo "<span class='bold'>&nbsp;".xl('Procedure').": </span><span class='text'>" .
305                         $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
306                 }
307             }
308         }
310         call_user_func($result{"formdir"} . "_report", $pid, $result{"encounter"}, $cols, $result{"form_id"});
312         echo "</div>";
313     }
317 function getRecHistoryData ($pid) {
318         //data is returned as a multi-level array:
319         //column name->dates->values
320         //$return{"lname"}[0..n]{"date"}
321         //$return{"lname"}[0..n]{"value"}
322         $res = sqlStatement("select * from history_data where pid='$pid' order by date");
323         
324         while($result = sqlFetchArray($res)) {
325                 foreach ($result as $key => $val) {
326                         if ($key == "pid" || $key == "date" || $key == "id") {
327                                 next;
328                         } else {
329                                 $curdate = $result{"date"};
330                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
331                                         $arcount{$key}++;
332                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
333                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
334                                 }
335                         }
336                 }
337         }
338         return $retar;
341 function getRecEmployerData ($pid) {
342         //data is returned as a multi-level array:
343         //column name->dates->values
344         //$return{"lname"}[0..n]{"date"}
345         //$return{"lname"}[0..n]{"value"}
346         $res = sqlStatement("select * from employer_data where pid='$pid' order by date");
347         
348         while($result = sqlFetchArray($res)) {
349                 foreach ($result as $key => $val) {
350                         if ($key == "pid" || $key == "date" || $key == "id") {
351                                 next;
352                         } else {
353                                 $curdate = $result{"date"};
354                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
355                                         $arcount{$key}++;
356                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
357                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
358                                 }
359                         }
360                 }
361         }
362         return $retar;
366 function getRecPatientData ($pid) {
367         //data is returned as a multi-level array:
368         //column name->dates->values
369         //$return{"lname"}[0..n]{"date"}
370         //$return{"lname"}[0..n]{"value"}
371         $res = sqlStatement("select * from patient_data where pid='$pid' order by date");
372         
373         while($result = sqlFetchArray($res)) {
374                 foreach ($result as $key => $val) {
375                         if ($key == "pid" || $key == "date" || $key == "id") {
376                                 next;
377                         } else {
378                                 $curdate = $result{"date"};
379                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
380                                         $arcount{$key}++;
381                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
382                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
383                                 }
384                         }
385                 }
386         }
387         return $retar;
391 function getRecInsuranceData ($pid, $ins_type) {
392         //data is returned as a multi-level array:
393         //column name->dates->values
394         //$return{"lname"}[0..n]{"date"}
395         //$return{"lname"}[0..n]{"value"}
396         $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");
397         
398         while($result = sqlFetchArray($res)) {
399                 foreach ($result as $key => $val) {
400                         if ($key == "pid" || $key == "date" || $key == "id") {
401                                 next;
402                         } else {
403                                 $curdate = $result{"date"};
404                                 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
405                                         $arcount{$key}++;
406                                         $retar{$key}[$arcount{$key}]{"value"} = $val;
407                                         $retar{$key}[$arcount{$key}]{"date"} = $curdate;
408                                 }
409                         }
410                 }
411         }
412         return $retar;
416 function printRecData($data_array, $recres, $N) {
417         //this function generates a formatted history of all changes to the data
418         //it is a multi-level recursive function that exhaustively displays all of
419         //the changes, with dates, of any data in the database under the given
420         //argument restrictions.
421         //$data_array is an array with table_column_name => "display name"
422         //$recres is the return from getRecPatientData for example
423         //$N is the number of items to display in one row
424         print "<table><tr>\n";
425         $count = 0;
426         foreach ($data_array as $akey => $aval) {
427                 if ($count == $N) {
428                         print "</tr><tr>\n";
429                         $count = 0;
430                 }
431                 print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
432                 printData($recres, $akey, "<br>", "Y-m-d");
433                 print "</span></td>\n";
434                 $count++;
435         }
436         print "</tr></table>\n";
442 function printData ($retar, $key, $sep, $date_format) {
443         //$retar{$key}
444         if (@array_key_exists($key,$retar)) {
445                 $length = sizeof($retar{$key});
446                 for ($iter = $length;$iter>=1;$iter--) {
447                         if ($retar{$key}[$iter]{"value"} != "0000-00-00 00:00:00") {
448                                 print $retar{$key}[$iter]{"value"} . " (" . date($date_format,strtotime($retar{$key}[$iter]{"date"})) . ")$sep";
449                         }
450                 }
451         }
454 function printRecDataOne($data_array, $recres, $N) {
455         //this function is like printRecData except it will only print out those elements that
456         //have values. when they do have values, this function will only print out the most recent
457         //value of each element.
458         //this may be considered a compressed data viewer.
459         //this function generates a formatted history of all changes to the data
460         //$data_array is an array with table_column_name => "display name"
461         //$recres is the return from getRecPatientData for example
462         //$N is the number of items to display in one row
463         print "<table><tr>\n";
464         $count = 0;
465         foreach ($data_array as $akey => $aval) {
466                 if (sizeof($recres{$akey})>0 && ($recres{$akey}[1]{"value"}!="0000-00-00 00:00:00")) {
467                         if ($count == $N) {
468                                 print "</tr><tr>\n";
469                                 $count = 0;
470                         }
471                         print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
472                         printDataOne($recres, $akey, "<br>", "Y-m-d");
473                         print "</span></td>\n";
474                         $count++;
475                 }
476         }
477         print "</tr></table>\n";
483 function printDataOne ($retar, $key, $sep, $date_format) {
484         //this function supports the printRecDataOne function above
485         if (@array_key_exists($key,$retar)) {
486                 $length = sizeof($retar{$key});
487                 if ($retar{$key}[$length]{"value"} != "0000-00-00 00:00:00") {
488                         //print $retar{$key}[$length]{"value"} . " (" . date($date_format,strtotime($retar{$key}[$length]{"date"})) . "$sep";
489                         print $retar{$key}[$length]{"value"} . "$sep";
490                 }
491         }