ongoing internationalization of date widget
[openemr.git] / interface / reports / sales_by_item.php
blob89a3a0a060d6826da029e391fcbb53304da228a9
1 <?php
2 /**
3 * This is a report of sales by item description.
5 * Copyright (C) 2015-2016 Terry Hill <terry@lillysystems.com>
6 * Copyright (C) 2006-2016 Rod Roark <rod@sunsetsystems.com>
7 * Copyright (C) 2017 Brady Miller <brady.g.miller@gmail.com>
9 * LICENSE: This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
20 * @package OpenEMR
21 * @author Rod Roark <rod@sunsetsystems.com>
22 * @author Terry Hill <terry@lillysystems.com>
23 * @author Brady Miller <brady.g.miller@gmail.com>
24 * @link http://www.open-emr.org
29 use OpenEMR\Core\Header;
31 require_once("../globals.php");
32 require_once("$srcdir/patient.inc");
33 require_once("$srcdir/acl.inc");
34 require_once "$srcdir/options.inc.php";
36 $form_provider = $_POST['form_provider'];
37 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
38 $form_details = $_POST['form_details'] ? true : false;
39 } else {
40 $form_details = false;
43 function bucks($amount)
45 if ($amount) {
46 echo oeFormatMoney($amount);
50 function display_desc($desc)
52 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
53 $desc = $matches[1];
56 return $desc;
59 function thisLineItem($patient_id, $encounter_id, $rowcat, $description, $transdate, $qty, $amount, $irnumber = '')
61 global $product, $category, $producttotal, $productqty, $cattotal, $catqty, $grandtotal, $grandqty;
62 global $productleft, $catleft;
64 $invnumber = $irnumber ? $irnumber : "$patient_id.$encounter_id";
65 $rowamount = sprintf('%01.2f', $amount);
67 $patdata = sqlQuery("SELECT " .
68 "p.fname, p.mname, p.lname, p.pubpid, p.DOB, " .
69 "p.street, p.city, p.state, p.postal_code, " .
70 "p.ss, p.sex, p.status, p.phone_home, " .
71 "p.phone_biz, p.phone_cell, p.hipaa_notice " .
72 "FROM patient_data AS p " .
73 "WHERE p.pid = ? LIMIT 1", array($patient_id));
75 $pat_name = $patdata['fname'] . ' ' . $patdata['mname'] . ' ' . $patdata['lname'];
77 if (empty($rowcat)) {
78 $rowcat = xl('None');
81 $rowproduct = $description;
82 if (! $rowproduct) {
83 $rowproduct = xl('Unknown');
86 if ($product != $rowproduct || $category != $rowcat) {
87 if ($product) {
88 // Print product total.
89 if ($_POST['form_csvexport']) {
90 if (! $_POST['form_details']) {
91 echo '"' . display_desc($category) . '",';
92 echo '"' . display_desc($product) . '",';
93 echo '"' . $productqty . '",';
94 echo '"';
95 bucks($producttotal);
96 echo '"' . "\n";
98 } else {
100 <tr bgcolor="#ddddff">
101 <td class="detail">
102 <?php echo text(display_desc($catleft));
103 $catleft = " "; ?>
104 </td>
105 <td class="detail" colspan="3">
106 <?php if ($_POST['form_details']) {
107 echo xlt('Total for') . ' ';
110 echo text(display_desc($product)); ?>
111 </td>
112 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
113 <td>
114 &nbsp;
115 </td>
116 <?php } ?>
117 <td align="right">
118 &nbsp;
119 </td>
120 <td align="right">
121 <?php echo text($productqty); ?>
122 </td>
123 <td align="right">
124 <?php text(bucks($producttotal)); ?>
125 </td>
126 </tr>
127 <?php
128 } // End not csv export
131 $producttotal = 0;
132 $productqty = 0;
133 $product = $rowproduct;
134 $productleft = $product;
137 if ($category != $rowcat) {
138 if ($category) {
139 // Print category total.
140 if (!$_POST['form_csvexport']) {
143 <tr bgcolor="#ffdddd">
144 <td class="detail">
145 &nbsp;
146 </td>
147 <td class="detail" colspan="3">
148 <?php echo xlt('Total for category') . ' ';
149 echo text(display_desc($category)); ?>
150 </td>
151 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
152 <td>
153 &nbsp;
154 </td>
155 <?php } ?>
156 <td align="right">
157 &nbsp;
158 </td>
159 <td align="right">
160 <?php echo text($catqty); ?>
161 </td>
162 <td align="right">
163 <?php text(bucks($cattotal)); ?>
164 </td>
165 </tr>
166 <?php
167 } // End not csv export
170 $cattotal = 0;
171 $catqty = 0;
172 $category = $rowcat;
173 $catleft = $category;
176 if ($_POST['form_details']) {
177 if ($_POST['form_csvexport']) {
178 echo '"' . display_desc($category) . '",';
179 echo '"' . display_desc($product) . '",';
180 echo '"' . oeFormatShortDate(display_desc($transdate)) . '",';
181 if ($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2) {
182 echo '"' . $pat_name . '",';
185 if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {
186 echo '"' . display_desc($invnumber) . '",';
189 if ($GLOBALS['sales_report_invoice'] == 1) {
190 echo '"' . $patient_id . '",';
193 // echo '"' . display_desc($invnumber) . '",';
194 echo '"' . display_desc($qty) . '",';
195 echo '"';
196 bucks($rowamount);
197 echo '"' . "\n";
198 } else {
201 <tr>
202 <td class="detail">
203 <?php echo text(display_desc($catleft));
204 $catleft = " "; ?>
205 </td>
206 <td class="detail">
207 <?php echo text(display_desc($productleft));
208 $productleft = " "; ?>
209 </td>
210 <td>
211 <?php echo text(oeFormatShortDate($transdate)); ?>
212 </td>
213 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
214 <td>
215 &nbsp;
216 </td>
217 <?php } if ($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2) { ?>
218 <td>
219 <?php echo text($pat_name); ?>
220 </td>
221 <?php } ?>
222 <td class="detail">
223 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) { ?>
224 <a href='../patient_file/pos_checkout.php?ptid=<?php echo attr($patient_id); ?>&enc=<?php echo attr($encounter_id); ?>'>
225 <?php echo text($invnumber); ?></a>
226 <?php }
228 if ($GLOBALS['sales_report_invoice'] == 1) {
229 echo text($patient_id);
232 </td>
233 <?php if ($GLOBALS['sales_report_invoice'] == 0) {?>
234 <td>
235 &nbsp;
236 </td>
237 <?php } ?>
238 <td align="right">
239 <?php echo text($qty); ?>
240 </td>
241 <td align="right">
242 <?php text(bucks($rowamount)); ?>
243 </td>
244 </tr>
245 <?php
246 } // End not csv export
247 } // end details
248 $producttotal += $rowamount;
249 $cattotal += $rowamount;
250 $grandtotal += $rowamount;
251 $productqty += $qty;
252 $catqty += $qty;
253 $grandqty += $qty;
254 } // end function
256 if (! acl_check('acct', 'rep')) {
257 die(xl("Unauthorized access."));
261 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
262 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
263 $form_facility = $_POST['form_facility'];
265 if ($_POST['form_csvexport']) {
266 header("Pragma: public");
267 header("Expires: 0");
268 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
269 header("Content-Type: application/force-download");
270 header("Content-Disposition: attachment; filename=sales_by_item.csv");
271 header("Content-Description: File Transfer");
272 // CSV headers:
273 if ($_POST['form_details']) {
274 echo '"Category",';
275 echo '"Item",';
276 echo '"Date",';
277 if ($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2) {
278 echo '"Name",';
281 if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {
282 echo '"Invoice",';
285 if ($GLOBALS['sales_report_invoice'] == 1) {
286 echo '"ID",';
289 echo '"Qty",';
290 echo '"Amount"' . "\n";
291 } else {
292 echo '"Category",';
293 echo '"Item",';
294 echo '"Qty",';
295 echo '"Total"' . "\n";
297 } // end export
298 else {
300 <html>
301 <head>
303 <?php Header::setupHeader(['datetime-picker', 'report-helper']); ?>
305 <style type="text/css">
307 /* specifically include & exclude from printing */
308 @media print {
309 #report_parameters {
310 visibility: hidden;
311 display: none;
313 #report_parameters_daterange {
314 visibility: visible;
315 display: inline;
317 #report_results {
318 margin-top: 30px;
322 /* specifically exclude some from the screen */
323 @media screen {
324 #report_parameters_daterange {
325 visibility: hidden;
326 display: none;
330 table.mymaintable, table.mymaintable td {
331 border: 1px solid #aaaaaa;
332 border-collapse: collapse;
334 table.mymaintable td {
335 padding: 1pt 4pt 1pt 4pt;
338 </style>
340 <script language="JavaScript">
342 $(document).ready(function() {
343 oeFixedHeaderSetup(document.getElementById('mymaintable'));
344 var win = top.printLogSetup ? top : opener.top;
345 win.printLogSetup(document.getElementById('printbutton'));
347 $('.datepicker').datetimepicker({
348 <?php $datetimepicker_timepicker = false; ?>
349 <?php $datetimepicker_showseconds = false; ?>
350 <?php $datetimepicker_formatInput = false; ?>
351 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
352 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
356 </script>
358 </head>
360 <title><?php echo xlt('Sales by Item') ?></title>
362 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' class="body_top">
364 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Sales by Item'); ?></span>
366 <form method='post' action='sales_by_item.php' id='theform'>
368 <div id="report_parameters">
369 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
370 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
371 <table>
372 <tr>
373 <td width='630px'>
374 <div style='float:left'>
375 <table class='text'>
376 <tr>
377 <td class='control-label'>
378 <?php echo xlt('Facility'); ?>:
379 </td>
380 <td>
381 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
382 </td>
383 <td class='control-label'>
384 <?php echo xlt('From'); ?>:
385 </td>
386 <td>
387 <input type='text' class='datepicker form-control' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr($form_from_date) ?>'
388 title='yyyy-mm-dd'>
389 </td>
390 <td class='control-label'>
391 <?php echo xlt('To'); ?>:
392 </td>
393 <td>
394 <input type='text' class='datepicker form-control' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>'
395 title='yyyy-mm-dd'>
396 </td>
397 </tr>
398 </table>
399 <table class='text'>
400 <tr>
401 <td class='control-label'>
402 <?php echo xlt('Provider'); ?>:
403 </td>
404 <td>
405 <?php
406 if (acl_check('acct', 'rep_a')) {
407 // Build a drop-down list of providers.
408 $query = "select id, lname, fname from users where " .
409 "authorized = 1 order by lname, fname";
410 $res = sqlStatement($query);
411 echo " &nbsp;<select name='form_provider' class='form-control'>\n";
412 echo " <option value=''>-- " . xlt('All Providers') . " --\n";
413 while ($row = sqlFetchArray($res)) {
414 $provid = $row['id'];
415 echo " <option value='". attr($provid) ."'";
416 if ($provid == $_REQUEST['form_provider']) {
417 echo " selected";
420 echo ">" . text($row['lname']) . ", " . text($row['fname']) . "\n";
423 echo " </select>\n";
424 } else {
425 echo "<input type='hidden' name='form_provider' value='" . attr($_SESSION['authUserID']) . "'>";
428 &nbsp;
429 </td>
430 <td>
431 <div class='checkbox'>
432 <label><input type='checkbox' name='form_details'<?php if ($form_details) {
433 echo ' checked';
434 } ?>>
435 <?php echo xlt('Details'); ?></label>
436 </div>
437 </td>
438 </tr>
439 </table>
440 </div>
442 </td>
443 <td align='left' valign='middle' height="100%">
444 <table style='border-left:1px solid; width:100%; height:100%' >
445 <tr>
446 <td>
447 <div class="text-center">
448 <div class="btn-group" role="group">
449 <a href='#' class='btn btn-default btn-save' onclick='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
450 <?php echo xlt('Submit'); ?>
451 </a>
452 <?php if ($_POST['form_refresh'] || $_POST['form_csvexport']) { ?>
453 <a href='#' class='btn btn-default btn-print' id='printbutton'>
454 <?php echo xlt('Print'); ?>
455 </a>
456 <a href='#' class='btn btn-default btn-transmit' onclick='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
457 <?php echo xlt('CSV Export'); ?>
458 </a>
459 <?php } ?>
460 </div>
461 </div>
462 </td>
463 </tr>
464 </table>
465 </td>
466 </tr>
467 </table>
469 </div> <!-- end of parameters -->
471 <?php
472 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
475 <div id="report_results">
476 <table width='98%' id='mymaintable' class='mymaintable'>
477 <thead>
478 <th>
479 <?php echo xlt('Category'); ?>
480 </th>
481 <th>
482 <?php echo xlt('Item'); ?>
483 </th>
484 <th>
485 <?php if ($form_details) {
486 echo xlt('Date');
487 } ?>
488 </th>
489 <?php if ($GLOBALS['sales_report_invoice'] == 2) {?>
490 <th>
491 &nbsp;
492 </th>
493 <?php } ?>
494 <th>
495 <?php
496 if ($GLOBALS['sales_report_invoice'] == 0) {
497 if ($form_details) {
498 echo ' ';
501 </th>
502 <th>
503 <?php
504 if ($form_details) {
505 echo xlt('Invoice');
509 if ($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2) {
510 if ($form_details) {
511 echo xlt('Name');
513 } ?>
514 </th>
515 <th>
516 <?php
517 if ($GLOBALS['sales_report_invoice'] == 2) {
518 if ($form_details) {
519 echo xlt('Invoice');
523 if ($GLOBALS['sales_report_invoice'] == 1) {
524 if ($form_details) {
525 echo xlt('ID');
529 </th>
530 <th align="right">
531 <?php echo xlt('Qty'); ?>
532 </th>
533 <th align="right">
534 <?php echo xlt('Amount'); ?>
535 </th>
536 </thead>
537 <tbody>
538 <?php
539 } // end not export
542 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
543 $from_date = $form_from_date . ' 00:00:00';
544 $to_date = $form_to_date . ' 23:59:59';
545 $category = "";
546 $catleft = "";
547 $cattotal = 0;
548 $catqty = 0;
549 $product = "";
550 $productleft = "";
551 $producttotal = 0;
552 $productqty = 0;
553 $grandtotal = 0;
554 $grandqty = 0;
556 $sqlBindArray = array();
557 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.units, " .
558 "b.code_text, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno, lo.title " .
559 "FROM billing AS b " .
560 "JOIN code_types AS ct ON ct.ct_key = b.code_type " .
561 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
562 "LEFT JOIN codes AS c ON c.code_type = ct.ct_id AND c.code = b.code AND c.modifier = b.modifier " .
563 "LEFT JOIN list_options AS lo ON lo.list_id = 'superbill' AND lo.option_id = c.superbill AND lo.activity = 1 " .
564 "WHERE b.code_type != 'COPAY' AND b.activity = 1 AND b.fee != 0 AND " .
565 "fe.date >= ? AND fe.date <= ?";
566 array_push($sqlBindArray, $from_date, $to_date);
567 // If a facility was specified.
568 if ($form_facility) {
569 $query .= " AND fe.facility_id = ?";
570 array_push($sqlBindArray, $form_facility);
573 if ($form_provider) {
574 $query .= " AND fe.provider_id = ?";
575 array_push($sqlBindArray, $form_provider);
578 $query .= " ORDER BY lo.title, b.code, fe.date, fe.id";
580 $res = sqlStatement($query, $sqlBindArray);
581 while ($row = sqlFetchArray($res)) {
582 thisLineItem(
583 $row['pid'],
584 $row['encounter'],
585 $row['title'],
586 $row['code'] . ' ' . $row['code_text'],
587 substr($row['date'], 0, 10),
588 $row['units'],
589 $row['fee'],
590 $row['invoice_refno']
595 $sqlBindArray = array();
596 $query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " .
597 "d.name, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno " .
598 "FROM drug_sales AS s " .
599 "JOIN drugs AS d ON d.drug_id = s.drug_id " .
600 "JOIN form_encounter AS fe ON " .
601 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
602 "fe.date >= ? AND fe.date <= ? " .
603 "WHERE s.fee != 0";
604 array_push($sqlBindArray, $from_date, $to_date);
605 // If a facility was specified.
606 if ($form_facility) {
607 $query .= " AND fe.facility_id = ?";
608 array_push($sqlBindArray, $form_facility);
611 if ($form_provider) {
612 $query .= " AND fe.provider_id = ?";
613 array_push($sqlBindArray, $form_provider);
616 $query .= " ORDER BY d.name, fe.date, fe.id";
618 $res = sqlStatement($query, $sqlBindArray);
619 while ($row = sqlFetchArray($res)) {
620 thisLineItem(
621 $row['pid'],
622 $row['encounter'],
623 xl('Products'),
624 $row['name'],
625 substr($row['date'], 0, 10),
626 $row['quantity'],
627 $row['fee'],
628 $row['invoice_refno']
632 if ($_POST['form_csvexport']) {
633 if (! $_POST['form_details']) {
634 echo '"' . display_desc($product) . '",';
635 echo '"' . $productqty . '",';
636 echo '"';
637 bucks($producttotal);
638 echo '"' . "\n";
640 } else {
643 <tr bgcolor="#ddddff">
644 <td class="detail">
645 <?php echo text(display_desc($catleft));
646 $catleft = " "; ?>
647 </td>
648 <td class="detail" colspan="3">
649 <?php if ($_POST['form_details']) {
650 echo xlt('Total for') . ' ';
653 echo text(display_desc($product)); ?>
654 </td>
655 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
656 <td>
657 &nbsp;
658 </td>
659 <?php } ?>
660 <td align="right">
661 &nbsp;
662 </td>
663 <td align="right">
664 <?php echo text($productqty); ?>
665 </td>
666 <td align="right">
667 <?php text(bucks($producttotal)); ?>
668 </td>
669 </tr>
671 <tr bgcolor="#ffdddd">
672 <td class="detail">
673 &nbsp;
674 </td>
675 <td class="detail" colspan="3"><b>
676 <?php echo xlt('Total for category') . ' ';
677 echo text(display_desc($category)); ?>
678 </b></td>
679 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
680 <td>
681 &nbsp;
682 </td>
683 <?php } ?>
684 <td align="right">
685 &nbsp;
686 </td>
687 <td align="right"><b>
688 <?php echo text($catqty); ?>
689 </b></td>
690 <td align="right"><b>
691 <?php text(bucks($cattotal)); ?>
692 </b></td>
693 </tr>
695 <tr>
696 <td class="detail" colspan="4"><b>
697 <?php echo xlt('Grand Total'); ?>
698 </b></td>
699 <?php if ($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
700 <td>
701 &nbsp;
702 </td>
703 <?php } ?>
704 <td align="right">
705 &nbsp;
706 </td>
707 <td align="right"><b>
708 <?php echo text($grandqty); ?>
709 </b></td>
710 <td align="right"><b>
711 <?php text(bucks($grandtotal)); ?>
712 </b></td>
713 </tr>
714 <?php $report_from_date = oeFormatShortDate($form_from_date) ;
715 $report_to_date = oeFormatShortDate($form_to_date) ;
717 <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>
718 <?php
719 } // End not csv export
722 if (! $_POST['form_csvexport']) {
723 if ($_POST['form_refresh']) {
726 </tbody>
727 </table>
728 </div> <!-- report results -->
729 <?php
730 } else { ?>
731 <div class='text'>
732 <?php echo xlt('Please input search criteria above, and click Submit to view results.'); ?>
733 </div>
734 <?php
735 } ?>
737 </form>
739 </body>
741 </html>
742 <?php
743 } // End not csv export