internationalization of the date widget, see feature request tracker item 2967294
[openemr.git] / interface / drugs / add_edit_lot.php
blobd471ead48b98ecd2a31448ad29e37f855d510d05
1 <?php
2 // Copyright (C) 2006, 2010 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 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'";
17 return "NULL";
20 function checkWarehouseUsed($warehouse_id) {
21 global $drug_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'");
25 return $row['count'];
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='') {
35 global $drug_id;
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;
48 $count = 0;
50 if ($allow_multiple /* || !checkWarehouseUsed('') */) {
51 echo "<option value=''>" . xl('Unassigned') . "</option>";
52 ++$count;
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))
63 echo " selected";
64 $got_selected = TRUE;
66 echo ">" . $lrow['title'] . "</option>\n";
68 ++$count;
71 if (!$got_selected && strlen($currvalue) > 0) {
72 $currescaped = htmlspecialchars($currvalue, ENT_QUOTES);
73 echo "<option value='$currescaped' selected>* $currescaped *</option>";
74 echo "</select>";
75 echo " <font color='red' title='" .
76 xl('Please choose a valid selection from the list.') . "'>" .
77 xl('Fix this') . "!</font>";
79 else {
80 echo "</select>";
83 return $count;
86 $drug_id = $_REQUEST['drug'] + 0;
87 $lot_id = $_REQUEST['lot'] + 0;
88 $info_msg = "";
90 if (!acl_check('admin', 'drugs')) die(xl('Not authorized'));
91 if (!$drug_id) die(xl('Drug ID missing!'));
93 <html>
94 <head>
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'>
99 <style>
100 td { font-size:10pt; }
101 </style>
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'); ?>');
117 return false;
119 return true;
122 </script>
124 </head>
126 <body class="body_top">
127 <?php
128 if ($lot_id) {
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');
147 if (!$info_msg) {
148 if ($lot_id) {
149 if ($_POST['form_save']) {
150 if (($row['on_hand'] + $form_quantity) < 0) {
151 $info_msg = xl('Transaction failed, insufficient quantity in destination lot');
153 else {
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'");
164 else {
165 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = '$drug_id' " .
166 "AND inventory_id = '$lot_id'");
169 else {
170 if ($form_quantity < 0) {
171 $info_msg = xl('Transaction failed, quantity is less than zero');
173 else {
174 $lot_id = sqlInsert("INSERT INTO drug_inventory ( " .
175 "drug_id, lot_number, manufacturer, expiration, " .
176 "vendor_id, warehouse_id, on_hand " .
177 ") VALUES ( " .
178 "'$drug_id', " .
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 . "' " .
185 ")");
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 " .
198 ") VALUES ( " .
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";
231 exit();
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()'>
237 <center>
239 <table border='0' width='100%'>
241 <tr>
242 <td valign='top' width='1%' nowrap><b><?php xl('Lot Number','e'); ?>:</b></td>
243 <td>
244 <input type='text' size='40' name='form_lot_number' maxlength='40' value='<?php echo $row['lot_number'] ?>' style='width:100%' />
245 </td>
246 </tr>
248 <tr>
249 <td valign='top' nowrap><b><?php xl('Manufacturer','e'); ?>:</b></td>
250 <td>
251 <input type='text' size='40' name='form_manufacturer' maxlength='250' value='<?php echo $row['manufacturer'] ?>' style='width:100%' />
252 </td>
253 </tr>
255 <tr>
256 <td valign='top' nowrap><b><?php xl('Expiration','e'); ?>:</b></td>
257 <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','\'','\''); ?>>
265 </td>
266 </tr>
268 <tr>
269 <td valign='top' nowrap><b><?php xl('Vendor','e'); ?>:</b></td>
270 <td>
271 <?php
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')),
276 $row['vendor_id']);
278 </td>
279 </tr>
281 <tr>
282 <td valign='top' nowrap><b><?php xl('Warehouse','e'); ?>:</b></td>
283 <td>
284 <?php
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.');
293 </td>
294 </tr>
296 <tr>
297 <td valign='top' nowrap><b><?php xl('On Hand','e'); ?>:</b></td>
298 <td>
299 <?php echo $row['on_hand'] + 0; ?>
300 </td>
301 </tr>
303 <tr>
304 <td valign='top' nowrap>&nbsp;</td>
305 <td>
306 <b><?php xl('Use the fields below for a purchase or transfer.','e'); ?></b>
307 </td>
308 </tr>
310 <tr>
311 <td valign='top' nowrap><b><?php xl('Date','e'); ?>:</b></td>
312 <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','\'','\''); ?>>
320 </td>
321 </tr>
323 <tr>
324 <td valign='top' nowrap><b><?php xl('Quantity','e'); ?>:</b></td>
325 <td>
326 <input type='text' size='5' name='form_quantity' maxlength='7' />
327 </td>
328 </tr>
330 <tr>
331 <td valign='top' nowrap><b><?php xl('Total Cost','e'); ?>:</b></td>
332 <td>
333 <input type='text' size='7' name='form_cost' maxlength='12' />
334 (for purchase only)
335 </td>
336 </tr>
338 <tr>
339 <td valign='top' nowrap><b><?php xl('Source Lot','e'); ?>:</b></td>
340 <td>
341 <select name='form_source_lot'>
342 <option value='0'> </option>
343 <?php
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'] . ")";
357 echo "</option>\n";
360 </select>
361 (for transfer only)
362 </td>
363 </tr>
365 </table>
368 <input type='submit' name='form_save' value='<?php xl('Save','e'); ?>' />
370 <?php if ($lot_id) { ?>
371 &nbsp;
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 ?>'" />
374 <?php } ?>
376 &nbsp;
377 <input type='button' value='<?php xl('Cancel','e'); ?>' onclick='window.close()' />
378 </p>
380 </center>
381 </form>
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"});
385 <?php
386 if ($info_msg) {
387 echo " alert('$info_msg');\n";
388 echo " window.close();\n";
391 </script>
392 </body>
393 </html>