Added ar_activity.deleted to track voids of payments and adjustments. (#3743)
[openemr.git] / interface / patient_file / front_payment.php
blob0271cb189333294671abff42233c1447453a6c67
1 <?php
3 /**
4 * Front payment gui.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2006-2020 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once("../globals.php");
16 require_once("$srcdir/patient.inc");
17 require_once("$srcdir/payment.inc.php");
18 require_once("$srcdir/forms.inc");
19 require_once("../../custom/code_types.inc.php");
20 require_once("$srcdir/options.inc.php");
21 require_once("$srcdir/encounter_events.inc.php");
23 use OpenEMR\Billing\BillingUtilities;
24 use OpenEMR\Common\Acl\AclMain;
25 use OpenEMR\Common\Csrf\CsrfUtils;
26 use OpenEMR\Core\Header;
27 use OpenEMR\OeUI\OemrUI;
28 use OpenEMR\Services\FacilityService;
30 $pid = $_REQUEST['hidden_patient_code'] > 0 ? $_REQUEST['hidden_patient_code'] : $pid;
32 $facilityService = new FacilityService();
35 <!DOCTYPE html>
36 <html>
37 <head>
38 <?php Header::setupHeader(['opener']);?>
40 <?php
41 // Format dollars for display.
43 function bucks($amount)
45 if ($amount) {
46 $amount = oeFormatMoney($amount);
47 return $amount;
49 return '';
52 function rawbucks($amount)
54 if ($amount) {
55 $amount = sprintf("%.2f", $amount);
56 return $amount;
58 return '';
61 // Display a row of data for an encounter.
63 $var_index = 0;
64 function echoLine($iname, $date, $charges, $ptpaid, $inspaid, $duept, $encounter = 0, $copay = 0, $patcopay = 0)
66 global $var_index;
67 $var_index++;
68 $balance = bucks($charges - $ptpaid - $inspaid);
69 $balance = (round($duept, 2) != 0) ? 0 : $balance;//if balance is due from patient, then insurance balance is displayed as zero
70 $encounter = $encounter ? $encounter : '';
71 echo " <tr id='tr_" . attr($var_index) . "' >\n";
72 echo " <td class='detail'>" . text(oeFormatShortDate($date)) . "</td>\n";
73 echo " <td class='detail' id='" . attr($date) . "' align='center'>" . text($encounter) . "</td>\n";
74 echo " <td class='detail' align='center' id='td_charges_$var_index' >" . text(bucks($charges)) . "</td>\n";
75 echo " <td class='detail' align='center' id='td_inspaid_$var_index' >" . text(bucks($inspaid * -1)) . "</td>\n";
76 echo " <td class='detail' align='center' id='td_ptpaid_$var_index' >" . text(bucks($ptpaid * -1)) . "</td>\n";
77 echo " <td class='detail' align='center' id='td_patient_copay_$var_index' >" . text(bucks($patcopay)) . "</td>\n";
78 echo " <td class='detail' align='center' id='td_copay_$var_index' >" . text(bucks($copay)) . "</td>\n";
79 echo " <td class='detail' align='center' id='balance_$var_index'>" . text(bucks($balance)) . "</td>\n";
80 echo " <td class='detail' align='center' id='duept_$var_index'>" . text(bucks(round($duept, 2) * 1)) . "</td>\n";
81 echo " <td class='detail' align='right'><input type='text' name='" . attr($iname) . "' id='paying_" . attr($var_index) . "' " .
82 " value='' onchange='coloring();calctotal()' autocomplete='off' " .
83 "onkeyup='calctotal()' style='width:50px'/></td>\n";
84 echo " </tr>\n";
87 // We use this to put dashes, colons, etc. back into a timestamp.
89 function decorateString($fmt, $str)
91 $res = '';
92 while ($fmt) {
93 $fc = substr($fmt, 0, 1);
94 $fmt = substr($fmt, 1);
95 if ($fc == '.') {
96 $res .= substr($str, 0, 1);
97 $str = substr($str, 1);
98 } else {
99 $res .= $fc;
103 return $res;
106 // Compute taxes from a tax rate string and a possibly taxable amount.
108 function calcTaxes($row, $amount)
110 $total = 0;
111 if (empty($row['taxrates'])) {
112 return $total;
115 $arates = explode(':', $row['taxrates']);
116 if (empty($arates)) {
117 return $total;
120 foreach ($arates as $value) {
121 if (empty($value)) {
122 continue;
125 $trow = sqlQuery("SELECT option_value FROM list_options WHERE " .
126 "list_id = 'taxrate' AND option_id = ? AND activity = 1 LIMIT 1", array($value));
127 if (empty($trow['option_value'])) {
128 echo "<!-- Missing tax rate '" . text($value) . "'! -->\n";
129 continue;
132 $tax = sprintf("%01.2f", $amount * $trow['option_value']);
133 // echo "<!-- Rate = '$value', amount = '$amount', tax = '$tax' -->\n";
134 $total += $tax;
137 return $total;
140 $now = time();
141 $today = date('Y-m-d', $now);
142 $timestamp = date('Y-m-d H:i:s', $now);
145 // $patdata = getPatientData($pid, 'fname,lname,pubpid');
147 $patdata = sqlQuery("SELECT " .
148 "p.fname, p.mname, p.lname, p.pubpid,p.pid, i.copay " .
149 "FROM patient_data AS p " .
150 "LEFT OUTER JOIN insurance_data AS i ON " .
151 "i.pid = p.pid AND i.type = 'primary' " .
152 "WHERE p.pid = ? ORDER BY i.date DESC LIMIT 1", array($pid));
154 $alertmsg = ''; // anything here pops up in an alert box
156 // If the Save button was clicked...
157 if ($_POST['form_save']) {
158 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
159 CsrfUtils::csrfNotVerified();
162 $form_pid = $_POST['form_pid'];
163 $form_method = trim($_POST['form_method']);
164 $form_source = trim($_POST['form_source']);
165 $patdata = getPatientData($form_pid, 'fname,mname,lname,pubpid');
166 $NameNew = $patdata['fname'] . " " . $patdata['lname'] . " " . $patdata['mname'];
168 if ($_REQUEST['radio_type_of_payment'] == 'pre_payment') {
169 $payment_id = sqlInsert(
170 "insert into ar_session set " .
171 "payer_id = ?" .
172 ", patient_id = ?" .
173 ", user_id = ?" .
174 ", closed = ?" .
175 ", reference = ?" .
176 ", check_date = now() , deposit_date = now() " .
177 ", pay_total = ?" .
178 ", payment_type = 'patient'" .
179 ", description = ?" .
180 ", adjustment_code = 'pre_payment'" .
181 ", post_to_date = now() " .
182 ", payment_method = ?",
183 array(0, $form_pid, $_SESSION['authUserID'], 0, $form_source, $_REQUEST['form_prepayment'], $NameNew, $form_method)
186 frontPayment($form_pid, 0, $form_method, $form_source, $_REQUEST['form_prepayment'], 0, $timestamp);//insertion to 'payments' table.
189 if ($_POST['form_upay'] && $_REQUEST['radio_type_of_payment'] != 'pre_payment') {
190 foreach ($_POST['form_upay'] as $enc => $payment) {
191 if ($amount = 0 + $payment) {
192 $zero_enc = $enc;
193 if ($_REQUEST['radio_type_of_payment'] == 'invoice_balance') {
194 if (!$enc) {
195 $enc = calendar_arrived($form_pid);
197 } else {
198 if (!$enc) {
199 $enc = calendar_arrived($form_pid);
203 //----------------------------------------------------------------------------------------------------
204 //Fetching the existing code and modifier
205 $ResultSearchNew = sqlStatement(
206 "SELECT * FROM billing LEFT JOIN code_types ON billing.code_type=code_types.ct_key " .
207 "WHERE code_types.ct_fee=1 AND billing.activity!=0 AND billing.pid =? AND encounter=? ORDER BY billing.code,billing.modifier",
208 array($form_pid, $enc)
210 if ($RowSearch = sqlFetchArray($ResultSearchNew)) {
211 $Codetype = $RowSearch['code_type'];
212 $Code = $RowSearch['code'];
213 $Modifier = $RowSearch['modifier'];
214 } else {
215 $Codetype = '';
216 $Code = '';
217 $Modifier = '';
220 //----------------------------------------------------------------------------------------------------
221 if ($_REQUEST['radio_type_of_payment'] == 'copay') {//copay saving to ar_session and ar_activity tables
222 $session_id = sqlInsert(
223 "INSERT INTO ar_session (payer_id,user_id,reference,check_date,deposit_date,pay_total," .
224 " global_amount,payment_type,description,patient_id,payment_method,adjustment_code,post_to_date) " .
225 " VALUES ('0',?,?,now(),now(),?,'','patient','COPAY',?,?,'patient_payment',now())",
226 array($_SESSION['authUserID'], $form_source, $amount, $form_pid, $form_method)
229 sqlBeginTrans();
230 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array($form_pid, $enc));
231 $insrt_id = sqlInsert(
232 "INSERT INTO ar_activity (pid,encounter,sequence_no,code_type,code,modifier,payer_type,post_time,post_user,session_id,pay_amount,account_code)" .
233 " VALUES (?,?,?,?,?,?,0,now(),?,?,?,'PCP')",
234 array($form_pid, $enc, $sequence_no['increment'], $Codetype, $Code, $Modifier, $_SESSION['authUserID'], $session_id, $amount)
236 sqlCommitTrans();
238 frontPayment($form_pid, $enc, $form_method, $form_source, $amount, 0, $timestamp);//insertion to 'payments' table.
241 if ($_REQUEST['radio_type_of_payment'] == 'invoice_balance' || $_REQUEST['radio_type_of_payment'] == 'cash') { //Payment by patient after insurance paid, cash patients similar to do not bill insurance in feesheet.
242 if ($_REQUEST['radio_type_of_payment'] == 'cash') {
243 sqlStatement(
244 "update form_encounter set last_level_closed=? where encounter=? and pid=? ",
245 array(4, $enc, $form_pid)
247 sqlStatement(
248 "update billing set billed=? where encounter=? and pid=?",
249 array(1, $enc, $form_pid)
253 $adjustment_code = 'patient_payment';
254 $payment_id = sqlInsert(
255 "insert into ar_session set " .
256 "payer_id = ?" .
257 ", patient_id = ?" .
258 ", user_id = ?" .
259 ", closed = ?" .
260 ", reference = ?" .
261 ", check_date = now() , deposit_date = now() " .
262 ", pay_total = ?" .
263 ", payment_type = 'patient'" .
264 ", description = ?" .
265 ", adjustment_code = ?" .
266 ", post_to_date = now() " .
267 ", payment_method = ?",
268 array(0, $form_pid, $_SESSION['authUserID'], 0, $form_source, $amount, $NameNew, $adjustment_code, $form_method)
271 //--------------------------------------------------------------------------------------------------------------------
273 frontPayment($form_pid, $enc, $form_method, $form_source, 0, $amount, $timestamp);//insertion to 'payments' table.
275 //--------------------------------------------------------------------------------------------------------------------
277 $resMoneyGot = sqlStatement(
278 "SELECT sum(pay_amount) as PatientPay FROM ar_activity where pid =? and " .
279 "encounter = ? and payer_type = 0 and account_code = 'PCP' AND deleted IS NULL",
280 array($form_pid, $enc)
281 );//new fees screen copay gives account_code='PCP'
282 $rowMoneyGot = sqlFetchArray($resMoneyGot);
283 $Copay = $rowMoneyGot['PatientPay'];
285 //--------------------------------------------------------------------------------------------------------------------
287 //Looping the existing code and modifier
288 $ResultSearchNew = sqlStatement(
289 "SELECT * FROM billing LEFT JOIN code_types ON billing.code_type=code_types.ct_key WHERE code_types.ct_fee=1 " .
290 "AND billing.activity!=0 AND billing.pid =? AND encounter=? ORDER BY billing.code,billing.modifier",
291 array($form_pid, $enc)
293 while ($RowSearch = sqlFetchArray($ResultSearchNew)) {
294 $Codetype = $RowSearch['code_type'];
295 $Code = $RowSearch['code'];
296 $Modifier = $RowSearch['modifier'];
297 $Fee = $RowSearch['fee'];
299 $resMoneyGot = sqlStatement(
300 "SELECT sum(pay_amount) as MoneyGot FROM ar_activity where pid = ? AND deleted IS NULL " .
301 "and code_type=? and code=? and modifier=? and encounter =? and !(payer_type=0 and account_code='PCP')",
302 array($form_pid, $Codetype, $Code, $Modifier, $enc)
304 //new fees screen copay gives account_code='PCP'
305 $rowMoneyGot = sqlFetchArray($resMoneyGot);
306 $MoneyGot = $rowMoneyGot['MoneyGot'];
308 $resMoneyAdjusted = sqlStatement(
309 "SELECT sum(adj_amount) as MoneyAdjusted FROM ar_activity where " .
310 "pid = ? and code_type = ? and code = ? and modifier = ? and encounter = ? AND deleted IS NULL",
311 array($form_pid, $Codetype, $Code, $Modifier, $enc)
313 $rowMoneyAdjusted = sqlFetchArray($resMoneyAdjusted);
314 $MoneyAdjusted = $rowMoneyAdjusted['MoneyAdjusted'];
316 $Remainder = $Fee - $Copay - $MoneyGot - $MoneyAdjusted;
317 $Copay = 0;
318 if (round($Remainder, 2) != 0 && $amount != 0) {
319 if ($amount - $Remainder >= 0) {
320 $insert_value = $Remainder;
321 $amount = $amount - $Remainder;
322 } else {
323 $insert_value = $amount;
324 $amount = 0;
327 sqlBeginTrans();
328 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array($form_pid, $enc));
329 sqlStatement(
330 "insert into ar_activity set " .
331 "pid = ?" .
332 ", encounter = ?" .
333 ", sequence_no = ?" .
334 ", code_type = ?" .
335 ", code = ?" .
336 ", modifier = ?" .
337 ", payer_type = ?" .
338 ", post_time = now() " .
339 ", post_user = ?" .
340 ", session_id = ?" .
341 ", pay_amount = ?" .
342 ", adj_amount = ?" .
343 ", account_code = 'PP'",
344 array($form_pid, $enc, $sequence_no['increment'], $Codetype, $Code, $Modifier, 0, $_SESSION['authUserID'], $payment_id, $insert_value, 0)
346 sqlCommitTrans();
347 }//if
348 }//while
349 if ($amount != 0) {//if any excess is there.
350 sqlBeginTrans();
351 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array($form_pid, $enc));
352 sqlStatement(
353 "insert into ar_activity set " .
354 "pid = ?" .
355 ", encounter = ?" .
356 ", sequence_no = ?" .
357 ", code_type = ?" .
358 ", code = ?" .
359 ", modifier = ?" .
360 ", payer_type = ?" .
361 ", post_time = now() " .
362 ", post_user = ?" .
363 ", session_id = ?" .
364 ", pay_amount = ?" .
365 ", adj_amount = ?" .
366 ", account_code = 'PP'",
367 array($form_pid, $enc, $sequence_no['increment'], $Codetype, $Code, $Modifier, 0, $_SESSION['authUserID'], $payment_id, $amount, 0)
369 sqlCommitTrans();
372 //--------------------------------------------------------------------------------------------------------------------
373 }//invoice_balance
374 }//if ($amount = 0 + $payment)
375 }//foreach
376 }//if ($_POST['form_upay'])
377 }//if ($_POST['form_save'])
379 if ($_POST['form_save'] || $_REQUEST['receipt']) {
380 if ($_REQUEST['receipt']) {
381 $form_pid = $_GET['patient'];
382 $timestamp = decorateString('....-..-.. ..:..:..', $_GET['time']);
385 // Get details for what we guess is the primary facility.
386 $frow = $facilityService->getPrimaryBusinessEntity(array("useLegacyImplementation" => true));
388 // Get the patient's name and chart number.
389 $patdata = getPatientData($form_pid, 'fname,mname,lname,pubpid');
391 // Re-fetch payment info.
392 $payrow = sqlQuery("SELECT " .
393 "SUM(amount1) AS amount1, " .
394 "SUM(amount2) AS amount2, " .
395 "MAX(method) AS method, " .
396 "MAX(source) AS source, " .
397 "MAX(dtime) AS dtime, " .
398 // "MAX(user) AS user " .
399 "MAX(user) AS user, " .
400 "MAX(encounter) as encounter " .
401 "FROM payments WHERE " .
402 "pid = ? AND dtime = ?", array($form_pid, $timestamp));
404 // Create key for deleting, just in case.
405 $ref_id = ($_REQUEST['radio_type_of_payment'] == 'copay') ? $session_id : $payment_id;
406 $payment_key = $form_pid . '.' . preg_replace('/[^0-9]/', '', $timestamp) . '.' . $ref_id;
408 if ($_REQUEST['radio_type_of_payment'] != 'pre_payment') {
409 // get facility from encounter
410 $tmprow = sqlQuery("SELECT `facility_id` FROM `form_encounter` WHERE `encounter` = ?", array($payrow['encounter']));
411 $frow = $facilityService->getById($tmprow['facility_id']);
412 } else {
413 // if pre_payment, then no encounter yet, so get main office address
414 $frow = $facilityService->getPrimaryBillingLocation();
417 // Now proceed with printing the receipt.
420 <title><?php echo xlt('Receipt for Payment'); ?></title>
421 <?php Header::setupHeader(); ?>
422 <script>
424 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
426 $(function () {
427 var win = top.printLogSetup ? top : opener.top;
428 win.printLogSetup(document.getElementById('printbutton'));
431 function closeHow(e) {
432 if (opener) {
433 dlgclose();
434 return;
436 top.activateTabByName('pat', true);
437 top.tabCloseByName(window.name);
440 // This is action to take before printing and is called from restoreSession.php.
441 function printlog_before_print() {
442 let divstyle = document.getElementById('hideonprint').style;
443 divstyle.display = 'none';
444 // currently exit is not hidden by default in case receipt print is not needed
445 // and left here for future option to force users to print via global etc..
446 // can still print later via reports.
447 divstyle = document.getElementById('showonprint').style;
448 divstyle.display = '';
451 // Process click on Delete button.
452 function deleteme() {
453 dlgopen('deleter.php?payment=' + <?php echo js_url($payment_key); ?> + '&csrf_token_form=' + <?php echo js_url(CsrfUtils::collectCsrfToken()); ?>, '_blank', 500, 450);
454 return false;
457 // Called by the deleteme.php window on a successful delete.
458 function imdeleted() {
459 if (opener) {
460 dlgclose(); // we're in reports/leftnav and callback reloads.
461 } else {
462 window.history.back(); // this is us full screen.
466 // Called to switch to the specified encounter having the specified DOS.
467 // This also closes the popup window.
468 function toencounter(enc, datestr, topframe) {
469 topframe.restoreSession();
470 top.goToEncounter(enc);
471 if (opener) dlgclose();
473 </script>
474 </head>
475 <body bgcolor='var(--white)'>
476 <center>
478 <p><h2><?php echo xlt('Receipt for Payment'); ?></h2>
480 <p><?php echo text($frow['name']) ?>
481 <br /><?php echo text($frow['street']) ?>
482 <br /><?php echo text($frow['city'] . ', ' . $frow['state']) . ' ' .
483 text($frow['postal_code']) ?>
484 <br /><?php echo text($frow['phone']) ?>
487 <table border='0' cellspacing='8'>
488 <tr>
489 <td><?php echo xlt('Date'); ?>:</td>
490 <td><?php echo text(oeFormatSDFT(strtotime($payrow['dtime']))) ?></td>
491 </tr>
492 <tr>
493 <td><?php echo xlt('Patient'); ?>:</td>
494 <td><?php echo text($patdata['fname']) . " " . text($patdata['mname']) . " " .
495 text($patdata['lname']) . " (" . text($patdata['pubpid']) . ")" ?></td>
496 </tr>
497 <tr>
498 <td><?php echo xlt('Paid Via'); ?>:</td>
499 <td><?php echo generate_display_field(array('data_type' => '1', 'list_id' => 'payment_method'), $payrow['method']); ?></td>
500 </tr>
501 <tr>
502 <td><?php echo xlt('Check/Ref Number'); ?>:</td>
503 <td><?php echo text($payrow['source']) ?></td>
504 </tr>
505 <tr>
506 <td><?php echo xlt('Amount for This Visit'); ?>:</td>
507 <td><?php echo text(oeFormatMoney($payrow['amount1'])) ?></td>
508 </tr>
509 <tr>
510 <td>
511 <?php
512 if ($_REQUEST['radio_type_of_payment'] == 'pre_payment') {
513 echo xlt('Pre-payment Amount');
514 } else {
515 echo xlt('Amount for Past Balance');
518 :</td>
519 <td><?php echo text(oeFormatMoney($payrow['amount2'])) ?></td>
520 </tr>
521 <tr>
522 <td><?php echo xlt('Received By'); ?>:</td>
523 <td><?php echo text($payrow['user']) ?></td>
524 </tr>
525 </table>
527 <div id='hideonprint'>
529 <input type='button' value='<?php echo xla('Print'); ?>' id='printbutton' />
531 <?php
532 $todaysenc = todaysEncounterIf($pid);
533 if ($todaysenc && $todaysenc != $encounter) {
534 echo "&nbsp;<input type='button' " .
535 "value='" . xla('Open Today`s Visit') . "' " .
536 "onclick='toencounter(" . attr_js($todaysenc) . ", " . attr_js($today) . ", (opener ? opener.top : top))' />\n";
540 <?php if (AclMain::aclCheckCore('admin', 'super') || AclMain::aclCheckCore('acct', 'bill')) {
541 // allowing biller to delete payments ?>
542 &nbsp;
543 <input type='button' value='<?php echo xla('Delete'); ?>' style='color:red' onclick='deleteme()' />
544 <?php } ?>
546 </div>
547 <div id='showonprint'>
548 <input type='button' value='<?php echo xla('Exit'); ?>' id='donebutton' onclick="closeHow(event)"/>
549 </div>
550 </center>
551 </body>
553 <?php
555 // End of receipt printing logic.
557 } else {
559 // Here we display the form for data entry.
562 <title><?php echo xlt('Record Payment'); ?></title>
564 <style>
565 body { font-family:sans-serif; font-size:10pt; font-weight:normal }
566 .dehead { color:var(--black); font-family:sans-serif; font-size:10pt; font-weight:bold }
567 .detail { color:var(--black); font-family:sans-serif; font-size:10pt; font-weight:normal }
568 #ajax_div_patient {
569 position: absolute;
570 z-index:10;
571 background-color: #FBFDD0;
572 border: 1px solid #ccc;
573 padding: 10px;
575 </style>
576 <!--Removed standard dependencies 12/29/17 as not needed any longer since moved to a tab/frame not popup.-->
580 <!-- supporting javascript code -->
581 <script>
582 var mypcc = '1';
583 </script>
584 <?php include_once("{$GLOBALS['srcdir']}/ajax/payment_ajax_jav.inc.php"); ?>
585 <script>
586 document.onclick=HideTheAjaxDivs;
587 </script>
589 <?php Header::setupAssets('topdialog'); ?>
591 <script>
592 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
593 function closeHow(e) {
594 if (opener) {
595 dlgclose();
596 return;
598 top.activateTabByName('pat', true);
599 top.tabCloseByName(window.name);
601 function calctotal() {
602 var f = document.forms[0];
603 var total = 0;
604 for (var i = 0; i < f.elements.length; ++i) {
605 var elem = f.elements[i];
606 var ename = elem.name;
607 if (ename.indexOf('form_upay[') == 0 || ename.indexOf('form_bpay[') == 0) {
608 if (elem.value.length > 0) total += Number(elem.value);
611 f.form_paytotal.value = Number(total).toFixed(2);
612 return true;
615 function coloring() {
616 for (var i = 1; ; ++i) {
617 if (document.getElementById('paying_' + i)) {
618 paying = document.getElementById('paying_' + i).value * 1;
619 patient_balance = document.getElementById('duept_' + i).innerHTML * 1;
621 //balance=document.getElementById('balance_'+i).innerHTML*1;
622 if (patient_balance > 0 && paying > 0) {
623 if (paying > patient_balance) {
624 document.getElementById('paying_' + i).style.background = '#FF0000';
626 else if (paying < patient_balance) {
627 document.getElementById('paying_' + i).style.background = '#99CC00';
629 else if (paying == patient_balance) {
630 document.getElementById('paying_' + i).style.background = 'var(--white)';
632 } else {
633 document.getElementById('paying_' + i).style.background = 'var(--white)';
636 else {
637 break;
642 function CheckVisible(MakeBlank) { //Displays and hides the check number text box.
643 if (document.getElementById('form_method').options[document.getElementById(
644 'form_method').selectedIndex].value == 'check_payment' || document.getElementById(
645 'form_method').options[document.getElementById('form_method').selectedIndex]
646 .value == 'bank_draft') {
647 document.getElementById('check_number').disabled = false;
648 } else {
649 document.getElementById('check_number').disabled = true;
653 function validate() {
654 var f = document.forms[0];
655 ok = -1;
656 top.restoreSession();
657 issue = 'no';
658 // prevent an empty form submission
659 let flgempty = true;
660 for (let i = 0; i < f.elements.length; ++i) {
661 let ename = f.elements[i].name;
662 if (f.elements[i].value == 'pre_payment' && f.elements[i].checked === true) {
663 if (Number(f.elements.namedItem("form_prepayment").value) !== 0) {
664 flgempty = false;
666 break;
668 if (ename.indexOf('form_upay[') === 0 || ename.indexOf('form_bpay[') === 0) {
669 if (Number(f.elements[i].value) !== 0) flgempty = false;
672 if (flgempty) {
673 alert(<?php echo xlj('A Payment is Required!. Please input a payment line item entry.'); ?>);
674 return false;
676 // continue validation.
677 if (((document.getElementById('form_method').options[document.getElementById('form_method').selectedIndex].value == 'check_payment' ||
678 document.getElementById('form_method').options[document.getElementById('form_method').selectedIndex].value == 'bank_draft') &&
679 document.getElementById('check_number').value == '')) {
680 alert(<?php echo xlj('Please Fill the Check/Ref Number'); ?>);
681 document.getElementById('check_number').focus();
682 return false;
684 if (document.getElementById('radio_type_of_payment_self1').checked == false &&
685 document.getElementById('radio_type_of_payment1').checked == false &&
686 document.getElementById('radio_type_of_payment2').checked == false &&
687 document.getElementById('radio_type_of_payment4').checked == false) {
688 alert(<?php echo xlj('Please Select Type Of Payment.'); ?>);
689 return false;
691 if (document.getElementById('radio_type_of_payment_self1').checked == true ||
692 document.getElementById('radio_type_of_payment1').checked == true) {
693 for (var i = 0; i < f.elements.length; ++i) {
694 var elem = f.elements[i];
695 var ename = elem.name;
696 if (ename.indexOf('form_upay[0') == 0) //Today is this text box.
698 if (elem.value * 1 > 0) {//A warning message, if the amount is posted with out encounter.
699 if (confirm(<?php echo xlj('If patient has appointment click OK to create encounter otherwise, cancel this and then create an encounter for today visit.'); ?>)) {
700 ok = 1;
701 } else {
702 elem.focus();
703 return false;
706 break;
711 if (document.getElementById('radio_type_of_payment1').checked == true){//CO-PAY
712 var total = 0;
713 for (var i = 0; i < f.elements.length; ++i) {
714 var elem = f.elements[i];
715 var ename = elem.name;
716 if (ename.indexOf('form_upay[0]') == 0) {//Today is this text box.
717 if (f.form_paytotal.value * 1 != elem.value * 1) {//Total CO-PAY is not posted against today
718 //A warning message, if the amount is posted against an old encounter.
719 if (confirm(<?php echo xlj('You are posting against an old encounter?'); ?>)) {
720 ok = 1;
721 } else {
722 elem.focus();
723 return false;
726 break;
729 }//Co Pay
730 else if (document.getElementById('radio_type_of_payment2').checked == true) {//Invoice Balance
731 for (var i = 0; i < f.elements.length; ++i) {
732 var elem = f.elements[i];
733 var ename = elem.name;
734 if (ename.indexOf('form_upay[0') == 0) {
735 if (elem.value * 1 > 0) {
736 alert(<?php echo xlj('Invoice Balance cannot be posted. No Encounter is created.'); ?>);
737 return false;
739 break;
743 if (ok == -1) {
744 if (confirm(<?php echo xlj('Would you like to save?'); ?>)) {
745 return true;
747 else {
748 return false;
753 function cursor_pointer() { //Point the cursor to the latest encounter(Today)
754 var f = document.forms[0];
755 var total = 0;
756 for (var i = 0; i < f.elements.length; ++i) {
757 var elem = f.elements[i];
758 var ename = elem.name;
759 if (ename.indexOf('form_upay[') == 0) {
760 elem.focus();
761 break;
765 //=====================================================
766 function make_it_hide_enc_pay() {
767 document.getElementById('td_head_insurance_payment').style.display = "none";
768 document.getElementById('td_head_patient_co_pay').style.display = "none";
769 document.getElementById('td_head_co_pay').style.display = "none";
770 document.getElementById('td_head_insurance_balance').style.display = "none";
771 for (var i = 1; ; ++i) {
772 var td_inspaid_elem = document.getElementById('td_inspaid_' + i)
773 var td_patient_copay_elem = document.getElementById('td_patient_copay_' + i)
774 var td_copay_elem = document.getElementById('td_copay_' + i)
775 var balance_elem = document.getElementById('balance_' + i)
776 if (td_inspaid_elem) {
777 td_inspaid_elem.style.display = "none";
778 td_patient_copay_elem.style.display = "none";
779 td_copay_elem.style.display = "none";
780 balance_elem.style.display = "none";
781 } else {
782 break;
785 document.getElementById('td_total_4').style.display = "none";
786 document.getElementById('td_total_7').style.display = "none";
787 document.getElementById('td_total_8').style.display = "none";
788 document.getElementById('td_total_6').style.display = "none";
789 document.getElementById('table_display').width = "420px";
791 //=====================================================
792 function make_visible() {
793 document.getElementById('td_head_rep_doc').style.display = "";
794 document.getElementById('td_head_description').style.display = "";
795 document.getElementById('td_head_total_charge').style.display = "none";
796 document.getElementById('td_head_insurance_payment').style.display = "none";
797 document.getElementById('td_head_patient_payment').style.display = "none";
798 document.getElementById('td_head_patient_co_pay').style.display = "none";
799 document.getElementById('td_head_co_pay').style.display = "none";
800 document.getElementById('td_head_insurance_balance').style.display = "none";
801 document.getElementById('td_head_patient_balance').style.display = "none";
802 for (var i = 1; ; ++i) {
803 var td_charges_elem = document.getElementById('td_charges_' + i)
804 var td_inspaid_elem = document.getElementById('td_inspaid_' + i)
805 var td_ptpaid_elem = document.getElementById('td_ptpaid_' + i)
806 var td_patient_copay_elem = document.getElementById('td_patient_copay_' + i)
808 var td_copay_elem = document.getElementById('td_copay_' + i)
809 var balance_elem = document.getElementById('balance_' + i)
810 var duept_elem = document.getElementById('duept_' + i)
811 if (td_charges_elem) {
812 td_charges_elem.style.display = "none";
813 td_inspaid_elem.style.display = "none";
814 td_ptpaid_elem.style.display = "none";
815 td_patient_copay_elem.style.display = "none";
816 td_copay_elem.style.display = "none";
817 balance_elem.style.display = "none";
818 duept_elem.style.display = "none";
819 } else {
820 break;
823 document.getElementById('td_total_7').style.display = "";
824 document.getElementById('td_total_8').style.display = "";
825 document.getElementById('td_total_1').style.display = "none";
826 document.getElementById('td_total_2').style.display = "none";
827 document.getElementById('td_total_3').style.display = "none";
828 document.getElementById('td_total_4').style.display = "none";
829 document.getElementById('td_total_5').style.display = "none";
830 document.getElementById('td_total_6').style.display = "none";
831 document.getElementById('table_display').width = "505px";
834 function make_it_hide() {
835 document.getElementById('td_head_rep_doc').style.display = "none";
836 document.getElementById('td_head_description').style.display = "none";
837 document.getElementById('td_head_total_charge').style.display = "";
838 document.getElementById('td_head_insurance_payment').style.display = "";
839 document.getElementById('td_head_patient_payment').style.display = "";
840 document.getElementById('td_head_patient_co_pay').style.display = "";
841 document.getElementById('td_head_co_pay').style.display = "";
842 document.getElementById('td_head_insurance_balance').style.display = "";
843 document.getElementById('td_head_patient_balance').style.display = "";
844 for (var i = 1; ; ++i) {
845 var td_charges_elem = document.getElementById('td_charges_' + i)
846 var td_inspaid_elem = document.getElementById('td_inspaid_' + i)
847 var td_ptpaid_elem = document.getElementById('td_ptpaid_' + i)
848 var td_patient_copay_elem = document.getElementById('td_patient_copay_' + i)
850 var td_copay_elem = document.getElementById('td_copay_' + i)
851 var balance_elem = document.getElementById('balance_' + i)
852 var duept_elem = document.getElementById('duept_' + i)
853 if (td_charges_elem) {
854 td_charges_elem.style.display = "";
855 td_inspaid_elem.style.display = "";
856 td_ptpaid_elem.style.display = "";
857 td_patient_copay_elem.style.display = "";
858 td_copay_elem.style.display = "";
859 balance_elem.style.display = "";
860 duept_elem.style.display = "";
861 } else {
862 break;
865 document.getElementById('td_total_1').style.display = "";
866 document.getElementById('td_total_2').style.display = "";
867 document.getElementById('td_total_3').style.display = "";
868 document.getElementById('td_total_4').style.display = "";
869 document.getElementById('td_total_5').style.display = "";
870 document.getElementById('td_total_6').style.display = "";
871 document.getElementById('td_total_7').style.display = "";
872 document.getElementById('td_total_8').style.display = "";
873 document.getElementById('table_display').width = "635px";
876 function make_visible_radio() {
877 document.getElementById('tr_radio1').style.display = "";
878 document.getElementById('tr_radio2').style.display = "none";
881 function make_hide_radio() {
882 document.getElementById('tr_radio1').style.display = "none";
883 document.getElementById('tr_radio2').style.display = "";
886 function make_visible_row() {
887 document.getElementById('table_display').style.display = "";
888 document.getElementById('table_display_prepayment').style.display = "none";
891 function make_hide_row() {
892 document.getElementById('table_display').style.display = "none";
893 document.getElementById('table_display_prepayment').style.display = "";
896 function make_self() {
897 make_visible_row();
898 make_it_hide();
899 make_it_hide_enc_pay();
900 document.getElementById('radio_type_of_payment_self1').checked = true;
901 cursor_pointer();
904 function make_insurance() {
905 make_visible_row();
906 make_it_hide();
907 cursor_pointer();
908 document.getElementById('radio_type_of_payment1').checked = true;
910 </script>
912 <style>
913 @media only screen and (max-width: 768px) {
914 [class*="col-"] {
915 width: 100%;
916 text-align: left!Important;
919 .table {
920 margin: auto;
921 width: 90% !important;
923 @media (min-width: 992px) {
924 .modal-lg {
925 width: 1000px !Important;
928 </style>
929 <title><?php echo xlt('Record Payment'); ?></title>
930 <?php $NameNew = $patdata['fname'] . " " . $patdata['lname'] . " " . $patdata['mname']; ?>
931 <?php
932 $arrOeUiSettings = array(
933 'heading_title' => xl('Accept Payment'),
934 'include_patient_name' => true,// use only in appropriate pages
935 'expandable' => false,
936 'expandable_files' => array(),//all file names need suffix _xpd
937 'action' => "",//conceal, reveal, search, reset, link or back
938 'action_title' => "",
939 'action_href' => "",//only for actions - reset, link or back
940 'show_help_icon' => false,
941 'help_file_name' => ""
943 $oemr_ui = new OemrUI($arrOeUiSettings);
945 </head>
946 <body>
947 <div class="container"><!--begin container div for form-->
948 <div class="row">
949 <div class="col-sm-12">
950 <div class="page-header">
951 <?php echo $oemr_ui->pageHeading() . "\r\n"; ?>
952 </div>
953 </div>
954 </div>
955 <div class="row">
956 <div class="col-sm-12">
957 <form method='post' action='front_payment.php<?php echo ($payid) ? "?payid=" . attr_url($payid) : ""; ?>' onsubmit='return validate();'>
958 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
959 <input name='form_pid' type='hidden' value='<?php echo attr($pid) ?>'>
960 <fieldset>
961 <legend><?php echo xlt('Payment'); ?></legend>
962 <div class="col-12 oe-custom-line">
963 <div class="col-3 offset-lg-3">
964 <label class="control-label" for="form_method"><?php echo xlt('Payment Method'); ?>:</label>
965 </div>
966 <div class="col-3">
967 <select class="form-control" id="form_method" name="form_method" onchange='CheckVisible("yes")'>
968 <?php
969 $query1112 = "SELECT * FROM list_options where list_id=? ORDER BY seq, title ";
970 $bres1112 = sqlStatement($query1112, array('payment_method'));
971 while ($brow1112 = sqlFetchArray($bres1112)) {
972 if ($brow1112['option_id'] == 'electronic' || $brow1112['option_id'] == 'bank_draft') {
973 continue;
975 echo "<option value='" . attr($brow1112['option_id']) . "'>" . text(xl_list_label($brow1112['title'])) . "</option>";
978 </select>
979 </div>
980 </div>
981 <div class="col-12 oe-custom-line">
982 <div class="col-3 offset-lg-3">
983 <label class="control-label" for="check_number"><?php echo xlt('Check/Ref Number'); ?>:</label>
984 </div>
985 <div class="col-3">
986 <div id="ajax_div_patient" style="display:none;"></div>
987 <input type='text' id="check_number" name='form_source' class= 'form-control' value='<?php echo attr($payrow['source']); ?>'>
988 </div>
989 </div>
990 <div class="col-12 oe-custom-line">
991 <div class="col-3 offset-lg-3">
992 <label class="control-label" for="form_discount"><?php echo xla('Patient Coverage'); ?>:</label>
993 </div>
994 <div class="col-6">
995 <div style="padding-left:15px">
996 <label class="radio-inline">
997 <input id="radio_type_of_coverage1" name="radio_type_of_coverage" onclick="make_visible_radio();make_self();" type="radio" value="self"><?php echo xlt('Self'); ?>
998 </label>
999 <label class="radio-inline">
1000 <input checked="checked" id="radio_type_of_coverag2" name="radio_type_of_coverage" onclick="make_hide_radio();make_insurance();" type="radio" value="insurance"><?php echo xlt('Insurance'); ?>
1001 </label>
1002 </div>
1003 </div>
1004 </div>
1005 <div class="col-12 oe-custom-line">
1006 <div class="col-3 offset-lg-3">
1007 <label class="control-label" for=""><?php echo xlt('Payment against'); ?>:</label>
1008 </div>
1009 <div class="col-6">
1010 <div id="tr_radio1" style="padding-left:15px; display:none"><!-- For radio Insurance -->
1011 <label class="radio-inline">
1012 <input id="radio_type_of_payment_self1" name="radio_type_of_payment" onclick="make_visible_row();make_it_hide_enc_pay();cursor_pointer();" type="radio" value="cash"><?php echo xlt('Encounter Payment'); ?>
1013 </label>
1014 </div>
1015 <div id="tr_radio2" style="padding-left:15px"><!-- For radio self -->
1016 <label class="radio-inline">
1017 <input checked="checked" id="radio_type_of_payment1" name="radio_type_of_payment" onclick="make_visible_row();cursor_pointer();" type="radio" value="copay"><?php echo xlt('Co Pay'); ?>
1018 </label>
1019 <label class="radio-inline">
1020 <input id="radio_type_of_payment2" name="radio_type_of_payment" onclick="make_visible_row();" type="radio" value="invoice_balance"><?php echo xlt('Invoice Balance'); ?><br />
1021 </label>
1022 <label class="radio-inline">
1023 <input id="radio_type_of_payment4" name="radio_type_of_payment" onclick="make_hide_row();" type="radio" value="pre_payment"><?php echo xlt('Pre Pay'); ?>
1024 </label>
1025 </div>
1026 </div>
1027 </div>
1028 <div class="col-12 oe-custom-line">
1029 <div id="table_display_prepayment" style="display:none">
1030 <div class="col-3 offset-lg-3">
1031 <label class="control-label" for="form_prepayment"><?php echo xlt('Pre Payment'); ?>:</label>
1032 </div>
1033 <div class="col-3">
1034 <input name='form_prepayment' id='form_prepayment'class='form-control' type='text' value =''>
1035 </div>
1036 </div>
1037 </div>
1038 </fieldset>
1039 <fieldset>
1040 <legend><?php echo xlt('Collect For'); ?></legend>
1041 <div class= "table-responsive">
1042 <table class = "table" id="table_display">
1043 <thead>
1044 <tr bgcolor="#CCCCCC" id="tr_head">
1045 <td class="dehead" width="70"><?php echo xlt('DOS'); ?></td>
1046 <td class="dehead" width="65"><?php echo xlt('Encounter'); ?></td>
1047 <td align="center" class="dehead" id="td_head_total_charge" width="80"><?php echo xlt('Total Charge'); ?></td>
1048 <td align="center" class="dehead" id="td_head_rep_doc" style='display:none' width="70"><?php echo xlt('Report/ Form'); ?></td>
1049 <td align="center" class="dehead" id="td_head_description" style='display:none' width="200"><?php echo xlt('Description'); ?></td>
1050 <td align="center" class="dehead" id="td_head_insurance_payment" width="80"><?php echo xlt('Insurance Payment'); ?></td>
1051 <td align="center" class="dehead" id="td_head_patient_payment" width="80"><?php echo xlt('Patient Payment'); ?></td>
1052 <td align="center" class="dehead" id="td_head_patient_co_pay" width="55"><?php echo xlt('Co Pay Paid'); ?></td>
1053 <td align="center" class="dehead" id="td_head_co_pay" width="55"><?php echo xlt('Required Co Pay'); ?></td>
1054 <td align="center" class="dehead" id="td_head_insurance_balance" width="80"><?php echo xlt('Insurance Balance'); ?></td>
1055 <td align="center" class="dehead" id="td_head_patient_balance" width="80"><?php echo xlt('Patient Balance'); ?></td>
1056 <td align="center" class="dehead" width="50"><?php echo xlt('Paying'); ?></td>
1057 </tr>
1058 </thead>
1059 <?php
1060 $encs = array();
1062 // Get the unbilled service charges and payments by encounter for this patient.
1064 $query = "SELECT fe.encounter, b.code_type, b.code, b.modifier, b.fee, " .
1065 "LEFT(fe.date, 10) AS encdate ,fe.last_level_closed " .
1066 "FROM form_encounter AS fe left join billing AS b on " .
1067 "b.pid = ? AND b.activity = 1 AND " . //AND b.billed = 0
1068 "b.code_type != 'TAX' AND b.fee != 0 " .
1069 "AND fe.pid = b.pid AND fe.encounter = b.encounter " .
1070 "where fe.pid = ? " .
1071 "ORDER BY b.encounter";
1072 $bres = sqlStatement($query, array($pid, $pid));
1074 while ($brow = sqlFetchArray($bres)) {
1075 $key = 0 - $brow['encounter'];
1076 if (empty($encs[$key])) {
1077 $encs[$key] = array(
1078 'encounter' => $brow['encounter'],
1079 'date' => $brow['encdate'],
1080 'last_level_closed' => $brow['last_level_closed'],
1081 'charges' => 0,
1082 'payments' => 0);
1085 if ($brow['code_type'] === 'COPAY') {
1086 //$encs[$key]['payments'] -= $brow['fee'];
1087 } else {
1088 $encs[$key]['charges'] += $brow['fee'];
1089 // Add taxes.
1090 $sql_array = array();
1091 $query = "SELECT taxrates FROM codes WHERE " .
1092 "code_type = ? AND " .
1093 "code = ? AND ";
1094 array_push($sql_array, $code_types[$brow['code_type']]['id'], $brow['code']);
1095 if ($brow['modifier']) {
1096 $query .= "modifier = ?";
1097 array_push($sql_array, $brow['modifier']);
1098 } else {
1099 $query .= "(modifier IS NULL OR modifier = '')";
1102 $query .= " LIMIT 1";
1103 $trow = sqlQuery($query, $sql_array);
1104 $encs[$key]['charges'] += calcTaxes($trow, $brow['fee']);
1108 // Do the same for unbilled product sales.
1110 $query = "SELECT fe.encounter, s.drug_id, s.fee, " .
1111 "LEFT(fe.date, 10) AS encdate,fe.last_level_closed " .
1112 "FROM form_encounter AS fe left join drug_sales AS s " .
1113 "on s.pid = ? AND s.fee != 0 " . //AND s.billed = 0
1114 "AND fe.pid = s.pid AND fe.encounter = s.encounter " .
1115 "where fe.pid = ? " .
1116 "ORDER BY s.encounter";
1118 $dres = sqlStatement($query, array($pid, $pid));
1120 while ($drow = sqlFetchArray($dres)) {
1121 $key = 0 - $drow['encounter'];
1122 if (empty($encs[$key])) {
1123 $encs[$key] = array(
1124 'encounter' => $drow['encounter'],
1125 'date' => $drow['encdate'],
1126 'last_level_closed' => $drow['last_level_closed'],
1127 'charges' => 0,
1128 'payments' => 0);
1131 $encs[$key]['charges'] += $drow['fee'];
1132 // Add taxes.
1133 $trow = sqlQuery("SELECT taxrates FROM drug_templates WHERE drug_id = ? " .
1134 "ORDER BY selector LIMIT 1", array($drow['drug_id']));
1135 $encs[$key]['charges'] += calcTaxes($trow, $drow['fee']);
1138 ksort($encs, SORT_NUMERIC);
1139 $gottoday = false;
1140 //Bringing on top the Today always
1141 foreach ($encs as $key => $value) {
1142 $dispdate = $value['date'];
1143 if (strcmp($dispdate, $today) == 0 && !$gottoday) {
1144 $gottoday = true;
1145 break;
1149 // If no billing was entered yet for today, then generate a line for
1150 // entering today's co-pay.
1152 if (!$gottoday) {
1153 echoLine("form_upay[0]", date("Y-m-d"), 0, 0, 0, 0 /*$duept*/);//No encounter yet defined.
1156 $gottoday = false;
1157 foreach ($encs as $key => $value) {
1158 $enc = $value['encounter'];
1159 $dispdate = $value['date'];
1160 if (strcmp($dispdate, $today) == 0 && !$gottoday) {
1161 $dispdate = date("Y-m-d");
1162 $gottoday = true;
1164 //------------------------------------------------------------------------------------
1165 $inscopay = BillingUtilities::getCopay($pid, $dispdate);
1166 $patcopay = BillingUtilities::getPatientCopay($pid, $enc);
1167 //Insurance Payment
1168 //-----------------
1169 $drow = sqlQuery(
1170 "SELECT SUM(pay_amount) AS payments, " .
1171 "SUM(adj_amount) AS adjustments FROM ar_activity WHERE " .
1172 "deleted IS NULL AND pid = ? and encounter = ? and " .
1173 "payer_type != 0 and account_code!='PCP' ",
1174 array($pid, $enc)
1176 $dpayment = $drow['payments'];
1177 $dadjustment = $drow['adjustments'];
1178 //Patient Payment
1179 //---------------
1180 $drow = sqlQuery(
1181 "SELECT SUM(pay_amount) AS payments, " .
1182 "SUM(adj_amount) AS adjustments FROM ar_activity WHERE " .
1183 "deleted IS NULL AND pid = ? and encounter = ? and " .
1184 "payer_type = 0 and account_code!='PCP' ",
1185 array($pid, $enc)
1187 $dpayment_pat = $drow['payments'];
1189 //------------------------------------------------------------------------------------
1190 //NumberOfInsurance
1191 $ResultNumberOfInsurance = sqlStatement("SELECT COUNT( DISTINCT TYPE ) NumberOfInsurance FROM insurance_data
1192 where pid = ? and provider>0 ", array($pid));
1193 $RowNumberOfInsurance = sqlFetchArray($ResultNumberOfInsurance);
1194 $NumberOfInsurance = $RowNumberOfInsurance['NumberOfInsurance'] * 1;
1195 //------------------------------------------------------------------------------------
1196 $duept = 0;
1197 if ((($NumberOfInsurance == 0 || $value['last_level_closed'] == 4 || $NumberOfInsurance == $value['last_level_closed']))) {//Patient balance
1198 $brow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
1199 "pid = ? and encounter = ? AND activity = 1", array($pid, $enc));
1200 $srow = sqlQuery("SELECT SUM(fee) AS amount FROM drug_sales WHERE " .
1201 "pid = ? and encounter = ? ", array($pid, $enc));
1202 $drow = sqlQuery("SELECT SUM(pay_amount) AS payments, " .
1203 "SUM(adj_amount) AS adjustments FROM ar_activity WHERE " .
1204 "deleted IS NULL AND pid = ? and encounter = ? ", array($pid, $enc));
1205 $duept = $brow['amount'] + $srow['amount'] - $drow['payments'] - $drow['adjustments'];
1208 echoLine(
1209 "form_upay[$enc]",
1210 $dispdate,
1211 $value['charges'],
1212 $dpayment_pat,
1213 ($dpayment + $dadjustment),
1214 $duept,
1215 $enc,
1216 $inscopay,
1217 $patcopay
1222 // Continue with display of the data entry form.
1225 <tr bgcolor="#CCCCCC">
1226 <td class="dehead" id='td_total_1'></td>
1227 <td class="dehead" id='td_total_2'></td>
1228 <td class="dehead" id='td_total_3'></td>
1229 <td class="dehead" id='td_total_4'></td>
1230 <td class="dehead" id='td_total_5'></td>
1231 <td class="dehead" id='td_total_6'></td>
1232 <td class="dehead" id='td_total_7'></td>
1233 <td class="dehead" id='td_total_8'></td>
1234 <td align="right" class="dehead"><?php echo xlt('Total');?></td>
1235 <td align="right" class="dehead"><input name='form_paytotal' readonly style='color:#00aa00;width:50px' type='text' value=''></td>
1236 </tr>
1237 </table>
1238 </div>
1239 </fieldset>
1240 <div class="form-group">
1241 <div class="col-sm-12 text-left position-override">
1242 <div class="btn-group btn-group-pinch" role="group">
1243 <button type='submit' class="btn btn-secondary btn-save" name='form_save' value='<?php echo xla('Generate Invoice');?>'><?php echo xlt('Generate Invoice');?></button>
1244 <button type='button' class="btn btn-link btn-cancel btn-separate-left" value='<?php echo xla('Cancel'); ?>' onclick='closeHow(event)'><?php echo xlt('Cancel'); ?></button>
1245 <input type="hidden" name="hidden_patient_code" id="hidden_patient_code" value="<?php echo attr($pid);?>"/>
1246 <input type='hidden' name='ajax_mode' id='ajax_mode' value='' />
1247 <input type='hidden' name='mode' id='mode' value='' />
1248 </div>
1249 </div>
1250 </div>
1251 </form>
1252 </div>
1253 </div>
1254 <script>
1255 calctotal();
1256 </script>
1257 </div><!--end of container div of accept payment i.e the form-->
1258 <?php
1259 $oemr_ui->oeBelowContainerDiv();
1260 } // forms else close
1262 </body>
1263 </html>