2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 require_once("{$GLOBALS['srcdir']}/acl.inc");
8 require_once("{$GLOBALS['srcdir']}/formatting.inc.php");
9 require_once($GLOBALS["srcdir"] . "/options.inc.php");
11 $patient_data_array = array(
12 title => xl('Title') . ": ",
13 fname => xl('First Name') . ": ",
14 mname => xl('Middle Name') . ": ",
15 lname => xl('Last Name') . ": ",
16 sex => xl('Sex') . ": ",
17 ss => xl('SS') . ": ",
18 DOB => xl('Date of Birth') . ": ",
19 street => xl('Street') . ": ",
20 city => xl('City') . ": ",
21 state => xl('State') . ": ",
22 postal_code => xl('Zip') . ": ",
23 country_code => xl('Country') . ": ",
24 occupation => xl('Occupation') . ": ",
25 phone_home => xl('Home Phone') . ": ",
26 phone_biz => xl('Business Phone') . ": ",
27 phone_contact => xl('Contact Phone') . ": ",
28 contact_relationship => xl('Contact Person') . ": ",
29 hipaa_mail => xl('Allows Mail') . ": ",
30 hipaa_voice => xl('Allows Voice msgs') . ": ",
31 hipaa_notice => xl('Notice Received') . ": ",
32 hipaa_message => xl('Leave Message With') . ": "
35 $history_data_array = array(
36 coffee => xl('Coffee Use') . ": ",
37 tobacco => xl('Tobacco Use') . ": ",
38 alcohol => xl('Alcohol Use') . ": ",
39 sleep_patterns => xl('Sleep Patterns') . ": ",
40 exercise_patterns => xl('Exercise Patterns') . ": ",
41 seatbelt_use => xl('Seatbelt Use') . ": ",
42 counseling => xl('Counseling') . ": ",
43 hazardous_activities => xl('Hazardous Activities') . ": ",
44 last_breast_exam => xl('Last Breast Exam') . ": ",
45 last_mammogram => xl('Last Mammogram') . ": ",
46 last_gynocological_exam => xl('Last Gyn. Exam') . ": ",
47 last_rectal_exam => xl('Last Rectal Exam') . ": ",
48 last_prostate_exam => xl('Last Prostate Exam') . ": ",
49 last_physical_exam => xl('Last Physical Exam') . ": ",
50 last_sigmoidoscopy_colonoscopy => xl('Last Sigmoid/Colonoscopy') . ": ",
51 cataract_surgery => xl('Last Cataract Surgery') . ": ",
52 tonsillectomy => xl('Last Tonsillectomy') . ": ",
53 cholecystestomy => xl('Last Cholecystestomy') . ": ",
54 heart_surgery => xl('Last Heart Surgery') . ": ",
55 hysterectomy => xl('Last Hysterectomy') . ": ",
56 hernia_repair => xl('Last Hernia Repair') . ": ",
57 hip_replacement => xl('Last Hip Replacement') . ": ",
58 knee_replacement => xl('Last Knee Replacement') . ": ",
59 appendectomy => xl('Last Appendectomy') . ": ",
60 history_mother => xl('Mothers History') . ": ",
61 history_father => xl('Fathers History') . ": ",
62 history_siblings => xl('Sibling History') . ": ",
63 history_offspring => xl('Offspring History') . ": ",
64 history_spouse => xl('Spouses History') . ": ",
65 relatives_cancer => xl('Relatives Cancer') . ": ",
66 relatives_tuberculosis => xl('Relatives Tuberculosis') . ": ",
67 relatives_diabetes => xl('Relatives Diabetes') . ": ",
68 relatives_high_blood_pressure => xl('Relatives Blood Pressure') . ": ",
69 relatives_heart_problems => xl('Relatives Heart') . ": ",
70 relatives_stroke => xl('Relatives Stroke') . ": ",
71 relatives_epilepsy => xl('Relatives Epilepsy') . ": ",
72 relatives_mental_illness => xl('Relatives Mental Illness') . ": ",
73 relatives_suicide => xl('Relatives Suicide') . ": "
76 $employer_data_array = array(
77 name => xl('Employer') . ": ",
78 street => xl('Address') . ": ",
79 city => xl('City') . ": ",
80 postal_code => xl('Zip') . ": ",
81 state => xl('State') . ": ",
82 country => xl('Country') . ": "
85 $insurance_data_array = array(
86 provider_name => xl('Provider') . ": ",
87 plan_name => xl('Plan Name') . ": ",
88 policy_number => xl('Policy Number') . ": ",
89 group_number => xl('Group Number') . ": ",
90 subscriber_fname => xl('Subscriber First Name') . ": ",
91 subscriber_mname => xl('Subscriber Middle Name') . ": ",
92 subscriber_lname => xl('Subscriber Last Name') . ": ",
93 subscriber_relationship => xl('Subscriber Relationship') . ": ",
94 subscriber_ss => xl('Subscriber SS') . ": ",
95 subscriber_DOB => xl('Subscriber Date of Birth') . ": ",
96 subscriber_phone => xl('Subscribter Phone') . ": ",
97 subscriber_street => xl('Subscriber Address') . ": ",
98 subscriber_postal_code => xl('Subscriber Zip') . ": ",
99 subscriber_city => xl('Subscriber City') . ": ",
100 subscriber_state => xl('Subscriber State') . ": ",
101 subscriber_country => xl('Subscriber Country') . ": ",
102 subscriber_employer => xl('Subscriber Employer') . ": ",
103 subscriber_employer_street => xl('Subscriber Employer Street') . ": ",
104 subscriber_employer_city => xl('Subscriber Employer City') . ": ",
105 subscriber_employer_postal_code => xl('Subscriber Employer Zip') . ": ",
106 subscriber_employer_state => xl('Subscriber Employer State') . ": ",
107 subscriber_employer_country => xl('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))
116 while(list($key, $value) = each($list))
118 if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
120 $ret[$key]['title'] = $key;
121 $ret[$key]['content'] = $value;
122 $ret[$key]['date'] = $list['date'];
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))
135 while(list($key, $value) = each($list))
137 if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
139 $ret[$key]['content'] = $value;
140 $ret[$key]['date'] = $list['date'];
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))
153 while(list($key, $value) = each($list))
155 if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
157 $ret[$key]['content'] = $value;
158 $ret[$key]['date'] = $list['date'];
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))
171 while(list($key, $value) = each($list))
173 if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
175 $ret[$key]['content'] = $value;
176 $ret[$key]['date'] = $list['date'];
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))
189 while(list($key, $value) = each($list))
191 if ($ret[$key]['content'] != $value && $ret[$key]['date'] < $list['date'])
193 $ret[$key]['content'] = $value;
194 $ret[$key]['date'] = $list['date'];
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";
208 function printPatientNotes($pid) {
209 // exclude ALL deleted notes
210 $res = sqlStatement("select * from pnotes where pid = '$pid' and deleted != 1 and activity = 1 order by date");
211 while($result = sqlFetchArray($res)) {
212 print "<span class=bold>" . oeFormatSDFT(strtotime($result{"date"})) .
213 ":</span><span class=text> " .
214 stripslashes(oeFormatPatientNote($result['body'])) . "</span><br>\n";
218 // Get the current value for a layout based transaction field.
220 function lbt_current_value($frow, $formid) {
221 $formname = $frow['form_id'];
222 $field_id = $frow['field_id'];
225 $ldrow = sqlQuery("SELECT field_value FROM lbt_data WHERE " .
226 "form_id = ? AND field_id = ?", array($formid, $field_id) );
227 if (!empty($ldrow)) $currvalue = $ldrow['field_value'];
232 // Display a particular transaction.
234 function lbt_report($id, $formname) {
236 $fres = sqlStatement("SELECT * FROM layout_options " .
237 "WHERE form_id = ? AND uor > 0 " .
238 "ORDER BY group_name, seq", array($formname));
239 while ($frow = sqlFetchArray($fres)) {
240 $field_id = $frow['field_id'];
241 $currvalue = lbt_current_value($frow, $id);
242 // For brevity, skip fields without a value.
243 if ($currvalue === '') continue;
244 $arr[$field_id] = wordwrap($currvalue, 30, "\n", true);
247 display_layout_rows($formname, $arr);
251 // Display all transactions for the specified patient.
253 function printPatientTransactions($pid) {
254 $res = sqlStatement("SELECT * FROM transactions WHERE pid = ? ORDER BY date", array($pid));
255 while($row = sqlFetchArray($res)) {
256 echo "<p><span class='bold'>" .
257 text(oeFormatSDFT(strtotime($row['date']))) .
259 generate_display_field(array('data_type'=>'1','list_id'=>'transactions'), $row['title']) .
261 lbt_report($row['id'], $row['title']);
266 function printPatientBilling($pid) {
267 $res = sqlStatement("select * from billing where pid='$pid' order by date");
268 while($result = sqlFetchArray($res)) {
269 echo "<span class=bold>" . oeFormatSDFT(strtotime($result{"date"})) . " : </span>";
270 echo "<span class=text>(".$result{"code_type"}.") ";
271 echo $result['code_type'] == 'COPAY' ? oeFormatMoney($result['code']) : $result['code'];
272 echo " - ". wordwrap($result['code_text'], 70, "\n", true)."</span>";
277 function getPatientBillingEncounter($pid, $encounter) {
278 /*******************************************************************
279 $sql = "SELECT billing.*, u.id, u.fname, u.mname, u.lname, " .
280 "CONCAT(u.fname,' ', u.lname) AS provider_name, u.federaltaxid " .
281 "FROM billing LEFT JOIN users AS u ON u.id = billing.provider_id " .
282 "WHERE pid='" . add_escape_custom($pid) . "' AND " .
283 "encounter = '" . add_escape_custom($encounter) .
284 "' AND activity='1' ORDER BY date";
285 *******************************************************************/
286 $erow = sqlQuery("SELECT provider_id FROM form_encounter WHERE " .
287 "pid = '$pid' AND encounter = '$encounter' " .
288 "ORDER BY id DESC LIMIT 1");
289 $inv_provider = $erow['provider_id'] + 0;
290 $sql = "SELECT b.*, u.id, u.fname, u.mname, u.lname, " .
291 "CONCAT(u.fname,' ', u.lname) AS provider_name, u.federaltaxid " .
292 "FROM billing AS b " .
293 "LEFT JOIN users AS u ON " .
294 "( b.provider_id != 0 AND u.id = b.provider_id ) OR " .
295 "( b.provider_id = 0 AND u.id = $inv_provider ) " .
296 "WHERE pid='" . add_escape_custom($pid) . "' AND " .
297 "encounter = '" . add_escape_custom($encounter) .
298 "' AND activity = '1' ORDER BY date";
299 /******************************************************************/
300 $res = sqlStatement($sql);
302 while($result = sqlFetchArray($res)) {
303 $billings[] = $result;
308 function printPatientForms($pid, $cols) {
309 //this function takes a $pid
310 $inclookupres = sqlStatement("select distinct formdir from forms where pid='$pid' AND deleted=0");
311 while($result = sqlFetchArray($inclookupres)) {
312 include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
315 $res = sqlStatement("select * from forms where pid='$pid' AND deleted=0 order by date");
316 while($result = sqlFetchArray($res)) {
317 if ($result{"form_name"} == "New Patient Encounter") {
318 echo "<div class='text encounter'>\n";
319 echo "<h1>" . $result["form_name"] . "</h1>";
321 // display the provider info
322 $tmp = sqlQuery("SELECT u.title, u.fname, u.mname, u.lname " .
323 "FROM forms AS f, users AS u WHERE " .
324 "f.pid = '$pid' AND f.encounter = ".$result['encounter']." AND " .
325 "f.formdir = 'newpatient' AND u.username = f.user " .
326 " AND f.deleted=0 ". //--JRM--
327 "ORDER BY f.id LIMIT 1");
328 echo " ".xl('Provider').": ".$tmp['title']." ".
329 $tmp['fname']." ".$tmp['mname']." ".$tmp['lname'];
333 echo "<div class='text encounter_form'>";
334 echo "<h1>" . $result["form_name"] . "</h1>";
336 echo "(" . oeFormatSDFT(strtotime($result["date"])) . ") ";
338 if (acl_check('acct', 'rep') || acl_check('acct', 'eob') || acl_check('acct', 'bill')) {
339 if ($result{"form_name"} == "New Patient Encounter") {
340 // display billing info
342 $bres = sqlStatement("SELECT b.date, b.code, b.code_text " .
343 "FROM billing AS b, code_types AS ct WHERE " .
345 "b.encounter = ? AND " .
346 "b.activity = 1 AND " .
347 "b.code_type = ct.ct_key AND " .
350 array($pid, $result['encounter']));
351 while ($brow=sqlFetchArray($bres)) {
352 echo "<span class='bold'> ".xl('Procedure').": </span><span class='text'>" .
353 $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
358 call_user_func($result{"formdir"} . "_report", $pid, $result{"encounter"}, $cols, $result{"form_id"});
364 function getRecHistoryData ($pid) {
365 //data is returned as a multi-level array:
366 //column name->dates->values
367 //$return{"lname"}[0..n]{"date"}
368 //$return{"lname"}[0..n]{"value"}
369 $res = sqlStatement("select * from history_data where pid=? order by date", array($pid) );
371 while($result = sqlFetchArray($res)) {
372 foreach ($result as $key => $val) {
373 if ($key == "pid" || $key == "date" || $key == "id") {
376 $curdate = $result{"date"};
377 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
379 $retar{$key}[$arcount{$key}]{"value"} = $val;
380 $retar{$key}[$arcount{$key}]{"date"} = $curdate;
388 function getRecEmployerData ($pid) {
389 //data is returned as a multi-level array:
390 //column name->dates->values
391 //$return{"lname"}[0..n]{"date"}
392 //$return{"lname"}[0..n]{"value"}
393 $res = sqlStatement("select * from employer_data where pid=? order by date", array($pid) );
395 while($result = sqlFetchArray($res)) {
396 foreach ($result as $key => $val) {
397 if ($key == "pid" || $key == "date" || $key == "id") {
400 $curdate = $result{"date"};
401 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
403 $retar{$key}[$arcount{$key}]{"value"} = $val;
404 $retar{$key}[$arcount{$key}]{"date"} = $curdate;
412 function getRecPatientData ($pid) {
413 //data is returned as a multi-level array:
414 //column name->dates->values
415 //$return{"lname"}[0..n]{"date"}
416 //$return{"lname"}[0..n]{"value"}
417 $res = sqlStatement("select * from patient_data where pid=? order by date", array($pid) );
419 while($result = sqlFetchArray($res)) {
420 foreach ($result as $key => $val) {
421 if ($key == "pid" || $key == "date" || $key == "id") {
424 $curdate = $result{"date"};
425 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
427 $retar{$key}[$arcount{$key}]{"value"} = $val;
428 $retar{$key}[$arcount{$key}]{"date"} = $curdate;
436 function getRecInsuranceData ($pid, $ins_type) {
437 //data is returned as a multi-level array:
438 //column name->dates->values
439 //$return{"lname"}[0..n]{"date"}
440 //$return{"lname"}[0..n]{"value"}
441 $res = sqlStatement("select *, ic.name as provider_name from insurance_data left join insurance_companies as ic on ic.id = provider where pid=? and type=? order by date", array($pid,$ins_type) );
443 while($result = sqlFetchArray($res)) {
444 foreach ($result as $key => $val) {
445 if ($key == "pid" || $key == "date" || $key == "id") {
448 $curdate = $result{"date"};
449 if (($retar{$key}[$arcount{$key}]{"value"} != $val)) {
451 $retar{$key}[$arcount{$key}]{"value"} = $val;
452 $retar{$key}[$arcount{$key}]{"date"} = $curdate;
460 function printRecData($data_array, $recres, $N) {
461 //this function generates a formatted history of all changes to the data
462 //it is a multi-level recursive function that exhaustively displays all of
463 //the changes, with dates, of any data in the database under the given
464 //argument restrictions.
465 //$data_array is an array with table_column_name => "display name"
466 //$recres is the return from getRecPatientData for example
467 //$N is the number of items to display in one row
468 print "<table><tr>\n";
470 foreach ($data_array as $akey => $aval) {
475 print "<td valign=top><span class=bold>$aval</span><br><span class=text>";
476 printData($recres, $akey, "<br>", "Y-m-d");
477 print "</span></td>\n";
480 print "</tr></table>\n";
483 function printData ($retar, $key, $sep, $date_format) {
485 if (@array_key_exists($key,$retar)) {
486 $length = sizeof($retar{$key});
487 for ($iter = $length;$iter>=1;$iter--) {
488 if ($retar{$key}[$iter]{"value"} != "0000-00-00 00:00:00") {
489 print $retar{$key}[$iter]{"value"} . " (" . oeFormatSDFT(strtotime($retar{$key}[$iter]{"date"})) . ")$sep";
495 function printRecDataOne($data_array, $recres, $N) {
496 //this function is like printRecData except it will only print out those elements that
497 //have values. when they do have values, this function will only print out the most recent
498 //value of each element.
499 //this may be considered a compressed data viewer.
500 //this function generates a formatted history of all changes to the data
501 //$data_array is an array with table_column_name => "display name"
502 //$recres is the return from getRecPatientData for example
503 //$N is the number of items to display in one row
504 print "<table><tr>\n";
506 foreach ($data_array as $akey => $aval) {
507 if (sizeof($recres{$akey})>0 && ($recres{$akey}[1]{"value"}!="0000-00-00 00:00:00")) {
512 print "<td valign=top><span class=bold>" . text($aval) . "</span><br><span class=text>";
513 printDataOne($recres, $akey, "<br>", "Y-m-d");
514 print "</span></td>\n";
518 print "</tr></table>\n";
521 function printDataOne ($retar, $key, $sep, $date_format) {
522 //this function supports the printRecDataOne function above
523 if (@array_key_exists($key,$retar)) {
524 $length = sizeof($retar{$key});
525 if ($retar{$key}[$length]{"value"} != "0000-00-00 00:00:00") {
526 $tmp = $retar{$key}[$length]{"value"};
527 if (strstr($key, 'DOB')) $tmp = oeFormatShortDate($tmp);
528 print text($tmp) . $sep;