Fix for possible reporting of deductible or coinsurance when it is zero.
[openemr.git] / library / sl_eob.inc.php
blobb754433a401ebf9735d0290dc15392af0ef3fd2d
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) {
399 // Determine the next insurance level to be billed.
400 $ferow = sqlQuery("SELECT date, last_level_billed " .
401 "FROM form_encounter WHERE " .
402 "pid = '$patient_id' AND encounter = '$encounter_id'");
403 $date_of_service = substr($ferow['date'], 0, 10);
404 $new_payer_type = 0 + $ferow['last_level_billed'];
405 if ($new_payer_type < 3 && !empty($ferow['last_level_billed']) || $new_payer_type == 0)
406 ++$new_payer_type;
408 $new_payer_id = arGetPayerID($patient_id, $date_of_service, $new_payer_type);
410 if ($new_payer_id) {
411 // Queue up the claim.
412 if (!$debug)
413 updateClaim(true, $patient_id, $encounter_id, $new_payer_id, $new_payer_type, 1, 5, '', 'hcfa');
415 else {
416 // Just reopen the claim.
417 if (!$debug)
418 updateClaim(true, $patient_id, $encounter_id, -1, -1, 1, 0, '');
421 return xl("Encounter ") . $encounter . xl(" is ready for re-billing.");
424 // Make this invoice re-billable, SQL-Ledger style.
426 function slSetupSecondary($invid, $debug) {
427 global $sl_err, $GLOBALS;
429 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2)
430 die("Internal error calling slSetupSecondary()");
432 $info_msg = '';
434 // Get some needed items from the SQL-Ledger invoice.
435 $arres = SLQuery("select invnumber, transdate, customer_id, employee_id, " .
436 "shipvia from ar where ar.id = $invid");
437 if ($sl_err) die($sl_err);
438 $arrow = SLGetRow($arres, 0);
439 if (! $arrow) die(xl('There is no match for invoice id') . ' = ' . "$trans_id.");
440 $customer_id = $arrow['customer_id'];
441 $date_of_service = $arrow['transdate'];
442 list($trash, $encounter) = explode(".", $arrow['invnumber']);
444 // Get the OpenEMR PID corresponding to the customer.
445 $pdrow = sqlQuery("SELECT patient_data.pid " .
446 "FROM integration_mapping, patient_data WHERE " .
447 "integration_mapping.foreign_id = $customer_id AND " .
448 "integration_mapping.foreign_table = 'customer' AND " .
449 "patient_data.id = integration_mapping.local_id");
450 $pid = $pdrow['pid'];
451 if (! $pid) die(xl("Cannot find patient from SQL-Ledger customer id") . " = $customer_id.");
453 // Determine the ID of the next insurance company (if any) to be billed.
454 $new_payer_id = -1;
455 $new_payer_type = -1;
456 $insdone = strtolower($arrow['shipvia']);
457 foreach (array('ins1' => 'primary', 'ins2' => 'secondary', 'ins3' => 'tertiary') as $key => $value) {
458 if (strpos($insdone, $key) === false) {
459 $nprow = sqlQuery("SELECT provider FROM insurance_data WHERE " .
460 "pid = '$pid' AND type = '$value' AND date <= '$date_of_service' " .
461 "ORDER BY date DESC LIMIT 1");
462 if (!empty($nprow['provider'])) {
463 $new_payer_id = $nprow['provider'];
464 $new_payer_type = substr($key, 3);
466 break;
470 // Find out if the encounter exists.
471 $ferow = sqlQuery("SELECT pid FROM form_encounter WHERE " .
472 "encounter = $encounter");
473 $encounter_pid = $ferow['pid'];
475 // If it exists, just update the billing items.
476 if ($encounter_pid) {
477 if ($encounter_pid != $pid)
478 die(xl("Expected form_encounter.pid to be ") . $pid . ', ' . xl(' but was ') . $encounter_pid);
480 // If there's a payer ID queue it up, otherwise just reopen it.
481 if ($new_payer_id > 0) {
482 // TBD: implement a default bill_process and target in config.php,
483 // it should not really be hard-coded here.
484 if (!$debug)
485 updateClaim(true, $pid, $encounter, $new_payer_id, $new_payer_type, 1, 5, '', 'hcfa');
486 } else {
487 if (!$debug)
488 updateClaim(true, $pid, $encounter, -1, -1, 1, 0, '');
491 $info_msg = xl("Encounter ") . $encounter . xl(" is ready for re-billing.");
492 return;
495 // If we get here then the encounter does not already exist. This should
496 // only happen if A/R was converted from an earlier system. In this case
497 // the encounter ID should be the date of service, and we will create the
498 // encounter.
500 // If it does not exist then it better be (or start with) a date.
501 if (! preg_match("/^20\d\d\d\d\d\d/", $encounter))
502 die(xl("Internal error: encounter '") . $encounter . xl("' should exist but does not."));
504 $employee_id = $arrow['employee_id'];
506 // Get the OpenEMR provider info corresponding to the SQL-Ledger salesman.
507 $drrow = sqlQuery("SELECT users.id, users.username, users.facility_id " .
508 "FROM integration_mapping, users WHERE " .
509 "integration_mapping.foreign_id = $employee_id AND " .
510 "integration_mapping.foreign_table = 'salesman' AND " .
511 "users.id = integration_mapping.local_id");
512 $provider_id = $drrow['id'];
513 if (! $provider_id) die(xl("Cannot find provider from SQL-Ledger employee = ") . $employee_id );
515 if (! $date_of_service) die(xl("Invoice has no date!"));
517 // Generate a new encounter number.
518 $conn = $GLOBALS['adodb']['db'];
519 $new_encounter = $conn->GenID("sequences");
521 // Create the "new encounter".
522 $encounter_id = 0;
523 $query = "INSERT INTO form_encounter ( " .
524 "date, reason, facility_id, pid, encounter, onset_date, provider_id " .
525 ") VALUES ( " .
526 "'$date_of_service', " .
527 "'" . xl('Imported from Accounting') . "', " .
528 "'" . addslashes($drrow['facility_id']) . "', " .
529 "$pid, " .
530 "$new_encounter, " .
531 "'$date_of_service', " .
532 "'$provider_id' " .
533 ")";
534 if ($debug) {
535 echo $query . "<br>\n";
536 echo xl("Call to addForm() goes here.<br>") . "\n";
537 } else {
538 $encounter_id = idSqlStatement($query);
539 if (! $encounter_id) die(xl("Insert failed: ") . $query);
540 addForm($new_encounter, xl("New Patient Encounter"), $encounter_id,
541 "newpatient", $pid, 1, $date_of_service);
542 $info_msg = xl("Encounter ") . $new_encounter . xl(" has been created. ");
545 // For each invoice line item with a billing code we will insert
546 // a billing row with payer_id set to -1. Order the line items
547 // chronologically so that each procedure code will be followed by
548 // its associated icd9 code.
550 $inres = SLQuery("SELECT * FROM invoice WHERE trans_id = $invid " .
551 "ORDER BY id");
552 if ($sl_err) die($sl_err);
554 // When nonzero, this will be the ID of a billing row that needs to
555 // have its justify field set.
556 $proc_ins_id = 0;
558 for ($irow = 0; $irow < SLRowCount($inres); ++$irow) {
559 $row = SLGetRow($inres, $irow);
560 $amount = sprintf('%01.2f', $row['sellprice'] * $row['qty']);
562 // Extract the billing code.
563 $code = xl("Unknown");
564 if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['serialnumber'], $matches)) {
565 $code = strtoupper($matches[1]);
567 else if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['description'], $matches)) {
568 $code = strtoupper($matches[1]);
571 list($code, $modifier) = explode("-", $code);
573 // Set the billing code type and description.
574 $code_type = "";
575 $code_text = "";
577 foreach ($code_types as $key => $value) {
578 if (preg_match("/$key/", $row['serialnumber'])) {
579 $code_type = $key;
580 if ($value['fee']) {
581 $code_text = xl("Procedure") . " $code";
582 } else {
583 $code_text = xl("Diagnosis") . " $code";
584 if ($proc_ins_id) {
585 $query = "UPDATE billing SET justify = '$code' WHERE id = $proc_ins_id";
586 if ($debug) {
587 echo $query . "<br>\n";
588 } else {
589 sqlQuery($query);
591 $proc_ins_id = 0;
594 break;
598 // Skip adjustments.
599 if (! $code_type) continue;
601 // Insert the billing item. If this for a procedure code then save
602 // the row ID so that we can update the "justify" field with the ICD9
603 // code, which should come next in the loop.
605 $query = "INSERT INTO billing ( " .
606 "date, code_type, code, pid, provider_id, user, groupname, authorized, " .
607 "encounter, code_text, activity, payer_id, billed, bill_process, " .
608 "bill_date, modifier, units, fee, justify, target " .
609 ") VALUES ( " .
610 "NOW(), " .
611 "'$code_type', " .
612 "'$code', " .
613 "$pid, " .
614 "0, " . // was $provider_id but that is now in form_encounter
615 "'" . $_SESSION['authId'] . "', " .
616 "'" . $_SESSION['authProvider'] . "', " .
617 "1, " .
618 "$new_encounter, " .
619 "'$code_text', " .
620 "1, " .
621 "$new_payer_id, " .
622 ($new_payer_id > 0 ? "1, " : "0, ") .
623 ($new_payer_id > 0 ? "5, " : "0, ") .
624 ($new_payer_id > 0 ? "NOW(), " : "NULL, ") .
625 "'$modifier', " .
626 "0, " .
627 "$amount, " .
628 "'', " .
629 ($new_payer_id > 0 ? "'hcfa' " : "NULL ") .
630 ")";
631 if ($debug) {
632 echo $query . "<br>\n";
633 } else {
634 $proc_ins_id = idSqlStatement($query);
635 if ($code_type != "CPT4" && $code_type != "HCPCS")
636 $proc_ins_id = 0;
640 // Finally, change this invoice number to contain the new encounter number.
642 $new_invnumber = "$pid.$new_encounter";
643 $query = "UPDATE ar SET invnumber = '$new_invnumber' WHERE id = $invid";
644 if ($debug) {
645 echo $query . "<br>\n";
646 } else {
647 SLQuery($query);
648 if ($sl_err) die($sl_err);
649 $info_msg .= xl("This invoice number has been changed to ") . $new_invnumber;
652 return $info_msg;