Fix for NFQ 0059 added check for procedure code in addition to standard code in lab...
[openemr.git] / library / sl_eob.inc.php
blobffcea5ebce6bb0622e560fc11cb815c561fff546
1 <?php
2 // Copyright (C) 2005-2009 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 include_once("patient.inc");
10 include_once("billing.inc");
12 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] !== 2) {
13 include_once("sql-ledger.inc");
14 include_once("invoice_summary.inc.php");
17 $chart_id_cash = 0;
18 $chart_id_ar = 0;
19 $chart_id_income = 0;
20 $services_id = 0;
22 function slInitialize() {
23 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) return;
25 global $chart_id_cash, $chart_id_ar, $chart_id_income, $services_id;
26 global $sl_cash_acc, $sl_ar_acc, $sl_income_acc, $sl_services_id;
28 SLConnect();
30 $chart_id_cash = SLQueryValue("select id from chart where accno = '$sl_cash_acc'");
31 if ($sl_err) die($sl_err);
32 if (! $chart_id_cash) die(xl("There is no COA entry for cash account ") . "'$sl_cash_acc'");
34 $chart_id_ar = SLQueryValue("select id from chart where accno = '$sl_ar_acc'");
35 if ($sl_err) die($sl_err);
36 if (! $chart_id_ar) die(xl("There is no COA entry for AR account ") . "'$sl_ar_acc'");
38 $chart_id_income = SLQueryValue("select id from chart where accno = '$sl_income_acc'");
39 if ($sl_err) die($sl_err);
40 if (! $chart_id_income) die(xl("There is no COA entry for income account ") . "'$sl_income_acc'");
42 $services_id = SLQueryValue("select id from parts where partnumber = '$sl_services_id'");
43 if ($sl_err) die($sl_err);
44 if (! $services_id) die(xl("There is no parts entry for services ID ") . "'$sl_services_id'");
47 function slTerminate() {
48 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) return;
49 SLClose();
52 // Try to figure out our invoice number (pid.encounter) from the
53 // claim ID and other stuff in the ERA. This should be straightforward
54 // except that some payers mangle the claim ID that we give them.
56 function slInvoiceNumber(&$out) {
57 $invnumber = $out['our_claim_id'];
58 $atmp = preg_split('/[ -]/', $invnumber);
59 $acount = count($atmp);
61 $pid = 0;
62 $encounter = 0;
63 if ($acount == 2) {
64 $pid = $atmp[0];
65 $encounter = $atmp[1];
67 else if ($acount == 3) {
68 $pid = $atmp[0];
69 $brow = sqlQuery("SELECT encounter FROM billing WHERE " .
70 "pid = '$pid' AND encounter = '" . $atmp[1] . "' AND activity = 1");
72 $encounter = $brow['encounter'];
74 else if ($acount == 1) {
75 $pres = sqlStatement("SELECT pid FROM patient_data WHERE " .
76 "lname LIKE '" . addslashes($out['patient_lname']) . "' AND " .
77 "fname LIKE '" . addslashes($out['patient_fname']) . "' " .
78 "ORDER BY pid DESC");
79 while ($prow = sqlFetchArray($pres)) {
80 if (strpos($invnumber, $prow['pid']) === 0) {
81 $pid = $prow['pid'];
82 $encounter = substr($invnumber, strlen($pid));
83 break;
88 if ($pid && $encounter) $invnumber = "$pid.$encounter";
89 return array($pid, $encounter, $invnumber);
92 // Insert a row into the acc_trans table.
93 // This should never be called if SQL-Ledger is not used.
95 function slAddTransaction($invid, $chartid, $amount, $date, $source, $memo, $insplan, $debug) {
96 global $sl_err;
97 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2)
98 die("Internal error calling slAddTransaction()");
99 $date = fixDate($date);
100 $query = "INSERT INTO acc_trans ( " .
101 "trans_id, " .
102 "chart_id, " .
103 "amount, " .
104 "transdate, " .
105 "source, " .
106 "project_id, " .
107 "memo " .
108 ") VALUES ( " .
109 "$invid, " . // trans_id
110 "$chartid, " . // chart_id
111 "$amount, " . // amount
112 "'$date', " . // transdate
113 "'$source', " . // source
114 "$insplan, " . // project_id
115 "'$memo' " . // memo
116 ")";
117 if ($debug) {
118 echo $query . "<br>\n";
119 } else {
120 SLQuery($query);
121 if ($sl_err) die($sl_err);
125 // Insert a row into the invoice table.
126 // This should never be called if SQL-Ledger is not used.
128 function slAddLineItem($invid, $serialnumber, $amount, $units, $insplan, $description, $debug) {
129 global $sl_err, $services_id;
130 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2)
131 die("Internal error calling slAddLineItem()");
132 $units = max(1, intval($units));
133 $price = $amount / $units;
134 $tmp = sprintf("%01.2f", $price);
135 if (abs($price - $tmp) < 0.000001) $price = $tmp;
136 $query = "INSERT INTO invoice ( " .
137 "trans_id, " .
138 "parts_id, " .
139 "description, " .
140 "qty, " .
141 "allocated, " .
142 "sellprice, " .
143 "fxsellprice, " .
144 "discount, " .
145 "unit, " .
146 "project_id, " .
147 "serialnumber " .
148 ") VALUES ( " .
149 "$invid, " . // trans_id
150 "$services_id, " . // parts_id
151 "'$description', " . // description
152 "$units, " . // qty
153 "0, " . // allocated
154 "$price, " . // sellprice
155 "$price, " . // fxsellprice
156 "0, " . // discount
157 "'', " . // unit
158 "$insplan, " . // project_id
159 "'$serialnumber'" . // serialnumber
160 ")";
161 if ($debug) {
162 echo $query . "<br>\n";
163 } else {
164 SLQuery($query);
165 if ($sl_err) die($sl_err);
169 // Update totals and payment date in the invoice header. Dollar amounts are
170 // stored as double precision floats so we have to be careful about rounding.
171 // This should never be called if SQL-Ledger is not used.
173 function slUpdateAR($invid, $amount, $paid = 0, $paydate = "", $debug) {
174 global $sl_err;
175 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2)
176 die("Internal error calling slUpdateAR()");
177 $paydate = fixDate($paydate);
178 $query = "UPDATE ar SET amount = round(CAST (amount AS numeric) + $amount, 2), " .
179 "netamount = round(CAST (netamount AS numeric) + $amount, 2)";
180 if ($paid) $query .= ", paid = round(CAST (paid AS numeric) + $paid, 2), datepaid = '$paydate'";
181 $query .= " WHERE id = $invid";
182 if ($debug) {
183 echo $query . "<br>\n";
184 } else {
185 SLQuery($query);
186 if ($sl_err) die($sl_err);
190 // This gets a posting session ID. If the payer ID is not 0 and a matching
191 // session already exists, then its ID is returned. Otherwise a new session
192 // is created.
194 function arGetSession($payer_id, $reference, $check_date, $deposit_date='', $pay_total=0) {
195 if (empty($deposit_date)) $deposit_date = $check_date;
196 if ($payer_id) {
197 $row = sqlQuery("SELECT session_id FROM ar_session WHERE " .
198 "payer_id = '$payer_id' AND reference = '$reference' AND " .
199 "check_date = '$check_date' AND deposit_date = '$deposit_date' " .
200 "ORDER BY session_id DESC LIMIT 1");
201 if (!empty($row['session_id'])) return $row['session_id'];
203 return sqlInsert("INSERT INTO ar_session ( " .
204 "payer_id, user_id, reference, check_date, deposit_date, pay_total " .
205 ") VALUES ( " .
206 "'$payer_id', " .
207 "'" . $_SESSION['authUserID'] . "', " .
208 "'$reference', " .
209 "'$check_date', " .
210 "'$deposit_date', " .
211 "'$pay_total' " .
212 ")");
215 // Post a payment, SQL-Ledger style.
217 function slPostPayment($trans_id, $thispay, $thisdate, $thissrc, $code, $thisins, $debug) {
218 global $chart_id_cash, $chart_id_ar;
219 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2)
220 die("Internal error calling slPostPayment()");
221 // Post a payment: add to ar, subtract from cash.
222 slAddTransaction($trans_id, $chart_id_ar , $thispay , $thisdate, $thissrc, $code, $thisins, $debug);
223 slAddTransaction($trans_id, $chart_id_cash, 0 - $thispay, $thisdate, $thissrc, $code, $thisins, $debug);
224 slUpdateAR($trans_id, 0, $thispay, $thisdate, $debug);
226 //writing the check details to Session Table on ERA proxcessing
227 function arPostSession($payer_id,$check_number,$check_date,$pay_total,$post_to_date,$deposit_date,$debug) {
228 $query = "INSERT INTO ar_session( " .
229 "payer_id,user_id,closed,reference,check_date,pay_total,post_to_date,deposit_date,patient_id,payment_type,adjustment_code,payment_method " .
230 ") VALUES ( " .
231 "'$payer_id'," .
232 $_SESSION['authUserID']."," .
233 "0," .
234 "'ePay - $check_number'," .
235 "'$check_date', " .
236 "$pay_total, " .
237 "'$post_to_date','$deposit_date', " .
238 "0,'insurance','insurance_payment','electronic'" .
239 ")";
240 if ($debug) {
241 echo $query . "<br>\n";
242 } else {
243 $sessionId=sqlInsert($query);
244 return $sessionId;
248 // Post a payment, new style.
250 function arPostPayment($patient_id, $encounter_id, $session_id, $amount, $code, $payer_type, $memo, $debug, $time='') {
251 $codeonly = $code;
252 $modifier = '';
253 $tmp = strpos($code, ':');
254 if ($tmp) {
255 $codeonly = substr($code, 0, $tmp);
256 $modifier = substr($code, $tmp+1);
258 if (empty($time)) $time = date('Y-m-d H:i:s');
259 $query = "INSERT INTO ar_activity ( " .
260 "pid, encounter, code, modifier, payer_type, post_time, post_user, " .
261 "session_id, memo, pay_amount " .
262 ") VALUES ( " .
263 "'$patient_id', " .
264 "'$encounter_id', " .
265 "'$codeonly', " .
266 "'$modifier', " .
267 "'$payer_type', " .
268 "'$time', " .
269 "'" . $_SESSION['authUserID'] . "', " .
270 "'$session_id', " .
271 "'$memo', " .
272 "'$amount' " .
273 ")";
274 sqlStatement($query);
275 return;
278 // Post a charge. This is called only from sl_eob_process.php where
279 // automated remittance processing can create a new service item.
280 // Here we add it as an unauthorized item to the billing table.
282 function arPostCharge($patient_id, $encounter_id, $session_id, $amount, $units, $thisdate, $code, $description, $debug) {
283 /*****************************************************************
284 // Select an existing billing item as a template.
285 $row= sqlQuery("SELECT * FROM billing WHERE " .
286 "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
287 "code_type = 'CPT4' AND activity = 1 " .
288 "ORDER BY id DESC LIMIT 1");
289 $this_authorized = 0;
290 $this_provider = 0;
291 if (!empty($row)) {
292 $this_authorized = $row['authorized'];
293 $this_provider = $row['provider_id'];
295 *****************************************************************/
297 $codeonly = $code;
298 $modifier = '';
299 $tmp = strpos($code, ':');
300 if ($tmp) {
301 $codeonly = substr($code, 0, $tmp);
302 $modifier = substr($code, $tmp+1);
305 addBilling($encounter_id,
306 'CPT4',
307 $codeonly,
308 addslashes($description),
309 $patient_id,
312 $modifier,
313 $units,
314 $amount,
316 '');
319 // See comments above.
320 // In the SQL-Ledger case this service item is added only to SL and
321 // not to the billing table.
323 function slPostCharge($trans_id, $thisamt, $thisunits, $thisdate, $code, $thisins, $description, $debug) {
324 global $chart_id_income, $chart_id_ar;
325 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2)
326 die("Internal error calling slPostCharge()");
327 // Post an adjustment: add negative invoice item, add to ar, subtract from income
328 slAddLineItem($trans_id, $code, $thisamt, $thisunits, $thisins, $description, $debug);
329 if ($thisamt) {
330 slAddTransaction($trans_id, $chart_id_ar , 0 - $thisamt, $thisdate, $description, $code, $thisins, $debug);
331 slAddTransaction($trans_id, $chart_id_income, $thisamt , $thisdate, $description, $code, $thisins, $debug);
332 slUpdateAR($trans_id, $thisamt, 0, '', $debug);
336 // Post an adjustment, SQL-Ledger style.
338 function slPostAdjustment($trans_id, $thisadj, $thisdate, $thissrc, $code, $thisins, $reason, $debug) {
339 global $chart_id_income, $chart_id_ar;
340 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2)
341 die("Internal error calling slPostAdjustment()");
342 // Post an adjustment: add negative invoice item, add to ar, subtract from income
343 $adjdate = fixDate($thisdate);
344 $description = "Adjustment $adjdate $reason";
345 slAddLineItem($trans_id, $code, 0 - $thisadj, 1, $thisins, $description, $debug);
346 if ($thisadj) {
347 slAddTransaction($trans_id, $chart_id_ar, $thisadj, $thisdate, "InvAdj $thissrc", $code, $thisins, $debug);
348 slAddTransaction($trans_id, $chart_id_income, 0 - $thisadj, $thisdate, "InvAdj $thissrc", $code, $thisins, $debug);
349 slUpdateAR($trans_id, 0 - $thisadj, 0, '', $debug);
353 // Post an adjustment, new style.
355 function arPostAdjustment($patient_id, $encounter_id, $session_id, $amount, $code, $payer_type, $reason, $debug, $time='') {
356 $codeonly = $code;
357 $modifier = '';
358 $tmp = strpos($code, ':');
359 if ($tmp) {
360 $codeonly = substr($code, 0, $tmp);
361 $modifier = substr($code, $tmp+1);
363 if (empty($time)) $time = date('Y-m-d H:i:s');
364 $query = "INSERT INTO ar_activity ( " .
365 "pid, encounter, code, modifier, payer_type, post_user, post_time, " .
366 "session_id, memo, adj_amount " .
367 ") VALUES ( " .
368 "'$patient_id', " .
369 "'$encounter_id', " .
370 "'$codeonly', " .
371 "'$modifier', " .
372 "'$payer_type', " .
373 "'" . $_SESSION['authUserID'] . "', " .
374 "'$time', " .
375 "'$session_id', " .
376 "'$reason', " .
377 "'$amount' " .
378 ")";
379 sqlStatement($query);
380 return;
383 function arGetPayerID($patient_id, $date_of_service, $payer_type) {
384 if ($payer_type < 1 || $payer_type > 3) return 0;
385 $tmp = array(1 => 'primary', 2 => 'secondary', 3 => 'tertiary');
386 $value = $tmp[$payer_type];
387 $query = "SELECT provider FROM insurance_data WHERE " .
388 "pid = ? AND type = ? AND date <= ? " .
389 "ORDER BY date DESC LIMIT 1";
390 $nprow = sqlQuery($query, array($patient_id,$value,$date_of_service) );
391 if (empty($nprow)) return 0;
392 return $nprow['provider'];
395 // Make this invoice re-billable, new style.
397 function arSetupSecondary($patient_id, $encounter_id, $debug,$crossover=0) {
398 if ($crossover==1) {
399 //if claim forwarded setting a new status
400 $status=6;
402 } else {
404 $status=1;
407 // Determine the next insurance level to be billed.
408 $ferow = sqlQuery("SELECT date, last_level_billed " .
409 "FROM form_encounter WHERE " .
410 "pid = '$patient_id' AND encounter = '$encounter_id'");
411 $date_of_service = substr($ferow['date'], 0, 10);
412 $new_payer_type = 0 + $ferow['last_level_billed'];
413 if ($new_payer_type < 3 && !empty($ferow['last_level_billed']) || $new_payer_type == 0)
414 ++$new_payer_type;
416 $new_payer_id = arGetPayerID($patient_id, $date_of_service, $new_payer_type);
418 if ($new_payer_id) {
419 // Queue up the claim.
420 if (!$debug)
421 updateClaim(true, $patient_id, $encounter_id, $new_payer_id, $new_payer_type,$status, 5, '', 'hcfa','',$crossover);
423 else {
424 // Just reopen the claim.
425 if (!$debug)
426 updateClaim(true, $patient_id, $encounter_id, -1, -1, $status, 0, '','','',$crossover);
429 return xl("Encounter ") . $encounter . xl(" is ready for re-billing.");
432 // Make this invoice re-billable, SQL-Ledger style.
434 function slSetupSecondary($invid, $debug) {
435 global $sl_err, $GLOBALS;
437 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2)
438 die("Internal error calling slSetupSecondary()");
440 $info_msg = '';
442 // Get some needed items from the SQL-Ledger invoice.
443 $arres = SLQuery("select invnumber, transdate, customer_id, employee_id, " .
444 "shipvia from ar where ar.id = $invid");
445 if ($sl_err) die($sl_err);
446 $arrow = SLGetRow($arres, 0);
447 if (! $arrow) die(xl('There is no match for invoice id') . ' = ' . "$trans_id.");
448 $customer_id = $arrow['customer_id'];
449 $date_of_service = $arrow['transdate'];
450 list($trash, $encounter) = explode(".", $arrow['invnumber']);
452 // Get the OpenEMR PID corresponding to the customer.
453 $pdrow = sqlQuery("SELECT patient_data.pid " .
454 "FROM integration_mapping, patient_data WHERE " .
455 "integration_mapping.foreign_id = $customer_id AND " .
456 "integration_mapping.foreign_table = 'customer' AND " .
457 "patient_data.id = integration_mapping.local_id");
458 $pid = $pdrow['pid'];
459 if (! $pid) die(xl("Cannot find patient from SQL-Ledger customer id") . " = $customer_id.");
461 // Determine the ID of the next insurance company (if any) to be billed.
462 $new_payer_id = -1;
463 $new_payer_type = -1;
464 $insdone = strtolower($arrow['shipvia']);
465 foreach (array('ins1' => 'primary', 'ins2' => 'secondary', 'ins3' => 'tertiary') as $key => $value) {
466 if (strpos($insdone, $key) === false) {
467 $nprow = sqlQuery("SELECT provider FROM insurance_data WHERE " .
468 "pid = '$pid' AND type = '$value' AND date <= '$date_of_service' " .
469 "ORDER BY date DESC LIMIT 1");
470 if (!empty($nprow['provider'])) {
471 $new_payer_id = $nprow['provider'];
472 $new_payer_type = substr($key, 3);
474 break;
478 // Find out if the encounter exists.
479 $ferow = sqlQuery("SELECT pid FROM form_encounter WHERE " .
480 "encounter = $encounter");
481 $encounter_pid = $ferow['pid'];
483 // If it exists, just update the billing items.
484 if ($encounter_pid) {
485 if ($encounter_pid != $pid)
486 die(xl("Expected form_encounter.pid to be ") . $pid . ', ' . xl(' but was ') . $encounter_pid);
488 // If there's a payer ID queue it up, otherwise just reopen it.
489 if ($new_payer_id > 0) {
490 // TBD: implement a default bill_process and target in config.php,
491 // it should not really be hard-coded here.
492 if (!$debug)
493 updateClaim(true, $pid, $encounter, $new_payer_id, $new_payer_type, 1, 5, '', 'hcfa');
494 } else {
495 if (!$debug)
496 updateClaim(true, $pid, $encounter, -1, -1, 1, 0, '');
499 $info_msg = xl("Encounter ") . $encounter . xl(" is ready for re-billing.");
500 return;
503 // If we get here then the encounter does not already exist. This should
504 // only happen if A/R was converted from an earlier system. In this case
505 // the encounter ID should be the date of service, and we will create the
506 // encounter.
508 // If it does not exist then it better be (or start with) a date.
509 if (! preg_match("/^20\d\d\d\d\d\d/", $encounter))
510 die(xl("Internal error: encounter '") . $encounter . xl("' should exist but does not."));
512 $employee_id = $arrow['employee_id'];
514 // Get the OpenEMR provider info corresponding to the SQL-Ledger salesman.
515 $drrow = sqlQuery("SELECT users.id, users.username, users.facility_id " .
516 "FROM integration_mapping, users WHERE " .
517 "integration_mapping.foreign_id = $employee_id AND " .
518 "integration_mapping.foreign_table = 'salesman' AND " .
519 "users.id = integration_mapping.local_id");
520 $provider_id = $drrow['id'];
521 if (! $provider_id) die(xl("Cannot find provider from SQL-Ledger employee = ") . $employee_id );
523 if (! $date_of_service) die(xl("Invoice has no date!"));
525 // Generate a new encounter number.
526 $conn = $GLOBALS['adodb']['db'];
527 $new_encounter = $conn->GenID("sequences");
529 // Create the "new encounter".
530 $encounter_id = 0;
531 $query = "INSERT INTO form_encounter ( " .
532 "date, reason, facility_id, pid, encounter, onset_date, provider_id " .
533 ") VALUES ( " .
534 "'$date_of_service', " .
535 "'" . xl('Imported from Accounting') . "', " .
536 "'" . addslashes($drrow['facility_id']) . "', " .
537 "$pid, " .
538 "$new_encounter, " .
539 "'$date_of_service', " .
540 "'$provider_id' " .
541 ")";
542 if ($debug) {
543 echo $query . "<br>\n";
544 echo xl("Call to addForm() goes here.<br>") . "\n";
545 } else {
546 $encounter_id = idSqlStatement($query);
547 if (! $encounter_id) die(xl("Insert failed: ") . $query);
548 addForm($new_encounter, xl("New Patient Encounter"), $encounter_id,
549 "newpatient", $pid, 1, $date_of_service);
550 $info_msg = xl("Encounter ") . $new_encounter . xl(" has been created. ");
553 // For each invoice line item with a billing code we will insert
554 // a billing row with payer_id set to -1. Order the line items
555 // chronologically so that each procedure code will be followed by
556 // its associated icd9 code.
558 $inres = SLQuery("SELECT * FROM invoice WHERE trans_id = $invid " .
559 "ORDER BY id");
560 if ($sl_err) die($sl_err);
562 // When nonzero, this will be the ID of a billing row that needs to
563 // have its justify field set.
564 $proc_ins_id = 0;
566 for ($irow = 0; $irow < SLRowCount($inres); ++$irow) {
567 $row = SLGetRow($inres, $irow);
568 $amount = sprintf('%01.2f', $row['sellprice'] * $row['qty']);
570 // Extract the billing code.
571 $code = xl("Unknown");
572 if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['serialnumber'], $matches)) {
573 $code = strtoupper($matches[1]);
575 else if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['description'], $matches)) {
576 $code = strtoupper($matches[1]);
579 list($code, $modifier) = explode("-", $code);
581 // Set the billing code type and description.
582 $code_type = "";
583 $code_text = "";
585 foreach ($code_types as $key => $value) {
586 if (preg_match("/$key/", $row['serialnumber'])) {
587 $code_type = $key;
588 if ($value['fee']) {
589 $code_text = xl("Procedure") . " $code";
590 } else {
591 $code_text = xl("Diagnosis") . " $code";
592 if ($proc_ins_id) {
593 $query = "UPDATE billing SET justify = '$code' WHERE id = $proc_ins_id";
594 if ($debug) {
595 echo $query . "<br>\n";
596 } else {
597 sqlQuery($query);
599 $proc_ins_id = 0;
602 break;
606 // Skip adjustments.
607 if (! $code_type) continue;
609 // Insert the billing item. If this for a procedure code then save
610 // the row ID so that we can update the "justify" field with the ICD9
611 // code, which should come next in the loop.
613 $query = "INSERT INTO billing ( " .
614 "date, code_type, code, pid, provider_id, user, groupname, authorized, " .
615 "encounter, code_text, activity, payer_id, billed, bill_process, " .
616 "bill_date, modifier, units, fee, justify, target " .
617 ") VALUES ( " .
618 "NOW(), " .
619 "'$code_type', " .
620 "'$code', " .
621 "$pid, " .
622 "0, " . // was $provider_id but that is now in form_encounter
623 "'" . $_SESSION['authId'] . "', " .
624 "'" . $_SESSION['authProvider'] . "', " .
625 "1, " .
626 "$new_encounter, " .
627 "'$code_text', " .
628 "1, " .
629 "$new_payer_id, " .
630 ($new_payer_id > 0 ? "1, " : "0, ") .
631 ($new_payer_id > 0 ? "5, " : "0, ") .
632 ($new_payer_id > 0 ? "NOW(), " : "NULL, ") .
633 "'$modifier', " .
634 "0, " .
635 "$amount, " .
636 "'', " .
637 ($new_payer_id > 0 ? "'hcfa' " : "NULL ") .
638 ")";
639 if ($debug) {
640 echo $query . "<br>\n";
641 } else {
642 $proc_ins_id = idSqlStatement($query);
643 if ($code_type != "CPT4" && $code_type != "HCPCS")
644 $proc_ins_id = 0;
648 // Finally, change this invoice number to contain the new encounter number.
650 $new_invnumber = "$pid.$new_encounter";
651 $query = "UPDATE ar SET invnumber = '$new_invnumber' WHERE id = $invid";
652 if ($debug) {
653 echo $query . "<br>\n";
654 } else {
655 SLQuery($query);
656 if ($sl_err) die($sl_err);
657 $info_msg .= xl("This invoice number has been changed to ") . $new_invnumber;
660 return $info_msg;