Merge branch 'master' of git://github.com/openemr/openemr
[openemr.git] / interface / reports / ippf_cyp_report.php
bloba137007b011df1cf423b4abae29c17f3bf701f9a
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>
107 } // End not csv export
108 } // end details
109 $producttotal += $rowresult;
110 $grandtotal += $rowresult;
111 $productqty += $qty;
112 $grandqty += $qty;
113 } // end function
115 if (! acl_check('acct', 'rep')) die(xl("Unauthorized access."));
117 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
118 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
119 $form_facility = $_POST['form_facility'];
121 if ($_POST['form_csvexport']) {
122 header("Pragma: public");
123 header("Expires: 0");
124 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
125 header("Content-Type: application/force-download");
126 header("Content-Disposition: attachment; filename=ippf_cyp_report.csv");
127 header("Content-Description: File Transfer");
128 // CSV headers:
129 if ($_POST['form_details']) {
130 echo '"Item",';
131 echo '"Date",';
132 echo '"Invoice",';
133 echo '"Qty",';
134 echo '"CYP",';
135 echo '"Result"' . "\n";
137 else {
138 echo '"Item",';
139 echo '"Qty",';
140 echo '"CYP",';
141 echo '"Result"' . "\n";
144 else { // not export
146 <html>
147 <head>
148 <?php html_header_show();?>
149 <title><?php xl('CYP Report','e') ?></title>
150 </head>
152 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
153 <center>
155 <h2><?php xl('CYP Report','e')?></h2>
157 <form method='post' action='ippf_cyp_report.php'>
159 <table border='0' cellpadding='3'>
161 <tr>
162 <td>
163 <?php
164 // Build a drop-down list of facilities.
166 $query = "SELECT id, name FROM facility ORDER BY name";
167 $fres = sqlStatement($query);
168 echo " <select name='form_facility'>\n";
169 echo " <option value=''>-- All Facilities --\n";
170 while ($frow = sqlFetchArray($fres)) {
171 $facid = $frow['id'];
172 echo " <option value='$facid'";
173 if ($facid == $form_facility) echo " selected";
174 echo ">" . $frow['name'] . "\n";
176 echo " </select>\n";
178 &nbsp;<?xl('From:','e')?>
179 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date ?>'
180 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
181 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
182 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
183 title='<?php xl('Click here to choose a date','e'); ?>'>
184 &nbsp;To:
185 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date ?>'
186 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
187 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
188 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
189 title='<?php xl('Click here to choose a date','e'); ?>'>
190 &nbsp;
191 <input type='checkbox' name='form_details' value='1'<?php if ($_POST['form_details']) echo " checked"; ?>><?php xl('Details','e') ?>
192 &nbsp;
193 <input type='submit' name='form_refresh' value="<?php xl('Refresh','e') ?>">
194 &nbsp;
195 <input type='submit' name='form_csvexport' value="<?php xl('Export to CSV','e') ?>">
196 &nbsp;
197 <input type='button' value='<?php xl('Print','e'); ?>' onclick='window.print()' />
198 </td>
199 </tr>
201 <tr>
202 <td height="1">
203 </td>
204 </tr>
206 </table>
208 <table border='0' cellpadding='1' cellspacing='2' width='98%'>
210 <tr bgcolor="#dddddd">
211 <td class="dehead">
212 <?php xl('Item','e') ?>
213 </td>
214 <?php if ($_POST['form_details']) { ?>
215 <td class="dehead">
216 <?php xl('Date','e') ?>
217 </td>
218 <td class="dehead">
219 <?php xl('Invoice','e') ?>
220 </td>
221 <?php } ?>
222 <td class="dehead" align="right">
223 <?php xl('Qty','e') ?>
224 </td>
225 <td class="dehead" align="right">
226 <?php xl('CYP','e') ?>
227 </td>
228 <td class="dehead" align="right">
229 <?php xl('Result','e') ?>
230 </td>
231 </tr>
232 <?php
233 } // end not export
235 // If generating a report.
237 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
238 $from_date = $form_from_date;
239 $to_date = $form_to_date;
241 $product = "";
242 $productleft = "";
243 $productcyp = 0;
244 $producttotal = 0; // total of results for product
245 $grandtotal = 0; // grand total of results
246 $productqty = 0;
247 $grandqty = 0;
249 $query = "SELECT b.pid, b.encounter, b.code_type, b.code, b.units, " .
250 "b.code_text, c.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " .
251 "FROM billing AS b " .
252 "JOIN codes AS c ON c.code_type = '12' AND c.code = b.code AND c.modifier = b.modifier AND c.cyp_factor > 0 " .
253 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
254 "WHERE b.code_type = 'MA' AND b.activity = 1 AND " .
255 "fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59'";
256 // If a facility was specified.
257 if ($form_facility) {
258 $query .= " AND fe.facility_id = '$form_facility'";
260 $query .= " ORDER BY b.code, fe.date, fe.id";
262 $res = sqlStatement($query);
263 while ($row = sqlFetchArray($res)) {
264 thisLineItem($row['pid'], $row['encounter'],
265 $row['code'] . ' ' . $row['code_text'],
266 substr($row['date'], 0, 10), $row['units'], $row['cyp_factor'],
267 $row['invoice_refno']);
270 $query = "SELECT s.sale_date, s.quantity, s.pid, s.encounter, " .
271 "d.name, d.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " .
272 "FROM drug_sales AS s " .
273 "JOIN drugs AS d ON d.drug_id = s.drug_id AND d.cyp_factor > 0 " .
274 "JOIN form_encounter AS fe ON " .
275 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
276 "fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59' " .
277 "WHERE s.fee != 0";
278 // If a facility was specified.
279 if ($form_facility) {
280 $query .= " AND fe.facility_id = '$form_facility'";
282 $query .= " ORDER BY d.name, fe.date, fe.id";
284 $res = sqlStatement($query);
285 while ($row = sqlFetchArray($res)) {
286 thisLineItem($row['pid'], $row['encounter'], $row['name'],
287 substr($row['date'], 0, 10), $row['quantity'], $row['cyp_factor'],
288 $row['invoice_refno']);
291 if ($_POST['form_csvexport']) {
292 if (! $_POST['form_details']) {
293 echo '"' . display_desc($product) . '",';
294 echo '"' . $productqty . '",';
295 echo '"' . formatcyp($productcyp) . '",';
296 echo '"' . formatcyp($producttotal) . '"' . "\n";
299 else {
302 <tr bgcolor="#ddddff">
303 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
304 <?php if ($_POST['form_details']) echo xl('Total for '); echo display_desc($product) ?>
305 </td>
306 <td class="dehead" align="right">
307 <?php echo $productqty; ?>
308 </td>
309 <td class="dehead" align="right">
310 <?php echo formatcyp($productcyp); ?>
311 </td>
312 <td class="dehead" align="right">
313 <?php echo formatcyp($producttotal); ?>
314 </td>
315 </tr>
317 <tr bgcolor="#ffdddd">
318 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
319 <?php xl('Grand Total','e'); ?>
320 </td>
321 <td class="dehead" align="right">
322 <?php echo $grandqty; ?>
323 </td>
324 <td class="dehead" align="right">
325 &nbsp;
326 </td>
327 <td class="dehead" align="right">
328 <?php echo formatcyp($grandtotal); ?>
329 </td>
330 </tr>
333 } // End not csv export
334 } // end report generation
336 if (! $_POST['form_csvexport']) {
339 </table>
340 </form>
341 </center>
342 </body>
344 <!-- stuff for the popup calendar -->
345 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
346 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
347 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
348 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
349 <script language="Javascript">
350 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
351 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
352 </script>
354 </html>
355 <?php
356 } // End not csv export