fixed undo of checkouts
[openemr.git] / library / coding.inc.php
blob55b9e4d1adb8c900bae5bbcdec75b2969568107f
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 = addslashes($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"><? 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;
182 foreach ($code_types as $key => $value) {
183 echo " <input type='radio' name='search_type' value='$key'";
184 if ($key == $default_search_type) echo " checked";
185 echo " />$key&nbsp;\n";
188 for&nbsp;
189 </td>
190 <td>
191 <input type='text' name='search_term' value=''> &nbsp;
192 </td>
193 <td>
194 <input type='submit' name='bn_search' value='Search'>
195 </td>
196 </tr>
197 </table>
198 </p>
200 <p style='margin-top:16px;margin-bottom:8px'>
201 <table cellspacing='5'>
202 <tr>
203 <td class='billcell'><b>Type</b></td>
204 <td class='billcell'><b>Code</b></td>
205 <? if (modifiers_are_used()) { ?>
206 <td class='billcell'><b>Mod</b></td>
207 <? } ?>
208 <? if (fees_are_used()) { ?>
209 <td class='billcell' align='right'><b>Fee</b>&nbsp;</td>
210 <? } ?>
211 <td class='billcell' align='center'><b>Auth</b></td>
212 <td class='billcell' align='center'><b>Delete</b></td>
213 <td class='billcell'><b>Description</b></td>
214 </tr>
217 // This writes a billing line item to the output page.
219 function echoLine($lino, $codetype, $code, $modifier, $auth = TRUE, $del = FALSE,
220 $fee = NULL, $id = NULL, $billed = FALSE, $code_text = NULL)
222 global $code_types;
223 if (! $code_text) {
224 $query = "select fee, code_text from codes where code_type = '" .
225 $code_types[$codetype]['id'] . "' and " .
226 "code = '$code' and ";
227 if ($modifier) {
228 $query .= "modifier = '$modifier'";
229 } else {
230 $query .= "(modifier is null or modifier = '')";
232 $result = sqlQuery($query);
233 $code_text = $result['code_text'];
234 if (!isset($fee)) $fee = $result['fee'];
236 $strike1 = ($id && $del) ? "<strike>" : "";
237 $strike2 = ($id && $del) ? "</strike>" : "";
238 echo " <tr>\n";
239 echo " <td class='billcell'>$strike1$codetype$strike2";
240 if ($id) {
241 echo "<input type='hidden' name='bill[$lino][id]' value='$id'>";
243 echo "<input type='hidden' name='bill[$lino][code_type]' value='$codetype'>";
244 echo "<input type='hidden' name='bill[$lino][code]' value='$code'>";
245 echo "<input type='hidden' name='bill[$lino][billed]' value='$billed'>";
246 echo "</td>\n";
247 echo " <td class='billcell'>$strike1$code$strike2</td>\n";
248 if ($billed) {
249 if (modifiers_are_used()) {
250 echo " <td class='billcell'>$strike1$modifier$strike2" .
251 "<input type='hidden' name='bill[$lino][mod]' value='$modifier'></td>\n";
253 if (fees_are_used()) {
254 echo " <td class='billcell' align='right'>$fee</td>\n";
256 echo " <td class='billcell' align='center'><input type='checkbox'" .
257 ($auth ? " checked" : "") . " disabled /></td>\n";
258 echo " <td class='billcell' align='center'><input type='checkbox'" .
259 " disabled /></td>\n";
260 } else {
261 if (modifiers_are_used()) {
262 if ($code_types[$codetype]['mod'] || $modifier) {
263 echo " <td class='billcell'><input type='text' name='bill[$lino][mod]' " .
264 "value='$modifier' size='" . $code_types[$codetype]['mod'] . "'></td>\n";
265 } else {
266 echo " <td class='billcell'>&nbsp;</td>\n";
269 if (fees_are_used()) {
270 if ($code_types[$codetype]['fee'] || $fee != 0) {
271 echo " <td class='billcell' align='right'><input type='text' name='bill[$lino][fee]' " .
272 "value='$fee' size='6' style='text-align:right'></td>\n";
273 } else {
274 echo " <td class='billcell'>&nbsp;</td>\n";
277 echo " <td class='billcell' align='center'><input type='checkbox' name='bill[$lino][auth]' " .
278 "value='1'" . ($auth ? " checked" : "") . " /></td>\n";
279 echo " <td class='billcell' align='center'><input type='checkbox' name='bill[$lino][del]' " .
280 "value='1'" . ($del ? " checked" : "") . " /></td>\n";
282 echo " <td class='billcell'>$strike1" . ucfirst(strtolower($code_text)) . "$strike2</td>\n";
283 echo " </tr>\n";
286 // Generate lines for items already in the database.
288 $lino = 0;
289 $encounter_provid = -1;
290 if ($result = getBillingByEncounter($pid, $encounter, "*") ) {
291 foreach ($result as $iter) {
292 ++$lino;
293 $del = $_POST['bill']["$lino"]['del']; // preserve Delete if checked
294 // list($code, $modifier) = explode("-", $iter["code"]);
295 echoLine($lino, $iter["code_type"], trim($iter["code"]), trim($iter["modifier"]),
296 $iter["authorized"], $del, $iter["fee"], $iter["id"], $iter["billed"], $iter["code_text"]);
297 if ($encounter_provid < 0 && ! $del) $encounter_provid = $iter["provider_id"];
301 // If there were no billing items then the default provider is the logged-in user.
303 if ($encounter_provid < 0) $encounter_provid = $_SESSION["authUserID"];
305 // Echo new billing items from this form here, but omit any line
306 // whose Delete checkbox is checked.
308 if ($_POST['bill']) {
309 foreach ($_POST['bill'] as $key => $iter) {
310 if ($iter["id"]) continue; // skip if it came from the database
311 if ($iter["del"]) continue; // skip if Delete was checked
312 echoLine(++$lino, $iter["code_type"], $iter["code"], trim($iter["mod"]),
313 $iter["auth"], $iter["del"], $iter["fee"]);
317 // If new billing code(s) were <select>ed, add their line(s) here.
319 if ($_POST['newcodes']) {
320 $arrcodes = explode('~', $_POST['newcodes']);
321 foreach ($arrcodes as $codestring) {
322 if ($codestring === '') continue;
323 $arrcode = explode('|', $codestring);
324 $newtype = $arrcode[0];
325 $newcode = $arrcode[1];
326 $newsel = $arrcode[2];
327 if ($newtype == 'COPAY') {
328 $tmp = sqlQuery("SELECT copay FROM insurance_data WHERE pid = '$pid' " .
329 "AND type = 'primary' ORDER BY date DESC LIMIT 1");
330 $code = sprintf('%01.2f', 0 + $tmp['copay']);
331 echoLine(++$bill_lino, $newtype, $code, '', '', '1', '0', '1',
332 sprintf('%01.2f', 0 - $code));
334 else if ($newtype == 'PROD') {
335 $result = sqlQuery("SELECT * FROM drug_templates WHERE " .
336 "drug_id = '$newcode' AND selector = '$newsel'");
337 $units = max(1, intval($result['quantity']));
338 $prrow = sqlQuery("SELECT prices.pr_price " .
339 "FROM patient_data, prices WHERE " .
340 "patient_data.pid = '$pid' AND " .
341 "prices.pr_id = '$newcode' AND " .
342 "prices.pr_selector = '$newsel' AND " .
343 "prices.pr_level = patient_data.pricelevel " .
344 "LIMIT 1");
345 $fee = empty($prrow) ? 0 : $prrow['pr_price'];
346 echoProdLine(++$prod_lino, $newcode, FALSE, $units, $fee);
348 else {
349 list($code, $modifier) = explode(":", $newcode);
350 $ndc_info = '';
351 // If HCPCS, find last NDC string used for this code.
352 if ($newtype == 'HCPCS' && $ndc_applies) {
353 $tmp = sqlQuery("SELECT ndc_info FROM billing WHERE " .
354 "code_type = '$newtype' AND code = '$code' AND ndc_info LIKE 'N4%' " .
355 "ORDER BY date DESC LIMIT 1");
356 if (!empty($tmp)) $ndc_info = $tmp['ndc_info'];
358 echoLine(++$bill_lino, $newtype, $code, trim($modifier), $ndc_info);
363 </table>
364 </p>
366 <br>
367 &nbsp;
369 <span class="billcell">PROVIDER:</span>
372 // Build a drop-down list of providers. This includes users who
373 // have the word "provider" anywhere in their "additional info"
374 // field, so that we can define providers (for billing purposes)
375 // who do not appear in the calendar.
377 $query = "SELECT id, lname, fname FROM users WHERE " .
378 "authorized = 1 OR info LIKE '%provider%' ORDER BY lname, fname";
379 $res = sqlStatement($query);
381 echo " <select name='ProviderID'>\n";
382 echo " <option value=''>-- Please Select --\n";
384 while ($row = sqlFetchArray($res)) {
385 $provid = $row['id'];
386 echo " <option value='$provid'";
387 if ($provid == $encounter_provid) echo " selected";
388 echo ">" . $row['lname'] . ", " . $row['fname'] . "\n";
391 echo " </select>\n";
394 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
396 <input type='submit' name='bn_save' value='Save' />
397 &nbsp;
398 <input type='submit' name='bn_refresh' value='Refresh'>
399 &nbsp;
400 <input type='button' value='Cancel' onclick='docancel()' />
402 <?php if ($code_types['UCSMC']) { ?>
403 <p style='font-family:sans-serif;font-size:8pt;color:#666666;'>
404 &nbsp;<br>
405 UCSMC codes provided by the University of Calgary Sports Medicine Centre
406 </p>
407 <? } ?>
409 </center>
411 </form>
412 <?php
413 // TBD: If $alertmsg, display it with a JavaScript alert().