simplify decrypt to return blank value when a empty value is sent (#2115)
[openemr.git] / interface / reports / sales_by_item.php
blobe75871c3760652c377ee729d420261e18be4bb39
1 <?php
2 /**
3 * This is a report of sales by item description.
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Rod Roark <rod@sunsetsystems.com>
8 * @author Terry Hill <terry@lillysystems.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2006-2016 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2015-2016 Terry Hill <terry@lillysystems.com>
12 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
17 require_once("../globals.php");
18 require_once("$srcdir/patient.inc");
19 require_once("$srcdir/acl.inc");
20 require_once "$srcdir/options.inc.php";
22 use OpenEMR\Core\Header;
24 if (!empty($_POST)) {
25 if (!verifyCsrfToken($_POST["csrf_token_form"])) {
26 csrfNotVerified();
30 $form_provider = $_POST['form_provider'];
31 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
32 $form_details = $_POST['form_details'] ? true : false;
33 } else {
34 $form_details = false;
37 function bucks($amount)
39 if ($amount) {
40 return oeFormatMoney($amount);
44 function display_desc($desc)
46 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
47 $desc = $matches[1];
50 return $desc;
53 function thisLineItem($patient_id, $encounter_id, $rowcat, $description, $transdate, $qty, $amount, $irnumber = '')
55 global $product, $category, $producttotal, $productqty, $cattotal, $catqty, $grandtotal, $grandqty;
56 global $productleft, $catleft;
58 $invnumber = $irnumber ? $irnumber : "$patient_id.$encounter_id";
59 $rowamount = sprintf('%01.2f', $amount);
61 $patdata = sqlQuery("SELECT " .
62 "p.fname, p.mname, p.lname, p.pubpid, p.DOB, " .
63 "p.street, p.city, p.state, p.postal_code, " .
64 "p.ss, p.sex, p.status, p.phone_home, " .
65 "p.phone_biz, p.phone_cell, p.hipaa_notice " .
66 "FROM patient_data AS p " .
67 "WHERE p.pid = ? LIMIT 1", array($patient_id));
69 $pat_name = $patdata['fname'] . ' ' . $patdata['mname'] . ' ' . $patdata['lname'];
71 if (empty($rowcat)) {
72 $rowcat = xl('None');
75 $rowproduct = $description;
76 if (! $rowproduct) {
77 $rowproduct = xl('Unknown');
80 if ($product != $rowproduct || $category != $rowcat) {
81 if ($product) {
82 // Print product total.
83 if ($_POST['form_csvexport']) {
84 if (! $_POST['form_details']) {
85 echo '"' . display_desc($category) . '",';
86 echo '"' . display_desc($product) . '",';
87 echo '"' . $productqty . '",';
88 echo '"';
89 echo bucks($producttotal);
90 echo '"' . "\n";
92 } else {
94 <tr bgcolor="#ddddff">
95 <td class="detail">
96 <?php echo text(display_desc($catleft));
97 $catleft = " "; ?>
98 </td>
99 <td class="detail" colspan="3">
100 <?php
101 if ($_POST['form_details']) {
102 echo xlt('Total for') . ' ';
105 echo text(display_desc($product)); ?>
106 </td>
107 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
108 <td>
109 &nbsp;
110 </td>
111 <?php } ?>
112 <td align="right">
113 &nbsp;
114 </td>
115 <td align="right">
116 <?php echo text($productqty); ?>
117 </td>
118 <td align="right">
119 <?php echo text(bucks($producttotal)); ?>
120 </td>
121 </tr>
122 <?php
123 } // End not csv export
126 $producttotal = 0;
127 $productqty = 0;
128 $product = $rowproduct;
129 $productleft = $product;
132 if ($category != $rowcat) {
133 if ($category) {
134 // Print category total.
135 if (!$_POST['form_csvexport']) {
138 <tr bgcolor="#ffdddd">
139 <td class="detail">
140 &nbsp;
141 </td>
142 <td class="detail" colspan="3">
143 <?php echo xlt('Total for category') . ' ';
144 echo text(display_desc($category)); ?>
145 </td>
146 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
147 <td>
148 &nbsp;
149 </td>
150 <?php } ?>
151 <td align="right">
152 &nbsp;
153 </td>
154 <td align="right">
155 <?php echo text($catqty); ?>
156 </td>
157 <td align="right">
158 <?php echo text(bucks($cattotal)); ?>
159 </td>
160 </tr>
161 <?php
162 } // End not csv export
165 $cattotal = 0;
166 $catqty = 0;
167 $category = $rowcat;
168 $catleft = $category;
171 if ($_POST['form_details']) {
172 if ($_POST['form_csvexport']) {
173 echo '"' . display_desc($category) . '",';
174 echo '"' . display_desc($product) . '",';
175 echo '"' . oeFormatShortDate(display_desc($transdate)) . '",';
176 if ($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2) {
177 echo '"' . $pat_name . '",';
180 if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {
181 echo '"' . display_desc($invnumber) . '",';
184 if ($GLOBALS['sales_report_invoice'] == 1) {
185 echo '"' . $patient_id . '",';
188 // echo '"' . display_desc($invnumber) . '",';
189 echo '"' . display_desc($qty) . '",';
190 echo '"';
191 echo bucks($rowamount);
192 echo '"' . "\n";
193 } else {
196 <tr>
197 <td class="detail">
198 <?php echo text(display_desc($catleft));
199 $catleft = " "; ?>
200 </td>
201 <td class="detail">
202 <?php echo text(display_desc($productleft));
203 $productleft = " "; ?>
204 </td>
205 <td>
206 <?php echo text(oeFormatShortDate($transdate)); ?>
207 </td>
208 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
209 <td>
210 &nbsp;
211 </td>
212 <?php } ?>
213 <?php if ($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2) { ?>
214 <td>
215 <?php echo text($pat_name); ?>
216 </td>
217 <?php } ?>
218 <td class="detail">
219 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) { ?>
220 <a href='../patient_file/pos_checkout.php?ptid=<?php echo attr_url($patient_id); ?>&enc=<?php echo attr_url($encounter_id); ?>'>
221 <?php echo text($invnumber); ?></a>
222 <?php }
224 if ($GLOBALS['sales_report_invoice'] == 1) {
225 echo text($patient_id);
228 </td>
229 <?php if ($GLOBALS['sales_report_invoice'] == 0) {?>
230 <td>
231 &nbsp;
232 </td>
233 <?php } ?>
234 <td align="right">
235 <?php echo text($qty); ?>
236 </td>
237 <td align="right">
238 <?php echo text(bucks($rowamount)); ?>
239 </td>
240 </tr>
241 <?php
242 } // End not csv export
243 } // end details
244 $producttotal += $rowamount;
245 $cattotal += $rowamount;
246 $grandtotal += $rowamount;
247 $productqty += $qty;
248 $catqty += $qty;
249 $grandqty += $qty;
250 } // end function
252 if (! acl_check('acct', 'rep')) {
253 die(xlt("Unauthorized access."));
256 $form_from_date = (isset($_POST['form_from_date'])) ? DateToYYYYMMDD($_POST['form_from_date']) : date('Y-m-d');
257 $form_to_date = (isset($_POST['form_to_date'])) ? DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d');
258 $form_facility = $_POST['form_facility'];
260 if ($_POST['form_csvexport']) {
261 header("Pragma: public");
262 header("Expires: 0");
263 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
264 header("Content-Type: application/force-download");
265 header("Content-Disposition: attachment; filename=sales_by_item.csv");
266 header("Content-Description: File Transfer");
267 // CSV headers:
268 if ($_POST['form_details']) {
269 echo '"Category",';
270 echo '"Item",';
271 echo '"Date",';
272 if ($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2) {
273 echo '"Name",';
276 if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {
277 echo '"Invoice",';
280 if ($GLOBALS['sales_report_invoice'] == 1) {
281 echo '"ID",';
284 echo '"Qty",';
285 echo '"Amount"' . "\n";
286 } else {
287 echo '"Category",';
288 echo '"Item",';
289 echo '"Qty",';
290 echo '"Total"' . "\n";
292 } // end export
293 else {
295 <html>
296 <head>
298 <title><?php echo xlt('Sales by Item'); ?></title>
300 <?php Header::setupHeader(['datetime-picker', 'report-helper']); ?>
302 <style type="text/css">
303 /* specifically include & exclude from printing */
304 @media print {
305 #report_parameters {
306 visibility: hidden;
307 display: none;
309 #report_parameters_daterange {
310 visibility: visible;
311 display: inline;
313 #report_results {
314 margin-top: 30px;
318 /* specifically exclude some from the screen */
319 @media screen {
320 #report_parameters_daterange {
321 visibility: hidden;
322 display: none;
326 table.mymaintable, table.mymaintable td {
327 border: 1px solid #aaaaaa;
328 border-collapse: collapse;
330 table.mymaintable td {
331 padding: 1pt 4pt 1pt 4pt;
333 </style>
335 <script language="JavaScript">
336 $(document).ready(function() {
337 oeFixedHeaderSetup(document.getElementById('mymaintable'));
338 var win = top.printLogSetup ? top : opener.top;
339 win.printLogSetup(document.getElementById('printbutton'));
341 $('.datepicker').datetimepicker({
342 <?php $datetimepicker_timepicker = false; ?>
343 <?php $datetimepicker_showseconds = false; ?>
344 <?php $datetimepicker_formatInput = true; ?>
345 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
346 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
349 </script>
350 </head>
352 <title><?php echo xlt('Sales by Item') ?></title>
354 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' class="body_top">
356 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Sales by Item'); ?></span>
358 <form method='post' action='sales_by_item.php' id='theform' onsubmit='return top.restoreSession()'>
359 <input type="hidden" name="csrf_token_form" value="<?php echo attr(collectCsrfToken()); ?>" />
361 <div id="report_parameters">
362 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
363 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
364 <table>
365 <tr>
366 <td width='630px'>
367 <div style='float:left'>
368 <table class='text'>
369 <tr>
370 <td class='control-label'>
371 <?php echo xlt('Facility'); ?>:
372 </td>
373 <td>
374 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
375 </td>
376 <td class='control-label'>
377 <?php echo xlt('From'); ?>:
378 </td>
379 <td>
380 <input type='text' class='datepicker form-control' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr(oeFormatShortDate($form_from_date)); ?>'>
381 </td>
382 <td class='control-label'>
383 <?php echo xlt('To'); ?>:
384 </td>
385 <td>
386 <input type='text' class='datepicker form-control' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr(oeFormatShortDate($form_to_date)); ?>'>
387 </td>
388 </tr>
389 </table>
390 <table class='text'>
391 <tr>
392 <td class='control-label'>
393 <?php echo xlt('Provider'); ?>:
394 </td>
395 <td>
396 <?php
397 if (acl_check('acct', 'rep_a')) {
398 // Build a drop-down list of providers.
399 $query = "select id, lname, fname from users where " .
400 "authorized = 1 order by lname, fname";
401 $res = sqlStatement($query);
402 echo " &nbsp;<select name='form_provider' class='form-control'>\n";
403 echo " <option value=''>-- " . xlt('All Providers') . " --\n";
404 while ($row = sqlFetchArray($res)) {
405 $provid = $row['id'];
406 echo " <option value='". attr($provid) ."'";
407 if ($provid == $_REQUEST['form_provider']) {
408 echo " selected";
411 echo ">" . text($row['lname']) . ", " . text($row['fname']) . "\n";
414 echo " </select>\n";
415 } else {
416 echo "<input type='hidden' name='form_provider' value='" . attr($_SESSION['authUserID']) . "'>";
419 &nbsp;
420 </td>
421 <td>
422 <div class='checkbox'>
423 <label><input type='checkbox' name='form_details'<?php echo ($form_details) ? ' checked' : ''; ?>>
424 <?php echo xlt('Details'); ?></label>
425 </div>
426 </td>
427 </tr>
428 </table>
429 </div>
431 </td>
432 <td align='left' valign='middle' height="100%">
433 <table style='border-left:1px solid; width:100%; height:100%' >
434 <tr>
435 <td>
436 <div class="text-center">
437 <div class="btn-group" role="group">
438 <a href='#' class='btn btn-default btn-save' onclick='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
439 <?php echo xlt('Submit'); ?>
440 </a>
441 <?php if ($_POST['form_refresh'] || $_POST['form_csvexport']) { ?>
442 <a href='#' class='btn btn-default btn-print' id='printbutton'>
443 <?php echo xlt('Print'); ?>
444 </a>
445 <a href='#' class='btn btn-default btn-transmit' onclick='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
446 <?php echo xlt('CSV Export'); ?>
447 </a>
448 <?php } ?>
449 </div>
450 </div>
451 </td>
452 </tr>
453 </table>
454 </td>
455 </tr>
456 </table>
458 </div> <!-- end of parameters -->
460 <?php
461 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
464 <div id="report_results">
465 <table width='98%' id='mymaintable' class='mymaintable'>
466 <thead>
467 <th>
468 <?php echo xlt('Category'); ?>
469 </th>
470 <th>
471 <?php echo xlt('Item'); ?>
472 </th>
473 <th>
474 <?php
475 if ($form_details) {
476 echo xlt('Date');
477 } ?>
478 </th>
479 <?php if ($GLOBALS['sales_report_invoice'] == 2) {?>
480 <th>
481 &nbsp;
482 </th>
483 <?php } ?>
484 <th>
485 <?php
486 if ($GLOBALS['sales_report_invoice'] == 0) {
487 if ($form_details) {
488 echo ' ';
491 </th>
492 <th>
493 <?php
494 if ($form_details) {
495 echo xlt('Invoice');
499 if ($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2) {
500 if ($form_details) {
501 echo xlt('Name');
503 } ?>
504 </th>
505 <th>
506 <?php
507 if ($GLOBALS['sales_report_invoice'] == 2) {
508 if ($form_details) {
509 echo xlt('Invoice');
513 if ($GLOBALS['sales_report_invoice'] == 1) {
514 if ($form_details) {
515 echo xlt('ID');
519 </th>
520 <th align="right">
521 <?php echo xlt('Qty'); ?>
522 </th>
523 <th align="right">
524 <?php echo xlt('Amount'); ?>
525 </th>
526 </thead>
527 <tbody>
528 <?php
529 } // end not export
532 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
533 $from_date = $form_from_date . ' 00:00:00';
534 $to_date = $form_to_date . ' 23:59:59';
535 $category = "";
536 $catleft = "";
537 $cattotal = 0;
538 $catqty = 0;
539 $product = "";
540 $productleft = "";
541 $producttotal = 0;
542 $productqty = 0;
543 $grandtotal = 0;
544 $grandqty = 0;
546 $sqlBindArray = array();
547 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.units, " .
548 "b.code_text, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno, lo.title " .
549 "FROM billing AS b " .
550 "JOIN code_types AS ct ON ct.ct_key = b.code_type " .
551 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
552 "LEFT JOIN codes AS c ON c.code_type = ct.ct_id AND c.code = b.code AND c.modifier = b.modifier " .
553 "LEFT JOIN list_options AS lo ON lo.list_id = 'superbill' AND lo.option_id = c.superbill AND lo.activity = 1 " .
554 "WHERE b.code_type != 'COPAY' AND b.activity = 1 AND b.fee != 0 AND " .
555 "fe.date >= ? AND fe.date <= ?";
556 array_push($sqlBindArray, $from_date, $to_date);
557 // If a facility was specified.
558 if ($form_facility) {
559 $query .= " AND fe.facility_id = ?";
560 array_push($sqlBindArray, $form_facility);
563 if ($form_provider) {
564 $query .= " AND fe.provider_id = ?";
565 array_push($sqlBindArray, $form_provider);
568 $query .= " ORDER BY lo.title, b.code, fe.date, fe.id";
570 $res = sqlStatement($query, $sqlBindArray);
571 while ($row = sqlFetchArray($res)) {
572 thisLineItem(
573 $row['pid'],
574 $row['encounter'],
575 $row['title'],
576 $row['code'] . ' ' . $row['code_text'],
577 substr($row['date'], 0, 10),
578 $row['units'],
579 $row['fee'],
580 $row['invoice_refno']
585 $sqlBindArray = array();
586 $query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " .
587 "d.name, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno " .
588 "FROM drug_sales AS s " .
589 "JOIN drugs AS d ON d.drug_id = s.drug_id " .
590 "JOIN form_encounter AS fe ON " .
591 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
592 "fe.date >= ? AND fe.date <= ? " .
593 "WHERE s.fee != 0";
594 array_push($sqlBindArray, $from_date, $to_date);
595 // If a facility was specified.
596 if ($form_facility) {
597 $query .= " AND fe.facility_id = ?";
598 array_push($sqlBindArray, $form_facility);
601 if ($form_provider) {
602 $query .= " AND fe.provider_id = ?";
603 array_push($sqlBindArray, $form_provider);
606 $query .= " ORDER BY d.name, fe.date, fe.id";
608 $res = sqlStatement($query, $sqlBindArray);
609 while ($row = sqlFetchArray($res)) {
610 thisLineItem(
611 $row['pid'],
612 $row['encounter'],
613 xl('Products'),
614 $row['name'],
615 substr($row['date'], 0, 10),
616 $row['quantity'],
617 $row['fee'],
618 $row['invoice_refno']
622 if ($_POST['form_csvexport']) {
623 if (! $_POST['form_details']) {
624 echo '"' . display_desc($product) . '",';
625 echo '"' . $productqty . '",';
626 echo '"';
627 echo bucks($producttotal);
628 echo '"' . "\n";
630 } else {
633 <tr bgcolor="#ddddff">
634 <td class="detail">
635 <?php echo text(display_desc($catleft));
636 $catleft = " "; ?>
637 </td>
638 <td class="detail" colspan="3">
639 <?php
640 if ($_POST['form_details']) {
641 echo xlt('Total for') . ' ';
644 echo text(display_desc($product)); ?>
645 </td>
646 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
647 <td>
648 &nbsp;
649 </td>
650 <?php } ?>
651 <td align="right">
652 &nbsp;
653 </td>
654 <td align="right">
655 <?php echo text($productqty); ?>
656 </td>
657 <td align="right">
658 <?php echo text(bucks($producttotal)); ?>
659 </td>
660 </tr>
662 <tr bgcolor="#ffdddd">
663 <td class="detail">
664 &nbsp;
665 </td>
666 <td class="detail" colspan="3"><b>
667 <?php echo xlt('Total for category') . ' ';
668 echo text(display_desc($category)); ?>
669 </b></td>
670 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
671 <td>
672 &nbsp;
673 </td>
674 <?php } ?>
675 <td align="right">
676 &nbsp;
677 </td>
678 <td align="right"><b>
679 <?php echo text($catqty); ?>
680 </b></td>
681 <td align="right"><b>
682 <?php echo text(bucks($cattotal)); ?>
683 </b></td>
684 </tr>
686 <tr>
687 <td class="detail" colspan="4"><b>
688 <?php echo xlt('Grand Total'); ?>
689 </b></td>
690 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
691 <td>
692 &nbsp;
693 </td>
694 <?php } ?>
695 <td align="right">
696 &nbsp;
697 </td>
698 <td align="right"><b>
699 <?php echo text($grandqty); ?>
700 </b></td>
701 <td align="right"><b>
702 <?php echo text(bucks($grandtotal)); ?>
703 </b></td>
704 </tr>
705 <?php $report_from_date = oeFormatShortDate($form_from_date) ;
706 $report_to_date = oeFormatShortDate($form_to_date) ;
708 <div align='right'><span class='title' ><?php echo xlt('Report Date'). ' '; ?><?php echo text($report_from_date);?> - <?php echo text($report_to_date);?></span></div>
709 <?php
710 } // End not csv export
713 if (! $_POST['form_csvexport']) {
714 if ($_POST['form_refresh']) {
717 </tbody>
718 </table>
719 </div> <!-- report results -->
720 <?php
721 } else { ?>
722 <div class='text'>
723 <?php echo xlt('Please input search criteria above, and click Submit to view results.'); ?>
724 </div>
725 <?php
726 } ?>
728 </form>
730 </body>
732 </html>
733 <?php
734 } // End not csv export