3 * This is a report of sales by item description.
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-2018 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
;
25 if (!verifyCsrfToken($_POST["csrf_token_form"])) {
30 $form_provider = $_POST['form_provider'];
31 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
32 $form_details = $_POST['form_details'] ?
true : false;
34 $form_details = false;
37 function bucks($amount)
40 return oeFormatMoney($amount);
44 function display_desc($desc)
46 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
53 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'];
75 $rowproduct = $description;
77 $rowproduct = xl('Unknown');
80 if ($product != $rowproduct ||
$category != $rowcat) {
82 // Print product total.
83 if ($_POST['form_csvexport']) {
84 if (! $_POST['form_details']) {
85 echo '"' . display_desc($category) . '",';
86 echo '"' . display_desc($product) . '",';
87 echo '"' . $productqty . '",';
89 echo bucks($producttotal);
94 <tr bgcolor
="#ddddff">
96 <?php
echo text(display_desc($catleft));
99 <td
class="detail" colspan
="3">
101 if ($_POST['form_details']) {
102 echo xlt('Total for') . ' ';
105 echo text(display_desc($product)); ?
>
107 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
116 <?php
echo text($productqty); ?
>
119 <?php
echo text(bucks($producttotal)); ?
>
123 } // End not csv export
128 $product = $rowproduct;
129 $productleft = $product;
132 if ($category != $rowcat) {
134 // Print category total.
135 if (!$_POST['form_csvexport']) {
138 <tr bgcolor
="#ffdddd">
142 <td
class="detail" colspan
="3">
143 <?php
echo xlt('Total for category') . ' ';
144 echo text(display_desc($category)); ?
>
146 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
155 <?php
echo text($catqty); ?
>
158 <?php
echo text(bucks($cattotal)); ?
>
162 } // End not csv export
168 $catleft = $category;
171 if ($_POST['form_details']) {
172 if ($_POST['form_csvexport']) {
173 echo '"' . display_desc($category) . '",';
174 echo '"' . display_desc($product) . '",';
175 echo '"' . oeFormatShortDate(display_desc($transdate)) . '",';
176 if ($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2) {
177 echo '"' . $pat_name . '",';
180 if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {
181 echo '"' . display_desc($invnumber) . '",';
184 if ($GLOBALS['sales_report_invoice'] == 1) {
185 echo '"' . $patient_id . '",';
188 // echo '"' . display_desc($invnumber) . '",';
189 echo '"' . display_desc($qty) . '",';
191 echo bucks($rowamount);
198 <?php
echo text(display_desc($catleft));
202 <?php
echo text(display_desc($productleft));
203 $productleft = " "; ?
>
206 <?php
echo text(oeFormatShortDate($transdate)); ?
>
208 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
213 <?php
if ($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2) { ?
>
215 <?php
echo text($pat_name); ?
>
219 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) { ?
>
220 <a href
='../patient_file/pos_checkout.php?ptid=<?php echo attr_url($patient_id); ?>&enc=<?php echo attr_url($encounter_id); ?>'>
221 <?php
echo text($invnumber); ?
></a
>
224 if ($GLOBALS['sales_report_invoice'] == 1) {
225 echo text($patient_id);
229 <?php
if ($GLOBALS['sales_report_invoice'] == 0) {?
>
235 <?php
echo text($qty); ?
>
238 <?php
echo text(bucks($rowamount)); ?
>
242 } // End not csv export
244 $producttotal +
= $rowamount;
245 $cattotal +
= $rowamount;
246 $grandtotal +
= $rowamount;
252 if (! acl_check('acct', 'rep')) {
253 die(xlt("Unauthorized access."));
256 $form_from_date = (isset($_POST['form_from_date'])) ?
DateToYYYYMMDD($_POST['form_from_date']) : date('Y-m-d');
257 $form_to_date = (isset($_POST['form_to_date'])) ?
DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d');
258 $form_facility = $_POST['form_facility'];
260 if ($_POST['form_csvexport']) {
261 header("Pragma: public");
262 header("Expires: 0");
263 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
264 header("Content-Type: application/force-download");
265 header("Content-Disposition: attachment; filename=sales_by_item.csv");
266 header("Content-Description: File Transfer");
268 if ($_POST['form_details']) {
272 if ($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2) {
276 if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {
280 if ($GLOBALS['sales_report_invoice'] == 1) {
285 echo '"Amount"' . "\n";
290 echo '"Total"' . "\n";
298 <title
><?php
echo xlt('Sales by Item'); ?
></title
>
300 <?php Header
::setupHeader(['datetime-picker', 'report-helper']); ?
>
302 <style type
="text/css">
303 /* specifically include & exclude from printing */
309 #report_parameters_daterange {
318 /* specifically exclude some from the screen */
320 #report_parameters_daterange {
326 table
.mymaintable
, table
.mymaintable td
{
327 border
: 1px solid
#aaaaaa;
328 border
-collapse
: collapse
;
330 table
.mymaintable td
{
331 padding
: 1pt
4pt
1pt
4pt
;
335 <script language
="JavaScript">
336 $
(document
).ready(function() {
337 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
338 var win
= top
.printLogSetup ? top
: opener
.top
;
339 win
.printLogSetup(document
.getElementById('printbutton'));
341 $
('.datepicker').datetimepicker({
342 <?php
$datetimepicker_timepicker = false; ?
>
343 <?php
$datetimepicker_showseconds = false; ?
>
344 <?php
$datetimepicker_formatInput = true; ?
>
345 <?php
require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?
>
346 <?php
// can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
352 <title
><?php
echo xlt('Sales by Item') ?
></title
>
354 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0' class="body_top">
356 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Sales by Item'); ?
></span
>
358 <form method
='post' action
='sales_by_item.php' id
='theform' onsubmit
='return top.restoreSession()'>
359 <input type
="hidden" name
="csrf_token_form" value
="<?php echo attr(collectCsrfToken()); ?>" />
361 <div id
="report_parameters">
362 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
363 <input type
='hidden' name
='form_csvexport' id
='form_csvexport' value
=''/>
367 <div style
='float:left'>
370 <td
class='control-label'>
371 <?php
echo xlt('Facility'); ?
>:
374 <?php
dropdown_facility($form_facility, 'form_facility', true); ?
>
376 <td
class='control-label'>
377 <?php
echo xlt('From'); ?
>:
380 <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)); ?>'>
382 <td
class='control-label'>
383 <?php
echo xlt('To'); ?
>:
386 <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)); ?>'>
392 <td
class='control-label'>
393 <?php
echo xlt('Provider'); ?
>:
397 if (acl_check('acct', 'rep_a')) {
398 // Build a drop-down list of providers.
399 $query = "select id, lname, fname from users where " .
400 "authorized = 1 order by lname, fname";
401 $res = sqlStatement($query);
402 echo " <select name='form_provider' class='form-control'>\n";
403 echo " <option value=''>-- " . xlt('All Providers') . " --\n";
404 while ($row = sqlFetchArray($res)) {
405 $provid = $row['id'];
406 echo " <option value='". attr($provid) ."'";
407 if ($provid == $_REQUEST['form_provider']) {
411 echo ">" . text($row['lname']) . ", " . text($row['fname']) . "\n";
416 echo "<input type='hidden' name='form_provider' value='" . attr($_SESSION['authUserID']) . "'>";
422 <div
class='checkbox'>
423 <label
><input type
='checkbox' name
='form_details'<?php
echo ($form_details) ?
' checked' : ''; ?
>>
424 <?php
echo xlt('Details'); ?
></label
>
432 <td align
='left' valign
='middle' height
="100%">
433 <table style
='border-left:1px solid; width:100%; height:100%' >
436 <div
class="text-center">
437 <div
class="btn-group" role
="group">
438 <a href
='#' class='btn btn-default btn-save' onclick
='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
439 <?php
echo xlt('Submit'); ?
>
441 <?php
if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) { ?
>
442 <a href
='#' class='btn btn-default btn-print' id
='printbutton'>
443 <?php
echo xlt('Print'); ?
>
445 <a href
='#' class='btn btn-default btn-transmit' onclick
='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
446 <?php
echo xlt('CSV Export'); ?
>
458 </div
> <!-- end of parameters
-->
461 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
464 <div id
="report_results">
465 <table width
='98%' id
='mymaintable' class='mymaintable'>
468 <?php
echo xlt('Category'); ?
>
471 <?php
echo xlt('Item'); ?
>
479 <?php
if ($GLOBALS['sales_report_invoice'] == 2) {?
>
486 if ($GLOBALS['sales_report_invoice'] == 0) {
499 if ($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2) {
507 if ($GLOBALS['sales_report_invoice'] == 2) {
513 if ($GLOBALS['sales_report_invoice'] == 1) {
521 <?php
echo xlt('Qty'); ?
>
524 <?php
echo xlt('Amount'); ?
>
532 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
533 $from_date = $form_from_date . ' 00:00:00';
534 $to_date = $form_to_date . ' 23:59:59';
546 $sqlBindArray = array();
547 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.units, " .
548 "b.code_text, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno, lo.title " .
549 "FROM billing AS b " .
550 "JOIN code_types AS ct ON ct.ct_key = b.code_type " .
551 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
552 "LEFT JOIN codes AS c ON c.code_type = ct.ct_id AND c.code = b.code AND c.modifier = b.modifier " .
553 "LEFT JOIN list_options AS lo ON lo.list_id = 'superbill' AND lo.option_id = c.superbill AND lo.activity = 1 " .
554 "WHERE b.code_type != 'COPAY' AND b.activity = 1 AND b.fee != 0 AND " .
555 "fe.date >= ? AND fe.date <= ?";
556 array_push($sqlBindArray, $from_date, $to_date);
557 // If a facility was specified.
558 if ($form_facility) {
559 $query .= " AND fe.facility_id = ?";
560 array_push($sqlBindArray, $form_facility);
563 if ($form_provider) {
564 $query .= " AND fe.provider_id = ?";
565 array_push($sqlBindArray, $form_provider);
568 $query .= " ORDER BY lo.title, b.code, fe.date, fe.id";
570 $res = sqlStatement($query, $sqlBindArray);
571 while ($row = sqlFetchArray($res)) {
576 $row['code'] . ' ' . $row['code_text'],
577 substr($row['date'], 0, 10),
580 $row['invoice_refno']
585 $sqlBindArray = array();
586 $query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " .
587 "d.name, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno " .
588 "FROM drug_sales AS s " .
589 "JOIN drugs AS d ON d.drug_id = s.drug_id " .
590 "JOIN form_encounter AS fe ON " .
591 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
592 "fe.date >= ? AND fe.date <= ? " .
594 array_push($sqlBindArray, $from_date, $to_date);
595 // If a facility was specified.
596 if ($form_facility) {
597 $query .= " AND fe.facility_id = ?";
598 array_push($sqlBindArray, $form_facility);
601 if ($form_provider) {
602 $query .= " AND fe.provider_id = ?";
603 array_push($sqlBindArray, $form_provider);
606 $query .= " ORDER BY d.name, fe.date, fe.id";
608 $res = sqlStatement($query, $sqlBindArray);
609 while ($row = sqlFetchArray($res)) {
615 substr($row['date'], 0, 10),
618 $row['invoice_refno']
622 if ($_POST['form_csvexport']) {
623 if (! $_POST['form_details']) {
624 echo '"' . display_desc($product) . '",';
625 echo '"' . $productqty . '",';
627 echo bucks($producttotal);
633 <tr bgcolor
="#ddddff">
635 <?php
echo text(display_desc($catleft));
638 <td
class="detail" colspan
="3">
640 if ($_POST['form_details']) {
641 echo xlt('Total for') . ' ';
644 echo text(display_desc($product)); ?
>
646 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
655 <?php
echo text($productqty); ?
>
658 <?php
echo text(bucks($producttotal)); ?
>
662 <tr bgcolor
="#ffdddd">
666 <td
class="detail" colspan
="3"><b
>
667 <?php
echo xlt('Total for category') . ' ';
668 echo text(display_desc($category)); ?
>
670 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
678 <td align
="right"><b
>
679 <?php
echo text($catqty); ?
>
681 <td align
="right"><b
>
682 <?php
echo text(bucks($cattotal)); ?
>
687 <td
class="detail" colspan
="4"><b
>
688 <?php
echo xlt('Grand Total'); ?
>
690 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
698 <td align
="right"><b
>
699 <?php
echo text($grandqty); ?
>
701 <td align
="right"><b
>
702 <?php
echo text(bucks($grandtotal)); ?
>
705 <?php
$report_from_date = oeFormatShortDate($form_from_date) ;
706 $report_to_date = oeFormatShortDate($form_to_date) ;
708 <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
>
710 } // End not csv export
713 if (! $_POST['form_csvexport']) {
714 if ($_POST['form_refresh']) {
719 </div
> <!-- report results
-->
723 <?php
echo xlt('Please input search criteria above, and click Submit to view results.'); ?
>
734 } // End not csv export