2 // Copyright (C) 2005 Rod Roark <rod@sunsetsystems.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This is the second of two pages to support posting of EOBs.
10 // The first is sl_eob_search.php.
12 include_once("../globals.php");
13 include_once("../../library/patient.inc");
14 include_once("../../library/forms.inc");
15 include_once("../../library/sql-ledger.inc");
16 include_once("../../library/invoice_summary.inc.php");
17 include_once("../../custom/code_types.inc.php");
19 $debug = 0; // set to 1 for debugging mode
39 // Format money for display.
41 function bucks($amount) {
43 printf("%.2f", $amount);
46 // Insert a row into the acc_trans table.
48 function addTransaction($invid, $chartid, $amount, $date, $source, $memo, $insplan) {
49 global $sl_err, $debug;
50 $date = fixDate($date);
51 $query = "INSERT INTO acc_trans ( " .
60 "$invid, " . // trans_id
61 "$chartid, " . // chart_id
62 "$amount, " . // amount
63 "'$date', " . // transdate
64 "'$source', " . // source
65 "$insplan, " . // project_id
69 echo $query . "<br>\n";
72 if ($sl_err) die($sl_err);
76 // Insert a row into the invoice table.
78 function addLineItem($invid, $serialnumber, $amount, $adjdate, $insplan, $reason) {
79 global $sl_err, $services_id, $debug;
80 $adjdate = fixDate($adjdate);
81 $description = "Adjustment $adjdate $reason";
82 $query = "INSERT INTO invoice ( " .
95 "$invid, " . // trans_id
96 "$services_id, " . // parts_id
97 "'$description', " . // description
100 "$amount, " . // sellprice
101 "$amount, " . // fxsellprice
104 "$insplan, " . // project_id
105 "'$serialnumber'" . // serialnumber
108 echo $query . "<br>\n";
111 if ($sl_err) die($sl_err);
115 // Update totals and payment date in the invoice header. Dollar amounts are
116 // stored as double precision floats so we have to be careful about rounding.
118 function updateAR($invid, $amount, $paid = 0, $paydate = "") {
119 global $sl_err, $debug;
120 $paydate = fixDate($paydate);
121 $query = "UPDATE ar SET amount = round(CAST (amount AS numeric) + $amount, 2), " .
122 "netamount = round(CAST (netamount AS numeric) + $amount, 2)";
123 if ($paid) $query .= ", paid = round(CAST (paid AS numeric) + $paid, 2), datepaid = '$paydate'";
124 $query .= " WHERE id = $invid";
126 echo $query . "<br>\n";
129 if ($sl_err) die($sl_err);
133 // Do whatever is necessary to make this invoice re-billable.
135 function setupSecondary($invid) {
136 global $sl_err, $debug, $info_msg, $GLOBALS;
138 // Get some needed items from the SQL-Ledger invoice.
139 $arres = SLQuery("select invnumber, transdate, customer_id, employee_id " .
140 "from ar where ar.id = $invid");
141 if ($sl_err) die($sl_err);
142 $arrow = SLGetRow($arres, 0);
143 if (! $arrow) die("There is no match for invoice id = $trans_id.");
144 $customer_id = $arrow['customer_id'];
145 list($trash, $encounter) = explode(".", $arrow['invnumber']);
147 // Get the OpenEMR PID corresponding to the customer.
148 $pdrow = sqlQuery("SELECT patient_data.pid " .
149 "FROM integration_mapping, patient_data WHERE " .
150 "integration_mapping.foreign_id = $customer_id AND " .
151 "integration_mapping.foreign_table = 'customer' AND " .
152 "patient_data.id = integration_mapping.local_id");
153 $pid = $pdrow['pid'];
154 if (! $pid) die("Cannot find patient from SQL-Ledger customer id = $customer_id.");
156 // Find out if the encounter exists.
157 $ferow = sqlQuery("SELECT pid FROM form_encounter WHERE " .
158 "encounter = $encounter");
159 $encounter_pid = $ferow['pid'];
161 // If it exists, just update the billing items.
162 if ($encounter_pid) {
163 if ($encounter_pid != $pid)
164 die("Expected form_encounter.pid to be $pid, but was $encounter_pid");
165 $query = "UPDATE billing SET billed = 0, bill_process = 0, payer_id = -1, " .
166 "bill_date = NULL, process_date = NULL, process_file = NULL " .
167 "WHERE encounter = $encounter AND pid = $pid AND activity = 1";
169 echo $query . "<br>\n";
173 $info_msg = "Encounter $encounter is ready for re-billing.";
177 // It does not exist then it better be a date.
178 if (! preg_match("/^20\d\d\d\d\d\d$/", $encounter))
179 die("Internal error: encounter '$encounter' should exist but does not.");
181 $employee_id = $arrow['employee_id'];
183 // Get the OpenEMR provider info corresponding to the SQL-Ledger salesman.
184 $drrow = sqlQuery("SELECT users.id, users.username, users.facility " .
185 "FROM integration_mapping, users WHERE " .
186 "integration_mapping.foreign_id = $employee_id AND " .
187 "integration_mapping.foreign_table = 'salesman' AND " .
188 "users.id = integration_mapping.local_id");
189 $provider_id = $drrow['id'];
190 if (! $provider_id) die("Cannot find provider from SQL-Ledger employee = $employee_id.");
192 $date_of_service = $arrow['transdate'];
193 if (! $date_of_service) die("Invoice has no date!");
195 // Generate a new encounter number.
196 $conn = $GLOBALS['adodb']['db'];
197 $new_encounter = $conn->GenID("sequences");
199 // Create the "new encounter".
201 $query = "INSERT INTO form_encounter ( " .
202 "date, reason, facility, pid, encounter, onset_date " .
204 "'$date_of_service', " .
205 "'Imported from Accounting', " .
206 "'" . addslashes($drrow['facility']) . "', " .
209 "'$date_of_service' " .
212 echo $query . "<br>\n";
213 echo "Call to addForm() goes here.<br>\n";
215 $encounter_id = idSqlStatement($query);
216 if (! $encounter_id) die("Insert failed: $query");
217 addForm($new_encounter, "New Patient Encounter", $encounter_id,
218 "newpatient", $pid, 1, $date_of_service);
219 $info_msg = "Encounter $new_encounter has been created. ";
222 // For each invoice line item with a billing code we will insert
223 // a billing row with payer_id set to -1. Order the line items
224 // chronologically so that each procedure code will be followed by
225 // its associated icd9 code.
227 $inres = SLQuery("SELECT * FROM invoice WHERE trans_id = $invid " .
229 if ($sl_err) die($sl_err);
231 // When nonzero, this will be the ID of a billing row that needs to
232 // have its justify field set.
235 for ($irow = 0; $irow < SLRowCount($inres); ++
$irow) {
236 $row = SLGetRow($inres, $irow);
237 $amount = $row['sellprice'];
239 // Extract the billing code.
241 if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['serialnumber'], $matches)) {
242 $code = strtoupper($matches[1]);
244 else if (preg_match("/([A-Za-z0-9]\d\d\S*)/", $row['description'], $matches)) {
245 $code = strtoupper($matches[1]);
248 list($code, $modifier) = explode("-", $code);
250 // Set the billing code type and description.
255 if (preg_match("/CPT/", $row['serialnumber'])) {
257 $code_text = "Procedure $code";
259 else if (preg_match("/HCPCS/", $row['serialnumber'])) {
260 $code_type = "HCPCS";
261 $code_text = "Procedure $code";
263 else if (preg_match("/ICD/", $row['serialnumber'])) {
265 $code_text = "Diagnosis $code";
267 $query = "UPDATE billing SET justify = '$code' WHERE id = $proc_ins_id";
269 echo $query . "<br>\n";
278 foreach ($code_types as $key => $value) {
279 if (preg_match("/$key/", $row['serialnumber'])) {
282 $code_text = "Procedure $code";
284 $code_text = "Diagnosis $code";
286 $query = "UPDATE billing SET justify = '$code' WHERE id = $proc_ins_id";
288 echo $query . "<br>\n";
300 if (! $code_type) continue;
302 // Insert the billing item. If this for a procedure code then save
303 // the row ID so that we can update the "justify" field with the ICD9
304 // code, which should come next in the loop.
306 $query = "INSERT INTO billing ( " .
307 "date, code_type, code, pid, provider_id, user, groupname, authorized, " .
308 "encounter, code_text, activity, payer_id, billed, bill_process, " .
309 "modifier, units, fee, justify " .
316 "'" . $_SESSION['authId'] . "', " .
317 "'" . $_SESSION['authProvider'] . "', " .
331 echo $query . "<br>\n";
333 $proc_ins_id = idSqlStatement($query);
334 if ($code_type != "CPT4" && $code_type != "HCPCS")
339 // Finally, change this invoice number to contain the new encounter number.
341 $new_invnumber = "$pid.$new_encounter";
342 $query = "UPDATE ar SET invnumber = '$new_invnumber' WHERE id = $invid";
344 echo $query . "<br>\n";
347 if ($sl_err) die($sl_err);
348 $info_msg .= "This invoice number has been changed to $new_invnumber.";
354 <link rel
=stylesheet href
="<?echo $css_header;?>" type
="text/css">
355 <title
>EOB Posting
- Invoice
</title
>
356 <script language
="JavaScript">
358 // An insurance radio button is selected.
359 function setins(istr
) {
360 var f
= document
.forms
[0];
361 for (var i
= 0; i
< f
.elements
.length
; ++i
) {
362 var ename
= f
.elements
[i
].name
;
363 if (ename
.indexOf('[src]') < 0) continue;
364 var evalue
= f
.elements
[i
].value
;
365 var tmp
= evalue
.substring(0, 4).toLowerCase();
366 if (tmp
>= 'ins1' && tmp
<= 'ins3')
367 evalue
= evalue
.substring(4);
368 else if (evalue
.substring(0, 2).toLowerCase() == 'pt')
369 evalue
= evalue
.substring(2);
370 while (evalue
.substring(0, 1) == '/')
371 evalue
= evalue
.substring(1);
372 f
.elements
[i
].value
= istr +
'/' + evalue
;
377 // Compute an adjustment that writes off the balance:
378 function writeoff(code
) {
379 var f
= document
.forms
[0];
381 f
['form_line[' + code +
'][bal]'].value
-
382 f
['form_line[' + code +
'][pay]'].value
;
383 f
['form_line[' + code +
'][adj]'].value
= Number(tmp
).toFixed(2);
387 // Onsubmit handler. A good excuse to write some JavaScript.
388 function validate(f
) {
389 for (var i
= 0; i
< f
.elements
.length
; ++i
) {
390 var ename
= f
.elements
[i
].name
;
391 var pfxlen
= ename
.indexOf('[pay]');
392 if (pfxlen
< 0) continue;
393 var pfx
= ename
.substring(0, pfxlen
);
394 var code
= pfx
.substring(pfx
.indexOf('[')+
1, pfxlen
-1);
395 if (f
[pfx+
'[pay]'].value || f
[pfx+
'[adj]'].value
) {
396 var srcobj
= f
[pfx+
'[src]'];
397 while (srcobj
.value
.length
) {
398 var tmp
= srcobj
.value
.substring(srcobj
.value
.length
- 1);
399 if (tmp
> ' ' && tmp
!= '/') break;
400 srcobj
.value
= srcobj
.value
.substring(0, srcobj
.value
.length
- 1);
402 var svalue
= srcobj
.value
;
404 alert('Source is missing for code ' + code
);
407 var tmp
= svalue
.substring(0, 4).toLowerCase();
408 if (tmp
>= 'ins1' && tmp
<= 'ins3') {
409 svalue
= svalue
.substring(4);
410 } else if (svalue
.substring(0, 2).toLowerCase() == 'pt') {
411 svalue
= svalue
.substring(2);
413 alert('Invalid or missing payer in source for code ' + code
);
417 if (svalue
.substring(0, 1) != '/') {
418 alert('Missing slash after payer in source for code ' + code
);
421 tmp
= svalue
.substring(1, 3).toLowerCase();
422 if (tmp
!= 'nm' && tmp
!= 'ci' && tmp
!= 'cp' && tmp
!= 'ne' &&
423 tmp
!= 'it' && tmp
!= 'pf' && tmp
!= 'pp' && tmp
!= 'ok')
425 alert('Invalid source designation "' + tmp +
'" for code ' + code
);
430 if (! f
[pfx+
'[date]'].value
) {
431 alert('Date is missing for code ' + code
);
435 if (f
[pfx+
'[pay]'].value
&& isNaN(parseFloat(f
[pfx+
'[pay]'].value
))) {
436 alert('Payment value for code ' + code +
' is not a number');
439 if (f
[pfx+
'[adj]'].value
&& isNaN(parseFloat(f
[pfx+
'[adj]'].value
))) {
440 alert('Adjustment value for code ' + code +
' is not a number');
443 // TBD: validate the date format
450 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0'>
452 $trans_id = $_GET['id'];
453 if (! $trans_id) die("You cannot access this page directly.");
457 $chart_id_cash = SLQueryValue("select id from chart where accno = '$sl_cash_acc'");
458 if ($sl_err) die($sl_err);
459 if (! $chart_id_cash) die("There is no COA entry for cash account '$sl_cash_acc'");
461 $chart_id_ar = SLQueryValue("select id from chart where accno = '$sl_ar_acc'");
462 if ($sl_err) die($sl_err);
463 if (! $chart_id_ar) die("There is no COA entry for AR account '$sl_ar_acc'");
465 $chart_id_income = SLQueryValue("select id from chart where accno = '$sl_income_acc'");
466 if ($sl_err) die($sl_err);
467 if (! $chart_id_income) die("There is no COA entry for income account '$sl_income_acc'");
469 $services_id = SLQueryValue("select id from parts where partnumber = '$sl_services_id'");
470 if ($sl_err) die($sl_err);
471 if (! $services_id) die("There is no parts entry for services ID '$sl_services_id'");
473 if ($_POST['form_save'] ||
$_POST['form_cancel']) {
474 if ($_POST['form_save']) {
476 echo "<p><b>This module is in test mode. The database will not be changed.</b><p>\n";
479 foreach ($_POST['form_line'] as $code => $cdata) {
480 $thissrc = trim($cdata['src']);
481 $thisdate = trim($cdata['date']);
482 $thispay = trim($cdata['pay']);
483 $thisadj = trim($cdata['adj']);
484 $thisins = trim($cdata['ins']);
485 $reason = trim($cdata['reason']);
486 if (strpos(strtolower($reason), 'ins') !== false)
487 $reason .= ' ' . $_POST['form_insurance'];
488 if (! $thisins) $thisins = 0;
490 // Post a payment: add to ar, subtract from cash.
491 addTransaction($trans_id, $chart_id_ar, $thispay, $thisdate, $thissrc, $code, $thisins);
492 addTransaction($trans_id, $chart_id_cash, 0 - $thispay, $thisdate, $thissrc, $code, $thisins);
493 updateAR($trans_id, 0, $thispay, $thisdate);
494 $paytotal +
= $thispay;
497 // Post an adjustment: add negative invoice item, add to ar, subtract from income
498 addLineItem($trans_id, $code, 0 - $thisadj, $thisdate, $thisins, $reason);
499 addTransaction($trans_id, $chart_id_ar, $thisadj, $thisdate, "InvAdj $thissrc", $code, $thisins);
500 addTransaction($trans_id, $chart_id_income, 0 - $thisadj, $thisdate, "InvAdj $thissrc", $code, $thisins);
501 updateAR($trans_id, 0 - $thisadj);
504 $form_duedate = fixDate($_POST['form_duedate']);
505 $form_notes = trim($_POST['form_notes']);
507 // Maintain the list of insurances whose EOBs we have posted.
508 // We use the "Ship Via" field of the invoice to hold these.
510 $form_eobs = trim($_POST['form_eobs']);
511 $form_insurance = trim($_POST['form_insurance']);
512 if (strpos($form_eobs, $form_insurance) === false) {
513 if ($form_eobs) $form_eobs .= ","; else $form_eobs = "EOBs: ";
514 $form_eobs .= $form_insurance;
517 $query = "UPDATE ar SET duedate = '$form_duedate', notes = '$form_notes', " .
518 "shipvia = '$form_eobs' WHERE id = $trans_id";
521 echo $query . "<br>\n";
524 if ($sl_err) die($sl_err);
526 if ($_POST['form_secondary']) {
527 setupSecondary($trans_id);
529 echo "<script language='JavaScript'>\n";
530 echo " var tmp = opener.document.forms[0].form_amount.value - $paytotal;\n";
531 echo " opener.document.forms[0].form_amount.value = Number(tmp).toFixed(2);\n";
533 echo "<script language='JavaScript'>\n";
535 if ($info_msg) echo " alert('$info_msg');\n";
536 if (! $debug) echo " window.close();\n";
537 echo "</script></body></html>\n";
542 // Get invoice data into $arrow.
543 $arres = SLQuery("select ar.*, customer.name, employee.name as doctor " .
544 "from ar, customer, employee where ar.id = $trans_id and " .
545 "customer.id = ar.customer_id and employee.id = ar.employee_id");
546 if ($sl_err) die($sl_err);
547 $arrow = SLGetRow($arres, 0);
548 if (! $arrow) die("There is no match for invoice id = $trans_id.");
550 // Determine the date of service. An 8-digit encounter number is
551 // presumed to be a date of service imported during conversion.
552 // Otherwise look it up in the form_encounter table.
555 list($trash, $encounter) = explode(".", $arrow['invnumber']);
556 if (strlen($encounter) == 8) {
557 $svcdate = substr($encounter, 0, 4) . "-" . substr($encounter, 4, 2) .
558 "-" . substr($encounter, 6, 2);
560 else if ($encounter) {
561 $tmp = sqlQuery("SELECT date FROM form_encounter WHERE " .
562 "encounter = $encounter");
563 $svcdate = substr($tmp['date'], 0, 10);
566 // Get invoice charge details.
567 $codes = get_invoice_summary($trans_id, true);
571 <form method
='post' action
='sl_eob_invoice.php?id=<? echo $trans_id ?>'
572 onsubmit
='return validate(this)'>
574 <table border
='0' cellpadding
='3'>
580 <?
echo $arrow['name'] ?
>
582 <td colspan
="2" rowspan
="3">
583 <textarea name
="form_notes" cols
="50" style
="height:100%"><?
echo $arrow['notes'] ?
></textarea
>
591 <?
echo $arrow['doctor'] ?
>
599 <?
echo $arrow['invnumber'] ?
>
611 Already posted
: 
;
612 <?
echo $arrow['shipvia'] ?
>
621 <?
echo $arrow['transdate'] ?
>
624 Now posting
for: 
;
625 <input type
='radio' name
='form_insurance' value
='Ins1' onclick
='setins("Ins1")' checked
/>Ins1
 
;
626 <input type
='radio' name
='form_insurance' value
='Ins2' onclick
='setins("Ins2")' />Ins2
 
;
627 <input type
='radio' name
='form_insurance' value
='Ins3' onclick
='setins("Ins3")' />Ins3
 
;
628 <input type
='radio' name
='form_insurance' value
='Pt' onclick
='setins("Pt")' />Patient
629 <input type
='hidden' name
='form_eobs' value
='<?echo addslashes($arrow['shipvia
']) ?>' />
637 <input type
='text' name
='form_duedate' size
='10' value
='<?echo $arrow['duedate
'] ?>'
638 title
='Due date mm/dd/yyyy or yyyy-mm-dd'>
641 <input type
="checkbox" name
="form_secondary" value
="1"> Needs secondary billing
643 <input type
='submit' name
='form_save' value
='Save'>
645 <input type
='button' value
='Cancel' onclick
='window.close()'>
654 <table border
='0' cellpadding
='2' cellspacing
='0' width
='98%'>
656 <tr bgcolor
="#cccccc">
660 <td
class="dehead" align
="right">
663 <td
class="dehead" align
="right">
684 foreach ($codes as $code => $cdata) {
686 $bgcolor = "#" . (($encount & 1) ?
"ddddff" : "ffdddd");
688 // this sorts the details more or less chronologically:
689 ksort($cdata['dtl']);
690 foreach ($cdata['dtl'] as $dkey => $ddata) {
691 $ddate = substr($dkey, 0, 10);
692 if (preg_match('/^(\d\d\d\d)(\d\d)(\d\d)\s*$/', $ddate, $matches)) {
693 $ddate = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
697 if ($ddata['chg'] > 0)
698 $tmpchg = $ddata['chg'];
699 else if ($ddata['chg'] < 0)
700 $tmpadj = 0 - $ddata['chg'];
702 <tr bgcolor
='<? echo $bgcolor ?>'>
704 <?
echo $dispcode; $dispcode = "" ?
>
706 <td
class="detail" align
="right">
709 <td
class="detail" align
="right">
713 <?
echo $ddata['src'] ?
>
719 <?
bucks($ddata['pmt']) ?
>
725 <?
echo $ddata['rsn'] ?
>
729 } // end of prior detail line
731 <tr bgcolor
='<? echo $bgcolor ?>'>
733 <?
echo $dispcode; $dispcode = "" ?
>
735 <td
class="detail" align
="right">
738 <td
class="detail" align
="right">
739 <input type
="hidden" name
="form_line[<? echo $code ?>][bal]" value
="<? bucks($cdata['bal']) ?>">
740 <input type
="hidden" name
="form_line[<? echo $code ?>][ins]" value
="<? echo $cdata['ins'] ?>">
741 <?
printf("%.2f", $cdata['bal']) ?
> 
;
744 <input type
="text" name
="form_line[<? echo $code ?>][src]" size
="10"
745 style
="background-color:<? echo $bgcolor ?>"
746 title
="NM=notmet, CI=coins, CP=copay, NE=notelig, IT=insterm, PF=ptfull, PP=ptpart" />
749 <input type
="text" name
="form_line[<? echo $code ?>][date]" size
="10" style
="background-color:<? echo $bgcolor ?>" />
752 <input type
="text" name
="form_line[<? echo $code ?>][pay]" size
="10" style
="background-color:<? echo $bgcolor ?>" />
755 <input type
="text" name
="form_line[<? echo $code ?>][adj]" size
="10" style
="background-color:<? echo $bgcolor ?>" />
756  
; <a href
="" onclick
="return writeoff('<? echo $code ?>')">W
</a
>
759 <select name
="form_line[<? echo $code ?>][reason]" style
="background-color:<? echo $bgcolor ?>">
761 foreach ($reasons as $value) {
762 echo " <option value=\"$value\">$value</option>\n";
776 <script language
="JavaScript">
777 var f1
= opener
.document
.forms
[0];
778 var f2
= document
.forms
[0];
780 foreach ($codes as $code => $cdata) {
781 echo " f2['form_line[$code][src]'].value = f1.form_source.value;\n";
782 echo " f2['form_line[$code][date]'].value = f1.form_paydate.value;\n";