added appointments-encounters report
[openemr.git] / interface / reports / appt_encounter_report.php
blob7a3a0267d88a2546c6346ac0be17e5801ed9db02
1 <?
2 // Copyright (C) 2005 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This report cross-references appointments with encounters.
10 // For a given date, show a line for each appointment with the
11 // matching encounter, and also for each encounter that has no
12 // matching appointment. This helps to catch these errors:
14 // * Appointments with no encounter
15 // * Encounters with no appointment
16 // * Codes not justified
17 // * Codes not authorized
18 // * Procedure codes without a fee
19 // * Fees assigned to diagnoses (instead of procedures)
20 // * Encounters not billed
22 include_once("../globals.php");
23 include_once("../../library/patient.inc");
25 $alertmsg = '';
27 function bucks($amount) {
28 if ($amount)
29 printf("%.2f", $amount);
32 $today = date("Y-m-d");
34 if ($_POST['form_search']) {
35 $form_date = fixDate($_POST['form_date'], "");
37 $query = "( " .
38 "SELECT " .
39 "LEFT(e.pc_eventDate, 10), e.pc_startTime, " .
40 "fe.encounter, " .
41 "f.authorized, " .
42 "p.fname, p.lname, p.pid, " .
43 "u.lname AS docname " .
44 "FROM openemr_postcalendar_events AS e " .
45 "LEFT OUTER JOIN form_encounter AS fe " .
46 "ON LEFT(fe.date, 10) = e.pc_eventDate AND fe.pid = e.pc_pid " .
47 "LEFT OUTER JOIN forms AS f ON f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
48 "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " .
49 "LEFT OUTER JOIN users AS u ON u.id = e.pc_aid " .
50 "WHERE e.pc_eventDate = '$form_date' AND " .
51 "( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
52 ") UNION ( " .
53 "SELECT " .
54 "LEFT(fe.date, 10), e.pc_startTime, " .
55 "fe.encounter, " .
56 "f.authorized, " .
57 "p.fname, p.lname, p.pid, " .
58 "u.lname AS docname " .
59 "FROM form_encounter AS fe " .
60 "LEFT OUTER JOIN openemr_postcalendar_events AS e " .
61 "ON LEFT(fe.date, 10) = e.pc_eventDate AND fe.pid = e.pc_pid AND " .
62 "( e.pc_catid = 5 OR e.pc_catid = 9 OR e.pc_catid = 10 ) " .
63 "LEFT OUTER JOIN forms AS f ON f.encounter = fe.encounter AND f.formdir = 'newpatient' " .
64 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid " .
65 "LEFT OUTER JOIN users AS u ON u.username = f.user " .
66 "WHERE LEFT(fe.date, 10) = '$form_date' " .
67 ") " .
68 "ORDER BY docname, pc_startTime";
70 $res = sqlStatement($query);
73 <html>
74 <head>
75 <link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
76 <title>Appointments and Encounters</title>
77 </head>
79 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
80 <center>
82 <form method='post' action='appt_encounter_report.php'>
84 <table border='0' cellpadding='5' cellspacing='0' width='98%'>
86 <tr>
87 <td height="1" colspan="2">
88 </td>
89 </tr>
91 <tr bgcolor='#ddddff'>
92 <td align='left'>
93 <h2>Appointments and Encounters</h2>
94 </td>
95 <td align='right'>
96 Booking Date:
97 <input type='text' name='form_date' size='10' value='<? echo $_POST['form_date']; ?>'
98 title='Date of appointments mm/dd/yyyy'>
99 &nbsp;
100 <input type='submit' name='form_search' value='Search'>
101 </td>
102 </tr>
104 <tr>
105 <td height="1" colspan="2">
106 </td>
107 </tr>
109 </table>
111 <table border='0' cellpadding='1' cellspacing='2' width='98%'>
113 <tr bgcolor="#dddddd">
114 <td class="dehead">
115 &nbsp;Practitioner
116 </td>
117 <td class="dehead">
118 &nbsp;Time
119 </td>
120 <td class="dehead">
121 &nbsp;Patient
122 </td>
123 <td class="dehead" align="right">
124 Chart&nbsp;
125 </td>
126 <td class="dehead" align="right">
127 Encounter&nbsp;
128 </td>
129 <td class="dehead" align="right">
130 Charge&nbsp;
131 </td>
132 <td class="dehead" align="center">
133 Billed
134 </td>
135 <td class="dehead">
136 &nbsp;Error
137 </td>
138 </tr>
140 if ($res) {
141 while ($row = sqlFetchArray($res)) {
142 $patient_id = $row['pid'];
143 $encounter = $row['encounter'];
145 $billed = "Y";
146 $errmsg = "";
147 $charges = 0;
149 // Scan the billing items for status and fee total.
151 $query = "SELECT code_type, authorized, billed, fee, justify " .
152 "FROM billing WHERE " .
153 "pid = '$patient_id' AND encounter = '$encounter' AND activity = 1";
154 $bres = sqlStatement($query);
156 while ($brow = sqlFetchArray($bres)) {
157 if (! $brow['billed']) $billed = "";
158 if (! $brow['authorized']) $errmsg = "Needs Auth";
159 if ($brow['code_type'] == 'CPT4' || $brow['code_type'] == 'HCPCS') {
160 $charges += $brow['fee'];
161 if ($brow['fee'] == 0 ) $errmsg = "Missing Fee";
162 if (! $brow['justify']) $errmsg = "Needs Justify";
163 } else {
164 if ($brow['fee'] != 0) $errmsg = "Misplaced Fee";
167 if (! $charges) $billed = "";
169 <tr bgcolor='<? echo $bgcolor ?>'>
170 <td class="detail">
171 &nbsp;<? echo $row['docname'] ?>
172 </td>
173 <td class="detail">
174 &nbsp;<? echo $row['pc_startTime'] ?>
175 </td>
176 <td class="detail">
177 &nbsp;<? echo $row['fname'] . " " . $row['lname'] ?>
178 </td>
179 <td class="detail" align="right">
180 <? echo $row['pid'] ?>&nbsp;
181 </td>
182 <td class="detail" align="right">
183 <? echo $row['encounter'] ?>&nbsp;
184 </td>
185 <td class="detail" align="right">
186 <? bucks($charges) ?>&nbsp;
187 </td>
188 <td class="detail" align="center">
189 <? echo $billed ?>
190 </td>
191 <td class="detail" align="left">
192 &nbsp;<? echo $errmsg ?>
193 </td>
194 </tr>
200 </table>
202 </form>
203 </center>
204 <script>
206 if ($alertmsg) {
207 echo " alert('$alertmsg');\n";
210 </script>
211 </body>
212 </html>