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";
34 $form_provider = $_POST['form_provider'];
35 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
36 $form_details = $_POST['form_details'] ?
true : false;
40 $form_details = false;
42 function bucks($amount) {
43 if ($amount) echo oeFormatMoney($amount);
46 function display_desc($desc) {
47 if (preg_match('/^\S*?:(.+)$/', $desc, $matches)) {
53 function thisLineItem($patient_id, $encounter_id, $rowcat, $description, $transdate, $qty, $amount, $irnumber='') {
54 global $product, $category, $producttotal, $productqty, $cattotal, $catqty, $grandtotal, $grandqty;
55 global $productleft, $catleft;
57 $invnumber = $irnumber ?
$irnumber : "$patient_id.$encounter_id";
58 $rowamount = sprintf('%01.2f', $amount);
60 $patdata = sqlQuery("SELECT " .
61 "p.fname, p.mname, p.lname, p.pubpid, p.DOB, " .
62 "p.street, p.city, p.state, p.postal_code, " .
63 "p.ss, p.sex, p.status, p.phone_home, " .
64 "p.phone_biz, p.phone_cell, p.hipaa_notice " .
65 "FROM patient_data AS p " .
66 "WHERE p.pid = ? LIMIT 1", array($patient_id));
68 $pat_name = $patdata['fname'] . ' ' . $patdata['mname'] . ' ' . $patdata['lname'];
70 if (empty($rowcat)) $rowcat = xl('None');
71 $rowproduct = $description;
72 if (! $rowproduct) $rowproduct = xl('Unknown');
74 if ($product != $rowproduct ||
$category != $rowcat) {
76 // Print product total.
77 if ($_POST['form_csvexport']) {
78 if (! $_POST['form_details']) {
79 echo '"' . display_desc($category) . '",';
80 echo '"' . display_desc($product) . '",';
81 echo '"' . $productqty . '",';
82 echo '"'; bucks($producttotal); echo '"' . "\n";
87 <tr bgcolor
="#ddddff">
89 <?php
echo text(display_desc($catleft)); $catleft = " "; ?
>
91 <td
class="detail" colspan
="3">
92 <?php
if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?
>
94 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
103 <?php
echo text($productqty); ?
>
106 <?php
text(bucks($producttotal)); ?
>
110 } // End not csv export
114 $product = $rowproduct;
115 $productleft = $product;
118 if ($category != $rowcat) {
120 // Print category total.
121 if (!$_POST['form_csvexport']) {
124 <tr bgcolor
="#ffdddd">
128 <td
class="detail" colspan
="3">
129 <?php
echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?
>
131 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
140 <?php
echo text($catqty); ?
>
143 <?php
text(bucks($cattotal)); ?
>
147 } // End not csv export
152 $catleft = $category;
155 if ($_POST['form_details']) {
156 if ($_POST['form_csvexport']) {
157 echo '"' . display_desc($category ) . '",';
158 echo '"' . display_desc($product ) . '",';
159 echo '"' . oeFormatShortDate(display_desc($transdate)) . '",';
160 if($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2 ) {
161 echo '"' . $pat_name . '",';
163 if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {
164 echo '"' . display_desc($invnumber) . '",';
166 if($GLOBALS['sales_report_invoice'] == 1) {
167 echo '"' . $patient_id . '",';
169 // echo '"' . display_desc($invnumber) . '",';
170 echo '"' . display_desc($qty ) . '",';
171 echo '"'; bucks($rowamount); echo '"' . "\n";
178 <?php
echo text(display_desc($catleft)); $catleft = " "; ?
>
181 <?php
echo text(display_desc($productleft)); $productleft = " "; ?
>
184 <?php
echo text(oeFormatShortDate($transdate)); ?
>
186 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
190 <?php
} if($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2 ) { ?
>
192 <?php
echo text($pat_name); ?
>
196 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) { ?
>
197 <a href
='../patient_file/pos_checkout.php?ptid=<?php echo attr($patient_id); ?>&enc=<?php echo attr($encounter_id); ?>'>
198 <?php
echo text($invnumber); ?
></a
>
200 if($GLOBALS['sales_report_invoice'] == 1 ) {
201 echo text($patient_id);
205 <?php
if($GLOBALS['sales_report_invoice'] == 0) {?
>
211 <?php
echo text($qty); ?
>
214 <?php
text(bucks($rowamount)); ?
>
219 } // End not csv export
221 $producttotal +
= $rowamount;
222 $cattotal +
= $rowamount;
223 $grandtotal +
= $rowamount;
229 if (! acl_check('acct', 'rep')) die(xl("Unauthorized access."));
232 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
233 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
234 $form_facility = $_POST['form_facility'];
236 if ($_POST['form_csvexport']) {
237 header("Pragma: public");
238 header("Expires: 0");
239 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
240 header("Content-Type: application/force-download");
241 header("Content-Disposition: attachment; filename=sales_by_item.csv");
242 header("Content-Description: File Transfer");
244 if ($_POST['form_details']) {
248 if($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2 ) {
251 if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {
254 if($GLOBALS['sales_report_invoice'] == 1) {
258 echo '"Amount"' . "\n";
264 echo '"Total"' . "\n";
271 <?php
html_header_show();?
>
272 <style type
="text/css">
274 /* specifically include & exclude from printing */
280 #report_parameters_daterange {
289 /* specifically exclude some from the screen */
291 #report_parameters_daterange {
297 table
.mymaintable
, table
.mymaintable td
{
298 border
: 1px solid
#aaaaaa;
299 border
-collapse
: collapse
;
301 table
.mymaintable td
{
302 padding
: 1pt
4pt
1pt
4pt
;
307 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-9-1/index.js"></script
>
308 <script type
="text/javascript" src
="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script
>
310 <script language
="JavaScript">
312 $
(document
).ready(function() {
313 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
314 var win
= top
.printLogSetup ? top
: opener
.top
;
315 win
.printLogSetup(document
.getElementById('printbutton'));
322 <title
><?php
echo xlt('Sales by Item') ?
></title
>
324 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0' class="body_top">
326 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Sales by Item'); ?
></span
>
328 <form method
='post' action
='sales_by_item.php' id
='theform'>
330 <div id
="report_parameters">
331 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
332 <input type
='hidden' name
='form_csvexport' id
='form_csvexport' value
=''/>
336 <div style
='float:left'>
340 <?php
echo xlt('Facility'); ?
>:
343 <?php
dropdown_facility($form_facility, 'form_facility', true); ?
>
346 <?php
echo xlt('From'); ?
>:
349 <input type
='text' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo attr($form_from_date) ?>'
350 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
351 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
352 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
353 title
='<?php echo xla('Click here to choose a date
'); ?>'>
356 <?php
echo xlt('To'); ?
>:
359 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr($form_to_date) ?>'
360 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
361 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
362 id
='img_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
363 title
='<?php echo xla('Click here to choose a date
'); ?>'>
370 <?php
echo xlt('Provider'); ?
>:
374 if (acl_check('acct', 'rep_a')) {
375 // Build a drop-down list of providers.
376 $query = "select id, lname, fname from users where " .
377 "authorized = 1 order by lname, fname";
378 $res = sqlStatement($query);
379 echo " <select name='form_provider'>\n";
380 echo " <option value=''>-- " . xlt('All Providers') . " --\n";
381 while ($row = sqlFetchArray($res)) {
382 $provid = $row['id'];
383 echo " <option value='". attr($provid) ."'";
384 if ($provid == $_REQUEST['form_provider']) echo " selected";
385 echo ">" . text($row['lname']) . ", " . text($row['fname']) . "\n";
389 echo "<input type='hidden' name='form_provider' value='" . attr($_SESSION['authUserID']) . "'>";
395 <label
><input type
='checkbox' name
='form_details'<?php
if ($form_details) echo ' checked'; ?
>>
396 <?php
echo xlt('Details'); ?
></label
>
403 <td align
='left' valign
='middle' height
="100%">
404 <table style
='border-left:1px solid; width:100%; height:100%' >
407 <div style
='margin-left:15px'>
408 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
410 <?php
echo xlt('Submit'); ?
>
414 <?php
if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) { ?
>
415 <a href
='#' class='css_button' id
='printbutton'>
417 <?php
echo xlt('Print'); ?
>
420 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
422 <?php
echo xlt('CSV Export'); ?
>
434 </div
> <!-- end of parameters
-->
437 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
440 <div id
="report_results">
441 <table width
='98%' id
='mymaintable' class='mymaintable'>
444 <?php
echo xlt('Category'); ?
>
447 <?php
echo xlt('Item'); ?
>
450 <?php
if ($form_details) echo xlt('Date'); ?
>
452 <?php
if($GLOBALS['sales_report_invoice'] == 2) {?
>
459 if($GLOBALS['sales_report_invoice'] == 0) {
460 if ($form_details) echo ' ';
465 if ($form_details) echo xlt('Invoice'); }
466 if($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2 ) {
467 if ($form_details) echo xlt('Name');
472 if($GLOBALS['sales_report_invoice'] == 2) {
473 if ($form_details) echo xlt('Invoice');
475 if($GLOBALS['sales_report_invoice'] == 1) {
476 if ($form_details) echo xlt('ID');
481 <?php
echo xlt('Qty'); ?
>
484 <?php
echo xlt('Amount'); ?
>
492 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
493 $from_date = $form_from_date . ' 00:00:00';
494 $to_date = $form_to_date . ' 23:59:59';
506 $sqlBindArray = array();
507 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.units, " .
508 "b.code_text, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno, lo.title " .
509 "FROM billing AS b " .
510 "JOIN code_types AS ct ON ct.ct_key = b.code_type " .
511 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
512 "LEFT JOIN codes AS c ON c.code_type = ct.ct_id AND c.code = b.code AND c.modifier = b.modifier " .
513 "LEFT JOIN list_options AS lo ON lo.list_id = 'superbill' AND lo.option_id = c.superbill AND lo.activity = 1 " .
514 "WHERE b.code_type != 'COPAY' AND b.activity = 1 AND b.fee != 0 AND " .
515 "fe.date >= ? AND fe.date <= ?";
516 array_push($sqlBindArray,$from_date,$to_date);
517 // If a facility was specified.
518 if ($form_facility) {
519 $query .= " AND fe.facility_id = ?";
520 array_push($sqlBindArray,$form_facility);
522 if ($form_provider) {
523 $query .= " AND fe.provider_id = ?";
524 array_push($sqlBindArray,$form_provider);
526 $query .= " ORDER BY lo.title, b.code, fe.date, fe.id";
528 $res = sqlStatement($query,$sqlBindArray);
529 while ($row = sqlFetchArray($res)) {
530 thisLineItem($row['pid'], $row['encounter'],
531 $row['title'], $row['code'] . ' ' . $row['code_text'],
532 substr($row['date'], 0, 10), $row['units'], $row['fee'], $row['invoice_refno']);
535 $sqlBindArray = array();
536 $query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " .
537 "d.name, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno " .
538 "FROM drug_sales AS s " .
539 "JOIN drugs AS d ON d.drug_id = s.drug_id " .
540 "JOIN form_encounter AS fe ON " .
541 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
542 "fe.date >= ? AND fe.date <= ? " .
544 array_push($sqlBindArray,$from_date,$to_date);
545 // If a facility was specified.
546 if ($form_facility) {
547 $query .= " AND fe.facility_id = ?";
548 array_push($sqlBindArray,$form_facility);
550 if ($form_provider) {
551 $query .= " AND fe.provider_id = ?";
552 array_push($sqlBindArray,$form_provider);
554 $query .= " ORDER BY d.name, fe.date, fe.id";
556 $res = sqlStatement($query,$sqlBindArray);
557 while ($row = sqlFetchArray($res)) {
558 thisLineItem($row['pid'], $row['encounter'], xl('Products'), $row['name'],
559 substr($row['date'], 0, 10), $row['quantity'], $row['fee'], $row['invoice_refno']);
562 if ($_POST['form_csvexport']) {
563 if (! $_POST['form_details']) {
564 echo '"' . display_desc($product) . '",';
565 echo '"' . $productqty . '",';
566 echo '"'; bucks($producttotal); echo '"' . "\n";
572 <tr bgcolor
="#ddddff">
574 <?php
echo text(display_desc($catleft)); $catleft = " "; ?
>
576 <td
class="detail" colspan
="3">
577 <?php
if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?
>
579 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
588 <?php
echo text($productqty); ?
>
591 <?php
text(bucks($producttotal)); ?
>
595 <tr bgcolor
="#ffdddd">
599 <td
class="detail" colspan
="3"><b
>
600 <?php
echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?
>
602 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
610 <td align
="right"><b
>
611 <?php
echo text($catqty); ?
>
613 <td align
="right"><b
>
614 <?php
text(bucks($cattotal)); ?
>
619 <td
class="detail" colspan
="4"><b
>
620 <?php
echo xlt('Grand Total'); ?
>
622 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
630 <td align
="right"><b
>
631 <?php
echo text($grandqty); ?
>
633 <td align
="right"><b
>
634 <?php
text(bucks($grandtotal)); ?
>
637 <?php
$report_from_date = oeFormatShortDate($form_from_date) ;
638 $report_to_date = oeFormatShortDate($form_to_date) ;
640 <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
>
643 } // End not csv export
646 if (! $_POST['form_csvexport']) {
647 if($_POST['form_refresh']){
652 </div
> <!-- report results
-->
655 <?php
echo xlt('Please input search criteria above, and click Submit to view results.' ); ?
>
663 <!-- stuff
for the popup calendar
-->
664 <link rel
='stylesheet' href
='<?php echo $css_header ?>' type
='text/css'>
665 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
666 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
667 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
668 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
670 <script language
="Javascript">
671 Calendar
.setup({inputField
:"form_from_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
672 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});
677 } // End not csv export