added gacl config files to upgrade instructions
[openemr.git] / interface / billing / indigent_patients_report.php
blob2de82569fba6d43f97f5e2737ad35ef767ae60f6
1 <?php
2 // Copyright (C) 2005, 2008 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 is the Indigent Patients Report. It displays a summary of
10 // encounters within the specified time period for patients without
11 // insurance.
13 require_once("../globals.php");
14 require_once("../../library/patient.inc");
15 require_once("../../library/sql-ledger.inc");
17 $alertmsg = '';
19 function bucks($amount) {
20 if ($amount) return sprintf("%.2f", $amount);
21 return "";
24 $form_start_date = fixDate($_POST['form_start_date'], date("Y-01-01"));
25 $form_end_date = fixDate($_POST['form_end_date'], date("Y-m-d"));
27 $INTEGRATED_AR = $GLOBALS['oer_config']['ws_accounting']['enabled'] === 2;
29 if (!$INTEGRATED_AR) SLConnect();
31 <html>
32 <head>
33 <? html_header_show();?>
34 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
35 <title><?php xl('Indigent Patients Report','e')?></title>
37 <script language="JavaScript">
39 </script>
41 </head>
43 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
44 <center>
46 <h3>Indigent Patients Report</h3>
48 <form method='post' action='indigent_patients_report.php'>
50 <table border='0' cellpadding='5' cellspacing='0'>
52 <tr>
53 <td height="1" colspan="10">
54 </td>
55 </tr>
57 <tr bgcolor='#ddddff'>
58 <td>
59 <?php xl('Start Date:','e')?>
60 </td>
61 <td>
62 <input type='text' name='form_start_date' size='10' value='<?php echo $form_start_date ?>'
63 title='<?php xl("Beginning date of service yyyy-mm-dd","e")?>'>
64 </td>
65 <td>
66 <?php xl('End Date:','e')?>
67 </td>
68 <td>
69 <input type='text' name='form_end_date' size='10' value='<?php echo $form_end_date ?>'
70 title='<?php xl("Ending date of service yyyy-mm-dd","e")?>'>
71 </td>
72 <td>
73 <input type='submit' name='form_search' value='<?php xl("Search","e")?>'>
74 </td>
75 </tr>
77 <tr>
78 <td height="1" colspan="10">
79 </td>
80 </tr>
82 </table>
84 <table border='0' cellpadding='1' cellspacing='2' width='98%'>
86 <tr bgcolor="#dddddd">
87 <td class="dehead">
88 &nbsp;<?php xl('Patient','e')?>
89 </td>
90 <td class="dehead">
91 &nbsp;<?php xl('SSN','e')?>
92 </td>
93 <td class="dehead">
94 &nbsp;<?php xl('Invoice','e')?>
95 </td>
96 <td class="dehead">
97 &nbsp;<?php xl('Svc Date','e')?>
98 </td>
99 <td class="dehead">
100 &nbsp;<?php xl('Due Date','e')?>
101 </td>
102 <td class="dehead" align="right">
103 <?php xl('Amount','e')?>&nbsp;
104 </td>
105 <td class="dehead" align="right">
106 <?php xl('Paid','e')?>&nbsp;
107 </td>
108 <td class="dehead" align="right">
109 <?php xl('Balance','e')?>&nbsp;
110 </td>
111 </tr>
112 <?php
113 if ($_POST['form_search']) {
115 $where = "";
117 if ($form_start_date) {
118 $where .= " AND e.date >= '$form_start_date'";
120 if ($form_end_date) {
121 $where .= " AND e.date <= '$form_end_date'";
124 $rez = sqlStatement("SELECT " .
125 "e.date, e.encounter, p.pid, p.lname, p.fname, p.mname, p.ss " .
126 "FROM form_encounter AS e, patient_data AS p, insurance_data AS i " .
127 "WHERE p.pid = e.pid AND i.pid = e.pid AND i.type = 'primary' " .
128 "AND i.provider = ''$where " .
129 "ORDER BY p.lname, p.fname, p.mname, p.pid, e.date"
132 $total_amount = 0;
133 $total_paid = 0;
135 for ($irow = 0; $row = sqlFetchArray($rez); ++$irow) {
136 $patient_id = $row['pid'];
137 $encounter_id = $row['encounter'];
138 $invnumber = $row['pid'] . "." . $row['encounter'];
140 if ($INTEGRATED_AR) {
141 $inv_duedate = '';
142 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM drug_sales WHERE " .
143 "pid = '$patient_id' AND encounter = '$encounter_id'");
144 $inv_amount = $arow['amount'];
145 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
146 "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
147 "activity = 1 AND code_type != 'COPAY'");
148 $inv_amount += $arow['amount'];
149 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
150 "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
151 "activity = 1 AND code_type = 'COPAY'");
152 $inv_paid = 0 - $arow['amount'];
153 $arow = sqlQuery("SELECT SUM(pay_amount) AS pay, " .
154 "sum(adj_amount) AS adj FROM ar_activity WHERE " .
155 "pid = '$patient_id' AND encounter = '$encounter_id'");
156 $inv_paid += $arow['pay'];
157 $inv_amount -= $arow['adj'];
159 else {
160 $ares = SLQuery("SELECT duedate, amount, paid FROM ar WHERE " .
161 "ar.invnumber = '$invnumber'");
162 if ($sl_err) die($sl_err);
163 if (SLRowCount($ares) == 0) continue;
164 $arow = SLGetRow($ares, 0);
165 $inv_amount = $arow['amount'];
166 $inv_paid = $arow['paid'];
167 $inv_duedate = $arow['duedate'];
169 $total_amount += bucks($inv_amount);
170 $total_paid += bucks($inv_paid);
172 $bgcolor = (($irow & 1) ? "#ffdddd" : "#ddddff");
174 <tr bgcolor='<?php echo $bgcolor ?>'>
175 <td class="detail">
176 &nbsp;<?php echo $row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname'] ?>
177 </td>
178 <td class="detail">
179 &nbsp;<?php echo $row['ss'] ?>
180 </td>
181 <td class="detail">
182 &nbsp;<?php echo $invnumber ?></a>
183 </td>
184 <td class="detail">
185 &nbsp;<?php echo substr($row['date'], 0, 10) ?>
186 </td>
187 <td class="detail">
188 &nbsp;<?php echo $inv_duedate ?>
189 </td>
190 <td class="detail" align="right">
191 <?php echo bucks($inv_amount) ?>&nbsp;
192 </td>
193 <td class="detail" align="right">
194 <?php echo bucks($inv_paid) ?>&nbsp;
195 </td>
196 <td class="detail" align="right">
197 <?php echo bucks($inv_amount - $inv_paid) ?>&nbsp;
198 </td>
199 </tr>
200 <?php
203 <tr bgcolor='#dddddd'>
204 <td class="detail">
205 &nbsp;Totals
206 </td>
207 <td class="detail">
208 &nbsp;
209 </td>
210 <td class="detail">
211 &nbsp;
212 </td>
213 <td class="detail">
214 &nbsp;
215 </td>
216 <td class="detail">
217 &nbsp;
218 </td>
219 <td class="detail" align="right">
220 <?php echo bucks($total_amount) ?>&nbsp;
221 </td>
222 <td class="detail" align="right">
223 <?php echo bucks($total_paid) ?>&nbsp;
224 </td>
225 <td class="detail" align="right">
226 <?php echo bucks($total_amount - $total_paid) ?>&nbsp;
227 </td>
228 </tr>
229 <?php
231 if (!$INTEGRATED_AR) SLClose();
234 </table>
236 </form>
237 </center>
238 <script>
239 <?php
240 if ($alertmsg) {
241 echo "alert('$alertmsg');\n";
244 </script>
245 </body>
246 </html>