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 $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)."'";
24 function checkWarehouseUsed($warehouse_id) {
26 $row = sqlQuery("SELECT count(*) AS count FROM drug_inventory WHERE " .
28 "destroy_date IS NULL AND warehouse_id = ?", array($drug_id,$warehouse_id) );
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='') {
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;
54 if ($allow_multiple /* || !checkWarehouseUsed('') */) {
55 echo "<option value=''>" . xlt('Unassigned') . "</option>";
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))
70 echo ">" . text($lrow['title']) . "</option>\n";
75 if (!$got_selected && strlen($currvalue) > 0) {
76 echo "<option value='".attr($currvalue)."' selected>* ".text($currvalue)." *</option>";
78 echo " <font color='red' title='" .
79 xla('Please choose a valid selection from the list.') . "'>" .
80 xlt('Fix this') . "!</font>";
89 $drug_id = $_REQUEST['drug'] +
0;
90 $lot_id = $_REQUEST['lot'] +
0;
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!'));
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'>
105 td
{ font
-size
:10pt
; }
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
')); ?>');
124 if (f
.form_trans_type
.value
== '6' && f
.form_distributor_id
.value
== '') {
125 alert('<?php echo addslashes(xl('A distributor is required
')); ?>');
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;
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
154 else if (type
== '5') { // adjustment
156 showSourceLot
= false;
159 showQuantity
= false;
160 showSaleDate
= false;
162 showSourceLot
= 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';
177 <body
class="body_top">
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
201 else if ($form_trans_type == '0') { // no transaction
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');
226 // Destination lot already exists.
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');
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) );
245 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = ? " .
246 "AND inventory_id = ?", array($drug_id,$lot_id) );
249 // Destination lot will be created.
251 if ($form_quantity < 0) {
252 $info_msg = xl('Transaction failed, quantity is less than zero');
255 $lot_id = sqlInsert("INSERT INTO drug_inventory ( " .
256 "drug_id, lot_number, manufacturer, expiration, " .
257 "vendor_id, warehouse_id, on_hand " .
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) . "' " .
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 " .
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";
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()'>
322 <table border
='0' width
='100%'>
325 <td valign
='top' width
='1%' nowrap
><b
><?php
echo xlt('Lot Number'); ?
>:</b
></td
>
327 <input type
='text' size
='40' name
='form_lot_number' maxlength
='40' value
='<?php echo attr($row['lot_number
']) ?>' style
='width:100%' />
332 <td valign
='top' nowrap
><b
><?php
echo xlt('Manufacturer'); ?
>:</b
></td
>
334 <input type
='text' size
='40' name
='form_manufacturer' maxlength
='250' value
='<?php echo attr($row['manufacturer
']) ?>' style
='width:100%' />
339 <td valign
='top' nowrap
><b
><?php
echo xlt('Expiration'); ?
>:</b
></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
'); ?>'>
352 <td valign
='top' nowrap
><b
><?php
echo xlt('Vendor'); ?
>:</b
></td
>
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')),
365 <td valign
='top' nowrap
><b
><?php
echo xlt('Warehouse'); ?
>:</b
></td
>
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.');
380 <td valign
='top' nowrap
><b
><?php
echo xlt('On Hand'); ?
>:</b
></td
>
382 <?php
echo text($row['on_hand'] +
0); ?
>
387 <td valign
='top' nowrap
><b
><?php
echo xlt('Transaction'); ?
>:</b
></td
>
389 <select name
='form_trans_type' onchange
='trans_type_changed()'>
393 '2' => xl('Purchase'),
395 '6' => xl('Distribution'),
396 '4' => xl('Transfer'),
397 '5' => xl('Adjustment'),
400 echo "<option value='" . attr($key) . "'";
401 if ($key == $form_trans_type) echo " selected";
402 echo ">" . text($value) . "</option>\n";
409 <tr id
='row_distributor'>
410 <td valign
='top' nowrap
><b
><?php
echo xlt('Distributor'); ?
>:</b
></td
>
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')), '');
421 <tr id
='row_sale_date'>
422 <td valign
='top' nowrap
><b
><?php
echo xlt('Date'); ?
>:</b
></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
'); ?>'>
434 <tr id
='row_quantity'>
435 <td valign
='top' nowrap
><b
><?php
echo xlt('Quantity'); ?
>:</b
></td
>
437 <input type
='text' size
='5' name
='form_quantity' maxlength
='7' />
442 <td valign
='top' nowrap
><b
><?php
echo xlt('Total Cost'); ?
>:</b
></td
>
444 <input type
='text' size
='7' name
='form_cost' maxlength
='12' />
448 <tr id
='row_source_lot'>
449 <td valign
='top' nowrap
><b
><?php
echo xlt('Source Lot'); ?
>:</b
></td
>
451 <select name
='form_source_lot'>
452 <option value
='0'> </option
>
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']) . ")";
475 <td valign
='top' nowrap
><b
><?php
echo xlt('Comments'); ?
>:</b
></td
>
477 <input type
='text' size
='40' name
='form_notes' maxlength
='255' style
='width:100%' />
484 <input type
='submit' name
='form_save' value
='<?php echo xla('Save
'); ?>' />
486 <?php
if ($lot_id) { ?
>
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) ?>'" />
493 <input type
='button' value
='<?php echo xla('Cancel
'); ?>' onclick
='window.close()' />
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"});
503 echo " alert('".addslashes($info_msg)."');\n";
504 echo " window.close();\n";
507 trans_type_changed();