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
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;
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)) {
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) {
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";
89 <tr bgcolor
="#ddddff">
91 <?php
echo text(display_desc($catleft)); $catleft = " "; ?
>
93 <td
class="detail" colspan
="3">
94 <?php
if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?
>
96 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
105 <?php
echo text($productqty); ?
>
108 <?php
text(bucks($producttotal)); ?
>
112 } // End not csv export
116 $product = $rowproduct;
117 $productleft = $product;
120 if ($category != $rowcat) {
122 // Print category total.
123 if (!$_POST['form_csvexport']) {
126 <tr bgcolor
="#ffdddd">
130 <td
class="detail" colspan
="3">
131 <?php
echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?
>
133 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
142 <?php
echo text($catqty); ?
>
145 <?php
text(bucks($cattotal)); ?
>
149 } // End not csv export
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";
180 <?php
echo text(display_desc($catleft)); $catleft = " "; ?
>
183 <?php
echo text(display_desc($productleft)); $productleft = " "; ?
>
186 <?php
echo text(oeFormatShortDate($transdate)); ?
>
188 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
192 <?php
} if($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2 ) { ?
>
194 <?php
echo text($pat_name); ?
>
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
>
202 if($GLOBALS['sales_report_invoice'] == 1 ) {
203 echo text($patient_id);
207 <?php
if($GLOBALS['sales_report_invoice'] == 0) {?
>
213 <?php
echo text($qty); ?
>
216 <?php
text(bucks($rowamount)); ?
>
221 } // End not csv export
223 $producttotal +
= $rowamount;
224 $cattotal +
= $rowamount;
225 $grandtotal +
= $rowamount;
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");
246 if ($_POST['form_details']) {
250 if($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2 ) {
253 if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {
256 if($GLOBALS['sales_report_invoice'] == 1) {
260 echo '"Amount"' . "\n";
266 echo '"Total"' . "\n";
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 */
286 #report_parameters_daterange {
295 /* specifically exclude some from the screen */
297 #report_parameters_daterange {
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
;
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 ?>
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
=''/>
350 <div style
='float:left'>
354 <?php
echo xlt('Facility'); ?
>:
357 <?php
dropdown_facility($form_facility, 'form_facility', true); ?
>
360 <?php
echo xlt('From'); ?
>:
363 <input type
='text' class='datepicker' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo attr($form_from_date) ?>'
367 <?php
echo xlt('To'); ?
>:
370 <input type
='text' class='datepicker' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr($form_to_date) ?>'
378 <?php
echo xlt('Provider'); ?
>:
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 " <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";
397 echo "<input type='hidden' name='form_provider' value='" . attr($_SESSION['authUserID']) . "'>";
403 <label
><input type
='checkbox' name
='form_details'<?php
if ($form_details) echo ' checked'; ?
>>
404 <?php
echo xlt('Details'); ?
></label
>
411 <td align
='left' valign
='middle' height
="100%">
412 <table style
='border-left:1px solid; width:100%; height:100%' >
415 <div style
='margin-left:15px'>
416 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
418 <?php
echo xlt('Submit'); ?
>
422 <?php
if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) { ?
>
423 <a href
='#' class='css_button' id
='printbutton'>
425 <?php
echo xlt('Print'); ?
>
428 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
430 <?php
echo xlt('CSV Export'); ?
>
442 </div
> <!-- end of parameters
-->
445 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
448 <div id
="report_results">
449 <table width
='98%' id
='mymaintable' class='mymaintable'>
452 <?php
echo xlt('Category'); ?
>
455 <?php
echo xlt('Item'); ?
>
458 <?php
if ($form_details) echo xlt('Date'); ?
>
460 <?php
if($GLOBALS['sales_report_invoice'] == 2) {?
>
467 if($GLOBALS['sales_report_invoice'] == 0) {
468 if ($form_details) echo ' ';
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');
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');
489 <?php
echo xlt('Qty'); ?
>
492 <?php
echo xlt('Amount'); ?
>
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';
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 <= ? " .
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";
580 <tr bgcolor
="#ddddff">
582 <?php
echo text(display_desc($catleft)); $catleft = " "; ?
>
584 <td
class="detail" colspan
="3">
585 <?php
if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?
>
587 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
596 <?php
echo text($productqty); ?
>
599 <?php
text(bucks($producttotal)); ?
>
603 <tr bgcolor
="#ffdddd">
607 <td
class="detail" colspan
="3"><b
>
608 <?php
echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?
>
610 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
618 <td align
="right"><b
>
619 <?php
echo text($catqty); ?
>
621 <td align
="right"><b
>
622 <?php
text(bucks($cattotal)); ?
>
627 <td
class="detail" colspan
="4"><b
>
628 <?php
echo xlt('Grand Total'); ?
>
630 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
638 <td align
="right"><b
>
639 <?php
echo text($grandqty); ?
>
641 <td align
="right"><b
>
642 <?php
text(bucks($grandtotal)); ?
>
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
>
651 } // End not csv export
654 if (! $_POST['form_csvexport']) {
655 if($_POST['form_refresh']){
660 </div
> <!-- report results
-->
663 <?php
echo xlt('Please input search criteria above, and click Submit to view results.' ); ?
>
673 } // End not csv export