fix: quick fix to enforce support of x509 database connection on install (#6157)
[openemr.git] / interface / reports / ippf_cyp_report.php
blob782a8c03344be68023d8630bb368d0c74c9c0a02
1 <?php
3 /**
4 * ippf_cyp_report.
6 * @package OpenEMR
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.php");
18 use OpenEMR\Common\Acl\AclMain;
19 use OpenEMR\Common\Csrf\CsrfUtils;
20 use OpenEMR\Common\Twig\TwigContainer;
21 use OpenEMR\Core\Header;
23 if (!empty($_POST)) {
24 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
25 CsrfUtils::csrfNotVerified();
29 function formatcyp($amount)
31 if ($amount) {
32 return sprintf("%.2f", $amount);
35 return '';
38 function display_desc($desc)
40 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
41 $desc = $matches[1];
44 return $desc;
47 function thisLineItem($patient_id, $encounter_id, $description, $transdate, $qty, $cypfactor, $irnumber = '')
49 global $product, $productcyp, $producttotal, $productqty, $grandtotal, $grandqty;
51 $invnumber = empty($irnumber) ? "$patient_id.$encounter_id" : $irnumber;
52 $rowcyp = sprintf('%01.2f', $cypfactor);
53 $rowresult = sprintf('%01.2f', $rowcyp * $qty);
55 $rowproduct = $description;
56 if (! $rowproduct) {
57 $rowproduct = 'Unknown';
60 if ($product != $rowproduct) {
61 if ($product) {
62 // Print product total.
63 if ($_POST['form_csvexport']) {
64 if (! $_POST['form_details']) {
65 echo csvEscape(display_desc($product)) . ',';
66 echo csvEscape($productqty) . ',';
67 echo csvEscape(formatcyp($productcyp)) . ',';
68 echo csvEscape(formatcyp($producttotal)) . "\n";
70 } else {
73 <tr bgcolor="#ddddff">
74 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
75 <?php
76 if ($_POST['form_details']) {
77 echo xlt('Total for ');
79 echo text(display_desc($product)); ?>
80 </td>
81 <td class="dehead" align="right">
82 <?php echo text($productqty); ?>
83 </td>
84 <td class="dehead" align="right">
85 <?php echo text(formatcyp($productcyp)); ?>
86 </td>
87 <td class="dehead" align="right">
88 <?php echo text(formatcyp($producttotal)); ?>
89 </td>
90 </tr>
91 <?php
92 } // End not csv export
95 $producttotal = 0;
96 $productqty = 0;
97 $product = $rowproduct;
98 $productleft = $product;
99 $productcyp = $rowcyp;
102 if ($_POST['form_details']) {
103 if ($_POST['form_csvexport']) {
104 echo csvEscape(display_desc($product)) . ',';
105 echo csvEscape(oeFormatShortDate(display_desc($transdate))) . ',';
106 echo csvEscape(display_desc($invnumber)) . ',';
107 echo csvEscape(display_desc($qty)) . ',';
108 echo csvEscape(formatcyp($rowcyp)) . ',';
109 echo csvEscape(formatcyp($rowresult)) . "\n";
110 } else {
113 <tr>
114 <td class="detail">
115 <?php echo text(display_desc($productleft));
116 $productleft = "&nbsp;"; ?>
117 </td>
118 <td class="dehead">
119 <?php echo text(oeFormatShortDate($transdate)); ?>
120 </td>
121 <td class="detail">
122 <?php echo text($invnumber); ?>
123 </td>
124 <td class="dehead" align="right">
125 <?php echo text($qty); ?>
126 </td>
127 <td class="dehead" align="right">
128 <?php echo text(formatcyp($rowcyp)); ?>
129 </td>
130 <td class="dehead" align="right">
131 <?php echo text(formatcyp($rowresult)); ?>
132 </td>
133 </tr>
134 <?php
135 } // End not csv export
136 } // end details
137 $producttotal += $rowresult;
138 $grandtotal += $rowresult;
139 $productqty += $qty;
140 $grandqty += $qty;
141 } // end function
143 if (! AclMain::aclCheckCore('acct', 'rep')) {
144 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("CYP Report")]);
145 exit;
148 $form_from_date = (isset($_POST['form_from_date'])) ? DateToYYYYMMDD($_POST['form_from_date']) : date('Y-m-d');
149 $form_to_date = (isset($_POST['form_to_date'])) ? DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d');
150 $form_facility = $_POST['form_facility'];
152 if ($_POST['form_csvexport']) {
153 header("Pragma: public");
154 header("Expires: 0");
155 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
156 header("Content-Type: application/force-download");
157 header("Content-Disposition: attachment; filename=ippf_cyp_report.csv");
158 header("Content-Description: File Transfer");
159 // CSV headers:
160 if ($_POST['form_details']) {
161 echo csvEscape("Item") . ',';
162 echo csvEscape("Date") . ',';
163 echo csvEscape("Invoice") . ',';
164 echo csvEscape("Qty") . ',';
165 echo csvEscape("CYP") . ',';
166 echo csvEscape("Result") . "\n";
167 } else {
168 echo csvEscape("Item") . ',';
169 echo csvEscape("Qty") . ',';
170 echo csvEscape("CYP") . ',';
171 echo csvEscape("Result") . "\n";
173 } else { // not export
175 <html>
176 <head>
178 <title><?php echo xlt('CYP Report') ?></title>
180 <?php Header::setupHeader(['datetime-picker']); ?>
182 <script>
183 $(function () {
184 var win = top.printLogSetup ? top : opener.top;
185 win.printLogSetup(document.getElementById('printbutton'));
187 $('.datepicker').datetimepicker({
188 <?php $datetimepicker_timepicker = false; ?>
189 <?php $datetimepicker_showseconds = false; ?>
190 <?php $datetimepicker_formatInput = true; ?>
191 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
192 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
195 </script>
197 </head>
199 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
200 <center>
202 <h2><?php echo xlt('CYP Report')?></h2>
204 <form method='post' action='ippf_cyp_report.php' onsubmit='return top.restoreSession()'>
205 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
207 <table border='0' cellpadding='3'>
209 <tr>
210 <td>
211 <?php
212 // Build a drop-down list of facilities.
214 $query = "SELECT id, name FROM facility ORDER BY name";
215 $fres = sqlStatement($query);
216 echo " <select name='form_facility'>\n";
217 echo " <option value=''>-- All Facilities --\n";
218 while ($frow = sqlFetchArray($fres)) {
219 $facid = $frow['id'];
220 echo " <option value='" . attr($facid) . "'";
221 if ($facid == $form_facility) {
222 echo " selected";
225 echo ">" . text($frow['name']) . "\n";
228 echo " </select>\n";
230 &nbsp;<?php echo xlt('From')?>:
231 <input type='text' class='datepicker' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr(oeFormatShortDate($form_from_date)); ?>'>
232 &nbsp;<?php echo xlt('To{{Range}}')?>:
233 <input type='text' class='datepicker' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr(oeFormatShortDate($form_to_date)); ?>'>
234 &nbsp;
235 <input type='checkbox' name='form_details' value='1'<?php echo ($_POST['form_details']) ? " checked" : ""; ?>><?php echo xlt('Details') ?>
236 &nbsp;
237 <input type='submit' name='form_refresh' value="<?php echo xla('Refresh') ?>">
238 &nbsp;
239 <input type='submit' name='form_csvexport' value="<?php echo xla('Export to CSV') ?>">
240 &nbsp;
241 <input type='button' value='<?php echo xla('Print'); ?>' id='printbutton' />
242 </td>
243 </tr>
245 <tr>
246 <td height="1">
247 </td>
248 </tr>
250 </table>
252 <table border='0' cellpadding='1' cellspacing='2' width='98%'>
254 <tr bgcolor="#dddddd">
255 <td class="dehead">
256 <?php echo xlt('Item') ?>
257 </td>
258 <?php if ($_POST['form_details']) { ?>
259 <td class="dehead">
260 <?php echo xlt('Date') ?>
261 </td>
262 <td class="dehead">
263 <?php echo xlt('Invoice') ?>
264 </td>
265 <?php } ?>
266 <td class="dehead" align="right">
267 <?php echo xlt('Qty') ?>
268 </td>
269 <td class="dehead" align="right">
270 <?php echo xlt('CYP') ?>
271 </td>
272 <td class="dehead" align="right">
273 <?php echo xlt('Result') ?>
274 </td>
275 </tr>
276 <?php
277 } // end not export
279 // If generating a report.
281 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
282 $from_date = $form_from_date;
283 $to_date = $form_to_date;
285 $product = "";
286 $productleft = "";
287 $productcyp = 0;
288 $producttotal = 0; // total of results for product
289 $grandtotal = 0; // grand total of results
290 $productqty = 0;
291 $grandqty = 0;
293 $sqlBindArray = array();
295 $query = "SELECT b.pid, b.encounter, b.code_type, b.code, b.units, " .
296 "b.code_text, c.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " .
297 "FROM billing AS b " .
298 "JOIN codes AS c ON c.code_type = '12' AND c.code = b.code AND c.modifier = b.modifier AND c.cyp_factor > 0 " .
299 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
300 "WHERE b.code_type = 'MA' AND b.activity = 1 AND " .
301 "fe.date >= ? AND fe.date <= ?";
302 array_push($sqlBindArray, $from_date . ' 00:00:00', $to_date . ' 23:59:59');
304 // If a facility was specified.
305 if ($form_facility) {
306 $query .= " AND fe.facility_id = ?";
307 array_push($sqlBindArray, $form_facility);
310 $query .= " ORDER BY b.code, fe.date, fe.id";
312 $res = sqlStatement($query, $sqlBindArray);
313 while ($row = sqlFetchArray($res)) {
314 thisLineItem(
315 $row['pid'],
316 $row['encounter'],
317 $row['code'] . ' ' . $row['code_text'],
318 substr($row['date'], 0, 10),
319 $row['units'],
320 $row['cyp_factor'],
321 $row['invoice_refno']
325 $sqlBindArray = array();
327 $query = "SELECT s.sale_date, s.quantity, s.pid, s.encounter, " .
328 "d.name, d.cyp_factor, fe.date, fe.facility_id, fe.invoice_refno " .
329 "FROM drug_sales AS s " .
330 "JOIN drugs AS d ON d.drug_id = s.drug_id AND d.cyp_factor > 0 " .
331 "JOIN form_encounter AS fe ON " .
332 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
333 "fe.date >= ? AND fe.date <= ? " .
334 "WHERE s.fee != 0";
335 array_push($sqlBindArray, $from_date . ' 00:00:00', $to_date . ' 23:59:59');
337 // If a facility was specified.
338 if ($form_facility) {
339 $query .= " AND fe.facility_id = ?";
340 array_push($sqlBindArray, $form_facility);
343 $query .= " ORDER BY d.name, fe.date, fe.id";
345 $res = sqlStatement($query, $sqlBindArray);
346 while ($row = sqlFetchArray($res)) {
347 thisLineItem(
348 $row['pid'],
349 $row['encounter'],
350 $row['name'],
351 substr($row['date'], 0, 10),
352 $row['quantity'],
353 $row['cyp_factor'],
354 $row['invoice_refno']
358 if ($_POST['form_csvexport']) {
359 if (! $_POST['form_details']) {
360 echo csvEscape(display_desc($product)) . ',';
361 echo csvEscape($productqty) . ',';
362 echo csvEscape(formatcyp($productcyp)) . ',';
363 echo csvEscape(formatcyp($producttotal)) . "\n";
365 } else {
368 <tr bgcolor="#ddddff">
369 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
370 <?php
371 if ($_POST['form_details']) {
372 echo xlt('Total for ');
374 echo text(display_desc($product)); ?>
375 </td>
376 <td class="dehead" align="right">
377 <?php echo text($productqty); ?>
378 </td>
379 <td class="dehead" align="right">
380 <?php echo text(formatcyp($productcyp)); ?>
381 </td>
382 <td class="dehead" align="right">
383 <?php echo text(formatcyp($producttotal)); ?>
384 </td>
385 </tr>
387 <tr bgcolor="#ffdddd">
388 <td class="detail" colspan="<?php echo $_POST['form_details'] ? 3 : 1; ?>">
389 <?php echo xlt('Grand Total'); ?>
390 </td>
391 <td class="dehead" align="right">
392 <?php echo text($grandqty); ?>
393 </td>
394 <td class="dehead" align="right">
395 &nbsp;
396 </td>
397 <td class="dehead" align="right">
398 <?php echo text(formatcyp($grandtotal)); ?>
399 </td>
400 </tr>
402 <?php
403 } // End not csv export
404 } // end report generation
406 if (! $_POST['form_csvexport']) {
409 </table>
410 </form>
411 </center>
412 </body>
413 </html>
414 <?php
415 } // End not csv export