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>;.
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";
33 require_once "$srcdir/formdata.inc.php";
35 $form_provider = $_POST['form_provider'];
36 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
37 $form_details = $_POST['form_details'] ?
true : false;
41 $form_details = false;
43 function bucks($amount) {
44 if ($amount) echo oeFormatMoney($amount);
47 function display_desc($desc) {
48 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
54 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'];
71 if (empty($rowcat)) $rowcat = xl('None');
72 $rowproduct = $description;
73 if (! $rowproduct) $rowproduct = xl('Unknown');
75 if ($product != $rowproduct ||
$category != $rowcat) {
77 // Print product total.
78 if ($_POST['form_csvexport']) {
79 if (! $_POST['form_details']) {
80 echo '"' . display_desc($category) . '",';
81 echo '"' . display_desc($product) . '",';
82 echo '"' . $productqty . '",';
83 echo '"'; bucks($producttotal); echo '"' . "\n";
88 <tr bgcolor
="#ddddff">
90 <?php
echo text(display_desc($catleft)); $catleft = " "; ?
>
92 <td
class="detail" colspan
="3">
93 <?php
if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?
>
95 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
104 <?php
echo text($productqty); ?
>
107 <?php
text(bucks($producttotal)); ?
>
111 } // End not csv export
115 $product = $rowproduct;
116 $productleft = $product;
119 if ($category != $rowcat) {
121 // Print category total.
122 if (!$_POST['form_csvexport']) {
125 <tr bgcolor
="#ffdddd">
129 <td
class="detail" colspan
="3">
130 <?php
echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?
>
132 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
141 <?php
echo text($catqty); ?
>
144 <?php
text(bucks($cattotal)); ?
>
148 } // End not csv export
153 $catleft = $category;
156 if ($_POST['form_details']) {
157 if ($_POST['form_csvexport']) {
158 echo '"' . display_desc($category ) . '",';
159 echo '"' . display_desc($product ) . '",';
160 echo '"' . oeFormatShortDate(display_desc($transdate)) . '",';
161 if($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2 ) {
162 echo '"' . $pat_name . '",';
164 if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {
165 echo '"' . display_desc($invnumber) . '",';
167 if($GLOBALS['sales_report_invoice'] == 1) {
168 echo '"' . $patient_id . '",';
170 // echo '"' . display_desc($invnumber) . '",';
171 echo '"' . display_desc($qty ) . '",';
172 echo '"'; bucks($rowamount); echo '"' . "\n";
179 <?php
echo text(display_desc($catleft)); $catleft = " "; ?
>
182 <?php
echo text(display_desc($productleft)); $productleft = " "; ?
>
185 <?php
echo text(oeFormatShortDate($transdate)); ?
>
187 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
191 <?php
} if($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2 ) { ?
>
193 <?php
echo text($pat_name); ?
>
197 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) { ?
>
198 <a href
='../patient_file/pos_checkout.php?ptid=<?php echo attr($patient_id); ?>&enc=<?php echo attr($encounter_id); ?>'>
199 <?php
echo text($invnumber); ?
></a
>
201 if($GLOBALS['sales_report_invoice'] == 1 ) {
202 echo text($patient_id);
206 <?php
if($GLOBALS['sales_report_invoice'] == 0) {?
>
212 <?php
echo text($qty); ?
>
215 <?php
text(bucks($rowamount)); ?
>
220 } // End not csv export
222 $producttotal +
= $rowamount;
223 $cattotal +
= $rowamount;
224 $grandtotal +
= $rowamount;
230 if (! acl_check('acct', 'rep')) die(xl("Unauthorized access."));
233 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
234 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
235 $form_facility = $_POST['form_facility'];
237 if ($_POST['form_csvexport']) {
238 header("Pragma: public");
239 header("Expires: 0");
240 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
241 header("Content-Type: application/force-download");
242 header("Content-Disposition: attachment; filename=sales_by_item.csv");
243 header("Content-Description: File Transfer");
245 if ($_POST['form_details']) {
249 if($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2 ) {
252 if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {
255 if($GLOBALS['sales_report_invoice'] == 1) {
259 echo '"Amount"' . "\n";
265 echo '"Total"' . "\n";
272 <?php
html_header_show();?
>
273 <style type
="text/css">
275 /* specifically include & exclude from printing */
281 #report_parameters_daterange {
290 /* specifically exclude some from the screen */
292 #report_parameters_daterange {
298 table
.mymaintable
, table
.mymaintable td
{
299 border
: 1px solid
#aaaaaa;
300 border
-collapse
: collapse
;
302 table
.mymaintable td
{
303 padding
: 1pt
4pt
1pt
4pt
;
308 <script type
="text/javascript" src
="../../library/js/jquery-1.9.1.min.js"></script
>
309 <script type
="text/javascript" src
="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script
>
311 <script language
="JavaScript">
313 $
(document
).ready(function() {
314 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
315 var win
= top
.printLogSetup ? top
: opener
.top
;
316 win
.printLogSetup(document
.getElementById('printbutton'));
323 <title
><?php
echo xlt('Sales by Item') ?
></title
>
325 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0' class="body_top">
327 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Sales by Item'); ?
></span
>
329 <form method
='post' action
='sales_by_item.php' id
='theform'>
331 <div id
="report_parameters">
332 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
333 <input type
='hidden' name
='form_csvexport' id
='form_csvexport' value
=''/>
337 <div style
='float:left'>
341 <?php
echo xlt('Facility'); ?
>:
344 <?php
dropdown_facility($form_facility, 'form_facility', true); ?
>
347 <?php
echo xlt('From'); ?
>:
350 <input type
='text' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo attr($form_from_date) ?>'
351 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
352 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
353 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
354 title
='<?php echo xla('Click here to choose a date
'); ?>'>
357 <?php
echo xlt('To'); ?
>:
360 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr($form_to_date) ?>'
361 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
362 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
363 id
='img_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
364 title
='<?php echo xla('Click here to choose a date
'); ?>'>
371 <?php
echo xlt('Provider'); ?
>:
375 if (acl_check('acct', 'rep_a')) {
376 // Build a drop-down list of providers.
377 $query = "select id, lname, fname from users where " .
378 "authorized = 1 order by lname, fname";
379 $res = sqlStatement($query);
380 echo " <select name='form_provider'>\n";
381 echo " <option value=''>-- " . xlt('All Providers') . " --\n";
382 while ($row = sqlFetchArray($res)) {
383 $provid = $row['id'];
384 echo " <option value='". attr($provid) ."'";
385 if ($provid == $_REQUEST['form_provider']) echo " selected";
386 echo ">" . text($row['lname']) . ", " . text($row['fname']) . "\n";
390 echo "<input type='hidden' name='form_provider' value='" . attr($_SESSION['authUserID']) . "'>";
396 <label
><input type
='checkbox' name
='form_details'<?php
if ($form_details) echo ' checked'; ?
>>
397 <?php
echo xlt('Details'); ?
></label
>
404 <td align
='left' valign
='middle' height
="100%">
405 <table style
='border-left:1px solid; width:100%; height:100%' >
408 <div style
='margin-left:15px'>
409 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
411 <?php
echo xlt('Submit'); ?
>
415 <?php
if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) { ?
>
416 <a href
='#' class='css_button' id
='printbutton'>
418 <?php
echo xlt('Print'); ?
>
421 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
423 <?php
echo xlt('CSV Export'); ?
>
435 </div
> <!-- end of parameters
-->
438 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
441 <div id
="report_results">
442 <table width
='98%' id
='mymaintable' class='mymaintable'>
445 <?php
echo xlt('Category'); ?
>
448 <?php
echo xlt('Item'); ?
>
451 <?php
if ($form_details) echo xlt('Date'); ?
>
453 <?php
if($GLOBALS['sales_report_invoice'] == 2) {?
>
460 if($GLOBALS['sales_report_invoice'] == 0) {
461 if ($form_details) echo ' ';
466 if ($form_details) echo xlt('Invoice'); }
467 if($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2 ) {
468 if ($form_details) echo xlt('Name');
473 if($GLOBALS['sales_report_invoice'] == 2) {
474 if ($form_details) echo xlt('Invoice');
476 if($GLOBALS['sales_report_invoice'] == 1) {
477 if ($form_details) echo xlt('ID');
482 <?php
echo xlt('Qty'); ?
>
485 <?php
echo xlt('Amount'); ?
>
493 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
494 $from_date = $form_from_date . ' 00:00:00';
495 $to_date = $form_to_date . ' 23:59:59';
507 $sqlBindArray = array();
508 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.units, " .
509 "b.code_text, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno, lo.title " .
510 "FROM billing AS b " .
511 "JOIN code_types AS ct ON ct.ct_key = b.code_type " .
512 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
513 "LEFT JOIN codes AS c ON c.code_type = ct.ct_id AND c.code = b.code AND c.modifier = b.modifier " .
514 "LEFT JOIN list_options AS lo ON lo.list_id = 'superbill' AND lo.option_id = c.superbill " .
515 "WHERE b.code_type != 'COPAY' AND b.activity = 1 AND b.fee != 0 AND " .
516 "fe.date >= ? AND fe.date <= ?";
517 array_push($sqlBindArray,$from_date,$to_date);
518 // If a facility was specified.
519 if ($form_facility) {
520 $query .= " AND fe.facility_id = ?";
521 array_push($sqlBindArray,$form_facility);
523 if ($form_provider) {
524 $query .= " AND fe.provider_id = ?";
525 array_push($sqlBindArray,$form_provider);
527 $query .= " ORDER BY lo.title, b.code, fe.date, fe.id";
529 $res = sqlStatement($query,$sqlBindArray);
530 while ($row = sqlFetchArray($res)) {
531 thisLineItem($row['pid'], $row['encounter'],
532 $row['title'], $row['code'] . ' ' . $row['code_text'],
533 substr($row['date'], 0, 10), $row['units'], $row['fee'], $row['invoice_refno']);
536 $sqlBindArray = array();
537 $query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " .
538 "d.name, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno " .
539 "FROM drug_sales AS s " .
540 "JOIN drugs AS d ON d.drug_id = s.drug_id " .
541 "JOIN form_encounter AS fe ON " .
542 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
543 "fe.date >= ? AND fe.date <= ? " .
545 array_push($sqlBindArray,$from_date,$to_date);
546 // If a facility was specified.
547 if ($form_facility) {
548 $query .= " AND fe.facility_id = ?";
549 array_push($sqlBindArray,$form_facility);
551 if ($form_provider) {
552 $query .= " AND fe.provider_id = ?";
553 array_push($sqlBindArray,$form_provider);
555 $query .= " ORDER BY d.name, fe.date, fe.id";
557 $res = sqlStatement($query,$sqlBindArray);
558 while ($row = sqlFetchArray($res)) {
559 thisLineItem($row['pid'], $row['encounter'], xl('Products'), $row['name'],
560 substr($row['date'], 0, 10), $row['quantity'], $row['fee'], $row['invoice_refno']);
563 if ($_POST['form_csvexport']) {
564 if (! $_POST['form_details']) {
565 echo '"' . display_desc($product) . '",';
566 echo '"' . $productqty . '",';
567 echo '"'; bucks($producttotal); echo '"' . "\n";
573 <tr bgcolor
="#ddddff">
575 <?php
echo text(display_desc($catleft)); $catleft = " "; ?
>
577 <td
class="detail" colspan
="3">
578 <?php
if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?
>
580 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
589 <?php
echo text($productqty); ?
>
592 <?php
text(bucks($producttotal)); ?
>
596 <tr bgcolor
="#ffdddd">
600 <td
class="detail" colspan
="3"><b
>
601 <?php
echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?
>
603 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
611 <td align
="right"><b
>
612 <?php
echo text($catqty); ?
>
614 <td align
="right"><b
>
615 <?php
text(bucks($cattotal)); ?
>
620 <td
class="detail" colspan
="4"><b
>
621 <?php
echo xlt('Grand Total'); ?
>
623 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
631 <td align
="right"><b
>
632 <?php
echo text($grandqty); ?
>
634 <td align
="right"><b
>
635 <?php
text(bucks($grandtotal)); ?
>
638 <?php
$report_from_date = oeFormatShortDate($form_from_date) ;
639 $report_to_date = oeFormatShortDate($form_to_date) ;
641 <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
>
644 } // End not csv export
647 if (! $_POST['form_csvexport']) {
648 if($_POST['form_refresh']){
653 </div
> <!-- report results
-->
656 <?php
echo xlt('Please input search criteria above, and click Submit to view results.' ); ?
>
664 <!-- stuff
for the popup calendar
-->
665 <link rel
='stylesheet' href
='<?php echo $css_header ?>' type
='text/css'>
666 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
667 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
668 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
669 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
671 <script language
="Javascript">
672 Calendar
.setup({inputField
:"form_from_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
673 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});
678 } // End not csv export