Added a code_type column and support for it to the ar_activity sql table, take 4.
[openemr.git] / interface / forms / fee_sheet / new.php
blob0b4e9ce098f90e65a72150f314b4877e56e597e6
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 $ct0 = '';//takes the code type of the first fee type code type entry from the fee sheet, against which the copay is posted
417 $cod0 = '';//takes the code of the first fee type code type entry from the fee sheet, against which the copay is posted
418 $mod0 = '';//takes the modifier of the first fee type code type entry from the fee sheet, against which the copay is posted
419 for ($lino = 1; $bill["$lino"]['code_type']; ++$lino) {
420 $iter = $bill["$lino"];
421 $code_type = $iter['code_type'];
422 $code = $iter['code'];
423 $del = $iter['del'];
425 // Skip disabled (billed) line items.
426 if ($iter['billed']) continue;
428 $id = $iter['id'];
429 $modifier = trim($iter['mod']);
430 if( !($cod0) && ($code_types[$code_type]['fee'] == 1) ){
431 $mod0 = $modifier;
432 $cod0 = $code;
433 $ct0 = $code_type;
435 $units = max(1, intval(trim($iter['units'])));
436 $fee = sprintf('%01.2f',(0 + trim($iter['price'])) * $units);
438 if($code_type == 'COPAY'){
439 if($id == ''){
440 //adding new copay from fee sheet into ar_session and ar_activity tables
441 if($fee < 0){
442 $fee = $fee * -1;
444 $session_id = idSqlStatement("INSERT INTO ar_session(payer_id,user_id,pay_total,payment_type,description,".
445 "patient_id,payment_method,adjustment_code,post_to_date) VALUES('0',?,?,'patient','COPAY',?,'','patient_payment',now())",
446 array($_SESSION['authId'],$fee,$pid));
447 SqlStatement("INSERT INTO ar_activity (pid,encounter,code_type,code,modifier,payer_type,post_time,post_user,session_id,".
448 "pay_amount,account_code) VALUES (?,?,?,?,?,0,now(),?,?,?,'PCP')",
449 array($pid,$encounter,$ct0,$cod0,$mod0,$_SESSION['authId'],$session_id,$fee));
450 }else{
451 //editing copay saved to ar_session and ar_activity
452 if($fee < 0){
453 $fee = $fee * -1;
455 $session_id = $id;
456 $res_amount = sqlQuery("SELECT pay_amount FROM ar_activity WHERE pid=? AND encounter=? AND session_id=?",
457 array($pid,$encounter,$session_id));
458 if($fee != $res_amount['pay_amount']){
459 sqlStatement("UPDATE ar_session SET user_id=?,pay_total=?,modified_time=now(),post_to_date=now() WHERE session_id=?",
460 array($_SESSION['authId'],$fee,$session_id));
461 sqlStatement("UPDATE ar_activity SET code_type=?, code=?, modifier=?, post_user=?, post_time=now(),".
462 "pay_amount=?, modified_time=now() WHERE pid=? AND encounter=? AND account_code='PCP' AND session_id=?",
463 array($ct0,$cod0,$mod0,$_SESSION['authId'],$fee,$pid,$encounter,$session_id));
466 if(!$cod0){
467 $copay_update = TRUE;
468 $update_session_id = $session_id;
470 continue;
472 $justify = trim($iter['justify']);
473 $notecodes = trim($iter['notecodes']);
474 if ($justify) $justify = str_replace(',', ':', $justify) . ':';
475 // $auth = $iter['auth'] ? "1" : "0";
476 $auth = "1";
477 $provid = 0 + $iter['provid'];
479 $ndc_info = '';
480 if ($iter['ndcnum']) {
481 $ndc_info = 'N4' . trim($iter['ndcnum']) . ' ' . $iter['ndcuom'] .
482 trim($iter['ndcqty']);
485 // If the item is already in the database...
486 if ($id) {
487 if ($del) {
488 deleteBilling($id);
490 else {
491 // authorizeBilling($id, $auth);
492 sqlQuery("UPDATE billing SET code = ?, " .
493 "units = ?, fee = ?, modifier = ?, " .
494 "authorized = ?, provider_id = ?, " .
495 "ndc_info = ?, justify = ?, notecodes = ? " .
496 "WHERE " .
497 "id = ? AND billed = 0 AND activity = 1", array($code,$units,$fee,$modifier,$auth,$provid,$ndc_info,$justify,$notecodes,$id) );
501 // Otherwise it's a new item...
502 else if (! $del) {
503 $code_text = lookup_code_descriptions($code_type.":".$code);
504 addBilling($encounter, $code_type, $code, $code_text, $pid, $auth,
505 $provid, $modifier, $units, $fee, $ndc_info, $justify, 0, $notecodes);
507 } // end for
509 //if modifier is not inserted during loop update the record using the first
510 //non-empty modifier and code
511 if($copay_update == TRUE && $update_session_id != '' && $mod0 != ''){
512 sqlStatement("UPDATE ar_activity SET code_type=?, code=?, modifier=?".
513 " WHERE pid=? AND encounter=? AND account_code='PCP' AND session_id=?",
514 array($ct0,$cod0,$mod0,$pid,$encounter,$update_session_id));
517 // Doing similarly to the above but for products.
518 $prod = $_POST['prod'];
519 for ($lino = 1; $prod["$lino"]['drug_id']; ++$lino) {
520 $iter = $prod["$lino"];
522 if (!empty($iter['billed'])) continue;
524 $drug_id = $iter['drug_id'];
525 $sale_id = $iter['sale_id']; // present only if already saved
526 $units = max(1, intval(trim($iter['units'])));
527 $fee = sprintf('%01.2f',(0 + trim($iter['price'])) * $units);
528 $del = $iter['del'];
530 // If the item is already in the database...
531 if ($sale_id) {
532 if ($del) {
533 // Zero out this sale and reverse its inventory update. We bring in
534 // drug_sales twice so that the original quantity can be referenced
535 // unambiguously.
536 sqlStatement("UPDATE drug_sales AS dsr, drug_sales AS ds, " .
537 "drug_inventory AS di " .
538 "SET di.on_hand = di.on_hand + dsr.quantity, " .
539 "ds.quantity = 0, ds.fee = 0 WHERE " .
540 "dsr.sale_id = ? AND ds.sale_id = dsr.sale_id AND " .
541 "di.inventory_id = ds.inventory_id", array($sale_id) );
542 // And delete the sale for good measure.
543 sqlStatement("DELETE FROM drug_sales WHERE sale_id = ?", array($sale_id) );
545 else {
546 // Modify the sale and adjust inventory accordingly.
547 $query = "UPDATE drug_sales AS dsr, drug_sales AS ds, " .
548 "drug_inventory AS di " .
549 "SET di.on_hand = di.on_hand + dsr.quantity - " . add_escape_custom($units) . ", " .
550 "ds.quantity = ?, ds.fee = ?, " .
551 "ds.sale_date = ? WHERE " .
552 "dsr.sale_id = ? AND ds.sale_id = dsr.sale_id AND " .
553 "di.inventory_id = ds.inventory_id";
554 sqlStatement($query, array($units,$fee,$visit_date,$sale_id) );
558 // Otherwise it's a new item...
559 else if (! $del) {
560 $sale_id = sellDrug($drug_id, $units, $fee, $pid, $encounter, 0,
561 $visit_date, '', $default_warehouse);
562 if (!$sale_id) die(xlt("Insufficient inventory for product ID") . " \"" . text($drug_id) . "\".");
564 } // end for
566 // Set the main/default service provider in the new-encounter form.
567 /*******************************************************************
568 sqlStatement("UPDATE forms, users SET forms.user = users.username WHERE " .
569 "forms.pid = '$pid' AND forms.encounter = '$encounter' AND " .
570 "forms.formdir = 'newpatient' AND users.id = '$provid'");
571 *******************************************************************/
572 sqlStatement("UPDATE form_encounter SET provider_id = ?, " .
573 "supervisor_id = ? WHERE " .
574 "pid = ? AND encounter = ?", array($main_provid,$main_supid,$pid,$encounter) );
576 // Save-and-Close is currently IPPF-specific but might be more generally
577 // useful. It provides the ability to mark an encounter as billed
578 // directly from the Fee Sheet, if there are no charges.
579 if ($_POST['bn_save_close']) {
580 $tmp1 = sqlQuery("SELECT SUM(ABS(fee)) AS sum FROM drug_sales WHERE " .
581 "pid = ? AND encounter = ?", array($pid,$encounter) );
582 $tmp2 = sqlQuery("SELECT SUM(ABS(fee)) AS sum FROM billing WHERE " .
583 "pid = ? AND encounter = ? AND billed = 0 AND " .
584 "activity = 1", array($pid,$encounter) );
585 if ($tmp1['sum'] + $tmp2['sum'] == 0) {
586 sqlStatement("update drug_sales SET billed = 1 WHERE " .
587 "pid = ? AND encounter = ? AND billed = 0", array($pid,$encounter));
588 sqlStatement("UPDATE billing SET billed = 1, bill_date = NOW() WHERE " .
589 "pid = ? AND encounter = ? AND billed = 0 AND " .
590 "activity = 1", array($pid,$encounter));
592 else {
593 // Would be good to display an error message here... they clicked
594 // Save and Close but the close could not be done. However the
595 // framework does not provide an easy way to do that.
599 // More IPPF stuff.
600 if (!empty($_POST['contrastart'])) {
601 $contrastart = $_POST['contrastart'];
602 sqlStatement("UPDATE patient_data SET contrastart = ?" .
603 " WHERE pid = ?", array($contrastart,$pid) );
606 // Note: Taxes are computed at checkout time (in pos_checkout.php which
607 // also posts to SL). Currently taxes with insurance claims make no sense,
608 // so for now we'll ignore tax computation in the insurance billing logic.
610 formHeader("Redirecting....");
611 formJump();
612 formFooter();
613 exit;
616 $billresult = getBillingByEncounter($pid, $encounter, "*");
618 <html>
619 <head>
620 <?php html_header_show(); ?>
621 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
622 <style>
623 .billcell { font-family: sans-serif; font-size: 10pt }
624 </style>
625 <script language="JavaScript">
627 var diags = new Array();
629 <?php
630 if ($billresult) {
631 foreach ($billresult as $iter) {
632 genDiagJS($iter["code_type"], trim($iter["code"]));
635 if ($_POST['bill']) {
636 foreach ($_POST['bill'] as $iter) {
637 if ($iter["del"]) continue; // skip if Delete was checked
638 if ($iter["id"]) continue; // skip if it came from the database
639 genDiagJS($iter["code_type"], $iter["code"]);
642 if ($_POST['newcodes']) {
643 $arrcodes = explode('~', $_POST['newcodes']);
644 foreach ($arrcodes as $codestring) {
645 if ($codestring === '') continue;
646 $arrcode = explode('|', $codestring);
647 list($code, $modifier) = explode(":", $arrcode[1]);
648 genDiagJS($arrcode[0], $code);
653 // This is invoked by <select onchange> for the various dropdowns,
654 // including search results.
655 function codeselect(selobj) {
656 var i = selobj.selectedIndex;
657 if (i > 0) {
658 top.restoreSession();
659 var f = document.forms[0];
660 f.newcodes.value = selobj.options[i].value;
661 f.submit();
665 function copayselect() {
666 top.restoreSession();
667 var f = document.forms[0];
668 f.newcodes.value = 'COPAY||';
669 f.submit();
672 function validate(f) {
673 for (var lino = 1; f['bill['+lino+'][code_type]']; ++lino) {
674 var pfx = 'bill['+lino+']';
675 if (f[pfx+'[ndcnum]'] && f[pfx+'[ndcnum]'].value) {
676 // Check NDC number format.
677 var ndcok = true;
678 var ndc = f[pfx+'[ndcnum]'].value;
679 var a = ndc.split('-');
680 if (a.length != 3) {
681 ndcok = false;
683 else if (a[0].length < 1 || a[1].length < 1 || a[2].length < 1 ||
684 a[0].length > 5 || a[1].length > 4 || a[2].length > 2) {
685 ndcok = false;
687 else {
688 for (var i = 0; i < 3; ++i) {
689 for (var j = 0; j < a[i].length; ++j) {
690 var c = a[i].charAt(j);
691 if (c < '0' || c > '9') ndcok = false;
695 if (!ndcok) {
696 alert('<?php echo addslashes(xl('Format incorrect for NDC')) ?> "' + ndc +
697 '", <?php echo addslashes(xl('should be like nnnnn-nnnn-nn')) ?>');
698 if (f[pfx+'[ndcnum]'].focus) f[pfx+'[ndcnum]'].focus();
699 return false;
701 // Check for valid quantity.
702 var qty = f[pfx+'[ndcqty]'].value - 0;
703 if (isNaN(qty) || qty <= 0) {
704 alert('<?php echo addslashes(xl('Quantity for NDC')) ?> "' + ndc +
705 '" <?php echo addslashes(xl('is not valid (decimal fractions are OK).')) ?>');
706 if (f[pfx+'[ndcqty]'].focus) f[pfx+'[ndcqty]'].focus();
707 return false;
711 top.restoreSession();
712 return true;
715 // When a justify selection is made, apply it to the current list for
716 // this procedure and then rebuild its selection list.
718 function setJustify(seljust) {
719 var theopts = seljust.options;
720 var jdisplay = theopts[0].text;
721 // Compute revised justification string. Note this does nothing if
722 // the first entry is still selected, which is handy at startup.
723 if (seljust.selectedIndex > 0) {
724 var newdiag = seljust.value;
725 if (newdiag.length == 0) {
726 jdisplay = '';
728 else {
729 if (jdisplay.length) jdisplay += ',';
730 jdisplay += newdiag;
733 // Rebuild selection list.
734 var jhaystack = ',' + jdisplay + ',';
735 var j = 0;
736 theopts.length = 0;
737 theopts[j++] = new Option(jdisplay,jdisplay,true,true);
738 for (var i = 0; i < diags.length; ++i) {
739 if (jhaystack.indexOf(',' + diags[i] + ',') < 0) {
740 theopts[j++] = new Option(diags[i],diags[i],false,false);
743 theopts[j++] = new Option('Clear','',false,false);
746 </script>
747 </head>
749 <body class="body_top">
750 <form method="post" action="<?php echo $rootdir; ?>/forms/fee_sheet/new.php"
751 onsubmit="return validate(this)">
752 <span class="title"><?php echo xlt('Fee Sheet'); ?></span><br>
753 <input type='hidden' name='newcodes' value=''>
755 <center>
757 <?php
758 $isBilled = isEncounterBilled($pid, $encounter);
759 if ($isBilled) {
760 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";
762 else { // the encounter is not yet billed
765 <table width='95%'>
766 <?php
767 $i = 0;
768 $last_category = '';
770 // Create drop-lists based on the fee_sheet_options table.
771 $res = sqlStatement("SELECT * FROM fee_sheet_options " .
772 "ORDER BY fs_category, fs_option");
773 while ($row = sqlFetchArray($res)) {
774 $fs_category = $row['fs_category'];
775 $fs_option = $row['fs_option'];
776 $fs_codes = $row['fs_codes'];
777 if($fs_category !== $last_category) {
778 endFSCategory();
779 $last_category = $fs_category;
780 ++$i;
781 echo ($i <= 1) ? " <tr>\n" : "";
782 echo " <td width='50%' align='center' nowrap>\n";
783 echo " <select style='width:96%' onchange='codeselect(this)'>\n";
784 echo " <option value=''> " . text(substr($fs_category, 1)) . "</option>\n";
786 echo " <option value='" . attr($fs_codes) . "'>" . text(substr($fs_option, 1)) . "</option>\n";
788 endFSCategory();
790 // Create drop-lists based on categories defined within the codes.
791 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
792 "WHERE list_id = 'superbill' ORDER BY seq");
793 while ($prow = sqlFetchArray($pres)) {
794 global $code_types;
795 ++$i;
796 echo ($i <= 1) ? " <tr>\n" : "";
797 echo " <td width='50%' align='center' nowrap>\n";
798 echo " <select style='width:96%' onchange='codeselect(this)'>\n";
799 echo " <option value=''> " . text($prow['title']) . "\n";
800 $res = sqlStatement("SELECT code_type, code, code_text,modifier FROM codes " .
801 "WHERE superbill = ? AND active = 1 " .
802 "ORDER BY code_text", array($prow['option_id']) );
803 while ($row = sqlFetchArray($res)) {
804 $ctkey = alphaCodeType($row['code_type']);
805 if ($code_types[$ctkey]['nofs']) continue;
806 echo " <option value='" . attr($ctkey) . "|" .
807 attr($row['code']) . ':'. attr($row['modifier']) . "|'>" . text($row['code_text']) . "</option>\n";
809 echo " </select>\n";
810 echo " </td>\n";
811 if ($i >= $FEE_SHEET_COLUMNS) {
812 echo " </tr>\n";
813 $i = 0;
817 // Create one more drop-list, for Products.
818 if ($GLOBALS['sell_non_drug_products']) {
819 ++$i;
820 echo ($i <= 1) ? " <tr>\n" : "";
821 echo " <td width='50%' align='center' nowrap>\n";
822 echo " <select name='Products' style='width:96%' onchange='codeselect(this)'>\n";
823 echo " <option value=''> " . xlt('Products') . "\n";
824 $tres = sqlStatement("SELECT dt.drug_id, dt.selector, d.name " .
825 "FROM drug_templates AS dt, drugs AS d WHERE " .
826 "d.drug_id = dt.drug_id AND d.active = 1 " .
827 "ORDER BY d.name, dt.selector, dt.drug_id");
828 while ($trow = sqlFetchArray($tres)) {
829 echo " <option value='PROD|" . attr($trow['drug_id']) . '|' . attr($trow['selector']) . "'>" .
830 text($trow['drug_id']) . ':' . text($trow['selector']);
831 if ($trow['name'] !== $trow['selector']) echo ' ' . text($trow['name']);
832 echo "</option>\n";
834 echo " </select>\n";
835 echo " </td>\n";
836 if ($i >= $FEE_SHEET_COLUMNS) {
837 echo " </tr>\n";
838 $i = 0;
842 $search_type = $default_search_type;
843 if ($_POST['search_type']) $search_type = $_POST['search_type'];
845 $ndc_applies = true; // Assume all payers require NDC info.
847 echo $i ? " <td></td>\n </tr>\n" : "";
848 echo " <tr>\n";
849 echo " <td colspan='" . attr($FEE_SHEET_COLUMNS) . "' align='center' nowrap>\n";
851 // If Search was clicked, do it and write the list of results here.
852 // There's no limit on the number of results!
854 $numrows = 0;
855 if ($_POST['bn_search'] && $_POST['search_term']) {
856 $res = code_set_search($search_type,$_POST['search_term']);
857 if (!empty($res)) {
858 $numrows = sqlNumRows($res);
862 echo " <select name='Search Results' style='width:98%' " .
863 "onchange='codeselect(this)'";
864 if (! $numrows) echo ' disabled';
865 echo ">\n";
866 echo " <option value=''> " . xlt("Search Results") . " ($numrows " . xlt("items") . ")\n";
868 if ($numrows) {
869 while ($row = sqlFetchArray($res)) {
870 $code = $row['code'];
871 if ($row['modifier']) $code .= ":" . $row['modifier'];
872 echo " <option value='" . attr($search_type) . "|" . attr($code) . "|'>" . text($code) . " " .
873 text($row['code_text']) . "</option>\n";
877 echo " </select>\n";
878 echo " </td>\n";
879 echo " </tr>\n";
882 </table>
884 <p style='margin-top:8px;margin-bottom:8px'>
885 <table>
886 <tr>
887 <td>
888 <input type='button' value='<?php echo xla('Add Copay');?>'
889 onclick="copayselect()" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
890 </td>
891 <td>
892 <?php echo xlt('Search'); ?>&nbsp;
893 <?php
894 foreach ($code_types as $key => $value) {
895 if (!empty($value['nofs'])) continue;
896 echo " <input type='radio' name='search_type' value='" . attr($key) . "'";
897 if ($key == $default_search_type) echo " checked";
898 echo " />" . text($key) . "&nbsp;\n";
901 <?php echo xlt('for'); ?>&nbsp;
902 </td>
903 <td>
904 <input type='text' name='search_term' value=''> &nbsp;
905 </td>
906 <td>
907 <input type='submit' name='bn_search' value='<?php echo xla('Search');?>'>
908 </td>
909 </tr>
910 </table>
911 </p>
912 <p style='margin-top:16px;margin-bottom:8px'>
914 <?php } // end encounter not billed ?>
916 <table cellspacing='5'>
917 <tr>
918 <td class='billcell'><b><?php echo xlt('Type');?></b></td>
919 <td class='billcell'><b><?php echo xlt('Code');?></b></td>
920 <?php if (modifiers_are_used(true)) { ?>
921 <td class='billcell'><b><?php echo xlt('Modifiers');?></b></td>
922 <?php } ?>
923 <?php if (fees_are_used()) { ?>
924 <td class='billcell' align='right'><b><?php echo xlt('Price');?></b>&nbsp;</td>
925 <td class='billcell' align='center'><b><?php echo xlt('Units');?></b></td>
926 <td class='billcell' align='center'<?php echo $usbillstyle; ?>><b><?php echo xlt('Justify');?></b></td>
927 <?php } ?>
928 <td class='billcell' align='center'><b><?php echo xlt('Provider');?></b></td>
929 <td class='billcell' align='center'<?php echo $usbillstyle; ?>><b><?php echo xlt('Note Codes');?></b></td>
930 <td class='billcell' align='center'<?php echo $usbillstyle; ?>><b><?php echo xlt('Auth');?></b></td>
931 <td class='billcell' align='center'><b><?php echo xlt('Delete');?></b></td>
932 <td class='billcell'><b><?php echo xlt('Description');?></b></td>
933 </tr>
935 <?php
936 $justinit = "var f = document.forms[0];\n";
938 // $encounter_provid = -1;
940 $hasCharges = false;
942 // Generate lines for items already in the billing table for this encounter,
943 // and also set the rendering provider if we come across one.
945 $bill_lino = 0;
946 if ($billresult) {
947 foreach ($billresult as $iter) {
948 ++$bill_lino;
949 $bline = $_POST['bill']["$bill_lino"];
950 $del = $bline['del']; // preserve Delete if checked
952 $modifier = trim($iter["modifier"]);
953 $units = $iter["units"];
954 $fee = $iter["fee"];
955 $authorized = $iter["authorized"];
956 $ndc_info = $iter["ndc_info"];
957 $justify = trim($iter['justify']);
958 $notecodes = trim($iter['notecodes']);
959 if ($justify) $justify = substr(str_replace(':', ',', $justify), 0, strlen($justify) - 1);
960 $provider_id = $iter['provider_id'];
962 // Also preserve other items from the form, if present.
963 if ($bline['id'] && !$iter["billed"]) {
964 $modifier = trim($bline['mod']);
965 $units = max(1, intval(trim($bline['units'])));
966 $fee = sprintf('%01.2f',(0 + trim($bline['price'])) * $units);
967 $authorized = $bline['auth'];
968 $ndc_info = '';
969 if ($bline['ndcnum']) {
970 $ndc_info = 'N4' . trim($bline['ndcnum']) . ' ' . $bline['ndcuom'] .
971 trim($bline['ndcqty']);
973 $justify = $bline['justify'];
974 $notecodes = trim($bline['notecodes']);
975 $provider_id = 0 + $bline['provid'];
978 if($iter['code_type'] == 'COPAY'){//moved copay display to below
979 --$bill_lino;
980 continue;
983 // list($code, $modifier) = explode("-", $iter["code"]);
984 echoLine($bill_lino, $iter["code_type"], trim($iter["code"]),
985 $modifier, $ndc_info, $authorized,
986 $del, $units, $fee, $iter["id"], $iter["billed"],
987 $iter["code_text"], $justify, $provider_id, $notecodes);
991 $resMoneyGot = sqlStatement("SELECT pay_amount as PatientPay,session_id as id,date(post_time) as date ".
992 "FROM ar_activity where pid =? and encounter =? and payer_type=0 and account_code='PCP'",
993 array($pid,$encounter));//new fees screen copay gives account_code='PCP'
994 while($rowMoneyGot = sqlFetchArray($resMoneyGot)){
995 $PatientPay=$rowMoneyGot['PatientPay']*-1;
996 $id=$rowMoneyGot['id'];
997 echoLine(++$bill_lino,'COPAY','','',$rowMoneyGot['date'],'1','','',$PatientPay,$id);
1000 // Echo new billing items from this form here, but omit any line
1001 // whose Delete checkbox is checked.
1003 if ($_POST['bill']) {
1004 foreach ($_POST['bill'] as $key => $iter) {
1005 if ($iter["id"]) continue; // skip if it came from the database
1006 if ($iter["del"]) continue; // skip if Delete was checked
1007 $ndc_info = '';
1008 if ($iter['ndcnum']) {
1009 $ndc_info = 'N4' . trim($iter['ndcnum']) . ' ' . $iter['ndcuom'] .
1010 trim($iter['ndcqty']);
1012 // $fee = 0 + trim($iter['fee']);
1013 $units = max(1, intval(trim($iter['units'])));
1014 $fee = sprintf('%01.2f',(0 + trim($iter['price'])) * $units);
1015 //the date is passed as $ndc_info, since this variable is not applicable in the case of copay.
1016 $ndc_info = '';
1017 if ($iter['code_type'] == 'COPAY'){
1018 $ndc_info = date("Y-m-d");
1019 if($fee > 0)
1020 $fee = 0 - $fee;
1022 echoLine(++$bill_lino, $iter["code_type"], $iter["code"], trim($iter["mod"]),
1023 $ndc_info, $iter["auth"], $iter["del"], $units,
1024 $fee, NULL, FALSE, NULL, $iter["justify"], 0 + $iter['provid'],
1025 $iter['notecodes']);
1029 // Generate lines for items already in the drug_sales table for this encounter.
1031 $query = "SELECT * FROM drug_sales WHERE " .
1032 "pid = ? AND encounter = ? " .
1033 "ORDER BY sale_id";
1034 $sres = sqlStatement($query, array($pid,$encounter) );
1035 $prod_lino = 0;
1036 while ($srow = sqlFetchArray($sres)) {
1037 ++$prod_lino;
1038 $pline = $_POST['prod']["$prod_lino"];
1039 $del = $pline['del']; // preserve Delete if checked
1040 $sale_id = $srow['sale_id'];
1041 $drug_id = $srow['drug_id'];
1042 $units = $srow['quantity'];
1043 $fee = $srow['fee'];
1044 $billed = $srow['billed'];
1045 // Also preserve other items from the form, if present and unbilled.
1046 if ($pline['sale_id'] && !$srow['billed']) {
1047 // $units = trim($pline['units']);
1048 // $fee = trim($pline['fee']);
1049 $units = max(1, intval(trim($pline['units'])));
1050 $fee = sprintf('%01.2f',(0 + trim($pline['price'])) * $units);
1052 echoProdLine($prod_lino, $drug_id, $del, $units, $fee, $sale_id, $billed);
1055 // Echo new product items from this form here, but omit any line
1056 // whose Delete checkbox is checked.
1058 if ($_POST['prod']) {
1059 foreach ($_POST['prod'] as $key => $iter) {
1060 if ($iter["sale_id"]) continue; // skip if it came from the database
1061 if ($iter["del"]) continue; // skip if Delete was checked
1062 // $fee = 0 + trim($iter['fee']);
1063 $units = max(1, intval(trim($iter['units'])));
1064 $fee = sprintf('%01.2f',(0 + trim($iter['price'])) * $units);
1065 echoProdLine(++$prod_lino, $iter['drug_id'], FALSE, $units, $fee);
1069 // If new billing code(s) were <select>ed, add their line(s) here.
1071 if ($_POST['newcodes']) {
1072 $arrcodes = explode('~', $_POST['newcodes']);
1073 foreach ($arrcodes as $codestring) {
1074 if ($codestring === '') continue;
1075 $arrcode = explode('|', $codestring);
1076 $newtype = $arrcode[0];
1077 $newcode = $arrcode[1];
1078 $newsel = $arrcode[2];
1079 if ($newtype == 'COPAY') {
1080 $tmp = sqlQuery("SELECT copay FROM insurance_data WHERE pid = ? " .
1081 "AND type = 'primary' ORDER BY date DESC LIMIT 1", array($pid) );
1082 $code = sprintf('%01.2f', 0 + $tmp['copay']);
1083 echoLine(++$bill_lino, $newtype, $code, '', date("Y-m-d"), '1', '0', '1',
1084 sprintf('%01.2f', 0 - $code));
1086 else if ($newtype == 'PROD') {
1087 $result = sqlQuery("SELECT * FROM drug_templates WHERE " .
1088 "drug_id = ? AND selector = ?", array($newcode,$newsel) );
1089 $units = max(1, intval($result['quantity']));
1090 $prrow = sqlQuery("SELECT prices.pr_price " .
1091 "FROM patient_data, prices WHERE " .
1092 "patient_data.pid = ? AND " .
1093 "prices.pr_id = ? AND " .
1094 "prices.pr_selector = ? AND " .
1095 "prices.pr_level = patient_data.pricelevel " .
1096 "LIMIT 1", array($pid,$newcode,$newsel) );
1097 $fee = empty($prrow) ? 0 : $prrow['pr_price'];
1098 echoProdLine(++$prod_lino, $newcode, FALSE, $units, $fee);
1100 else {
1101 list($code, $modifier) = explode(":", $newcode);
1102 $ndc_info = '';
1103 // If HCPCS, find last NDC string used for this code.
1104 if ($newtype == 'HCPCS' && $ndc_applies) {
1105 $tmp = sqlQuery("SELECT ndc_info FROM billing WHERE " .
1106 "code_type = ? AND code = ? AND ndc_info LIKE 'N4%' " .
1107 "ORDER BY date DESC LIMIT 1", array($newtype,$code) );
1108 if (!empty($tmp)) $ndc_info = $tmp['ndc_info'];
1110 echoLine(++$bill_lino, $newtype, $code, trim($modifier), $ndc_info);
1115 $tmp = sqlQuery("SELECT provider_id, supervisor_id FROM form_encounter " .
1116 "WHERE pid = ? AND encounter = ? " .
1117 "ORDER BY id DESC LIMIT 1", array($pid,$encounter) );
1118 $encounter_provid = 0 + $tmp['provider_id'];
1119 $encounter_supid = 0 + $tmp['supervisor_id'];
1121 </table>
1122 </p>
1124 <br />
1125 &nbsp;
1127 <?php
1128 // Choose rendering and supervising providers.
1129 echo "<span class='billcell'><b>\n";
1130 echo xlt('Providers') . ": &nbsp;";
1132 echo "&nbsp;&nbsp;" . xlt('Rendering') . "\n";
1133 genProviderSelect('ProviderID', '-- '.xl("Please Select").' --', $encounter_provid, $isBilled);
1135 if (!$GLOBALS['ippf_specific']) {
1136 echo "&nbsp;&nbsp;" . xlt('Supervising') . "\n";
1137 genProviderSelect('SupervisorID', '-- '.xl("N/A").' --', $encounter_supid, $isBilled);
1140 echo "</b></span>\n";
1144 &nbsp;
1146 <?php
1147 // If applicable, ask for the contraceptive services start date.
1148 $trow = sqlQuery("SELECT count(*) AS count FROM layout_options WHERE " .
1149 "form_id = 'DEM' AND field_id = 'contrastart' AND uor > 0");
1150 if ($trow['count'] && $contraception && !$isBilled) {
1151 $date1 = substr($visit_row['date'], 0, 10);
1152 // If admission or surgical, then force contrastart.
1153 if ($contraception > 1 ||
1154 strpos(strtolower($visit_row['pc_catname']), 'admission') !== false)
1156 echo " <input type='hidden' name='contrastart' value='" . attr($date1) . "' />\n";
1158 else {
1159 // echo "<!-- contraception = $contraception -->\n"; // debugging
1160 $trow = sqlQuery("SELECT contrastart " .
1161 "FROM patient_data WHERE " .
1162 "pid = ? LIMIT 1", array($pid) );
1163 if (empty($trow['contrastart']) || substr($trow['contrastart'], 0, 4) == '0000') {
1164 $date0 = date('Y-m-d', strtotime($date1) - (60 * 60 * 24));
1165 echo " <select name='contrastart'>\n";
1166 echo " <option value='" . attr($date1) . "'>" . xlt('This visit begins new contraceptive use') . "</option>\n";
1167 echo " <option value='" . attr($date0) . "'>" . xlt('Contraceptive services previously started') . "</option>\n";
1168 echo " <option value=''>" . xlt('None of the above') . "</option>\n";
1169 echo " </select>\n";
1170 echo "&nbsp; &nbsp; &nbsp;\n";
1175 // If there is a choice of warehouses, allow override of user default.
1176 if ($prod_lino > 0) { // if any products are in this form
1177 $trow = sqlQuery("SELECT count(*) AS count FROM list_options WHERE list_id = 'warehouse'");
1178 if ($trow['count'] > 1) {
1179 $trow = sqlQuery("SELECT default_warehouse FROM users WHERE username = ?", array($_SESSION['authUser']) );
1180 echo " <span class='billcell'><b>" . xlt('Warehouse') . ":</b></span>\n";
1181 echo generate_select_list('default_warehouse', 'warehouse',
1182 $trow['default_warehouse'], '');
1183 echo "&nbsp; &nbsp; &nbsp;\n";
1187 // Allow the patient price level to be fixed here.
1188 $plres = sqlStatement("SELECT option_id, title FROM list_options " .
1189 "WHERE list_id = 'pricelevel' ORDER BY seq");
1190 if (true) {
1191 $trow = sqlQuery("SELECT pricelevel FROM patient_data WHERE " .
1192 "pid = ? LIMIT 1", array($pid) );
1193 $pricelevel = $trow['pricelevel'];
1194 echo " <span class='billcell'><b>" . xlt('Price Level') . ":</b></span>\n";
1195 echo " <select name='pricelevel'";
1196 if ($isBilled) echo " disabled";
1197 echo ">\n";
1198 while ($plrow = sqlFetchArray($plres)) {
1199 $key = $plrow['option_id'];
1200 $val = $plrow['title'];
1201 echo " <option value='" . attr($key) . "'";
1202 if ($key == $pricelevel) echo ' selected';
1203 echo ">" . text($val) . "</option>\n";
1205 echo " </select>\n";
1209 &nbsp; &nbsp; &nbsp;
1211 <?php if (!$isBilled) { ?>
1212 <input type='submit' name='bn_save' value='<?php echo xla('Save');?>' />
1213 &nbsp;
1214 <?php if (!$hasCharges) { ?>
1215 <input type='submit' name='bn_save_close' value='<?php echo xla('Save and Close');?>' />
1216 &nbsp;
1217 <?php } ?>
1218 <input type='submit' name='bn_refresh' value='<?php echo xla('Refresh');?>'>
1219 &nbsp;
1220 <?php } ?>
1222 <input type='button' value='<?php echo xla('Cancel');?>'
1223 onclick="top.restoreSession();location='<?php echo "$rootdir/patient_file/encounter/$returnurl" ?>'" />
1225 <?php if ($code_types['UCSMC']) { ?>
1226 <p style='font-family:sans-serif;font-size:8pt;color:#666666;'>
1227 &nbsp;<br>
1228 <?php echo xlt('UCSMC codes provided by the University of Calgary Sports Medicine Centre');?>
1229 </p>
1230 <?php } ?>
1232 </center>
1234 </form>
1236 <?php
1237 // TBD: If $alertmsg, display it with a JavaScript alert().
1240 <script language='JavaScript'>
1241 <?php echo $justinit; ?>
1242 </script>
1244 </body>
1245 </html>