3 * This is a report of sales by item description. It's driven from
4 * SQL-Ledger so as to include all types of invoice items.
6 * Copyright (C) 2015 Terry Hill <terry@lillysystems.com>
7 * Copyright (C) 2006-2010 Rod Roark <rod@sunsetsystems.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 * @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/sql-ledger.inc");
31 require_once("$srcdir/acl.inc");
32 require_once("$srcdir/formatting.inc.php");
33 require_once "$srcdir/options.inc.php";
34 require_once "$srcdir/formdata.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."));
233 $INTEGRATED_AR = $GLOBALS['oer_config']['ws_accounting']['enabled'] === 2;
235 if (!$INTEGRATED_AR) SLConnect();
237 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
238 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
239 $form_facility = $_POST['form_facility'];
241 if ($_POST['form_csvexport']) {
242 header("Pragma: public");
243 header("Expires: 0");
244 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
245 header("Content-Type: application/force-download");
246 header("Content-Disposition: attachment; filename=sales_by_item.csv");
247 header("Content-Description: File Transfer");
249 if ($_POST['form_details']) {
253 if($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2 ) {
256 if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {
259 if($GLOBALS['sales_report_invoice'] == 1) {
263 echo '"Amount"' . "\n";
269 echo '"Total"' . "\n";
276 <?php
html_header_show();?
>
277 <style type
="text/css">
278 /* specifically include & exclude from printing */
284 #report_parameters_daterange {
293 /* specifically exclude some from the screen */
295 #report_parameters_daterange {
302 <title
><?php
echo xlt('Sales by Item') ?
></title
>
304 <script type
="text/javascript" src
="../../library/js/jquery.1.3.2.js"></script
>
306 <script language
="JavaScript">
307 $
(document
).ready(function() {
308 var win
= top
.printLogSetup ? top
: opener
.top
;
309 win
.printLogSetup(document
.getElementById('printbutton'));
315 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0' class="body_top">
317 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Sales by Item'); ?
></span
>
319 <form method
='post' action
='sales_by_item.php' id
='theform'>
321 <div id
="report_parameters">
322 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
323 <input type
='hidden' name
='form_csvexport' id
='form_csvexport' value
=''/>
327 <div style
='float:left'>
331 <?php
echo xlt('Facility'); ?
>:
334 <?php
dropdown_facility($form_facility, 'form_facility', true); ?
>
337 <?php
echo xlt('From'); ?
>:
340 <input type
='text' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo attr($form_from_date) ?>'
341 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
342 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
343 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
344 title
='<?php echo xla('Click here to choose a date
'); ?>'>
347 <?php
echo xlt('To'); ?
>:
350 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr($form_to_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_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
354 title
='<?php echo xla('Click here to choose a date
'); ?>'>
361 <?php
echo xlt('Provider'); ?
>:
365 if (acl_check('acct', 'rep_a')) {
366 // Build a drop-down list of providers.
367 $query = "select id, lname, fname from users where " .
368 "authorized = 1 order by lname, fname";
369 $res = sqlStatement($query);
370 echo " <select name='form_provider'>\n";
371 echo " <option value=''>-- " . xlt('All Providers') . " --\n";
372 while ($row = sqlFetchArray($res)) {
373 $provid = $row['id'];
374 echo " <option value='". attr($provid) ."'";
375 if ($provid == $_REQUEST['form_provider']) echo " selected";
376 echo ">" . text($row['lname']) . ", " . text($row['fname']) . "\n";
380 echo "<input type='hidden' name='form_provider' value='" . attr($_SESSION['authUserID']) . "'>";
386 <label
><input type
='checkbox' name
='form_details'<?php
if ($form_details) echo ' checked'; ?
>>
387 <?php
echo xlt('Details'); ?
></label
>
394 <td align
='left' valign
='middle' height
="100%">
395 <table style
='border-left:1px solid; width:100%; height:100%' >
398 <div style
='margin-left:15px'>
399 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
401 <?php
echo xlt('Submit'); ?
>
405 <?php
if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) { ?
>
406 <a href
='#' class='css_button' id
='printbutton'>
408 <?php
echo xlt('Print'); ?
>
411 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
413 <?php
echo xlt('CSV Export'); ?
>
425 </div
> <!-- end of parameters
-->
428 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
430 <div id
="report_results">
434 <?php
echo xlt('Category'); ?
>
437 <?php
echo xlt('Item'); ?
>
440 <?php
if ($form_details) echo xlt('Date'); ?
>
442 <?php
if($GLOBALS['sales_report_invoice'] == 2) {?
>
449 if($GLOBALS['sales_report_invoice'] == 0) {
450 if ($form_details) echo ' ';
455 if ($form_details) echo xlt('Invoice'); }
456 if($GLOBALS['sales_report_invoice'] == 1 ||
$GLOBALS['sales_report_invoice'] == 2 ) {
457 if ($form_details) echo xlt('Name');
462 if($GLOBALS['sales_report_invoice'] == 2) {
463 if ($form_details) echo xlt('Invoice');
465 if($GLOBALS['sales_report_invoice'] == 1) {
466 if ($form_details) echo xlt('ID');
471 <?php
echo xlt('Qty'); ?
>
474 <?php
echo xlt('Amount'); ?
>
481 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
482 $from_date = $form_from_date . ' 00:00:00';
483 $to_date = $form_to_date . ' 23:59:59';
495 if ($INTEGRATED_AR) {
496 $sqlBindArray = array();
497 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.units, " .
498 "b.code_text, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno, lo.title " .
499 "FROM billing AS b " .
500 "JOIN code_types AS ct ON ct.ct_key = b.code_type " .
501 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
502 "LEFT JOIN codes AS c ON c.code_type = ct.ct_id AND c.code = b.code AND c.modifier = b.modifier " .
503 "LEFT JOIN list_options AS lo ON lo.list_id = 'superbill' AND lo.option_id = c.superbill " .
504 "WHERE b.code_type != 'COPAY' AND b.activity = 1 AND b.fee != 0 AND " .
505 "fe.date >= ? AND fe.date <= ?";
506 array_push($sqlBindArray,$from_date,$to_date);
507 // If a facility was specified.
508 if ($form_facility) {
509 $query .= " AND fe.facility_id = ?";
510 array_push($sqlBindArray,$form_facility);
512 if ($form_provider) {
513 $query .= " AND fe.provider_id = ?";
514 array_push($sqlBindArray,$form_provider);
516 $query .= " ORDER BY lo.title, b.code, fe.date, fe.id";
518 $res = sqlStatement($query,$sqlBindArray);
519 while ($row = sqlFetchArray($res)) {
520 thisLineItem($row['pid'], $row['encounter'],
521 $row['title'], $row['code'] . ' ' . $row['code_text'],
522 substr($row['date'], 0, 10), $row['units'], $row['fee'], $row['invoice_refno']);
525 $sqlBindArray = array();
526 $query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " .
527 "d.name, fe.date, fe.facility_id, fe.provider_id, fe.invoice_refno " .
528 "FROM drug_sales AS s " .
529 "JOIN drugs AS d ON d.drug_id = s.drug_id " .
530 "JOIN form_encounter AS fe ON " .
531 "fe.pid = s.pid AND fe.encounter = s.encounter AND " .
532 "fe.date >= ? AND fe.date <= ? " .
534 array_push($sqlBindArray,$from_date,$to_date);
535 // If a facility was specified.
536 if ($form_facility) {
537 $query .= " AND fe.facility_id = ?";
538 array_push($sqlBindArray,$form_facility);
540 if ($form_provider) {
541 $query .= " AND fe.provider_id = ?";
542 array_push($sqlBindArray,$form_provider);
544 $query .= " ORDER BY d.name, fe.date, fe.id";
546 $res = sqlStatement($query,$sqlBindArray);
547 while ($row = sqlFetchArray($res)) {
548 thisLineItem($row['pid'], $row['encounter'], xl('Products'), $row['name'],
549 substr($row['date'], 0, 10), $row['quantity'], $row['fee'], $row['invoice_refno']);
553 $query = "SELECT ar.invnumber, ar.transdate, " .
554 "invoice.description, invoice.qty, invoice.sellprice " .
555 "FROM ar, invoice WHERE " .
556 "ar.transdate >= '$from_date' AND ar.transdate <= '$to_date' " .
557 "AND invoice.trans_id = ar.id " .
558 "ORDER BY invoice.description, ar.transdate, ar.id";
559 $t_res = SLQuery($query);
560 if ($sl_err) die($sl_err);
561 for ($irow = 0; $irow < SLRowCount($t_res); ++
$irow) {
562 $row = SLGetRow($t_res, $irow);
563 list($patient_id, $encounter_id) = explode(".", $row['invnumber']);
564 // If a facility was specified then skip invoices whose encounters
565 // do not indicate that facility.
566 if ($form_facility) {
567 $tmp = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " .
568 "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
569 "facility_id = '$form_facility'");
570 if (empty($tmp['count'])) continue;
572 thisLineItem($patient_id, $encounter_id, '', $row['description'],
573 $row['transdate'], $row['qty'], $row['sellprice'] * $row['qty']);
575 } // end not $INTEGRATED_AR
577 if ($_POST['form_csvexport']) {
578 if (! $_POST['form_details']) {
579 echo '"' . display_desc($product) . '",';
580 echo '"' . $productqty . '",';
581 echo '"'; bucks($producttotal); echo '"' . "\n";
587 <tr bgcolor
="#ddddff">
589 <?php
echo text(display_desc($catleft)); $catleft = " "; ?
>
591 <td
class="detail" colspan
="3">
592 <?php
if ($_POST['form_details']) echo xlt('Total for') . ' '; echo text(display_desc($product)); ?
>
594 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
603 <?php
echo text($productqty); ?
>
606 <?php
text(bucks($producttotal)); ?
>
610 <tr bgcolor
="#ffdddd">
614 <td
class="detail" colspan
="3"><b
>
615 <?php
echo xlt('Total for category') . ' '; echo text(display_desc($category)); ?
>
617 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
625 <td align
="right"><b
>
626 <?php
echo text($catqty); ?
>
628 <td align
="right"><b
>
629 <?php
text(bucks($cattotal)); ?
>
634 <td
class="detail" colspan
="4"><b
>
635 <?php
echo xlt('Grand Total'); ?
>
637 <?php
if($GLOBALS['sales_report_invoice'] == 0 ||
$GLOBALS['sales_report_invoice'] == 2) {?
>
645 <td align
="right"><b
>
646 <?php
echo text($grandqty); ?
>
648 <td align
="right"><b
>
649 <?php
text(bucks($grandtotal)); ?
>
652 <?php
$report_from_date = oeFormatShortDate($form_from_date) ;
653 $report_to_date = oeFormatShortDate($form_to_date) ;
655 <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
>
658 } // End not csv export
660 if (!$INTEGRATED_AR) SLClose();
662 if (! $_POST['form_csvexport']) {
663 if($_POST['form_refresh']){
667 </div
> <!-- report results
-->
670 <?php
echo xlt('Please input search criteria above, and click Submit to view results.' ); ?
>
678 <!-- stuff
for the popup calendar
-->
679 <link rel
='stylesheet' href
='<?php echo $css_header ?>' type
='text/css'>
680 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
681 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
682 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
683 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
685 <script language
="Javascript">
686 Calendar
.setup({inputField
:"form_from_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
687 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});
692 } // End not csv export