new datepicker in encounter
[openemr.git] / interface / reports / sales_by_item.php
blob60f75ce6b9ac01e6bd266c9e89ac4cabcce8276f
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
27 $sanitize_all_escapes=true;
28 $fake_register_globals=false;
30 require_once("../globals.php");
31 require_once("$srcdir/patient.inc");
32 require_once("$srcdir/acl.inc");
33 require_once("$srcdir/formatting.inc.php");
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;
40 else
42 $form_details = false;
44 function bucks($amount) {
45 if ($amount) echo oeFormatMoney($amount);
48 function display_desc($desc) {
49 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
50 $desc = $matches[1];
52 return $desc;
55 function thisLineItem($patient_id, $encounter_id, $rowcat, $description, $transdate, $qty, $amount, $irnumber='') {
56 global $product, $category, $producttotal, $productqty, $cattotal, $catqty, $grandtotal, $grandqty;
57 global $productleft, $catleft;
59 $invnumber = $irnumber ? $irnumber : "$patient_id.$encounter_id";
60 $rowamount = sprintf('%01.2f', $amount);
62 $patdata = sqlQuery("SELECT " .
63 "p.fname, p.mname, p.lname, p.pubpid, p.DOB, " .
64 "p.street, p.city, p.state, p.postal_code, " .
65 "p.ss, p.sex, p.status, p.phone_home, " .
66 "p.phone_biz, p.phone_cell, p.hipaa_notice " .
67 "FROM patient_data AS p " .
68 "WHERE p.pid = ? LIMIT 1", array($patient_id));
70 $pat_name = $patdata['fname'] . ' ' . $patdata['mname'] . ' ' . $patdata['lname'];
72 if (empty($rowcat)) $rowcat = xl('None');
73 $rowproduct = $description;
74 if (! $rowproduct) $rowproduct = xl('Unknown');
76 if ($product != $rowproduct || $category != $rowcat) {
77 if ($product) {
78 // Print product total.
79 if ($_POST['form_csvexport']) {
80 if (! $_POST['form_details']) {
81 echo '"' . display_desc($category) . '",';
82 echo '"' . display_desc($product) . '",';
83 echo '"' . $productqty . '",';
84 echo '"'; bucks($producttotal); echo '"' . "\n";
87 else {
89 <tr bgcolor="#ddddff">
90 <td class="detail">
91 <?php echo text(display_desc($catleft)); $catleft = " "; ?>
92 </td>
93 <td class="detail" colspan="3">
94 <?php if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?>
95 </td>
96 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
97 <td>
98 &nbsp;
99 </td>
100 <?php } ?>
101 <td align="right">
102 &nbsp;
103 </td>
104 <td align="right">
105 <?php echo text($productqty); ?>
106 </td>
107 <td align="right">
108 <?php text(bucks($producttotal)); ?>
109 </td>
110 </tr>
111 <?php
112 } // End not csv export
114 $producttotal = 0;
115 $productqty = 0;
116 $product = $rowproduct;
117 $productleft = $product;
120 if ($category != $rowcat) {
121 if ($category) {
122 // Print category total.
123 if (!$_POST['form_csvexport']) {
126 <tr bgcolor="#ffdddd">
127 <td class="detail">
128 &nbsp;
129 </td>
130 <td class="detail" colspan="3">
131 <?php echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?>
132 </td>
133 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
134 <td>
135 &nbsp;
136 </td>
137 <?php } ?>
138 <td align="right">
139 &nbsp;
140 </td>
141 <td align="right">
142 <?php echo text($catqty); ?>
143 </td>
144 <td align="right">
145 <?php text(bucks($cattotal)); ?>
146 </td>
147 </tr>
148 <?php
149 } // End not csv export
151 $cattotal = 0;
152 $catqty = 0;
153 $category = $rowcat;
154 $catleft = $category;
157 if ($_POST['form_details']) {
158 if ($_POST['form_csvexport']) {
159 echo '"' . display_desc($category ) . '",';
160 echo '"' . display_desc($product ) . '",';
161 echo '"' . oeFormatShortDate(display_desc($transdate)) . '",';
162 if($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2 ) {
163 echo '"' . $pat_name . '",';
165 if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {
166 echo '"' . display_desc($invnumber) . '",';
168 if($GLOBALS['sales_report_invoice'] == 1) {
169 echo '"' . $patient_id . '",';
171 // echo '"' . display_desc($invnumber) . '",';
172 echo '"' . display_desc($qty ) . '",';
173 echo '"'; bucks($rowamount); echo '"' . "\n";
175 else {
178 <tr>
179 <td class="detail">
180 <?php echo text(display_desc($catleft)); $catleft = " "; ?>
181 </td>
182 <td class="detail">
183 <?php echo text(display_desc($productleft)); $productleft = " "; ?>
184 </td>
185 <td>
186 <?php echo text(oeFormatShortDate($transdate)); ?>
187 </td>
188 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
189 <td>
190 &nbsp;
191 </td>
192 <?php } if($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2 ) { ?>
193 <td>
194 <?php echo text($pat_name); ?>
195 </td>
196 <?php } ?>
197 <td class="detail">
198 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) { ?>
199 <a href='../patient_file/pos_checkout.php?ptid=<?php echo attr($patient_id); ?>&enc=<?php echo attr($encounter_id); ?>'>
200 <?php echo text($invnumber); ?></a>
201 <?php }
202 if($GLOBALS['sales_report_invoice'] == 1 ) {
203 echo text($patient_id);
206 </td>
207 <?php if($GLOBALS['sales_report_invoice'] == 0) {?>
208 <td>
209 &nbsp;
210 </td>
211 <?php } ?>
212 <td align="right">
213 <?php echo text($qty); ?>
214 </td>
215 <td align="right">
216 <?php text(bucks($rowamount)); ?>
217 </td>
218 </tr>
219 <?php
221 } // End not csv export
222 } // end details
223 $producttotal += $rowamount;
224 $cattotal += $rowamount;
225 $grandtotal += $rowamount;
226 $productqty += $qty;
227 $catqty += $qty;
228 $grandqty += $qty;
229 } // end function
231 if (! acl_check('acct', 'rep')) die(xl("Unauthorized access."));
234 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
235 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
236 $form_facility = $_POST['form_facility'];
238 if ($_POST['form_csvexport']) {
239 header("Pragma: public");
240 header("Expires: 0");
241 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
242 header("Content-Type: application/force-download");
243 header("Content-Disposition: attachment; filename=sales_by_item.csv");
244 header("Content-Description: File Transfer");
245 // CSV headers:
246 if ($_POST['form_details']) {
247 echo '"Category",';
248 echo '"Item",';
249 echo '"Date",';
250 if($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2 ) {
251 echo '"Name",';
253 if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {
254 echo '"Invoice",';
256 if($GLOBALS['sales_report_invoice'] == 1) {
257 echo '"ID",';
259 echo '"Qty",';
260 echo '"Amount"' . "\n";
262 else {
263 echo '"Category",';
264 echo '"Item",';
265 echo '"Qty",';
266 echo '"Total"' . "\n";
268 } // end export
269 else {
271 <html>
272 <head>
273 <?php html_header_show();?>
275 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
276 <link rel="stylesheet" href="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
278 <style type="text/css">
280 /* specifically include & exclude from printing */
281 @media print {
282 #report_parameters {
283 visibility: hidden;
284 display: none;
286 #report_parameters_daterange {
287 visibility: visible;
288 display: inline;
290 #report_results {
291 margin-top: 30px;
295 /* specifically exclude some from the screen */
296 @media screen {
297 #report_parameters_daterange {
298 visibility: hidden;
299 display: none;
303 table.mymaintable, table.mymaintable td {
304 border: 1px solid #aaaaaa;
305 border-collapse: collapse;
307 table.mymaintable td {
308 padding: 1pt 4pt 1pt 4pt;
311 </style>
313 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script>
314 <script type="text/javascript" src="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script>
315 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script>
317 <script language="JavaScript">
319 $(document).ready(function() {
320 oeFixedHeaderSetup(document.getElementById('mymaintable'));
321 var win = top.printLogSetup ? top : opener.top;
322 win.printLogSetup(document.getElementById('printbutton'));
324 $('.datepicker').datetimepicker({
325 <?php $datetimepicker_timepicker = false; ?>
326 <?php $datetimepicker_formatInput = false; ?>
327 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
328 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
332 </script>
334 </head>
336 <title><?php echo xlt('Sales by Item') ?></title>
338 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' class="body_top">
340 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Sales by Item'); ?></span>
342 <form method='post' action='sales_by_item.php' id='theform'>
344 <div id="report_parameters">
345 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
346 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
347 <table>
348 <tr>
349 <td width='630px'>
350 <div style='float:left'>
351 <table class='text'>
352 <tr>
353 <td class='label'>
354 <?php echo xlt('Facility'); ?>:
355 </td>
356 <td>
357 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
358 </td>
359 <td class='label'>
360 <?php echo xlt('From'); ?>:
361 </td>
362 <td>
363 <input type='text' class='datepicker' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr($form_from_date) ?>'
364 title='yyyy-mm-dd'>
365 </td>
366 <td class='label'>
367 <?php echo xlt('To'); ?>:
368 </td>
369 <td>
370 <input type='text' class='datepicker' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>'
371 title='yyyy-mm-dd'>
372 </td>
373 </tr>
374 </table>
375 <table class='text'>
376 <tr>
377 <td class='label'>
378 <?php echo xlt('Provider'); ?>:
379 </td>
380 <td>
381 <?php
382 if (acl_check('acct', 'rep_a')) {
383 // Build a drop-down list of providers.
384 $query = "select id, lname, fname from users where " .
385 "authorized = 1 order by lname, fname";
386 $res = sqlStatement($query);
387 echo " &nbsp;<select name='form_provider'>\n";
388 echo " <option value=''>-- " . xlt('All Providers') . " --\n";
389 while ($row = sqlFetchArray($res)) {
390 $provid = $row['id'];
391 echo " <option value='". attr($provid) ."'";
392 if ($provid == $_REQUEST['form_provider']) echo " selected";
393 echo ">" . text($row['lname']) . ", " . text($row['fname']) . "\n";
395 echo " </select>\n";
396 } else {
397 echo "<input type='hidden' name='form_provider' value='" . attr($_SESSION['authUserID']) . "'>";
400 &nbsp;
401 </td>
402 <td>
403 <label><input type='checkbox' name='form_details'<?php if ($form_details) echo ' checked'; ?>>
404 <?php echo xlt('Details'); ?></label>
405 </td>
406 </tr>
407 </table>
408 </div>
410 </td>
411 <td align='left' valign='middle' height="100%">
412 <table style='border-left:1px solid; width:100%; height:100%' >
413 <tr>
414 <td>
415 <div style='margin-left:15px'>
416 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
417 <span>
418 <?php echo xlt('Submit'); ?>
419 </span>
420 </a>
422 <?php if ($_POST['form_refresh'] || $_POST['form_csvexport']) { ?>
423 <a href='#' class='css_button' id='printbutton'>
424 <span>
425 <?php echo xlt('Print'); ?>
426 </span>
427 </a>
428 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
429 <span>
430 <?php echo xlt('CSV Export'); ?>
431 </span>
432 </a>
433 <?php } ?>
434 </div>
435 </td>
436 </tr>
437 </table>
438 </td>
439 </tr>
440 </table>
442 </div> <!-- end of parameters -->
444 <?php
445 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
448 <div id="report_results">
449 <table width='98%' id='mymaintable' class='mymaintable'>
450 <thead>
451 <th>
452 <?php echo xlt('Category'); ?>
453 </th>
454 <th>
455 <?php echo xlt('Item'); ?>
456 </th>
457 <th>
458 <?php if ($form_details) echo xlt('Date'); ?>
459 </th>
460 <?php if($GLOBALS['sales_report_invoice'] == 2) {?>
461 <th>
462 &nbsp;
463 </th>
464 <?php } ?>
465 <th>
466 <?php
467 if($GLOBALS['sales_report_invoice'] == 0) {
468 if ($form_details) echo ' ';
470 </th>
471 <th>
472 <?php
473 if ($form_details) echo xlt('Invoice'); }
474 if($GLOBALS['sales_report_invoice'] == 1 || $GLOBALS['sales_report_invoice'] == 2 ) {
475 if ($form_details) echo xlt('Name');
476 } ?>
477 </th>
478 <th>
479 <?php
480 if($GLOBALS['sales_report_invoice'] == 2) {
481 if ($form_details) echo xlt('Invoice');
483 if($GLOBALS['sales_report_invoice'] == 1) {
484 if ($form_details) echo xlt('ID');
487 </th>
488 <th align="right">
489 <?php echo xlt('Qty'); ?>
490 </th>
491 <th align="right">
492 <?php echo xlt('Amount'); ?>
493 </th>
494 </thead>
495 <tbody>
496 <?php
497 } // end not export
500 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
501 $from_date = $form_from_date . ' 00:00:00';
502 $to_date = $form_to_date . ' 23:59:59';
503 $category = "";
504 $catleft = "";
505 $cattotal = 0;
506 $catqty = 0;
507 $product = "";
508 $productleft = "";
509 $producttotal = 0;
510 $productqty = 0;
511 $grandtotal = 0;
512 $grandqty = 0;
514 $sqlBindArray = array();
515 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.units, " .
516 "b.code_text, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno, lo.title " .
517 "FROM billing AS b " .
518 "JOIN code_types AS ct ON ct.ct_key = b.code_type " .
519 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
520 "LEFT JOIN codes AS c ON c.code_type = ct.ct_id AND c.code = b.code AND c.modifier = b.modifier " .
521 "LEFT JOIN list_options AS lo ON lo.list_id = 'superbill' AND lo.option_id = c.superbill AND lo.activity = 1 " .
522 "WHERE b.code_type != 'COPAY' AND b.activity = 1 AND b.fee != 0 AND " .
523 "fe.date >= ? AND fe.date <= ?";
524 array_push($sqlBindArray,$from_date,$to_date);
525 // If a facility was specified.
526 if ($form_facility) {
527 $query .= " AND fe.facility_id = ?";
528 array_push($sqlBindArray,$form_facility);
530 if ($form_provider) {
531 $query .= " AND fe.provider_id = ?";
532 array_push($sqlBindArray,$form_provider);
534 $query .= " ORDER BY lo.title, b.code, fe.date, fe.id";
536 $res = sqlStatement($query,$sqlBindArray);
537 while ($row = sqlFetchArray($res)) {
538 thisLineItem($row['pid'], $row['encounter'],
539 $row['title'], $row['code'] . ' ' . $row['code_text'],
540 substr($row['date'], 0, 10), $row['units'], $row['fee'], $row['invoice_refno']);
543 $sqlBindArray = array();
544 $query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " .
545 "d.name, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno " .
546 "FROM drug_sales AS s " .
547 "JOIN drugs AS d ON d.drug_id = s.drug_id " .
548 "JOIN form_encounter AS fe ON " .
549 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
550 "fe.date >= ? AND fe.date <= ? " .
551 "WHERE s.fee != 0";
552 array_push($sqlBindArray,$from_date,$to_date);
553 // If a facility was specified.
554 if ($form_facility) {
555 $query .= " AND fe.facility_id = ?";
556 array_push($sqlBindArray,$form_facility);
558 if ($form_provider) {
559 $query .= " AND fe.provider_id = ?";
560 array_push($sqlBindArray,$form_provider);
562 $query .= " ORDER BY d.name, fe.date, fe.id";
564 $res = sqlStatement($query,$sqlBindArray);
565 while ($row = sqlFetchArray($res)) {
566 thisLineItem($row['pid'], $row['encounter'], xl('Products'), $row['name'],
567 substr($row['date'], 0, 10), $row['quantity'], $row['fee'], $row['invoice_refno']);
570 if ($_POST['form_csvexport']) {
571 if (! $_POST['form_details']) {
572 echo '"' . display_desc($product) . '",';
573 echo '"' . $productqty . '",';
574 echo '"'; bucks($producttotal); echo '"' . "\n";
577 else {
580 <tr bgcolor="#ddddff">
581 <td class="detail">
582 <?php echo text(display_desc($catleft)); $catleft = " "; ?>
583 </td>
584 <td class="detail" colspan="3">
585 <?php if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?>
586 </td>
587 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
588 <td>
589 &nbsp;
590 </td>
591 <?php } ?>
592 <td align="right">
593 &nbsp;
594 </td>
595 <td align="right">
596 <?php echo text($productqty); ?>
597 </td>
598 <td align="right">
599 <?php text(bucks($producttotal)); ?>
600 </td>
601 </tr>
603 <tr bgcolor="#ffdddd">
604 <td class="detail">
605 &nbsp;
606 </td>
607 <td class="detail" colspan="3"><b>
608 <?php echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?>
609 </b></td>
610 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
611 <td>
612 &nbsp;
613 </td>
614 <?php } ?>
615 <td align="right">
616 &nbsp;
617 </td>
618 <td align="right"><b>
619 <?php echo text($catqty); ?>
620 </b></td>
621 <td align="right"><b>
622 <?php text(bucks($cattotal)); ?>
623 </b></td>
624 </tr>
626 <tr>
627 <td class="detail" colspan="4"><b>
628 <?php echo xlt('Grand Total'); ?>
629 </b></td>
630 <?php if($GLOBALS['sales_report_invoice'] == 0 || $GLOBALS['sales_report_invoice'] == 2) {?>
631 <td>
632 &nbsp;
633 </td>
634 <?php } ?>
635 <td align="right">
636 &nbsp;
637 </td>
638 <td align="right"><b>
639 <?php echo text($grandqty); ?>
640 </b></td>
641 <td align="right"><b>
642 <?php text(bucks($grandtotal)); ?>
643 </b></td>
644 </tr>
645 <?php $report_from_date = oeFormatShortDate($form_from_date) ;
646 $report_to_date = oeFormatShortDate($form_to_date) ;
648 <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>
649 <?php
651 } // End not csv export
654 if (! $_POST['form_csvexport']) {
655 if($_POST['form_refresh']){
658 </tbody>
659 </table>
660 </div> <!-- report results -->
661 <?php } else { ?>
662 <div class='text'>
663 <?php echo xlt('Please input search criteria above, and click Submit to view results.' ); ?>
664 </div>
665 <?php } ?>
667 </form>
669 </body>
671 </html>
672 <?php
673 } // End not csv export