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); ?
>
108 } // End not csv export
110 $producttotal +
= $rowresult;
111 $grandtotal +
= $rowresult;
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");
130 if ($_POST['form_details']) {
136 echo '"Result"' . "\n";
142 echo '"Result"' . "\n";
149 <?php
html_header_show();?
>
150 <title
><?php
xl('CYP Report','e') ?
></title
>
153 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0'>
156 <h2
><?php
xl('CYP Report','e')?
></h2
>
158 <form method
='post' action
='ippf_cyp_report.php'>
160 <table border
='0' cellpadding
='3'>
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";
179  
;<?
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
'); ?>'>
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
'); ?>'>
192 <input type
='checkbox' name
='form_details' value
='1'<?php
if ($_POST['form_details']) echo " checked"; ?
>><?php
xl('Details','e') ?
>
194 <input type
='submit' name
='form_refresh' value
="<?php xl('Refresh','e') ?>">
196 <input type
='submit' name
='form_csvexport' value
="<?php xl('Export to CSV','e') ?>">
198 <input type
='button' value
='<?php xl('Print','e
'); ?>' onclick
='window.print()' />
209 <table border
='0' cellpadding
='1' cellspacing
='2' width
='98%'>
211 <tr bgcolor
="#dddddd">
213 <?php
xl('Item','e') ?
>
215 <?php
if ($_POST['form_details']) { ?
>
217 <?php
xl('Date','e') ?
>
220 <?php
xl('Invoice','e') ?
>
223 <td
class="dehead" align
="right">
224 <?php
xl('Qty','e') ?
>
226 <td
class="dehead" align
="right">
227 <?php
xl('CYP','e') ?
>
229 <td
class="dehead" align
="right">
230 <?php
xl('Result','e') ?
>
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;
245 $producttotal = 0; // total of results for product
246 $grandtotal = 0; // grand total of results
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' " .
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";
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) ?
>
307 <td
class="dehead" align
="right">
308 <?php
echo $productqty; ?
>
310 <td
class="dehead" align
="right">
311 <?php
echo formatcyp($productcyp); ?
>
313 <td
class="dehead" align
="right">
314 <?php
echo formatcyp($producttotal); ?
>
318 <tr bgcolor
="#ffdddd">
319 <td
class="detail" colspan
="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
320 <?php
xl('Grand Total','e'); ?
>
322 <td
class="dehead" align
="right">
323 <?php
echo $grandqty; ?
>
325 <td
class="dehead" align
="right">
328 <td
class="dehead" align
="right">
329 <?php
echo formatcyp($grandtotal); ?
>
335 } // End not csv export
336 } // end report generation
338 if (! $_POST['form_csvexport']) {
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"});
358 } // End not csv export