Added Polish language
[openemr.git] / interface / drugs / add_edit_lot.php
blob647050ab3d542866c389ec3bd376aaf7b5cf1aca
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 $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!'));
95 <html>
96 <head>
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'>
101 <style>
102 td { font-size:10pt; }
103 </style>
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'); ?>');
119 return false;
121 if (f.form_trans_type.value == '6' && f.form_distributor_id.value == '') {
122 alert('<?php xl('A distributor is required','e'); ?>');
123 return false;
125 return true;
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;
134 var showCost = 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
149 showCost = false;
151 else if (type == '5') { // adjustment
152 showCost = false;
153 showSourceLot = false;
155 else {
156 showQuantity = false;
157 showSaleDate = false;
158 showCost = false;
159 showSourceLot = false;
160 showNotes = 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';
170 </script>
172 </head>
174 <body class="body_top">
175 <?php
176 if ($lot_id) {
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
196 $form_cost = 0;
198 else if ($form_trans_type == '0') { // no transaction
199 $form_quantity = 0;
200 $form_cost = 0;
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');
222 if (!$info_msg) {
223 // Destination lot already exists.
224 if ($lot_id) {
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');
230 else {
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'");
241 else {
242 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = '$drug_id' " .
243 "AND inventory_id = '$lot_id'");
246 // Destination lot will be created.
247 else {
248 if ($form_quantity < 0) {
249 $info_msg = xl('Transaction failed, quantity is less than zero');
251 else {
252 $lot_id = sqlInsert("INSERT INTO drug_inventory ( " .
253 "drug_id, lot_number, manufacturer, expiration, " .
254 "vendor_id, warehouse_id, on_hand " .
255 ") VALUES ( " .
256 "'$drug_id', " .
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 . "' " .
263 ")");
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 " .
275 ") VALUES ( " .
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', " .
283 "'$form_notes' )");
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";
310 exit();
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()'>
316 <center>
318 <table border='0' width='100%'>
320 <tr>
321 <td valign='top' width='1%' nowrap><b><?php xl('Lot Number','e'); ?>:</b></td>
322 <td>
323 <input type='text' size='40' name='form_lot_number' maxlength='40' value='<?php echo $row['lot_number'] ?>' style='width:100%' />
324 </td>
325 </tr>
327 <tr>
328 <td valign='top' nowrap><b><?php xl('Manufacturer','e'); ?>:</b></td>
329 <td>
330 <input type='text' size='40' name='form_manufacturer' maxlength='250' value='<?php echo $row['manufacturer'] ?>' style='width:100%' />
331 </td>
332 </tr>
334 <tr>
335 <td valign='top' nowrap><b><?php xl('Expiration','e'); ?>:</b></td>
336 <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','\'','\''); ?>>
344 </td>
345 </tr>
347 <tr>
348 <td valign='top' nowrap><b><?php xl('Vendor','e'); ?>:</b></td>
349 <td>
350 <?php
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')),
355 $row['vendor_id']);
357 </td>
358 </tr>
360 <tr>
361 <td valign='top' nowrap><b><?php xl('Warehouse','e'); ?>:</b></td>
362 <td>
363 <?php
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.');
372 </td>
373 </tr>
375 <tr>
376 <td valign='top' nowrap><b><?php xl('On Hand','e'); ?>:</b></td>
377 <td>
378 <?php echo $row['on_hand'] + 0; ?>
379 </td>
380 </tr>
382 <tr>
383 <td valign='top' nowrap><b><?php xl('Transaction','e'); ?>:</b></td>
384 <td>
385 <select name='form_trans_type' onchange='trans_type_changed()'>
386 <?php
387 foreach (array(
388 '0' => xl('None'),
389 '2' => xl('Purchase'),
390 '3' => xl('Return'),
391 '6' => xl('Distribution'),
392 '4' => xl('Transfer'),
393 '5' => xl('Adjustment'),
394 ) as $key => $value)
396 echo "<option value='$key'";
397 if ($key == $form_trans_type) echo " selected";
398 echo ">$value</option>\n";
401 </select>
402 </td>
403 </tr>
405 <tr id='row_distributor'>
406 <td valign='top' nowrap><b><?php xl('Distributor','e'); ?>:</b></td>
407 <td>
408 <?php
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')), '');
414 </td>
415 </tr>
417 <tr id='row_sale_date'>
418 <td valign='top' nowrap><b><?php xl('Date','e'); ?>:</b></td>
419 <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','\'','\''); ?>>
427 </td>
428 </tr>
430 <tr id='row_quantity'>
431 <td valign='top' nowrap><b><?php xl('Quantity','e'); ?>:</b></td>
432 <td>
433 <input type='text' size='5' name='form_quantity' maxlength='7' />
434 </td>
435 </tr>
437 <tr id='row_cost'>
438 <td valign='top' nowrap><b><?php xl('Total Cost','e'); ?>:</b></td>
439 <td>
440 <input type='text' size='7' name='form_cost' maxlength='12' />
441 </td>
442 </tr>
444 <tr id='row_source_lot'>
445 <td valign='top' nowrap><b><?php xl('Source Lot','e'); ?>:</b></td>
446 <td>
447 <select name='form_source_lot'>
448 <option value='0'> </option>
449 <?php
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'] . ")";
463 echo "</option>\n";
466 </select>
467 </td>
468 </tr>
470 <tr id='row_notes'>
471 <td valign='top' nowrap><b><?php xl('Comments','e'); ?>:</b></td>
472 <td>
473 <input type='text' size='40' name='form_notes' maxlength='255' style='width:100%' />
474 </td>
475 </tr>
477 </table>
480 <input type='submit' name='form_save' value='<?php xl('Save','e'); ?>' />
482 <?php if ($lot_id) { ?>
483 &nbsp;
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 ?>'" />
486 <?php } ?>
488 &nbsp;
489 <input type='button' value='<?php xl('Cancel','e'); ?>' onclick='window.close()' />
490 </p>
492 </center>
493 </form>
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"});
497 <?php
498 if ($info_msg) {
499 echo " alert('$info_msg');\n";
500 echo " window.close();\n";
503 trans_type_changed();
504 </script>
505 </body>
506 </html>