migrated ubiquitous libraries to composer autoloader (#421)
[openemr.git] / interface / reports / sales_by_item.php
blobce1f47a4b5e98733b9ec953fbd1024ec8fc2206b
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";
34 $form_provider = $_POST['form_provider'];
35 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
36 $form_details = $_POST['form_details'] ? true : false;
38 else
40 $form_details = false;
42 function bucks($amount) {
43 if ($amount) echo oeFormatMoney($amount);
46 function display_desc($desc) {
47 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
48 $desc = $matches[1];
50 return $desc;
53 function thisLineItem($patient_id, $encounter_id, $rowcat, $description, $transdate, $qty, $amount, $irnumber='') {
54 global $product, $category, $producttotal, $productqty, $cattotal, $catqty, $grandtotal, $grandqty;
55 global $productleft, $catleft;
57 $invnumber = $irnumber ? $irnumber : "$patient_id.$encounter_id";
58 $rowamount = sprintf('%01.2f', $amount);
60 $patdata = sqlQuery("SELECT " .
61 "p.fname, p.mname, p.lname, p.pubpid, p.DOB, " .
62 "p.street, p.city, p.state, p.postal_code, " .
63 "p.ss, p.sex, p.status, p.phone_home, " .
64 "p.phone_biz, p.phone_cell, p.hipaa_notice " .
65 "FROM patient_data AS p " .
66 "WHERE p.pid = ? LIMIT 1", array($patient_id));
68 $pat_name = $patdata['fname'] . ' ' . $patdata['mname'] . ' ' . $patdata['lname'];
70 if (empty($rowcat)) $rowcat = xl('None');
71 $rowproduct = $description;
72 if (! $rowproduct) $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 '"'; bucks($producttotal); echo '"' . "\n";
85 else {
87 <tr bgcolor="#ddddff">
88 <td class="detail">
89 <?php echo text(display_desc($catleft)); $catleft = " "; ?>
90 </td>
91 <td class="detail" colspan="3">
92 <?php if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?>
93 </td>
94 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
95 <td>
96 &nbsp;
97 </td>
98 <?php } ?>
99 <td align="right">
100 &nbsp;
101 </td>
102 <td align="right">
103 <?php echo text($productqty); ?>
104 </td>
105 <td align="right">
106 <?php text(bucks($producttotal)); ?>
107 </td>
108 </tr>
109 <?php
110 } // End not csv export
112 $producttotal = 0;
113 $productqty = 0;
114 $product = $rowproduct;
115 $productleft = $product;
118 if ($category != $rowcat) {
119 if ($category) {
120 // Print category total.
121 if (!$_POST['form_csvexport']) {
124 <tr bgcolor="#ffdddd">
125 <td class="detail">
126 &nbsp;
127 </td>
128 <td class="detail" colspan="3">
129 <?php echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?>
130 </td>
131 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
132 <td>
133 &nbsp;
134 </td>
135 <?php } ?>
136 <td align="right">
137 &nbsp;
138 </td>
139 <td align="right">
140 <?php echo text($catqty); ?>
141 </td>
142 <td align="right">
143 <?php text(bucks($cattotal)); ?>
144 </td>
145 </tr>
146 <?php
147 } // End not csv export
149 $cattotal = 0;
150 $catqty = 0;
151 $category = $rowcat;
152 $catleft = $category;
155 if ($_POST['form_details']) {
156 if ($_POST['form_csvexport']) {
157 echo '"' . display_desc($category ) . '",';
158 echo '"' . display_desc($product ) . '",';
159 echo '"' . oeFormatShortDate(display_desc($transdate)) . '",';
160 if($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2 ) {
161 echo '"' . $pat_name . '",';
163 if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {
164 echo '"' . display_desc($invnumber) . '",';
166 if($GLOBALS['sales_report_invoice'] == 1) {
167 echo '"' . $patient_id . '",';
169 // echo '"' . display_desc($invnumber) . '",';
170 echo '"' . display_desc($qty ) . '",';
171 echo '"'; bucks($rowamount); echo '"' . "\n";
173 else {
176 <tr>
177 <td class="detail">
178 <?php echo text(display_desc($catleft)); $catleft = " "; ?>
179 </td>
180 <td class="detail">
181 <?php echo text(display_desc($productleft)); $productleft = " "; ?>
182 </td>
183 <td>
184 <?php echo text(oeFormatShortDate($transdate)); ?>
185 </td>
186 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
187 <td>
188 &nbsp;
189 </td>
190 <?php } if($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2 ) { ?>
191 <td>
192 <?php echo text($pat_name); ?>
193 </td>
194 <?php } ?>
195 <td class="detail">
196 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) { ?>
197 <a href='../patient_file/pos_checkout.php?ptid=<?php echo attr($patient_id); ?>&enc=<?php echo attr($encounter_id); ?>'>
198 <?php echo text($invnumber); ?></a>
199 <?php }
200 if($GLOBALS['sales_report_invoice'] == 1 ) {
201 echo text($patient_id);
204 </td>
205 <?php if($GLOBALS['sales_report_invoice'] == 0) {?>
206 <td>
207 &nbsp;
208 </td>
209 <?php } ?>
210 <td align="right">
211 <?php echo text($qty); ?>
212 </td>
213 <td align="right">
214 <?php text(bucks($rowamount)); ?>
215 </td>
216 </tr>
217 <?php
219 } // End not csv export
220 } // end details
221 $producttotal += $rowamount;
222 $cattotal += $rowamount;
223 $grandtotal += $rowamount;
224 $productqty += $qty;
225 $catqty += $qty;
226 $grandqty += $qty;
227 } // end function
229 if (! acl_check('acct', 'rep')) die(xl("Unauthorized access."));
232 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
233 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
234 $form_facility = $_POST['form_facility'];
236 if ($_POST['form_csvexport']) {
237 header("Pragma: public");
238 header("Expires: 0");
239 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
240 header("Content-Type: application/force-download");
241 header("Content-Disposition: attachment; filename=sales_by_item.csv");
242 header("Content-Description: File Transfer");
243 // CSV headers:
244 if ($_POST['form_details']) {
245 echo '"Category",';
246 echo '"Item",';
247 echo '"Date",';
248 if($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2 ) {
249 echo '"Name",';
251 if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {
252 echo '"Invoice",';
254 if($GLOBALS['sales_report_invoice'] == 1) {
255 echo '"ID",';
257 echo '"Qty",';
258 echo '"Amount"' . "\n";
260 else {
261 echo '"Category",';
262 echo '"Item",';
263 echo '"Qty",';
264 echo '"Total"' . "\n";
266 } // end export
267 else {
269 <html>
270 <head>
271 <?php html_header_show();?>
272 <style type="text/css">
274 /* specifically include & exclude from printing */
275 @media print {
276 #report_parameters {
277 visibility: hidden;
278 display: none;
280 #report_parameters_daterange {
281 visibility: visible;
282 display: inline;
284 #report_results {
285 margin-top: 30px;
289 /* specifically exclude some from the screen */
290 @media screen {
291 #report_parameters_daterange {
292 visibility: hidden;
293 display: none;
297 table.mymaintable, table.mymaintable td {
298 border: 1px solid #aaaaaa;
299 border-collapse: collapse;
301 table.mymaintable td {
302 padding: 1pt 4pt 1pt 4pt;
305 </style>
307 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-9-1/index.js"></script>
308 <script type="text/javascript" src="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script>
310 <script language="JavaScript">
312 $(document).ready(function() {
313 oeFixedHeaderSetup(document.getElementById('mymaintable'));
314 var win = top.printLogSetup ? top : opener.top;
315 win.printLogSetup(document.getElementById('printbutton'));
318 </script>
320 </head>
322 <title><?php echo xlt('Sales by Item') ?></title>
324 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' class="body_top">
326 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Sales by Item'); ?></span>
328 <form method='post' action='sales_by_item.php' id='theform'>
330 <div id="report_parameters">
331 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
332 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
333 <table>
334 <tr>
335 <td width='630px'>
336 <div style='float:left'>
337 <table class='text'>
338 <tr>
339 <td class='label'>
340 <?php echo xlt('Facility'); ?>:
341 </td>
342 <td>
343 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
344 </td>
345 <td class='label'>
346 <?php echo xlt('From'); ?>:
347 </td>
348 <td>
349 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr($form_from_date) ?>'
350 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
351 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
352 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
353 title='<?php echo xla('Click here to choose a date'); ?>'>
354 </td>
355 <td class='label'>
356 <?php echo xlt('To'); ?>:
357 </td>
358 <td>
359 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>'
360 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
361 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
362 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
363 title='<?php echo xla('Click here to choose a date'); ?>'>
364 </td>
365 </tr>
366 </table>
367 <table class='text'>
368 <tr>
369 <td class='label'>
370 <?php echo xlt('Provider'); ?>:
371 </td>
372 <td>
373 <?php
374 if (acl_check('acct', 'rep_a')) {
375 // Build a drop-down list of providers.
376 $query = "select id, lname, fname from users where " .
377 "authorized = 1 order by lname, fname";
378 $res = sqlStatement($query);
379 echo " &nbsp;<select name='form_provider'>\n";
380 echo " <option value=''>-- " . xlt('All Providers') . " --\n";
381 while ($row = sqlFetchArray($res)) {
382 $provid = $row['id'];
383 echo " <option value='". attr($provid) ."'";
384 if ($provid == $_REQUEST['form_provider']) echo " selected";
385 echo ">" . text($row['lname']) . ", " . text($row['fname']) . "\n";
387 echo " </select>\n";
388 } else {
389 echo "<input type='hidden' name='form_provider' value='" . attr($_SESSION['authUserID']) . "'>";
392 &nbsp;
393 </td>
394 <td>
395 <label><input type='checkbox' name='form_details'<?php if ($form_details) echo ' checked'; ?>>
396 <?php echo xlt('Details'); ?></label>
397 </td>
398 </tr>
399 </table>
400 </div>
402 </td>
403 <td align='left' valign='middle' height="100%">
404 <table style='border-left:1px solid; width:100%; height:100%' >
405 <tr>
406 <td>
407 <div style='margin-left:15px'>
408 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
409 <span>
410 <?php echo xlt('Submit'); ?>
411 </span>
412 </a>
414 <?php if ($_POST['form_refresh'] || $_POST['form_csvexport']) { ?>
415 <a href='#' class='css_button' id='printbutton'>
416 <span>
417 <?php echo xlt('Print'); ?>
418 </span>
419 </a>
420 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
421 <span>
422 <?php echo xlt('CSV Export'); ?>
423 </span>
424 </a>
425 <?php } ?>
426 </div>
427 </td>
428 </tr>
429 </table>
430 </td>
431 </tr>
432 </table>
434 </div> <!-- end of parameters -->
436 <?php
437 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
440 <div id="report_results">
441 <table width='98%' id='mymaintable' class='mymaintable'>
442 <thead>
443 <th>
444 <?php echo xlt('Category'); ?>
445 </th>
446 <th>
447 <?php echo xlt('Item'); ?>
448 </th>
449 <th>
450 <?php if ($form_details) echo xlt('Date'); ?>
451 </th>
452 <?php if($GLOBALS['sales_report_invoice'] == 2) {?>
453 <th>
454 &nbsp;
455 </th>
456 <?php } ?>
457 <th>
458 <?php
459 if($GLOBALS['sales_report_invoice'] == 0) {
460 if ($form_details) echo ' ';
462 </th>
463 <th>
464 <?php
465 if ($form_details) echo xlt('Invoice'); }
466 if($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2 ) {
467 if ($form_details) echo xlt('Name');
468 } ?>
469 </th>
470 <th>
471 <?php
472 if($GLOBALS['sales_report_invoice'] == 2) {
473 if ($form_details) echo xlt('Invoice');
475 if($GLOBALS['sales_report_invoice'] == 1) {
476 if ($form_details) echo xlt('ID');
479 </th>
480 <th align="right">
481 <?php echo xlt('Qty'); ?>
482 </th>
483 <th align="right">
484 <?php echo xlt('Amount'); ?>
485 </th>
486 </thead>
487 <tbody>
488 <?php
489 } // end not export
492 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
493 $from_date = $form_from_date . ' 00:00:00';
494 $to_date = $form_to_date . ' 23:59:59';
495 $category = "";
496 $catleft = "";
497 $cattotal = 0;
498 $catqty = 0;
499 $product = "";
500 $productleft = "";
501 $producttotal = 0;
502 $productqty = 0;
503 $grandtotal = 0;
504 $grandqty = 0;
506 $sqlBindArray = array();
507 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.units, " .
508 "b.code_text, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno, lo.title " .
509 "FROM billing AS b " .
510 "JOIN code_types AS ct ON ct.ct_key = b.code_type " .
511 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
512 "LEFT JOIN codes AS c ON c.code_type = ct.ct_id AND c.code = b.code AND c.modifier = b.modifier " .
513 "LEFT JOIN list_options AS lo ON lo.list_id = 'superbill' AND lo.option_id = c.superbill AND lo.activity = 1 " .
514 "WHERE b.code_type != 'COPAY' AND b.activity = 1 AND b.fee != 0 AND " .
515 "fe.date >= ? AND fe.date <= ?";
516 array_push($sqlBindArray,$from_date,$to_date);
517 // If a facility was specified.
518 if ($form_facility) {
519 $query .= " AND fe.facility_id = ?";
520 array_push($sqlBindArray,$form_facility);
522 if ($form_provider) {
523 $query .= " AND fe.provider_id = ?";
524 array_push($sqlBindArray,$form_provider);
526 $query .= " ORDER BY lo.title, b.code, fe.date, fe.id";
528 $res = sqlStatement($query,$sqlBindArray);
529 while ($row = sqlFetchArray($res)) {
530 thisLineItem($row['pid'], $row['encounter'],
531 $row['title'], $row['code'] . ' ' . $row['code_text'],
532 substr($row['date'], 0, 10), $row['units'], $row['fee'], $row['invoice_refno']);
535 $sqlBindArray = array();
536 $query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " .
537 "d.name, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno " .
538 "FROM drug_sales AS s " .
539 "JOIN drugs AS d ON d.drug_id = s.drug_id " .
540 "JOIN form_encounter AS fe ON " .
541 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
542 "fe.date >= ? AND fe.date <= ? " .
543 "WHERE s.fee != 0";
544 array_push($sqlBindArray,$from_date,$to_date);
545 // If a facility was specified.
546 if ($form_facility) {
547 $query .= " AND fe.facility_id = ?";
548 array_push($sqlBindArray,$form_facility);
550 if ($form_provider) {
551 $query .= " AND fe.provider_id = ?";
552 array_push($sqlBindArray,$form_provider);
554 $query .= " ORDER BY d.name, fe.date, fe.id";
556 $res = sqlStatement($query,$sqlBindArray);
557 while ($row = sqlFetchArray($res)) {
558 thisLineItem($row['pid'], $row['encounter'], xl('Products'), $row['name'],
559 substr($row['date'], 0, 10), $row['quantity'], $row['fee'], $row['invoice_refno']);
562 if ($_POST['form_csvexport']) {
563 if (! $_POST['form_details']) {
564 echo '"' . display_desc($product) . '",';
565 echo '"' . $productqty . '",';
566 echo '"'; bucks($producttotal); echo '"' . "\n";
569 else {
572 <tr bgcolor="#ddddff">
573 <td class="detail">
574 <?php echo text(display_desc($catleft)); $catleft = " "; ?>
575 </td>
576 <td class="detail" colspan="3">
577 <?php if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?>
578 </td>
579 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
580 <td>
581 &nbsp;
582 </td>
583 <?php } ?>
584 <td align="right">
585 &nbsp;
586 </td>
587 <td align="right">
588 <?php echo text($productqty); ?>
589 </td>
590 <td align="right">
591 <?php text(bucks($producttotal)); ?>
592 </td>
593 </tr>
595 <tr bgcolor="#ffdddd">
596 <td class="detail">
597 &nbsp;
598 </td>
599 <td class="detail" colspan="3"><b>
600 <?php echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?>
601 </b></td>
602 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
603 <td>
604 &nbsp;
605 </td>
606 <?php } ?>
607 <td align="right">
608 &nbsp;
609 </td>
610 <td align="right"><b>
611 <?php echo text($catqty); ?>
612 </b></td>
613 <td align="right"><b>
614 <?php text(bucks($cattotal)); ?>
615 </b></td>
616 </tr>
618 <tr>
619 <td class="detail" colspan="4"><b>
620 <?php echo xlt('Grand Total'); ?>
621 </b></td>
622 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
623 <td>
624 &nbsp;
625 </td>
626 <?php } ?>
627 <td align="right">
628 &nbsp;
629 </td>
630 <td align="right"><b>
631 <?php echo text($grandqty); ?>
632 </b></td>
633 <td align="right"><b>
634 <?php text(bucks($grandtotal)); ?>
635 </b></td>
636 </tr>
637 <?php $report_from_date = oeFormatShortDate($form_from_date) ;
638 $report_to_date = oeFormatShortDate($form_to_date) ;
640 <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>
641 <?php
643 } // End not csv export
646 if (! $_POST['form_csvexport']) {
647 if($_POST['form_refresh']){
650 </tbody>
651 </table>
652 </div> <!-- report results -->
653 <?php } else { ?>
654 <div class='text'>
655 <?php echo xlt('Please input search criteria above, and click Submit to view results.' ); ?>
656 </div>
657 <?php } ?>
659 </form>
661 </body>
663 <!-- stuff for the popup calendar -->
664 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
665 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
666 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
667 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
668 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
670 <script language="Javascript">
671 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
672 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
673 </script>
675 </html>
676 <?php
677 } // End not csv export