Highway to PSR2
[openemr.git] / interface / reports / ippf_cyp_report.php
blob7eaaf707c3d13c224a79f804a63e531f40b267ee
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");
13 function formatcyp($amount)
15 if ($amount) {
16 return sprintf("%.2f", $amount);
19 return '';
22 function display_desc($desc)
24 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
25 $desc = $matches[1];
28 return $desc;
31 function thisLineItem($patient_id, $encounter_id, $description, $transdate, $qty, $cypfactor, $irnumber = '')
33 global $product, $productcyp, $producttotal, $productqty, $grandtotal, $grandqty;
35 $invnumber = empty($irnumber) ? "$patient_id.$encounter_id" : $irnumber;
36 $rowcyp = sprintf('%01.2f', $cypfactor);
37 $rowresult = sprintf('%01.2f', $rowcyp * $qty);
39 $rowproduct = $description;
40 if (! $rowproduct) {
41 $rowproduct = 'Unknown';
44 if ($product != $rowproduct) {
45 if ($product) {
46 // Print product total.
47 if ($_POST['form_csvexport']) {
48 if (! $_POST['form_details']) {
49 echo '"' . display_desc($product) . '",';
50 echo '"' . $productqty . '",';
51 echo '"' . formatcyp($productcyp) . '",';
52 echo '"' . formatcyp($producttotal) . '"' . "\n";
54 } else {
57 <tr bgcolor="#ddddff">
58 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
59 <?php if ($_POST['form_details']) {
60 echo xl('Total for ');
61 } echo display_desc($product) ?>
62 </td>
63 <td class="dehead" align="right">
64 <?php echo $productqty; ?>
65 </td>
66 <td class="dehead" align="right">
67 <?php echo formatcyp($productcyp); ?>
68 </td>
69 <td class="dehead" align="right">
70 <?php echo formatcyp($producttotal); ?>
71 </td>
72 </tr>
73 <?php
74 } // End not csv export
77 $producttotal = 0;
78 $productqty = 0;
79 $product = $rowproduct;
80 $productleft = $product;
81 $productcyp = $rowcyp;
84 if ($_POST['form_details']) {
85 if ($_POST['form_csvexport']) {
86 echo '"' . display_desc($product) . '",';
87 echo '"' . oeFormatShortDate(display_desc($transdate)) . '",';
88 echo '"' . display_desc($invnumber) . '",';
89 echo '"' . display_desc($qty) . '",';
90 echo '"' . formatcyp($rowcyp) . '",';
91 echo '"' . formatcyp($rowresult) . '"' . "\n";
92 } else {
95 <tr>
96 <td class="detail">
97 <?php echo display_desc($productleft);
98 $productleft = "&nbsp;"; ?>
99 </td>
100 <td class="dehead">
101 <?php echo oeFormatShortDate($transdate); ?>
102 </td>
103 <td class="detail">
104 <?php echo $invnumber; ?>
105 </td>
106 <td class="dehead" align="right">
107 <?php echo $qty; ?>
108 </td>
109 <td class="dehead" align="right">
110 <?php echo formatcyp($rowcyp); ?>
111 </td>
112 <td class="dehead" align="right">
113 <?php echo formatcyp($rowresult); ?>
114 </td>
115 </tr>
116 <?php
117 } // End not csv export
118 } // end details
119 $producttotal += $rowresult;
120 $grandtotal += $rowresult;
121 $productqty += $qty;
122 $grandqty += $qty;
123 } // end function
125 if (! acl_check('acct', 'rep')) {
126 die(xl("Unauthorized access."));
129 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
130 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
131 $form_facility = $_POST['form_facility'];
133 if ($_POST['form_csvexport']) {
134 header("Pragma: public");
135 header("Expires: 0");
136 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
137 header("Content-Type: application/force-download");
138 header("Content-Disposition: attachment; filename=ippf_cyp_report.csv");
139 header("Content-Description: File Transfer");
140 // CSV headers:
141 if ($_POST['form_details']) {
142 echo '"Item",';
143 echo '"Date",';
144 echo '"Invoice",';
145 echo '"Qty",';
146 echo '"CYP",';
147 echo '"Result"' . "\n";
148 } else {
149 echo '"Item",';
150 echo '"Qty",';
151 echo '"CYP",';
152 echo '"Result"' . "\n";
154 } else { // not export
156 <html>
157 <head>
158 <?php html_header_show();?>
159 <title><?php xl('CYP Report', 'e') ?></title>
161 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-9-1/index.js"></script>
162 <script language="JavaScript">
163 $(document).ready(function() {
164 var win = top.printLogSetup ? top : opener.top;
165 win.printLogSetup(document.getElementById('printbutton'));
167 </script>
169 </head>
171 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
172 <center>
174 <h2><?php xl('CYP Report', 'e')?></h2>
176 <form method='post' action='ippf_cyp_report.php'>
178 <table border='0' cellpadding='3'>
180 <tr>
181 <td>
182 <?php
183 // Build a drop-down list of facilities.
185 $query = "SELECT id, name FROM facility ORDER BY name";
186 $fres = sqlStatement($query);
187 echo " <select name='form_facility'>\n";
188 echo " <option value=''>-- All Facilities --\n";
189 while ($frow = sqlFetchArray($fres)) {
190 $facid = $frow['id'];
191 echo " <option value='$facid'";
192 if ($facid == $form_facility) {
193 echo " selected";
196 echo ">" . $frow['name'] . "\n";
199 echo " </select>\n";
201 &nbsp;<?xl('From:','e')?>
202 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date ?>'
203 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
204 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
205 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
206 title='<?php xl('Click here to choose a date', 'e'); ?>'>
207 &nbsp;To:
208 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date ?>'
209 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
210 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
211 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
212 title='<?php xl('Click here to choose a date', 'e'); ?>'>
213 &nbsp;
214 <input type='checkbox' name='form_details' value='1'<?php if ($_POST['form_details']) {
215 echo " checked";
216 } ?>><?php xl('Details', 'e') ?>
217 &nbsp;
218 <input type='submit' name='form_refresh' value="<?php xl('Refresh', 'e') ?>">
219 &nbsp;
220 <input type='submit' name='form_csvexport' value="<?php xl('Export to CSV', 'e') ?>">
221 &nbsp;
222 <input type='button' value='<?php echo xla('Print'); ?>' id='printbutton' />
223 </td>
224 </tr>
226 <tr>
227 <td height="1">
228 </td>
229 </tr>
231 </table>
233 <table border='0' cellpadding='1' cellspacing='2' width='98%'>
235 <tr bgcolor="#dddddd">
236 <td class="dehead">
237 <?php xl('Item', 'e') ?>
238 </td>
239 <?php if ($_POST['form_details']) { ?>
240 <td class="dehead">
241 <?php xl('Date', 'e') ?>
242 </td>
243 <td class="dehead">
244 <?php xl('Invoice', 'e') ?>
245 </td>
246 <?php } ?>
247 <td class="dehead" align="right">
248 <?php xl('Qty', 'e') ?>
249 </td>
250 <td class="dehead" align="right">
251 <?php xl('CYP', 'e') ?>
252 </td>
253 <td class="dehead" align="right">
254 <?php xl('Result', 'e') ?>
255 </td>
256 </tr>
257 <?php
258 } // end not export
260 // If generating a report.
262 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
263 $from_date = $form_from_date;
264 $to_date = $form_to_date;
266 $product = "";
267 $productleft = "";
268 $productcyp = 0;
269 $producttotal = 0; // total of results for product
270 $grandtotal = 0; // grand total of results
271 $productqty = 0;
272 $grandqty = 0;
274 $query = "SELECT b.pid, b.encounter, b.code_type, b.code, b.units, " .
275 "b.code_text, c.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " .
276 "FROM billing AS b " .
277 "JOIN codes AS c ON c.code_type = '12' AND c.code = b.code AND c.modifier = b.modifier AND c.cyp_factor > 0 " .
278 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
279 "WHERE b.code_type = 'MA' AND b.activity = 1 AND " .
280 "fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59'";
281 // If a facility was specified.
282 if ($form_facility) {
283 $query .= " AND fe.facility_id = '$form_facility'";
286 $query .= " ORDER BY b.code, fe.date, fe.id";
288 $res = sqlStatement($query);
289 while ($row = sqlFetchArray($res)) {
290 thisLineItem(
291 $row['pid'],
292 $row['encounter'],
293 $row['code'] . ' ' . $row['code_text'],
294 substr($row['date'], 0, 10),
295 $row['units'],
296 $row['cyp_factor'],
297 $row['invoice_refno']
302 $query = "SELECT s.sale_date, s.quantity, s.pid, s.encounter, " .
303 "d.name, d.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " .
304 "FROM drug_sales AS s " .
305 "JOIN drugs AS d ON d.drug_id = s.drug_id AND d.cyp_factor > 0 " .
306 "JOIN form_encounter AS fe ON " .
307 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
308 "fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59' " .
309 "WHERE s.fee != 0";
310 // If a facility was specified.
311 if ($form_facility) {
312 $query .= " AND fe.facility_id = '$form_facility'";
315 $query .= " ORDER BY d.name, fe.date, fe.id";
317 $res = sqlStatement($query);
318 while ($row = sqlFetchArray($res)) {
319 thisLineItem(
320 $row['pid'],
321 $row['encounter'],
322 $row['name'],
323 substr($row['date'], 0, 10),
324 $row['quantity'],
325 $row['cyp_factor'],
326 $row['invoice_refno']
330 if ($_POST['form_csvexport']) {
331 if (! $_POST['form_details']) {
332 echo '"' . display_desc($product) . '",';
333 echo '"' . $productqty . '",';
334 echo '"' . formatcyp($productcyp) . '",';
335 echo '"' . formatcyp($producttotal) . '"' . "\n";
337 } else {
340 <tr bgcolor="#ddddff">
341 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
342 <?php if ($_POST['form_details']) {
343 echo xl('Total for ');
344 } echo display_desc($product) ?>
345 </td>
346 <td class="dehead" align="right">
347 <?php echo $productqty; ?>
348 </td>
349 <td class="dehead" align="right">
350 <?php echo formatcyp($productcyp); ?>
351 </td>
352 <td class="dehead" align="right">
353 <?php echo formatcyp($producttotal); ?>
354 </td>
355 </tr>
357 <tr bgcolor="#ffdddd">
358 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
359 <?php xl('Grand Total', 'e'); ?>
360 </td>
361 <td class="dehead" align="right">
362 <?php echo $grandqty; ?>
363 </td>
364 <td class="dehead" align="right">
365 &nbsp;
366 </td>
367 <td class="dehead" align="right">
368 <?php echo formatcyp($grandtotal); ?>
369 </td>
370 </tr>
372 <?php
373 } // End not csv export
374 } // end report generation
376 if (! $_POST['form_csvexport']) {
379 </table>
380 </form>
381 </center>
382 </body>
384 <!-- stuff for the popup calendar -->
385 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
386 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
387 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
388 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
389 <script language="Javascript">
390 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
391 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
392 </script>
394 </html>
395 <?php
396 } // End not csv export