fix in recurrence widget (#426)
[openemr.git] / interface / drugs / add_edit_lot.php
blob74b6124516d9603ef91e7dfb7ec10f0f9d89021e
1 <?php
2 // Copyright (C) 2006-2016 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 $sanitize_all_escapes = true;
10 $fake_register_globals = false;
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) {
18 if ($fld) return "'".add_escape_custom($fld)."'";
19 return "NULL";
22 function checkWarehouseUsed($warehouse_id) {
23 global $drug_id;
24 $row = sqlQuery("SELECT count(*) AS count FROM drug_inventory WHERE " .
25 "drug_id = ? AND " .
26 "destroy_date IS NULL AND warehouse_id = ?", array($drug_id,$warehouse_id) );
27 return $row['count'];
30 // Generate a <select> list of warehouses.
31 // If multiple lots are not allowed for this product, then restrict the
32 // list to warehouses that are unused for the product.
33 // Returns the number of warehouses allowed.
34 // For these purposes the "unassigned" option is considered a warehouse.
36 function genWarehouseList($tag_name, $currvalue, $title, $class='') {
37 global $drug_id;
39 $drow = sqlQuery("SELECT allow_multiple FROM drugs WHERE drug_id = ?", array($drug_id));
40 $allow_multiple = $drow['allow_multiple'];
42 $lres = sqlStatement("SELECT * FROM list_options " .
43 "WHERE list_id = 'warehouse' AND activity = 1 ORDER BY seq, title");
45 echo "<select name='".attr($tag_name)."' id='".attr($tag_name)."'";
46 if ($class) echo " class='".attr($class)."'";
47 echo " title='".attr($title)."'>";
49 $got_selected = FALSE;
50 $count = 0;
52 if ($allow_multiple /* || !checkWarehouseUsed('') */) {
53 echo "<option value=''>" . xlt('Unassigned') . "</option>";
54 ++$count;
57 while ($lrow = sqlFetchArray($lres)) {
58 $whid = $lrow['option_id'];
59 if ($whid != $currvalue && !$allow_multiple && checkWarehouseUsed($whid)) continue;
61 echo "<option value='".attr($whid)."'";
62 if ((strlen($currvalue) == 0 && $lrow['is_default']) ||
63 (strlen($currvalue) > 0 && $whid == $currvalue))
65 echo " selected";
66 $got_selected = TRUE;
68 echo ">" . text($lrow['title']) . "</option>\n";
70 ++$count;
73 if (!$got_selected && strlen($currvalue) > 0) {
74 echo "<option value='".attr($currvalue)."' selected>* ".text($currvalue)." *</option>";
75 echo "</select>";
76 echo " <font color='red' title='" .
77 xla('Please choose a valid selection from the list.') . "'>" .
78 xlt('Fix this') . "!</font>";
80 else {
81 echo "</select>";
84 return $count;
87 $drug_id = $_REQUEST['drug'] + 0;
88 $lot_id = $_REQUEST['lot'] + 0;
89 $info_msg = "";
91 $form_trans_type = isset($_POST['form_trans_type']) ? $_POST['form_trans_type'] : '0';
93 if (!acl_check('admin', 'drugs')) die(xlt('Not authorized'));
94 if (!$drug_id) die(xlt('Drug ID missing!'));
96 <html>
97 <head>
98 <?php html_header_show();?>
99 <title><?php echo $lot_id ? xlt("Edit") : xlt("Add New"); xlt('Lot','e',' '); ?></title>
100 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
102 <style>
103 td { font-size:10pt; }
104 </style>
106 <script type="text/javascript" src="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js"></script>
107 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
108 <script type="text/javascript" src="../../library/textformat.js"></script>
109 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
110 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
111 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
113 <script language="JavaScript">
115 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
117 function validate() {
118 var f = document.forms[0];
119 if (f.form_source_lot.value == '0' && f.form_lot_number.value.search(/\S/) < 0) {
120 alert('<?php echo addslashes(xl('A lot number is required')); ?>');
121 return false;
123 if (f.form_trans_type.value == '6' && f.form_distributor_id.value == '') {
124 alert('<?php echo addslashes(xl('A distributor is required')); ?>');
125 return false;
127 return true;
130 function trans_type_changed() {
131 var f = document.forms[0];
132 var sel = f.form_trans_type;
133 var type = sel.options[sel.selectedIndex].value;
134 var showQuantity = true;
135 var showSaleDate = true;
136 var showCost = true;
137 var showSourceLot = true;
138 var showNotes = true;
139 var showDistributor = false;
140 if (type == '2') { // purchase
141 showSourceLot = false;
143 else if (type == '3') { // return
144 showSourceLot = false;
146 else if (type == '6') { // distribution
147 showSourceLot = false;
148 showDistributor = true;
150 else if (type == '4') { // transfer
151 showCost = false;
153 else if (type == '5') { // adjustment
154 showCost = false;
155 showSourceLot = false;
157 else {
158 showQuantity = false;
159 showSaleDate = false;
160 showCost = false;
161 showSourceLot = false;
162 showNotes = false;
164 document.getElementById('row_quantity' ).style.display = showQuantity ? '' : 'none';
165 document.getElementById('row_sale_date' ).style.display = showSaleDate ? '' : 'none';
166 document.getElementById('row_cost' ).style.display = showCost ? '' : 'none';
167 document.getElementById('row_source_lot').style.display = showSourceLot ? '' : 'none';
168 document.getElementById('row_notes' ).style.display = showNotes ? '' : 'none';
169 document.getElementById('row_distributor').style.display = showDistributor ? '' : 'none';
172 </script>
174 </head>
176 <body class="body_top">
177 <?php
178 if ($lot_id) {
179 $row = sqlQuery("SELECT * FROM drug_inventory WHERE drug_id = ? " .
180 "AND inventory_id = ?", array($drug_id,$lot_id));
183 // If we are saving, then save and close the window.
185 if ($_POST['form_save'] || $_POST['form_delete']) {
187 $form_quantity = $_POST['form_quantity'] + 0;
188 $form_cost = sprintf('%0.2f', $_POST['form_cost']);
189 $form_source_lot = $_POST['form_source_lot'] + 0;
190 $form_distributor_id = $_POST['form_distributor_id'] + 0;
192 // Some fixups depending on transaction type.
193 if ($form_trans_type == '3') { // return
194 $form_quantity = 0 - $form_quantity;
195 $form_cost = 0 - $form_cost;
197 else if ($form_trans_type == '5') { // adjustment
198 $form_cost = 0;
200 else if ($form_trans_type == '0') { // no transaction
201 $form_quantity = 0;
202 $form_cost = 0;
204 else if ($form_trans_type == '6') { // distribution
205 $form_quantity = 0 - $form_quantity;
206 $form_cost = 0 - $form_cost;
208 if ($form_trans_type != '4') { // not transfer
209 $form_source_lot = 0;
211 if ($form_trans_type != '6') { // not distribution
212 $form_distributor_id = '0';
215 // If a transfer, make sure there is sufficient quantity in the source lot.
216 if ($_POST['form_save'] && $form_source_lot && $form_quantity) {
217 $srow = sqlQuery("SELECT on_hand FROM drug_inventory WHERE " .
218 "drug_id = ? AND inventory_id = ?", array($drug_id,$form_source_lot) );
219 if ($srow['on_hand'] < $form_quantity) {
220 $info_msg = xl('Transfer failed, insufficient quantity in source lot');
224 if (!$info_msg) {
225 // Destination lot already exists.
226 if ($lot_id) {
227 if ($_POST['form_save']) {
228 // Make sure the destination quantity will not end up negative.
229 if (($row['on_hand'] + $form_quantity) < 0) {
230 $info_msg = xl('Transaction failed, insufficient quantity in destination lot');
232 else {
233 sqlStatement("UPDATE drug_inventory SET " .
234 "lot_number = '" . add_escape_custom($_POST['form_lot_number']) . "', " .
235 "manufacturer = '" . add_escape_custom($_POST['form_manufacturer']) . "', " .
236 "expiration = " . QuotedOrNull($_POST['form_expiration']) . ", " .
237 "vendor_id = '" . add_escape_custom($_POST['form_vendor_id']) . "', " .
238 "warehouse_id = '" . add_escape_custom($_POST['form_warehouse_id']) . "', " .
239 "on_hand = on_hand + '" . add_escape_custom($form_quantity) . "' " .
240 "WHERE drug_id = ? AND inventory_id = ?", array($drug_id,$lot_id) );
243 else {
244 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = ? " .
245 "AND inventory_id = ?", array($drug_id,$lot_id) );
248 // Destination lot will be created.
249 else {
250 if ($form_quantity < 0) {
251 $info_msg = xl('Transaction failed, quantity is less than zero');
253 else {
254 $lot_id = sqlInsert("INSERT INTO drug_inventory ( " .
255 "drug_id, lot_number, manufacturer, expiration, " .
256 "vendor_id, warehouse_id, on_hand " .
257 ") VALUES ( " .
258 "'" . add_escape_custom($drug_id) . "', " .
259 "'" . add_escape_custom($_POST['form_lot_number']) . "', " .
260 "'" . add_escape_custom($_POST['form_manufacturer']) . "', " .
261 QuotedOrNull($_POST['form_expiration']) . ", " .
262 "'" . add_escape_custom($_POST['form_vendor_id']) . "', " .
263 "'" . add_escape_custom($_POST['form_warehouse_id']) . "', " .
264 "'" . add_escape_custom($form_quantity) . "' " .
265 ")");
269 // Create the corresponding drug_sales transaction.
270 if ($_POST['form_save'] && $form_quantity) {
271 $form_notes = $_POST['form_notes'];
272 $form_sale_date = $_POST['form_sale_date'];
273 if (empty($form_sale_date)) $form_sale_date = date('Y-m-d');
274 sqlInsert("INSERT INTO drug_sales ( " .
275 "drug_id, inventory_id, prescription_id, pid, encounter, user, " .
276 "sale_date, quantity, fee, xfer_inventory_id, distributor_id, notes " .
277 ") VALUES ( " .
278 "'" . add_escape_custom($drug_id) . "', " .
279 "'" . add_escape_custom($lot_id) . "', '0', '0', '0', " .
280 "'" . add_escape_custom($_SESSION['authUser']) . "', " .
281 "'" . add_escape_custom($form_sale_date) . "', " .
282 "'" . add_escape_custom(0 - $form_quantity) . "', " .
283 "'" . add_escape_custom(0 - $form_cost) . "', " .
284 "'" . add_escape_custom($form_source_lot) . "', " .
285 "'" . add_escape_custom($form_distributor_id) . "', " .
286 "'" . add_escape_custom($form_notes) . "' )");
288 // If this is a transfer then reduce source QOH, and also copy some
289 // fields from the source when they are missing.
290 if ($form_source_lot) {
291 sqlStatement("UPDATE drug_inventory SET " .
292 "on_hand = on_hand - ? " .
293 "WHERE inventory_id = ?", array($form_quantity,$form_source_lot) );
295 foreach (array('lot_number', 'manufacturer', 'expiration', 'vendor_id') as $item) {
296 sqlStatement("UPDATE drug_inventory AS di1, drug_inventory AS di2 " .
297 "SET di1.".add_escape_custom($item)." = di2.".add_escape_custom($item)." " .
298 "WHERE di1.inventory_id = ? AND " .
299 "di2.inventory_id = ? AND " .
300 "( 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) );
304 } // end if not $info_msg
306 // Close this window and redisplay the updated list of drugs.
308 echo "<script language='JavaScript'>\n";
309 if ($info_msg) echo " alert('".addslashes($info_msg)."');\n";
310 echo " window.close();\n";
311 echo " if (opener.refreshme) opener.refreshme();\n";
312 echo "</script></body></html>\n";
313 exit();
317 <form method='post' name='theform' action='add_edit_lot.php?drug=<?php echo attr($drug_id) ?>&lot=<?php echo attr($lot_id) ?>'
318 onsubmit='return validate()'>
319 <center>
321 <table border='0' width='100%'>
323 <tr>
324 <td valign='top' width='1%' nowrap><b><?php echo xlt('Lot Number'); ?>:</b></td>
325 <td>
326 <input type='text' size='40' name='form_lot_number' maxlength='40' value='<?php echo attr($row['lot_number']) ?>' style='width:100%' />
327 </td>
328 </tr>
330 <tr>
331 <td valign='top' nowrap><b><?php echo xlt('Manufacturer'); ?>:</b></td>
332 <td>
333 <input type='text' size='40' name='form_manufacturer' maxlength='250' value='<?php echo attr($row['manufacturer']) ?>' style='width:100%' />
334 </td>
335 </tr>
337 <tr>
338 <td valign='top' nowrap><b><?php echo xlt('Expiration'); ?>:</b></td>
339 <td>
340 <input type='text' size='10' name='form_expiration' id='form_expiration'
341 value='<?php echo attr($row['expiration']) ?>'
342 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
343 title='<?php echo xla('yyyy-mm-dd date of expiration'); ?>' />
344 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
345 id='img_expiration' border='0' alt='[?]' style='cursor:pointer'
346 title='<?php echo xla('Click here to choose a date'); ?>'>
347 </td>
348 </tr>
350 <tr>
351 <td valign='top' nowrap><b><?php echo xlt('Vendor'); ?>:</b></td>
352 <td>
353 <?php
354 // Address book entries for vendors.
355 generate_form_field(array('data_type' => 14, 'field_id' => 'vendor_id',
356 'list_id' => '', 'edit_options' => 'V',
357 'description' => xl('Address book entry for the vendor')),
358 $row['vendor_id']);
360 </td>
361 </tr>
363 <tr>
364 <td valign='top' nowrap><b><?php echo xlt('Warehouse'); ?>:</b></td>
365 <td>
366 <?php
367 // generate_select_list("form_warehouse_id", 'warehouse',
368 // $row['warehouse_id'], xl('Location of this lot'), xl('Unassigned'));
369 if (!genWarehouseList("form_warehouse_id", $row['warehouse_id'],
370 xl('Location of this lot')))
372 $info_msg = xl('This product allows only one lot per warehouse.');
375 </td>
376 </tr>
378 <tr>
379 <td valign='top' nowrap><b><?php echo xlt('On Hand'); ?>:</b></td>
380 <td>
381 <?php echo text($row['on_hand'] + 0); ?>
382 </td>
383 </tr>
385 <tr>
386 <td valign='top' nowrap><b><?php echo xlt('Transaction'); ?>:</b></td>
387 <td>
388 <select name='form_trans_type' onchange='trans_type_changed()'>
389 <?php
390 foreach (array(
391 '0' => xl('None'),
392 '2' => xl('Purchase'),
393 '3' => xl('Return'),
394 '6' => xl('Distribution'),
395 '4' => xl('Transfer'),
396 '5' => xl('Adjustment'),
397 ) as $key => $value)
399 echo "<option value='" . attr($key) . "'";
400 if ($key == $form_trans_type) echo " selected";
401 echo ">" . text($value) . "</option>\n";
404 </select>
405 </td>
406 </tr>
408 <tr id='row_distributor'>
409 <td valign='top' nowrap><b><?php echo xlt('Distributor'); ?>:</b></td>
410 <td>
411 <?php
412 // Address book entries for distributors.
413 generate_form_field(array('data_type' => 14, 'field_id' => 'distributor_id',
414 'list_id' => '', 'edit_options' => 'R',
415 'description' => xl('Address book entry for the distributor')), '');
417 </td>
418 </tr>
420 <tr id='row_sale_date'>
421 <td valign='top' nowrap><b><?php echo xlt('Date'); ?>:</b></td>
422 <td>
423 <input type='text' size='10' name='form_sale_date' id='form_sale_date'
424 value='<?php echo attr(date('Y-m-d')) ?>'
425 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
426 title='<?php echo xla('yyyy-mm-dd date of purchase or transfer'); ?>' />
427 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
428 id='img_sale_date' border='0' alt='[?]' style='cursor:pointer'
429 title='<?php echo xla('Click here to choose a date'); ?>'>
430 </td>
431 </tr>
433 <tr id='row_quantity'>
434 <td valign='top' nowrap><b><?php echo xlt('Quantity'); ?>:</b></td>
435 <td>
436 <input type='text' size='5' name='form_quantity' maxlength='7' />
437 </td>
438 </tr>
440 <tr id='row_cost'>
441 <td valign='top' nowrap><b><?php echo xlt('Total Cost'); ?>:</b></td>
442 <td>
443 <input type='text' size='7' name='form_cost' maxlength='12' />
444 </td>
445 </tr>
447 <tr id='row_source_lot'>
448 <td valign='top' nowrap><b><?php echo xlt('Source Lot'); ?>:</b></td>
449 <td>
450 <select name='form_source_lot'>
451 <option value='0'> </option>
452 <?php
453 $lres = sqlStatement("SELECT " .
454 "di.inventory_id, di.lot_number, di.on_hand, lo.title " .
455 "FROM drug_inventory AS di " .
456 "LEFT JOIN list_options AS lo ON lo.list_id = 'warehouse' AND " .
457 "lo.option_id = di.warehouse_id AND lo.activity = 1 " .
458 "WHERE di.drug_id = ? AND di.inventory_id != ? AND " .
459 "di.on_hand > 0 AND di.destroy_date IS NULL " .
460 "ORDER BY di.lot_number, lo.title, di.inventory_id", array ($drug_id,$lot_id));
461 while ($lrow = sqlFetchArray($lres)) {
462 echo "<option value='" . attr($lrow['inventory_id']) . "'>";
463 echo text($lrow['lot_number']);
464 if (!empty($lrow['title'])) echo " / " . text($lrow['title']);
465 echo " (" . text($lrow['on_hand']) . ")";
466 echo "</option>\n";
469 </select>
470 </td>
471 </tr>
473 <tr id='row_notes'>
474 <td valign='top' nowrap><b><?php echo xlt('Comments'); ?>:</b></td>
475 <td>
476 <input type='text' size='40' name='form_notes' maxlength='255' style='width:100%' />
477 </td>
478 </tr>
480 </table>
483 <input type='submit' name='form_save' value='<?php echo xla('Save'); ?>' />
485 <?php if ($lot_id) { ?>
486 &nbsp;
487 <input type='button' value='<?php echo xla('Destroy...'); ?>'
488 onclick="window.location.href='destroy_lot.php?drug=<?php echo attr($drug_id) ?>&lot=<?php echo attr($lot_id) ?>'" />
489 <?php } ?>
491 &nbsp;
492 <input type='button' value='<?php echo xla('Cancel'); ?>' onclick='window.close()' />
493 </p>
495 </center>
496 </form>
497 <script language='JavaScript'>
498 Calendar.setup({inputField:"form_expiration", ifFormat:"%Y-%m-%d", button:"img_expiration"});
499 Calendar.setup({inputField:"form_sale_date", ifFormat:"%Y-%m-%d", button:"img_sale_date"});
500 <?php
501 if ($info_msg) {
502 echo " alert('".addslashes($info_msg)."');\n";
503 echo " window.close();\n";
506 trans_type_changed();
507 </script>
508 </body>
509 </html>