Support for optional logging of print actions.
[openemr.git] / interface / reports / inventory_transactions.php
blob538359c1adb89a922f981fc09cbe448a90ac677d
1 <?php
2 // Copyright (C) 2010-2015 Rod Roark <rod@sunsetsystems.com>
3 //
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);
26 return '';
29 function esc4Export($str) {
30 return str_replace('"', '\\"', $str);
33 function thisLineItem($row, $xfer=false) {
34 global $grandtotal, $grandqty, $encount, $form_action;
36 $invnumber = '';
37 $dpname = '';
39 if (!empty($row['pid'])) {
40 $ttype = xl('Sale');
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'];
54 else {
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');
68 else {
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";
84 else {
85 $bgcolor = (++$encount & 1) ? "#ddddff" : "#ffdddd";
88 <tr bgcolor="<?php echo $bgcolor; ?>">
89 <td class="detail">
90 <?php echo htmlspecialchars(oeFormatShortDate($row['sale_date'])); ?>
91 </td>
92 <td class="detail">
93 <?php echo htmlspecialchars($ttype); ?>
94 </td>
95 <td class="detail">
96 <?php echo htmlspecialchars($row['name']); ?>
97 </td>
98 <td class="detail">
99 <?php echo htmlspecialchars($row['lot_number']); ?>
100 </td>
101 <td class="detail">
102 <?php echo htmlspecialchars($row['warehouse']); ?>
103 </td>
104 <td class="detail">
105 <?php echo htmlspecialchars($dpname); ?>
106 </td>
107 <td class="detail" align="right">
108 <?php echo htmlspecialchars(0 - $row['quantity']); ?>
109 </td>
110 <td class="detail" align="right">
111 <?php echo htmlspecialchars(bucks($row['fee'])); ?>
112 </td>
113 <td class="detail" align="center">
114 <?php echo empty($row['billed']) ? '&nbsp;' : '*'; ?>
115 </td>
116 <td class="detail">
117 <?php echo htmlspecialchars($row['notes']); ?>
118 </td>
119 </tr>
120 <?php
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
127 // the source lot.
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);
137 } // end function
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';
148 $encount = 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");
157 // CSV headers:
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";
168 } // end export
169 else {
171 <html>
172 <head>
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 */
179 @media print {
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 */
185 @media 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 }
191 </style>
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>
197 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.9.1.min.js"></script>
199 <script language='JavaScript'>
201 $(document).ready(function() {
202 var win = top.printLogSetup ? top : opener.top;
203 win.printLogSetup(document.getElementById('printbutton'));
206 function mysubmit(action) {
207 var f = document.forms[0];
208 f.form_action.value = action;
209 top.restoreSession();
210 f.submit();
213 </script>
215 </head>
217 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' class='body_top'>
218 <center>
220 <h2><?php echo htmlspecialchars(xl('Inventory Transactions'), ENT_NOQUOTES) ?></h2>
222 <form method='post' action='inventory_transactions.php'>
224 <div id="report_parameters">
225 <!-- form_action is set to "submit" or "export" at form submit time -->
226 <input type='hidden' name='form_action' value='' />
227 <table>
228 <tr>
229 <td width='50%'>
230 <table class='text'>
231 <tr>
232 <td class='label'>
233 <?php echo htmlspecialchars(xl('Type'), ENT_NOQUOTES); ?>:
234 </td>
235 <td nowrap>
236 <select name='form_trans_type' onchange='trans_type_changed()'>
237 <?php
238 foreach (array(
239 '0' => xl('All'),
240 '2' => xl('Purchase/Return'),
241 '1' => xl('Sale'),
242 '6' => xl('Distribution'),
243 '4' => xl('Transfer'),
244 '5' => xl('Adjustment'),
245 ) as $key => $value)
247 echo " <option value='$key'";
248 if ($key == $form_trans_type) echo " selected";
249 echo ">" . htmlspecialchars($value, ENT_NOQUOTES) . "</option>\n";
252 </select>
253 </td>
254 <td class='label'>
255 <?php echo htmlspecialchars(xl('From'), ENT_NOQUOTES); ?>:
256 </td>
257 <td nowrap>
258 <input type='text' name='form_from_date' id="form_from_date" size='10'
259 value='<?php echo htmlspecialchars($form_from_date, ENT_QUOTES) ?>'
260 title='<?php echo htmlspecialchars(xl('yyyy-mm-dd'), ENT_QUOTES) ?>'
261 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'>
262 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
263 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
264 title='<?php echo htmlspecialchars(xl('Click here to choose a date'), ENT_QUOTES); ?>'>
265 </td>
266 <td class='label'>
267 <?php xl('To','e'); ?>:
268 </td>
269 <td nowrap>
270 <input type='text' name='form_to_date' id="form_to_date" size='10'
271 value='<?php echo htmlspecialchars($form_to_date, ENT_QUOTES) ?>'
272 title='<?php echo htmlspecialchars(xl('yyyy-mm-dd'), ENT_QUOTES) ?>'
273 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'>
274 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
275 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
276 title='<?php echo htmlspecialchars(xl('Click here to choose a date'), ENT_QUOTES); ?>'>
277 </td>
278 </tr>
279 </table>
280 </td>
281 <td align='left' valign='middle'>
282 <table style='border-left:1px solid; width:100%; height:100%'>
283 <tr>
284 <td valign='middle'>
285 <a href='#' class='css_button' onclick='mysubmit("submit")' style='margin-left:1em'>
286 <span><?php echo htmlspecialchars(xl('Submit'), ENT_NOQUOTES); ?></span>
287 </a>
288 <?php if ($form_action) { ?>
289 <a href='#' class='css_button' id='printbutton' style='margin-left:1em'>
290 <span><?php echo htmlspecialchars(xl('Print'), ENT_NOQUOTES); ?></span>
291 </a>
292 <a href='#' class='css_button' onclick='mysubmit("export")' style='margin-left:1em'>
293 <span><?php echo htmlspecialchars(xl('CSV Export'), ENT_NOQUOTES); ?></span>
294 </a>
295 <?php } ?>
296 </td>
297 </tr>
298 </table>
299 </td>
300 </tr>
301 </table>
302 </div>
304 <?php if ($form_action) { // if submit (already not export here) ?>
306 <div id="report_results">
307 <table border='0' cellpadding='1' cellspacing='2' width='98%'>
308 <tr bgcolor="#dddddd">
309 <td class="dehead">
310 <?php echo htmlspecialchars(xl('Date'), ENT_NOQUOTES); ?>
311 </td>
312 <td class="dehead">
313 <?php echo htmlspecialchars(xl('Transaction'), ENT_NOQUOTES); ?>
314 </td>
315 <td class="dehead">
316 <?php echo htmlspecialchars(xl('Product'), ENT_NOQUOTES); ?>
317 </td>
318 <td class="dehead">
319 <?php echo htmlspecialchars(xl('Lot'), ENT_NOQUOTES); ?>
320 </td>
321 <td class="dehead">
322 <?php echo htmlspecialchars(xl('Warehouse'), ENT_NOQUOTES); ?>
323 </td>
324 <td class="dehead">
325 <?php echo htmlspecialchars(xl('Who'), ENT_NOQUOTES); ?>
326 </td>
327 <td class="dehead" align="right">
328 <?php echo htmlspecialchars(xl('Qty'), ENT_NOQUOTES); ?>
329 </td>
330 <td class="dehead" align="right">
331 <?php echo htmlspecialchars(xl('Amount'), ENT_NOQUOTES); ?>
332 </td>
333 <td class="dehead" align="Center">
334 <?php echo htmlspecialchars(xl('Billed'), ENT_NOQUOTES); ?>
335 </td>
336 <td class="dehead">
337 <?php echo htmlspecialchars(xl('Notes'), ENT_NOQUOTES); ?>
338 </td>
339 </tr>
340 <?php
341 } // end if submit
342 } // end not export
344 if ($form_action) { // if submit or export
345 $from_date = $form_from_date;
346 $to_date = $form_to_date;
348 $grandtotal = 0;
349 $grandqty = 0;
351 $query = "SELECT s.sale_date, s.fee, s.quantity, s.pid, s.encounter, " .
352 "s.billed, s.notes, s.distributor_id, s.xfer_inventory_id, " .
353 "p.fname AS pfname, p.mname AS pmname, p.lname AS plname, " .
354 "u.fname AS dfname, u.mname AS dmname, u.lname AS dlname, u.organization, " .
355 "d.name, fe.date, fe.invoice_refno, " .
356 "i1.lot_number, i2.lot_number AS lot_number_2, " .
357 "lo1.title AS warehouse, lo2.title AS warehouse_2 " .
358 "FROM drug_sales AS s " .
359 "JOIN drugs AS d ON d.drug_id = s.drug_id " .
360 "LEFT JOIN drug_inventory AS i1 ON i1.inventory_id = s.inventory_id " .
361 "LEFT JOIN drug_inventory AS i2 ON i2.inventory_id = s.xfer_inventory_id " .
362 "LEFT JOIN patient_data AS p ON p.pid = s.pid " .
363 "LEFT JOIN users AS u ON u.id = s.distributor_id " .
364 "LEFT JOIN list_options AS lo1 ON lo1.list_id = 'warehouse' AND " .
365 "lo1.option_id = i1.warehouse_id " .
366 "LEFT JOIN list_options AS lo2 ON lo2.list_id = 'warehouse' AND " .
367 "lo2.option_id = i2.warehouse_id " .
368 "LEFT JOIN form_encounter AS fe ON fe.pid = s.pid AND fe.encounter = s.encounter " .
369 "WHERE s.sale_date >= ? AND s.sale_date <= ? ";
370 if ($form_trans_type == 2) { // purchase/return
371 $query .= "AND s.pid = 0 AND s.distributor_id = 0 AND s.xfer_inventory_id = 0 AND s.fee != 0 ";
373 else if ($form_trans_type == 4) { // transfer
374 $query .= "AND s.xfer_inventory_id != 0 ";
376 else if ($form_trans_type == 5) { // adjustment
377 $query .= "AND s.pid = 0 AND s.distributor_id = 0 AND s.xfer_inventory_id = 0 AND s.fee = 0 ";
379 else if ($form_trans_type == 6) { // distribution
380 $query .= "AND s.distributor_id != 0 ";
382 else if ($form_trans_type == 1) { // sale
383 $query .= "AND s.pid != 0 ";
385 $query .= "ORDER BY s.sale_date, s.sale_id";
387 $res = sqlStatement($query, array($from_date, $to_date));
388 while ($row = sqlFetchArray($res)) {
389 thisLineItem($row);
392 // Grand totals line.
393 if ($form_action != 'export') { // if submit
396 <tr bgcolor="#dddddd">
397 <td class="dehead" colspan="6">
398 <?php echo htmlspecialchars(xl('Grand Total'), ENT_NOQUOTES); ?>
399 </td>
400 <td class="dehead" align="right">
401 <?php echo htmlspecialchars($grandqty, ENT_NOQUOTES); ?>
402 </td>
403 <td class="dehead" align="right">
404 <?php echo htmlspecialchars(bucks($grandtotal), ENT_NOQUOTES); ?>
405 </td>
406 <td class="dehead" colspan="2">
408 </td>
409 </tr>
411 <?php
412 } // End if submit
413 } // end if submit or export
415 if ($form_action != 'export') {
416 if ($form_action) {
418 </table>
419 </div>
420 <?php
421 } // end if ($form_action)
424 </form>
425 </center>
426 </body>
428 <!-- stuff for the popup calendar -->
429 <script language="Javascript">
430 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
431 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
432 </script>
434 </html>
435 <?php
436 } // End not export