2 // Copyright (C) 2005-2006 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 provides for manual posting of EOBs. It is invoked from
10 // sl_eob_search.php. For automated (X12 835) remittance posting
11 // see sl_eob_process.php.
13 include_once("../globals.php");
14 include_once("../../library/patient.inc");
15 include_once("../../library/forms.inc");
16 include_once("../../library/sl_eob.inc.php");
17 include_once("../../library/invoice_summary.inc.php");
18 include_once("../../custom/code_types.inc.php");
20 $debug = 0; // set to 1 for debugging mode
22 $INTEGRATED_AR = $GLOBALS['oer_config']['ws_accounting']['enabled'] === 2;
25 "", // not choosing this allows a reason with no adjustment amount
41 xl("Untimely filing"),
48 // Format money for display.
50 function bucks($amount) {
52 printf("%.2f", $amount);
57 <?
html_header_show();?
>
58 <link rel
=stylesheet href
="<?php echo $css_header;?>" type
="text/css">
59 <title
><?php
xl('EOB Posting - Invoice','e')?
></title
>
60 <script language
="JavaScript">
62 // An insurance radio button is selected.
63 function setins(istr
) {
64 <?php
if (!$INTEGRATED_AR) { ?
>
65 var f
= document
.forms
[0];
66 for (var i
= 0; i
< f
.elements
.length
; ++i
) {
67 var ename
= f
.elements
[i
].name
;
68 if (ename
.indexOf('[src]') < 0) continue;
69 var evalue
= f
.elements
[i
].value
;
70 var tmp
= evalue
.substring(0, 4).toLowerCase();
71 if (tmp
>= 'ins1' && tmp
<= 'ins3')
72 evalue
= evalue
.substring(4);
73 else if (evalue
.substring(0, 2).toLowerCase() == 'pt')
74 evalue
= evalue
.substring(2);
75 while (evalue
.substring(0, 1) == '/')
76 evalue
= evalue
.substring(1);
77 f
.elements
[i
].value
= istr +
'/' + evalue
;
83 // Compute an adjustment that writes off the balance:
84 function writeoff(code
) {
85 var f
= document
.forms
[0];
86 var belement
= f
['form_line[' + code +
'][bal]'];
87 var pelement
= f
['form_line[' + code +
'][pay]'];
88 var aelement
= f
['form_line[' + code +
'][adj]'];
89 var relement
= f
['form_line[' + code +
'][reason]'];
90 var tmp
= belement
.value
- pelement
.value
;
91 aelement
.value
= Number(tmp
).toFixed(2);
92 if (aelement
.value
&& ! relement
.value
) relement
.selectedIndex
= 1;
96 // Onsubmit handler. A good excuse to write some JavaScript.
97 function validate(f
) {
98 for (var i
= 0; i
< f
.elements
.length
; ++i
) {
99 var ename
= f
.elements
[i
].name
;
100 var pfxlen
= ename
.indexOf('[pay]');
101 if (pfxlen
< 0) continue;
102 var pfx
= ename
.substring(0, pfxlen
);
103 var code
= pfx
.substring(pfx
.indexOf('[')+
1, pfxlen
-1);
104 if (f
[pfx+
'[pay]'].value || f
[pfx+
'[adj]'].value
) {
105 <?php
if (!$INTEGRATED_AR) { // source validation not appropriate ?>
106 var srcobj
= f
[pfx+
'[src]'];
107 while (srcobj
.value
.length
) {
108 var tmp
= srcobj
.value
.substring(srcobj
.value
.length
- 1);
109 if (tmp
> ' ' && tmp
!= '/') break;
110 srcobj
.value
= srcobj
.value
.substring(0, srcobj
.value
.length
- 1);
112 var svalue
= srcobj
.value
;
114 alert('<?php xl('Source is missing
for code
','e
') ?>' + code
);
117 var tmp
= svalue
.substring(0, 4).toLowerCase();
118 if (tmp
>= 'ins1' && tmp
<= 'ins3') {
119 svalue
= svalue
.substring(4);
120 } else if (svalue
.substring(0, 2).toLowerCase() == 'pt') {
121 svalue
= svalue
.substring(2);
123 alert('<?php xl('Invalid
or missing payer in source
for code
','e
')?>' + code
);
127 if (svalue
.substring(0, 1) != '/') {
128 alert('<?php xl('Missing slash after payer in source
for code
','e
')?>' + code
);
131 if (false) { // Please keep this, Oakland Clinic wants it. -- Rod
132 tmp
= svalue
.substring(1, 3).toLowerCase();
133 if (tmp
!= 'nm' && tmp
!= 'ci' && tmp
!= 'cp' && tmp
!= 'ne' &&
134 tmp
!= 'it' && tmp
!= 'pf' && tmp
!= 'pp' && tmp
!= 'ok')
136 alert('<?php xl('Invalid source designation
"','e') ?>' + tmp + '<?php xl('" for code
','e
') ?>' + code
);
143 if (! f
[pfx+
'[date]'].value
) {
144 alert('<?php xl('Date is missing
for code
','e
')?>' + code
);
148 if (f
[pfx+
'[pay]'].value
&& isNaN(parseFloat(f
[pfx+
'[pay]'].value
))) {
149 alert('<?php xl('Payment value
for code
','e
') ?>' + code +
'<?php xl(' is not a number
','e
') ?>');
152 if (f
[pfx+
'[adj]'].value
&& isNaN(parseFloat(f
[pfx+
'[adj]'].value
))) {
153 alert('<?php xl('Adjustment value
for code
','e
') ?>' + code +
'<?php xl(' is not a number
','e
') ?>');
156 if (f
[pfx+
'[adj]'].value
&& ! f
[pfx+
'[reason]'].value
) {
157 alert('<?php xl('Please select an adjustment reason
for code
','e
') ?>' + code
);
160 // TBD: validate the date format
167 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0'>
169 $trans_id = 0 +
$_GET['id'];
170 if (! $trans_id) die(xl("You cannot access this page directly."));
172 if ($INTEGRATED_AR) {
173 // In the Integrated A/R case, $trans_id matches form_encounter.id.
174 $ferow = sqlQuery("SELECT e.*, p.fname, p.mname, p.lname " .
175 "FROM form_encounter AS e, patient_data AS p WHERE " .
176 "e.id = '$trans_id' AND p.pid = e.pid");
177 if (empty($ferow)) die("There is no encounter with form_encounter.id = '$trans_id'.");
178 $patient_id = 0 +
$ferow['pid'];
179 $encounter_id = 0 +
$ferow['encounter'];
180 $svcdate = substr($ferow['date'], 0, 10);
181 $form_payer_id = 0 +
$_POST['form_payer_id'];
182 $form_reference = $_POST['form_reference'];
183 $form_check_date = fixDate($_POST['form_check_date'], date('Y-m-d'));
184 $form_deposit_date = fixDate($_POST['form_deposit_date'], $form_check_date);
185 $form_pay_total = 0 +
$_POST['form_pay_total'];
192 if (preg_match('/^Ins(\d)/i', $_POST['form_insurance'], $matches)) {
193 $payer_type = $matches[1];
196 if ($_POST['form_save'] ||
$_POST['form_cancel']) {
197 if ($_POST['form_save']) {
199 echo xl("This module is in test mode. The database will not be changed.",'','<p><b>',"</b><p>\n");
202 if ($INTEGRATED_AR) {
203 $session_id = arGetSession($form_payer_id, $form_reference,
204 $form_check_date, $form_deposit_date, $form_pay_total);
205 // The sl_eob_search page needs its invoice links modified to invoke
206 // javascript to load form parms for all the above and submit.
207 // At the same time that page would be modified to work off the
208 // openemr database exclusively.
209 // And back to the sl_eob_invoice page, I think we may want to move
210 // the source input fields from row level to header level.
214 foreach ($_POST['form_line'] as $code => $cdata) {
215 if (!$INTEGRATED_AR) {
216 $thissrc = trim($cdata['src']);
217 $thisdate = trim($cdata['date']);
219 $thispay = trim($cdata['pay']);
220 $thisadj = trim($cdata['adj']);
221 $thisins = trim($cdata['ins']);
222 $reason = trim($cdata['reason']);
223 if (strpos(strtolower($reason), 'ins') !== false)
224 $reason .= ' ' . $_POST['form_insurance'];
225 if (! $thisins) $thisins = 0;
228 if ($INTEGRATED_AR) {
229 arPostPayment($patient_id, $encounter_id, $session_id,
230 $thispay, $code, $payer_type, $reason, $debug);
232 slPostPayment($trans_id, $thispay, $thisdate, $thissrc, $code, $thisins, $debug);
234 $paytotal +
= $thispay;
237 // Be sure to record adjustment reasons even for zero adjustments.
238 if ($thisadj ||
$reason) {
239 // "To copay" and "To ded'ble" need to become a comment in a zero
240 // adjustment, formatted just like sl_eob_process.php.
241 if (preg_match("/To copay/", $reason)) {
242 $reason = $_POST['form_insurance'] . " coins: $thisadj";
245 else if (preg_match("/To ded'ble/", $reason)) {
246 $reason = $_POST['form_insurance'] . " dedbl: $thisadj";
249 if ($INTEGRATED_AR) {
250 arPostAdjustment($patient_id, $encounter_id, $session_id,
251 $thisadj, $code, $payer_type, $reason, $debug);
253 slPostAdjustment($trans_id, $thisadj, $thisdate, $thissrc, $code, $thisins, $reason, $debug);
258 // Maintain which insurances are marked as finished.
260 if ($INTEGRATED_AR) {
261 $form_done = 0 +
$_POST['form_done'];
262 sqlStatement("UPDATE form_encounter " .
263 "SET last_level_closed = $form_done WHERE " .
264 "pid = '$patient_id' AND encounter = '$encounter_id'");
267 $form_duedate = fixDate($_POST['form_duedate']);
268 $form_notes = trim($_POST['form_notes']);
269 // We use the "Ship Via" field of the invoice to hold these.
271 foreach (array('Ins1', 'Ins2', 'Ins3') as $value) {
272 if ($_POST["form_done_$value"]) {
273 if ($form_eobs) $form_eobs .= ","; else $form_eobs = "Done: ";
274 $form_eobs .= $value;
277 $query = "UPDATE ar SET duedate = '$form_duedate', notes = '$form_notes', " .
278 "shipvia = '$form_eobs' WHERE id = $trans_id";
280 echo $query . "<br>\n";
283 if ($sl_err) die($sl_err);
287 if ($_POST['form_secondary']) {
288 if ($INTEGRATED_AR) {
289 arSetupSecondary($patient_id, $encounter_id, $debug);
291 slSetupSecondary($trans_id, $debug);
295 echo "<script language='JavaScript'>\n";
296 echo " if (opener.document.forms[0].form_amount) {\n";
297 echo " var tmp = opener.document.forms[0].form_amount.value - $paytotal;\n";
298 echo " opener.document.forms[0].form_amount.value = Number(tmp).toFixed(2);\n";
301 echo "<script language='JavaScript'>\n";
303 if ($info_msg) echo " alert('$info_msg');\n";
304 if (! $debug) echo " window.close();\n";
305 echo "</script></body></html>\n";
306 if (!$INTEGRATED_AR) SLClose();
310 if ($INTEGRATED_AR) {
311 /*****************************************************************
312 $ferow = sqlQuery("SELECT e.*, p.fname, p.mname, p.lname " .
313 "FROM form_encounter AS e, patient_data AS p WHERE " .
314 "e.pid = '$patient_id' AND e.encounter = '$encounter_id' AND ".
316 if (empty($ferow)) die("There is no encounter $patient_id.$encounter_id.");
317 $svcdate = substr($ferow['date'], 0, 10);
318 *****************************************************************/
319 // Get invoice charge details.
320 $codes = ar_get_invoice_summary($patient_id, $encounter_id, true);
323 // Get invoice data into $arrow.
324 $arres = SLQuery("select ar.*, customer.name, employee.name as doctor " .
325 "from ar, customer, employee where ar.id = $trans_id and " .
326 "customer.id = ar.customer_id and employee.id = ar.employee_id");
327 if ($sl_err) die($sl_err);
328 $arrow = SLGetRow($arres, 0);
329 if (! $arrow) die(xl("There is no match for invoice id = ") . $trans_id);
331 // Determine the date of service. An 8-digit encounter number is
332 // presumed to be a date of service imported during conversion.
333 // Otherwise look it up in the form_encounter table.
336 list($patient_id, $encounter) = explode(".", $arrow['invnumber']);
337 if (strlen($encounter) == 8) {
338 $svcdate = substr($encounter, 0, 4) . "-" . substr($encounter, 4, 2) .
339 "-" . substr($encounter, 6, 2);
341 else if ($encounter) {
342 $tmp = sqlQuery("SELECT date FROM form_encounter WHERE " .
343 "encounter = $encounter");
344 $svcdate = substr($tmp['date'], 0, 10);
347 // Get invoice charge details.
348 $codes = get_invoice_summary($trans_id, true);
351 $pdrow = sqlQuery("select genericname2, genericval2 " .
352 "from patient_data where pid = '$patient_id' limit 1");
356 <form method
='post' action
='sl_eob_invoice.php?id=<?php echo $trans_id ?>'
357 onsubmit
='return validate(this)'>
359 <table border
='0' cellpadding
='3'>
362 <?php
xl('Patient:','e')?
>
366 if ($INTEGRATED_AR) {
367 echo $ferow['fname'] . ' ' . $ferow['mname'] . ' ' . $ferow['lname'];
374 <td colspan
="2" rowspan
="3">
376 if ($INTEGRATED_AR) {
377 for ($i = 1; $i <= 3; ++
$i) {
378 $payerid = arGetPayerID($patient_id, $svcdate, $i);
380 $tmp = sqlQuery("SELECT name FROM insurance_companies WHERE id = $payerid");
381 echo "Ins$i: " . $tmp['name'] . "<br />";
386 echo " <textarea name='form_notes' cols='50' style='height:100%'>";
387 echo $arrow['notes'];
388 echo "</textarea>\n";
395 <?php
xl('Provider:','e')?
>
399 if ($INTEGRATED_AR) {
400 $tmp = sqlQuery("SELECT u.fname, u.mname, u.lname " .
401 "FROM billing AS b, users AS u WHERE " .
402 "b.pid = '$patient_id' AND b.encounter = '$encounter_id' AND " .
403 "b.activity = 1 AND u.id = b.provider_id " .
404 "ORDER BY u.authorized DESC, b.fee DESC, b.id ASC LIMIT 1");
405 echo $tmp['fname'] . ' ' . $tmp['mname'] . ' ' . $tmp['lname'];
408 echo $arrow['doctor'];
415 <?php
xl('Invoice:','e')?
>
419 if ($INTEGRATED_AR) {
420 echo "$patient_id.$encounter_id";
423 echo $arrow['invnumber'];
431 <?php
xl('Svc Date:','e'); ?
>
439 <?php
xl('Done with:','e',''," ")?
>;
441 // Write a checkbox for each insurance. It is to be checked when
442 // we no longer expect any payments from that company for the claim.
443 if ($INTEGRATED_AR) {
444 $last_level_closed = 0 +
$ferow['last_level_closed'];
445 foreach (array(0 => 'None', 1 => 'Ins1', 2 => 'Ins2', 3 => 'Ins3') as $key => $value) {
446 if ($key && !arGetPayerID($patient_id, $svcdate, $key)) continue;
447 $checked = ($last_level_closed == $key) ?
" checked" : "";
448 echo " <input type='radio' name='form_done' value='$key'$checked />$value \n";
452 // The information is stored in the 'shipvia' field of the invoice.
453 $insgot = strtolower($arrow['notes']);
454 $insdone = strtolower($arrow['shipvia']);
455 foreach (array('Ins1', 'Ins2', 'Ins3') as $value) {
456 $lcvalue = strtolower($value);
457 $checked = (strpos($insdone, $lcvalue) === false) ?
"" : " checked";
458 if (strpos($insgot, $lcvalue) !== false) {
459 echo " <input type='checkbox' name='form_done_$value' value='1'$checked />$value \n";
466 if ($INTEGRATED_AR) {
468 echo xl('Check/EOB No.:');
471 echo "<input type='text' name='form_reference' size='10' value='' />\n";
479 <?php
xl('Bill Date:','e') ?
>
483 if ($INTEGRATED_AR) {
487 echo $arrow['transdate'];
492 <?php
xl('Now posting for:','e',''," ")?
>;
495 // TBD: check the first not-done-with insurance, not always Ins1!
497 <input type
='radio' name
='form_insurance' value
='Ins1' onclick
='setins("Ins1")' checked
/><?php
xl('Ins1','e')?
> 
;
498 <input type
='radio' name
='form_insurance' value
='Ins2' onclick
='setins("Ins2")' /><?php
xl('Ins2','e')?
> 
;
499 <input type
='radio' name
='form_insurance' value
='Ins3' onclick
='setins("Ins3")' /><?php
xl('Ins3','e')?
> 
;
500 <input type
='radio' name
='form_insurance' value
='Pt' onclick
='setins("Pt")' /><?php
xl('Patient','e')?
>
503 // TBD: I think the following is unused and can be removed.
505 <input type
='hidden' name
='form_eobs' value
='<?php echo addslashes($arrow['shipvia
']) ?>' />
509 if ($INTEGRATED_AR) {
511 echo xl('Check/EOB Date:');
514 echo "<input type='text' name='form_check_date' size='10' value='' />\n";
522 if (!$INTEGRATED_AR) xl('Due Date:','e');
527 <?php
if (!$INTEGRATED_AR) { ?
>
528 <input type
='text' name
='form_duedate' size
='10' value
='<?php echo $arrow['duedate
'] ?>'
529 title
='<?php xl('Due date mm
/dd
/yyyy
or yyyy
-mm
-dd
','e
')?>'>
533 <input type
="checkbox" name
="form_secondary" value
="1"> <?php
xl('Needs secondary billing','e')?
>
535 <input type
='submit' name
='form_save' value
='<?php xl('Save
','e
')?>'>
537 <input type
='button' value
='<?php xl('Cancel
','e
')?>' onclick
='window.close()'>
540 if ($INTEGRATED_AR) {
542 echo xl('Deposit Date:');
545 echo "<input type='text' name='form_deposit_date' size='10' value='' />\n";
546 echo "<input type='hidden' name='form_payer_id' value='' />\n";
547 echo "<input type='hidden' name='form_orig_reference' value='' />\n";
548 echo "<input type='hidden' name='form_orig_check_date' value='' />\n";
549 echo "<input type='hidden' name='form_orig_deposit_date' value='' />\n";
550 echo "<input type='hidden' name='form_pay_total' value='' />\n";
555 <?php
if ($pdrow['genericname2'] == 'Billing') { ?
>
558 <?php
xl('Billing Note:','e')?
>
560 <td colspan
='3' style
='color:red'>
561 <?php
echo $pdrow['genericval2'] ?
>
571 <table border
='0' cellpadding
='2' cellspacing
='0' width
='98%'>
573 <tr bgcolor
="#cccccc">
575 <?php
xl('Code','e')?
>
577 <td
class="dehead" align
="right">
578 <?php
xl('Charge','e')?
>
580 <td
class="dehead" align
="right">
581 <?php
xl('Balance','e')?
> 
;
584 <?php
xl('By/Source','e')?
>
587 <?php
xl('Date','e')?
>
590 <?php
xl('Pay','e')?
>
593 <?php
xl('Adjust','e')?
>
596 <?php
xl('Reason','e')?
>
601 foreach ($codes as $code => $cdata) {
603 $bgcolor = "#" . (($encount & 1) ?
"ddddff" : "ffdddd");
605 // this sorts the details more or less chronologically:
606 ksort($cdata['dtl']);
607 foreach ($cdata['dtl'] as $dkey => $ddata) {
608 $ddate = substr($dkey, 0, 10);
609 if (preg_match('/^(\d\d\d\d)(\d\d)(\d\d)\s*$/', $ddate, $matches)) {
610 $ddate = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
614 /*****************************************************************
615 if ($ddata['chg'] > 0)
616 $tmpchg = $ddata['chg'];
617 else if ($ddata['chg'] < 0)
618 $tmpadj = 0 - $ddata['chg'];
619 *****************************************************************/
620 if ($ddata['chg'] != 0) {
621 if (isset($ddata['rsn'])) $tmpadj = 0 - $ddata['chg'];
622 else $tmpchg = $ddata['chg'];
625 <tr bgcolor
='<?php echo $bgcolor ?>'>
627 <?php
echo $dispcode; $dispcode = "" ?
>
629 <td
class="detail" align
="right">
630 <?php
bucks($tmpchg) ?
>
632 <td
class="detail" align
="right">
637 if (isset($ddata['plv'])) {
638 if (!$ddata['plv']) echo 'Pt/';
639 else echo 'Ins' . $ddata['plv'] . '/';
648 <?php
bucks($ddata['pmt']) ?
>
651 <?php
bucks($tmpadj) ?
>
654 <?php
echo $ddata['rsn'] ?
>
658 } // end of prior detail line
660 <tr bgcolor
='<?php echo $bgcolor ?>'>
662 <?php
echo $dispcode; $dispcode = "" ?
>
664 <td
class="detail" align
="right">
667 <td
class="detail" align
="right">
668 <input type
="hidden" name
="form_line[<?php echo $code ?>][bal]" value
="<?php bucks($cdata['bal']) ?>">
669 <input type
="hidden" name
="form_line[<?php echo $code ?>][ins]" value
="<?php echo $cdata['ins'] ?>">
670 <?php
printf("%.2f", $cdata['bal']) ?
> 
;
674 <?php
if (!$INTEGRATED_AR) { ?
>
675 <input type
="text" name
="form_line[<?php echo $code ?>][src]" size
="10"
676 style
="background-color:<?php echo $bgcolor ?>" />
677 <!-- title
="NM=notmet, CI=coins, CP=copay, NE=notelig, IT=insterm, PF=ptfull, PP=ptpart" -->
683 <?php
if (!$INTEGRATED_AR) { ?
>
684 <input type
="text" name
="form_line[<?php echo $code ?>][date]" size
="10"
685 style
="background-color:<?php echo $bgcolor ?>" />
690 <input type
="text" name
="form_line[<?php echo $code ?>][pay]" size
="10"
691 style
="background-color:<?php echo $bgcolor ?>" />
694 <input type
="text" name
="form_line[<?php echo $code ?>][adj]" size
="10"
695 style
="background-color:<?php echo $bgcolor ?>" />
696  
; <a href
="" onclick
="return writeoff('<?php echo $code ?>')">W
</a
>
699 <select name
="form_line[<?php echo $code ?>][reason]"
700 style
="background-color:<?php echo $bgcolor ?>">
702 foreach ($reasons as $value) {
703 echo " <option value=\"$value\">$value</option>\n";
708 // TBD: Maybe a comment field would be good here, for appending
715 if (!$INTEGRATED_AR) SLClose();
721 <script language
="JavaScript">
722 var f1
= opener
.document
.forms
[0];
723 var f2
= document
.forms
[0];
724 if (f1
.form_source
) {
726 if ($INTEGRATED_AR) {
727 // These support creation and lookup of ar_session table entries:
728 echo " f2.form_reference.value = f1.form_source.value;\n";
729 echo " f2.form_check_date.value = f1.form_paydate.value;\n";
730 echo " f2.form_deposit_date.value = f1.form_deposit_date.value;\n";
731 echo " f2.form_payer_id.value = f1.form_payer_id.value;\n";
732 echo " f2.form_pay_total.value = f1.form_amount.value;\n";
733 echo " f2.form_orig_reference.value = f1.form_source.value;\n";
734 echo " f2.form_orig_check_date.value = f1.form_paydate.value;\n";
735 echo " f2.form_orig_deposit_date.value = f1.form_deposit_date.value;\n";
737 // While I'm thinking about it, some notes about eob sessions.
738 // If they do not have all of the session key fields in the search
739 // page, then show a warning at the top of the invoice page.
740 // Also when they go to save the invoice page and a session key
741 // field has changed, alert them to that and allow a cancel.
743 // Another point... when posting EOBs, the incoming payer ID might
744 // not match the payer ID for the patient's insurance. This is
745 // because the same payer might be entered more than once into the
746 // insurance_companies table. I don't think it matters much.
749 foreach ($codes as $code => $cdata) {
750 echo " f2['form_line[$code][src]'].value = f1.form_source.value;\n";
751 echo " f2['form_line[$code][date]'].value = f1.form_paydate.value;\n";