2 // Copyright (C) 2009 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("../../library/patient.inc");
11 require_once("../../library/acl.inc");
13 function formatcyp($amount) {
14 if ($amount) return sprintf("%.2f", $amount);
18 function display_desc($desc) {
19 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
25 function thisLineItem($patient_id, $encounter_id, $description, $transdate, $qty, $cypfactor) {
26 global $product, $productcyp, $producttotal, $productqty, $grandtotal, $grandqty;
28 $invnumber = "$patient_id.$encounter_id";
29 $rowcyp = sprintf('%01.2f', $cypfactor);
30 $rowresult = sprintf('%01.2f', $rowcyp * $qty);
32 $rowproduct = $description;
33 if (! $rowproduct) $rowproduct = 'Unknown';
35 if ($product != $rowproduct) {
37 // Print product total.
38 if ($_POST['form_csvexport']) {
39 if (! $_POST['form_details']) {
40 echo '"' . display_desc($product) . '",';
41 echo '"' . $productqty . '",';
42 echo '"' . formatcyp($productcyp) . '",';
43 echo '"' . formatcyp($producttotal) . '"' . "\n";
49 <tr bgcolor
="#ddddff">
50 <td
class="detail" colspan
="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
51 <?php
if ($_POST['form_details']) echo xl('Total for '); echo display_desc($product) ?
>
53 <td
class="dehead" align
="right">
54 <?php
echo $productqty; ?
>
56 <td
class="dehead" align
="right">
57 <?php
echo formatcyp($productcyp); ?
>
59 <td
class="dehead" align
="right">
60 <?php
echo formatcyp($producttotal); ?
>
64 } // End not csv export
68 $product = $rowproduct;
69 $productleft = $product;
70 $productcyp = $rowcyp;
73 if ($_POST['form_details']) {
74 if ($_POST['form_csvexport']) {
75 echo '"' . display_desc($product ) . '",';
76 echo '"' . display_desc($transdate) . '",';
77 echo '"' . display_desc($invnumber) . '",';
78 echo '"' . display_desc($qty ) . '",';
79 echo '"' . formatcyp($rowcyp) . '",';
80 echo '"' . formatcyp($rowresult) . '"' . "\n";
87 <?php
echo display_desc($productleft); $productleft = " "; ?
>
90 <?php
echo $transdate; ?
>
93 <?php
echo $invnumber; ?
>
95 <td
class="dehead" align
="right">
98 <td
class="dehead" align
="right">
99 <?php
echo formatcyp($rowcyp); ?
>
101 <td
class="dehead" align
="right">
102 <?php
echo formatcyp($rowresult); ?
>
106 } // End not csv export
108 $producttotal +
= $rowresult;
109 $grandtotal +
= $rowresult;
114 if (! acl_check('acct', 'rep')) die(xl("Unauthorized access."));
116 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
117 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
118 $form_facility = $_POST['form_facility'];
120 if ($_POST['form_csvexport']) {
121 header("Pragma: public");
122 header("Expires: 0");
123 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
124 header("Content-Type: application/force-download");
125 header("Content-Disposition: attachment; filename=ippf_cyp_report.csv");
126 header("Content-Description: File Transfer");
128 if ($_POST['form_details']) {
134 echo '"Result"' . "\n";
140 echo '"Result"' . "\n";
147 <?php
html_header_show();?
>
148 <title
><?php
xl('CYP Report','e') ?
></title
>
151 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0'>
154 <h2
><?php
xl('CYP Report','e')?
></h2
>
156 <form method
='post' action
='ippf_cyp_report.php'>
158 <table border
='0' cellpadding
='3'>
163 // Build a drop-down list of facilities.
165 $query = "SELECT id, name FROM facility ORDER BY name";
166 $fres = sqlStatement($query);
167 echo " <select name='form_facility'>\n";
168 echo " <option value=''>-- All Facilities --\n";
169 while ($frow = sqlFetchArray($fres)) {
170 $facid = $frow['id'];
171 echo " <option value='$facid'";
172 if ($facid == $form_facility) echo " selected";
173 echo ">" . $frow['name'] . "\n";
177  
;<?
xl('From:','e')?
>
178 <input type
='text' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo $form_from_date ?>'
179 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
180 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
181 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
182 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
184 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo $form_to_date ?>'
185 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
186 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
187 id
='img_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
188 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
190 <input type
='checkbox' name
='form_details' value
='1'<?php
if ($_POST['form_details']) echo " checked"; ?
>><?php
xl('Details','e') ?
>
192 <input type
='submit' name
='form_refresh' value
="<?php xl('Refresh','e') ?>">
194 <input type
='submit' name
='form_csvexport' value
="<?php xl('Export to CSV','e') ?>">
196 <input type
='button' value
='<?php xl('Print','e
'); ?>' onclick
='window.print()' />
207 <table border
='0' cellpadding
='1' cellspacing
='2' width
='98%'>
209 <tr bgcolor
="#dddddd">
213 <?php
if ($_POST['form_details']) { ?
>
218 <?
xl('Invoice','e')?
>
221 <td
class="dehead" align
="right">
224 <td
class="dehead" align
="right">
227 <td
class="dehead" align
="right">
234 // If generating a report.
236 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
237 $from_date = $form_from_date;
238 $to_date = $form_to_date;
243 $producttotal = 0; // total of results for product
244 $grandtotal = 0; // grand total of results
248 $query = "SELECT b.pid, b.encounter, b.code_type, b.code, b.units, " .
249 "b.code_text, c.cyp_factor, fe.date, fe.facility_id " .
250 "FROM billing AS b " .
251 "JOIN codes AS c ON c.code_type = '12' AND c.code = b.code AND c.modifier = b.modifier AND c.cyp_factor > 0 " .
252 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
253 "WHERE b.code_type = 'MA' AND b.activity = 1 AND " .
254 "fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59'";
255 // If a facility was specified.
256 if ($form_facility) {
257 $query .= " AND fe.facility_id = '$form_facility'";
259 $query .= " ORDER BY b.code, fe.date, fe.id";
261 $res = sqlStatement($query);
262 while ($row = sqlFetchArray($res)) {
263 thisLineItem($row['pid'], $row['encounter'],
264 $row['code'] . ' ' . $row['code_text'],
265 substr($row['date'], 0, 10), $row['units'], $row['cyp_factor']);
268 $query = "SELECT s.sale_date, s.quantity, s.pid, s.encounter, " .
269 "d.name, d.cyp_factor, fe.date, fe.facility_id " .
270 "FROM drug_sales AS s " .
271 "JOIN drugs AS d ON d.drug_id = s.drug_id AND d.cyp_factor > 0 " .
272 "JOIN form_encounter AS fe ON " .
273 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
274 "fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59' " .
276 // If a facility was specified.
277 if ($form_facility) {
278 $query .= " AND fe.facility_id = '$form_facility'";
280 $query .= " ORDER BY d.name, fe.date, fe.id";
282 $res = sqlStatement($query);
283 while ($row = sqlFetchArray($res)) {
284 thisLineItem($row['pid'], $row['encounter'], $row['name'],
285 substr($row['date'], 0, 10), $row['quantity'], $row['cyp_factor']);
288 if ($_POST['form_csvexport']) {
289 if (! $_POST['form_details']) {
290 echo '"' . display_desc($product) . '",';
291 echo '"' . $productqty . '",';
292 echo '"' . formatcyp($productcyp) . '",';
293 echo '"' . formatcyp($producttotal) . '"' . "\n";
299 <tr bgcolor
="#ddddff">
300 <td
class="detail" colspan
="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
301 <?php
if ($_POST['form_details']) echo xl('Total for '); echo display_desc($product) ?
>
303 <td
class="dehead" align
="right">
304 <?php
echo $productqty; ?
>
306 <td
class="dehead" align
="right">
307 <?php
echo formatcyp($productcyp); ?
>
309 <td
class="dehead" align
="right">
310 <?php
echo formatcyp($producttotal); ?
>
314 <tr bgcolor
="#ffdddd">
315 <td
class="detail" colspan
="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
316 <?php
xl('Grand Total','e'); ?
>
318 <td
class="dehead" align
="right">
319 <?php
echo $grandqty; ?
>
321 <td
class="dehead" align
="right">
324 <td
class="dehead" align
="right">
325 <?php
echo formatcyp($grandtotal); ?
>
330 } // End not csv export
331 } // end report generation
333 if (! $_POST['form_csvexport']) {
341 <!-- stuff
for the popup calendar
-->
342 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
343 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
344 <script type
="text/javascript" src
="../../library/dynarch_calendar_en.js"></script
>
345 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
346 <script language
="Javascript">
347 Calendar
.setup({inputField
:"form_from_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
348 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});
353 } // End not csv export