2 // Copyright (C) 2006-2016 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.
12 require_once("../globals.php");
13 require_once("$srcdir/acl.inc");
14 require_once("drugs.inc.php");
15 require_once("$srcdir/options.inc.php");
17 function QuotedOrNull($fld)
20 return "'".add_escape_custom($fld)."'";
26 function checkWarehouseUsed($warehouse_id)
29 $row = sqlQuery("SELECT count(*) AS count FROM drug_inventory WHERE " .
31 "destroy_date IS NULL AND warehouse_id = ?", array($drug_id,$warehouse_id));
35 // Generate a <select> list of warehouses.
36 // If multiple lots are not allowed for this product, then restrict the
37 // list to warehouses that are unused for the product.
38 // Returns the number of warehouses allowed.
39 // For these purposes the "unassigned" option is considered a warehouse.
41 function genWarehouseList($tag_name, $currvalue, $title, $class = '')
45 $drow = sqlQuery("SELECT allow_multiple FROM drugs WHERE drug_id = ?", array($drug_id));
46 $allow_multiple = $drow['allow_multiple'];
48 $lres = sqlStatement("SELECT * FROM list_options " .
49 "WHERE list_id = 'warehouse' AND activity = 1 ORDER BY seq, title");
51 echo "<select name='".attr($tag_name)."' id='".attr($tag_name)."'";
53 echo " class='".attr($class)."'";
56 echo " title='".attr($title)."'>";
58 $got_selected = false;
61 if ($allow_multiple /* || !checkWarehouseUsed('') */) {
62 echo "<option value=''>" . xlt('Unassigned') . "</option>";
66 while ($lrow = sqlFetchArray($lres)) {
67 $whid = $lrow['option_id'];
68 if ($whid != $currvalue && !$allow_multiple && checkWarehouseUsed($whid)) {
72 echo "<option value='".attr($whid)."'";
73 if ((strlen($currvalue) == 0 && $lrow['is_default']) ||
74 (strlen($currvalue) > 0 && $whid == $currvalue)) {
79 echo ">" . text($lrow['title']) . "</option>\n";
84 if (!$got_selected && strlen($currvalue) > 0) {
85 echo "<option value='".attr($currvalue)."' selected>* ".text($currvalue)." *</option>";
87 echo " <font color='red' title='" .
88 xla('Please choose a valid selection from the list.') . "'>" .
89 xlt('Fix this') . "!</font>";
97 $drug_id = $_REQUEST['drug'] +
0;
98 $lot_id = $_REQUEST['lot'] +
0;
101 $form_trans_type = isset($_POST['form_trans_type']) ?
$_POST['form_trans_type'] : '0';
103 if (!acl_check('admin', 'drugs')) {
104 die(xlt('Not authorized'));
108 die(xlt('Drug ID missing!'));
113 <?php
html_header_show();?
>
114 <title
><?php
echo $lot_id ?
xlt("Edit") : xlt("Add New");
115 xlt('Lot', 'e', ' '); ?
></title
>
116 <link rel
="stylesheet" href
='<?php echo $css_header ?>' type
='text/css'>
119 td
{ font
-size
:10pt
; }
122 <script type
="text/javascript" src
="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js"></script
>
123 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
124 <script type
="text/javascript" src
="../../library/textformat.js"></script
>
125 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
126 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
127 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
129 <script language
="JavaScript">
131 var mypcc
= '<?php echo $GLOBALS['phone_country_code
'] ?>';
133 function validate() {
134 var f
= document
.forms
[0];
135 if (f
.form_source_lot
.value
== '0' && f
.form_lot_number
.value
.search(/\S
/) < 0) {
136 alert('<?php echo addslashes(xl('A lot number is required
')); ?>');
139 if (f
.form_trans_type
.value
== '6' && f
.form_distributor_id
.value
== '') {
140 alert('<?php echo addslashes(xl('A distributor is required
')); ?>');
146 function trans_type_changed() {
147 var f
= document
.forms
[0];
148 var sel
= f
.form_trans_type
;
149 var type
= sel
.options
[sel
.selectedIndex
].value
;
150 var showQuantity
= true;
151 var showSaleDate
= true;
153 var showSourceLot
= true;
154 var showNotes
= true;
155 var showDistributor
= false;
156 if (type
== '2') { // purchase
157 showSourceLot
= false;
159 else if (type
== '3') { // return
160 showSourceLot
= false;
162 else if (type
== '6') { // distribution
163 showSourceLot
= false;
164 showDistributor
= true;
166 else if (type
== '4') { // transfer
169 else if (type
== '5') { // adjustment
171 showSourceLot
= false;
174 showQuantity
= false;
175 showSaleDate
= false;
177 showSourceLot
= false;
180 document
.getElementById('row_quantity' ).style
.display
= showQuantity ?
'' : 'none';
181 document
.getElementById('row_sale_date' ).style
.display
= showSaleDate ?
'' : 'none';
182 document
.getElementById('row_cost' ).style
.display
= showCost ?
'' : 'none';
183 document
.getElementById('row_source_lot').style
.display
= showSourceLot ?
'' : 'none';
184 document
.getElementById('row_notes' ).style
.display
= showNotes ?
'' : 'none';
185 document
.getElementById('row_distributor').style
.display
= showDistributor ?
'' : 'none';
192 <body
class="body_top">
195 $row = sqlQuery("SELECT * FROM drug_inventory WHERE drug_id = ? " .
196 "AND inventory_id = ?", array($drug_id,$lot_id));
199 // If we are saving, then save and close the window.
201 if ($_POST['form_save'] ||
$_POST['form_delete']) {
202 $form_quantity = $_POST['form_quantity'] +
0;
203 $form_cost = sprintf('%0.2f', $_POST['form_cost']);
204 $form_source_lot = $_POST['form_source_lot'] +
0;
205 $form_distributor_id = $_POST['form_distributor_id'] +
0;
207 // Some fixups depending on transaction type.
208 if ($form_trans_type == '3') { // return
209 $form_quantity = 0 - $form_quantity;
210 $form_cost = 0 - $form_cost;
211 } else if ($form_trans_type == '5') { // adjustment
213 } else if ($form_trans_type == '0') { // no transaction
216 } else if ($form_trans_type == '6') { // distribution
217 $form_quantity = 0 - $form_quantity;
218 $form_cost = 0 - $form_cost;
221 if ($form_trans_type != '4') { // not transfer
222 $form_source_lot = 0;
225 if ($form_trans_type != '6') { // not distribution
226 $form_distributor_id = '0';
229 // If a transfer, make sure there is sufficient quantity in the source lot.
230 if ($_POST['form_save'] && $form_source_lot && $form_quantity) {
231 $srow = sqlQuery("SELECT on_hand FROM drug_inventory WHERE " .
232 "drug_id = ? AND inventory_id = ?", array($drug_id,$form_source_lot));
233 if ($srow['on_hand'] < $form_quantity) {
234 $info_msg = xl('Transfer failed, insufficient quantity in source lot');
239 // Destination lot already exists.
241 if ($_POST['form_save']) {
242 // Make sure the destination quantity will not end up negative.
243 if (($row['on_hand'] +
$form_quantity) < 0) {
244 $info_msg = xl('Transaction failed, insufficient quantity in destination lot');
246 sqlStatement("UPDATE drug_inventory SET " .
247 "lot_number = '" . add_escape_custom($_POST['form_lot_number']) . "', " .
248 "manufacturer = '" . add_escape_custom($_POST['form_manufacturer']) . "', " .
249 "expiration = " . QuotedOrNull($_POST['form_expiration']) . ", " .
250 "vendor_id = '" . add_escape_custom($_POST['form_vendor_id']) . "', " .
251 "warehouse_id = '" . add_escape_custom($_POST['form_warehouse_id']) . "', " .
252 "on_hand = on_hand + '" . add_escape_custom($form_quantity) . "' " .
253 "WHERE drug_id = ? AND inventory_id = ?", array($drug_id,$lot_id));
256 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = ? " .
257 "AND inventory_id = ?", array($drug_id,$lot_id));
259 } // Destination lot will be created.
261 if ($form_quantity < 0) {
262 $info_msg = xl('Transaction failed, quantity is less than zero');
264 $lot_id = sqlInsert("INSERT INTO drug_inventory ( " .
265 "drug_id, lot_number, manufacturer, expiration, " .
266 "vendor_id, warehouse_id, on_hand " .
268 "'" . add_escape_custom($drug_id) . "', " .
269 "'" . add_escape_custom($_POST['form_lot_number']) . "', " .
270 "'" . add_escape_custom($_POST['form_manufacturer']) . "', " .
271 QuotedOrNull($_POST['form_expiration']) . ", " .
272 "'" . add_escape_custom($_POST['form_vendor_id']) . "', " .
273 "'" . add_escape_custom($_POST['form_warehouse_id']) . "', " .
274 "'" . add_escape_custom($form_quantity) . "' " .
279 // Create the corresponding drug_sales transaction.
280 if ($_POST['form_save'] && $form_quantity) {
281 $form_notes = $_POST['form_notes'];
282 $form_sale_date = $_POST['form_sale_date'];
283 if (empty($form_sale_date)) {
284 $form_sale_date = date('Y-m-d');
287 sqlInsert("INSERT INTO drug_sales ( " .
288 "drug_id, inventory_id, prescription_id, pid, encounter, user, " .
289 "sale_date, quantity, fee, xfer_inventory_id, distributor_id, notes " .
291 "'" . add_escape_custom($drug_id) . "', " .
292 "'" . add_escape_custom($lot_id) . "', '0', '0', '0', " .
293 "'" . add_escape_custom($_SESSION['authUser']) . "', " .
294 "'" . add_escape_custom($form_sale_date) . "', " .
295 "'" . add_escape_custom(0 - $form_quantity) . "', " .
296 "'" . add_escape_custom(0 - $form_cost) . "', " .
297 "'" . add_escape_custom($form_source_lot) . "', " .
298 "'" . add_escape_custom($form_distributor_id) . "', " .
299 "'" . add_escape_custom($form_notes) . "' )");
301 // If this is a transfer then reduce source QOH, and also copy some
302 // fields from the source when they are missing.
303 if ($form_source_lot) {
304 sqlStatement("UPDATE drug_inventory SET " .
305 "on_hand = on_hand - ? " .
306 "WHERE inventory_id = ?", array($form_quantity,$form_source_lot));
308 foreach (array('lot_number', 'manufacturer', 'expiration', 'vendor_id') as $item) {
309 sqlStatement("UPDATE drug_inventory AS di1, drug_inventory AS di2 " .
310 "SET di1.".add_escape_custom($item)." = di2.".add_escape_custom($item)." " .
311 "WHERE di1.inventory_id = ? AND " .
312 "di2.inventory_id = ? AND " .
313 "( di1.".add_escape_custom($item)." IS NULL OR di1.".add_escape_custom($item)." = '' OR di1.".add_escape_custom($item)." = '0' )", array($lot_id,$form_source_lot));
317 } // end if not $info_msg
319 // Close this window and redisplay the updated list of drugs.
321 echo "<script language='JavaScript'>\n";
323 echo " alert('".addslashes($info_msg)."');\n";
326 echo " window.close();\n";
327 echo " if (opener.refreshme) opener.refreshme();\n";
328 echo "</script></body></html>\n";
333 <form method
='post' name
='theform' action
='add_edit_lot.php?drug=<?php echo attr($drug_id) ?>&lot=<?php echo attr($lot_id) ?>'
334 onsubmit
='return validate()'>
337 <table border
='0' width
='100%'>
340 <td valign
='top' width
='1%' nowrap
><b
><?php
echo xlt('Lot Number'); ?
>:</b
></td
>
342 <input type
='text' size
='40' name
='form_lot_number' maxlength
='40' value
='<?php echo attr($row['lot_number
']) ?>' style
='width:100%' />
347 <td valign
='top' nowrap
><b
><?php
echo xlt('Manufacturer'); ?
>:</b
></td
>
349 <input type
='text' size
='40' name
='form_manufacturer' maxlength
='250' value
='<?php echo attr($row['manufacturer
']) ?>' style
='width:100%' />
354 <td valign
='top' nowrap
><b
><?php
echo xlt('Expiration'); ?
>:</b
></td
>
356 <input type
='text' size
='10' name
='form_expiration' id
='form_expiration'
357 value
='<?php echo attr($row['expiration
']) ?>'
358 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'
359 title
='<?php echo xla('yyyy
-mm
-dd date of expiration
'); ?>' />
360 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
361 id
='img_expiration' border
='0' alt
='[?]' style
='cursor:pointer'
362 title
='<?php echo xla('Click here to choose a date
'); ?>'>
367 <td valign
='top' nowrap
><b
><?php
echo xlt('Vendor'); ?
>:</b
></td
>
370 // Address book entries for vendors.
372 array('data_type' => 14, 'field_id' => 'vendor_id',
373 'list_id' => '', 'edit_options' => 'V',
374 'description' => xl('Address book entry for the vendor')),
382 <td valign
='top' nowrap
><b
><?php
echo xlt('Warehouse'); ?
>:</b
></td
>
385 // generate_select_list("form_warehouse_id", 'warehouse',
386 // $row['warehouse_id'], xl('Location of this lot'), xl('Unassigned'));
387 if (!genWarehouseList(
389 $row['warehouse_id'],
390 xl('Location of this lot')
392 $info_msg = xl('This product allows only one lot per warehouse.');
399 <td valign
='top' nowrap
><b
><?php
echo xlt('On Hand'); ?
>:</b
></td
>
401 <?php
echo text($row['on_hand'] +
0); ?
>
406 <td valign
='top' nowrap
><b
><?php
echo xlt('Transaction'); ?
>:</b
></td
>
408 <select name
='form_trans_type' onchange
='trans_type_changed()'>
412 '2' => xl('Purchase'),
414 '6' => xl('Distribution'),
415 '4' => xl('Transfer'),
416 '5' => xl('Adjustment'),
417 ) as $key => $value) {
418 echo "<option value='" . attr($key) . "'";
419 if ($key == $form_trans_type) {
423 echo ">" . text($value) . "</option>\n";
430 <tr id
='row_distributor'>
431 <td valign
='top' nowrap
><b
><?php
echo xlt('Distributor'); ?
>:</b
></td
>
434 // Address book entries for distributors.
435 generate_form_field(array('data_type' => 14, 'field_id' => 'distributor_id',
436 'list_id' => '', 'edit_options' => 'R',
437 'description' => xl('Address book entry for the distributor')), '');
442 <tr id
='row_sale_date'>
443 <td valign
='top' nowrap
><b
><?php
echo xlt('Date'); ?
>:</b
></td
>
445 <input type
='text' size
='10' name
='form_sale_date' id
='form_sale_date'
446 value
='<?php echo attr(date('Y
-m
-d
')) ?>'
447 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'
448 title
='<?php echo xla('yyyy
-mm
-dd date of purchase
or transfer
'); ?>' />
449 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
450 id
='img_sale_date' border
='0' alt
='[?]' style
='cursor:pointer'
451 title
='<?php echo xla('Click here to choose a date
'); ?>'>
455 <tr id
='row_quantity'>
456 <td valign
='top' nowrap
><b
><?php
echo xlt('Quantity'); ?
>:</b
></td
>
458 <input type
='text' size
='5' name
='form_quantity' maxlength
='7' />
463 <td valign
='top' nowrap
><b
><?php
echo xlt('Total Cost'); ?
>:</b
></td
>
465 <input type
='text' size
='7' name
='form_cost' maxlength
='12' />
469 <tr id
='row_source_lot'>
470 <td valign
='top' nowrap
><b
><?php
echo xlt('Source Lot'); ?
>:</b
></td
>
472 <select name
='form_source_lot'>
473 <option value
='0'> </option
>
475 $lres = sqlStatement("SELECT " .
476 "di.inventory_id, di.lot_number, di.on_hand, lo.title " .
477 "FROM drug_inventory AS di " .
478 "LEFT JOIN list_options AS lo ON lo.list_id = 'warehouse' AND " .
479 "lo.option_id = di.warehouse_id AND lo.activity = 1 " .
480 "WHERE di.drug_id = ? AND di.inventory_id != ? AND " .
481 "di.on_hand > 0 AND di.destroy_date IS NULL " .
482 "ORDER BY di.lot_number, lo.title, di.inventory_id", array ($drug_id,$lot_id));
483 while ($lrow = sqlFetchArray($lres)) {
484 echo "<option value='" . attr($lrow['inventory_id']) . "'>";
485 echo text($lrow['lot_number']);
486 if (!empty($lrow['title'])) {
487 echo " / " . text($lrow['title']);
490 echo " (" . text($lrow['on_hand']) . ")";
499 <td valign
='top' nowrap
><b
><?php
echo xlt('Comments'); ?
>:</b
></td
>
501 <input type
='text' size
='40' name
='form_notes' maxlength
='255' style
='width:100%' />
508 <input type
='submit' name
='form_save' value
='<?php echo xla('Save
'); ?>' />
510 <?php
if ($lot_id) { ?
>
512 <input type
='button' value
='<?php echo xla('Destroy
...'); ?>'
513 onclick
="window.location.href='destroy_lot.php?drug=<?php echo attr($drug_id) ?>&lot=<?php echo attr($lot_id) ?>'" />
517 <input type
='button' value
='<?php echo xla('Cancel
'); ?>' onclick
='window.close()' />
522 <script language
='JavaScript'>
523 Calendar
.setup({inputField
:"form_expiration", ifFormat
:"%Y-%m-%d", button
:"img_expiration"});
524 Calendar
.setup({inputField
:"form_sale_date", ifFormat
:"%Y-%m-%d", button
:"img_sale_date"});
527 echo " alert('".addslashes($info_msg)."');\n";
528 echo " window.close();\n";
531 trans_type_changed();