2 // Copyright (C) 2006, 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 require_once("../globals.php");
10 require_once("$srcdir/acl.inc");
11 require_once("drugs.inc.php");
12 require_once("$srcdir/formdata.inc.php");
13 require_once("$srcdir/options.inc.php");
15 function QuotedOrNull($fld) {
16 if ($fld) return "'$fld'";
20 function checkWarehouseUsed($warehouse_id) {
22 $row = sqlQuery("SELECT count(*) AS count FROM drug_inventory WHERE " .
23 "drug_id = '$drug_id' AND " .
24 "destroy_date IS NULL AND warehouse_id = '$warehouse_id'");
28 // Generate a <select> list of warehouses.
29 // If multiple lots are not allowed for this product, then restrict the
30 // list to warehouses that are unused for the product.
31 // Returns the number of warehouses allowed.
32 // For these purposes the "unassigned" option is considered a warehouse.
34 function genWarehouseList($tag_name, $currvalue, $title, $class='') {
37 $drow = sqlQuery("SELECT allow_multiple FROM drugs WHERE drug_id = '$drug_id'");
38 $allow_multiple = $drow['allow_multiple'];
40 $lres = sqlStatement("SELECT * FROM list_options " .
41 "WHERE list_id = 'warehouse' ORDER BY seq, title");
43 echo "<select name='$tag_name' id='$tag_name'";
44 if ($class) echo " class='$class'";
45 echo " title='$title'>";
47 $got_selected = FALSE;
50 if ($allow_multiple /* || !checkWarehouseUsed('') */) {
51 echo "<option value=''>" . xl('Unassigned') . "</option>";
55 while ($lrow = sqlFetchArray($lres)) {
56 $whid = $lrow['option_id'];
57 if ($whid != $currvalue && !$allow_multiple && checkWarehouseUsed($whid)) continue;
59 echo "<option value='$whid'";
60 if ((strlen($currvalue) == 0 && $lrow['is_default']) ||
61 (strlen($currvalue) > 0 && $whid == $currvalue))
66 echo ">" . $lrow['title'] . "</option>\n";
71 if (!$got_selected && strlen($currvalue) > 0) {
72 $currescaped = htmlspecialchars($currvalue, ENT_QUOTES
);
73 echo "<option value='$currescaped' selected>* $currescaped *</option>";
75 echo " <font color='red' title='" .
76 xl('Please choose a valid selection from the list.') . "'>" .
77 xl('Fix this') . "!</font>";
86 $drug_id = $_REQUEST['drug'] +
0;
87 $lot_id = $_REQUEST['lot'] +
0;
90 $form_trans_type = isset($_POST['form_trans_type']) ?
formData('form_trans_type') : '0';
92 if (!acl_check('admin', 'drugs')) die(xl('Not authorized'));
93 if (!$drug_id) die(xl('Drug ID missing!'));
97 <?php
html_header_show();?
>
98 <title
><?php
echo $lot_id ?
xl("Edit") : xl("Add New"); xl('Lot','e',' '); ?
></title
>
99 <link rel
="stylesheet" href
='<?php echo $css_header ?>' type
='text/css'>
102 td
{ font
-size
:10pt
; }
105 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
106 <script type
="text/javascript" src
="../../library/textformat.js"></script
>
107 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
108 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
109 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
111 <script language
="JavaScript">
113 var mypcc
= '<?php echo $GLOBALS['phone_country_code
'] ?>';
115 function validate() {
116 var f
= document
.forms
[0];
117 if (f
.form_source_lot
.value
== '0' && f
.form_lot_number
.value
.search(/\S
/) < 0) {
118 alert('<?php xl('A lot number is required
','e
'); ?>');
121 if (f
.form_trans_type
.value
== '6' && f
.form_distributor_id
.value
== '') {
122 alert('<?php xl('A distributor is required
','e
'); ?>');
128 function trans_type_changed() {
129 var f
= document
.forms
[0];
130 var sel
= f
.form_trans_type
;
131 var type
= sel
.options
[sel
.selectedIndex
].value
;
132 var showQuantity
= true;
133 var showSaleDate
= true;
135 var showSourceLot
= true;
136 var showNotes
= true;
137 var showDistributor
= false;
138 if (type
== '2') { // purchase
139 showSourceLot
= false;
141 else if (type
== '3') { // return
142 showSourceLot
= false;
144 else if (type
== '6') { // distribution
145 showSourceLot
= false;
146 showDistributor
= true;
148 else if (type
== '4') { // transfer
151 else if (type
== '5') { // adjustment
153 showSourceLot
= false;
156 showQuantity
= false;
157 showSaleDate
= false;
159 showSourceLot
= false;
162 document
.getElementById('row_quantity' ).style
.display
= showQuantity ?
'' : 'none';
163 document
.getElementById('row_sale_date' ).style
.display
= showSaleDate ?
'' : 'none';
164 document
.getElementById('row_cost' ).style
.display
= showCost ?
'' : 'none';
165 document
.getElementById('row_source_lot').style
.display
= showSourceLot ?
'' : 'none';
166 document
.getElementById('row_notes' ).style
.display
= showNotes ?
'' : 'none';
167 document
.getElementById('row_distributor').style
.display
= showDistributor ?
'' : 'none';
174 <body
class="body_top">
177 $row = sqlQuery("SELECT * FROM drug_inventory WHERE drug_id = '$drug_id' " .
178 "AND inventory_id = '$lot_id'");
181 // If we are saving, then save and close the window.
183 if ($_POST['form_save'] ||
$_POST['form_delete']) {
185 $form_quantity = formData('form_quantity') +
0;
186 $form_cost = sprintf('%0.2f', formData('form_cost'));
187 $form_source_lot = formData('form_source_lot') +
0;
188 $form_distributor_id = formData('form_distributor_id') +
0;
190 // Some fixups depending on transaction type.
191 if ($form_trans_type == '3') { // return
192 $form_quantity = 0 - $form_quantity;
193 $form_cost = 0 - $form_cost;
195 else if ($form_trans_type == '5') { // adjustment
198 else if ($form_trans_type == '0') { // no transaction
202 else if ($form_trans_type == '6') { // distribution
203 $form_quantity = 0 - $form_quantity;
204 $form_cost = 0 - $form_cost;
206 if ($form_trans_type != '4') { // not transfer
207 $form_source_lot = 0;
209 if ($form_trans_type != '6') { // not distribution
210 $form_distributor_id = '0';
213 // If a transfer, make sure there is sufficient quantity in the source lot.
214 if ($form_save && $form_source_lot && $form_quantity) {
215 $srow = sqlQuery("SELECT on_hand FROM drug_inventory WHERE " .
216 "drug_id = '$drug_id' AND inventory_id = '$form_source_lot'");
217 if ($srow['on_hand'] < $form_quantity) {
218 $info_msg = xl('Transfer failed, insufficient quantity in source lot');
223 // Destination lot already exists.
225 if ($_POST['form_save']) {
226 // Make sure the destination quantity will not end up negative.
227 if (($row['on_hand'] +
$form_quantity) < 0) {
228 $info_msg = xl('Transaction failed, insufficient quantity in destination lot');
231 sqlStatement("UPDATE drug_inventory SET " .
232 "lot_number = '" . formData('form_lot_number') . "', " .
233 "manufacturer = '" . formData('form_manufacturer') . "', " .
234 "expiration = " . QuotedOrNull($form_expiration) . ", " .
235 "vendor_id = '" . formData('form_vendor_id') . "', " .
236 "warehouse_id = '" . formData('form_warehouse_id') . "', " .
237 "on_hand = on_hand + '" . $form_quantity . "' " .
238 "WHERE drug_id = '$drug_id' AND inventory_id = '$lot_id'");
242 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = '$drug_id' " .
243 "AND inventory_id = '$lot_id'");
246 // Destination lot will be created.
248 if ($form_quantity < 0) {
249 $info_msg = xl('Transaction failed, quantity is less than zero');
252 $lot_id = sqlInsert("INSERT INTO drug_inventory ( " .
253 "drug_id, lot_number, manufacturer, expiration, " .
254 "vendor_id, warehouse_id, on_hand " .
257 "'" . formData('form_lot_number') . "', " .
258 "'" . formData('form_manufacturer') . "', " .
259 QuotedOrNull($form_expiration) . ", " .
260 "'" . formData('form_vendor_id') . "', " .
261 "'" . formData('form_warehouse_id') . "', " .
262 "'" . $form_quantity . "' " .
267 // Create the corresponding drug_sales transaction.
268 if ($_POST['form_save'] && $form_quantity) {
269 $form_notes = formData('form_notes');
270 $form_sale_date = formData('form_sale_date');
271 if (empty($form_sale_date)) $form_sale_date = date('Y-m-d');
272 sqlInsert("INSERT INTO drug_sales ( " .
273 "drug_id, inventory_id, prescription_id, pid, encounter, user, " .
274 "sale_date, quantity, fee, xfer_inventory_id, distributor_id, notes " .
276 "'$drug_id', '$lot_id', '0', '0', '0', " .
277 "'" . $_SESSION['authUser'] . "', " .
278 "'$form_sale_date', " .
279 "'" . (0 - $form_quantity) . "', " .
280 "'" . (0 - $form_cost) . "', " .
281 "'$form_source_lot', " .
282 "'$form_distributor_id', " .
285 // If this is a transfer then reduce source QOH, and also copy some
286 // fields from the source when they are missing.
287 if ($form_source_lot) {
288 sqlStatement("UPDATE drug_inventory SET " .
289 "on_hand = on_hand - '$form_quantity' " .
290 "WHERE inventory_id = '$form_source_lot'");
292 foreach (array('lot_number', 'manufacturer', 'expiration', 'vendor_id') as $item) {
293 sqlStatement("UPDATE drug_inventory AS di1, drug_inventory AS di2 " .
294 "SET di1.$item = di2.$item " .
295 "WHERE di1.inventory_id = '$lot_id' AND " .
296 "di2.inventory_id = '$form_source_lot' AND " .
297 "( di1.$item IS NULL OR di1.$item = '' OR di1.$item = '0' )");
301 } // end if not $info_msg
303 // Close this window and redisplay the updated list of drugs.
305 echo "<script language='JavaScript'>\n";
306 if ($info_msg) echo " alert('$info_msg');\n";
307 echo " window.close();\n";
308 echo " if (opener.refreshme) opener.refreshme();\n";
309 echo "</script></body></html>\n";
314 <form method
='post' name
='theform' action
='add_edit_lot.php?drug=<?php echo $drug_id ?>&lot=<?php echo $lot_id ?>'
315 onsubmit
='return validate()'>
318 <table border
='0' width
='100%'>
321 <td valign
='top' width
='1%' nowrap
><b
><?php
xl('Lot Number','e'); ?
>:</b
></td
>
323 <input type
='text' size
='40' name
='form_lot_number' maxlength
='40' value
='<?php echo $row['lot_number
'] ?>' style
='width:100%' />
328 <td valign
='top' nowrap
><b
><?php
xl('Manufacturer','e'); ?
>:</b
></td
>
330 <input type
='text' size
='40' name
='form_manufacturer' maxlength
='250' value
='<?php echo $row['manufacturer
'] ?>' style
='width:100%' />
335 <td valign
='top' nowrap
><b
><?php
xl('Expiration','e'); ?
>:</b
></td
>
337 <input type
='text' size
='10' name
='form_expiration' id
='form_expiration'
338 value
='<?php echo $row['expiration
'] ?>'
339 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'
340 title
=<?php
xl('yyyy-mm-dd date of expiration','e','\'','\''); ?
> />
341 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
342 id
='img_expiration' border
='0' alt
='[?]' style
='cursor:pointer'
343 title
=<?php
xl('Click here to choose a date','e','\'','\''); ?
>>
348 <td valign
='top' nowrap
><b
><?php
xl('Vendor','e'); ?
>:</b
></td
>
351 // Address book entries for vendors.
352 generate_form_field(array('data_type' => 14, 'field_id' => 'vendor_id',
353 'list_id' => '', 'edit_options' => 'V',
354 'description' => xl('Address book entry for the vendor')),
361 <td valign
='top' nowrap
><b
><?php
xl('Warehouse','e'); ?
>:</b
></td
>
364 // generate_select_list("form_warehouse_id", 'warehouse',
365 // $row['warehouse_id'], xl('Location of this lot'), xl('Unassigned'));
366 if (!genWarehouseList("form_warehouse_id", $row['warehouse_id'],
367 xl('Location of this lot')))
369 $info_msg = xl('This product allows only one lot per warehouse.');
376 <td valign
='top' nowrap
><b
><?php
xl('On Hand','e'); ?
>:</b
></td
>
378 <?php
echo $row['on_hand'] +
0; ?
>
383 <td valign
='top' nowrap
><b
><?php
xl('Transaction','e'); ?
>:</b
></td
>
385 <select name
='form_trans_type' onchange
='trans_type_changed()'>
389 '2' => xl('Purchase'),
391 '6' => xl('Distribution'),
392 '4' => xl('Transfer'),
393 '5' => xl('Adjustment'),
396 echo "<option value='$key'";
397 if ($key == $form_trans_type) echo " selected";
398 echo ">$value</option>\n";
405 <tr id
='row_distributor'>
406 <td valign
='top' nowrap
><b
><?php
xl('Distributor','e'); ?
>:</b
></td
>
409 // Address book entries for distributors.
410 generate_form_field(array('data_type' => 14, 'field_id' => 'distributor_id',
411 'list_id' => '', 'edit_options' => 'R',
412 'description' => xl('Address book entry for the distributor')), '');
417 <tr id
='row_sale_date'>
418 <td valign
='top' nowrap
><b
><?php
xl('Date','e'); ?
>:</b
></td
>
420 <input type
='text' size
='10' name
='form_sale_date' id
='form_sale_date'
421 value
='<?php echo date('Y
-m
-d
') ?>'
422 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'
423 title
=<?php
xl('yyyy-mm-dd date of purchase or transfer','e','\'','\''); ?
> />
424 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
425 id
='img_sale_date' border
='0' alt
='[?]' style
='cursor:pointer'
426 title
=<?php
xl('Click here to choose a date','e','\'','\''); ?
>>
430 <tr id
='row_quantity'>
431 <td valign
='top' nowrap
><b
><?php
xl('Quantity','e'); ?
>:</b
></td
>
433 <input type
='text' size
='5' name
='form_quantity' maxlength
='7' />
438 <td valign
='top' nowrap
><b
><?php
xl('Total Cost','e'); ?
>:</b
></td
>
440 <input type
='text' size
='7' name
='form_cost' maxlength
='12' />
444 <tr id
='row_source_lot'>
445 <td valign
='top' nowrap
><b
><?php
xl('Source Lot','e'); ?
>:</b
></td
>
447 <select name
='form_source_lot'>
448 <option value
='0'> </option
>
450 $lres = sqlStatement("SELECT " .
451 "di.inventory_id, di.lot_number, di.on_hand, lo.title " .
452 "FROM drug_inventory AS di " .
453 "LEFT JOIN list_options AS lo ON lo.list_id = 'warehouse' AND " .
454 "lo.option_id = di.warehouse_id " .
455 "WHERE di.drug_id = '$drug_id' AND di.inventory_id != '$lot_id' AND " .
456 "di.on_hand > 0 AND di.destroy_date IS NULL " .
457 "ORDER BY di.lot_number, lo.title, di.inventory_id");
458 while ($lrow = sqlFetchArray($lres)) {
459 echo "<option value='" . $lrow['inventory_id'] . "'>";
460 echo $lrow['lot_number'];
461 if (!empty($lrow['title'])) echo " / " . $lrow['title'];
462 echo " (" . $lrow['on_hand'] . ")";
471 <td valign
='top' nowrap
><b
><?php
xl('Comments','e'); ?
>:</b
></td
>
473 <input type
='text' size
='40' name
='form_notes' maxlength
='255' style
='width:100%' />
480 <input type
='submit' name
='form_save' value
='<?php xl('Save
','e
'); ?>' />
482 <?php
if ($lot_id) { ?
>
484 <input type
='button' value
='<?php xl('Destroy
...','e
'); ?>'
485 onclick
="window.location.href='destroy_lot.php?drug=<?php echo $drug_id ?>&lot=<?php echo $lot_id ?>'" />
489 <input type
='button' value
='<?php xl('Cancel
','e
'); ?>' onclick
='window.close()' />
494 <script language
='JavaScript'>
495 Calendar
.setup({inputField
:"form_expiration", ifFormat
:"%Y-%m-%d", button
:"img_expiration"});
496 Calendar
.setup({inputField
:"form_sale_date", ifFormat
:"%Y-%m-%d", button
:"img_sale_date"});
499 echo " alert('$info_msg');\n";
500 echo " window.close();\n";
503 trans_type_changed();