More complete support for activity flag in list_options table. (#274)
[openemr.git] / interface / reports / sales_by_item.php
blob2dd605e189cecc65466a4eaeb2379189b32f5bb1
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>
8 * LICENSE: This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
19 * @package OpenEMR
20 * @author Rod Roark <rod@sunsetsystems.com>
21 * @author Terry Hill <terry@lillysystems.com>
22 * @link http://www.open-emr.org
25 $sanitize_all_escapes=true;
26 $fake_register_globals=false;
28 require_once("../globals.php");
29 require_once("$srcdir/patient.inc");
30 require_once("$srcdir/acl.inc");
31 require_once("$srcdir/formatting.inc.php");
32 require_once "$srcdir/options.inc.php";
33 require_once "$srcdir/formdata.inc.php";
35 $form_provider = $_POST['form_provider'];
36 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
37 $form_details = $_POST['form_details'] ? true : false;
39 else
41 $form_details = false;
43 function bucks($amount) {
44 if ($amount) echo oeFormatMoney($amount);
47 function display_desc($desc) {
48 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
49 $desc = $matches[1];
51 return $desc;
54 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)) $rowcat = xl('None');
72 $rowproduct = $description;
73 if (! $rowproduct) $rowproduct = xl('Unknown');
75 if ($product != $rowproduct || $category != $rowcat) {
76 if ($product) {
77 // Print product total.
78 if ($_POST['form_csvexport']) {
79 if (! $_POST['form_details']) {
80 echo '"' . display_desc($category) . '",';
81 echo '"' . display_desc($product) . '",';
82 echo '"' . $productqty . '",';
83 echo '"'; bucks($producttotal); echo '"' . "\n";
86 else {
88 <tr bgcolor="#ddddff">
89 <td class="detail">
90 <?php echo text(display_desc($catleft)); $catleft = " "; ?>
91 </td>
92 <td class="detail" colspan="3">
93 <?php if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?>
94 </td>
95 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
96 <td>
97 &nbsp;
98 </td>
99 <?php } ?>
100 <td align="right">
101 &nbsp;
102 </td>
103 <td align="right">
104 <?php echo text($productqty); ?>
105 </td>
106 <td align="right">
107 <?php text(bucks($producttotal)); ?>
108 </td>
109 </tr>
110 <?php
111 } // End not csv export
113 $producttotal = 0;
114 $productqty = 0;
115 $product = $rowproduct;
116 $productleft = $product;
119 if ($category != $rowcat) {
120 if ($category) {
121 // Print category total.
122 if (!$_POST['form_csvexport']) {
125 <tr bgcolor="#ffdddd">
126 <td class="detail">
127 &nbsp;
128 </td>
129 <td class="detail" colspan="3">
130 <?php echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?>
131 </td>
132 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
133 <td>
134 &nbsp;
135 </td>
136 <?php } ?>
137 <td align="right">
138 &nbsp;
139 </td>
140 <td align="right">
141 <?php echo text($catqty); ?>
142 </td>
143 <td align="right">
144 <?php text(bucks($cattotal)); ?>
145 </td>
146 </tr>
147 <?php
148 } // End not csv export
150 $cattotal = 0;
151 $catqty = 0;
152 $category = $rowcat;
153 $catleft = $category;
156 if ($_POST['form_details']) {
157 if ($_POST['form_csvexport']) {
158 echo '"' . display_desc($category ) . '",';
159 echo '"' . display_desc($product ) . '",';
160 echo '"' . oeFormatShortDate(display_desc($transdate)) . '",';
161 if($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2 ) {
162 echo '"' . $pat_name . '",';
164 if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {
165 echo '"' . display_desc($invnumber) . '",';
167 if($GLOBALS['sales_report_invoice'] == 1) {
168 echo '"' . $patient_id . '",';
170 // echo '"' . display_desc($invnumber) . '",';
171 echo '"' . display_desc($qty ) . '",';
172 echo '"'; bucks($rowamount); echo '"' . "\n";
174 else {
177 <tr>
178 <td class="detail">
179 <?php echo text(display_desc($catleft)); $catleft = " "; ?>
180 </td>
181 <td class="detail">
182 <?php echo text(display_desc($productleft)); $productleft = " "; ?>
183 </td>
184 <td>
185 <?php echo text(oeFormatShortDate($transdate)); ?>
186 </td>
187 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
188 <td>
189 &nbsp;
190 </td>
191 <?php } if($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2 ) { ?>
192 <td>
193 <?php echo text($pat_name); ?>
194 </td>
195 <?php } ?>
196 <td class="detail">
197 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) { ?>
198 <a href='../patient_file/pos_checkout.php?ptid=<?php echo attr($patient_id); ?>&enc=<?php echo attr($encounter_id); ?>'>
199 <?php echo text($invnumber); ?></a>
200 <?php }
201 if($GLOBALS['sales_report_invoice'] == 1 ) {
202 echo text($patient_id);
205 </td>
206 <?php if($GLOBALS['sales_report_invoice'] == 0) {?>
207 <td>
208 &nbsp;
209 </td>
210 <?php } ?>
211 <td align="right">
212 <?php echo text($qty); ?>
213 </td>
214 <td align="right">
215 <?php text(bucks($rowamount)); ?>
216 </td>
217 </tr>
218 <?php
220 } // End not csv export
221 } // end details
222 $producttotal += $rowamount;
223 $cattotal += $rowamount;
224 $grandtotal += $rowamount;
225 $productqty += $qty;
226 $catqty += $qty;
227 $grandqty += $qty;
228 } // end function
230 if (! acl_check('acct', 'rep')) die(xl("Unauthorized access."));
233 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
234 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
235 $form_facility = $_POST['form_facility'];
237 if ($_POST['form_csvexport']) {
238 header("Pragma: public");
239 header("Expires: 0");
240 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
241 header("Content-Type: application/force-download");
242 header("Content-Disposition: attachment; filename=sales_by_item.csv");
243 header("Content-Description: File Transfer");
244 // CSV headers:
245 if ($_POST['form_details']) {
246 echo '"Category",';
247 echo '"Item",';
248 echo '"Date",';
249 if($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2 ) {
250 echo '"Name",';
252 if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {
253 echo '"Invoice",';
255 if($GLOBALS['sales_report_invoice'] == 1) {
256 echo '"ID",';
258 echo '"Qty",';
259 echo '"Amount"' . "\n";
261 else {
262 echo '"Category",';
263 echo '"Item",';
264 echo '"Qty",';
265 echo '"Total"' . "\n";
267 } // end export
268 else {
270 <html>
271 <head>
272 <?php html_header_show();?>
273 <style type="text/css">
275 /* specifically include & exclude from printing */
276 @media print {
277 #report_parameters {
278 visibility: hidden;
279 display: none;
281 #report_parameters_daterange {
282 visibility: visible;
283 display: inline;
285 #report_results {
286 margin-top: 30px;
290 /* specifically exclude some from the screen */
291 @media screen {
292 #report_parameters_daterange {
293 visibility: hidden;
294 display: none;
298 table.mymaintable, table.mymaintable td {
299 border: 1px solid #aaaaaa;
300 border-collapse: collapse;
302 table.mymaintable td {
303 padding: 1pt 4pt 1pt 4pt;
306 </style>
308 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-9-1/index.js"></script>
309 <script type="text/javascript" src="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script>
311 <script language="JavaScript">
313 $(document).ready(function() {
314 oeFixedHeaderSetup(document.getElementById('mymaintable'));
315 var win = top.printLogSetup ? top : opener.top;
316 win.printLogSetup(document.getElementById('printbutton'));
319 </script>
321 </head>
323 <title><?php echo xlt('Sales by Item') ?></title>
325 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' class="body_top">
327 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Sales by Item'); ?></span>
329 <form method='post' action='sales_by_item.php' id='theform'>
331 <div id="report_parameters">
332 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
333 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
334 <table>
335 <tr>
336 <td width='630px'>
337 <div style='float:left'>
338 <table class='text'>
339 <tr>
340 <td class='label'>
341 <?php echo xlt('Facility'); ?>:
342 </td>
343 <td>
344 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
345 </td>
346 <td class='label'>
347 <?php echo xlt('From'); ?>:
348 </td>
349 <td>
350 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr($form_from_date) ?>'
351 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
352 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
353 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
354 title='<?php echo xla('Click here to choose a date'); ?>'>
355 </td>
356 <td class='label'>
357 <?php echo xlt('To'); ?>:
358 </td>
359 <td>
360 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>'
361 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
362 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
363 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
364 title='<?php echo xla('Click here to choose a date'); ?>'>
365 </td>
366 </tr>
367 </table>
368 <table class='text'>
369 <tr>
370 <td class='label'>
371 <?php echo xlt('Provider'); ?>:
372 </td>
373 <td>
374 <?php
375 if (acl_check('acct', 'rep_a')) {
376 // Build a drop-down list of providers.
377 $query = "select id, lname, fname from users where " .
378 "authorized = 1 order by lname, fname";
379 $res = sqlStatement($query);
380 echo " &nbsp;<select name='form_provider'>\n";
381 echo " <option value=''>-- " . xlt('All Providers') . " --\n";
382 while ($row = sqlFetchArray($res)) {
383 $provid = $row['id'];
384 echo " <option value='". attr($provid) ."'";
385 if ($provid == $_REQUEST['form_provider']) echo " selected";
386 echo ">" . text($row['lname']) . ", " . text($row['fname']) . "\n";
388 echo " </select>\n";
389 } else {
390 echo "<input type='hidden' name='form_provider' value='" . attr($_SESSION['authUserID']) . "'>";
393 &nbsp;
394 </td>
395 <td>
396 <label><input type='checkbox' name='form_details'<?php if ($form_details) echo ' checked'; ?>>
397 <?php echo xlt('Details'); ?></label>
398 </td>
399 </tr>
400 </table>
401 </div>
403 </td>
404 <td align='left' valign='middle' height="100%">
405 <table style='border-left:1px solid; width:100%; height:100%' >
406 <tr>
407 <td>
408 <div style='margin-left:15px'>
409 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
410 <span>
411 <?php echo xlt('Submit'); ?>
412 </span>
413 </a>
415 <?php if ($_POST['form_refresh'] || $_POST['form_csvexport']) { ?>
416 <a href='#' class='css_button' id='printbutton'>
417 <span>
418 <?php echo xlt('Print'); ?>
419 </span>
420 </a>
421 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
422 <span>
423 <?php echo xlt('CSV Export'); ?>
424 </span>
425 </a>
426 <?php } ?>
427 </div>
428 </td>
429 </tr>
430 </table>
431 </td>
432 </tr>
433 </table>
435 </div> <!-- end of parameters -->
437 <?php
438 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
441 <div id="report_results">
442 <table width='98%' id='mymaintable' class='mymaintable'>
443 <thead>
444 <th>
445 <?php echo xlt('Category'); ?>
446 </th>
447 <th>
448 <?php echo xlt('Item'); ?>
449 </th>
450 <th>
451 <?php if ($form_details) echo xlt('Date'); ?>
452 </th>
453 <?php if($GLOBALS['sales_report_invoice'] == 2) {?>
454 <th>
455 &nbsp;
456 </th>
457 <?php } ?>
458 <th>
459 <?php
460 if($GLOBALS['sales_report_invoice'] == 0) {
461 if ($form_details) echo ' ';
463 </th>
464 <th>
465 <?php
466 if ($form_details) echo xlt('Invoice'); }
467 if($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2 ) {
468 if ($form_details) echo xlt('Name');
469 } ?>
470 </th>
471 <th>
472 <?php
473 if($GLOBALS['sales_report_invoice'] == 2) {
474 if ($form_details) echo xlt('Invoice');
476 if($GLOBALS['sales_report_invoice'] == 1) {
477 if ($form_details) echo xlt('ID');
480 </th>
481 <th align="right">
482 <?php echo xlt('Qty'); ?>
483 </th>
484 <th align="right">
485 <?php echo xlt('Amount'); ?>
486 </th>
487 </thead>
488 <tbody>
489 <?php
490 } // end not export
493 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
494 $from_date = $form_from_date . ' 00:00:00';
495 $to_date = $form_to_date . ' 23:59:59';
496 $category = "";
497 $catleft = "";
498 $cattotal = 0;
499 $catqty = 0;
500 $product = "";
501 $productleft = "";
502 $producttotal = 0;
503 $productqty = 0;
504 $grandtotal = 0;
505 $grandqty = 0;
507 $sqlBindArray = array();
508 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.units, " .
509 "b.code_text, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno, lo.title " .
510 "FROM billing AS b " .
511 "JOIN code_types AS ct ON ct.ct_key = b.code_type " .
512 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
513 "LEFT JOIN codes AS c ON c.code_type = ct.ct_id AND c.code = b.code AND c.modifier = b.modifier " .
514 "LEFT JOIN list_options AS lo ON lo.list_id = 'superbill' AND lo.option_id = c.superbill AND lo.activity = 1 " .
515 "WHERE b.code_type != 'COPAY' AND b.activity = 1 AND b.fee != 0 AND " .
516 "fe.date >= ? AND fe.date <= ?";
517 array_push($sqlBindArray,$from_date,$to_date);
518 // If a facility was specified.
519 if ($form_facility) {
520 $query .= " AND fe.facility_id = ?";
521 array_push($sqlBindArray,$form_facility);
523 if ($form_provider) {
524 $query .= " AND fe.provider_id = ?";
525 array_push($sqlBindArray,$form_provider);
527 $query .= " ORDER BY lo.title, b.code, fe.date, fe.id";
529 $res = sqlStatement($query,$sqlBindArray);
530 while ($row = sqlFetchArray($res)) {
531 thisLineItem($row['pid'], $row['encounter'],
532 $row['title'], $row['code'] . ' ' . $row['code_text'],
533 substr($row['date'], 0, 10), $row['units'], $row['fee'], $row['invoice_refno']);
536 $sqlBindArray = array();
537 $query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " .
538 "d.name, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno " .
539 "FROM drug_sales AS s " .
540 "JOIN drugs AS d ON d.drug_id = s.drug_id " .
541 "JOIN form_encounter AS fe ON " .
542 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
543 "fe.date >= ? AND fe.date <= ? " .
544 "WHERE s.fee != 0";
545 array_push($sqlBindArray,$from_date,$to_date);
546 // If a facility was specified.
547 if ($form_facility) {
548 $query .= " AND fe.facility_id = ?";
549 array_push($sqlBindArray,$form_facility);
551 if ($form_provider) {
552 $query .= " AND fe.provider_id = ?";
553 array_push($sqlBindArray,$form_provider);
555 $query .= " ORDER BY d.name, fe.date, fe.id";
557 $res = sqlStatement($query,$sqlBindArray);
558 while ($row = sqlFetchArray($res)) {
559 thisLineItem($row['pid'], $row['encounter'], xl('Products'), $row['name'],
560 substr($row['date'], 0, 10), $row['quantity'], $row['fee'], $row['invoice_refno']);
563 if ($_POST['form_csvexport']) {
564 if (! $_POST['form_details']) {
565 echo '"' . display_desc($product) . '",';
566 echo '"' . $productqty . '",';
567 echo '"'; bucks($producttotal); echo '"' . "\n";
570 else {
573 <tr bgcolor="#ddddff">
574 <td class="detail">
575 <?php echo text(display_desc($catleft)); $catleft = " "; ?>
576 </td>
577 <td class="detail" colspan="3">
578 <?php if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?>
579 </td>
580 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
581 <td>
582 &nbsp;
583 </td>
584 <?php } ?>
585 <td align="right">
586 &nbsp;
587 </td>
588 <td align="right">
589 <?php echo text($productqty); ?>
590 </td>
591 <td align="right">
592 <?php text(bucks($producttotal)); ?>
593 </td>
594 </tr>
596 <tr bgcolor="#ffdddd">
597 <td class="detail">
598 &nbsp;
599 </td>
600 <td class="detail" colspan="3"><b>
601 <?php echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?>
602 </b></td>
603 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
604 <td>
605 &nbsp;
606 </td>
607 <?php } ?>
608 <td align="right">
609 &nbsp;
610 </td>
611 <td align="right"><b>
612 <?php echo text($catqty); ?>
613 </b></td>
614 <td align="right"><b>
615 <?php text(bucks($cattotal)); ?>
616 </b></td>
617 </tr>
619 <tr>
620 <td class="detail" colspan="4"><b>
621 <?php echo xlt('Grand Total'); ?>
622 </b></td>
623 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
624 <td>
625 &nbsp;
626 </td>
627 <?php } ?>
628 <td align="right">
629 &nbsp;
630 </td>
631 <td align="right"><b>
632 <?php echo text($grandqty); ?>
633 </b></td>
634 <td align="right"><b>
635 <?php text(bucks($grandtotal)); ?>
636 </b></td>
637 </tr>
638 <?php $report_from_date = oeFormatShortDate($form_from_date) ;
639 $report_to_date = oeFormatShortDate($form_to_date) ;
641 <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>
642 <?php
644 } // End not csv export
647 if (! $_POST['form_csvexport']) {
648 if($_POST['form_refresh']){
651 </tbody>
652 </table>
653 </div> <!-- report results -->
654 <?php } else { ?>
655 <div class='text'>
656 <?php echo xlt('Please input search criteria above, and click Submit to view results.' ); ?>
657 </div>
658 <?php } ?>
660 </form>
662 </body>
664 <!-- stuff for the popup calendar -->
665 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
666 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
667 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
668 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
669 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
671 <script language="Javascript">
672 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
673 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
674 </script>
676 </html>
677 <?php
678 } // End not csv export