minor changes to prior commit
[openemr.git] / interface / reports / sales_by_item.php
blob0b892ef7bfe6f1fd15d9609e9ca4f511dca82d44
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 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 $form_provider = $_POST['form_provider'];
25 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
26 $form_details = $_POST['form_details'] ? true : false;
27 } else {
28 $form_details = false;
31 function bucks($amount)
33 if ($amount) {
34 echo oeFormatMoney($amount);
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, $rowcat, $description, $transdate, $qty, $amount, $irnumber = '')
49 global $product, $category, $producttotal, $productqty, $cattotal, $catqty, $grandtotal, $grandqty;
50 global $productleft, $catleft;
52 $invnumber = $irnumber ? $irnumber : "$patient_id.$encounter_id";
53 $rowamount = sprintf('%01.2f', $amount);
55 $patdata = sqlQuery("SELECT " .
56 "p.fname, p.mname, p.lname, p.pubpid, p.DOB, " .
57 "p.street, p.city, p.state, p.postal_code, " .
58 "p.ss, p.sex, p.status, p.phone_home, " .
59 "p.phone_biz, p.phone_cell, p.hipaa_notice " .
60 "FROM patient_data AS p " .
61 "WHERE p.pid = ? LIMIT 1", array($patient_id));
63 $pat_name = $patdata['fname'] . ' ' . $patdata['mname'] . ' ' . $patdata['lname'];
65 if (empty($rowcat)) {
66 $rowcat = xl('None');
69 $rowproduct = $description;
70 if (! $rowproduct) {
71 $rowproduct = xl('Unknown');
74 if ($product != $rowproduct || $category != $rowcat) {
75 if ($product) {
76 // Print product total.
77 if ($_POST['form_csvexport']) {
78 if (! $_POST['form_details']) {
79 echo '"' . display_desc($category) . '",';
80 echo '"' . display_desc($product) . '",';
81 echo '"' . $productqty . '",';
82 echo '"';
83 bucks($producttotal);
84 echo '"' . "\n";
86 } else {
88 <tr bgcolor="#ddddff">
89 <td class="detail">
90 <?php echo text(display_desc($catleft));
91 $catleft = " "; ?>
92 </td>
93 <td class="detail" colspan="3">
94 <?php
95 if ($_POST['form_details']) {
96 echo xlt('Total for') . ' ';
99 echo text(display_desc($product)); ?>
100 </td>
101 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
102 <td>
103 &nbsp;
104 </td>
105 <?php } ?>
106 <td align="right">
107 &nbsp;
108 </td>
109 <td align="right">
110 <?php echo text($productqty); ?>
111 </td>
112 <td align="right">
113 <?php text(bucks($producttotal)); ?>
114 </td>
115 </tr>
116 <?php
117 } // End not csv export
120 $producttotal = 0;
121 $productqty = 0;
122 $product = $rowproduct;
123 $productleft = $product;
126 if ($category != $rowcat) {
127 if ($category) {
128 // Print category total.
129 if (!$_POST['form_csvexport']) {
132 <tr bgcolor="#ffdddd">
133 <td class="detail">
134 &nbsp;
135 </td>
136 <td class="detail" colspan="3">
137 <?php echo xlt('Total for category') . ' ';
138 echo text(display_desc($category)); ?>
139 </td>
140 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
141 <td>
142 &nbsp;
143 </td>
144 <?php } ?>
145 <td align="right">
146 &nbsp;
147 </td>
148 <td align="right">
149 <?php echo text($catqty); ?>
150 </td>
151 <td align="right">
152 <?php text(bucks($cattotal)); ?>
153 </td>
154 </tr>
155 <?php
156 } // End not csv export
159 $cattotal = 0;
160 $catqty = 0;
161 $category = $rowcat;
162 $catleft = $category;
165 if ($_POST['form_details']) {
166 if ($_POST['form_csvexport']) {
167 echo '"' . display_desc($category) . '",';
168 echo '"' . display_desc($product) . '",';
169 echo '"' . oeFormatShortDate(display_desc($transdate)) . '",';
170 if ($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2) {
171 echo '"' . $pat_name . '",';
174 if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {
175 echo '"' . display_desc($invnumber) . '",';
178 if ($GLOBALS['sales_report_invoice'] == 1) {
179 echo '"' . $patient_id . '",';
182 // echo '"' . display_desc($invnumber) . '",';
183 echo '"' . display_desc($qty) . '",';
184 echo '"';
185 bucks($rowamount);
186 echo '"' . "\n";
187 } else {
190 <tr>
191 <td class="detail">
192 <?php echo text(display_desc($catleft));
193 $catleft = " "; ?>
194 </td>
195 <td class="detail">
196 <?php echo text(display_desc($productleft));
197 $productleft = " "; ?>
198 </td>
199 <td>
200 <?php echo text(oeFormatShortDate($transdate)); ?>
201 </td>
202 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
203 <td>
204 &nbsp;
205 </td>
206 <?php } ?>
207 <?php if ($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2) { ?>
208 <td>
209 <?php echo text($pat_name); ?>
210 </td>
211 <?php } ?>
212 <td class="detail">
213 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) { ?>
214 <a href='../patient_file/pos_checkout.php?ptid=<?php echo attr($patient_id); ?>&enc=<?php echo attr($encounter_id); ?>'>
215 <?php echo text($invnumber); ?></a>
216 <?php }
218 if ($GLOBALS['sales_report_invoice'] == 1) {
219 echo text($patient_id);
222 </td>
223 <?php if ($GLOBALS['sales_report_invoice'] == 0) {?>
224 <td>
225 &nbsp;
226 </td>
227 <?php } ?>
228 <td align="right">
229 <?php echo text($qty); ?>
230 </td>
231 <td align="right">
232 <?php text(bucks($rowamount)); ?>
233 </td>
234 </tr>
235 <?php
236 } // End not csv export
237 } // end details
238 $producttotal += $rowamount;
239 $cattotal += $rowamount;
240 $grandtotal += $rowamount;
241 $productqty += $qty;
242 $catqty += $qty;
243 $grandqty += $qty;
244 } // end function
246 if (! acl_check('acct', 'rep')) {
247 die(xlt("Unauthorized access."));
250 $form_from_date = (isset($_POST['form_from_date'])) ? DateToYYYYMMDD($_POST['form_from_date']) : date('Y-m-d');
251 $form_to_date = (isset($_POST['form_to_date'])) ? DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d');
252 $form_facility = $_POST['form_facility'];
254 if ($_POST['form_csvexport']) {
255 header("Pragma: public");
256 header("Expires: 0");
257 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
258 header("Content-Type: application/force-download");
259 header("Content-Disposition: attachment; filename=sales_by_item.csv");
260 header("Content-Description: File Transfer");
261 // CSV headers:
262 if ($_POST['form_details']) {
263 echo '"Category",';
264 echo '"Item",';
265 echo '"Date",';
266 if ($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2) {
267 echo '"Name",';
270 if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {
271 echo '"Invoice",';
274 if ($GLOBALS['sales_report_invoice'] == 1) {
275 echo '"ID",';
278 echo '"Qty",';
279 echo '"Amount"' . "\n";
280 } else {
281 echo '"Category",';
282 echo '"Item",';
283 echo '"Qty",';
284 echo '"Total"' . "\n";
286 } // end export
287 else {
289 <html>
290 <head>
292 <title><?php echo xlt('Sales by Item'); ?></title>
294 <?php Header::setupHeader(['datetime-picker', 'report-helper']); ?>
296 <style type="text/css">
297 /* specifically include & exclude from printing */
298 @media print {
299 #report_parameters {
300 visibility: hidden;
301 display: none;
303 #report_parameters_daterange {
304 visibility: visible;
305 display: inline;
307 #report_results {
308 margin-top: 30px;
312 /* specifically exclude some from the screen */
313 @media screen {
314 #report_parameters_daterange {
315 visibility: hidden;
316 display: none;
320 table.mymaintable, table.mymaintable td {
321 border: 1px solid #aaaaaa;
322 border-collapse: collapse;
324 table.mymaintable td {
325 padding: 1pt 4pt 1pt 4pt;
327 </style>
329 <script language="JavaScript">
330 $(document).ready(function() {
331 oeFixedHeaderSetup(document.getElementById('mymaintable'));
332 var win = top.printLogSetup ? top : opener.top;
333 win.printLogSetup(document.getElementById('printbutton'));
335 $('.datepicker').datetimepicker({
336 <?php $datetimepicker_timepicker = false; ?>
337 <?php $datetimepicker_showseconds = false; ?>
338 <?php $datetimepicker_formatInput = true; ?>
339 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
340 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
343 </script>
344 </head>
346 <title><?php echo xlt('Sales by Item') ?></title>
348 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' class="body_top">
350 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Sales by Item'); ?></span>
352 <form method='post' action='sales_by_item.php' id='theform' onsubmit='return top.restoreSession()'>
354 <div id="report_parameters">
355 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
356 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
357 <table>
358 <tr>
359 <td width='630px'>
360 <div style='float:left'>
361 <table class='text'>
362 <tr>
363 <td class='control-label'>
364 <?php echo xlt('Facility'); ?>:
365 </td>
366 <td>
367 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
368 </td>
369 <td class='control-label'>
370 <?php echo xlt('From'); ?>:
371 </td>
372 <td>
373 <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)); ?>'>
374 </td>
375 <td class='control-label'>
376 <?php echo xlt('To'); ?>:
377 </td>
378 <td>
379 <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)); ?>'>
380 </td>
381 </tr>
382 </table>
383 <table class='text'>
384 <tr>
385 <td class='control-label'>
386 <?php echo xlt('Provider'); ?>:
387 </td>
388 <td>
389 <?php
390 if (acl_check('acct', 'rep_a')) {
391 // Build a drop-down list of providers.
392 $query = "select id, lname, fname from users where " .
393 "authorized = 1 order by lname, fname";
394 $res = sqlStatement($query);
395 echo " &nbsp;<select name='form_provider' class='form-control'>\n";
396 echo " <option value=''>-- " . xlt('All Providers') . " --\n";
397 while ($row = sqlFetchArray($res)) {
398 $provid = $row['id'];
399 echo " <option value='". attr($provid) ."'";
400 if ($provid == $_REQUEST['form_provider']) {
401 echo " selected";
404 echo ">" . text($row['lname']) . ", " . text($row['fname']) . "\n";
407 echo " </select>\n";
408 } else {
409 echo "<input type='hidden' name='form_provider' value='" . attr($_SESSION['authUserID']) . "'>";
412 &nbsp;
413 </td>
414 <td>
415 <div class='checkbox'>
416 <label><input type='checkbox' name='form_details'<?php echo ($form_details) ? ' checked' : ''; ?>>
417 <?php echo xlt('Details'); ?></label>
418 </div>
419 </td>
420 </tr>
421 </table>
422 </div>
424 </td>
425 <td align='left' valign='middle' height="100%">
426 <table style='border-left:1px solid; width:100%; height:100%' >
427 <tr>
428 <td>
429 <div class="text-center">
430 <div class="btn-group" role="group">
431 <a href='#' class='btn btn-default btn-save' onclick='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
432 <?php echo xlt('Submit'); ?>
433 </a>
434 <?php if ($_POST['form_refresh'] || $_POST['form_csvexport']) { ?>
435 <a href='#' class='btn btn-default btn-print' id='printbutton'>
436 <?php echo xlt('Print'); ?>
437 </a>
438 <a href='#' class='btn btn-default btn-transmit' onclick='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
439 <?php echo xlt('CSV Export'); ?>
440 </a>
441 <?php } ?>
442 </div>
443 </div>
444 </td>
445 </tr>
446 </table>
447 </td>
448 </tr>
449 </table>
451 </div> <!-- end of parameters -->
453 <?php
454 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
457 <div id="report_results">
458 <table width='98%' id='mymaintable' class='mymaintable'>
459 <thead>
460 <th>
461 <?php echo xlt('Category'); ?>
462 </th>
463 <th>
464 <?php echo xlt('Item'); ?>
465 </th>
466 <th>
467 <?php
468 if ($form_details) {
469 echo xlt('Date');
470 } ?>
471 </th>
472 <?php if ($GLOBALS['sales_report_invoice'] == 2) {?>
473 <th>
474 &nbsp;
475 </th>
476 <?php } ?>
477 <th>
478 <?php
479 if ($GLOBALS['sales_report_invoice'] == 0) {
480 if ($form_details) {
481 echo ' ';
484 </th>
485 <th>
486 <?php
487 if ($form_details) {
488 echo xlt('Invoice');
492 if ($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2) {
493 if ($form_details) {
494 echo xlt('Name');
496 } ?>
497 </th>
498 <th>
499 <?php
500 if ($GLOBALS['sales_report_invoice'] == 2) {
501 if ($form_details) {
502 echo xlt('Invoice');
506 if ($GLOBALS['sales_report_invoice'] == 1) {
507 if ($form_details) {
508 echo xlt('ID');
512 </th>
513 <th align="right">
514 <?php echo xlt('Qty'); ?>
515 </th>
516 <th align="right">
517 <?php echo xlt('Amount'); ?>
518 </th>
519 </thead>
520 <tbody>
521 <?php
522 } // end not export
525 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
526 $from_date = $form_from_date . ' 00:00:00';
527 $to_date = $form_to_date . ' 23:59:59';
528 $category = "";
529 $catleft = "";
530 $cattotal = 0;
531 $catqty = 0;
532 $product = "";
533 $productleft = "";
534 $producttotal = 0;
535 $productqty = 0;
536 $grandtotal = 0;
537 $grandqty = 0;
539 $sqlBindArray = array();
540 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.units, " .
541 "b.code_text, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno, lo.title " .
542 "FROM billing AS b " .
543 "JOIN code_types AS ct ON ct.ct_key = b.code_type " .
544 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
545 "LEFT JOIN codes AS c ON c.code_type = ct.ct_id AND c.code = b.code AND c.modifier = b.modifier " .
546 "LEFT JOIN list_options AS lo ON lo.list_id = 'superbill' AND lo.option_id = c.superbill AND lo.activity = 1 " .
547 "WHERE b.code_type != 'COPAY' AND b.activity = 1 AND b.fee != 0 AND " .
548 "fe.date >= ? AND fe.date <= ?";
549 array_push($sqlBindArray, $from_date, $to_date);
550 // If a facility was specified.
551 if ($form_facility) {
552 $query .= " AND fe.facility_id = ?";
553 array_push($sqlBindArray, $form_facility);
556 if ($form_provider) {
557 $query .= " AND fe.provider_id = ?";
558 array_push($sqlBindArray, $form_provider);
561 $query .= " ORDER BY lo.title, b.code, fe.date, fe.id";
563 $res = sqlStatement($query, $sqlBindArray);
564 while ($row = sqlFetchArray($res)) {
565 thisLineItem(
566 $row['pid'],
567 $row['encounter'],
568 $row['title'],
569 $row['code'] . ' ' . $row['code_text'],
570 substr($row['date'], 0, 10),
571 $row['units'],
572 $row['fee'],
573 $row['invoice_refno']
578 $sqlBindArray = array();
579 $query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " .
580 "d.name, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno " .
581 "FROM drug_sales AS s " .
582 "JOIN drugs AS d ON d.drug_id = s.drug_id " .
583 "JOIN form_encounter AS fe ON " .
584 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
585 "fe.date >= ? AND fe.date <= ? " .
586 "WHERE s.fee != 0";
587 array_push($sqlBindArray, $from_date, $to_date);
588 // If a facility was specified.
589 if ($form_facility) {
590 $query .= " AND fe.facility_id = ?";
591 array_push($sqlBindArray, $form_facility);
594 if ($form_provider) {
595 $query .= " AND fe.provider_id = ?";
596 array_push($sqlBindArray, $form_provider);
599 $query .= " ORDER BY d.name, fe.date, fe.id";
601 $res = sqlStatement($query, $sqlBindArray);
602 while ($row = sqlFetchArray($res)) {
603 thisLineItem(
604 $row['pid'],
605 $row['encounter'],
606 xl('Products'),
607 $row['name'],
608 substr($row['date'], 0, 10),
609 $row['quantity'],
610 $row['fee'],
611 $row['invoice_refno']
615 if ($_POST['form_csvexport']) {
616 if (! $_POST['form_details']) {
617 echo '"' . display_desc($product) . '",';
618 echo '"' . $productqty . '",';
619 echo '"';
620 bucks($producttotal);
621 echo '"' . "\n";
623 } else {
626 <tr bgcolor="#ddddff">
627 <td class="detail">
628 <?php echo text(display_desc($catleft));
629 $catleft = " "; ?>
630 </td>
631 <td class="detail" colspan="3">
632 <?php
633 if ($_POST['form_details']) {
634 echo xlt('Total for') . ' ';
637 echo text(display_desc($product)); ?>
638 </td>
639 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
640 <td>
641 &nbsp;
642 </td>
643 <?php } ?>
644 <td align="right">
645 &nbsp;
646 </td>
647 <td align="right">
648 <?php echo text($productqty); ?>
649 </td>
650 <td align="right">
651 <?php text(bucks($producttotal)); ?>
652 </td>
653 </tr>
655 <tr bgcolor="#ffdddd">
656 <td class="detail">
657 &nbsp;
658 </td>
659 <td class="detail" colspan="3"><b>
660 <?php echo xlt('Total for category') . ' ';
661 echo text(display_desc($category)); ?>
662 </b></td>
663 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
664 <td>
665 &nbsp;
666 </td>
667 <?php } ?>
668 <td align="right">
669 &nbsp;
670 </td>
671 <td align="right"><b>
672 <?php echo text($catqty); ?>
673 </b></td>
674 <td align="right"><b>
675 <?php text(bucks($cattotal)); ?>
676 </b></td>
677 </tr>
679 <tr>
680 <td class="detail" colspan="4"><b>
681 <?php echo xlt('Grand Total'); ?>
682 </b></td>
683 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
684 <td>
685 &nbsp;
686 </td>
687 <?php } ?>
688 <td align="right">
689 &nbsp;
690 </td>
691 <td align="right"><b>
692 <?php echo text($grandqty); ?>
693 </b></td>
694 <td align="right"><b>
695 <?php text(bucks($grandtotal)); ?>
696 </b></td>
697 </tr>
698 <?php $report_from_date = oeFormatShortDate($form_from_date) ;
699 $report_to_date = oeFormatShortDate($form_to_date) ;
701 <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>
702 <?php
703 } // End not csv export
706 if (! $_POST['form_csvexport']) {
707 if ($_POST['form_refresh']) {
710 </tbody>
711 </table>
712 </div> <!-- report results -->
713 <?php
714 } else { ?>
715 <div class='text'>
716 <?php echo xlt('Please input search criteria above, and click Submit to view results.'); ?>
717 </div>
718 <?php
719 } ?>
721 </form>
723 </body>
725 </html>
726 <?php
727 } // End not csv export