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>;.
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;
40 $form_details = false;
43 function bucks($amount)
46 echo oeFormatMoney($amount);
50 function display_desc($desc)
52 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
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'];
81 $rowproduct = $description;
83 $rowproduct = xl('Unknown');
86 if ($product != $rowproduct ||
$category != $rowcat) {
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 . '",';
100 <tr bgcolor
="#ddddff">
102 <?php
echo text(display_desc($catleft));
105 <td
class="detail" colspan
="3">
106 <?php
if ($_POST['form_details']) {
107 echo xlt('Total for') . ' ';
110 echo text(display_desc($product)); ?
>
112 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
121 <?php
echo text($productqty); ?
>
124 <?php
text(bucks($producttotal)); ?
>
128 } // End not csv export
133 $product = $rowproduct;
134 $productleft = $product;
137 if ($category != $rowcat) {
139 // Print category total.
140 if (!$_POST['form_csvexport']) {
143 <tr bgcolor
="#ffdddd">
147 <td
class="detail" colspan
="3">
148 <?php
echo xlt('Total for category') . ' ';
149 echo text(display_desc($category)); ?
>
151 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
160 <?php
echo text($catqty); ?
>
163 <?php
text(bucks($cattotal)); ?
>
167 } // End not csv export
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) . '",';
203 <?php
echo text(display_desc($catleft));
207 <?php
echo text(display_desc($productleft));
208 $productleft = " "; ?
>
211 <?php
echo text(oeFormatShortDate($transdate)); ?
>
213 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
217 <?php
} if ($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2) { ?
>
219 <?php
echo text($pat_name); ?
>
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
>
228 if ($GLOBALS['sales_report_invoice'] == 1) {
229 echo text($patient_id);
233 <?php
if ($GLOBALS['sales_report_invoice'] == 0) {?
>
239 <?php
echo text($qty); ?
>
242 <?php
text(bucks($rowamount)); ?
>
246 } // End not csv export
248 $producttotal +
= $rowamount;
249 $cattotal +
= $rowamount;
250 $grandtotal +
= $rowamount;
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");
273 if ($_POST['form_details']) {
277 if ($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2) {
281 if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {
285 if ($GLOBALS['sales_report_invoice'] == 1) {
290 echo '"Amount"' . "\n";
295 echo '"Total"' . "\n";
303 <?php Header
::setupHeader(['datetime-picker', 'report-helper']); ?
>
305 <style type
="text/css">
307 /* specifically include & exclude from printing */
313 #report_parameters_daterange {
322 /* specifically exclude some from the screen */
324 #report_parameters_daterange {
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
;
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 ?>
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
=''/>
374 <div style
='float:left'>
377 <td
class='control-label'>
378 <?php
echo xlt('Facility'); ?
>:
381 <?php
dropdown_facility($form_facility, 'form_facility', true); ?
>
383 <td
class='control-label'>
384 <?php
echo xlt('From'); ?
>:
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) ?>'
390 <td
class='control-label'>
391 <?php
echo xlt('To'); ?
>:
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) ?>'
401 <td
class='control-label'>
402 <?php
echo xlt('Provider'); ?
>:
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 " <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']) {
420 echo ">" . text($row['lname']) . ", " . text($row['fname']) . "\n";
425 echo "<input type='hidden' name='form_provider' value='" . attr($_SESSION['authUserID']) . "'>";
431 <div
class='checkbox'>
432 <label
><input type
='checkbox' name
='form_details'<?php
if ($form_details) {
435 <?php
echo xlt('Details'); ?
></label
>
443 <td align
='left' valign
='middle' height
="100%">
444 <table style
='border-left:1px solid; width:100%; height:100%' >
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'); ?
>
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'); ?
>
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'); ?
>
469 </div
> <!-- end of parameters
-->
472 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
475 <div id
="report_results">
476 <table width
='98%' id
='mymaintable' class='mymaintable'>
479 <?php
echo xlt('Category'); ?
>
482 <?php
echo xlt('Item'); ?
>
485 <?php
if ($form_details) {
489 <?php
if ($GLOBALS['sales_report_invoice'] == 2) {?
>
496 if ($GLOBALS['sales_report_invoice'] == 0) {
509 if ($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2) {
517 if ($GLOBALS['sales_report_invoice'] == 2) {
523 if ($GLOBALS['sales_report_invoice'] == 1) {
531 <?php
echo xlt('Qty'); ?
>
534 <?php
echo xlt('Amount'); ?
>
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';
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)) {
586 $row['code'] . ' ' . $row['code_text'],
587 substr($row['date'], 0, 10),
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 <= ? " .
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)) {
625 substr($row['date'], 0, 10),
628 $row['invoice_refno']
632 if ($_POST['form_csvexport']) {
633 if (! $_POST['form_details']) {
634 echo '"' . display_desc($product) . '",';
635 echo '"' . $productqty . '",';
637 bucks($producttotal);
643 <tr bgcolor
="#ddddff">
645 <?php
echo text(display_desc($catleft));
648 <td
class="detail" colspan
="3">
649 <?php
if ($_POST['form_details']) {
650 echo xlt('Total for') . ' ';
653 echo text(display_desc($product)); ?
>
655 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
664 <?php
echo text($productqty); ?
>
667 <?php
text(bucks($producttotal)); ?
>
671 <tr bgcolor
="#ffdddd">
675 <td
class="detail" colspan
="3"><b
>
676 <?php
echo xlt('Total for category') . ' ';
677 echo text(display_desc($category)); ?
>
679 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
687 <td align
="right"><b
>
688 <?php
echo text($catqty); ?
>
690 <td align
="right"><b
>
691 <?php
text(bucks($cattotal)); ?
>
696 <td
class="detail" colspan
="4"><b
>
697 <?php
echo xlt('Grand Total'); ?
>
699 <?php
if ($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
707 <td align
="right"><b
>
708 <?php
echo text($grandqty); ?
>
710 <td align
="right"><b
>
711 <?php
text(bucks($grandtotal)); ?
>
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
>
719 } // End not csv export
722 if (! $_POST['form_csvexport']) {
723 if ($_POST['form_refresh']) {
728 </div
> <!-- report results
-->
732 <?php
echo xlt('Please input search criteria above, and click Submit to view results.'); ?
>
743 } // End not csv export