ready for 2nd patch of 4.2.0
[openemr.git] / library / coding.inc.php
bloba6f616749504b7046eb32ceb05090eba93aa074e
1 <?php
2 // Copyright (C) 2005-2006 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 include_once(dirname(__FILE__) . '/api.inc');
10 include_once(dirname(__FILE__) . '/../interface/forms/fee_sheet/codes.php');
11 include_once(dirname(__FILE__) . '/../custom/code_types.inc.php');
13 // $FEE_SHEET_COLUMNS should be defined in codes.php.
14 if (empty($FEE_SHEET_COLUMNS)) $FEE_SHEET_COLUMNS = 2;
16 // If Save was clicked, save the new and modified billing lines;
17 // then if no error, redirect to patient_encounter.php.
19 if ($_POST['bn_save']) {
20 $provid = $_POST['ProviderID'];
21 if (! $provid) $provid = $_SESSION["authUserID"];
22 $bill = $_POST['bill'];
23 for ($lino = 1; $bill["$lino"]['code_type']; ++$lino) {
24 $iter = $bill["$lino"];
26 // Skip disabled (billed) line items.
27 if ($iter['billed']) continue;
29 $id = $iter['id'];
30 $code_type = $iter['code_type'];
31 $code = $iter['code'];
32 $modifier = trim($iter['mod']);
33 $fee = trim($iter['fee']);
34 $auth = $iter['auth'] ? "1" : "0";
35 $del = $iter['del'];
37 // If the item is already in the database...
38 if ($id) {
39 if ($del) {
40 deleteBilling($id);
42 else {
43 // authorizeBilling($id, $auth);
44 sqlQuery("update billing set fee = '$fee', modifier = '$modifier', " .
45 "authorized = $auth, provider_id = '$provid' where " .
46 "id = '$id' and billed = 0 and activity = 1");
50 // Otherwise it's a new item...
51 else if (! $del) {
52 $query = "select code_text from codes where code_type = '" .
53 $code_types[$code_type]['id'] . "' and " .
54 "code = '$code' and ";
55 if ($modifier) {
56 $query .= "modifier = '$modifier'";
57 } else {
58 $query .= "(modifier is null or modifier = '')";
60 $result = sqlQuery($query);
61 $code_text = $result['code_text'];
62 addBilling($encounter, $code_type, $code, $code_text, $pid, $auth,
63 $provid, $modifier, "", $fee);
67 terminate_coding();
68 exit;
72 <style>
73 .billcell { font-family: sans-serif; font-size: 10pt }
74 </style>
75 <script language="JavaScript">
77 // This is invoked by <select onchange> for the various dropdowns,
78 // including search results.
79 function codeselect(selobj) {
80 var i = selobj.selectedIndex;
81 if (i > 0) {
82 top.restoreSession();
83 var f = document.forms[0];
84 f.newcodes.value = selobj.options[i].value;
85 f.submit();
89 </script>
91 <form method="post" action="<?php echo coding_form_action(); ?>">
92 <span class="title"><?php echo ($GLOBALS['phone_country_code'] == '1') ? 'Fee' : 'Coding' ?> Sheet</span><br>
93 <input type='hidden' name='newcodes' value=''>
95 <center>
96 <table width='95%'>
97 <?php
98 $i = 0;
99 $last_category = '';
101 // Helper function for creating drop-lists.
102 function endFSCategory() {
103 global $i, $last_category, $FEE_SHEET_COLUMNS;
104 if (! $last_category) return;
105 echo " </select>\n";
106 echo " </td>\n";
107 if ($i >= $FEE_SHEET_COLUMNS) {
108 echo " </tr>\n";
109 $i = 0;
113 // Create all the drop-lists of preselected service codes.
114 $res = sqlStatement("SELECT * FROM fee_sheet_options " .
115 "ORDER BY fs_category, fs_option");
116 while ($row = sqlFetchArray($res)) {
117 $fs_category = $row['fs_category'];
118 $fs_option = $row['fs_option'];
119 $fs_codes = $row['fs_codes'];
120 if($fs_category !== $last_category) {
121 endFSCategory();
122 $last_category = $fs_category;
123 ++$i;
124 echo ($i <= 1) ? " <tr>\n" : "";
125 echo " <td width='50%' align='center' nowrap>\n";
126 echo " <select style='width:96%' onchange='codeselect(this)'>\n";
127 echo " <option value=''> " . substr($fs_category, 1) . "\n";
129 echo " <option value='$fs_codes'>" . substr($fs_option, 1) . "\n";
131 endFSCategory();
133 $search_type = $default_search_type;
134 if ($_POST['search_type']) $search_type = $_POST['search_type'];
136 echo $i ? " <td></td>\n </tr>\n" : "";
137 echo " <tr>\n";
138 echo " <td colspan='$FEE_SHEET_COLUMNS' align='center' nowrap>\n";
140 // If Search was clicked, do it and write the list of results here.
141 // There's no limit on the number of results!
143 $numrows = 0;
144 if ($_POST['bn_search'] && $_POST['search_term']) {
145 $query = "select code, modifier, code_text from codes where " .
146 "(code_text like '%" . $_POST['search_term'] . "%' or " .
147 "code like '%" . $_POST['search_term'] . "%') and " .
148 "code_type = '" . $code_types[$search_type]['id'] . "' " .
149 "order by code";
150 $res = sqlStatement($query);
151 $numrows = mysql_num_rows($res); // FIXME - not portable!
154 echo " <select name='Search Results' style='width:98%' " .
155 "onchange='codeselect(this)'";
156 if (! $numrows) echo ' disabled';
157 echo ">\n";
158 echo " <option value=''> Search Results ($numrows items)\n";
160 if ($numrows) {
161 while ($row = sqlFetchArray($res)) {
162 $code = $row['code'];
163 if ($row['modifier']) $code .= ":" . $row['modifier'];
164 echo " <option value='$search_type|$code|'>$code " .
165 ucfirst(strtolower($row['code_text'])) . "</option>\n";
169 echo " </select>\n";
170 echo " </td>\n";
171 echo " </tr>\n";
174 </table>
176 <p style='margin-top:8px;margin-bottom:8px'>
177 <table>
178 <tr>
179 <td>
180 Search&nbsp;
181 <?php
183 foreach ($code_types as $key => $value) {
184 echo " <input type='radio' name='search_type' value='$key'";
185 if ($key == $default_search_type) echo " checked";
186 echo " />$key&nbsp;\n";
189 for&nbsp;
190 </td>
191 <td>
192 <input type='text' name='search_term' value=''> &nbsp;
193 </td>
194 <td>
195 <input type='submit' name='bn_search' value='Search'>
196 </td>
197 </tr>
198 </table>
199 </p>
201 <p style='margin-top:16px;margin-bottom:8px'>
202 <table cellspacing='5'>
203 <tr>
204 <td class='billcell'><b>Type</b></td>
205 <td class='billcell'><b>Code</b></td>
206 <?php if (modifiers_are_used()) { ?>
207 <td class='billcell'><b>Mod</b></td>
208 <?php } ?>
209 <?php if (fees_are_used()) { ?>
210 <td class='billcell' align='right'><b>Fee</b>&nbsp;</td>
211 <?php } ?>
212 <td class='billcell' align='center'><b>Auth</b></td>
213 <td class='billcell' align='center'><b>Delete</b></td>
214 <td class='billcell'><b>Description</b></td>
215 </tr>
216 <?php
219 // This writes a billing line item to the output page.
221 function echoLine($lino, $codetype, $code, $modifier, $auth = TRUE, $del = FALSE,
222 $fee = NULL, $id = NULL, $billed = FALSE, $code_text = NULL)
224 global $code_types;
225 if (! $code_text) {
226 $query = "select fee, code_text from codes where code_type = '" .
227 $code_types[$codetype]['id'] . "' and " .
228 "code = '$code' and ";
229 if ($modifier) {
230 $query .= "modifier = '$modifier'";
231 } else {
232 $query .= "(modifier is null or modifier = '')";
234 $result = sqlQuery($query);
235 $code_text = $result['code_text'];
236 if (!isset($fee)) $fee = $result['fee'];
238 $strike1 = ($id && $del) ? "<strike>" : "";
239 $strike2 = ($id && $del) ? "</strike>" : "";
240 echo " <tr>\n";
241 echo " <td class='billcell'>$strike1$codetype$strike2";
242 if ($id) {
243 echo "<input type='hidden' name='bill[$lino][id]' value='$id'>";
245 echo "<input type='hidden' name='bill[$lino][code_type]' value='$codetype'>";
246 echo "<input type='hidden' name='bill[$lino][code]' value='$code'>";
247 echo "<input type='hidden' name='bill[$lino][billed]' value='$billed'>";
248 echo "</td>\n";
249 echo " <td class='billcell'>$strike1$code$strike2</td>\n";
250 if ($billed) {
251 if (modifiers_are_used()) {
252 echo " <td class='billcell'>$strike1$modifier$strike2" .
253 "<input type='hidden' name='bill[$lino][mod]' value='$modifier'></td>\n";
255 if (fees_are_used()) {
256 echo " <td class='billcell' align='right'>$fee</td>\n";
258 echo " <td class='billcell' align='center'><input type='checkbox'" .
259 ($auth ? " checked" : "") . " disabled /></td>\n";
260 echo " <td class='billcell' align='center'><input type='checkbox'" .
261 " disabled /></td>\n";
262 } else {
263 if (modifiers_are_used()) {
264 if ($code_types[$codetype]['mod'] || $modifier) {
265 echo " <td class='billcell'><input type='text' name='bill[$lino][mod]' " .
266 "value='$modifier' size='" . $code_types[$codetype]['mod'] . "'></td>\n";
267 } else {
268 echo " <td class='billcell'>&nbsp;</td>\n";
271 if (fees_are_used()) {
272 if ($code_types[$codetype]['fee'] || $fee != 0) {
273 echo " <td class='billcell' align='right'><input type='text' name='bill[$lino][fee]' " .
274 "value='$fee' size='6' style='text-align:right'></td>\n";
275 } else {
276 echo " <td class='billcell'>&nbsp;</td>\n";
279 echo " <td class='billcell' align='center'><input type='checkbox' name='bill[$lino][auth]' " .
280 "value='1'" . ($auth ? " checked" : "") . " /></td>\n";
281 echo " <td class='billcell' align='center'><input type='checkbox' name='bill[$lino][del]' " .
282 "value='1'" . ($del ? " checked" : "") . " /></td>\n";
284 echo " <td class='billcell'>$strike1" . ucfirst(strtolower($code_text)) . "$strike2</td>\n";
285 echo " </tr>\n";
288 // Generate lines for items already in the database.
290 $lino = 0;
291 $encounter_provid = -1;
292 if ($result = getBillingByEncounter($pid, $encounter, "*") ) {
293 foreach ($result as $iter) {
294 ++$lino;
295 $del = $_POST['bill']["$lino"]['del']; // preserve Delete if checked
296 // list($code, $modifier) = explode("-", $iter["code"]);
297 echoLine($lino, $iter["code_type"], trim($iter["code"]), trim($iter["modifier"]),
298 $iter["authorized"], $del, $iter["fee"], $iter["id"], $iter["billed"], $iter["code_text"]);
299 if ($encounter_provid < 0 && ! $del) $encounter_provid = $iter["provider_id"];
303 // If there were no billing items then the default provider is the logged-in user.
305 if ($encounter_provid < 0) $encounter_provid = $_SESSION["authUserID"];
307 // Echo new billing items from this form here, but omit any line
308 // whose Delete checkbox is checked.
310 if ($_POST['bill']) {
311 foreach ($_POST['bill'] as $key => $iter) {
312 if ($iter["id"]) continue; // skip if it came from the database
313 if ($iter["del"]) continue; // skip if Delete was checked
314 echoLine(++$lino, $iter["code_type"], $iter["code"], trim($iter["mod"]),
315 $iter["auth"], $iter["del"], $iter["fee"]);
319 // If new billing code(s) were <select>ed, add their line(s) here.
321 if ($_POST['newcodes']) {
322 $arrcodes = explode('~', $_POST['newcodes']);
323 foreach ($arrcodes as $codestring) {
324 if ($codestring === '') continue;
325 $arrcode = explode('|', $codestring);
326 $newtype = $arrcode[0];
327 $newcode = $arrcode[1];
328 $newsel = $arrcode[2];
329 if ($newtype == 'COPAY') {
330 $tmp = sqlQuery("SELECT copay FROM insurance_data WHERE pid = '$pid' " .
331 "AND type = 'primary' ORDER BY date DESC LIMIT 1");
332 $code = sprintf('%01.2f', 0 + $tmp['copay']);
333 echoLine(++$bill_lino, $newtype, $code, '', '', '1', '0', '1',
334 sprintf('%01.2f', 0 - $code));
336 else if ($newtype == 'PROD') {
337 $result = sqlQuery("SELECT * FROM drug_templates WHERE " .
338 "drug_id = '$newcode' AND selector = '$newsel'");
339 $units = max(1, intval($result['quantity']));
340 $prrow = sqlQuery("SELECT prices.pr_price " .
341 "FROM patient_data, prices WHERE " .
342 "patient_data.pid = '$pid' AND " .
343 "prices.pr_id = '$newcode' AND " .
344 "prices.pr_selector = '$newsel' AND " .
345 "prices.pr_level = patient_data.pricelevel " .
346 "LIMIT 1");
347 $fee = empty($prrow) ? 0 : $prrow['pr_price'];
348 echoProdLine(++$prod_lino, $newcode, FALSE, $units, $fee);
350 else {
351 list($code, $modifier) = explode(":", $newcode);
352 $ndc_info = '';
353 // If HCPCS, find last NDC string used for this code.
354 if ($newtype == 'HCPCS' && $ndc_applies) {
355 $tmp = sqlQuery("SELECT ndc_info FROM billing WHERE " .
356 "code_type = '$newtype' AND code = '$code' AND ndc_info LIKE 'N4%' " .
357 "ORDER BY date DESC LIMIT 1");
358 if (!empty($tmp)) $ndc_info = $tmp['ndc_info'];
360 echoLine(++$bill_lino, $newtype, $code, trim($modifier), $ndc_info);
365 </table>
366 </p>
368 <br>
369 &nbsp;
371 <span class="billcell">PROVIDER:</span>
373 <?php
375 // Build a drop-down list of providers. This includes users who
376 // have the word "provider" anywhere in their "additional info"
377 // field, so that we can define providers (for billing purposes)
378 // who do not appear in the calendar.
380 $query = "SELECT id, lname, fname FROM users WHERE " .
381 "authorized = 1 OR info LIKE '%provider%' ORDER BY lname, fname";
382 $res = sqlStatement($query);
384 echo " <select name='ProviderID'>\n";
385 echo " <option value=''>-- Please Select --\n";
387 while ($row = sqlFetchArray($res)) {
388 $provid = $row['id'];
389 echo " <option value='$provid'";
390 if ($provid == $encounter_provid) echo " selected";
391 echo ">" . $row['lname'] . ", " . $row['fname'] . "\n";
394 echo " </select>\n";
397 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
399 <input type='submit' name='bn_save' value='Save' />
400 &nbsp;
401 <input type='submit' name='bn_refresh' value='Refresh'>
402 &nbsp;
403 <input type='button' value='Cancel' onclick='docancel()' />
405 <?php if ($code_types['UCSMC']) { ?>
406 <p style='font-family:sans-serif;font-size:8pt;color:#666666;'>
407 &nbsp;<br>
408 UCSMC codes provided by the University of Calgary Sports Medicine Centre
409 </p>
410 <?php } ?>
412 </center>
414 </form>
415 <?php
416 // TBD: If $alertmsg, display it with a JavaScript alert().