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