Fix for deleting payments via the Front Office Receipts report.
[openemr.git] / interface / drugs / add_edit_lot.php
blob38e8900f015ffd2d22a3abcb0afebb5611ad622b
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 $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/formdata.inc.php");
16 require_once("$srcdir/options.inc.php");
17 require_once("$srcdir/htmlspecialchars.inc.php");
19 function QuotedOrNull($fld) {
20 if ($fld) return "'".add_escape_custom($fld)."'";
21 return "NULL";
24 function checkWarehouseUsed($warehouse_id) {
25 global $drug_id;
26 $row = sqlQuery("SELECT count(*) AS count FROM drug_inventory WHERE " .
27 "drug_id = ? AND " .
28 "destroy_date IS NULL AND warehouse_id = ?", array($drug_id,$warehouse_id) );
29 return $row['count'];
32 // Generate a <select> list of warehouses.
33 // If multiple lots are not allowed for this product, then restrict the
34 // list to warehouses that are unused for the product.
35 // Returns the number of warehouses allowed.
36 // For these purposes the "unassigned" option is considered a warehouse.
38 function genWarehouseList($tag_name, $currvalue, $title, $class='') {
39 global $drug_id;
41 $drow = sqlQuery("SELECT allow_multiple FROM drugs WHERE drug_id = ?", array($drug_id));
42 $allow_multiple = $drow['allow_multiple'];
44 $lres = sqlStatement("SELECT * FROM list_options " .
45 "WHERE list_id = 'warehouse' ORDER BY seq, title");
47 echo "<select name='".attr($tag_name)."' id='".attr($tag_name)."'";
48 if ($class) echo " class='".attr($class)."'";
49 echo " title='".attr($title)."'>";
51 $got_selected = FALSE;
52 $count = 0;
54 if ($allow_multiple /* || !checkWarehouseUsed('') */) {
55 echo "<option value=''>" . xlt('Unassigned') . "</option>";
56 ++$count;
59 while ($lrow = sqlFetchArray($lres)) {
60 $whid = $lrow['option_id'];
61 if ($whid != $currvalue && !$allow_multiple && checkWarehouseUsed($whid)) continue;
63 echo "<option value='".attr($whid)."'";
64 if ((strlen($currvalue) == 0 && $lrow['is_default']) ||
65 (strlen($currvalue) > 0 && $whid == $currvalue))
67 echo " selected";
68 $got_selected = TRUE;
70 echo ">" . text($lrow['title']) . "</option>\n";
72 ++$count;
75 if (!$got_selected && strlen($currvalue) > 0) {
76 echo "<option value='".attr($currvalue)."' selected>* ".text($currvalue)." *</option>";
77 echo "</select>";
78 echo " <font color='red' title='" .
79 xla('Please choose a valid selection from the list.') . "'>" .
80 xlt('Fix this') . "!</font>";
82 else {
83 echo "</select>";
86 return $count;
89 $drug_id = $_REQUEST['drug'] + 0;
90 $lot_id = $_REQUEST['lot'] + 0;
91 $info_msg = "";
93 $form_trans_type = isset($_POST['form_trans_type']) ? $_POST['form_trans_type'] : '0';
95 if (!acl_check('admin', 'drugs')) die(xlt('Not authorized'));
96 if (!$drug_id) die(xlt('Drug ID missing!'));
98 <html>
99 <head>
100 <?php html_header_show();?>
101 <title><?php echo $lot_id ? xlt("Edit") : xlt("Add New"); xlt('Lot','e',' '); ?></title>
102 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
104 <style>
105 td { font-size:10pt; }
106 </style>
108 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
109 <script type="text/javascript" src="../../library/textformat.js"></script>
110 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
111 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
112 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
114 <script language="JavaScript">
116 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
118 function validate() {
119 var f = document.forms[0];
120 if (f.form_source_lot.value == '0' && f.form_lot_number.value.search(/\S/) < 0) {
121 alert('<?php echo addslashes(xl('A lot number is required')); ?>');
122 return false;
124 if (f.form_trans_type.value == '6' && f.form_distributor_id.value == '') {
125 alert('<?php echo addslashes(xl('A distributor is required')); ?>');
126 return false;
128 return true;
131 function trans_type_changed() {
132 var f = document.forms[0];
133 var sel = f.form_trans_type;
134 var type = sel.options[sel.selectedIndex].value;
135 var showQuantity = true;
136 var showSaleDate = true;
137 var showCost = true;
138 var showSourceLot = true;
139 var showNotes = true;
140 var showDistributor = false;
141 if (type == '2') { // purchase
142 showSourceLot = false;
144 else if (type == '3') { // return
145 showSourceLot = false;
147 else if (type == '6') { // distribution
148 showSourceLot = false;
149 showDistributor = true;
151 else if (type == '4') { // transfer
152 showCost = false;
154 else if (type == '5') { // adjustment
155 showCost = false;
156 showSourceLot = false;
158 else {
159 showQuantity = false;
160 showSaleDate = false;
161 showCost = false;
162 showSourceLot = false;
163 showNotes = false;
165 document.getElementById('row_quantity' ).style.display = showQuantity ? '' : 'none';
166 document.getElementById('row_sale_date' ).style.display = showSaleDate ? '' : 'none';
167 document.getElementById('row_cost' ).style.display = showCost ? '' : 'none';
168 document.getElementById('row_source_lot').style.display = showSourceLot ? '' : 'none';
169 document.getElementById('row_notes' ).style.display = showNotes ? '' : 'none';
170 document.getElementById('row_distributor').style.display = showDistributor ? '' : 'none';
173 </script>
175 </head>
177 <body class="body_top">
178 <?php
179 if ($lot_id) {
180 $row = sqlQuery("SELECT * FROM drug_inventory WHERE drug_id = ? " .
181 "AND inventory_id = ?", array($drug_id,$lot_id));
184 // If we are saving, then save and close the window.
186 if ($_POST['form_save'] || $_POST['form_delete']) {
188 $form_quantity = $_POST['form_quantity'] + 0;
189 $form_cost = sprintf('%0.2f', $_POST['form_cost']);
190 $form_source_lot = $_POST['form_source_lot'] + 0;
191 $form_distributor_id = $_POST['form_distributor_id'] + 0;
193 // Some fixups depending on transaction type.
194 if ($form_trans_type == '3') { // return
195 $form_quantity = 0 - $form_quantity;
196 $form_cost = 0 - $form_cost;
198 else if ($form_trans_type == '5') { // adjustment
199 $form_cost = 0;
201 else if ($form_trans_type == '0') { // no transaction
202 $form_quantity = 0;
203 $form_cost = 0;
205 else if ($form_trans_type == '6') { // distribution
206 $form_quantity = 0 - $form_quantity;
207 $form_cost = 0 - $form_cost;
209 if ($form_trans_type != '4') { // not transfer
210 $form_source_lot = 0;
212 if ($form_trans_type != '6') { // not distribution
213 $form_distributor_id = '0';
216 // If a transfer, make sure there is sufficient quantity in the source lot.
217 if ($_POST['form_save'] && $form_source_lot && $form_quantity) {
218 $srow = sqlQuery("SELECT on_hand FROM drug_inventory WHERE " .
219 "drug_id = ? AND inventory_id = ?", array($drug_id,$form_source_lot) );
220 if ($srow['on_hand'] < $form_quantity) {
221 $info_msg = xl('Transfer failed, insufficient quantity in source lot');
225 if (!$info_msg) {
226 // Destination lot already exists.
227 if ($lot_id) {
228 if ($_POST['form_save']) {
229 // Make sure the destination quantity will not end up negative.
230 if (($row['on_hand'] + $form_quantity) < 0) {
231 $info_msg = xl('Transaction failed, insufficient quantity in destination lot');
233 else {
234 sqlStatement("UPDATE drug_inventory SET " .
235 "lot_number = '" . add_escape_custom($_POST['form_lot_number']) . "', " .
236 "manufacturer = '" . add_escape_custom($_POST['form_manufacturer']) . "', " .
237 "expiration = " . QuotedOrNull($_POST['form_expiration']) . ", " .
238 "vendor_id = '" . add_escape_custom($_POST['form_vendor_id']) . "', " .
239 "warehouse_id = '" . add_escape_custom($_POST['form_warehouse_id']) . "', " .
240 "on_hand = on_hand + '" . add_escape_custom($form_quantity) . "' " .
241 "WHERE drug_id = ? AND inventory_id = ?", array($drug_id,$lot_id) );
244 else {
245 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = ? " .
246 "AND inventory_id = ?", array($drug_id,$lot_id) );
249 // Destination lot will be created.
250 else {
251 if ($form_quantity < 0) {
252 $info_msg = xl('Transaction failed, quantity is less than zero');
254 else {
255 $lot_id = sqlInsert("INSERT INTO drug_inventory ( " .
256 "drug_id, lot_number, manufacturer, expiration, " .
257 "vendor_id, warehouse_id, on_hand " .
258 ") VALUES ( " .
259 "'" . add_escape_custom($drug_id) . "', " .
260 "'" . add_escape_custom($_POST['form_lot_number']) . "', " .
261 "'" . add_escape_custom($_POST['form_manufacturer']) . "', " .
262 QuotedOrNull($_POST['form_expiration']) . ", " .
263 "'" . add_escape_custom($_POST['form_vendor_id']) . "', " .
264 "'" . add_escape_custom($_POST['form_warehouse_id']) . "', " .
265 "'" . add_escape_custom($form_quantity) . "' " .
266 ")");
270 // Create the corresponding drug_sales transaction.
271 if ($_POST['form_save'] && $form_quantity) {
272 $form_notes = $_POST['form_notes'];
273 $form_sale_date = $_POST['form_sale_date'];
274 if (empty($form_sale_date)) $form_sale_date = date('Y-m-d');
275 sqlInsert("INSERT INTO drug_sales ( " .
276 "drug_id, inventory_id, prescription_id, pid, encounter, user, " .
277 "sale_date, quantity, fee, xfer_inventory_id, distributor_id, notes " .
278 ") VALUES ( " .
279 "'" . add_escape_custom($drug_id) . "', " .
280 "'" . add_escape_custom($lot_id) . "', '0', '0', '0', " .
281 "'" . add_escape_custom($_SESSION['authUser']) . "', " .
282 "'" . add_escape_custom($form_sale_date) . "', " .
283 "'" . add_escape_custom(0 - $form_quantity) . "', " .
284 "'" . add_escape_custom(0 - $form_cost) . "', " .
285 "'" . add_escape_custom($form_source_lot) . "', " .
286 "'" . add_escape_custom($form_distributor_id) . "', " .
287 "'" . add_escape_custom($form_notes) . "' )");
289 // If this is a transfer then reduce source QOH, and also copy some
290 // fields from the source when they are missing.
291 if ($form_source_lot) {
292 sqlStatement("UPDATE drug_inventory SET " .
293 "on_hand = on_hand - ? " .
294 "WHERE inventory_id = ?", array($form_quantity,$form_source_lot) );
296 foreach (array('lot_number', 'manufacturer', 'expiration', 'vendor_id') as $item) {
297 sqlStatement("UPDATE drug_inventory AS di1, drug_inventory AS di2 " .
298 "SET di1.".add_escape_custom($item)." = di2.".add_escape_custom($item)." " .
299 "WHERE di1.inventory_id = ? AND " .
300 "di2.inventory_id = ? AND " .
301 "( 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) );
305 } // end if not $info_msg
307 // Close this window and redisplay the updated list of drugs.
309 echo "<script language='JavaScript'>\n";
310 if ($info_msg) echo " alert('".addslashes($info_msg)."');\n";
311 echo " window.close();\n";
312 echo " if (opener.refreshme) opener.refreshme();\n";
313 echo "</script></body></html>\n";
314 exit();
318 <form method='post' name='theform' action='add_edit_lot.php?drug=<?php echo attr($drug_id) ?>&lot=<?php echo attr($lot_id) ?>'
319 onsubmit='return validate()'>
320 <center>
322 <table border='0' width='100%'>
324 <tr>
325 <td valign='top' width='1%' nowrap><b><?php echo xlt('Lot Number'); ?>:</b></td>
326 <td>
327 <input type='text' size='40' name='form_lot_number' maxlength='40' value='<?php echo attr($row['lot_number']) ?>' style='width:100%' />
328 </td>
329 </tr>
331 <tr>
332 <td valign='top' nowrap><b><?php echo xlt('Manufacturer'); ?>:</b></td>
333 <td>
334 <input type='text' size='40' name='form_manufacturer' maxlength='250' value='<?php echo attr($row['manufacturer']) ?>' style='width:100%' />
335 </td>
336 </tr>
338 <tr>
339 <td valign='top' nowrap><b><?php echo xlt('Expiration'); ?>:</b></td>
340 <td>
341 <input type='text' size='10' name='form_expiration' id='form_expiration'
342 value='<?php echo attr($row['expiration']) ?>'
343 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
344 title='<?php echo xla('yyyy-mm-dd date of expiration'); ?>' />
345 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
346 id='img_expiration' border='0' alt='[?]' style='cursor:pointer'
347 title='<?php echo xla('Click here to choose a date'); ?>'>
348 </td>
349 </tr>
351 <tr>
352 <td valign='top' nowrap><b><?php echo xlt('Vendor'); ?>:</b></td>
353 <td>
354 <?php
355 // Address book entries for vendors.
356 generate_form_field(array('data_type' => 14, 'field_id' => 'vendor_id',
357 'list_id' => '', 'edit_options' => 'V',
358 'description' => xl('Address book entry for the vendor')),
359 $row['vendor_id']);
361 </td>
362 </tr>
364 <tr>
365 <td valign='top' nowrap><b><?php echo xlt('Warehouse'); ?>:</b></td>
366 <td>
367 <?php
368 // generate_select_list("form_warehouse_id", 'warehouse',
369 // $row['warehouse_id'], xl('Location of this lot'), xl('Unassigned'));
370 if (!genWarehouseList("form_warehouse_id", $row['warehouse_id'],
371 xl('Location of this lot')))
373 $info_msg = xl('This product allows only one lot per warehouse.');
376 </td>
377 </tr>
379 <tr>
380 <td valign='top' nowrap><b><?php echo xlt('On Hand'); ?>:</b></td>
381 <td>
382 <?php echo text($row['on_hand'] + 0); ?>
383 </td>
384 </tr>
386 <tr>
387 <td valign='top' nowrap><b><?php echo xlt('Transaction'); ?>:</b></td>
388 <td>
389 <select name='form_trans_type' onchange='trans_type_changed()'>
390 <?php
391 foreach (array(
392 '0' => xl('None'),
393 '2' => xl('Purchase'),
394 '3' => xl('Return'),
395 '6' => xl('Distribution'),
396 '4' => xl('Transfer'),
397 '5' => xl('Adjustment'),
398 ) as $key => $value)
400 echo "<option value='" . attr($key) . "'";
401 if ($key == $form_trans_type) echo " selected";
402 echo ">" . text($value) . "</option>\n";
405 </select>
406 </td>
407 </tr>
409 <tr id='row_distributor'>
410 <td valign='top' nowrap><b><?php echo xlt('Distributor'); ?>:</b></td>
411 <td>
412 <?php
413 // Address book entries for distributors.
414 generate_form_field(array('data_type' => 14, 'field_id' => 'distributor_id',
415 'list_id' => '', 'edit_options' => 'R',
416 'description' => xl('Address book entry for the distributor')), '');
418 </td>
419 </tr>
421 <tr id='row_sale_date'>
422 <td valign='top' nowrap><b><?php echo xlt('Date'); ?>:</b></td>
423 <td>
424 <input type='text' size='10' name='form_sale_date' id='form_sale_date'
425 value='<?php echo attr(date('Y-m-d')) ?>'
426 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
427 title='<?php echo xla('yyyy-mm-dd date of purchase or transfer'); ?>' />
428 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
429 id='img_sale_date' border='0' alt='[?]' style='cursor:pointer'
430 title='<?php echo xla('Click here to choose a date'); ?>'>
431 </td>
432 </tr>
434 <tr id='row_quantity'>
435 <td valign='top' nowrap><b><?php echo xlt('Quantity'); ?>:</b></td>
436 <td>
437 <input type='text' size='5' name='form_quantity' maxlength='7' />
438 </td>
439 </tr>
441 <tr id='row_cost'>
442 <td valign='top' nowrap><b><?php echo xlt('Total Cost'); ?>:</b></td>
443 <td>
444 <input type='text' size='7' name='form_cost' maxlength='12' />
445 </td>
446 </tr>
448 <tr id='row_source_lot'>
449 <td valign='top' nowrap><b><?php echo xlt('Source Lot'); ?>:</b></td>
450 <td>
451 <select name='form_source_lot'>
452 <option value='0'> </option>
453 <?php
454 $lres = sqlStatement("SELECT " .
455 "di.inventory_id, di.lot_number, di.on_hand, lo.title " .
456 "FROM drug_inventory AS di " .
457 "LEFT JOIN list_options AS lo ON lo.list_id = 'warehouse' AND " .
458 "lo.option_id = di.warehouse_id " .
459 "WHERE di.drug_id = ? AND di.inventory_id != ? AND " .
460 "di.on_hand > 0 AND di.destroy_date IS NULL " .
461 "ORDER BY di.lot_number, lo.title, di.inventory_id", array ($drug_id,$lot_id));
462 while ($lrow = sqlFetchArray($lres)) {
463 echo "<option value='" . attr($lrow['inventory_id']) . "'>";
464 echo text($lrow['lot_number']);
465 if (!empty($lrow['title'])) echo " / " . text($lrow['title']);
466 echo " (" . text($lrow['on_hand']) . ")";
467 echo "</option>\n";
470 </select>
471 </td>
472 </tr>
474 <tr id='row_notes'>
475 <td valign='top' nowrap><b><?php echo xlt('Comments'); ?>:</b></td>
476 <td>
477 <input type='text' size='40' name='form_notes' maxlength='255' style='width:100%' />
478 </td>
479 </tr>
481 </table>
484 <input type='submit' name='form_save' value='<?php echo xla('Save'); ?>' />
486 <?php if ($lot_id) { ?>
487 &nbsp;
488 <input type='button' value='<?php echo xla('Destroy...'); ?>'
489 onclick="window.location.href='destroy_lot.php?drug=<?php echo attr($drug_id) ?>&lot=<?php echo attr($lot_id) ?>'" />
490 <?php } ?>
492 &nbsp;
493 <input type='button' value='<?php echo xla('Cancel'); ?>' onclick='window.close()' />
494 </p>
496 </center>
497 </form>
498 <script language='JavaScript'>
499 Calendar.setup({inputField:"form_expiration", ifFormat:"%Y-%m-%d", button:"img_expiration"});
500 Calendar.setup({inputField:"form_sale_date", ifFormat:"%Y-%m-%d", button:"img_sale_date"});
501 <?php
502 if ($info_msg) {
503 echo " alert('".addslashes($info_msg)."');\n";
504 echo " window.close();\n";
507 trans_type_changed();
508 </script>
509 </body>
510 </html>