Support for storing code type in justify column of billing sql table
[openemr.git] / interface / forms / fee_sheet / new.php
blob6edb4c85fa51878cef325b93ccfd634e3319992f
1 <?php
2 // Copyright (C) 2005-2011 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 $fake_register_globals=false;
10 $sanitize_all_escapes=true;
12 require_once("../../globals.php");
13 require_once("$srcdir/acl.inc");
14 require_once("$srcdir/api.inc");
15 require_once("codes.php");
16 require_once("../../../custom/code_types.inc.php");
17 require_once("../../drugs/drugs.inc.php");
18 require_once("$srcdir/formatting.inc.php");
19 require_once("$srcdir/options.inc.php");
20 require_once("$srcdir/formdata.inc.php");
22 // Some table cells will not be displayed unless insurance billing is used.
23 $usbillstyle = $GLOBALS['ippf_specific'] ? " style='display:none'" : "";
25 function alphaCodeType($id) {
26 global $code_types;
27 foreach ($code_types as $key => $value) {
28 if ($value['id'] == $id) return $key;
30 return '';
33 // Helper function for creating drop-lists.
34 function endFSCategory() {
35 global $i, $last_category, $FEE_SHEET_COLUMNS;
36 if (! $last_category) return;
37 echo " </select>\n";
38 echo " </td>\n";
39 if ($i >= $FEE_SHEET_COLUMNS) {
40 echo " </tr>\n";
41 $i = 0;
45 // Generate JavaScript to build the array of diagnoses.
46 function genDiagJS($code_type, $code) {
47 global $code_types;
48 if ($code_types[$code_type]['diag']) {
49 echo "diags.push('" . attr($code_type) . "|" . attr($code) . "');\n";
53 // For IPPF only. Returns 0 = none, 1 = nonsurgical, 2 = surgical.
55 function contraceptionClass($code_type, $code) {
56 global $code_types;
57 if (!$GLOBALS['ippf_specific']) return 0;
58 $contra = 0;
59 // Get the related service codes.
60 $codesrow = sqlQuery("SELECT related_code FROM codes WHERE " .
61 "code_type = ? " .
62 " AND code = ? LIMIT 1", array($code_types[$code_type]['id'],$code) );
63 if (!empty($codesrow['related_code']) && $code_type == 'MA') {
64 $relcodes = explode(';', $codesrow['related_code']);
65 foreach ($relcodes as $relstring) {
66 if ($relstring === '') continue;
67 list($reltype, $relcode) = explode(':', $relstring);
68 if ($reltype !== 'IPPF') continue;
69 if (preg_match('/^11....110/' , $relcode)) $contra |= 1;
70 else if (preg_match('/^11....999/' , $relcode)) $contra |= 1;
71 else if (preg_match('/^112152010/' , $relcode)) $contra |= 1;
72 else if (preg_match('/^11317[1-2]111/', $relcode)) $contra |= 1;
73 else if (preg_match('/^12118[1-2].13/', $relcode)) $contra |= 2;
74 else if (preg_match('/^12118[1-2]999/', $relcode)) $contra |= 2;
77 return $contra;
80 // This writes a billing line item to the output page.
82 function echoLine($lino, $codetype, $code, $modifier, $ndc_info='',
83 $auth = TRUE, $del = FALSE, $units = NULL, $fee = NULL, $id = NULL,
84 $billed = FALSE, $code_text = NULL, $justify = NULL, $provider_id = 0, $notecodes='')
86 global $code_types, $ndc_applies, $ndc_uom_choices, $justinit, $pid;
87 global $contraception, $usbillstyle, $hasCharges;
89 if ($codetype == 'COPAY') {
90 if (!$code_text) $code_text = 'Cash';
91 if ($fee > 0) $fee = 0 - $fee;
93 if (! $code_text) {
94 $sqlArray = array();
95 $query = "select id, units, code_text from codes where code_type = ? " .
96 " and " .
97 "code = ? and ";
98 array_push($sqlArray,$code_types[$codetype]['id'],$code);
99 if ($modifier) {
100 $query .= "modifier = ?";
101 array_push($sqlArray,$modifier);
102 } else {
103 $query .= "(modifier is null or modifier = '')";
105 $result = sqlQuery($query, $sqlArray);
106 $code_text = $result['code_text'];
107 if (empty($units)) $units = max(1, intval($result['units']));
108 if (!isset($fee)) {
109 // Fees come from the prices table now.
110 $query = "SELECT prices.pr_price " .
111 "FROM patient_data, prices WHERE " .
112 "patient_data.pid = ? AND " .
113 "prices.pr_id = ? AND " .
114 "prices.pr_selector = '' AND " .
115 "prices.pr_level = patient_data.pricelevel " .
116 "LIMIT 1";
117 echo "\n<!-- $query -->\n"; // debugging
118 $prrow = sqlQuery($query, array($pid,$result['id']) );
119 $fee = empty($prrow) ? 0 : $prrow['pr_price'];
122 $fee = sprintf('%01.2f', $fee);
123 if (empty($units)) $units = 1;
124 $units = max(1, intval($units));
125 // We put unit price on the screen, not the total line item fee.
126 $price = $fee / $units;
127 $strike1 = ($id && $del) ? "<strike>" : "";
128 $strike2 = ($id && $del) ? "</strike>" : "";
129 echo " <tr>\n";
130 echo " <td class='billcell'>$strike1" .
131 ($codetype == 'COPAY' ? xl($codetype) : $codetype) . $strike2;
132 //if the line to ouput is copay, show the date here passed as $ndc_info,
133 //since this variable is not applicable in the case of copay.
134 if($codetype == 'COPAY'){
135 echo "(".htmlspecialchars($ndc_info).")";
136 $ndc_info = '';
138 if ($id) {
139 echo "<input type='hidden' name='bill[".attr($lino)."][id]' value='$id'>";
141 echo "<input type='hidden' name='bill[".attr($lino)."][code_type]' value='".attr($codetype)."'>";
142 echo "<input type='hidden' name='bill[".attr($lino)."][code]' value='".attr($code)."'>";
143 echo "<input type='hidden' name='bill[".attr($lino)."][billed]' value='".attr($billed)."'>";
144 echo "</td>\n";
145 if ($codetype != 'COPAY') {
146 echo " <td class='billcell'>$strike1" . text($code) . "$strike2</td>\n";
147 } else {
148 echo " <td class='billcell'>&nbsp;</td>\n";
150 if ($billed) {
151 if (modifiers_are_used(true)) {
152 echo " <td class='billcell'>$strike1" . text($modifier) . "$strike2" .
153 "<input type='hidden' name='bill[".attr($lino)."][mod]' value='".attr($modifier)."'></td>\n";
155 if (fees_are_used()) {
156 echo " <td class='billcell' align='right'>" . text(oeFormatMoney($price)) . "</td>\n";
157 if ($codetype != 'COPAY') {
158 echo " <td class='billcell' align='center'>" . text($units) . "</td>\n";
159 } else {
160 echo " <td class='billcell'>&nbsp;</td>\n";
162 echo " <td class='billcell' align='center'$usbillstyle>" . text($justify) . "</td>\n";
165 // Show provider for this line.
166 echo " <td class='billcell' align='center'>";
167 genProviderSelect('', '-- '.xl("Default").' --', $provider_id, true);
168 echo "</td>\n";
169 if ($codetype == 'HCPCS' || $codetype == 'CPT4') {
170 echo " <td class='billcell' align='center'$usbillstyle>" .
171 htmlspecialchars($notecodes, ENT_NOQUOTES) . "</td>\n";
173 else {
174 echo " <td class='billcell' align='center'$usbillstyle></td>\n";
176 echo " <td class='billcell' align='center'$usbillstyle><input type='checkbox'" .
177 ($auth ? " checked" : "") . " disabled /></td>\n";
178 echo " <td class='billcell' align='center'><input type='checkbox'" .
179 " disabled /></td>\n";
181 else { // not billed
182 if (modifiers_are_used(true)) {
183 if ($codetype != 'COPAY' && ($code_types[$codetype]['mod'] || $modifier)) {
184 echo " <td class='billcell'><input type='text' name='bill[".attr($lino)."][mod]' " .
185 "value='" . attr($modifier) . "' " .
186 "title='" . xla("Multiple modifiers can be separated by colons or spaces, maximum of 4 (M1:M2:M3:M4)") . "' " .
187 "value='" . attr($modifier) . "' size='" . attr($code_types[$codetype]['mod']) . "'></td>\n";
188 } else {
189 echo " <td class='billcell'>&nbsp;</td>\n";
192 if (fees_are_used()) {
193 if ($codetype == 'COPAY' || $code_types[$codetype]['fee'] || $fee != 0) {
194 echo " <td class='billcell' align='right'>" .
195 "<input type='text' name='bill[".attr($lino)."][price]' " .
196 "value='" . attr($price) . "' size='6'";
197 if (acl_check('acct','disc'))
198 echo " style='text-align:right'";
199 else
200 echo " style='text-align:right;background-color:transparent' readonly";
201 echo "></td>\n";
202 echo " <td class='billcell' align='center'>";
203 if ($codetype != 'COPAY') {
204 echo "<input type='text' name='bill[".attr($lino)."][units]' " .
205 "value='" . attr($units) . "' size='2' style='text-align:right'>";
206 } else {
207 echo "<input type='hidden' name='bill[".attr($lino)."][units]' value='" . attr($units) . "'>";
209 echo "</td>\n";
210 if ($code_types[$codetype]['just'] || $justify) {
211 echo " <td class='billcell' align='center'$usbillstyle ";
212 echo "title='" . xla("Select one or more diagnosis codes to justify the service") . "' >";
213 echo "<select name='bill[".attr($lino)."][justify]' onchange='setJustify(this)'>";
214 echo "<option value='" . attr($justify) . "'>" . text($justify) . "</option></select>";
215 echo "</td>\n";
216 $justinit .= "setJustify(f['bill[".attr($lino)."][justify]']);\n";
217 } else {
218 echo " <td class='billcell'$usbillstyle>&nbsp;</td>\n";
220 } else {
221 echo " <td class='billcell'>&nbsp;</td>\n";
222 echo " <td class='billcell'>&nbsp;</td>\n";
223 echo " <td class='billcell'$usbillstyle>&nbsp;</td>\n"; // justify
227 // Provider drop-list for this line.
228 echo " <td class='billcell' align='center'>";
229 genProviderSelect("bill[$lino][provid]", '-- '.xl("Default").' --', $provider_id);
230 echo "</td>\n";
231 if ($codetype == 'HCPCS' || $codetype == 'CPT4') {
232 echo " <td class='billcell' align='center'$usbillstyle><input type='text' name='bill[".attr($lino)."][notecodes]' " .
233 "value='" . htmlspecialchars($notecodes, ENT_QUOTES) . "' maxlength='10' size='8' /></td>\n";
235 else {
236 echo " <td class='billcell' align='center'$usbillstyle></td>\n";
238 echo " <td class='billcell' align='center'$usbillstyle><input type='checkbox' name='bill[".attr($lino)."][auth]' " .
239 "value='1'" . ($auth ? " checked" : "") . " /></td>\n";
240 echo " <td class='billcell' align='center'><input type='checkbox' name='bill[".attr($lino)."][del]' " .
241 "value='1'" . ($del ? " checked" : "") . " /></td>\n";
244 echo " <td class='billcell'>$strike1" . text($code_text) . "$strike2</td>\n";
245 echo " </tr>\n";
247 // If NDC info exists or may be required, add a line for it.
248 if ($codetype == 'HCPCS' && $ndc_applies && !$billed) {
249 $ndcnum = ''; $ndcuom = ''; $ndcqty = '';
250 if (preg_match('/^N4(\S+)\s+(\S\S)(.*)/', $ndc_info, $tmp)) {
251 $ndcnum = $tmp[1]; $ndcuom = $tmp[2]; $ndcqty = $tmp[3];
253 echo " <tr>\n";
254 echo " <td class='billcell' colspan='2'>&nbsp;</td>\n";
255 echo " <td class='billcell' colspan='6'>&nbsp;NDC:&nbsp;";
256 echo "<input type='text' name='bill[".attr($lino)."][ndcnum]' value='" . attr($ndcnum) . "' " .
257 "size='11' style='background-color:transparent'>";
258 echo " &nbsp;Qty:&nbsp;";
259 echo "<input type='text' name='bill[".attr($lino)."][ndcqty]' value='" . attr($ndcqty) . "' " .
260 "size='3' style='background-color:transparent;text-align:right'>";
261 echo " ";
262 echo "<select name='bill[".attr($lino)."][ndcuom]' style='background-color:transparent'>";
263 foreach ($ndc_uom_choices as $key => $value) {
264 echo "<option value='" . attr($key) . "'";
265 if ($key == $ndcuom) echo " selected";
266 echo ">" . text($value) . "</option>";
268 echo "</select>";
269 echo "</td>\n";
270 echo " </tr>\n";
272 else if ($ndc_info) {
273 echo " <tr>\n";
274 echo " <td class='billcell' colspan='2'>&nbsp;</td>\n";
275 echo " <td class='billcell' colspan='6'>&nbsp;" . xlt("NDC Data") . ": " . text($ndc_info) . "</td>\n";
276 echo " </tr>\n";
279 // For IPPF. Track contraceptive services.
280 if (!$del) $contraception |= contraceptionClass($codetype, $code);
282 if ($fee != 0) $hasCharges = true;
285 // This writes a product (drug_sales) line item to the output page.
287 function echoProdLine($lino, $drug_id, $del = FALSE, $units = NULL,
288 $fee = NULL, $sale_id = 0, $billed = FALSE)
290 global $code_types, $ndc_applies, $pid, $usbillstyle, $hasCharges;
292 $drow = sqlQuery("SELECT name FROM drugs WHERE drug_id = ?", array($drug_id) );
293 $code_text = $drow['name'];
295 $fee = sprintf('%01.2f', $fee);
296 if (empty($units)) $units = 1;
297 $units = max(1, intval($units));
298 // We put unit price on the screen, not the total line item fee.
299 $price = $fee / $units;
300 $strike1 = ($sale_id && $del) ? "<strike>" : "";
301 $strike2 = ($sale_id && $del) ? "</strike>" : "";
302 echo " <tr>\n";
303 echo " <td class='billcell'>{$strike1}" . xlt("Product") . "$strike2";
304 echo "<input type='hidden' name='prod[".attr($lino)."][sale_id]' value='" . attr($sale_id) . "'>";
305 echo "<input type='hidden' name='prod[".attr($lino)."][drug_id]' value='" . attr($drug_id) . "'>";
306 echo "<input type='hidden' name='prod[".attr($lino)."][billed]' value='" . attr($billed) . "'>";
307 echo "</td>\n";
308 echo " <td class='billcell'>$strike1" . text($drug_id) . "$strike2</td>\n";
309 if (modifiers_are_used(true)) {
310 echo " <td class='billcell'>&nbsp;</td>\n";
312 if ($billed) {
313 if (fees_are_used()) {
314 echo " <td class='billcell' align='right'>" . text(oeFormatMoney($price)) . "</td>\n";
315 echo " <td class='billcell' align='center'>" . text($units) . "</td>\n";
316 echo " <td class='billcell' align='center'$usbillstyle>&nbsp;</td>\n"; // justify
318 echo " <td class='billcell' align='center'>&nbsp;</td>\n"; // provider
319 echo " <td class='billcell' align='center'$usbillstyle>&nbsp;</td>\n"; // auth
320 echo " <td class='billcell' align='center'><input type='checkbox'" . // del
321 " disabled /></td>\n";
322 } else {
323 if (fees_are_used()) {
324 echo " <td class='billcell' align='right'>" .
325 "<input type='text' name='prod[".attr($lino)."][price]' " .
326 "value='" . attr($price) . "' size='6'";
327 if (acl_check('acct','disc'))
328 echo " style='text-align:right'";
329 else
330 echo " style='text-align:right;background-color:transparent' readonly";
331 echo "></td>\n";
332 echo " <td class='billcell' align='center'>";
333 echo "<input type='text' name='prod[".attr($lino)."][units]' " .
334 "value='" . attr($units) . "' size='2' style='text-align:right'>";
335 echo "</td>\n";
336 echo " <td class='billcell'$usbillstyle>&nbsp;</td>\n"; // justify
338 echo " <td class='billcell' align='center'>&nbsp;</td>\n"; // provider
339 echo " <td class='billcell' align='center'$usbillstyle>&nbsp;</td>\n"; // auth
340 echo " <td class='billcell' align='center'><input type='checkbox' name='prod[".attr($lino)."][del]' " .
341 "value='1'" . ($del ? " checked" : "") . " /></td>\n";
344 echo " <td class='billcell'>$strike1" . text($code_text) . "$strike2</td>\n";
345 echo " </tr>\n";
347 if ($fee != 0) $hasCharges = true;
350 // Build a drop-down list of providers. This includes users who
351 // have the word "provider" anywhere in their "additional info"
352 // field, so that we can define providers (for billing purposes)
353 // who do not appear in the calendar.
355 function genProviderSelect($selname, $toptext, $default=0, $disabled=false) {
356 $query = "SELECT id, lname, fname FROM users WHERE " .
357 "( authorized = 1 OR info LIKE '%provider%' ) AND username != '' " .
358 "AND active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
359 "ORDER BY lname, fname";
360 $res = sqlStatement($query);
361 echo " <select name='" . attr($selname) . "'";
362 if ($disabled) echo " disabled";
363 echo ">\n";
364 echo " <option value=''>" . text($toptext) . "\n";
365 while ($row = sqlFetchArray($res)) {
366 $provid = $row['id'];
367 echo " <option value='" . attr($provid) . "'";
368 if ($provid == $default) echo " selected";
369 echo ">" . text($row['lname'] . ", " . $row['fname']) . "\n";
371 echo " </select>\n";
374 // This is just for IPPF, to indicate if the visit includes contraceptive services.
375 $contraception = 0;
377 // Possible units of measure for NDC drug quantities.
379 $ndc_uom_choices = array(
380 'ML' => 'ML',
381 'GR' => 'Grams',
382 'ME' => 'Milligrams',
383 'F2' => 'I.U.',
384 'UN' => 'Units'
387 // $FEE_SHEET_COLUMNS should be defined in codes.php.
388 if (empty($FEE_SHEET_COLUMNS)) $FEE_SHEET_COLUMNS = 2;
390 $returnurl = $GLOBALS['concurrent_layout'] ? 'encounter_top.php' : 'patient_encounter.php';
392 // Update price level in patient demographics.
393 if (!empty($_POST['pricelevel'])) {
394 sqlStatement("UPDATE patient_data SET pricelevel = ? WHERE pid = ?", array($_POST['pricelevel'],$pid) );
397 // Get some info about this visit.
398 $visit_row = sqlQuery("SELECT fe.date, opc.pc_catname " .
399 "FROM form_encounter AS fe " .
400 "LEFT JOIN openemr_postcalendar_categories AS opc ON opc.pc_catid = fe.pc_catid " .
401 "WHERE fe.pid = ? AND fe.encounter = ? LIMIT 1", array($pid,$encounter) );
402 $visit_date = substr($visit_row['date'], 0, 10);
404 // If Save or Save-and-Close was clicked, save the new and modified billing
405 // lines; then if no error, redirect to $returnurl.
407 if ($_POST['bn_save'] || $_POST['bn_save_close']) {
408 $main_provid = 0 + $_POST['ProviderID'];
409 $main_supid = 0 + $_POST['SupervisorID'];
410 if ($main_supid == $main_provid) $main_supid = 0;
411 $default_warehouse = $_POST['default_warehouse'];
413 $bill = $_POST['bill'];
414 $copay_update = FALSE;
415 $update_session_id = '';
416 $cod0 = '';//takes the code of the first non-empty modifier from the fee sheet, against which the copay is posted
417 $mod0 = '';//takes the first non-empty modifier from the fee sheet, against which the copay is posted
418 for ($lino = 1; $bill["$lino"]['code_type']; ++$lino) {
419 $iter = $bill["$lino"];
420 $code_type = $iter['code_type'];
421 $code = $iter['code'];
422 $del = $iter['del'];
424 // Skip disabled (billed) line items.
425 if ($iter['billed']) continue;
427 $id = $iter['id'];
428 $modifier = trim($iter['mod']);
429 if(!$mod0 && $modifier != ''){
430 $mod0 = $modifier;
431 $cod0 = $code;
433 $units = max(1, intval(trim($iter['units'])));
434 $fee = sprintf('%01.2f',(0 + trim($iter['price'])) * $units);
436 if($code_type == 'COPAY'){
437 if($id == ''){
438 //adding new copay from fee sheet into ar_session and ar_activity tables
439 if($fee < 0){
440 $fee = $fee * -1;
442 $session_id = idSqlStatement("INSERT INTO ar_session(payer_id,user_id,pay_total,payment_type,description,".
443 "patient_id,payment_method,adjustment_code,post_to_date) VALUES('0',?,?,'patient','COPAY',?,'','patient_payment',now())",
444 array($_SESSION['authId'],$fee,$pid));
445 SqlStatement("INSERT INTO ar_activity (pid,encounter,code,modifier,payer_type,post_time,post_user,session_id,".
446 "pay_amount,account_code) VALUES (?,?,?,?,0,now(),?,?,?,'PCP')",
447 array($pid,$encounter,$cod0,$mod0,$_SESSION['authId'],$session_id,$fee));
448 }else{
449 //editing copay saved to ar_session and ar_activity
450 if($fee < 0){
451 $fee = $fee * -1;
453 $session_id = $id;
454 $res_amount = sqlQuery("SELECT pay_amount FROM ar_activity WHERE pid=? AND encounter=? AND session_id=?",
455 array($pid,$encounter,$session_id));
456 if($fee != $res_amount['pay_amount']){
457 sqlStatement("UPDATE ar_session SET user_id=?,pay_total=?,modified_time=now(),post_to_date=now() WHERE session_id=?",
458 array($_SESSION['authId'],$fee,$session_id));
459 sqlStatement("UPDATE ar_activity SET code=?, modifier=?, post_user=?, post_time=now(),".
460 "pay_amount=?, modified_time=now() WHERE pid=? AND encounter=? AND account_code='PCP' AND session_id=?",
461 array($cod0,$mod0,$_SESSION['authId'],$fee,$pid,$encounter,$session_id));
464 if(!$mod0){
465 $copay_update = TRUE;
466 $update_session_id = $session_id;
468 continue;
470 $justify = trim($iter['justify']);
471 $notecodes = trim($iter['notecodes']);
472 if ($justify) $justify = str_replace(',', ':', $justify) . ':';
473 // $auth = $iter['auth'] ? "1" : "0";
474 $auth = "1";
475 $provid = 0 + $iter['provid'];
477 $ndc_info = '';
478 if ($iter['ndcnum']) {
479 $ndc_info = 'N4' . trim($iter['ndcnum']) . ' ' . $iter['ndcuom'] .
480 trim($iter['ndcqty']);
483 // If the item is already in the database...
484 if ($id) {
485 if ($del) {
486 deleteBilling($id);
488 else {
489 // authorizeBilling($id, $auth);
490 sqlQuery("UPDATE billing SET code = ?, " .
491 "units = ?, fee = ?, modifier = ?, " .
492 "authorized = ?, provider_id = ?, " .
493 "ndc_info = ?, justify = ?, notecodes = ? " .
494 "WHERE " .
495 "id = ? AND billed = 0 AND activity = 1", array($code,$units,$fee,$modifier,$auth,$provid,$ndc_info,$justify,$notecodes,$id) );
499 // Otherwise it's a new item...
500 else if (! $del) {
501 $code_text = lookup_code_descriptions($code_type.":".$code);
502 addBilling($encounter, $code_type, $code, $code_text, $pid, $auth,
503 $provid, $modifier, $units, $fee, $ndc_info, $justify, 0, $notecodes);
505 } // end for
507 //if modifier is not inserted during loop update the record using the first
508 //non-empty modifier and code
509 if($copay_update == TRUE && $update_session_id != '' && $mod0 != ''){
510 sqlStatement("UPDATE ar_activity SET code=?, modifier=?".
511 " WHERE pid=? AND encounter=? AND account_code='PCP' AND session_id=?",
512 array($cod0,$mod0,$pid,$encounter,$update_session_id));
515 // Doing similarly to the above but for products.
516 $prod = $_POST['prod'];
517 for ($lino = 1; $prod["$lino"]['drug_id']; ++$lino) {
518 $iter = $prod["$lino"];
520 if (!empty($iter['billed'])) continue;
522 $drug_id = $iter['drug_id'];
523 $sale_id = $iter['sale_id']; // present only if already saved
524 $units = max(1, intval(trim($iter['units'])));
525 $fee = sprintf('%01.2f',(0 + trim($iter['price'])) * $units);
526 $del = $iter['del'];
528 // If the item is already in the database...
529 if ($sale_id) {
530 if ($del) {
531 // Zero out this sale and reverse its inventory update. We bring in
532 // drug_sales twice so that the original quantity can be referenced
533 // unambiguously.
534 sqlStatement("UPDATE drug_sales AS dsr, drug_sales AS ds, " .
535 "drug_inventory AS di " .
536 "SET di.on_hand = di.on_hand + dsr.quantity, " .
537 "ds.quantity = 0, ds.fee = 0 WHERE " .
538 "dsr.sale_id = ? AND ds.sale_id = dsr.sale_id AND " .
539 "di.inventory_id = ds.inventory_id", array($sale_id) );
540 // And delete the sale for good measure.
541 sqlStatement("DELETE FROM drug_sales WHERE sale_id = ?", array($sale_id) );
543 else {
544 // Modify the sale and adjust inventory accordingly.
545 $query = "UPDATE drug_sales AS dsr, drug_sales AS ds, " .
546 "drug_inventory AS di " .
547 "SET di.on_hand = di.on_hand + dsr.quantity - " . add_escape_custom($units) . ", " .
548 "ds.quantity = ?, ds.fee = ?, " .
549 "ds.sale_date = ? WHERE " .
550 "dsr.sale_id = ? AND ds.sale_id = dsr.sale_id AND " .
551 "di.inventory_id = ds.inventory_id";
552 sqlStatement($query, array($units,$fee,$visit_date,$sale_id) );
556 // Otherwise it's a new item...
557 else if (! $del) {
558 $sale_id = sellDrug($drug_id, $units, $fee, $pid, $encounter, 0,
559 $visit_date, '', $default_warehouse);
560 if (!$sale_id) die(xlt("Insufficient inventory for product ID") . " \"" . text($drug_id) . "\".");
562 } // end for
564 // Set the main/default service provider in the new-encounter form.
565 /*******************************************************************
566 sqlStatement("UPDATE forms, users SET forms.user = users.username WHERE " .
567 "forms.pid = '$pid' AND forms.encounter = '$encounter' AND " .
568 "forms.formdir = 'newpatient' AND users.id = '$provid'");
569 *******************************************************************/
570 sqlStatement("UPDATE form_encounter SET provider_id = ?, " .
571 "supervisor_id = ? WHERE " .
572 "pid = ? AND encounter = ?", array($main_provid,$main_supid,$pid,$encounter) );
574 // Save-and-Close is currently IPPF-specific but might be more generally
575 // useful. It provides the ability to mark an encounter as billed
576 // directly from the Fee Sheet, if there are no charges.
577 if ($_POST['bn_save_close']) {
578 $tmp1 = sqlQuery("SELECT SUM(ABS(fee)) AS sum FROM drug_sales WHERE " .
579 "pid = ? AND encounter = ?", array($pid,$encounter) );
580 $tmp2 = sqlQuery("SELECT SUM(ABS(fee)) AS sum FROM billing WHERE " .
581 "pid = ? AND encounter = ? AND billed = 0 AND " .
582 "activity = 1", array($pid,$encounter) );
583 if ($tmp1['sum'] + $tmp2['sum'] == 0) {
584 sqlStatement("update drug_sales SET billed = 1 WHERE " .
585 "pid = ? AND encounter = ? AND billed = 0", array($pid,$encounter));
586 sqlStatement("UPDATE billing SET billed = 1, bill_date = NOW() WHERE " .
587 "pid = ? AND encounter = ? AND billed = 0 AND " .
588 "activity = 1", array($pid,$encounter));
590 else {
591 // Would be good to display an error message here... they clicked
592 // Save and Close but the close could not be done. However the
593 // framework does not provide an easy way to do that.
597 // More IPPF stuff.
598 if (!empty($_POST['contrastart'])) {
599 $contrastart = $_POST['contrastart'];
600 sqlStatement("UPDATE patient_data SET contrastart = ?" .
601 " WHERE pid = ?", array($contrastart,$pid) );
604 // Note: Taxes are computed at checkout time (in pos_checkout.php which
605 // also posts to SL). Currently taxes with insurance claims make no sense,
606 // so for now we'll ignore tax computation in the insurance billing logic.
608 formHeader("Redirecting....");
609 formJump();
610 formFooter();
611 exit;
614 $billresult = getBillingByEncounter($pid, $encounter, "*");
616 <html>
617 <head>
618 <?php html_header_show(); ?>
619 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
620 <style>
621 .billcell { font-family: sans-serif; font-size: 10pt }
622 </style>
623 <script language="JavaScript">
625 var diags = new Array();
627 <?php
628 if ($billresult) {
629 foreach ($billresult as $iter) {
630 genDiagJS($iter["code_type"], trim($iter["code"]));
633 if ($_POST['bill']) {
634 foreach ($_POST['bill'] as $iter) {
635 if ($iter["del"]) continue; // skip if Delete was checked
636 if ($iter["id"]) continue; // skip if it came from the database
637 genDiagJS($iter["code_type"], $iter["code"]);
640 if ($_POST['newcodes']) {
641 $arrcodes = explode('~', $_POST['newcodes']);
642 foreach ($arrcodes as $codestring) {
643 if ($codestring === '') continue;
644 $arrcode = explode('|', $codestring);
645 list($code, $modifier) = explode(":", $arrcode[1]);
646 genDiagJS($arrcode[0], $code);
651 // This is invoked by <select onchange> for the various dropdowns,
652 // including search results.
653 function codeselect(selobj) {
654 var i = selobj.selectedIndex;
655 if (i > 0) {
656 top.restoreSession();
657 var f = document.forms[0];
658 f.newcodes.value = selobj.options[i].value;
659 f.submit();
663 function copayselect() {
664 top.restoreSession();
665 var f = document.forms[0];
666 f.newcodes.value = 'COPAY||';
667 f.submit();
670 function validate(f) {
671 for (var lino = 1; f['bill['+lino+'][code_type]']; ++lino) {
672 var pfx = 'bill['+lino+']';
673 if (f[pfx+'[ndcnum]'] && f[pfx+'[ndcnum]'].value) {
674 // Check NDC number format.
675 var ndcok = true;
676 var ndc = f[pfx+'[ndcnum]'].value;
677 var a = ndc.split('-');
678 if (a.length != 3) {
679 ndcok = false;
681 else if (a[0].length < 1 || a[1].length < 1 || a[2].length < 1 ||
682 a[0].length > 5 || a[1].length > 4 || a[2].length > 2) {
683 ndcok = false;
685 else {
686 for (var i = 0; i < 3; ++i) {
687 for (var j = 0; j < a[i].length; ++j) {
688 var c = a[i].charAt(j);
689 if (c < '0' || c > '9') ndcok = false;
693 if (!ndcok) {
694 alert('<?php echo addslashes(xl('Format incorrect for NDC')) ?> "' + ndc +
695 '", <?php echo addslashes(xl('should be like nnnnn-nnnn-nn')) ?>');
696 if (f[pfx+'[ndcnum]'].focus) f[pfx+'[ndcnum]'].focus();
697 return false;
699 // Check for valid quantity.
700 var qty = f[pfx+'[ndcqty]'].value - 0;
701 if (isNaN(qty) || qty <= 0) {
702 alert('<?php echo addslashes(xl('Quantity for NDC')) ?> "' + ndc +
703 '" <?php echo addslashes(xl('is not valid (decimal fractions are OK).')) ?>');
704 if (f[pfx+'[ndcqty]'].focus) f[pfx+'[ndcqty]'].focus();
705 return false;
709 top.restoreSession();
710 return true;
713 // When a justify selection is made, apply it to the current list for
714 // this procedure and then rebuild its selection list.
716 function setJustify(seljust) {
717 var theopts = seljust.options;
718 var jdisplay = theopts[0].text;
719 // Compute revised justification string. Note this does nothing if
720 // the first entry is still selected, which is handy at startup.
721 if (seljust.selectedIndex > 0) {
722 var newdiag = seljust.value;
723 if (newdiag.length == 0) {
724 jdisplay = '';
726 else {
727 if (jdisplay.length) jdisplay += ',';
728 jdisplay += newdiag;
731 // Rebuild selection list.
732 var jhaystack = ',' + jdisplay + ',';
733 var j = 0;
734 theopts.length = 0;
735 theopts[j++] = new Option(jdisplay,jdisplay,true,true);
736 for (var i = 0; i < diags.length; ++i) {
737 if (jhaystack.indexOf(',' + diags[i] + ',') < 0) {
738 theopts[j++] = new Option(diags[i],diags[i],false,false);
741 theopts[j++] = new Option('Clear','',false,false);
744 </script>
745 </head>
747 <body class="body_top">
748 <form method="post" action="<?php echo $rootdir; ?>/forms/fee_sheet/new.php"
749 onsubmit="return validate(this)">
750 <span class="title"><?php echo xlt('Fee Sheet'); ?></span><br>
751 <input type='hidden' name='newcodes' value=''>
753 <center>
755 <?php
756 $isBilled = isEncounterBilled($pid, $encounter);
757 if ($isBilled) {
758 echo "<p><font color='green'>" . xlt("This encounter has been billed. If you need to change it, it must be re-opened.") . "</font></p>\n";
760 else { // the encounter is not yet billed
763 <table width='95%'>
764 <?php
765 $i = 0;
766 $last_category = '';
768 // Create drop-lists based on the fee_sheet_options table.
769 $res = sqlStatement("SELECT * FROM fee_sheet_options " .
770 "ORDER BY fs_category, fs_option");
771 while ($row = sqlFetchArray($res)) {
772 $fs_category = $row['fs_category'];
773 $fs_option = $row['fs_option'];
774 $fs_codes = $row['fs_codes'];
775 if($fs_category !== $last_category) {
776 endFSCategory();
777 $last_category = $fs_category;
778 ++$i;
779 echo ($i <= 1) ? " <tr>\n" : "";
780 echo " <td width='50%' align='center' nowrap>\n";
781 echo " <select style='width:96%' onchange='codeselect(this)'>\n";
782 echo " <option value=''> " . text(substr($fs_category, 1)) . "</option>\n";
784 echo " <option value='" . attr($fs_codes) . "'>" . text(substr($fs_option, 1)) . "</option>\n";
786 endFSCategory();
788 // Create drop-lists based on categories defined within the codes.
789 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
790 "WHERE list_id = 'superbill' ORDER BY seq");
791 while ($prow = sqlFetchArray($pres)) {
792 global $code_types;
793 ++$i;
794 echo ($i <= 1) ? " <tr>\n" : "";
795 echo " <td width='50%' align='center' nowrap>\n";
796 echo " <select style='width:96%' onchange='codeselect(this)'>\n";
797 echo " <option value=''> " . text($prow['title']) . "\n";
798 $res = sqlStatement("SELECT code_type, code, code_text,modifier FROM codes " .
799 "WHERE superbill = ? AND active = 1 " .
800 "ORDER BY code_text", array($prow['option_id']) );
801 while ($row = sqlFetchArray($res)) {
802 $ctkey = alphaCodeType($row['code_type']);
803 if ($code_types[$ctkey]['nofs']) continue;
804 echo " <option value='" . attr($ctkey) . "|" .
805 attr($row['code']) . ':'. attr($row['modifier']) . "|'>" . text($row['code_text']) . "</option>\n";
807 echo " </select>\n";
808 echo " </td>\n";
809 if ($i >= $FEE_SHEET_COLUMNS) {
810 echo " </tr>\n";
811 $i = 0;
815 // Create one more drop-list, for Products.
816 if ($GLOBALS['sell_non_drug_products']) {
817 ++$i;
818 echo ($i <= 1) ? " <tr>\n" : "";
819 echo " <td width='50%' align='center' nowrap>\n";
820 echo " <select name='Products' style='width:96%' onchange='codeselect(this)'>\n";
821 echo " <option value=''> " . xlt('Products') . "\n";
822 $tres = sqlStatement("SELECT dt.drug_id, dt.selector, d.name " .
823 "FROM drug_templates AS dt, drugs AS d WHERE " .
824 "d.drug_id = dt.drug_id AND d.active = 1 " .
825 "ORDER BY d.name, dt.selector, dt.drug_id");
826 while ($trow = sqlFetchArray($tres)) {
827 echo " <option value='PROD|" . attr($trow['drug_id']) . '|' . attr($trow['selector']) . "'>" .
828 text($trow['drug_id']) . ':' . text($trow['selector']);
829 if ($trow['name'] !== $trow['selector']) echo ' ' . text($trow['name']);
830 echo "</option>\n";
832 echo " </select>\n";
833 echo " </td>\n";
834 if ($i >= $FEE_SHEET_COLUMNS) {
835 echo " </tr>\n";
836 $i = 0;
840 $search_type = $default_search_type;
841 if ($_POST['search_type']) $search_type = $_POST['search_type'];
843 $ndc_applies = true; // Assume all payers require NDC info.
845 echo $i ? " <td></td>\n </tr>\n" : "";
846 echo " <tr>\n";
847 echo " <td colspan='" . attr($FEE_SHEET_COLUMNS) . "' align='center' nowrap>\n";
849 // If Search was clicked, do it and write the list of results here.
850 // There's no limit on the number of results!
852 $numrows = 0;
853 if ($_POST['bn_search'] && $_POST['search_term']) {
854 $res = code_set_search($search_type,$_POST['search_term']);
855 if (!empty($res)) {
856 $numrows = sqlNumRows($res);
860 echo " <select name='Search Results' style='width:98%' " .
861 "onchange='codeselect(this)'";
862 if (! $numrows) echo ' disabled';
863 echo ">\n";
864 echo " <option value=''> " . xlt("Search Results") . " ($numrows " . xlt("items") . ")\n";
866 if ($numrows) {
867 while ($row = sqlFetchArray($res)) {
868 $code = $row['code'];
869 if ($row['modifier']) $code .= ":" . $row['modifier'];
870 echo " <option value='" . attr($search_type) . "|" . attr($code) . "|'>" . text($code) . " " .
871 text($row['code_text']) . "</option>\n";
875 echo " </select>\n";
876 echo " </td>\n";
877 echo " </tr>\n";
880 </table>
882 <p style='margin-top:8px;margin-bottom:8px'>
883 <table>
884 <tr>
885 <td>
886 <input type='button' value='<?php echo xla('Add Copay');?>'
887 onclick="copayselect()" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
888 </td>
889 <td>
890 <?php echo xlt('Search'); ?>&nbsp;
891 <?php
892 foreach ($code_types as $key => $value) {
893 if (!empty($value['nofs'])) continue;
894 echo " <input type='radio' name='search_type' value='" . attr($key) . "'";
895 if ($key == $default_search_type) echo " checked";
896 echo " />" . text($key) . "&nbsp;\n";
899 <?php echo xlt('for'); ?>&nbsp;
900 </td>
901 <td>
902 <input type='text' name='search_term' value=''> &nbsp;
903 </td>
904 <td>
905 <input type='submit' name='bn_search' value='<?php echo xla('Search');?>'>
906 </td>
907 </tr>
908 </table>
909 </p>
910 <p style='margin-top:16px;margin-bottom:8px'>
912 <?php } // end encounter not billed ?>
914 <table cellspacing='5'>
915 <tr>
916 <td class='billcell'><b><?php echo xlt('Type');?></b></td>
917 <td class='billcell'><b><?php echo xlt('Code');?></b></td>
918 <?php if (modifiers_are_used(true)) { ?>
919 <td class='billcell'><b><?php echo xlt('Modifiers');?></b></td>
920 <?php } ?>
921 <?php if (fees_are_used()) { ?>
922 <td class='billcell' align='right'><b><?php echo xlt('Price');?></b>&nbsp;</td>
923 <td class='billcell' align='center'><b><?php echo xlt('Units');?></b></td>
924 <td class='billcell' align='center'<?php echo $usbillstyle; ?>><b><?php echo xlt('Justify');?></b></td>
925 <?php } ?>
926 <td class='billcell' align='center'><b><?php echo xlt('Provider');?></b></td>
927 <td class='billcell' align='center'<?php echo $usbillstyle; ?>><b><?php echo xlt('Note Codes');?></b></td>
928 <td class='billcell' align='center'<?php echo $usbillstyle; ?>><b><?php echo xlt('Auth');?></b></td>
929 <td class='billcell' align='center'><b><?php echo xlt('Delete');?></b></td>
930 <td class='billcell'><b><?php echo xlt('Description');?></b></td>
931 </tr>
933 <?php
934 $justinit = "var f = document.forms[0];\n";
936 // $encounter_provid = -1;
938 $hasCharges = false;
940 // Generate lines for items already in the billing table for this encounter,
941 // and also set the rendering provider if we come across one.
943 $bill_lino = 0;
944 if ($billresult) {
945 foreach ($billresult as $iter) {
946 ++$bill_lino;
947 $bline = $_POST['bill']["$bill_lino"];
948 $del = $bline['del']; // preserve Delete if checked
950 $modifier = trim($iter["modifier"]);
951 $units = $iter["units"];
952 $fee = $iter["fee"];
953 $authorized = $iter["authorized"];
954 $ndc_info = $iter["ndc_info"];
955 $justify = trim($iter['justify']);
956 $notecodes = trim($iter['notecodes']);
957 if ($justify) $justify = substr(str_replace(':', ',', $justify), 0, strlen($justify) - 1);
958 $provider_id = $iter['provider_id'];
960 // Also preserve other items from the form, if present.
961 if ($bline['id'] && !$iter["billed"]) {
962 $modifier = trim($bline['mod']);
963 $units = max(1, intval(trim($bline['units'])));
964 $fee = sprintf('%01.2f',(0 + trim($bline['price'])) * $units);
965 $authorized = $bline['auth'];
966 $ndc_info = '';
967 if ($bline['ndcnum']) {
968 $ndc_info = 'N4' . trim($bline['ndcnum']) . ' ' . $bline['ndcuom'] .
969 trim($bline['ndcqty']);
971 $justify = $bline['justify'];
972 $notecodes = trim($bline['notecodes']);
973 $provider_id = 0 + $bline['provid'];
976 if($iter['code_type'] == 'COPAY'){//moved copay display to below
977 --$bill_lino;
978 continue;
981 // list($code, $modifier) = explode("-", $iter["code"]);
982 echoLine($bill_lino, $iter["code_type"], trim($iter["code"]),
983 $modifier, $ndc_info, $authorized,
984 $del, $units, $fee, $iter["id"], $iter["billed"],
985 $iter["code_text"], $justify, $provider_id, $notecodes);
989 $resMoneyGot = sqlStatement("SELECT pay_amount as PatientPay,session_id as id,date(post_time) as date ".
990 "FROM ar_activity where pid =? and encounter =? and payer_type=0 and account_code='PCP'",
991 array($pid,$encounter));//new fees screen copay gives account_code='PCP'
992 while($rowMoneyGot = sqlFetchArray($resMoneyGot)){
993 $PatientPay=$rowMoneyGot['PatientPay']*-1;
994 $id=$rowMoneyGot['id'];
995 echoLine(++$bill_lino,'COPAY','','',$rowMoneyGot['date'],'1','','',$PatientPay,$id);
998 // Echo new billing items from this form here, but omit any line
999 // whose Delete checkbox is checked.
1001 if ($_POST['bill']) {
1002 foreach ($_POST['bill'] as $key => $iter) {
1003 if ($iter["id"]) continue; // skip if it came from the database
1004 if ($iter["del"]) continue; // skip if Delete was checked
1005 $ndc_info = '';
1006 if ($iter['ndcnum']) {
1007 $ndc_info = 'N4' . trim($iter['ndcnum']) . ' ' . $iter['ndcuom'] .
1008 trim($iter['ndcqty']);
1010 // $fee = 0 + trim($iter['fee']);
1011 $units = max(1, intval(trim($iter['units'])));
1012 $fee = sprintf('%01.2f',(0 + trim($iter['price'])) * $units);
1013 //the date is passed as $ndc_info, since this variable is not applicable in the case of copay.
1014 $ndc_info = '';
1015 if ($iter['code_type'] == 'COPAY'){
1016 $ndc_info = date("Y-m-d");
1017 if($fee > 0)
1018 $fee = 0 - $fee;
1020 echoLine(++$bill_lino, $iter["code_type"], $iter["code"], trim($iter["mod"]),
1021 $ndc_info, $iter["auth"], $iter["del"], $units,
1022 $fee, NULL, FALSE, NULL, $iter["justify"], 0 + $iter['provid'],
1023 $iter['notecodes']);
1027 // Generate lines for items already in the drug_sales table for this encounter.
1029 $query = "SELECT * FROM drug_sales WHERE " .
1030 "pid = ? AND encounter = ? " .
1031 "ORDER BY sale_id";
1032 $sres = sqlStatement($query, array($pid,$encounter) );
1033 $prod_lino = 0;
1034 while ($srow = sqlFetchArray($sres)) {
1035 ++$prod_lino;
1036 $pline = $_POST['prod']["$prod_lino"];
1037 $del = $pline['del']; // preserve Delete if checked
1038 $sale_id = $srow['sale_id'];
1039 $drug_id = $srow['drug_id'];
1040 $units = $srow['quantity'];
1041 $fee = $srow['fee'];
1042 $billed = $srow['billed'];
1043 // Also preserve other items from the form, if present and unbilled.
1044 if ($pline['sale_id'] && !$srow['billed']) {
1045 // $units = trim($pline['units']);
1046 // $fee = trim($pline['fee']);
1047 $units = max(1, intval(trim($pline['units'])));
1048 $fee = sprintf('%01.2f',(0 + trim($pline['price'])) * $units);
1050 echoProdLine($prod_lino, $drug_id, $del, $units, $fee, $sale_id, $billed);
1053 // Echo new product items from this form here, but omit any line
1054 // whose Delete checkbox is checked.
1056 if ($_POST['prod']) {
1057 foreach ($_POST['prod'] as $key => $iter) {
1058 if ($iter["sale_id"]) continue; // skip if it came from the database
1059 if ($iter["del"]) continue; // skip if Delete was checked
1060 // $fee = 0 + trim($iter['fee']);
1061 $units = max(1, intval(trim($iter['units'])));
1062 $fee = sprintf('%01.2f',(0 + trim($iter['price'])) * $units);
1063 echoProdLine(++$prod_lino, $iter['drug_id'], FALSE, $units, $fee);
1067 // If new billing code(s) were <select>ed, add their line(s) here.
1069 if ($_POST['newcodes']) {
1070 $arrcodes = explode('~', $_POST['newcodes']);
1071 foreach ($arrcodes as $codestring) {
1072 if ($codestring === '') continue;
1073 $arrcode = explode('|', $codestring);
1074 $newtype = $arrcode[0];
1075 $newcode = $arrcode[1];
1076 $newsel = $arrcode[2];
1077 if ($newtype == 'COPAY') {
1078 $tmp = sqlQuery("SELECT copay FROM insurance_data WHERE pid = ? " .
1079 "AND type = 'primary' ORDER BY date DESC LIMIT 1", array($pid) );
1080 $code = sprintf('%01.2f', 0 + $tmp['copay']);
1081 echoLine(++$bill_lino, $newtype, $code, '', date("Y-m-d"), '1', '0', '1',
1082 sprintf('%01.2f', 0 - $code));
1084 else if ($newtype == 'PROD') {
1085 $result = sqlQuery("SELECT * FROM drug_templates WHERE " .
1086 "drug_id = ? AND selector = ?", array($newcode,$newsel) );
1087 $units = max(1, intval($result['quantity']));
1088 $prrow = sqlQuery("SELECT prices.pr_price " .
1089 "FROM patient_data, prices WHERE " .
1090 "patient_data.pid = ? AND " .
1091 "prices.pr_id = ? AND " .
1092 "prices.pr_selector = ? AND " .
1093 "prices.pr_level = patient_data.pricelevel " .
1094 "LIMIT 1", array($pid,$newcode,$newsel) );
1095 $fee = empty($prrow) ? 0 : $prrow['pr_price'];
1096 echoProdLine(++$prod_lino, $newcode, FALSE, $units, $fee);
1098 else {
1099 list($code, $modifier) = explode(":", $newcode);
1100 $ndc_info = '';
1101 // If HCPCS, find last NDC string used for this code.
1102 if ($newtype == 'HCPCS' && $ndc_applies) {
1103 $tmp = sqlQuery("SELECT ndc_info FROM billing WHERE " .
1104 "code_type = ? AND code = ? AND ndc_info LIKE 'N4%' " .
1105 "ORDER BY date DESC LIMIT 1", array($newtype,$code) );
1106 if (!empty($tmp)) $ndc_info = $tmp['ndc_info'];
1108 echoLine(++$bill_lino, $newtype, $code, trim($modifier), $ndc_info);
1113 $tmp = sqlQuery("SELECT provider_id, supervisor_id FROM form_encounter " .
1114 "WHERE pid = ? AND encounter = ? " .
1115 "ORDER BY id DESC LIMIT 1", array($pid,$encounter) );
1116 $encounter_provid = 0 + $tmp['provider_id'];
1117 $encounter_supid = 0 + $tmp['supervisor_id'];
1119 </table>
1120 </p>
1122 <br />
1123 &nbsp;
1125 <?php
1126 // Choose rendering and supervising providers.
1127 echo "<span class='billcell'><b>\n";
1128 echo xlt('Providers') . ": &nbsp;";
1130 echo "&nbsp;&nbsp;" . xlt('Rendering') . "\n";
1131 genProviderSelect('ProviderID', '-- '.xl("Please Select").' --', $encounter_provid, $isBilled);
1133 if (!$GLOBALS['ippf_specific']) {
1134 echo "&nbsp;&nbsp;" . xlt('Supervising') . "\n";
1135 genProviderSelect('SupervisorID', '-- '.xl("N/A").' --', $encounter_supid, $isBilled);
1138 echo "</b></span>\n";
1142 &nbsp;
1144 <?php
1145 // If applicable, ask for the contraceptive services start date.
1146 $trow = sqlQuery("SELECT count(*) AS count FROM layout_options WHERE " .
1147 "form_id = 'DEM' AND field_id = 'contrastart' AND uor > 0");
1148 if ($trow['count'] && $contraception && !$isBilled) {
1149 $date1 = substr($visit_row['date'], 0, 10);
1150 // If admission or surgical, then force contrastart.
1151 if ($contraception > 1 ||
1152 strpos(strtolower($visit_row['pc_catname']), 'admission') !== false)
1154 echo " <input type='hidden' name='contrastart' value='" . attr($date1) . "' />\n";
1156 else {
1157 // echo "<!-- contraception = $contraception -->\n"; // debugging
1158 $trow = sqlQuery("SELECT contrastart " .
1159 "FROM patient_data WHERE " .
1160 "pid = ? LIMIT 1", array($pid) );
1161 if (empty($trow['contrastart']) || substr($trow['contrastart'], 0, 4) == '0000') {
1162 $date0 = date('Y-m-d', strtotime($date1) - (60 * 60 * 24));
1163 echo " <select name='contrastart'>\n";
1164 echo " <option value='" . attr($date1) . "'>" . xlt('This visit begins new contraceptive use') . "</option>\n";
1165 echo " <option value='" . attr($date0) . "'>" . xlt('Contraceptive services previously started') . "</option>\n";
1166 echo " <option value=''>" . xlt('None of the above') . "</option>\n";
1167 echo " </select>\n";
1168 echo "&nbsp; &nbsp; &nbsp;\n";
1173 // If there is a choice of warehouses, allow override of user default.
1174 if ($prod_lino > 0) { // if any products are in this form
1175 $trow = sqlQuery("SELECT count(*) AS count FROM list_options WHERE list_id = 'warehouse'");
1176 if ($trow['count'] > 1) {
1177 $trow = sqlQuery("SELECT default_warehouse FROM users WHERE username = ?", array($_SESSION['authUser']) );
1178 echo " <span class='billcell'><b>" . xlt('Warehouse') . ":</b></span>\n";
1179 echo generate_select_list('default_warehouse', 'warehouse',
1180 $trow['default_warehouse'], '');
1181 echo "&nbsp; &nbsp; &nbsp;\n";
1185 // Allow the patient price level to be fixed here.
1186 $plres = sqlStatement("SELECT option_id, title FROM list_options " .
1187 "WHERE list_id = 'pricelevel' ORDER BY seq");
1188 if (true) {
1189 $trow = sqlQuery("SELECT pricelevel FROM patient_data WHERE " .
1190 "pid = ? LIMIT 1", array($pid) );
1191 $pricelevel = $trow['pricelevel'];
1192 echo " <span class='billcell'><b>" . xlt('Price Level') . ":</b></span>\n";
1193 echo " <select name='pricelevel'";
1194 if ($isBilled) echo " disabled";
1195 echo ">\n";
1196 while ($plrow = sqlFetchArray($plres)) {
1197 $key = $plrow['option_id'];
1198 $val = $plrow['title'];
1199 echo " <option value='" . attr($key) . "'";
1200 if ($key == $pricelevel) echo ' selected';
1201 echo ">" . text($val) . "</option>\n";
1203 echo " </select>\n";
1207 &nbsp; &nbsp; &nbsp;
1209 <?php if (!$isBilled) { ?>
1210 <input type='submit' name='bn_save' value='<?php echo xla('Save');?>' />
1211 &nbsp;
1212 <?php if (!$hasCharges) { ?>
1213 <input type='submit' name='bn_save_close' value='<?php echo xla('Save and Close');?>' />
1214 &nbsp;
1215 <?php } ?>
1216 <input type='submit' name='bn_refresh' value='<?php echo xla('Refresh');?>'>
1217 &nbsp;
1218 <?php } ?>
1220 <input type='button' value='<?php echo xla('Cancel');?>'
1221 onclick="top.restoreSession();location='<?php echo "$rootdir/patient_file/encounter/$returnurl" ?>'" />
1223 <?php if ($code_types['UCSMC']) { ?>
1224 <p style='font-family:sans-serif;font-size:8pt;color:#666666;'>
1225 &nbsp;<br>
1226 <?php echo xlt('UCSMC codes provided by the University of Calgary Sports Medicine Centre');?>
1227 </p>
1228 <?php } ?>
1230 </center>
1232 </form>
1234 <?php
1235 // TBD: If $alertmsg, display it with a JavaScript alert().
1238 <script language='JavaScript'>
1239 <?php echo $justinit; ?>
1240 </script>
1242 </body>
1243 </html>