added date to oldEvt call
[openemr.git] / interface / drugs / add_edit_drug.php
blob463cddd020412886824c3cc69560adc5bc823c98
1 <?php
2 // Copyright (C) 2006, 2008 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");
13 $alertmsg = '';
14 $drug_id = $_REQUEST['drug'];
15 $info_msg = "";
16 $tmpl_line_no = 0;
18 if (!acl_check('admin', 'drugs')) die("Not authorized!");
20 // Format dollars for display.
22 function bucks($amount) {
23 if ($amount) {
24 $amount = sprintf("%.2f", $amount);
25 if ($amount != 0.00) return $amount;
27 return '';
30 // Write a line of data for one template to the form.
32 function writeTemplateLine($selector, $dosage, $period, $quantity, $refills, $prices, $taxrates) {
33 global $tmpl_line_no, $interval_array;
34 ++$tmpl_line_no;
36 echo " <tr>\n";
37 echo " <td class='tmplcell drugsonly'>";
38 echo "<input type='text' name='tmpl[$tmpl_line_no][selector]' value='$selector' size='8' maxlength='100'>";
39 echo "</td>\n";
40 echo " <td class='tmplcell drugsonly'>";
41 echo "<input type='text' name='tmpl[$tmpl_line_no][dosage]' value='$dosage' size='6' maxlength='10'>";
42 echo "</td>\n";
43 echo " <td class='tmplcell drugsonly'>";
44 echo "<select name='tmpl[$tmpl_line_no][period]'>";
45 foreach ($interval_array as $key => $value) {
46 echo "<option value='$key'";
47 if ($key == $period) echo " selected";
48 echo ">$value</option>";
50 echo "</td>\n";
51 echo " <td class='tmplcell drugsonly'>";
52 echo "<input type='text' name='tmpl[$tmpl_line_no][quantity]' value='$quantity' size='3' maxlength='7'>";
53 echo "</td>\n";
54 echo " <td class='tmplcell drugsonly'>";
55 echo "<input type='text' name='tmpl[$tmpl_line_no][refills]' value='$refills' size='3' maxlength='5'>";
56 echo "</td>\n";
57 foreach ($prices as $pricelevel => $price) {
58 echo " <td class='tmplcell'>";
59 echo "<input type='text' name='tmpl[$tmpl_line_no][price][$pricelevel]' value='$price' size='6' maxlength='12'>";
60 echo "</td>\n";
62 $pres = sqlStatement("SELECT option_id FROM list_options " .
63 "WHERE list_id = 'taxrate' ORDER BY seq");
64 while ($prow = sqlFetchArray($pres)) {
65 echo " <td class='tmplcell'>";
66 echo "<input type='checkbox' name='tmpl[$tmpl_line_no][taxrate][" . $prow['option_id'] . "]' value='1'";
67 if (strpos(":$taxrates", $prow['option_id']) !== false) echo " checked";
68 echo " /></td>\n";
70 echo " </tr>\n";
73 // Translation for form fields.
74 function escapedff($name) {
75 $field = trim($_POST[$name]);
76 if (!get_magic_quotes_gpc()) return addslashes($field);
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 </style>
97 <script type="text/javascript" src="../../library/topdialog.js"></script>
98 <script type="text/javascript" src="../../library/dialog.js"></script>
100 <script language="JavaScript">
102 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
104 // This is for callback by the find-code popup.
105 // Appends to or erases the current list of related codes.
106 function set_related(codetype, code, selector, codedesc) {
107 var f = document.forms[0];
108 var s = f.form_related_code.value;
109 if (code) {
110 if (s.length > 0) s += ';';
111 s += codetype + ':' + code;
112 } else {
113 s = '';
115 f.form_related_code.value = s;
118 // This invokes the find-code popup.
119 function sel_related() {
120 dlgopen('../patient_file/encounter/find_code_popup.php', '_blank', 500, 400);
123 </script>
125 </head>
127 <body class="body_top">
128 <?php
129 // If we are saving, then save and close the window.
130 // First check for duplicates.
132 if ($_POST['form_save']) {
133 $crow = sqlQuery("SELECT COUNT(*) AS count FROM drugs WHERE " .
134 "name = '" . escapedff('form_name') . "' AND " .
135 "form = '" . escapedff('form_form') . "' AND " .
136 "size = '" . escapedff('form_size') . "' AND " .
137 "unit = '" . escapedff('form_unit') . "' AND " .
138 "route = '" . escapedff('form_route') . "' AND " .
139 "drug_id != '$drug_id'");
140 if ($crow['count']) {
141 $alertmsg = "Cannot add this entry because it already exists!";
145 if (($_POST['form_save'] || $_POST['form_delete']) && !$alertmsg) {
146 $new_drug = false;
147 if ($drug_id) {
148 if ($_POST['form_save']) { // updating an existing drug
149 sqlStatement("UPDATE drugs SET " .
150 "name = '" . escapedff('form_name') . "', " .
151 "ndc_number = '" . escapedff('form_ndc_number') . "', " .
152 "on_order = '" . escapedff('form_on_order') . "', " .
153 "reorder_point = '" . escapedff('form_reorder_point') . "', " .
154 "form = '" . escapedff('form_form') . "', " .
155 "size = '" . escapedff('form_size') . "', " .
156 "unit = '" . escapedff('form_unit') . "', " .
157 "route = '" . escapedff('form_route') . "', " .
158 "related_code = '" . escapedff('form_related_code') . "' " .
159 "WHERE drug_id = '$drug_id'");
160 sqlStatement("DELETE FROM drug_templates WHERE drug_id = '$drug_id'");
162 else { // deleting
163 if (acl_check('admin', 'super')) {
164 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = '$drug_id'");
165 sqlStatement("DELETE FROM drug_templates WHERE drug_id = '$drug_id'");
166 sqlStatement("DELETE FROM drugs WHERE drug_id = '$drug_id'");
167 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
171 else if ($_POST['form_save']) { // saving a new drug
172 $new_drug = true;
173 $drug_id = sqlInsert("INSERT INTO drugs ( " .
174 "name, ndc_number, on_order, reorder_point, form, " .
175 "size, unit, route, related_code " .
176 ") VALUES ( " .
177 "'" . escapedff('form_name') . "', " .
178 "'" . escapedff('form_ndc_number') . "', " .
179 "'" . escapedff('form_on_order') . "', " .
180 "'" . escapedff('form_reorder_point') . "', " .
181 "'" . escapedff('form_form') . "', " .
182 "'" . escapedff('form_size') . "', " .
183 "'" . escapedff('form_unit') . "', " .
184 "'" . escapedff('form_route') . "', " .
185 "'" . escapedff('form_related_code') . "' " .
186 ")");
189 if ($_POST['form_save'] && $drug_id) {
190 $tmpl = $_POST['tmpl'];
191 // If using the simplified drug form, then force the one and only
192 // selector name to be the same as the product name.
193 if ($GLOBALS['sell_non_drug_products'] == 2) {
194 $tmpl["1"]['selector'] = escapedff('form_name');
196 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
197 for ($lino = 1; isset($tmpl["$lino"]['selector']); ++$lino) {
198 $iter = $tmpl["$lino"];
199 $selector = trim($iter['selector']);
200 if ($selector) {
201 $taxrates = "";
202 if (!empty($iter['taxrate'])) {
203 foreach ($iter['taxrate'] as $key => $value) {
204 $taxrates .= "$key:";
207 sqlInsert("INSERT INTO drug_templates ( " .
208 "drug_id, selector, dosage, period, quantity, refills, taxrates " .
209 ") VALUES ( " .
210 "$drug_id, " .
211 "'" . $selector . "', " .
212 "'" . trim($iter['dosage']) . "', " .
213 "'" . trim($iter['period']) . "', " .
214 "'" . trim($iter['quantity']) . "', " .
215 "'" . trim($iter['refills']) . "', " .
216 "'" . $taxrates . "' " .
217 ")");
219 // Add prices for this drug ID and selector.
220 foreach ($iter['price'] as $key => $value) {
221 $value = $value + 0;
222 if ($value) {
223 sqlStatement("INSERT INTO prices ( " .
224 "pr_id, pr_selector, pr_level, pr_price ) VALUES ( " .
225 "'$drug_id', '$selector', '$key', '$value' )");
227 } // end foreach price
228 } // end if selector is present
229 } // end for each selector
230 } // end if saving a drug
232 // Close this window and redisplay the updated list of drugs.
234 echo "<script language='JavaScript'>\n";
235 if ($info_msg) echo " alert('$info_msg');\n";
236 echo " if (opener.refreshme) opener.refreshme();\n";
237 if ($new_drug) {
238 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
239 } else {
240 echo " window.close();\n";
242 echo "</script></body></html>\n";
243 exit();
246 if ($drug_id) {
247 $row = sqlQuery("SELECT * FROM drugs WHERE drug_id = '$drug_id'");
248 $tres = sqlStatement("SELECT * FROM drug_templates WHERE " .
249 "drug_id = '$drug_id' ORDER BY selector");
253 <form method='post' name='theform' action='add_edit_drug.php?drug=<?php echo $drug_id; ?>'>
254 <center>
256 <table border='0' width='100%'>
258 <tr>
259 <td valign='top' nowrap><b><?php xl('Name','e'); ?>:</b></td>
260 <td>
261 <input type='text' size='40' name='form_name' maxlength='80' value='<?php echo $row['name'] ?>' style='width:100%' />
262 </td>
263 </tr>
265 <tr>
266 <td valign='top' nowrap><b><?php xl('NDC Number','e'); ?>:</b></td>
267 <td>
268 <input type='text' size='40' name='form_ndc_number' maxlength='20' value='<?php echo $row['ndc_number'] ?>' style='width:100%' />
269 </td>
270 </tr>
272 <tr>
273 <td valign='top' nowrap><b><?php xl('On Order','e'); ?>:</b></td>
274 <td>
275 <input type='text' size='5' name='form_on_order' maxlength='7' value='<?php echo $row['on_order'] ?>' />
276 </td>
277 </tr>
279 <tr>
280 <td valign='top' nowrap><b><?php xl('Reorder At','e'); ?>:</b></td>
281 <td>
282 <input type='text' size='5' name='form_reorder_point' maxlength='7' value='<?php echo $row['reorder_point'] ?>' />
283 </td>
284 </tr>
286 <tr class='drugsonly'>
287 <td valign='top' nowrap><b><?php xl('Form','e'); ?>:</b></td>
288 <td>
289 <select name='form_form'>
290 <?php
291 foreach ($form_array as $key => $value) {
292 echo " <option value='$key'";
293 if ($key == $row['form']) echo " selected";
294 echo ">$value\n";
297 </select>
298 </td>
299 </tr>
301 <tr class='drugsonly'>
302 <td valign='top' nowrap><b><?php xl('Pill Size','e'); ?>:</b></td>
303 <td>
304 <input type='text' size='5' name='form_size' maxlength='7' value='<?php echo $row['size'] ?>' />
305 </td>
306 </tr>
308 <tr class='drugsonly'>
309 <td valign='top' nowrap><b><?php xl('Units','e'); ?>:</b></td>
310 <td>
311 <select name='form_unit'>
312 <?php
313 foreach ($unit_array as $key => $value) {
314 echo " <option value='$key'";
315 if ($key == $row['unit']) echo " selected";
316 echo ">$value\n";
319 </select>
320 </td>
321 </tr>
323 <tr class='drugsonly'>
324 <td valign='top' nowrap><b><?php xl('Route','e'); ?>:</b></td>
325 <td>
326 <select name='form_route'>
327 <?php
328 foreach ($route_array as $key => $value) {
329 echo " <option value='$key'";
330 if ($key == $row['route']) echo " selected";
331 echo ">$value\n";
334 </select>
335 </td>
336 </tr>
338 <tr>
339 <td valign='top' nowrap><b><?php xl('Relate To','e'); ?>:</b></td>
340 <td>
341 <input type='text' size='50' name='form_related_code'
342 value='<?php echo $row['related_code'] ?>' onclick='sel_related()'
343 title='<?php xl('Click to select related code','e'); ?>'
344 style='width:100%' readonly />
345 </td>
346 </tr>
348 <tr>
349 <td valign='top' nowrap>
350 <b><?php xl($GLOBALS['sell_non_drug_products'] == 2 ? 'Fees' : 'Templates','e'); ?>:</b>
351 </td>
352 <td>
353 <table border='0' width='100%'>
354 <tr>
355 <td class='drugsonly'><b><?php xl('Name' ,'e'); ?></b></td>
356 <td class='drugsonly'><b><?php xl('Schedule','e'); ?></b></td>
357 <td class='drugsonly'><b><?php xl('Interval','e'); ?></b></td>
358 <td class='drugsonly'><b><?php xl('Qty' ,'e'); ?></b></td>
359 <td class='drugsonly'><b><?php xl('Refills' ,'e'); ?></b></td>
360 <?php
361 // Show a heading for each price level. Also create an array of prices
362 // for new template lines.
363 $emptyPrices = array();
364 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
365 "WHERE list_id = 'pricelevel' ORDER BY seq");
366 while ($prow = sqlFetchArray($pres)) {
367 $emptyPrices[$prow['option_id']] = '';
368 echo " <td><b>" . $prow['title'] . "</b></td>\n";
370 // Show a heading for each tax rate.
371 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
372 "WHERE list_id = 'taxrate' ORDER BY seq");
373 while ($prow = sqlFetchArray($pres)) {
374 echo " <td><b>" . $prow['title'] . "</b></td>\n";
377 </tr>
378 <?php
379 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ? 1 : 3;
380 if ($tres) {
381 while ($trow = sqlFetchArray($tres)) {
382 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ? 0 : 1;
383 $selector = $trow['selector'];
384 // Get array of prices.
385 $prices = array();
386 $pres = sqlStatement("SELECT lo.option_id, p.pr_price " .
387 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
388 "p.pr_id = '$drug_id' AND p.pr_selector = '$selector' AND " .
389 "p.pr_level = lo.option_id " .
390 "WHERE list_id = 'pricelevel' ORDER BY lo.seq");
391 while ($prow = sqlFetchArray($pres)) {
392 $prices[$prow['option_id']] = $prow['pr_price'];
394 writeTemplateLine($selector, $trow['dosage'], $trow['period'],
395 $trow['quantity'], $trow['refills'], $prices, $trow['taxrates']);
398 for ($i = 0; $i < $blank_lines; ++$i) {
399 $selector = $GLOBALS['sell_non_drug_products'] == 2 ? $row['name'] : '';
400 writeTemplateLine($selector, '', '', '', '', $emptyPrices, '');
403 </table>
404 </td>
405 </tr>
407 </table>
410 <input type='submit' name='form_save' value='<?php xl('Save','e'); ?>' />
412 <?php if (acl_check('admin', 'super')) { ?>
413 &nbsp;
414 <input type='submit' name='form_delete' value='<?php xl('Delete','e'); ?>' style='color:red' />
415 <?php } ?>
417 &nbsp;
418 <input type='button' value='<?php xl('Cancel','e'); ?>' onclick='window.close()' />
420 </p>
422 </center>
423 </form>
425 <script language="JavaScript">
426 <?php
427 if ($alertmsg) {
428 echo "alert('" . htmlentities($alertmsg) . "');\n";
431 </script>
433 </body>
434 </html>