Merge branch 'master' of git://github.com/openemr/openemr
[openemr.git] / interface / drugs / add_edit_drug.php
blob4605b8613e8e21fbb105a3b16aba4b865e65b1cc
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/options.inc.php");
14 $alertmsg = '';
15 $drug_id = $_REQUEST['drug'];
16 $info_msg = "";
17 $tmpl_line_no = 0;
19 if (!acl_check('admin', 'drugs')) die(xl('Not authorized'));
21 // Format dollars for display.
23 function bucks($amount) {
24 if ($amount) {
25 $amount = sprintf("%.2f", $amount);
26 if ($amount != 0.00) return $amount;
28 return '';
31 // Write a line of data for one template to the form.
33 function writeTemplateLine($selector, $dosage, $period, $quantity, $refills, $prices, $taxrates) {
34 global $tmpl_line_no;
35 ++$tmpl_line_no;
37 echo " <tr>\n";
38 echo " <td class='tmplcell drugsonly'>";
39 echo "<input type='text' name='form_tmpl[$tmpl_line_no][selector]' value='$selector' size='8' maxlength='100'>";
40 echo "</td>\n";
41 echo " <td class='tmplcell drugsonly'>";
42 echo "<input type='text' name='form_tmpl[$tmpl_line_no][dosage]' value='$dosage' size='6' maxlength='10'>";
43 echo "</td>\n";
44 echo " <td class='tmplcell drugsonly'>";
45 generate_form_field(array('data_type'=>1,'field_id'=>'tmpl['.$tmpl_line_no.'][period]','list_id'=>'drug_interval','empty_title'=>'SKIP'), $period);
46 echo "</td>\n";
47 echo " <td class='tmplcell drugsonly'>";
48 echo "<input type='text' name='form_tmpl[$tmpl_line_no][quantity]' value='$quantity' size='3' maxlength='7'>";
49 echo "</td>\n";
50 echo " <td class='tmplcell drugsonly'>";
51 echo "<input type='text' name='form_tmpl[$tmpl_line_no][refills]' value='$refills' size='3' maxlength='5'>";
52 echo "</td>\n";
53 foreach ($prices as $pricelevel => $price) {
54 echo " <td class='tmplcell'>";
55 echo "<input type='text' name='form_tmpl[$tmpl_line_no][price][$pricelevel]' value='$price' size='6' maxlength='12'>";
56 echo "</td>\n";
58 $pres = sqlStatement("SELECT option_id FROM list_options " .
59 "WHERE list_id = 'taxrate' ORDER BY seq");
60 while ($prow = sqlFetchArray($pres)) {
61 echo " <td class='tmplcell'>";
62 echo "<input type='checkbox' name='form_tmpl[$tmpl_line_no][taxrate][" . $prow['option_id'] . "]' value='1'";
63 if (strpos(":$taxrates", $prow['option_id']) !== false) echo " checked";
64 echo " /></td>\n";
66 echo " </tr>\n";
69 // Translation for form fields.
70 function escapedff($name) {
71 $field = trim($_POST[$name]);
72 if (!get_magic_quotes_gpc()) return addslashes($field);
73 return $field;
75 function numericff($name) {
76 $field = trim($_POST[$name]) + 0;
77 return $field;
80 <html>
81 <head>
82 <?php html_header_show(); ?>
83 <title><?php echo $drug_id ? xl("Edit") : xl("Add New"); xl('Drug','e',' '); ?></title>
84 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
86 <style>
87 td { font-size:10pt; }
89 <?php if ($GLOBALS['sell_non_drug_products'] == 2) { ?>
90 .drugsonly { display:none; }
91 <?php } else { ?>
92 .drugsonly { }
93 <?php } ?>
95 <?php if (empty($GLOBALS['ippf_specific'])) { ?>
96 .ippfonly { display:none; }
97 <?php } else { ?>
98 .ippfonly { }
99 <?php } ?>
101 </style>
103 <script type="text/javascript" src="../../library/topdialog.js"></script>
104 <script type="text/javascript" src="../../library/dialog.js"></script>
105 <script type="text/javascript" src="../../library/textformat.js"></script>
107 <script language="JavaScript">
109 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
111 // This is for callback by the find-code popup.
112 // Appends to or erases the current list of related codes.
113 function set_related(codetype, code, selector, codedesc) {
114 var f = document.forms[0];
115 var s = f.form_related_code.value;
116 if (code) {
117 if (s.length > 0) s += ';';
118 s += codetype + ':' + code;
119 } else {
120 s = '';
122 f.form_related_code.value = s;
125 // This invokes the find-code popup.
126 function sel_related() {
127 dlgopen('../patient_file/encounter/find_code_popup.php', '_blank', 500, 400);
130 </script>
132 </head>
134 <body class="body_top">
135 <?php
136 // If we are saving, then save and close the window.
137 // First check for duplicates.
139 if ($_POST['form_save']) {
140 $crow = sqlQuery("SELECT COUNT(*) AS count FROM drugs WHERE " .
141 "name = '" . escapedff('form_name') . "' AND " .
142 "form = '" . escapedff('form_form') . "' AND " .
143 "size = '" . escapedff('form_size') . "' AND " .
144 "unit = '" . escapedff('form_unit') . "' AND " .
145 "route = '" . escapedff('form_route') . "' AND " .
146 "drug_id != '$drug_id'");
147 if ($crow['count']) {
148 $alertmsg = xl('Cannot add this entry because it already exists!');
152 if (($_POST['form_save'] || $_POST['form_delete']) && !$alertmsg) {
153 $new_drug = false;
154 if ($drug_id) {
155 if ($_POST['form_save']) { // updating an existing drug
156 sqlStatement("UPDATE drugs SET " .
157 "name = '" . escapedff('form_name') . "', " .
158 "ndc_number = '" . escapedff('form_ndc_number') . "', " .
159 "on_order = '" . escapedff('form_on_order') . "', " .
160 "reorder_point = '" . escapedff('form_reorder_point') . "', " .
161 "form = '" . escapedff('form_form') . "', " .
162 "size = '" . escapedff('form_size') . "', " .
163 "unit = '" . escapedff('form_unit') . "', " .
164 "route = '" . escapedff('form_route') . "', " .
165 "cyp_factor = '" . numericff('form_cyp_factor') . "', " .
166 "related_code = '" . escapedff('form_related_code') . "', " .
167 "allow_multiple = " . (empty($_POST['form_allow_multiple' ]) ? 0 : 1) . ", " .
168 "allow_combining = " . (empty($_POST['form_allow_combining']) ? 0 : 1) . ", " .
169 "active = " . (empty($_POST['form_active']) ? 0 : 1) . " " .
170 "WHERE drug_id = '$drug_id'");
171 sqlStatement("DELETE FROM drug_templates WHERE drug_id = '$drug_id'");
173 else { // deleting
174 if (acl_check('admin', 'super')) {
175 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = '$drug_id'");
176 sqlStatement("DELETE FROM drug_templates WHERE drug_id = '$drug_id'");
177 sqlStatement("DELETE FROM drugs WHERE drug_id = '$drug_id'");
178 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
182 else if ($_POST['form_save']) { // saving a new drug
183 $new_drug = true;
184 $drug_id = sqlInsert("INSERT INTO drugs ( " .
185 "name, ndc_number, on_order, reorder_point, form, " .
186 "size, unit, route, cyp_factor, related_code, " .
187 "allow_multiple, allow_combining, active " .
188 ") VALUES ( " .
189 "'" . escapedff('form_name') . "', " .
190 "'" . escapedff('form_ndc_number') . "', " .
191 "'" . escapedff('form_on_order') . "', " .
192 "'" . escapedff('form_reorder_point') . "', " .
193 "'" . escapedff('form_form') . "', " .
194 "'" . escapedff('form_size') . "', " .
195 "'" . escapedff('form_unit') . "', " .
196 "'" . escapedff('form_route') . "', " .
197 "'" . numericff('form_cyp_factor') . "', " .
198 "'" . escapedff('form_related_code') . "', " .
199 (empty($_POST['form_allow_multiple' ]) ? 0 : 1) . ", " .
200 (empty($_POST['form_allow_combining']) ? 0 : 1) . ", " .
201 (empty($_POST['form_active']) ? 0 : 1) .
202 ")");
205 if ($_POST['form_save'] && $drug_id) {
206 $tmpl = $_POST['form_tmpl'];
207 // If using the simplified drug form, then force the one and only
208 // selector name to be the same as the product name.
209 if ($GLOBALS['sell_non_drug_products'] == 2) {
210 $tmpl["1"]['selector'] = escapedff('form_name');
212 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
213 for ($lino = 1; isset($tmpl["$lino"]['selector']); ++$lino) {
214 $iter = $tmpl["$lino"];
215 $selector = trim($iter['selector']);
216 if ($selector) {
217 $taxrates = "";
218 if (!empty($iter['taxrate'])) {
219 foreach ($iter['taxrate'] as $key => $value) {
220 $taxrates .= "$key:";
223 sqlInsert("INSERT INTO drug_templates ( " .
224 "drug_id, selector, dosage, period, quantity, refills, taxrates " .
225 ") VALUES ( " .
226 "$drug_id, " .
227 "'" . $selector . "', " .
228 "'" . trim($iter['dosage']) . "', " .
229 "'" . trim($iter['period']) . "', " .
230 "'" . trim($iter['quantity']) . "', " .
231 "'" . trim($iter['refills']) . "', " .
232 "'" . $taxrates . "' " .
233 ")");
235 // Add prices for this drug ID and selector.
236 foreach ($iter['price'] as $key => $value) {
237 $value = $value + 0;
238 if ($value) {
239 sqlStatement("INSERT INTO prices ( " .
240 "pr_id, pr_selector, pr_level, pr_price ) VALUES ( " .
241 "'$drug_id', '$selector', '$key', '$value' )");
243 } // end foreach price
244 } // end if selector is present
245 } // end for each selector
246 } // end if saving a drug
248 // Close this window and redisplay the updated list of drugs.
250 echo "<script language='JavaScript'>\n";
251 if ($info_msg) echo " alert('$info_msg');\n";
252 echo " if (opener.refreshme) opener.refreshme();\n";
253 if ($new_drug) {
254 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
255 } else {
256 echo " window.close();\n";
258 echo "</script></body></html>\n";
259 exit();
262 if ($drug_id) {
263 $row = sqlQuery("SELECT * FROM drugs WHERE drug_id = '$drug_id'");
264 $tres = sqlStatement("SELECT * FROM drug_templates WHERE " .
265 "drug_id = '$drug_id' ORDER BY selector");
267 else {
268 $row = array(
269 'name' => '',
270 'active' => '1',
271 'allow_multiple' => '1',
272 'allow_combining' => '',
273 'ndc_number' => '',
274 'on_order' => '0',
275 'reorder_point' => '0',
276 'form' => '',
277 'size' => '',
278 'unit' => '',
279 'route' => '',
280 'cyp_factor' => '',
281 'related_code' => '',
286 <form method='post' name='theform' action='add_edit_drug.php?drug=<?php echo $drug_id; ?>'>
287 <center>
289 <table border='0' width='100%'>
291 <tr>
292 <td valign='top' nowrap><b><?php xl('Name','e'); ?>:</b></td>
293 <td>
294 <input type='text' size='40' name='form_name' maxlength='80' value='<?php echo $row['name'] ?>' style='width:100%' />
295 </td>
296 </tr>
298 <tr>
299 <td valign='top' nowrap><b><?php xl('Active','e'); ?>:</b></td>
300 <td>
301 <input type='checkbox' name='form_active' value='1'<?php if ($row['active']) echo ' checked'; ?> />
302 </td>
303 </tr>
305 <tr>
306 <td valign='top' nowrap><b><?php xl('Allow','e'); ?>:</b></td>
307 <td>
308 <input type='checkbox' name='form_allow_multiple' value='1'<?php if ($row['allow_multiple']) echo ' checked'; ?> />
309 <?php xl('Multiple Lots','e'); ?> &nbsp;
310 <input type='checkbox' name='form_allow_combining' value='1'<?php if ($row['allow_combining']) echo ' checked'; ?> />
311 <?php xl('Combining Lots','e'); ?>
312 </td>
313 </tr>
315 <tr>
316 <td valign='top' nowrap><b><?php xl('NDC Number','e'); ?>:</b></td>
317 <td>
318 <input type='text' size='40' name='form_ndc_number' maxlength='20'
319 value='<?php echo $row['ndc_number'] ?>' style='width:100%'
320 onkeyup='maskkeyup(this,"<?php echo addslashes($GLOBALS['gbl_mask_product_id']); ?>")'
321 onblur='maskblur(this,"<?php echo addslashes($GLOBALS['gbl_mask_product_id']); ?>")'
323 </td>
324 </tr>
326 <tr>
327 <td valign='top' nowrap><b><?php xl('On Order','e'); ?>:</b></td>
328 <td>
329 <input type='text' size='5' name='form_on_order' maxlength='7' value='<?php echo $row['on_order'] ?>' />
330 </td>
331 </tr>
333 <tr>
334 <td valign='top' nowrap><b><?php xl('Reorder At','e'); ?>:</b></td>
335 <td>
336 <input type='text' size='5' name='form_reorder_point' maxlength='7' value='<?php echo $row['reorder_point'] ?>' />
337 </td>
338 </tr>
340 <tr class='drugsonly'>
341 <td valign='top' nowrap><b><?php xl('Form','e'); ?>:</b></td>
342 <td>
343 <?php
344 generate_form_field(array('data_type'=>1,'field_id'=>'form','list_id'=>'drug_form','empty_title'=>'SKIP'), $row['form']);
346 </td>
347 </tr>
349 <tr class='drugsonly'>
350 <td valign='top' nowrap><b><?php xl('Pill Size','e'); ?>:</b></td>
351 <td>
352 <input type='text' size='5' name='form_size' maxlength='7' value='<?php echo $row['size'] ?>' />
353 </td>
354 </tr>
356 <tr class='drugsonly'>
357 <td valign='top' nowrap><b><?php xl('Units','e'); ?>:</b></td>
358 <td>
359 <?php
360 generate_form_field(array('data_type'=>1,'field_id'=>'unit','list_id'=>'drug_units','empty_title'=>'SKIP'), $row['unit']);
362 </td>
363 </tr>
365 <tr class='drugsonly'>
366 <td valign='top' nowrap><b><?php xl('Route','e'); ?>:</b></td>
367 <td>
368 <?php
369 generate_form_field(array('data_type'=>1,'field_id'=>'route','list_id'=>'drug_route','empty_title'=>'SKIP'), $row['route']);
371 </td>
372 </tr>
374 <tr class='ippfonly'>
375 <td valign='top' nowrap><b><?php xl('CYP Factor','e'); ?>:</b></td>
376 <td>
377 <input type='text' size='10' name='form_cyp_factor' maxlength='20' value='<?php echo $row['cyp_factor'] ?>' />
378 </td>
379 </tr>
381 <tr>
382 <td valign='top' nowrap><b><?php xl('Relate To','e'); ?>:</b></td>
383 <td>
384 <input type='text' size='50' name='form_related_code'
385 value='<?php echo $row['related_code'] ?>' onclick='sel_related()'
386 title='<?php xl('Click to select related code','e'); ?>'
387 style='width:100%' readonly />
388 </td>
389 </tr>
391 <tr>
392 <td valign='top' nowrap>
393 <b><?php $GLOBALS['sell_non_drug_products'] == 2 ? xl('Fees','e') : xl('Templates','e'); ?>:</b>
394 </td>
395 <td>
396 <table border='0' width='100%'>
397 <tr>
398 <td class='drugsonly'><b><?php xl('Name' ,'e'); ?></b></td>
399 <td class='drugsonly'><b><?php xl('Schedule','e'); ?></b></td>
400 <td class='drugsonly'><b><?php xl('Interval','e'); ?></b></td>
401 <td class='drugsonly'><b><?php xl('Qty' ,'e'); ?></b></td>
402 <td class='drugsonly'><b><?php xl('Refills' ,'e'); ?></b></td>
403 <?php
404 // Show a heading for each price level. Also create an array of prices
405 // for new template lines.
406 $emptyPrices = array();
407 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
408 "WHERE list_id = 'pricelevel' ORDER BY seq");
409 while ($prow = sqlFetchArray($pres)) {
410 $emptyPrices[$prow['option_id']] = '';
411 echo " <td><b>" .
412 generate_display_field(array('data_type'=>'1','list_id'=>'pricelevel'), $prow['option_id']) .
413 "</b></td>\n";
415 // Show a heading for each tax rate.
416 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
417 "WHERE list_id = 'taxrate' ORDER BY seq");
418 while ($prow = sqlFetchArray($pres)) {
419 echo " <td><b>" .
420 generate_display_field(array('data_type'=>'1','list_id'=>'taxrate'), $prow['option_id']) .
421 "</b></td>\n";
424 </tr>
425 <?php
426 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ? 1 : 3;
427 if ($tres) {
428 while ($trow = sqlFetchArray($tres)) {
429 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ? 0 : 1;
430 $selector = $trow['selector'];
431 // Get array of prices.
432 $prices = array();
433 $pres = sqlStatement("SELECT lo.option_id, p.pr_price " .
434 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
435 "p.pr_id = '$drug_id' AND p.pr_selector = '$selector' AND " .
436 "p.pr_level = lo.option_id " .
437 "WHERE list_id = 'pricelevel' ORDER BY lo.seq");
438 while ($prow = sqlFetchArray($pres)) {
439 $prices[$prow['option_id']] = $prow['pr_price'];
441 writeTemplateLine($selector, $trow['dosage'], $trow['period'],
442 $trow['quantity'], $trow['refills'], $prices, $trow['taxrates']);
445 for ($i = 0; $i < $blank_lines; ++$i) {
446 $selector = $GLOBALS['sell_non_drug_products'] == 2 ? $row['name'] : '';
447 writeTemplateLine($selector, '', '', '', '', $emptyPrices, '');
450 </table>
451 </td>
452 </tr>
454 </table>
457 <input type='submit' name='form_save' value='<?php xl('Save','e'); ?>' />
459 <?php if (acl_check('admin', 'super')) { ?>
460 &nbsp;
461 <input type='submit' name='form_delete' value='<?php xl('Delete','e'); ?>' style='color:red' />
462 <?php } ?>
464 &nbsp;
465 <input type='button' value='<?php xl('Cancel','e'); ?>' onclick='window.close()' />
467 </p>
469 </center>
470 </form>
472 <script language="JavaScript">
473 <?php
474 if ($alertmsg) {
475 echo "alert('" . htmlentities($alertmsg) . "');\n";
478 </script>
480 </body>
481 </html>