2 // Copyright (C) 2009-2010 Rod Roark <rod@sunsetsystems.com>
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);
19 function display_desc($desc) {
20 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
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) {
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";
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) ?
>
54 <td
class="dehead" align
="right">
55 <?php
echo $productqty; ?
>
57 <td
class="dehead" align
="right">
58 <?php
echo formatcyp($productcyp); ?
>
60 <td
class="dehead" align
="right">
61 <?php
echo formatcyp($producttotal); ?
>
65 } // End not csv export
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";
88 <?php
echo display_desc($productleft); $productleft = " "; ?
>
91 <?php
echo oeFormatShortDate($transdate); ?
>
94 <?php
echo $invnumber; ?
>
96 <td
class="dehead" align
="right">
99 <td
class="dehead" align
="right">
100 <?php
echo formatcyp($rowcyp); ?
>
102 <td
class="dehead" align
="right">
103 <?php
echo formatcyp($rowresult); ?
>
107 } // End not csv export
109 $producttotal +
= $rowresult;
110 $grandtotal +
= $rowresult;
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");
129 if ($_POST['form_details']) {
135 echo '"Result"' . "\n";
141 echo '"Result"' . "\n";
148 <?php
html_header_show();?
>
149 <title
><?php
xl('CYP Report','e') ?
></title
>
152 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0'>
155 <h2
><?php
xl('CYP Report','e')?
></h2
>
157 <form method
='post' action
='ippf_cyp_report.php'>
159 <table border
='0' cellpadding
='3'>
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";
178  
;<?
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
'); ?>'>
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
'); ?>'>
191 <input type
='checkbox' name
='form_details' value
='1'<?php
if ($_POST['form_details']) echo " checked"; ?
>><?php
xl('Details','e') ?
>
193 <input type
='submit' name
='form_refresh' value
="<?php xl('Refresh','e') ?>">
195 <input type
='submit' name
='form_csvexport' value
="<?php xl('Export to CSV','e') ?>">
197 <input type
='button' value
='<?php xl('Print','e
'); ?>' onclick
='window.print()' />
208 <table border
='0' cellpadding
='1' cellspacing
='2' width
='98%'>
210 <tr bgcolor
="#dddddd">
212 <?php
xl('Item','e') ?
>
214 <?php
if ($_POST['form_details']) { ?
>
216 <?php
xl('Date','e') ?
>
219 <?php
xl('Invoice','e') ?
>
222 <td
class="dehead" align
="right">
223 <?php
xl('Qty','e') ?
>
225 <td
class="dehead" align
="right">
226 <?php
xl('CYP','e') ?
>
228 <td
class="dehead" align
="right">
229 <?php
xl('Result','e') ?
>
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;
244 $producttotal = 0; // total of results for product
245 $grandtotal = 0; // grand total of results
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' " .
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";
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) ?
>
306 <td
class="dehead" align
="right">
307 <?php
echo $productqty; ?
>
309 <td
class="dehead" align
="right">
310 <?php
echo formatcyp($productcyp); ?
>
312 <td
class="dehead" align
="right">
313 <?php
echo formatcyp($producttotal); ?
>
317 <tr bgcolor
="#ffdddd">
318 <td
class="detail" colspan
="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
319 <?php
xl('Grand Total','e'); ?
>
321 <td
class="dehead" align
="right">
322 <?php
echo $grandqty; ?
>
324 <td
class="dehead" align
="right">
327 <td
class="dehead" align
="right">
328 <?php
echo formatcyp($grandtotal); ?
>
333 } // End not csv export
334 } // end report generation
336 if (! $_POST['form_csvexport']) {
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"});
356 } // End not csv export