2 // Copyright (C) 2005-2006 Rod Roark <rod@sunsetsystems.com>
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;
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";
37 // If the item is already in the database...
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...
52 $query = "select code_text from codes where code_type = '" .
53 $code_types[$code_type]['id'] . "' and " .
54 "code = '$code' and ";
56 $query .= "modifier = '$modifier'";
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);
73 .billcell
{ font
-family
: sans
-serif
; font
-size
: 10pt
}
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
;
83 var f
= document
.forms
[0];
84 f
.newcodes
.value
= selobj
.options
[i
].value
;
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
=''>
101 // Helper function for creating drop-lists.
102 function endFSCategory() {
103 global $i, $last_category, $FEE_SHEET_COLUMNS;
104 if (! $last_category) return;
107 if ($i >= $FEE_SHEET_COLUMNS) {
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) {
122 $last_category = $fs_category;
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";
133 $search_type = $default_search_type;
134 if ($_POST['search_type']) $search_type = $_POST['search_type'];
136 echo $i ?
" <td></td>\n </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!
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'] . "' " .
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';
158 echo " <option value=''> Search Results ($numrows items)\n";
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";
176 <p style
='margin-top:8px;margin-bottom:8px'>
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 \n";
192 <input type
='text' name
='search_term' value
=''>  
;
195 <input type
='submit' name
='bn_search' value
='Search'>
201 <p style
='margin-top:16px;margin-bottom:8px'>
202 <table cellspacing
='5'>
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
>
209 <?php
if (fees_are_used()) { ?
>
210 <td
class='billcell' align
='right'><b
>Fee
</b
> 
;</td
>
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
>
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)
226 $query = "select fee, code_text from codes where code_type = '" .
227 $code_types[$codetype]['id'] . "' and " .
228 "code = '$code' and ";
230 $query .= "modifier = '$modifier'";
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>" : "";
241 echo " <td class='billcell'>$strike1$codetype$strike2";
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'>";
249 echo " <td class='billcell'>$strike1$code$strike2</td>\n";
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";
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";
268 echo " <td class='billcell'> </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";
276 echo " <td class='billcell'> </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";
288 // Generate lines for items already in the database.
291 $encounter_provid = -1;
292 if ($result = getBillingByEncounter($pid, $encounter, "*") ) {
293 foreach ($result as $iter) {
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 " .
347 $fee = empty($prrow) ?
0 : $prrow['pr_price'];
348 echoProdLine(++
$prod_lino, $newcode, FALSE, $units, $fee);
351 list($code, $modifier) = explode(":", $newcode);
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);
371 <span
class="billcell">PROVIDER
:</span
>
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";
397  
;  
;  
;  
;  
;  
;  
;  
;  
;  
;
399 <input type
='submit' name
='bn_save' value
='Save' />
401 <input type
='submit' name
='bn_refresh' value
='Refresh'>
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;'>
408 UCSMC codes provided by the University of Calgary Sports Medicine Centre
416 // TBD: If $alertmsg, display it with a JavaScript alert().