2 // Copyright (C) 2010 Rod Roark <rod@sunsetsystems.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This is an inventory transactions list.
11 //SANITIZE ALL ESCAPES
12 $sanitize_all_escapes=true;
15 //STOP FAKE REGISTER GLOBALS
16 $fake_register_globals=false;
19 require_once("../globals.php");
20 require_once("$srcdir/patient.inc");
21 require_once("$srcdir/acl.inc");
22 require_once("$srcdir/formatting.inc.php");
24 function bucks($amount) {
25 if ($amount != 0) return oeFormatMoney($amount);
29 function esc4Export($str) {
30 return str_replace('"', '\\"', $str);
33 function thisLineItem($row, $xfer=false) {
34 global $grandtotal, $grandqty, $encount, $form_action;
39 if (!empty($row['pid'])) {
41 $dpname = $row['plname'];
42 if (!empty($row['pfname'])) {
43 $dpname .= ', ' . $row['pfname'];
44 if (!empty($row['pmname'])) $dpname .= ' ' . $row['pmname'];
46 $invnumber = empty($row['invoice_refno']) ?
47 "{$row['pid']}.{$row['encounter']}" : $row['invoice_refno'];
49 else if (!empty($row['distributor_id'])) {
50 $ttype = xl('Distribution');
51 if (!empty($row['organization'])) {
52 $dpname = $row['organization'];
55 $dpname = $row['dlname'];
56 if (!empty($row['dfname'])) {
57 $dpname .= ', ' . $row['dfname'];
58 if (!empty($row['dmname'])) $dpname .= ' ' . $row['dmname'];
62 else if (!empty($row['xfer_inventory_id']) ||
$xfer) {
63 $ttype = xl('Transfer');
65 else if ($row['fee'] != 0) {
66 $ttype = xl('Purchase');
69 $ttype = xl('Adjustment');
72 if ($form_action == 'export') {
73 echo '"' . oeFormatShortDate($row['sale_date']) . '",';
74 echo '"' . $ttype . '",';
75 echo '"' . esc4Export($row['name']) . '",';
76 echo '"' . esc4Export($row['lot_number']) . '",';
77 echo '"' . esc4Export($row['warehouse']) . '",';
78 echo '"' . esc4Export($dpname) . '",';
79 echo '"' . (0 - $row['quantity']) . '",';
80 echo '"' . bucks($row['fee']) . '",';
81 echo '"' . $row['billed'] . '",';
82 echo '"' . esc4Export($row['notes']) . '"' . "\n";
85 $bgcolor = (++
$encount & 1) ?
"#ddddff" : "#ffdddd";
88 <tr bgcolor
="<?php echo $bgcolor; ?>">
90 <?php
echo htmlspecialchars(oeFormatShortDate($row['sale_date'])); ?
>
93 <?php
echo htmlspecialchars($ttype); ?
>
96 <?php
echo htmlspecialchars($row['name']); ?
>
99 <?php
echo htmlspecialchars($row['lot_number']); ?
>
102 <?php
echo htmlspecialchars($row['warehouse']); ?
>
105 <?php
echo htmlspecialchars($dpname); ?
>
107 <td
class="detail" align
="right">
108 <?php
echo htmlspecialchars(0 - $row['quantity']); ?
>
110 <td
class="detail" align
="right">
111 <?php
echo htmlspecialchars(bucks($row['fee'])); ?
>
113 <td
class="detail" align
="center">
114 <?php
echo empty($row['billed']) ?
' ' : '*'; ?
>
117 <?php
echo htmlspecialchars($row['notes']); ?
>
121 } // End not csv export
123 $grandtotal +
= $row['fee'];
124 $grandqty -= $row['quantity'];
126 // In the special case of a transfer, generate a second line item for
128 if (!empty($row['xfer_inventory_id'])) {
129 $row['xfer_inventory_id'] = 0;
130 $row['lot_number'] = $row['lot_number_2'];
131 $row['warehouse'] = $row['warehouse_2'];
132 $row['quantity'] = 0 - $row['quantity'];
133 $row['fee'] = 0 - $row['fee'];
134 thisLineItem($row, true);
139 if (! acl_check('acct', 'rep')) die(htmlspecialchars(xl("Unauthorized access."), ENT_NOQUOTES
));
141 // this is "" or "submit" or "export".
142 $form_action = $_POST['form_action'];
144 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
145 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
146 $form_trans_type = isset($_POST['form_trans_type']) ?
$_POST['form_trans_type'] : '0';
150 if ($form_action == 'export') {
151 header("Pragma: public");
152 header("Expires: 0");
153 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
154 header("Content-Type: application/force-download");
155 header("Content-Disposition: attachment; filename=inventory_transactions.csv");
156 header("Content-Description: File Transfer");
158 echo '"' . xl('Date' ) . '",';
159 echo '"' . xl('Transaction') . '",';
160 echo '"' . xl('Product' ) . '",';
161 echo '"' . xl('Lot' ) . '",';
162 echo '"' . xl('Warehouse' ) . '",';
163 echo '"' . xl('Who' ) . '",';
164 echo '"' . xl('Qty' ) . '",';
165 echo '"' . xl('Amount' ) . '",';
166 echo '"' . xl('Billed' ) . '",';
167 echo '"' . xl('Notes' ) . '"' . "\n";
173 <?php
html_header_show(); ?
>
174 <title
><?php
echo htmlspecialchars(xl('Inventory Transactions'), ENT_NOQUOTES
) ?
></title
>
175 <link rel
='stylesheet' href
='<?php echo $css_header ?>' type
='text/css'>
177 <style type
="text/css">
178 /* specifically include & exclude from printing */
180 #report_parameters {visibility: hidden; display: none;}
181 #report_parameters_daterange {visibility: visible; display: inline;}
182 #report_results {margin-top: 30px;}
184 /* specifically exclude some from the screen */
186 #report_parameters_daterange {visibility: hidden; display: none;}
188 body
{ font
-family
:sans
-serif
; font
-size
:10pt
; font
-weight
:normal
}
189 .dehead
{ color
:#000000; font-family:sans-serif; font-size:10pt; font-weight:bold }
190 .detail
{ color
:#000000; font-family:sans-serif; font-size:10pt; font-weight:normal }
193 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
194 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
195 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
196 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
198 <script language
='JavaScript'>
199 function mysubmit(action
) {
200 var f
= document
.forms
[0];
201 f
.form_action
.value
= action
;
202 top
.restoreSession();
209 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0' class='body_top'>
212 <h2
><?php
echo htmlspecialchars(xl('Inventory Transactions'), ENT_NOQUOTES
) ?
></h2
>
214 <form method
='post' action
='inventory_transactions.php'>
216 <div id
="report_parameters">
217 <!-- form_action is set to
"submit" or "export" at form submit time
-->
218 <input type
='hidden' name
='form_action' value
='' />
225 <?php
echo htmlspecialchars(xl('Type'), ENT_NOQUOTES
); ?
>:
228 <select name
='form_trans_type' onchange
='trans_type_changed()'>
232 '2' => xl('Purchase/Return'),
234 '6' => xl('Distribution'),
235 '4' => xl('Transfer'),
236 '5' => xl('Adjustment'),
239 echo " <option value='$key'";
240 if ($key == $form_trans_type) echo " selected";
241 echo ">" . htmlspecialchars($value, ENT_NOQUOTES
) . "</option>\n";
247 <?php
echo htmlspecialchars(xl('From'), ENT_NOQUOTES
); ?
>:
250 <input type
='text' name
='form_from_date' id
="form_from_date" size
='10'
251 value
='<?php echo htmlspecialchars($form_from_date, ENT_QUOTES) ?>'
252 title
='<?php echo htmlspecialchars(xl('yyyy
-mm
-dd
'), ENT_QUOTES) ?>'
253 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'>
254 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
255 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
256 title
='<?php echo htmlspecialchars(xl('Click here to choose a date
'), ENT_QUOTES); ?>'>
259 <?php
xl('To','e'); ?
>:
262 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10'
263 value
='<?php echo htmlspecialchars($form_to_date, ENT_QUOTES) ?>'
264 title
='<?php echo htmlspecialchars(xl('yyyy
-mm
-dd
'), ENT_QUOTES) ?>'
265 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'>
266 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
267 id
='img_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
268 title
='<?php echo htmlspecialchars(xl('Click here to choose a date
'), ENT_QUOTES); ?>'>
273 <td align
='left' valign
='middle'>
274 <table style
='border-left:1px solid; width:100%; height:100%'>
277 <a href
='#' class='css_button' onclick
='mysubmit("submit")' style
='margin-left:1em'>
278 <span
><?php
echo htmlspecialchars(xl('Submit'), ENT_NOQUOTES
); ?
></span
>
280 <?php
if ($form_action) { ?
>
281 <a href
='#' class='css_button' onclick
='window.print()' style
='margin-left:1em'>
282 <span
><?php
echo htmlspecialchars(xl('Print'), ENT_NOQUOTES
); ?
></span
>
284 <a href
='#' class='css_button' onclick
='mysubmit("export")' style
='margin-left:1em'>
285 <span
><?php
echo htmlspecialchars(xl('CSV Export'), ENT_NOQUOTES
); ?
></span
>
296 <?php
if ($form_action) { // if submit (already not export here) ?>
298 <div id
="report_results">
299 <table border
='0' cellpadding
='1' cellspacing
='2' width
='98%'>
300 <tr bgcolor
="#dddddd">
302 <?php
echo htmlspecialchars(xl('Date'), ENT_NOQUOTES
); ?
>
305 <?php
echo htmlspecialchars(xl('Transaction'), ENT_NOQUOTES
); ?
>
308 <?php
echo htmlspecialchars(xl('Product'), ENT_NOQUOTES
); ?
>
311 <?php
echo htmlspecialchars(xl('Lot'), ENT_NOQUOTES
); ?
>
314 <?php
echo htmlspecialchars(xl('Warehouse'), ENT_NOQUOTES
); ?
>
317 <?php
echo htmlspecialchars(xl('Who'), ENT_NOQUOTES
); ?
>
319 <td
class="dehead" align
="right">
320 <?php
echo htmlspecialchars(xl('Qty'), ENT_NOQUOTES
); ?
>
322 <td
class="dehead" align
="right">
323 <?php
echo htmlspecialchars(xl('Amount'), ENT_NOQUOTES
); ?
>
325 <td
class="dehead" align
="Center">
326 <?php
echo htmlspecialchars(xl('Billed'), ENT_NOQUOTES
); ?
>
329 <?php
echo htmlspecialchars(xl('Notes'), ENT_NOQUOTES
); ?
>
336 if ($form_action) { // if submit or export
337 $from_date = $form_from_date;
338 $to_date = $form_to_date;
343 $query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " .
344 "s.billed, s.notes, s.distributor_id, s.xfer_inventory_id, " .
345 "p.fname AS pfname, p.mname AS pmname, p.lname AS plname, " .
346 "u.fname AS dfname, u.mname AS dmname, u.lname AS dlname, u.organization, " .
347 "d.name, fe.date, fe.invoice_refno, " .
348 "i1.lot_number, i2.lot_number AS lot_number_2, " .
349 "lo1.title AS warehouse, lo2.title AS warehouse_2 " .
350 "FROM drug_sales AS s " .
351 "JOIN drugs AS d ON d.drug_id = s.drug_id " .
352 "LEFT JOIN drug_inventory AS i1 ON i1.inventory_id = s.inventory_id " .
353 "LEFT JOIN drug_inventory AS i2 ON i2.inventory_id = s.xfer_inventory_id " .
354 "LEFT JOIN patient_data AS p ON p.pid = s.pid " .
355 "LEFT JOIN users AS u ON u.id = s.distributor_id " .
356 "LEFT JOIN list_options AS lo1 ON lo1.list_id = 'warehouse' AND " .
357 "lo1.option_id = i1.warehouse_id " .
358 "LEFT JOIN list_options AS lo2 ON lo2.list_id = 'warehouse' AND " .
359 "lo2.option_id = i2.warehouse_id " .
360 "LEFT JOIN form_encounter AS fe ON fe.pid = s.pid AND fe.encounter = s.encounter " .
361 "WHERE s.sale_date >= ? AND s.sale_date <= ? ";
362 if ($form_trans_type == 2) { // purchase/return
363 $query .= "AND s.pid = 0 AND s.distributor_id = 0 AND s.xfer_inventory_id = 0 AND s.fee != 0 ";
365 else if ($form_trans_type == 4) { // transfer
366 $query .= "AND s.xfer_inventory_id != 0 ";
368 else if ($form_trans_type == 5) { // adjustment
369 $query .= "AND s.pid = 0 AND s.distributor_id = 0 AND s.xfer_inventory_id = 0 AND s.fee = 0 ";
371 else if ($form_trans_type == 6) { // distribution
372 $query .= "AND s.distributor_id != 0 ";
374 else if ($form_trans_type == 1) { // sale
375 $query .= "AND s.pid != 0 ";
377 $query .= "ORDER BY s.sale_date, s.sale_id";
379 $res = sqlStatement($query, array($from_date, $to_date));
380 while ($row = sqlFetchArray($res)) {
384 // Grand totals line.
385 if ($form_action != 'export') { // if submit
388 <tr bgcolor
="#dddddd">
389 <td
class="dehead" colspan
="6">
390 <?php
echo htmlspecialchars(xl('Grand Total'), ENT_NOQUOTES
); ?
>
392 <td
class="dehead" align
="right">
393 <?php
echo htmlspecialchars($grandqty, ENT_NOQUOTES
); ?
>
395 <td
class="dehead" align
="right">
396 <?php
echo htmlspecialchars(bucks($grandtotal), ENT_NOQUOTES
); ?
>
398 <td
class="dehead" colspan
="2">
405 } // end if submit or export
407 if ($form_action != 'export') {
413 } // end if ($form_action)
420 <!-- stuff
for the popup calendar
-->
421 <script language
="Javascript">
422 Calendar
.setup({inputField
:"form_from_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
423 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});