various bugs (#4331)
[openemr.git] / interface / billing / sl_eob_invoice.php
blob74263a45dcc89c6a30c4c0304165d77758d16499
1 <?php
3 /**
4 * This provides for manual posting of EOBs. It is invoked from
5 * sl_eob_search.php. For automated (X12 835) remittance posting
6 * see sl_eob_process.php.
8 * @package OpenEMR
9 * @link http://www.open-emr.org
10 * @author Rod Roark <rod@sunsetsystems.com>
11 * @author Roberto Vasquez <robertogagliotta@gmail.com>
12 * @author Terry Hill <terry@lillysystems.com>
13 * @author Jerry Padgett <sjpadgett@gmail.com>
14 * @author Stephen Waite <stephen.waite@cmsvt.com>
15 * @author Brady Miller <brady.g.miller@gmail.com>
16 * @copyright Copyright (c) 2005-2020 Rod Roark <rod@sunsetsystems.com>
17 * @copyright Copyright (c) 2018-2020 Stephen Waite <stephen.waite@cmsvt.com>
18 * @copyright Copyright (c) 2019-2020 Brady Miller <brady.g.miller@gmail.com>
19 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
22 require_once("../globals.php");
23 require_once("$srcdir/patient.inc");
24 require_once("$srcdir/forms.inc");
25 require_once("../../custom/code_types.inc.php");
26 require_once "$srcdir/user.inc";
27 require_once("$srcdir/payment.inc.php");
29 use OpenEMR\Billing\InvoiceSummary;
30 use OpenEMR\Billing\SLEOB;
31 use OpenEMR\Common\Csrf\CsrfUtils;
32 use OpenEMR\Common\Logging\EventAuditLogger;
33 use OpenEMR\Core\Header;
35 $debug = 0; // set to 1 for debugging mode
36 $save_stay = (!empty($_REQUEST['form_save']) && ($_REQUEST['form_save'] == '1')) ? true : false;
37 $from_posting = (0 + ($_REQUEST['isPosting'] ?? null)) ? 1 : 0;
38 $g_posting_adj_disable = $GLOBALS['posting_adj_disable'] ? 'checked' : '';
39 if ($from_posting) {
40 $posting_adj_disable = prevSetting('sl_eob_search.', 'posting_adj_disable', 'posting_adj_disable', $g_posting_adj_disable);
41 } else {
42 $posting_adj_disable = $g_posting_adj_disable;
45 // If we permit deletion of transactions. Might change this later.
46 $ALLOW_DELETE = true;
48 $info_msg = "";
50 // Format money for display.
52 function bucks($amount)
54 if ($amount) {
55 return sprintf("%.2f", $amount);
60 <html>
61 <head>
62 <?php Header::setupHeader(['datetime-picker', 'opener', 'no_dialog']); ?>
63 <title><?php echo xlt('EOB Posting - Invoice') ?></title>
64 <script>
66 const adjDisable = <?php echo js_escape($posting_adj_disable); ?>;
67 // An insurance radio button is selected.
68 function setins(istr) {
69 return true;
72 function goEncounterSummary(pid) {
73 if(pid) {
74 if(typeof opener.toEncSummary === 'function') {
75 opener.toEncSummary(pid);
78 doClose();
81 function doClose() {
82 window.close();
85 // Compute an adjustment that writes off the balance:
86 function writeoff(code) {
87 const f = document.forms[0];
88 const belement = f['form_line[' + code + '][bal]'];
89 const pelement = f['form_line[' + code + '][pay]'];
90 const aelement = f['form_line[' + code + '][adj]'];
91 const relement = f['form_line[' + code + '][reason]'];
92 const tmp = belement.value - pelement.value;
93 aelement.value = Number(tmp).toFixed(2);
94 if (aelement.value && !relement.value) {
95 relement.selectedIndex = 1;
97 return false;
100 // Onsubmit handler. A good excuse to write some JavaScript.
101 function validate(f) {
102 let delcount = 0;
103 let allempty = true;
105 for (let i = 0; i < f.elements.length; ++i) {
106 let ename = f.elements[i].name;
107 // Count deletes.
108 if (ename.substring(0, 9) == 'form_del[') {
109 if (f.elements[i].checked) {
110 ++delcount;
112 continue;
114 let pfxlen = ename.indexOf('[pay]');
115 if (pfxlen < 0) {
116 continue
118 let pfx = ename.substring(0, pfxlen);
119 let code = pfx.substring(pfx.indexOf('[') + 1, pfxlen - 1);
120 let cPay = parseFloat(f[pfx + '[pay]'].value).toFixed(2);
121 let cAdjust = parseFloat(f[pfx + '[adj]'].value).toFixed(2);
123 if ((cPay !== 0) || cAdjust !== 0) {
124 allempty = false;
126 if(adjDisable) {
127 if ((cAdjust == 0 && ins_done.value == 'changed')) {
128 allempty = false;
131 if ((cPay !== 0) && isNaN(parseFloat(f[pfx + '[pay]'].value))) {
132 let message = <?php echo xlj('Payment value for code') ?> + " " + code + " " + <?php echo xlj('is not a number') ?>;
133 (async (message, time) => {
134 await asyncAlertMsg(message, time, 'danger', 'lg');
135 })(message, 3000)
136 .then(res => { });
137 return false;
139 if ((cAdjust !== 0) && isNaN(parseFloat(f[pfx + '[adj]'].value))) {
140 let message = <?php echo xlj('Adjustment value for code') ?> + " " + code + " " + <?php echo xlj('is not a number') ?>;
141 (async (message, time) => {
142 await asyncAlertMsg(message, time, 'danger', 'lg');
143 })(message, 3000)
144 .then(res => { });
145 return false;
147 if ((cAdjust !== 0) && !f[pfx + '[reason]'].value && !adjDisable) {
148 let message = <?php echo xlj('Please select an adjustment reason for code') ?> + " " + code;
149 (async (message, time) => {
150 await asyncAlertMsg(message, time, 'danger', 'lg');
151 })(message, 3000)
152 .then(res => { });
153 return false;
155 // TBD: validate the date format
157 // Check if save is clicked with nothing to post.
158 if (allempty && delcount === 0) {
159 let message = <?php echo xlj('Nothing to Post! Please review entries or use Cancel to exit transaction') ?>;
160 (async (message, time) => {
161 await asyncAlertMsg(message, time, 'danger', 'lg');
162 })(message, 3000)
163 .then(res => { });
164 return false;
166 // Demand confirmation if deleting anything.
167 if (delcount > 0) {
168 if (!confirm(<?php echo xlj('Really delete'); ?> + ' ' + delcount +
169 ' ' + <?php echo xlj('transactions'); ?> + '?' +
170 ' ' + <?php echo xlj('This action will be logged'); ?> + '!')
171 ) return false;
173 return true;
176 // Get current date
177 function getFormattedToday() {
178 let today = new Date();
179 let dd = today.getDate();
180 let mm = today.getMonth() + 1; //January is 0!
181 let yyyy = today.getFullYear();
182 if (dd < 10) {
183 dd = '0' + dd;
185 if (mm < 10) {
186 mm = '0' + mm;
188 return (yyyy + '-' + mm + '-' + dd);
191 // Update Payment Fields
192 function updateFields(payField, adjField, balField, coPayField, isFirstProcCode) {
193 let payAmount = 0.0;
194 let adjAmount = 0.0;
195 let balAmount = 0.0;
196 let coPayAmount = 0.0;
198 // coPayFiled will be null if there is no co-pay entry in the fee sheet
199 if (coPayField) {
200 coPayAmount = coPayField.value;
203 // if balance field is 0.00, its value comes back as null, so check for nul-ness first
204 if (balField) {
205 balAmount = (balField.value) ? balField.value : 0;
208 if (payField) {
209 payAmount = (payField.value) ? payField.value : 0;
212 // alert('balance = >' + balAmount +'< payAmount = ' + payAmount + ' copay = ' + coPayAmount + ' isFirstProcCode = ' + isFirstProcCode);
214 // subtract the co-pay only from the first procedure code
215 if (isFirstProcCode == 1) {
216 balAmount = parseFloat(balAmount) + parseFloat(coPayAmount);
219 if (adjDisable) {
220 return;
223 adjAmount = balAmount - payAmount;
224 // Assign rounded adjustment value back to TextField
225 adjField.value = adjAmount = Math.round(adjAmount * 100) / 100;
228 $(function () {
229 $('.datepicker').datetimepicker({
230 <?php $datetimepicker_timepicker = false; ?>
231 <?php $datetimepicker_showseconds = false; ?>
232 <?php $datetimepicker_formatInput = true; ?>
233 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
234 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
238 $("#ins_done").on("change", function() {
239 $("#ins_done").val('changed');
242 </script>
243 <style>
244 @media only screen and (max-width: 768px) {
245 [class*="col-"] {
246 width: 100%;
247 text-align: left !Important;
251 .table {
252 margin: auto;
253 width: 99%;
256 .table > tbody > tr > td {
257 border-top: none;
260 .last_detail {
261 border-bottom: 1px var(--black) solid;
262 margin-top: 2px;
265 @media (min-width: 992px) {
266 .modal-lg {
267 width: 1000px !Important;
270 </style>
271 </head>
272 <body>
273 <?php
274 $trans_id = 0 + $_GET['id'];
275 if (!$trans_id) {
276 die(xlt("You cannot access this page directly."));
279 // A/R case, $trans_id matches form_encounter.id.
280 $ferow = sqlQuery("SELECT e.*, p.fname, p.mname, p.lname FROM form_encounter AS e, patient_data AS p WHERE e.id = ? AND p.pid = e.pid", array($trans_id));
281 if (empty($ferow)) {
282 die("There is no encounter with form_encounter.id = '" . text($trans_id) . "'.");
284 $patient_id = 0 + $ferow['pid'];
285 $encounter_id = 0 + $ferow['encounter'];
286 $svcdate = substr($ferow['date'], 0, 10);
287 $form_payer_id = (!empty($_POST['form_payer_id'])) ? (0 + $_POST['form_payer_id']) : 0;
288 $form_reference = $_POST['form_reference'] ?? null;
289 $form_check_date = fixDate(($_POST['form_check_date'] ?? ''), date('Y-m-d'));
290 $form_deposit_date = fixDate(($_POST['form_deposit_date'] ?? ''), $form_check_date);
291 $form_pay_total = (!empty($_POST['form_pay_total'])) ? (0 + $_POST['form_pay_total']) : 0;
293 $payer_type = 0;
294 if (preg_match('/^Ins(\d)/i', ($_POST['form_insurance'] ?? ''), $matches)) {
295 $payer_type = $matches[1];
298 if (!empty($_POST['form_save']) || !empty($_POST['form_cancel']) || !empty($_POST['isLastClosed'])) {
299 if (!empty($_POST['form_save'])) {
300 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
301 CsrfUtils::csrfNotVerified();
304 if ($debug) {
305 echo "<p><b>" . xlt("This module is in test mode. The database will not be changed.") . "</b><p>\n";
308 $session_id = SLEOB::arGetSession($form_payer_id, $form_reference, $form_check_date, $form_deposit_date, $form_pay_total);
309 // The sl_eob_search page needs its invoice links modified to invoke
310 // javascript to load form parms for all the above and submit.
311 // At the same time that page would be modified to work off the
312 // openemr database exclusively.
313 // And back to the sl_eob_invoice page, I think we may want to move
314 // the source input fields from row level to header level.
316 // Handle deletes. row_delete() is borrowed from deleter.php.
317 if ($ALLOW_DELETE && !$debug) {
318 if (!empty($_POST['form_del']) && is_array($_POST['form_del'])) {
319 foreach ($_POST['form_del'] as $arseq => $dummy) {
320 row_modify(
321 "ar_activity",
322 "deleted = NOW()",
323 "pid = '" . add_escape_custom($patient_id) .
324 "' AND encounter = '" . add_escape_custom($encounter_id) .
325 "' AND sequence_no = '" . add_escape_custom($arseq) .
326 "' AND deleted IS NULL"
332 $paytotal = 0;
333 foreach ($_POST['form_line'] as $code => $cdata) {
334 $thispay = trim($cdata['pay']);
335 $thisadj = trim($cdata['adj']);
336 $thisins = trim($cdata['ins']);
337 $thiscodetype = trim($cdata['code_type']);
338 $reason = $cdata['reason'];
340 // Get the adjustment reason type. Possible values are:
341 // 1 = Charge adjustment
342 // 2 = Coinsurance
343 // 3 = Deductible
344 // 4 = Other pt resp
345 // 5 = Comment
346 $reason_type = '1';
347 if ($reason) {
348 $tmp = sqlQuery("SELECT option_value FROM list_options WHERE list_id = 'adjreason' AND activity = 1 AND option_id = ?", array($reason));
349 if (empty($tmp['option_value'])) {
350 // This should not happen but if it does, apply old logic.
351 if (preg_match("/To copay/", $reason)) {
352 $reason_type = 2;
353 } elseif (preg_match("/To ded'ble/", $reason)) {
354 $reason_type = 3;
356 $info_msg .= xl("No adjustment reason type found for") . " \"$reason\". ";
357 } else {
358 $reason_type = $tmp['option_value'];
362 if (!$thisins) {
363 $thisins = 0;
366 if (0.0 + $thispay) {
367 SLEOB::arPostPayment($patient_id, $encounter_id, $session_id, $thispay, $code, $payer_type, '', $debug, '', $thiscodetype);
368 $paytotal += $thispay;
371 // Be sure to record adjustment reasons, even for zero adjustments if
372 // they happen to be comments.
373 if (
374 (0.0 + $thisadj) ||
375 ($reason && $reason_type == 5) ||
376 ($reason && ($reason_type > 1 && $reason_type < 6))
378 // "To copay" and "To ded'ble" need to become a comment in a zero
379 // adjustment, formatted just like sl_eob_process.php.
380 if ($reason_type == '2') {
381 $reason = $_POST['form_insurance'] . " coins: $thisadj";
382 $thisadj = 0;
383 } elseif ($reason_type == '3') {
384 $reason = $_POST['form_insurance'] . " dedbl: $thisadj";
385 $thisadj = 0;
386 } elseif ($reason_type == '4') {
387 $reason = $_POST['form_insurance'] . " ptresp: $thisadj $reason";
388 $thisadj = 0;
389 } elseif ($reason_type == '5') {
390 $reason = $_POST['form_insurance'] . " note: $thisadj $reason";
391 $thisadj = 0;
392 } else {
393 // An adjustment reason including "Ins" is assumed to be assigned by
394 // insurance, and in that case we identify which one by appending
395 // Ins1, Ins2 or Ins3.
396 if (strpos(strtolower($reason), 'ins') != false) {
397 $reason .= ' ' . $_POST['form_insurance'];
400 SLEOB::arPostAdjustment($patient_id, $encounter_id, $session_id, $thisadj, $code, $payer_type, $reason, $debug, '', $thiscodetype);
404 // Maintain which insurances are marked as finished.
406 $form_done = 0 + $_POST['form_done'];
407 $form_stmt_count = 0 + $_POST['form_stmt_count'];
408 sqlStatement("UPDATE form_encounter SET last_level_closed = ?, stmt_count = ? WHERE pid = ? AND encounter = ?", array($form_done, $form_stmt_count, $patient_id, $encounter_id));
410 if (!empty($_POST['form_secondary'])) {
411 SLEOB::arSetupSecondary($patient_id, $encounter_id, $debug);
413 echo "<script>\n";
414 echo " if (opener.document.forms[0] != undefined) {\n";
415 echo " if (opener.document.forms[0].form_amount) {\n";
416 echo " var tmp = opener.document.forms[0].form_amount.value - " . attr($paytotal) . ";\n";
417 echo " opener.document.forms[0].form_amount.value = Number(tmp).toFixed(2);\n";
418 echo " }\n";
419 echo " }\n";
420 } else {
421 echo "<script>\n";
423 if ($info_msg) {
424 echo " alert(" . js_escape($info_msg) . ");\n";
426 if (!$debug && !$save_stay && !$_POST['isLastClosed']) {
427 echo "doClose();\n";
429 if (!$debug && ($save_stay || $_POST['isLastClosed'])) {
430 if ($_POST['isLastClosed']) {
431 // save last closed level
432 $form_done = 0 + $_POST['form_done'];
433 $form_stmt_count = 0 + $_POST['form_stmt_count'];
434 sqlStatement("UPDATE form_encounter SET last_level_closed = ?, stmt_count = ? WHERE pid = ? AND encounter = ?", array($form_done, $form_stmt_count, $patient_id, $encounter_id));
436 // will reload page w/o reposting
437 echo "location.replace(location)\n";
439 echo "</script>\n";
440 if (!$save_stay && !$_POST['isLastClosed']) {
441 exit();
445 // Get invoice charge details.
446 $codes = InvoiceSummary::arGetInvoiceSummary($patient_id, $encounter_id, true);
447 $pdrow = sqlQuery("select billing_note from patient_data where pid = ? limit 1", array($patient_id));
450 <div class="container-fluid">
451 <div class="row">
452 <h2><?php echo xlt('EOB Invoice'); ?></h2>
453 </div>
454 <div class="container-fluid">
455 <form class="form" action='sl_eob_invoice.php?id=<?php echo attr_url($trans_id); ?>' method='post' onsubmit='return validate(this)'>
456 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>"/>
457 <input type="hidden" name="isPosting" value="<?php echo attr($from_posting); ?>"/>
458 <input type="hidden" name="isLastClosed" value="" />
459 <fieldset>
460 <legend><?php echo xlt('Invoice Actions'); ?></legend>
461 <div class="form-row">
462 <div class="form-group col-lg">
463 <label class="col-form-label" for="form_name"><?php echo xlt('Patient'); ?>:</label>
464 <input type="text" class="form-control" id='form_name'
465 name='form_name'
466 value="<?php echo attr($ferow['fname']) . ' ' . attr($ferow['mname']) . ' ' . attr($ferow['lname']); ?>"
467 disabled />
468 </div>
469 <div class="form-group col-lg">
470 <label class="col-form-label" for="form_provider"><?php echo xlt('Provider'); ?>:</label>
471 <?php
472 $tmp = sqlQuery("SELECT fname, mname, lname " .
473 "FROM users WHERE id = ?", array($ferow['provider_id']));
474 $provider = text($tmp['fname']) . ' ' . text($tmp['mname']) . ' ' . text($tmp['lname']);
475 $tmp = sqlQuery("SELECT bill_date FROM billing WHERE " .
476 "pid = ? AND encounter = ? AND " .
477 "activity = 1 ORDER BY fee DESC, id ASC LIMIT 1", array($patient_id, $encounter_id));
478 $billdate = substr(($tmp['bill_date'] . "Not Billed"), 0, 10);
480 <input type="text" class="form-control" id='form_provider'
481 name='form_provider' value="<?php echo attr($provider); ?>" disabled />
482 </div>
483 <div class="form-group col-lg">
484 <label class="col-form-label" for="form_invoice"><?php echo xlt('Invoice'); ?>:</label>
485 <input type="text" class="form-control" id='form_provider'
486 name='form_provider' value='<?php echo attr($patient_id) . "." . attr($encounter_id); ?>'
487 disabled />
488 </div>
489 <div class="form-group col-lg">
490 <label class="col-form-label" for="svc_date"><?php echo xlt('Svc Date'); ?>:</label>
491 <input type="text" class="form-control" id='svc_date' name='form_provider'
492 value='<?php echo attr($svcdate); ?>' disabled />
493 </div>
494 <div class="card bg-light col-lg-4">
495 <div class="card-title mx-auto"><?php echo xlt('Insurance'); ?></div>
496 <?php
497 for ($i = 1; $i <= 3; ++$i) {
498 $payerid = SLEOB::arGetPayerID($patient_id, $svcdate, $i);
499 if ($payerid) {
500 $tmp = sqlQuery("SELECT name FROM insurance_companies WHERE id = ?", array($payerid));
501 echo "$i: " . $tmp['name'] . "<br />";
505 </div>
506 </div>
507 <div class="form-group mt-3">
508 <textarea name="insurance_name" id="insurance_name" class="form-control" cols="5" rows="2" readonly><?php echo attr($insurance ?? ''); ?></textarea>
509 </div>
510 <div class="form-row">
511 <div class="form-group col-lg">
512 <label class="col-form-label" for="form_stmt_count"><?php echo xlt('Statements Sent'); ?>:</label>
513 <input type='text' name='form_stmt_count' id='form_stmt_count' class="form-control" value='<?php echo attr((0 + $ferow['stmt_count'])); ?>' />
514 </div>
515 <div class="form-group col-lg">
516 <label class="col-form-label" for="form_last_bill"><?php echo xlt('Last Bill Date'); ?>:</label>
517 <input type='text' name="form_last_bill" id='form_last_bill' class="form-control"
518 value ='<?php echo attr($billdate); ?>' disabled />
519 </div>
520 <div class="form-group col-lg">
521 <label class="col-form-label" for="form_reference"><?php echo xlt('Check/EOB No.'); ?>:</label>
522 <input type='text' name='form_reference' id='form_reference' class="form-control" value='' />
523 </div>
524 <div class="form-group col-lg">
525 <label class="col-form-label" for="form_check_date"><?php echo xlt('Check/EOB Date'); ?>:</label>
526 <input type='text' name='form_check_date' id='form_check_date' class='form-control datepicker' value='' />
527 </div>
528 <div class="form-group col-lg">
529 <label class="col-form-label" for="form_deposit_date"><?php echo xlt('Deposit Date'); ?>:</label>
530 <input type='text' name='form_deposit_date' id='form_deposit_date' class='form-control datepicker' value='' />
531 <input type='hidden' name='form_payer_id' value='' />
532 <input type='hidden' name='form_orig_reference' value='' />
533 <input type='hidden' name='form_orig_check_date' value='' />
534 <input type='hidden' name='form_orig_deposit_date' value='' />
535 <input type='hidden' name='form_pay_total' value='' />
536 </div>
537 </div>
538 <div class="form-row">
539 <div class="form-group col-lg">
540 <label class="col-form-label" for="type_code"><?php echo xlt('Now posting for'); ?>:</label>
541 <div class="pl-3">
542 <?php
543 $last_level_closed = 0 + $ferow['last_level_closed'];
545 <label class="radio-inline">
546 <input <?php echo $last_level_closed === 0 ? attr('checked') : ''; ?> name='form_insurance' onclick='setins("Ins1")' type='radio'
547 value='Ins1' /><?php echo xlt('Ins1') ?>
548 </label>
549 <label class="radio-inline">
550 <input <?php echo $last_level_closed === 1 ? attr('checked') : ''; ?> name='form_insurance' onclick='setins("Ins2")' type='radio'
551 value='Ins2' /><?php echo xlt('Ins2') ?>
552 </label>
553 <label class="radio-inline">
554 <input <?php echo $last_level_closed === 2 ? attr('checked') : ''; ?> name='form_insurance' onclick='setins("Ins3")' type='radio'
555 value='Ins3' /><?php echo xlt('Ins3') ?>
556 </label>
557 <label class="radio-inline">
558 <input <?php echo $last_level_closed === 3 ? attr('checked') : ''; ?> name='form_insurance' onclick='setins("Pt")' type='radio'
559 value='Pt' /><?php echo xlt('Patient') ?>
560 </label>
561 <?php
562 // TBD: I think the following is unused and can be removed.
564 <input name='form_eobs' type='hidden' value='<?php echo attr($arrow['shipvia'] ?? '') ?>'/>
565 </div>
566 </div>
567 <div class="form-group col-lg" id='ins_done'>
568 <label class="col-form-label" for=""><?php echo xlt('Done with'); ?>:</label>
569 <a class="btn btn-save bg-light text-primary"
570 onclick="document.forms[0].isLastClosed.value='3'; document.forms[0].submit()"><?php echo xlt("Save Level"); ?>
571 </a>
572 <div class="pl-3">
573 <?php
574 // Write a checkbox for each insurance. It is to be checked when
575 // we no longer expect any payments from that company for the claim.
576 $last_level_closed = 0 + $ferow['last_level_closed'];
577 foreach (array(0 => 'None', 1 => 'Ins1', 2 => 'Ins2', 3 => 'Ins3') as $key => $value) {
578 if ($key && !SLEOB::arGetPayerID($patient_id, $svcdate, $key)) {
579 continue;
581 $checked = ($last_level_closed == $key) ? " checked" : "";
582 echo "<label class='radio-inline'>";
583 echo "<input type='radio' name='form_done' value='" . attr($key) . "'$checked />" . text($value);
584 echo "</label>";
587 </div>
588 </div>
589 <div class="form-group col-lg">
590 <label class="col-form-label" for=""><?php echo xlt('Secondary billing'); ?>:</label>
591 <div class="pl-3">
592 <label class="checkbox-inline">
593 <input name="form_secondary" type="checkbox" value="1" /><?php echo xlt('Needs secondary billing') ?>
594 </label>
595 </div>
596 </div>
597 </div>
598 </fieldset>
599 <fieldset>
600 <legend><?php echo xlt('Invoice Details'); ?></legend>
601 <div class="table-responsive">
602 <table class="table table-sm">
603 <thead>
604 <tr>
605 <th><?php echo xlt('Code') ?></th>
606 <th class="text-left"><?php echo xlt('Charge') ?></th>
607 <th class="text-left"><?php echo xlt('Balance') ?>&nbsp;</th>
608 <th><?php echo xlt('By/Source') ?></th>
609 <th><?php echo xlt('Date') ?></th>
610 <th><?php echo xlt('Pay') ?></th>
611 <th><?php echo xlt('Adjust') ?></th>
612 <th>&nbsp;</th>
613 <th><?php echo xlt('Reason') ?></th>
614 <?php
615 if ($ALLOW_DELETE) { ?>
616 <th><?php echo xlt('Del') ?></th>
617 <?php
618 } ?>
619 </tr>
620 </thead>
621 <?php
622 $firstProcCodeIndex = -1;
623 $encount = 0;
624 foreach ($codes as $code => $cdata) {
625 ++$encount;
626 $dispcode = $code;
628 // remember the index of the first entry whose code is not "CO-PAY", i.e. it's a legitimate proc code
629 if ($firstProcCodeIndex == -1 && strcmp($code, "CO-PAY") != 0) {
630 $firstProcCodeIndex = $encount;
633 // this sorts the details more or less chronologically:
634 ksort($cdata['dtl']);
635 foreach ($cdata['dtl'] as $dkey => $ddata) {
636 $ddate = substr($dkey, 0, 10);
637 if (preg_match('/^(\d\d\d\d)(\d\d)(\d\d)\s*$/', $ddate, $matches)) {
638 $ddate = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
640 $tmpchg = "";
641 $tmpadj = "";
642 if (!empty($ddata['chg']) && ($ddata['chg'] != 0)) {
643 if (isset($ddata['rsn'])) {
644 $tmpadj = 0 - $ddata['chg'];
645 } else {
646 $tmpchg = $ddata['chg'];
650 <tr>
651 <td class="detail" style="background:<?php echo $dispcode ? 'lightyellow' : ''; ?>"><?php echo text($dispcode); $dispcode = "" ?></td>
652 <td class="detail"><?php echo text(bucks($tmpchg)); ?></td>
653 <td class="detail">&nbsp;</td>
654 <td class="detail">
655 <?php
656 if (isset($ddata['plv'])) {
657 if (!$ddata['plv']) {
658 echo 'Pt/';
659 } else {
660 echo 'Ins' . text($ddata['plv']) . '/';
663 echo text($ddata['src'] ?? '');
665 </td>
666 <td class="detail"><?php echo text($ddate); ?></td>
667 <td class="detail"><?php echo text(bucks($ddata['pmt'] ?? '')); ?></td>
668 <td class="detail"><?php echo text(bucks($tmpadj)); ?></td>
669 <td class="detail">&nbsp;</td>
670 <td class="detail"><?php echo text($ddata['rsn'] ?? ''); ?></td>
671 <?php
672 if ($ALLOW_DELETE) { ?>
673 <td class="detail">
674 <?php
675 if (!empty($ddata['arseq'])) { ?>
676 <input name="form_del[<?php echo attr($ddata['arseq']); ?>]"
677 type="checkbox" />
678 <?php
679 } else {
680 ?> &nbsp;
681 <?php
682 } ?>
683 </td>
684 <?php } ?>
685 </tr>
686 <?php } // end of prior detail line ?>
687 <tr>
688 <td class="last_detail"><?php echo text($dispcode);
689 $dispcode = "" ?>
690 </td>
691 <td class="last_detail">&nbsp;</td>
692 <td class="last_detail">
693 <input name="form_line[<?php echo attr($code); ?>][bal]" type="hidden"
694 value="<?php echo attr(bucks($cdata['bal'])); ?>" />
695 <input name="form_line[<?php echo attr($code); ?>][ins]" type="hidden"
696 value="<?php echo attr($cdata['ins'] ?? ''); ?>" />
697 <input name="form_line[<?php echo attr($code); ?>][code_type]" type="hidden"
698 value="<?php echo attr($cdata['code_type']); ?>" /> <?php echo text(sprintf("%.2f", $cdata['bal'])); ?>
699 &nbsp;
700 </td>
701 <td class="last_detail"></td>
702 <td class="last_detail"></td>
703 <td class="last_detail">
704 <input name="form_line[<?php echo attr($code); ?>][pay]"
705 onkeyup="updateFields(document.forms[0]['form_line[<?php echo attr($code); ?>][pay]'], document.forms[0]['form_line[<?php echo attr($code); ?>][adj]'], document.forms[0]['form_line[<?php echo attr($code); ?>][bal]'], document.forms[0]['form_line[CO-PAY][bal]'], <?php echo ($firstProcCodeIndex == $encount) ? 1 : 0 ?>)"
706 onfocus="this.select()" autofocus size="10" type="text" class="form-control"
707 value="0.00" />
708 </td>
709 <td class="last_detail">
710 <input name="form_line[<?php echo attr($code); ?>][adj]" size="10" type="text"
711 class="form-control"
712 value='<?php echo attr((!empty($totalAdjAmount)) ? $totalAdjAmount : '0.00'); ?>'
713 onclick="this.select()" />
714 </td>
715 <td class="last_detail text-center">
716 <a href="#" class="text-decoration-none" onclick="return writeoff(<?php echo attr_js($code); ?>)">WO</a>
717 </td>
718 <td class="last_detail">
719 <select class="form-control" name="form_line[<?php echo attr($code); ?>][reason]">
720 <?php
721 // Adjustment reasons are now taken from the list_options table.
722 echo " <option value=''></option>\n";
723 $ores = sqlStatement("SELECT option_id, title, is_default FROM list_options " .
724 "WHERE list_id = 'adjreason' AND activity = 1 ORDER BY seq, title");
725 while ($orow = sqlFetchArray($ores)) {
726 echo " <option value='" . attr($orow['option_id']) . "'";
727 if ($orow['is_default']) {
728 echo " selected";
730 echo ">" . text($orow['title']) . "</option>\n";
733 </select>
734 <?php
735 // TBD: Maybe a comment field would be good here, for appending
736 // to the reason.
738 </td>
739 <?php if ($ALLOW_DELETE) { ?>
740 <td class="last_detail">&nbsp;</td>
741 <?php } ?>
742 </tr>
743 <?php } // end of code ?>
744 </table>
745 </div>
746 </fieldset>
747 <?php //can change position of buttons by creating a class 'position-override' and adding rule text-align:center or right as the case may be in individual stylesheets ?>
748 <div class="form-group col-lg clearfix">
749 <div class="col-sm-12 text-left position-override" id="search-btn">
750 <div class="btn-group" role="group">
751 <!-- @todo leave as I may still use sjp 08/2020 -->
752 <!--<button type='submit' class="btn btn-primary btn-save" name='form_save' id="btn-save-stay"
753 onclick="this.value='1';"><?php /*echo xlt("Save Current"); */?></button>-->
754 <button type='submit' class="btn btn-primary btn-save" name='form_save' id="btn-save"
755 onclick="this.value='2';"><?php echo xlt("Save"); ?></button>
756 <button type='button' class="btn btn-secondary btn-cancel" name='form_cancel'
757 id="btn-cancel" onclick='doClose()'><?php echo xlt("Close"); ?></button>
758 </div>
759 <?php if ($from_posting) { ?>
760 <button type='button' class="btn btn-secondary btn-view float-right" name='form_goto' id="btn-goto"
761 onclick="goEncounterSummary(<?php echo attr_js($patient_id) ?>)"><?php echo xlt("Past Encounters"); ?></button>
762 <?php } ?>
763 </div>
764 </div>
765 </form>
766 </div>
767 </div><!--End of container div-->
768 <?php if ($from_posting) { ?>
769 <script>
770 var f1 = opener.document.forms[0];
771 var f2 = document.forms[0];
772 if (f1.form_source) {
773 <?php
774 // These support creation and lookup of ar_session table entries:
775 echo " f2.form_reference.value = f1.form_source.value;\n";
776 echo " f2.form_check_date.value = f1.form_paydate.value;\n";
777 echo " //f2.form_deposit_date.value = f1.form_deposit_date.value;\n";
778 echo " if (f1.form_deposit_date.value != '')\n";
779 echo " f2.form_deposit_date.value = f1.form_deposit_date.value;\n";
780 echo " else\n";
781 echo " f2.form_deposit_date.value = getFormattedToday();\n";
782 echo " f2.form_payer_id.value = f1.form_payer_id.value;\n";
783 echo " f2.form_pay_total.value = f1.form_amount.value;\n";
784 echo " f2.form_orig_reference.value = f1.form_source.value;\n";
785 echo " f2.form_orig_check_date.value = f1.form_paydate.value;\n";
786 echo " f2.form_orig_deposit_date.value = f1.form_deposit_date.value;\n";
788 // While I'm thinking about it, some notes about eob sessions.
789 // If they do not have all of the session key fields in the search
790 // page, then show a warning at the top of the invoice page.
791 // Also when they go to save the invoice page and a session key
792 // field has changed, alert them to that and allow a cancel.
794 // Another point... when posting EOBs, the incoming payer ID might
795 // not match the payer ID for the patient's insurance. This is
796 // because the same payer might be entered more than once into the
797 // insurance_companies table. I don't think it matters much.
800 setins("Ins1");
801 </script>
802 <?php } ?>
803 </body>
804 </html>