Fix for deleting payments via the Front Office Receipts report.
[openemr.git] / interface / reports / ippf_cyp_report.php
blobb6394e003552fd6e709a60e70fdf987a61978818
1 <?php
2 // Copyright (C) 2009-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 require_once("../globals.php");
10 require_once("$srcdir/patient.inc");
11 require_once("$srcdir/acl.inc");
12 require_once("$srcdir/formatting.inc.php");
14 function formatcyp($amount) {
15 if ($amount) return sprintf("%.2f", $amount);
16 return '';
19 function display_desc($desc) {
20 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
21 $desc = $matches[1];
23 return $desc;
26 function thisLineItem($patient_id, $encounter_id, $description, $transdate, $qty, $cypfactor, $irnumber='') {
27 global $product, $productcyp, $producttotal, $productqty, $grandtotal, $grandqty;
29 $invnumber = empty($irnumber) ? "$patient_id.$encounter_id" : $irnumber;
30 $rowcyp = sprintf('%01.2f', $cypfactor);
31 $rowresult = sprintf('%01.2f', $rowcyp * $qty);
33 $rowproduct = $description;
34 if (! $rowproduct) $rowproduct = 'Unknown';
36 if ($product != $rowproduct) {
37 if ($product) {
38 // Print product total.
39 if ($_POST['form_csvexport']) {
40 if (! $_POST['form_details']) {
41 echo '"' . display_desc($product) . '",';
42 echo '"' . $productqty . '",';
43 echo '"' . formatcyp($productcyp) . '",';
44 echo '"' . formatcyp($producttotal) . '"' . "\n";
47 else {
50 <tr bgcolor="#ddddff">
51 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
52 <?php if ($_POST['form_details']) echo xl('Total for '); echo display_desc($product) ?>
53 </td>
54 <td class="dehead" align="right">
55 <?php echo $productqty; ?>
56 </td>
57 <td class="dehead" align="right">
58 <?php echo formatcyp($productcyp); ?>
59 </td>
60 <td class="dehead" align="right">
61 <?php echo formatcyp($producttotal); ?>
62 </td>
63 </tr>
64 <?php
65 } // End not csv export
67 $producttotal = 0;
68 $productqty = 0;
69 $product = $rowproduct;
70 $productleft = $product;
71 $productcyp = $rowcyp;
74 if ($_POST['form_details']) {
75 if ($_POST['form_csvexport']) {
76 echo '"' . display_desc($product ) . '",';
77 echo '"' . oeFormatShortDate(display_desc($transdate)) . '",';
78 echo '"' . display_desc($invnumber) . '",';
79 echo '"' . display_desc($qty ) . '",';
80 echo '"' . formatcyp($rowcyp) . '",';
81 echo '"' . formatcyp($rowresult) . '"' . "\n";
83 else {
86 <tr>
87 <td class="detail">
88 <?php echo display_desc($productleft); $productleft = "&nbsp;"; ?>
89 </td>
90 <td class="dehead">
91 <?php echo oeFormatShortDate($transdate); ?>
92 </td>
93 <td class="detail">
94 <?php echo $invnumber; ?>
95 </td>
96 <td class="dehead" align="right">
97 <?php echo $qty; ?>
98 </td>
99 <td class="dehead" align="right">
100 <?php echo formatcyp($rowcyp); ?>
101 </td>
102 <td class="dehead" align="right">
103 <?php echo formatcyp($rowresult); ?>
104 </td>
105 </tr>
106 <?php
108 } // End not csv export
109 } // end details
110 $producttotal += $rowresult;
111 $grandtotal += $rowresult;
112 $productqty += $qty;
113 $grandqty += $qty;
114 } // end function
116 if (! acl_check('acct', 'rep')) die(xl("Unauthorized access."));
118 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
119 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
120 $form_facility = $_POST['form_facility'];
122 if ($_POST['form_csvexport']) {
123 header("Pragma: public");
124 header("Expires: 0");
125 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
126 header("Content-Type: application/force-download");
127 header("Content-Disposition: attachment; filename=ippf_cyp_report.csv");
128 header("Content-Description: File Transfer");
129 // CSV headers:
130 if ($_POST['form_details']) {
131 echo '"Item",';
132 echo '"Date",';
133 echo '"Invoice",';
134 echo '"Qty",';
135 echo '"CYP",';
136 echo '"Result"' . "\n";
138 else {
139 echo '"Item",';
140 echo '"Qty",';
141 echo '"CYP",';
142 echo '"Result"' . "\n";
145 else { // not export
147 <html>
148 <head>
149 <?php html_header_show();?>
150 <title><?php xl('CYP Report','e') ?></title>
151 </head>
153 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
154 <center>
156 <h2><?php xl('CYP Report','e')?></h2>
158 <form method='post' action='ippf_cyp_report.php'>
160 <table border='0' cellpadding='3'>
162 <tr>
163 <td>
164 <?php
165 // Build a drop-down list of facilities.
167 $query = "SELECT id, name FROM facility ORDER BY name";
168 $fres = sqlStatement($query);
169 echo " <select name='form_facility'>\n";
170 echo " <option value=''>-- All Facilities --\n";
171 while ($frow = sqlFetchArray($fres)) {
172 $facid = $frow['id'];
173 echo " <option value='$facid'";
174 if ($facid == $form_facility) echo " selected";
175 echo ">" . $frow['name'] . "\n";
177 echo " </select>\n";
179 &nbsp;<?xl('From:','e')?>
180 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date ?>'
181 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
182 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
183 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
184 title='<?php xl('Click here to choose a date','e'); ?>'>
185 &nbsp;To:
186 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date ?>'
187 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
188 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
189 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
190 title='<?php xl('Click here to choose a date','e'); ?>'>
191 &nbsp;
192 <input type='checkbox' name='form_details' value='1'<?php if ($_POST['form_details']) echo " checked"; ?>><?php xl('Details','e') ?>
193 &nbsp;
194 <input type='submit' name='form_refresh' value="<?php xl('Refresh','e') ?>">
195 &nbsp;
196 <input type='submit' name='form_csvexport' value="<?php xl('Export to CSV','e') ?>">
197 &nbsp;
198 <input type='button' value='<?php xl('Print','e'); ?>' onclick='window.print()' />
199 </td>
200 </tr>
202 <tr>
203 <td height="1">
204 </td>
205 </tr>
207 </table>
209 <table border='0' cellpadding='1' cellspacing='2' width='98%'>
211 <tr bgcolor="#dddddd">
212 <td class="dehead">
213 <?php xl('Item','e') ?>
214 </td>
215 <?php if ($_POST['form_details']) { ?>
216 <td class="dehead">
217 <?php xl('Date','e') ?>
218 </td>
219 <td class="dehead">
220 <?php xl('Invoice','e') ?>
221 </td>
222 <?php } ?>
223 <td class="dehead" align="right">
224 <?php xl('Qty','e') ?>
225 </td>
226 <td class="dehead" align="right">
227 <?php xl('CYP','e') ?>
228 </td>
229 <td class="dehead" align="right">
230 <?php xl('Result','e') ?>
231 </td>
232 </tr>
233 <?php
234 } // end not export
236 // If generating a report.
238 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
239 $from_date = $form_from_date;
240 $to_date = $form_to_date;
242 $product = "";
243 $productleft = "";
244 $productcyp = 0;
245 $producttotal = 0; // total of results for product
246 $grandtotal = 0; // grand total of results
247 $productqty = 0;
248 $grandqty = 0;
250 $query = "SELECT b.pid, b.encounter, b.code_type, b.code, b.units, " .
251 "b.code_text, c.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " .
252 "FROM billing AS b " .
253 "JOIN codes AS c ON c.code_type = '12' AND c.code = b.code AND c.modifier = b.modifier AND c.cyp_factor > 0 " .
254 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
255 "WHERE b.code_type = 'MA' AND b.activity = 1 AND " .
256 "fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59'";
257 // If a facility was specified.
258 if ($form_facility) {
259 $query .= " AND fe.facility_id = '$form_facility'";
261 $query .= " ORDER BY b.code, fe.date, fe.id";
263 $res = sqlStatement($query);
264 while ($row = sqlFetchArray($res)) {
265 thisLineItem($row['pid'], $row['encounter'],
266 $row['code'] . ' ' . $row['code_text'],
267 substr($row['date'], 0, 10), $row['units'], $row['cyp_factor'],
268 $row['invoice_refno']);
271 $query = "SELECT s.sale_date, s.quantity, s.pid, s.encounter, " .
272 "d.name, d.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " .
273 "FROM drug_sales AS s " .
274 "JOIN drugs AS d ON d.drug_id = s.drug_id AND d.cyp_factor > 0 " .
275 "JOIN form_encounter AS fe ON " .
276 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
277 "fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59' " .
278 "WHERE s.fee != 0";
279 // If a facility was specified.
280 if ($form_facility) {
281 $query .= " AND fe.facility_id = '$form_facility'";
283 $query .= " ORDER BY d.name, fe.date, fe.id";
285 $res = sqlStatement($query);
286 while ($row = sqlFetchArray($res)) {
287 thisLineItem($row['pid'], $row['encounter'], $row['name'],
288 substr($row['date'], 0, 10), $row['quantity'], $row['cyp_factor'],
289 $row['invoice_refno']);
292 if ($_POST['form_csvexport']) {
293 if (! $_POST['form_details']) {
294 echo '"' . display_desc($product) . '",';
295 echo '"' . $productqty . '",';
296 echo '"' . formatcyp($productcyp) . '",';
297 echo '"' . formatcyp($producttotal) . '"' . "\n";
300 else {
303 <tr bgcolor="#ddddff">
304 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
305 <?php if ($_POST['form_details']) echo xl('Total for '); echo display_desc($product) ?>
306 </td>
307 <td class="dehead" align="right">
308 <?php echo $productqty; ?>
309 </td>
310 <td class="dehead" align="right">
311 <?php echo formatcyp($productcyp); ?>
312 </td>
313 <td class="dehead" align="right">
314 <?php echo formatcyp($producttotal); ?>
315 </td>
316 </tr>
318 <tr bgcolor="#ffdddd">
319 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
320 <?php xl('Grand Total','e'); ?>
321 </td>
322 <td class="dehead" align="right">
323 <?php echo $grandqty; ?>
324 </td>
325 <td class="dehead" align="right">
326 &nbsp;
327 </td>
328 <td class="dehead" align="right">
329 <?php echo formatcyp($grandtotal); ?>
330 </td>
331 </tr>
333 <?php
335 } // End not csv export
336 } // end report generation
338 if (! $_POST['form_csvexport']) {
341 </table>
342 </form>
343 </center>
344 </body>
346 <!-- stuff for the popup calendar -->
347 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
348 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
349 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
350 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
351 <script language="Javascript">
352 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
353 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
354 </script>
356 </html>
357 <?php
358 } // End not csv export