7 * @link http://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2009-2010 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2017-2019 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once("../globals.php");
16 require_once("$srcdir/patient.inc");
18 use OpenEMR\Common\Acl\AclMain
;
19 use OpenEMR\Common\Csrf\CsrfUtils
;
20 use OpenEMR\Core\Header
;
23 if (!CsrfUtils
::verifyCsrfToken($_POST["csrf_token_form"])) {
24 CsrfUtils
::csrfNotVerified();
28 function formatcyp($amount)
31 return sprintf("%.2f", $amount);
37 function display_desc($desc)
39 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
46 function thisLineItem($patient_id, $encounter_id, $description, $transdate, $qty, $cypfactor, $irnumber = '')
48 global $product, $productcyp, $producttotal, $productqty, $grandtotal, $grandqty;
50 $invnumber = empty($irnumber) ?
"$patient_id.$encounter_id" : $irnumber;
51 $rowcyp = sprintf('%01.2f', $cypfactor);
52 $rowresult = sprintf('%01.2f', $rowcyp * $qty);
54 $rowproduct = $description;
56 $rowproduct = 'Unknown';
59 if ($product != $rowproduct) {
61 // Print product total.
62 if ($_POST['form_csvexport']) {
63 if (! $_POST['form_details']) {
64 echo csvEscape(display_desc($product)) . ',';
65 echo csvEscape($productqty) . ',';
66 echo csvEscape(formatcyp($productcyp)) . ',';
67 echo csvEscape(formatcyp($producttotal)) . "\n";
72 <tr bgcolor
="#ddddff">
73 <td
class="detail" colspan
="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
75 if ($_POST['form_details']) {
76 echo xlt('Total for ');
78 echo text(display_desc($product)); ?
>
80 <td
class="dehead" align
="right">
81 <?php
echo text($productqty); ?
>
83 <td
class="dehead" align
="right">
84 <?php
echo text(formatcyp($productcyp)); ?
>
86 <td
class="dehead" align
="right">
87 <?php
echo text(formatcyp($producttotal)); ?
>
91 } // End not csv export
96 $product = $rowproduct;
97 $productleft = $product;
98 $productcyp = $rowcyp;
101 if ($_POST['form_details']) {
102 if ($_POST['form_csvexport']) {
103 echo csvEscape(display_desc($product)) . ',';
104 echo csvEscape(oeFormatShortDate(display_desc($transdate))) . ',';
105 echo csvEscape(display_desc($invnumber)) . ',';
106 echo csvEscape(display_desc($qty)) . ',';
107 echo csvEscape(formatcyp($rowcyp)) . ',';
108 echo csvEscape(formatcyp($rowresult)) . "\n";
114 <?php
echo text(display_desc($productleft));
115 $productleft = " "; ?
>
118 <?php
echo text(oeFormatShortDate($transdate)); ?
>
121 <?php
echo text($invnumber); ?
>
123 <td
class="dehead" align
="right">
124 <?php
echo text($qty); ?
>
126 <td
class="dehead" align
="right">
127 <?php
echo text(formatcyp($rowcyp)); ?
>
129 <td
class="dehead" align
="right">
130 <?php
echo text(formatcyp($rowresult)); ?
>
134 } // End not csv export
136 $producttotal +
= $rowresult;
137 $grandtotal +
= $rowresult;
142 if (! AclMain
::aclCheckCore('acct', 'rep')) {
143 die(xlt("Unauthorized access."));
146 $form_from_date = (isset($_POST['form_from_date'])) ?
DateToYYYYMMDD($_POST['form_from_date']) : date('Y-m-d');
147 $form_to_date = (isset($_POST['form_to_date'])) ?
DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d');
148 $form_facility = $_POST['form_facility'];
150 if ($_POST['form_csvexport']) {
151 header("Pragma: public");
152 header("Expires: 0");
153 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
154 header("Content-Type: application/force-download");
155 header("Content-Disposition: attachment; filename=ippf_cyp_report.csv");
156 header("Content-Description: File Transfer");
158 if ($_POST['form_details']) {
159 echo csvEscape("Item") . ',';
160 echo csvEscape("Date") . ',';
161 echo csvEscape("Invoice") . ',';
162 echo csvEscape("Qty") . ',';
163 echo csvEscape("CYP") . ',';
164 echo csvEscape("Result") . "\n";
166 echo csvEscape("Item") . ',';
167 echo csvEscape("Qty") . ',';
168 echo csvEscape("CYP") . ',';
169 echo csvEscape("Result") . "\n";
171 } else { // not export
176 <title
><?php
echo xlt('CYP Report') ?
></title
>
178 <?php Header
::setupHeader(['datetime-picker']); ?
>
182 var win
= top
.printLogSetup ? top
: opener
.top
;
183 win
.printLogSetup(document
.getElementById('printbutton'));
185 $
('.datepicker').datetimepicker({
186 <?php
$datetimepicker_timepicker = false; ?
>
187 <?php
$datetimepicker_showseconds = false; ?
>
188 <?php
$datetimepicker_formatInput = true; ?
>
189 <?php
require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?
>
190 <?php
// can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
197 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0'>
200 <h2
><?php
echo xlt('CYP Report')?
></h2
>
202 <form method
='post' action
='ippf_cyp_report.php' onsubmit
='return top.restoreSession()'>
203 <input type
="hidden" name
="csrf_token_form" value
="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
205 <table border
='0' cellpadding
='3'>
210 // Build a drop-down list of facilities.
212 $query = "SELECT id, name FROM facility ORDER BY name";
213 $fres = sqlStatement($query);
214 echo " <select name='form_facility'>\n";
215 echo " <option value=''>-- All Facilities --\n";
216 while ($frow = sqlFetchArray($fres)) {
217 $facid = $frow['id'];
218 echo " <option value='" . attr($facid) . "'";
219 if ($facid == $form_facility) {
223 echo ">" . text($frow['name']) . "\n";
228  
;<?php
echo xlt('From')?
>:
229 <input type
='text' class='datepicker' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo attr(oeFormatShortDate($form_from_date)); ?>'>
230  
;<?php
echo xlt('To{{Range}}')?
>:
231 <input type
='text' class='datepicker' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr(oeFormatShortDate($form_to_date)); ?>'>
233 <input type
='checkbox' name
='form_details' value
='1'<?php
echo ($_POST['form_details']) ?
" checked" : ""; ?
>><?php
echo xlt('Details') ?
>
235 <input type
='submit' name
='form_refresh' value
="<?php echo xla('Refresh') ?>">
237 <input type
='submit' name
='form_csvexport' value
="<?php echo xla('Export to CSV') ?>">
239 <input type
='button' value
='<?php echo xla('Print'); ?>' id
='printbutton' />
250 <table border
='0' cellpadding
='1' cellspacing
='2' width
='98%'>
252 <tr bgcolor
="#dddddd">
254 <?php
echo xlt('Item') ?
>
256 <?php
if ($_POST['form_details']) { ?
>
258 <?php
echo xlt('Date') ?
>
261 <?php
echo xlt('Invoice') ?
>
264 <td
class="dehead" align
="right">
265 <?php
echo xlt('Qty') ?
>
267 <td
class="dehead" align
="right">
268 <?php
echo xlt('CYP') ?
>
270 <td
class="dehead" align
="right">
271 <?php
echo xlt('Result') ?
>
277 // If generating a report.
279 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
280 $from_date = $form_from_date;
281 $to_date = $form_to_date;
286 $producttotal = 0; // total of results for product
287 $grandtotal = 0; // grand total of results
291 $sqlBindArray = array();
293 $query = "SELECT b.pid, b.encounter, b.code_type, b.code, b.units, " .
294 "b.code_text, c.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " .
295 "FROM billing AS b " .
296 "JOIN codes AS c ON c.code_type = '12' AND c.code = b.code AND c.modifier = b.modifier AND c.cyp_factor > 0 " .
297 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
298 "WHERE b.code_type = 'MA' AND b.activity = 1 AND " .
299 "fe.date >= ? AND fe.date <= ?";
300 array_push($sqlBindArray, $from_date . ' 00:00:00', $to_date . ' 23:59:59');
302 // If a facility was specified.
303 if ($form_facility) {
304 $query .= " AND fe.facility_id = ?";
305 array_push($sqlBindArray, $form_facility);
308 $query .= " ORDER BY b.code, fe.date, fe.id";
310 $res = sqlStatement($query, $sqlBindArray);
311 while ($row = sqlFetchArray($res)) {
315 $row['code'] . ' ' . $row['code_text'],
316 substr($row['date'], 0, 10),
319 $row['invoice_refno']
323 $sqlBindArray = array();
325 $query = "SELECT s.sale_date, s.quantity, s.pid, s.encounter, " .
326 "d.name, d.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " .
327 "FROM drug_sales AS s " .
328 "JOIN drugs AS d ON d.drug_id = s.drug_id AND d.cyp_factor > 0 " .
329 "JOIN form_encounter AS fe ON " .
330 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
331 "fe.date >= ? AND fe.date <= ? " .
333 array_push($sqlBindArray, $from_date . ' 00:00:00', $to_date . ' 23:59:59');
335 // If a facility was specified.
336 if ($form_facility) {
337 $query .= " AND fe.facility_id = ?";
338 array_push($sqlBindArray, $form_facility);
341 $query .= " ORDER BY d.name, fe.date, fe.id";
343 $res = sqlStatement($query, $sqlBindArray);
344 while ($row = sqlFetchArray($res)) {
349 substr($row['date'], 0, 10),
352 $row['invoice_refno']
356 if ($_POST['form_csvexport']) {
357 if (! $_POST['form_details']) {
358 echo csvEscape(display_desc($product)) . ',';
359 echo csvEscape($productqty) . ',';
360 echo csvEscape(formatcyp($productcyp)) . ',';
361 echo csvEscape(formatcyp($producttotal)) . "\n";
366 <tr bgcolor
="#ddddff">
367 <td
class="detail" colspan
="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
369 if ($_POST['form_details']) {
370 echo xlt('Total for ');
372 echo text(display_desc($product)); ?
>
374 <td
class="dehead" align
="right">
375 <?php
echo text($productqty); ?
>
377 <td
class="dehead" align
="right">
378 <?php
echo text(formatcyp($productcyp)); ?
>
380 <td
class="dehead" align
="right">
381 <?php
echo text(formatcyp($producttotal)); ?
>
385 <tr bgcolor
="#ffdddd">
386 <td
class="detail" colspan
="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
387 <?php
echo xlt('Grand Total'); ?
>
389 <td
class="dehead" align
="right">
390 <?php
echo text($grandqty); ?
>
392 <td
class="dehead" align
="right">
395 <td
class="dehead" align
="right">
396 <?php
echo text(formatcyp($grandtotal)); ?
>
401 } // End not csv export
402 } // end report generation
404 if (! $_POST['form_csvexport']) {
413 } // End not csv export