Support for optional logging of print actions.
[openemr.git] / interface / reports / ippf_cyp_report.php
blob35320ea5ed88ebc96206f3a71afe0b2967fa5c6e
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>
152 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.9.1.min.js"></script>
153 <script language="JavaScript">
154 $(document).ready(function() {
155 var win = top.printLogSetup ? top : opener.top;
156 win.printLogSetup(document.getElementById('printbutton'));
158 </script>
160 </head>
162 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
163 <center>
165 <h2><?php xl('CYP Report','e')?></h2>
167 <form method='post' action='ippf_cyp_report.php'>
169 <table border='0' cellpadding='3'>
171 <tr>
172 <td>
173 <?php
174 // Build a drop-down list of facilities.
176 $query = "SELECT id, name FROM facility ORDER BY name";
177 $fres = sqlStatement($query);
178 echo " <select name='form_facility'>\n";
179 echo " <option value=''>-- All Facilities --\n";
180 while ($frow = sqlFetchArray($fres)) {
181 $facid = $frow['id'];
182 echo " <option value='$facid'";
183 if ($facid == $form_facility) echo " selected";
184 echo ">" . $frow['name'] . "\n";
186 echo " </select>\n";
188 &nbsp;<?xl('From:','e')?>
189 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date ?>'
190 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
191 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
192 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
193 title='<?php xl('Click here to choose a date','e'); ?>'>
194 &nbsp;To:
195 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date ?>'
196 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
197 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
198 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
199 title='<?php xl('Click here to choose a date','e'); ?>'>
200 &nbsp;
201 <input type='checkbox' name='form_details' value='1'<?php if ($_POST['form_details']) echo " checked"; ?>><?php xl('Details','e') ?>
202 &nbsp;
203 <input type='submit' name='form_refresh' value="<?php xl('Refresh','e') ?>">
204 &nbsp;
205 <input type='submit' name='form_csvexport' value="<?php xl('Export to CSV','e') ?>">
206 &nbsp;
207 <input type='button' value='<?php echo xla('Print'); ?>' id='printbutton' />
208 </td>
209 </tr>
211 <tr>
212 <td height="1">
213 </td>
214 </tr>
216 </table>
218 <table border='0' cellpadding='1' cellspacing='2' width='98%'>
220 <tr bgcolor="#dddddd">
221 <td class="dehead">
222 <?php xl('Item','e') ?>
223 </td>
224 <?php if ($_POST['form_details']) { ?>
225 <td class="dehead">
226 <?php xl('Date','e') ?>
227 </td>
228 <td class="dehead">
229 <?php xl('Invoice','e') ?>
230 </td>
231 <?php } ?>
232 <td class="dehead" align="right">
233 <?php xl('Qty','e') ?>
234 </td>
235 <td class="dehead" align="right">
236 <?php xl('CYP','e') ?>
237 </td>
238 <td class="dehead" align="right">
239 <?php xl('Result','e') ?>
240 </td>
241 </tr>
242 <?php
243 } // end not export
245 // If generating a report.
247 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
248 $from_date = $form_from_date;
249 $to_date = $form_to_date;
251 $product = "";
252 $productleft = "";
253 $productcyp = 0;
254 $producttotal = 0; // total of results for product
255 $grandtotal = 0; // grand total of results
256 $productqty = 0;
257 $grandqty = 0;
259 $query = "SELECT b.pid, b.encounter, b.code_type, b.code, b.units, " .
260 "b.code_text, c.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " .
261 "FROM billing AS b " .
262 "JOIN codes AS c ON c.code_type = '12' AND c.code = b.code AND c.modifier = b.modifier AND c.cyp_factor > 0 " .
263 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
264 "WHERE b.code_type = 'MA' AND b.activity = 1 AND " .
265 "fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59'";
266 // If a facility was specified.
267 if ($form_facility) {
268 $query .= " AND fe.facility_id = '$form_facility'";
270 $query .= " ORDER BY b.code, fe.date, fe.id";
272 $res = sqlStatement($query);
273 while ($row = sqlFetchArray($res)) {
274 thisLineItem($row['pid'], $row['encounter'],
275 $row['code'] . ' ' . $row['code_text'],
276 substr($row['date'], 0, 10), $row['units'], $row['cyp_factor'],
277 $row['invoice_refno']);
280 $query = "SELECT s.sale_date, s.quantity, s.pid, s.encounter, " .
281 "d.name, d.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " .
282 "FROM drug_sales AS s " .
283 "JOIN drugs AS d ON d.drug_id = s.drug_id AND d.cyp_factor > 0 " .
284 "JOIN form_encounter AS fe ON " .
285 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
286 "fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59' " .
287 "WHERE s.fee != 0";
288 // If a facility was specified.
289 if ($form_facility) {
290 $query .= " AND fe.facility_id = '$form_facility'";
292 $query .= " ORDER BY d.name, fe.date, fe.id";
294 $res = sqlStatement($query);
295 while ($row = sqlFetchArray($res)) {
296 thisLineItem($row['pid'], $row['encounter'], $row['name'],
297 substr($row['date'], 0, 10), $row['quantity'], $row['cyp_factor'],
298 $row['invoice_refno']);
301 if ($_POST['form_csvexport']) {
302 if (! $_POST['form_details']) {
303 echo '"' . display_desc($product) . '",';
304 echo '"' . $productqty . '",';
305 echo '"' . formatcyp($productcyp) . '",';
306 echo '"' . formatcyp($producttotal) . '"' . "\n";
309 else {
312 <tr bgcolor="#ddddff">
313 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
314 <?php if ($_POST['form_details']) echo xl('Total for '); echo display_desc($product) ?>
315 </td>
316 <td class="dehead" align="right">
317 <?php echo $productqty; ?>
318 </td>
319 <td class="dehead" align="right">
320 <?php echo formatcyp($productcyp); ?>
321 </td>
322 <td class="dehead" align="right">
323 <?php echo formatcyp($producttotal); ?>
324 </td>
325 </tr>
327 <tr bgcolor="#ffdddd">
328 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
329 <?php xl('Grand Total','e'); ?>
330 </td>
331 <td class="dehead" align="right">
332 <?php echo $grandqty; ?>
333 </td>
334 <td class="dehead" align="right">
335 &nbsp;
336 </td>
337 <td class="dehead" align="right">
338 <?php echo formatcyp($grandtotal); ?>
339 </td>
340 </tr>
342 <?php
344 } // End not csv export
345 } // end report generation
347 if (! $_POST['form_csvexport']) {
350 </table>
351 </form>
352 </center>
353 </body>
355 <!-- stuff for the popup calendar -->
356 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
357 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
358 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
359 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
360 <script language="Javascript">
361 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
362 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
363 </script>
365 </html>
366 <?php
367 } // End not csv export