fix in recurrence widget (#426)
[openemr.git] / interface / drugs / add_edit_drug.php
blob5066793dc86a94046787197716783be3af30a1ad
1 <?php
2 // Copyright (C) 2006-2011 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 $alertmsg = '';
18 $drug_id = $_REQUEST['drug'];
19 $info_msg = "";
20 $tmpl_line_no = 0;
22 if (!acl_check('admin', 'drugs')) die(xlt('Not authorized'));
24 // Format dollars for display.
26 function bucks($amount) {
27 if ($amount) {
28 $amount = sprintf("%.2f", $amount);
29 if ($amount != 0.00) return $amount;
31 return '';
34 // Write a line of data for one template to the form.
36 function writeTemplateLine($selector, $dosage, $period, $quantity, $refills, $prices, $taxrates) {
37 global $tmpl_line_no;
38 ++$tmpl_line_no;
40 echo " <tr>\n";
41 echo " <td class='tmplcell drugsonly'>";
42 echo "<input type='text' name='form_tmpl[$tmpl_line_no][selector]' value='" . attr($selector) . "' size='8' maxlength='100'>";
43 echo "</td>\n";
44 echo " <td class='tmplcell drugsonly'>";
45 echo "<input type='text' name='form_tmpl[$tmpl_line_no][dosage]' value='" . attr($dosage) . "' size='6' maxlength='10'>";
46 echo "</td>\n";
47 echo " <td class='tmplcell drugsonly'>";
48 generate_form_field(array(
49 'data_type' => 1,
50 'field_id' => 'tmpl[' . $tmpl_line_no . '][period]',
51 'list_id' => 'drug_interval',
52 'empty_title' => 'SKIP'
53 ), $period);
54 echo "</td>\n";
55 echo " <td class='tmplcell drugsonly'>";
56 echo "<input type='text' name='form_tmpl[$tmpl_line_no][quantity]' value='" . attr($quantity) . "' size='3' maxlength='7'>";
57 echo "</td>\n";
58 echo " <td class='tmplcell drugsonly'>";
59 echo "<input type='text' name='form_tmpl[$tmpl_line_no][refills]' value='" . attr($refills) . "' size='3' maxlength='5'>";
60 echo "</td>\n";
61 foreach ($prices as $pricelevel => $price) {
62 echo " <td class='tmplcell'>";
63 echo "<input type='text' name='form_tmpl[$tmpl_line_no][price][" . attr($pricelevel) . "]' value='" . attr($price) . "' size='6' maxlength='12'>";
64 echo "</td>\n";
66 $pres = sqlStatement("SELECT option_id FROM list_options " .
67 "WHERE list_id = 'taxrate' AND activity = 1 ORDER BY seq");
68 while ($prow = sqlFetchArray($pres)) {
69 echo " <td class='tmplcell'>";
70 echo "<input type='checkbox' name='form_tmpl[$tmpl_line_no][taxrate][" . attr($prow['option_id']) . "]' value='1'";
71 if (strpos(":$taxrates", $prow['option_id']) !== false) echo " checked";
72 echo " /></td>\n";
74 echo " </tr>\n";
77 // Translation for form fields used in SQL queries.
79 function escapedff($name) {
80 return add_escape_custom(trim($_POST[$name]));
82 function numericff($name) {
83 $field = trim($_POST[$name]) + 0;
84 return add_escape_custom($field);
87 <html>
88 <head>
89 <?php html_header_show(); ?>
90 <title><?php echo $drug_id ? xlt("Edit") : xlt("Add New"); echo ' ' . xlt('Drug'); ?></title>
91 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
93 <style>
94 td { font-size:10pt; }
96 <?php if ($GLOBALS['sell_non_drug_products'] == 2) { ?>
97 .drugsonly { display:none; }
98 <?php } else { ?>
99 .drugsonly { }
100 <?php } ?>
102 <?php if (empty($GLOBALS['ippf_specific'])) { ?>
103 .ippfonly { display:none; }
104 <?php } else { ?>
105 .ippfonly { }
106 <?php } ?>
108 </style>
110 <script type="text/javascript" src="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js"></script>
111 <script type="text/javascript" src="../../library/topdialog.js"></script>
112 <script type="text/javascript" src="../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
113 <script type="text/javascript" src="../../library/textformat.js"></script>
115 <script language="JavaScript">
117 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
119 // This is for callback by the find-code popup.
120 // Appends to or erases the current list of related codes.
121 function set_related(codetype, code, selector, codedesc) {
122 var f = document.forms[0];
123 var s = f.form_related_code.value;
124 if (code) {
125 if (s.length > 0) s += ';';
126 s += codetype + ':' + code;
127 } else {
128 s = '';
130 f.form_related_code.value = s;
133 // This invokes the find-code popup.
134 function sel_related() {
135 dlgopen('../patient_file/encounter/find_code_popup.php', '_blank', 500, 400);
138 </script>
140 </head>
142 <body class="body_top">
143 <?php
144 // If we are saving, then save and close the window.
145 // First check for duplicates.
147 if ($_POST['form_save']) {
148 $crow = sqlQuery("SELECT COUNT(*) AS count FROM drugs WHERE " .
149 "name = '" . escapedff('form_name') . "' AND " .
150 "form = '" . escapedff('form_form') . "' AND " .
151 "size = '" . escapedff('form_size') . "' AND " .
152 "unit = '" . escapedff('form_unit') . "' AND " .
153 "route = '" . escapedff('form_route') . "' AND " .
154 "drug_id != ?", array($drug_id));
155 if ($crow['count']) {
156 $alertmsg = addslashes(xl('Cannot add this entry because it already exists!'));
160 if (($_POST['form_save'] || $_POST['form_delete']) && !$alertmsg) {
161 $new_drug = false;
162 if ($drug_id) {
163 if ($_POST['form_save']) { // updating an existing drug
164 sqlStatement("UPDATE drugs SET " .
165 "name = '" . escapedff('form_name') . "', " .
166 "ndc_number = '" . escapedff('form_ndc_number') . "', " .
167 "drug_code = '" . escapedff('form_drug_code') . "', " .
168 "on_order = '" . escapedff('form_on_order') . "', " .
169 "reorder_point = '" . escapedff('form_reorder_point') . "', " .
170 "max_level = '" . escapedff('form_max_level') . "', " .
171 "form = '" . escapedff('form_form') . "', " .
172 "size = '" . escapedff('form_size') . "', " .
173 "unit = '" . escapedff('form_unit') . "', " .
174 "route = '" . escapedff('form_route') . "', " .
175 "cyp_factor = '" . numericff('form_cyp_factor') . "', " .
176 "related_code = '" . escapedff('form_related_code') . "', " .
177 "allow_multiple = " . (empty($_POST['form_allow_multiple' ]) ? 0 : 1) . ", " .
178 "allow_combining = " . (empty($_POST['form_allow_combining']) ? 0 : 1) . ", " .
179 "active = " . (empty($_POST['form_active']) ? 0 : 1) . " " .
180 "WHERE drug_id = ?", array($drug_id));
181 sqlStatement("DELETE FROM drug_templates WHERE drug_id = ?", array($drug_id));
183 else { // deleting
184 if (acl_check('admin', 'super')) {
185 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = ?", array($drug_id));
186 sqlStatement("DELETE FROM drug_templates WHERE drug_id = ?", array($drug_id));
187 sqlStatement("DELETE FROM drugs WHERE drug_id = ?", array($drug_id));
188 sqlStatement("DELETE FROM prices WHERE pr_id = ? AND pr_selector != ''", array($drug_id));
192 else if ($_POST['form_save']) { // saving a new drug
193 $new_drug = true;
194 $drug_id = sqlInsert("INSERT INTO drugs ( " .
195 "name, ndc_number, drug_code, on_order, reorder_point, max_level, form, " .
196 "size, unit, route, cyp_factor, related_code, " .
197 "allow_multiple, allow_combining, active " .
198 ") VALUES ( " .
199 "'" . escapedff('form_name') . "', " .
200 "'" . escapedff('form_ndc_number') . "', " .
201 "'" . escapedff('form_drug_code') . "', " .
202 "'" . escapedff('form_on_order') . "', " .
203 "'" . escapedff('form_reorder_point') . "', " .
204 "'" . escapedff('form_max_level') . "', " .
205 "'" . escapedff('form_form') . "', " .
206 "'" . escapedff('form_size') . "', " .
207 "'" . escapedff('form_unit') . "', " .
208 "'" . escapedff('form_route') . "', " .
209 "'" . numericff('form_cyp_factor') . "', " .
210 "'" . escapedff('form_related_code') . "', " .
211 (empty($_POST['form_allow_multiple' ]) ? 0 : 1) . ", " .
212 (empty($_POST['form_allow_combining']) ? 0 : 1) . ", " .
213 (empty($_POST['form_active']) ? 0 : 1) .
214 ")");
217 if ($_POST['form_save'] && $drug_id) {
218 $tmpl = $_POST['form_tmpl'];
219 // If using the simplified drug form, then force the one and only
220 // selector name to be the same as the product name.
221 if ($GLOBALS['sell_non_drug_products'] == 2) {
222 $tmpl["1"]['selector'] = $_POST['form_name'];
224 sqlStatement("DELETE FROM prices WHERE pr_id = ? AND pr_selector != ''", array($drug_id));
225 for ($lino = 1; isset($tmpl["$lino"]['selector']); ++$lino) {
226 $iter = $tmpl["$lino"];
227 $selector = trim($iter['selector']);
228 if ($selector) {
229 $taxrates = "";
230 if (!empty($iter['taxrate'])) {
231 foreach ($iter['taxrate'] as $key => $value) {
232 $taxrates .= "$key:";
235 sqlInsert("INSERT INTO drug_templates ( " .
236 "drug_id, selector, dosage, period, quantity, refills, taxrates " .
237 ") VALUES ( ?, ?, ?, ?, ?, ?, ? )",
238 array($drug_id, $selector, trim($iter['dosage']), trim($iter['period']),
239 trim($iter['quantity']), trim($iter['refills']), $taxrates));
241 // Add prices for this drug ID and selector.
242 foreach ($iter['price'] as $key => $value) {
243 $value = $value + 0;
244 if ($value) {
245 sqlStatement("INSERT INTO prices ( " .
246 "pr_id, pr_selector, pr_level, pr_price ) VALUES ( " .
247 "?, ?, ?, ? )",
248 array($drug_id, $selector, $key, $value));
250 } // end foreach price
251 } // end if selector is present
252 } // end for each selector
253 // Save warehouse-specific mins and maxes for this drug.
254 sqlStatement("DELETE FROM product_warehouse WHERE pw_drug_id = ?", array($drug_id));
255 foreach ($_POST['form_wh_min'] as $whid => $whmin) {
256 $whmin = 0 + $whmin;
257 $whmax = 0 + $_POST['form_wh_max'][$whid];
258 if ($whmin != 0 || $whmax != 0) {
259 sqlStatement("INSERT INTO product_warehouse ( " .
260 "pw_drug_id, pw_warehouse, pw_min_level, pw_max_level ) VALUES ( " .
261 "?, ?, ?, ? )", array($drug_id, $whid, $whmin, $whmax));
264 } // end if saving a drug
266 // Close this window and redisplay the updated list of drugs.
268 echo "<script language='JavaScript'>\n";
269 if ($info_msg) echo " alert('$info_msg');\n";
270 echo " if (opener.refreshme) opener.refreshme();\n";
271 if ($new_drug) {
272 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
273 } else {
274 echo " window.close();\n";
276 echo "</script></body></html>\n";
277 exit();
280 if ($drug_id) {
281 $row = sqlQuery("SELECT * FROM drugs WHERE drug_id = ?", array($drug_id));
282 $tres = sqlStatement("SELECT * FROM drug_templates WHERE " .
283 "drug_id = ? ORDER BY selector", array($drug_id));
285 else {
286 $row = array(
287 'name' => '',
288 'active' => '1',
289 'allow_multiple' => '1',
290 'allow_combining' => '',
291 'ndc_number' => '',
292 'on_order' => '0',
293 'reorder_point' => '0',
294 'max_level' => '0',
295 'form' => '',
296 'size' => '',
297 'unit' => '',
298 'route' => '',
299 'cyp_factor' => '',
300 'related_code' => '',
305 <form method='post' name='theform' action='add_edit_drug.php?drug=<?php echo $drug_id; ?>'>
306 <center>
308 <table border='0' width='100%'>
310 <tr>
311 <td valign='top' nowrap><b><?php echo xlt('Name'); ?>:</b></td>
312 <td>
313 <input type='text' size='40' name='form_name' maxlength='80' value='<?php echo attr($row['name']) ?>' style='width:100%' />
314 </td>
315 </tr>
317 <tr>
318 <td valign='top' nowrap><b><?php echo xlt('Active'); ?>:</b></td>
319 <td>
320 <input type='checkbox' name='form_active' value='1'<?php if ($row['active']) echo ' checked'; ?> />
321 </td>
322 </tr>
324 <tr>
325 <td valign='top' nowrap><b><?php echo xlt('Allow'); ?>:</b></td>
326 <td>
327 <input type='checkbox' name='form_allow_multiple' value='1'<?php if ($row['allow_multiple']) echo ' checked'; ?> />
328 <?php echo xlt('Multiple Lots'); ?> &nbsp;
329 <input type='checkbox' name='form_allow_combining' value='1'<?php if ($row['allow_combining']) echo ' checked'; ?> />
330 <?php echo xlt('Combining Lots'); ?>
331 </td>
332 </tr>
334 <tr>
335 <td valign='top' nowrap><b><?php echo xlt('NDC Number'); ?>:</b></td>
336 <td>
337 <input type='text' size='40' name='form_ndc_number' maxlength='20'
338 value='<?php echo attr($row['ndc_number']) ?>' style='width:100%'
339 onkeyup='maskkeyup(this,"<?php echo addslashes($GLOBALS['gbl_mask_product_id']); ?>")'
340 onblur='maskblur(this,"<?php echo addslashes($GLOBALS['gbl_mask_product_id']); ?>")'
342 </td>
343 </tr>
344 <tr>
345 <td valign='top' nowrap><b><?php echo xlt('Drug Code'); ?>:</b></td>
346 <td>
347 <input type='text' size='5' name='form_drug_code' maxlength='10'
348 value='<?php echo attr($row['drug_code']) ?>'
350 </td>
351 </tr>
352 <tr>
353 <td valign='top' nowrap><b><?php echo xlt('On Order'); ?>:</b></td>
354 <td>
355 <input type='text' size='5' name='form_on_order' maxlength='7' value='<?php echo attr($row['on_order']) ?>' />
356 </td>
357 </tr>
359 <tr>
360 <td valign='top' nowrap><b><?php echo xlt('Limits'); ?>:</b></td>
361 <td>
362 <table>
363 <tr>
364 <td valign='top' nowrap>&nbsp;</td>
365 <td valign='top' nowrap><?php echo xlt('Global'); ?></td>
366 <?php
367 // One column header per warehouse title.
368 $pwarr = array();
369 $pwres = sqlStatement("SELECT lo.option_id, lo.title, " .
370 "pw.pw_min_level, pw.pw_max_level " .
371 "FROM list_options AS lo " .
372 "LEFT JOIN product_warehouse AS pw ON " .
373 "pw.pw_drug_id = ? AND " .
374 "pw.pw_warehouse = lo.option_id WHERE " .
375 "lo.list_id = 'warehouse' AND lo.activity = 1 ORDER BY lo.seq, lo.title",
376 array($drug_id));
377 while ($pwrow = sqlFetchArray($pwres)) {
378 $pwarr[] = $pwrow;
379 echo " <td valign='top' nowrap>" .
380 text($pwrow['title']) . "</td>\n";
383 </tr>
384 <tr>
385 <td valign='top' nowrap><?php echo xlt('Min'); ?>&nbsp;</td>
386 <td valign='top'>
387 <input type='text' size='5' name='form_reorder_point' maxlength='7'
388 value='<?php echo attr($row['reorder_point']) ?>'
389 title='<?php echo xla('Reorder point, 0 if not applicable'); ?>'
390 />&nbsp;&nbsp;
391 </td>
392 <?php
393 foreach ($pwarr as $pwrow) {
394 echo " <td valign='top'>";
395 echo "<input type='text' name='form_wh_min[" .
396 attr($pwrow['option_id']) .
397 "]' value='" . attr(0 + $pwrow['pw_min_level']) . "' size='5' " .
398 "title='" . xla('Warehouse minimum, 0 if not applicable') . "' />";
399 echo "&nbsp;&nbsp;</td>\n";
402 </tr>
403 <tr>
404 <td valign='top' nowrap><?php echo xlt('Max'); ?>&nbsp;</td>
405 <td>
406 <input type='text' size='5' name='form_max_level' maxlength='7'
407 value='<?php echo attr($row['max_level']) ?>'
408 title='<?php echo xla('Maximum reasonable inventory, 0 if not applicable'); ?>'
410 </td>
411 <?php
412 foreach ($pwarr as $pwrow) {
413 echo " <td valign='top'>";
414 echo "<input type='text' name='form_wh_max[" .
415 htmlspecialchars($pwrow['option_id']) .
416 "]' value='" . attr(0 + $pwrow['pw_max_level']) . "' size='5' " .
417 "title='" . xla('Warehouse maximum, 0 if not applicable') . "' />";
418 echo "</td>\n";
421 </tr>
422 </table>
423 </td>
424 </tr>
426 <tr class='drugsonly'>
427 <td valign='top' nowrap><b><?php echo xlt('Form'); ?>:</b></td>
428 <td>
429 <?php
430 generate_form_field(array('data_type'=>1,'field_id'=>'form','list_id'=>'drug_form','empty_title'=>'SKIP'), $row['form']);
432 </td>
433 </tr>
435 <tr class='drugsonly'>
436 <td valign='top' nowrap><b><?php echo xlt('Pill Size'); ?>:</b></td>
437 <td>
438 <input type='text' size='5' name='form_size' maxlength='7' value='<?php echo attr($row['size']) ?>' />
439 </td>
440 </tr>
442 <tr class='drugsonly'>
443 <td valign='top' nowrap><b><?php echo xlt('Units'); ?>:</b></td>
444 <td>
445 <?php
446 generate_form_field(array('data_type'=>1,'field_id'=>'unit','list_id'=>'drug_units','empty_title'=>'SKIP'), $row['unit']);
448 </td>
449 </tr>
451 <tr class='drugsonly'>
452 <td valign='top' nowrap><b><?php echo xlt('Route'); ?>:</b></td>
453 <td>
454 <?php
455 generate_form_field(array('data_type'=>1,'field_id'=>'route','list_id'=>'drug_route','empty_title'=>'SKIP'), $row['route']);
457 </td>
458 </tr>
460 <tr class='ippfonly'>
461 <td valign='top' nowrap><b><?php echo xlt('CYP Factor'); ?>:</b></td>
462 <td>
463 <input type='text' size='10' name='form_cyp_factor' maxlength='20' value='<?php echo attr($row['cyp_factor']) ?>' />
464 </td>
465 </tr>
467 <tr>
468 <td valign='top' nowrap><b><?php echo xlt('Relate To'); ?>:</b></td>
469 <td>
470 <input type='text' size='50' name='form_related_code'
471 value='<?php echo attr($row['related_code']) ?>' onclick='sel_related()'
472 title='<?php echo xla('Click to select related code'); ?>'
473 style='width:100%' readonly />
474 </td>
475 </tr>
477 <tr>
478 <td valign='top' nowrap>
479 <b><?php echo $GLOBALS['sell_non_drug_products'] == 2 ? xlt('Fees') : xlt('Templates'); ?>:</b>
480 </td>
481 <td>
482 <table border='0' width='100%'>
483 <tr>
484 <td class='drugsonly'><b><?php echo xlt('Name' ); ?></b></td>
485 <td class='drugsonly'><b><?php echo xlt('Schedule'); ?></b></td>
486 <td class='drugsonly'><b><?php echo xlt('Interval'); ?></b></td>
487 <td class='drugsonly'><b><?php echo xlt('Qty' ); ?></b></td>
488 <td class='drugsonly'><b><?php echo xlt('Refills' ); ?></b></td>
489 <?php
490 // Show a heading for each price level. Also create an array of prices
491 // for new template lines.
492 $emptyPrices = array();
493 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
494 "WHERE list_id = 'pricelevel' AND activity = 1 ORDER BY seq");
495 while ($prow = sqlFetchArray($pres)) {
496 $emptyPrices[$prow['option_id']] = '';
497 echo " <td><b>" .
498 generate_display_field(array('data_type'=>'1','list_id'=>'pricelevel'), $prow['option_id']) .
499 "</b></td>\n";
501 // Show a heading for each tax rate.
502 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
503 "WHERE list_id = 'taxrate' AND activity = 1 ORDER BY seq");
504 while ($prow = sqlFetchArray($pres)) {
505 echo " <td><b>" .
506 generate_display_field(array('data_type'=>'1','list_id'=>'taxrate'), $prow['option_id']) .
507 "</b></td>\n";
510 </tr>
511 <?php
512 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ? 1 : 3;
513 if ($tres) {
514 while ($trow = sqlFetchArray($tres)) {
515 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ? 0 : 1;
516 $selector = $trow['selector'];
517 // Get array of prices.
518 $prices = array();
519 $pres = sqlStatement("SELECT lo.option_id, p.pr_price " .
520 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
521 "p.pr_id = ? AND p.pr_selector = ? AND " .
522 "p.pr_level = lo.option_id " .
523 "WHERE lo.list_id = 'pricelevel' AND lo.activity = 1 ORDER BY lo.seq",
524 array($drug_id, $selector));
525 while ($prow = sqlFetchArray($pres)) {
526 $prices[$prow['option_id']] = $prow['pr_price'];
528 writeTemplateLine($selector, $trow['dosage'], $trow['period'],
529 $trow['quantity'], $trow['refills'], $prices, $trow['taxrates']);
532 for ($i = 0; $i < $blank_lines; ++$i) {
533 $selector = $GLOBALS['sell_non_drug_products'] == 2 ? $row['name'] : '';
534 writeTemplateLine($selector, '', '', '', '', $emptyPrices, '');
537 </table>
538 </td>
539 </tr>
541 </table>
544 <input type='submit' name='form_save' value='<?php echo xla('Save'); ?>' />
546 <?php if (acl_check('admin', 'super')) { ?>
547 &nbsp;
548 <input type='submit' name='form_delete' value='<?php echo xla('Delete'); ?>' style='color:red' />
549 <?php } ?>
551 &nbsp;
552 <input type='button' value='<?php echo xla('Cancel'); ?>' onclick='window.close()' />
554 </p>
556 </center>
557 </form>
559 <script language="JavaScript">
560 <?php
561 if ($alertmsg) {
562 echo "alert('" . htmlentities($alertmsg) . "');\n";
565 </script>
567 </body>
568 </html>