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 if (!acl_check('admin', 'drugs')) die(xl('Not authorized'));
91 if (!$drug_id) die(xl('Drug ID missing!'));
95 <?php
html_header_show();?
>
96 <title
><?php
echo $lot_id ?
xl("Edit") : xl("Add New"); xl('Lot','e',' '); ?
></title
>
97 <link rel
="stylesheet" href
='<?php echo $css_header ?>' type
='text/css'>
100 td
{ font
-size
:10pt
; }
103 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
104 <script type
="text/javascript" src
="../../library/textformat.js"></script
>
105 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
106 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
107 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
109 <script language
="JavaScript">
111 var mypcc
= '<?php echo $GLOBALS['phone_country_code
'] ?>';
113 function validate() {
114 var f
= document
.forms
[0];
115 if (f
.form_source_lot
.value
== '0' && f
.form_lot_number
.value
.search(/\S
/) < 0) {
116 alert('<?php xl('A lot number is required
!','e
'); ?>');
126 <body
class="body_top">
129 $row = sqlQuery("SELECT * FROM drug_inventory WHERE drug_id = '$drug_id' " .
130 "AND inventory_id = '$lot_id'");
133 // If we are saving, then save and close the window.
135 if ($_POST['form_save'] ||
$_POST['form_delete']) {
137 $form_quantity = formData('form_quantity') +
0;
139 if ($form_save && $form_source_lot && $form_quantity) {
140 $srow = sqlQuery("SELECT on_hand FROM drug_inventory WHERE " .
141 "drug_id = '$drug_id' AND inventory_id = '$form_source_lot'");
142 if ($srow['on_hand'] < $form_quantity) {
143 $info_msg = xl('Transfer failed, insufficient quantity in source lot');
149 if ($_POST['form_save']) {
150 if (($row['on_hand'] +
$form_quantity) < 0) {
151 $info_msg = xl('Transaction failed, insufficient quantity in destination lot');
154 sqlStatement("UPDATE drug_inventory SET " .
155 "lot_number = '" . formData('form_lot_number') . "', " .
156 "manufacturer = '" . formData('form_manufacturer') . "', " .
157 "expiration = " . QuotedOrNull($form_expiration) . ", " .
158 "vendor_id = '" . formData('form_vendor_id') . "', " .
159 "warehouse_id = '" . formData('form_warehouse_id') . "', " .
160 "on_hand = on_hand + '" . $form_quantity . "' " .
161 "WHERE drug_id = '$drug_id' AND inventory_id = '$lot_id'");
165 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = '$drug_id' " .
166 "AND inventory_id = '$lot_id'");
170 if ($form_quantity < 0) {
171 $info_msg = xl('Transaction failed, quantity is less than zero');
174 $lot_id = sqlInsert("INSERT INTO drug_inventory ( " .
175 "drug_id, lot_number, manufacturer, expiration, " .
176 "vendor_id, warehouse_id, on_hand " .
179 "'" . formData('form_lot_number') . "', " .
180 "'" . formData('form_manufacturer') . "', " .
181 QuotedOrNull($form_expiration) . ", " .
182 "'" . formData('form_vendor_id') . "', " .
183 "'" . formData('form_warehouse_id') . "', " .
184 "'" . $form_quantity . "' " .
189 // Create the corresponding drug_sales transaction.
190 if ($_POST['form_save'] && $form_quantity) {
191 $form_source_lot = formData('form_source_lot') +
0;
192 $form_cost = sprintf('%0.2f', formData('form_cost'));
193 $form_sale_date = formData('form_sale_date');
194 if (empty($form_sale_date)) $form_sale_date = date('Y-m-d');
195 sqlInsert("INSERT INTO drug_sales ( " .
196 "drug_id, inventory_id, prescription_id, pid, encounter, user, " .
197 "sale_date, quantity, fee, xfer_inventory_id " .
199 "'$drug_id', '$lot_id', '0', '0', '0', " .
200 "'" . $_SESSION['authUser'] . "', " .
201 "'$form_sale_date', " .
202 "'" . (0 - $form_quantity) . "', " .
203 "'" . (0 - $form_cost) . "', " .
204 "'$form_source_lot' )");
206 // If this is a transfer then reduce source QOH, and also copy some
207 // fields from the source when they are missing.
208 if ($form_source_lot) {
209 sqlStatement("UPDATE drug_inventory SET " .
210 "on_hand = on_hand - '$form_quantity' " .
211 "WHERE inventory_id = '$form_source_lot'");
213 foreach (array('lot_number', 'manufacturer', 'expiration', 'vendor_id') as $item) {
214 sqlStatement("UPDATE drug_inventory AS di1, drug_inventory AS di2 " .
215 "SET di1.$item = di2.$item " .
216 "WHERE di1.inventory_id = '$lot_id' AND " .
217 "di2.inventory_id = '$form_source_lot' AND " .
218 "( di1.$item IS NULL OR di1.$item = '' OR di1.$item = '0' )");
222 } // end if not $info_msg
224 // Close this window and redisplay the updated list of drugs.
226 echo "<script language='JavaScript'>\n";
227 if ($info_msg) echo " alert('$info_msg');\n";
228 echo " window.close();\n";
229 echo " if (opener.refreshme) opener.refreshme();\n";
230 echo "</script></body></html>\n";
235 <form method
='post' name
='theform' action
='add_edit_lot.php?drug=<?php echo $drug_id ?>&lot=<?php echo $lot_id ?>'
236 onsubmit
='return validate()'>
239 <table border
='0' width
='100%'>
242 <td valign
='top' width
='1%' nowrap
><b
><?php
xl('Lot Number','e'); ?
>:</b
></td
>
244 <input type
='text' size
='40' name
='form_lot_number' maxlength
='40' value
='<?php echo $row['lot_number
'] ?>' style
='width:100%' />
249 <td valign
='top' nowrap
><b
><?php
xl('Manufacturer','e'); ?
>:</b
></td
>
251 <input type
='text' size
='40' name
='form_manufacturer' maxlength
='250' value
='<?php echo $row['manufacturer
'] ?>' style
='width:100%' />
256 <td valign
='top' nowrap
><b
><?php
xl('Expiration','e'); ?
>:</b
></td
>
258 <input type
='text' size
='10' name
='form_expiration' id
='form_expiration'
259 value
='<?php echo $row['expiration
'] ?>'
260 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'
261 title
=<?php
xl('yyyy-mm-dd date of expiration','e','\'','\''); ?
> />
262 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
263 id
='img_expiration' border
='0' alt
='[?]' style
='cursor:pointer'
264 title
=<?php
xl('Click here to choose a date','e','\'','\''); ?
>>
269 <td valign
='top' nowrap
><b
><?php
xl('Vendor','e'); ?
>:</b
></td
>
272 // Address book entries for vendors.
273 generate_form_field(array('data_type' => 14, 'field_id' => 'vendor_id',
274 'list_id' => '', 'edit_options' => 'V',
275 'description' => xl('Address book entry for the vendor')),
282 <td valign
='top' nowrap
><b
><?php
xl('Warehouse','e'); ?
>:</b
></td
>
285 // generate_select_list("form_warehouse_id", 'warehouse',
286 // $row['warehouse_id'], xl('Location of this lot'), xl('Unassigned'));
287 if (!genWarehouseList("form_warehouse_id", $row['warehouse_id'],
288 xl('Location of this lot')))
290 $info_msg = xl('This product allows only one lot per warehouse.');
297 <td valign
='top' nowrap
><b
><?php
xl('On Hand','e'); ?
>:</b
></td
>
299 <?php
echo $row['on_hand'] +
0; ?
>
304 <td valign
='top' nowrap
> 
;</td
>
306 <b
><?php
xl('Use the fields below for a purchase or transfer.','e'); ?
></b
>
311 <td valign
='top' nowrap
><b
><?php
xl('Date','e'); ?
>:</b
></td
>
313 <input type
='text' size
='10' name
='form_sale_date' id
='form_sale_date'
314 value
='<?php echo date('Y
-m
-d
') ?>'
315 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'
316 title
=<?php
xl('yyyy-mm-dd date of purchase or transfer','e','\'','\''); ?
> />
317 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
318 id
='img_sale_date' border
='0' alt
='[?]' style
='cursor:pointer'
319 title
=<?php
xl('Click here to choose a date','e','\'','\''); ?
>>
324 <td valign
='top' nowrap
><b
><?php
xl('Quantity','e'); ?
>:</b
></td
>
326 <input type
='text' size
='5' name
='form_quantity' maxlength
='7' />
331 <td valign
='top' nowrap
><b
><?php
xl('Total Cost','e'); ?
>:</b
></td
>
333 <input type
='text' size
='7' name
='form_cost' maxlength
='12' />
339 <td valign
='top' nowrap
><b
><?php
xl('Source Lot','e'); ?
>:</b
></td
>
341 <select name
='form_source_lot'>
342 <option value
='0'> </option
>
344 $lres = sqlStatement("SELECT " .
345 "di.inventory_id, di.lot_number, di.on_hand, lo.title " .
346 "FROM drug_inventory AS di " .
347 "LEFT JOIN list_options AS lo ON lo.list_id = 'warehouse' AND " .
348 "lo.option_id = di.warehouse_id " .
349 "WHERE di.drug_id = '$drug_id' AND di.inventory_id != '$lot_id' AND " .
350 "di.on_hand > 0 AND di.destroy_date IS NULL " .
351 "ORDER BY di.lot_number, lo.title, di.inventory_id");
352 while ($lrow = sqlFetchArray($lres)) {
353 echo "<option value='" . $lrow['inventory_id'] . "'>";
354 echo $lrow['lot_number'];
355 if (!empty($lrow['title'])) echo " / " . $lrow['title'];
356 echo " (" . $lrow['on_hand'] . ")";
368 <input type
='submit' name
='form_save' value
='<?php xl('Save
','e
'); ?>' />
370 <?php
if ($lot_id) { ?
>
372 <input type
='button' value
='<?php xl('Destroy
...','e
'); ?>'
373 onclick
="window.location.href='destroy_lot.php?drug=<?php echo $drug_id ?>&lot=<?php echo $lot_id ?>'" />
377 <input type
='button' value
='<?php xl('Cancel
','e
'); ?>' onclick
='window.close()' />
382 <script language
='JavaScript'>
383 Calendar
.setup({inputField
:"form_expiration", ifFormat
:"%Y-%m-%d", button
:"img_expiration"});
384 Calendar
.setup({inputField
:"form_sale_date", ifFormat
:"%Y-%m-%d", button
:"img_sale_date"});
387 echo " alert('$info_msg');\n";
388 echo " window.close();\n";