incremented patch version 5
[openemr.git] / interface / billing / sl_receipts_report.php
blobbff1b3402097a513f169141e2926f806d94925e8
1 <?php
2 // Copyright (C) 2006-2010 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This module was written for one of my clients to report on cash
10 // receipts by practitioner. It is not as complete as it should be
11 // but I wanted to make the code available to the project because
12 // many other practices have this same need. - rod@sunsetsystems.com
14 require_once("../globals.php");
15 require_once("$srcdir/patient.inc");
16 require_once("$srcdir/sql-ledger.inc");
17 require_once("$srcdir/acl.inc");
18 require_once("$srcdir/formatting.inc.php");
19 require_once "$srcdir/options.inc.php";
20 require_once "$srcdir/formdata.inc.php";
21 require_once("../../custom/code_types.inc.php");
23 // This determines if a particular procedure code corresponds to receipts
24 // for the "Clinic" column as opposed to receipts for the practitioner. Each
25 // practice will have its own policies in this regard, so you'll probably
26 // have to customize this function. If you use the "fee sheet" encounter
27 // form then the code below may work for you.
29 include_once("../forms/fee_sheet/codes.php");
30 function is_clinic($code) {
31 global $bcodes;
32 $i = strpos($code, ':');
33 if ($i) $code = substr($code, 0, $i);
34 return ($bcodes['CPT4'][xl('Lab')][$code] ||
35 $bcodes['CPT4'][xl('Immunizations')][$code] ||
36 $bcodes['HCPCS'][xl('Therapeutic Injections')][$code]);
39 function bucks($amount) {
40 if ($amount) echo oeFormatMoney($amount);
43 if (! acl_check('acct', 'rep')) die(xl("Unauthorized access."));
45 $INTEGRATED_AR = $GLOBALS['oer_config']['ws_accounting']['enabled'] === 2;
47 if (!$INTEGRATED_AR) {
48 SLConnect();
49 $chart_id_cash = SLQueryValue("select id from chart where accno = '$sl_cash_acc'");
50 if ($sl_err) die($sl_err);
53 $form_use_edate = $_POST['form_use_edate'];
55 $form_proc_codefull = trim($_POST['form_proc_codefull']);
56 // Parse the code type and the code from <code_type>:<code>
57 $tmp_code_array = explode(':',$form_proc_codefull);
58 $form_proc_codetype = $tmp_code_array[0];
59 $form_proc_code = $tmp_code_array[1];
61 $form_dx_codefull = trim($_POST['form_dx_codefull']);
62 // Parse the code type and the code from <code_type>:<code>
63 $tmp_code_array = explode(':',$form_dx_codefull);
64 $form_dx_codetype = $tmp_code_array[0];
65 $form_dx_code = $tmp_code_array[1];
67 $form_procedures = empty($_POST['form_procedures']) ? 0 : 1;
68 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-01'));
69 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
70 $form_facility = $_POST['form_facility'];
72 <html>
73 <head>
74 <?php if (function_exists('html_header_show')) html_header_show(); ?>
75 <style type="text/css">
76 /* specifically include & exclude from printing */
77 @media print {
78 #report_parameters {
79 visibility: hidden;
80 display: none;
82 #report_parameters_daterange {
83 visibility: visible;
84 display: inline;
86 #report_results {
87 margin-top: 30px;
91 /* specifically exclude some from the screen */
92 @media screen { N
93 #report_parameters_daterange {
94 visibility: hidden;
95 display: none;
98 </style>
100 <script type="text/javascript" src="../../library/dialog.js"></script>
101 <script language="JavaScript">
102 // This is for callback by the find-code popup.
103 // Erases the current entry
104 // The target element is set by the find-code popup
105 // (this allows use of this in multiple form elements on the same page)
106 function set_related_target(codetype, code, selector, codedesc, target_element) {
107 var f = document.forms[0];
108 var s = f[target_element].value;
109 if (code) {
110 s = codetype + ':' + code;
111 } else {
112 s = '';
114 f[target_element].value = s;
117 // This invokes the find-code (procedure/service codes) popup.
118 function sel_procedure() {
119 dlgopen('../patient_file/encounter/find_code_popup.php?target_element=form_proc_codefull&codetype=<?php echo attr(collect_codetypes("procedure","csv")) ?>', '_blank', 500, 400);
122 // This invokes the find-code (diagnosis codes) popup.
123 function sel_diagnosis() {
124 dlgopen('../patient_file/encounter/find_code_popup.php?target_element=form_dx_codefull&codetype=<?php echo attr(collect_codetypes("diagnosis","csv")) ?>', '_blank', 500, 400);
127 </script>
129 <title><?xl('Cash Receipts by Provider','e')?></title>
130 </head>
132 <body class="body_top">
134 <span class='title'><?php xl('Report','e'); ?> - <?php xl('Cash Receipts by Provider','e'); ?></span>
136 <form method='post' action='sl_receipts_report.php' id='theform'>
138 <div id="report_parameters">
140 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
142 <table>
143 <tr>
144 <td width='660px'>
145 <div style='float:left'>
147 <table class='text'>
148 <tr>
149 <td class='label'>
150 <?php xl('Facility','e'); ?>:
151 </td>
152 <td>
153 <?php dropdown_facility(strip_escape_custom($form_facility), 'form_facility'); ?>
154 </td>
155 <td class='label'>
156 <?php xl('Provider','e'); ?>:
157 </td>
158 <td>
159 <?php
160 if (acl_check('acct', 'rep_a')) {
161 // Build a drop-down list of providers.
163 $query = "select id, lname, fname from users where " .
164 "authorized = 1 order by lname, fname";
165 $res = sqlStatement($query);
166 echo " &nbsp;<select name='form_doctor'>\n";
167 echo " <option value=''>-- " . xl('All Providers', 'e') . " --\n";
168 while ($row = sqlFetchArray($res)) {
169 $provid = $row['id'];
170 echo " <option value='$provid'";
171 if ($provid == $_POST['form_doctor']) echo " selected";
172 echo ">" . $row['lname'] . ", " . $row['fname'] . "\n";
174 echo " </select>\n";
175 } else {
176 echo "<input type='hidden' name='form_doctor' value='" . $_SESSION['authUserID'] . "'>";
179 </td>
180 <td>
181 <select name='form_use_edate'>
182 <option value='0'><?php xl('Payment Date','e'); ?></option>
183 <option value='1'<?php if ($form_use_edate) echo ' selected' ?>><?php xl('Invoice Date','e'); ?></option>
184 </select>
185 </td>
186 </tr>
187 <tr>
188 <td class='label'>
189 <?php xl('From','e'); ?>:
190 </td>
191 <td>
192 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date; ?>'
193 title='Date of appointments mm/dd/yyyy' >
194 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
195 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
196 title='<?php xl('Click here to choose a date','e'); ?>'>
197 </td>
198 <td class='label'>
199 <?php xl('To','e'); ?>:
200 </td>
201 <td>
202 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date; ?>'
203 title='Optional end date mm/dd/yyyy' >
204 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
205 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
206 title='<?php xl('Click here to choose a date','e'); ?>'>
207 </td>
208 <td>&nbsp;</td>
209 </tr>
210 <tr>
211 <td>
212 <?php if (!$GLOBALS['simplified_demographics']) echo '&nbsp;' . xl('Procedure/Service', 'e') . ':'; ?>
213 </td>
214 <td>
215 <input type='text' name='form_proc_codefull' size='11' value='<?php echo $form_proc_codefull; ?>' onclick='sel_procedure()'
216 title='<?php xl('Optional procedure/service code','e'); ?>'
217 <?php if ($GLOBALS['simplified_demographics']) echo "style='display:none'"; ?>>
218 </td>
220 <td>
221 <?php if (!$GLOBALS['simplified_demographics']) echo '&nbsp;' . xl('Diagnosis', 'e') . ':'; ?>
222 </td>
223 <td>
224 <input type='text' name='form_dx_codefull' size='11' value='<?php echo $form_dx_codefull; ?>' onclick='sel_diagnosis()'
225 title='<?php xl('Enter a diagnosis code to exclude all invoices not containing it','e'); ?>'
226 <?php if ($GLOBALS['simplified_demographics']) echo "style='display:none'"; ?>>
227 </td>
229 <td>
230 <input type='checkbox' name='form_details' value='1'<?php if ($_POST['form_details']) echo " checked"; ?>><?xl('Details','e')?>
231 <input type='checkbox' name='form_procedures' value='1'<?php if ($form_procedures) echo " checked"; ?>><?xl('Procedures','e')?>
232 </td>
233 </tr>
234 </table>
236 </div>
238 </td>
239 <td align='left' valign='middle' height="100%">
240 <table style='border-left:1px solid; width:100%; height:100%' >
241 <tr>
242 <td>
243 <div style='margin-left:15px'>
244 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
245 <span>
246 <?php xl('Submit','e'); ?>
247 </span>
248 </a>
250 <?php if ($_POST['form_refresh']) { ?>
251 <a href='#' class='css_button' onclick='window.print()'>
252 <span>
253 <?php xl('Print','e'); ?>
254 </span>
255 </a>
256 <?php } ?>
257 </div>
258 </td>
259 </tr>
260 </table>
261 </td>
262 </tr>
263 </table>
264 </div>
266 <?php
267 if ($_POST['form_refresh']) {
269 <div id="report_results">
270 <table border='0' cellpadding='1' cellspacing='2' width='98%'>
271 <thead>
272 <th>
273 <?php xl('Practitioner','e') ?>
274 </th>
275 <th>
276 <?php xl('Date','e') ?>
277 </th>
278 <?php if ($form_procedures) { ?>
279 <th>
280 <?php xl('Invoice','e') ?>
281 </th>
282 <?php } ?>
283 <?php if ($form_proc_codefull) { ?>
284 <th align='right'>
285 <?php xl('InvAmt','e') ?>
286 </th>
287 <?php } ?>
288 <?php if ($form_proc_codefull) { ?>
289 <th>
290 <?php xl('Insurance','e') ?>
291 </th>
292 <?php } ?>
293 <?php if ($form_procedures) { ?>
294 <th>
295 <?php xl('Procedure','e') ?>
296 </th>
297 <th align="right">
298 <?php xl('Prof.','e') ?>
299 </th>
300 <th align="right">
301 <?php xl('Clinic','e') ?>
302 </th>
303 <?php } else { ?>
304 <th align="right">
305 <?php xl('Received','e') ?>
306 </th>
307 <?php } ?>
308 </thead>
309 <?php
310 if ($_POST['form_refresh']) {
311 $form_doctor = $_POST['form_doctor'];
312 $arows = array();
314 if ($INTEGRATED_AR) {
315 $ids_to_skip = array();
316 $irow = 0;
318 // Get copays. These will be ignored if a CPT code was specified.
320 if (!$form_proc_code || !$form_proc_codetype) {
321 /*************************************************************
322 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.modifier, " .
323 "fe.date, fe.id AS trans_id, u.id AS docid " .
324 "FROM billing AS b " .
325 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
326 "JOIN forms AS f ON f.pid = b.pid AND f.encounter = b.encounter AND f.formdir = 'newpatient' " .
327 "LEFT OUTER JOIN users AS u ON u.username = f.user " .
328 "WHERE b.code_type = 'COPAY' AND b.activity = 1 AND " .
329 "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59'";
330 // If a facility was specified.
331 if ($form_facility) {
332 $query .= " AND fe.facility_id = '$form_facility'";
334 // If a doctor was specified.
335 if ($form_doctor) {
336 $query .= " AND u.id = '$form_doctor'";
338 *************************************************************/
339 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.modifier, " .
340 "fe.date, fe.id AS trans_id, fe.provider_id AS docid, fe.invoice_refno " .
341 "FROM billing AS b " .
342 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
343 "WHERE b.code_type = 'COPAY' AND b.activity = 1 AND " .
344 "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59'";
345 // If a facility was specified.
346 if ($form_facility) {
347 $query .= " AND fe.facility_id = '$form_facility'";
349 // If a doctor was specified.
350 if ($form_doctor) {
351 $query .= " AND fe.provider_id = '$form_doctor'";
353 /************************************************************/
355 $res = sqlStatement($query);
356 while ($row = sqlFetchArray($res)) {
357 $trans_id = $row['trans_id'];
358 $thedate = substr($row['date'], 0, 10);
359 $patient_id = $row['pid'];
360 $encounter_id = $row['encounter'];
362 if (!empty($ids_to_skip[$trans_id])) continue;
364 // If a diagnosis code was given then skip any invoices without
365 // that diagnosis.
366 if ($form_dx_code && $form_dx_codetype) {
367 $tmp = sqlQuery("SELECT count(*) AS count FROM billing WHERE " .
368 "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
369 "code_type = '$form_dx_codetype' AND code LIKE '$form_dx_code' AND " .
370 "activity = 1");
371 if (empty($tmp['count'])) {
372 $ids_to_skip[$trans_id] = 1;
373 continue;
377 $key = sprintf("%08u%s%08u%08u%06u", $row['docid'], $thedate,
378 $patient_id, $encounter_id, ++$irow);
379 $arows[$key] = array();
380 $arows[$key]['transdate'] = $thedate;
381 $arows[$key]['amount'] = $row['fee'];
382 $arows[$key]['docid'] = $row['docid'];
383 $arows[$key]['project_id'] = 0;
384 $arows[$key]['memo'] = '';
385 $arows[$key]['invnumber'] = "$patient_id.$encounter_id";
386 $arows[$key]['irnumber'] = $row['invoice_refno'];
387 } // end while
388 } // end copays (not $form_proc_code)
390 // Get ar_activity (having payments), form_encounter, forms, users, optional ar_session
391 /***************************************************************
392 $query = "SELECT a.pid, a.encounter, a.post_time, a.code, a.modifier, a.pay_amount, " .
393 "fe.date, fe.id AS trans_id, u.id AS docid, s.deposit_date, s.payer_id " .
394 "FROM ar_activity AS a " .
395 "JOIN form_encounter AS fe ON fe.pid = a.pid AND fe.encounter = a.encounter " .
396 "JOIN forms AS f ON f.pid = a.pid AND f.encounter = a.encounter AND f.formdir = 'newpatient' " .
397 "LEFT OUTER JOIN users AS u ON u.username = f.user " .
398 "LEFT OUTER JOIN ar_session AS s ON s.session_id = a.session_id " .
399 "WHERE a.pay_amount != 0 AND ( " .
400 "a.post_time >= '$form_from_date 00:00:00' AND a.post_time <= '$form_to_date 23:59:59' " .
401 "OR fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' " .
402 "OR s.deposit_date >= '$form_from_date' AND s.deposit_date <= '$form_to_date' )";
403 // If a procedure code was specified.
404 if ($form_proc_code) $query .= " AND a.code = '$form_proc_code'";
405 // If a facility was specified.
406 if ($form_facility) $query .= " AND fe.facility_id = '$form_facility'";
407 // If a doctor was specified.
408 if ($form_doctor) $query .= " AND u.id = '$form_doctor'";
409 ***************************************************************/
410 $query = "SELECT a.pid, a.encounter, a.post_time, a.code, a.modifier, a.pay_amount, " .
411 "fe.date, fe.id AS trans_id, fe.provider_id AS docid, fe.invoice_refno, s.deposit_date, s.payer_id, " .
412 "b.provider_id " .
413 "FROM ar_activity AS a " .
414 "JOIN form_encounter AS fe ON fe.pid = a.pid AND fe.encounter = a.encounter " .
415 "LEFT OUTER JOIN ar_session AS s ON s.session_id = a.session_id " .
416 "LEFT OUTER JOIN billing AS b ON b.pid = a.pid AND b.encounter = a.encounter AND " .
417 "b.code = a.code AND b.modifier = a.modifier AND b.activity = 1 AND " .
418 "b.code_type != 'COPAY' AND b.code_type != 'TAX' " .
419 "WHERE a.pay_amount != 0 AND ( " .
420 "a.post_time >= '$form_from_date 00:00:00' AND a.post_time <= '$form_to_date 23:59:59' " .
421 "OR fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' " .
422 "OR s.deposit_date >= '$form_from_date' AND s.deposit_date <= '$form_to_date' )";
423 // If a procedure code was specified.
424 // Support code type if it is in the ar_activity table. Note it is not always included, so
425 // also support a blank code type in ar_activity table.
426 if ($form_proc_codetype && $form_proc_code) $query .= " AND (a.code_type = '$form_proc_codetype' OR a.code_type = '') AND a.code = '$form_proc_code'";
427 // If a facility was specified.
428 if ($form_facility) $query .= " AND fe.facility_id = '$form_facility'";
429 // If a doctor was specified.
430 if ($form_doctor) {
431 $query .= " AND ( b.provider_id = '$form_doctor' OR " .
432 "( ( b.provider_id IS NULL OR b.provider_id = 0 ) AND " .
433 "fe.provider_id = '$form_doctor' ) )";
435 /**************************************************************/
437 $res = sqlStatement($query);
438 while ($row = sqlFetchArray($res)) {
439 $trans_id = $row['trans_id'];
440 $patient_id = $row['pid'];
441 $encounter_id = $row['encounter'];
443 if (!empty($ids_to_skip[$trans_id])) continue;
445 if ($form_use_edate) {
446 $thedate = substr($row['date'], 0, 10);
447 } else {
448 if (!empty($row['deposit_date']))
449 $thedate = $row['deposit_date'];
450 else
451 $thedate = substr($row['post_time'], 0, 10);
453 if (strcmp($thedate, $form_from_date) < 0 || strcmp($thedate, $form_to_date) > 0) continue;
455 // If a diagnosis code was given then skip any invoices without
456 // that diagnosis.
457 if ($form_dx_code && $form_dx_codetype) {
458 $tmp = sqlQuery("SELECT count(*) AS count FROM billing WHERE " .
459 "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
460 "code_type = '$form_dx_codetype' AND code LIKE '$form_dx_code' AND " .
461 "activity = 1");
462 if (empty($tmp['count'])) {
463 $ids_to_skip[$trans_id] = 1;
464 continue;
468 $docid = empty($row['encounter_id']) ? $row['docid'] : $row['encounter_id'];
469 $key = sprintf("%08u%s%08u%08u%06u", $docid, $thedate,
470 $patient_id, $encounter_id, ++$irow);
471 $arows[$key] = array();
472 $arows[$key]['transdate'] = $thedate;
473 $arows[$key]['amount'] = 0 - $row['pay_amount'];
474 $arows[$key]['docid'] = $docid;
475 $arows[$key]['project_id'] = empty($row['payer_id']) ? 0 : $row['payer_id'];
476 $arows[$key]['memo'] = $row['code'];
477 $arows[$key]['invnumber'] = "$patient_id.$encounter_id";
478 $arows[$key]['irnumber'] = $row['invoice_refno'];
479 } // end while
480 } // end $INTEGRATED_AR
482 else {
483 if ($form_proc_code) {
484 $query = "SELECT acc_trans.amount, acc_trans.transdate, " .
485 "acc_trans.memo, acc_trans.project_id, acc_trans.trans_id, " .
486 "ar.invnumber, ar.employee_id, invoice.sellprice, invoice.qty " .
487 "FROM acc_trans, ar, invoice WHERE " .
488 "acc_trans.chart_id = $chart_id_cash AND " .
489 "acc_trans.memo ILIKE '$form_proc_code' AND " .
490 "ar.id = acc_trans.trans_id AND " .
491 "invoice.trans_id = acc_trans.trans_id AND " .
492 "invoice.serialnumber ILIKE acc_trans.memo AND " .
493 "invoice.sellprice >= 0.00 AND " .
494 "( invoice.description ILIKE 'CPT%' OR invoice.description ILIKE 'Proc%' ) AND ";
496 else {
497 $query = "select acc_trans.amount, acc_trans.transdate, " .
498 "acc_trans.memo, acc_trans.trans_id, " .
499 "ar.invnumber, ar.employee_id from acc_trans, ar where " .
500 "acc_trans.chart_id = $chart_id_cash and " .
501 "ar.id = acc_trans.trans_id and ";
504 if ($form_use_edate) {
505 $query .= "ar.transdate >= '$form_from_date' and " .
506 "ar.transdate <= '$form_to_date'";
507 } else {
508 $query .= "acc_trans.transdate >= '$form_from_date' and " .
509 "acc_trans.transdate <= '$form_to_date'";
512 $query .= " order by ar.invnumber";
514 // echo "<!-- $query -->\n"; // debugging
516 $t_res = SLQuery($query);
517 if ($sl_err) die($sl_err);
519 $docname = "";
520 $docnameleft = "";
521 $main_docid = 0;
522 $doctotal1 = 0;
523 $grandtotal1 = 0;
524 $doctotal2 = 0;
525 $grandtotal2 = 0;
526 $last_trans_id = 0;
527 $skipping = false;
529 for ($irow = 0; $irow < SLRowCount($t_res); ++$irow) {
530 $row = SLGetRow($t_res, $irow);
532 list($patient_id, $encounter_id) = explode(".", $row['invnumber']);
534 // Under some conditions we may skip invoices that matched the SQL query.
536 if ($row['trans_id'] == $last_trans_id) {
537 if ($skipping) continue;
538 // same invoice and not skipping, do nothing.
539 } else { // new invoice
540 $skipping = false;
541 // If a diagnosis code was given then skip any invoices without
542 // that diagnosis.
543 if ($form_dx_code) {
544 if (!SLQueryValue("SELECT count(*) FROM invoice WHERE " .
545 "invoice.trans_id = '" . $row['trans_id'] . "' AND " .
546 "( invoice.description ILIKE 'ICD9:$form_dx_code %' OR " .
547 "invoice.serialnumber ILIKE 'ICD9:$form_dx_code' )"))
549 $skipping = true;
550 continue;
553 // If a facility was specified then skip invoices whose encounters
554 // do not indicate that facility.
555 if ($form_facility) {
556 $tmp = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " .
557 "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
558 "facility_id = '$form_facility'");
559 if (empty($tmp['count'])) {
560 $skipping = true;
561 continue;
564 // Find out who the practitioner is.
565 /***********************************************************
566 $tmp = sqlQuery("SELECT users.id, users.authorized FROM forms, users WHERE " .
567 "forms.pid = '$patient_id' AND forms.encounter = '$encounter_id' AND " .
568 "forms.formdir = 'newpatient' AND users.username = forms.user");
569 $main_docid = empty($tmp['id']) ? 0 : $tmp['id'];
570 if (empty($tmp['authorized'])) {
571 $tmp = sqlQuery("SELECT users.id FROM billing, users WHERE " .
572 "billing.pid = '$patient_id' AND billing.encounter = '$encounter_id' AND " .
573 "billing.activity = 1 AND billing.fee > 0 AND " .
574 "users.id = billing.provider_id AND users.authorized = 1 " .
575 "ORDER BY billing.fee DESC, billing.id ASC LIMIT 1");
576 if (!empty($tmp['id'])) $main_docid = $tmp['id'];
578 ***********************************************************/
579 $tmp = sqlQuery("SELECT provider_id FROM form_encounter WHERE " .
580 "pid = '$patient_id' AND encounter = '$encounter_id' " .
581 "ORDER BY id DESC LIMIT 1");
582 $main_docid = $tmp['provider_id'] + 0;
584 // If a practitioner was specified then skip other practitioners.
585 if ($form_doctor) {
586 if ($form_doctor != $main_docid) {
587 $skipping = true;
588 continue;
591 } // end new invoice
593 $row['docid'] = $main_docid;
594 $key = sprintf("%08u%s%08u%08u%06u", $main_docid, $row['transdate'],
595 $patient_id, $encounter_id, $irow);
596 $arows[$key] = $row;
599 } // end not $INTEGRATED_AR
601 ksort($arows);
602 $docid = 0;
604 foreach ($arows as $row) {
606 // Get insurance company name
607 $insconame = '';
608 if ($form_proc_codefull && $row['project_id']) {
609 $tmp = sqlQuery("SELECT name FROM insurance_companies WHERE " .
610 "id = '" . $row['project_id'] . "'");
611 $insconame = $tmp['name'];
614 $amount1 = 0;
615 $amount2 = 0;
616 if ($form_procedures && is_clinic($row['memo']))
617 $amount2 -= $row['amount'];
618 else
619 $amount1 -= $row['amount'];
621 // if ($docid != $row['employee_id']) {
622 if ($docid != $row['docid']) {
623 if ($docid) {
624 // Print doc totals.
627 <tr bgcolor="#ddddff">
628 <td class="detail" colspan="<?php echo ($form_proc_codefull ? 4 : 2) + ($form_procedures ? 2 : 0); ?>">
629 <?php echo xl('Totals for ') . $docname ?>
630 </td>
631 <td align="right">
632 <?php bucks($doctotal1) ?>
633 </td>
634 <?php if ($form_procedures) { ?>
635 <td align="right">
636 <?php bucks($doctotal2) ?>
637 </td>
638 <?php } ?>
639 </tr>
640 <?php
642 $doctotal1 = 0;
643 $doctotal2 = 0;
645 $docid = $row['docid'];
646 $tmp = sqlQuery("SELECT lname, fname FROM users WHERE id = '$docid'");
647 $docname = empty($tmp) ? 'Unknown' : $tmp['fname'] . ' ' . $tmp['lname'];
649 $docnameleft = $docname;
652 if ($_POST['form_details']) {
655 <tr>
656 <td class="detail">
657 <?php echo $docnameleft; $docnameleft = "&nbsp;" ?>
658 </td>
659 <td class="detail">
660 <?php echo oeFormatShortDate($row['transdate']) ?>
661 </td>
662 <?php if ($form_procedures) { ?>
663 <td class="detail">
664 <?php echo empty($row['irnumber']) ? $row['invnumber'] : $row['irnumber']; ?>
665 </td>
666 <?php } ?>
667 <?php
668 if ($form_proc_code && $form_proc_codetype) {
669 echo " <td class='detail' align='right'>";
670 if ($INTEGRATED_AR) {
671 list($patient_id, $encounter_id) = explode(".", $row['invnumber']);
672 $tmp = sqlQuery("SELECT SUM(fee) AS sum FROM billing WHERE " .
673 "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
674 "code_type = '$form_proc_codetype' AND code = '$form_proc_code' AND activity = 1");
675 bucks($tmp['sum']);
677 else {
678 bucks($row['sellprice'] * $row['qty']);
680 echo " </td>\n";
683 <?php if ($form_proc_codefull) { ?>
684 <td class="detail">
685 <?php echo $insconame ?>
686 </td>
687 <?php } ?>
688 <?php if ($form_procedures) { ?>
689 <td class="detail">
690 <?php echo $row['memo'] ?>
691 </td>
692 <?php } ?>
693 <td class="detail" align="right">
694 <?php bucks($amount1) ?>
695 </td>
696 <?php if ($form_procedures) { ?>
697 <td class="detail" align="right">
698 <?php bucks($amount2) ?>
699 </td>
700 <?php } ?>
701 </tr>
702 <?php
703 } // end details
704 $doctotal1 += $amount1;
705 $doctotal2 += $amount2;
706 $grandtotal1 += $amount1;
707 $grandtotal2 += $amount2;
711 <tr bgcolor="#ddddff">
712 <td class="detail" colspan="<?php echo ($form_proc_codefull ? 4 : 2) + ($form_procedures ? 2 : 0); ?>">
713 <?php echo xl('Totals for ') . $docname ?>
714 </td>
715 <td align="right">
716 <?php bucks($doctotal1) ?>
717 </td>
718 <?php if ($form_procedures) { ?>
719 <td align="right">
720 <?php bucks($doctotal2) ?>
721 </td>
722 <?php } ?>
723 </tr>
725 <tr bgcolor="#ffdddd">
726 <td class="detail" colspan="<?php echo ($form_proc_codefull ? 4 : 2) + ($form_procedures ? 2 : 0); ?>">
727 <?php xl('Grand Totals','e') ?>
728 </td>
729 <td align="right">
730 <?php bucks($grandtotal1) ?>
731 </td>
732 <?php if ($form_procedures) { ?>
733 <td align="right">
734 <?php bucks($grandtotal2) ?>
735 </td>
736 <?php } ?>
737 </tr>
739 <?php
741 if (!$INTEGRATED_AR) SLClose();
744 </table>
745 </div>
746 <?php } else { ?>
747 <div class='text'>
748 <?php echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?>
749 </div>
750 <?php } ?>
752 </form>
753 </body>
755 <!-- stuff for the popup calendar -->
756 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
757 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
758 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
759 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
760 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
761 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
763 <script language="Javascript">
764 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
765 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
766 </script>
768 </html>