added ending dates of service
[openemr.git] / interface / drugs / add_edit_drug.php
blobcf79761ea2e51627535c4ddca9b9f61c32088acb
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;
79 function numericff($name) {
80 $field = trim($_POST[$name]) + 0;
81 return $field;
84 <html>
85 <head>
86 <?php html_header_show();?>
87 <title><?php echo $drug_id ? xl("Edit") : xl("Add New"); xl (' Drug','e'); ?></title>
88 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
90 <style>
91 td { font-size:10pt; }
93 <?php if ($GLOBALS['sell_non_drug_products'] == 2) { ?>
94 .drugsonly { display:none; }
95 <?php } else { ?>
96 .drugsonly { }
97 <?php } ?>
99 <?php if (empty($GLOBALS['ippf_specific'])) { ?>
100 .ippfonly { display:none; }
101 <?php } else { ?>
102 .ippfonly { }
103 <?php } ?>
105 </style>
107 <script type="text/javascript" src="../../library/topdialog.js"></script>
108 <script type="text/javascript" src="../../library/dialog.js"></script>
110 <script language="JavaScript">
112 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
114 // This is for callback by the find-code popup.
115 // Appends to or erases the current list of related codes.
116 function set_related(codetype, code, selector, codedesc) {
117 var f = document.forms[0];
118 var s = f.form_related_code.value;
119 if (code) {
120 if (s.length > 0) s += ';';
121 s += codetype + ':' + code;
122 } else {
123 s = '';
125 f.form_related_code.value = s;
128 // This invokes the find-code popup.
129 function sel_related() {
130 dlgopen('../patient_file/encounter/find_code_popup.php', '_blank', 500, 400);
133 </script>
135 </head>
137 <body class="body_top">
138 <?php
139 // If we are saving, then save and close the window.
140 // First check for duplicates.
142 if ($_POST['form_save']) {
143 $crow = sqlQuery("SELECT COUNT(*) AS count FROM drugs WHERE " .
144 "name = '" . escapedff('form_name') . "' AND " .
145 "form = '" . escapedff('form_form') . "' AND " .
146 "size = '" . escapedff('form_size') . "' AND " .
147 "unit = '" . escapedff('form_unit') . "' AND " .
148 "route = '" . escapedff('form_route') . "' AND " .
149 "drug_id != '$drug_id'");
150 if ($crow['count']) {
151 $alertmsg = "Cannot add this entry because it already exists!";
155 if (($_POST['form_save'] || $_POST['form_delete']) && !$alertmsg) {
156 $new_drug = false;
157 if ($drug_id) {
158 if ($_POST['form_save']) { // updating an existing drug
159 sqlStatement("UPDATE drugs SET " .
160 "name = '" . escapedff('form_name') . "', " .
161 "ndc_number = '" . escapedff('form_ndc_number') . "', " .
162 "on_order = '" . escapedff('form_on_order') . "', " .
163 "reorder_point = '" . escapedff('form_reorder_point') . "', " .
164 "form = '" . escapedff('form_form') . "', " .
165 "size = '" . escapedff('form_size') . "', " .
166 "unit = '" . escapedff('form_unit') . "', " .
167 "route = '" . escapedff('form_route') . "', " .
168 "cyp_factor = '" . numericff('form_cyp_factor') . "', " .
169 "related_code = '" . escapedff('form_related_code') . "' " .
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 ") VALUES ( " .
188 "'" . escapedff('form_name') . "', " .
189 "'" . escapedff('form_ndc_number') . "', " .
190 "'" . escapedff('form_on_order') . "', " .
191 "'" . escapedff('form_reorder_point') . "', " .
192 "'" . escapedff('form_form') . "', " .
193 "'" . escapedff('form_size') . "', " .
194 "'" . escapedff('form_unit') . "', " .
195 "'" . escapedff('form_route') . "', " .
196 "'" . numericff('form_cyp_factor') . "', " .
197 "'" . escapedff('form_related_code') . "' " .
198 ")");
201 if ($_POST['form_save'] && $drug_id) {
202 $tmpl = $_POST['tmpl'];
203 // If using the simplified drug form, then force the one and only
204 // selector name to be the same as the product name.
205 if ($GLOBALS['sell_non_drug_products'] == 2) {
206 $tmpl["1"]['selector'] = escapedff('form_name');
208 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
209 for ($lino = 1; isset($tmpl["$lino"]['selector']); ++$lino) {
210 $iter = $tmpl["$lino"];
211 $selector = trim($iter['selector']);
212 if ($selector) {
213 $taxrates = "";
214 if (!empty($iter['taxrate'])) {
215 foreach ($iter['taxrate'] as $key => $value) {
216 $taxrates .= "$key:";
219 sqlInsert("INSERT INTO drug_templates ( " .
220 "drug_id, selector, dosage, period, quantity, refills, taxrates " .
221 ") VALUES ( " .
222 "$drug_id, " .
223 "'" . $selector . "', " .
224 "'" . trim($iter['dosage']) . "', " .
225 "'" . trim($iter['period']) . "', " .
226 "'" . trim($iter['quantity']) . "', " .
227 "'" . trim($iter['refills']) . "', " .
228 "'" . $taxrates . "' " .
229 ")");
231 // Add prices for this drug ID and selector.
232 foreach ($iter['price'] as $key => $value) {
233 $value = $value + 0;
234 if ($value) {
235 sqlStatement("INSERT INTO prices ( " .
236 "pr_id, pr_selector, pr_level, pr_price ) VALUES ( " .
237 "'$drug_id', '$selector', '$key', '$value' )");
239 } // end foreach price
240 } // end if selector is present
241 } // end for each selector
242 } // end if saving a drug
244 // Close this window and redisplay the updated list of drugs.
246 echo "<script language='JavaScript'>\n";
247 if ($info_msg) echo " alert('$info_msg');\n";
248 echo " if (opener.refreshme) opener.refreshme();\n";
249 if ($new_drug) {
250 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
251 } else {
252 echo " window.close();\n";
254 echo "</script></body></html>\n";
255 exit();
258 if ($drug_id) {
259 $row = sqlQuery("SELECT * FROM drugs WHERE drug_id = '$drug_id'");
260 $tres = sqlStatement("SELECT * FROM drug_templates WHERE " .
261 "drug_id = '$drug_id' ORDER BY selector");
265 <form method='post' name='theform' action='add_edit_drug.php?drug=<?php echo $drug_id; ?>'>
266 <center>
268 <table border='0' width='100%'>
270 <tr>
271 <td valign='top' nowrap><b><?php xl('Name','e'); ?>:</b></td>
272 <td>
273 <input type='text' size='40' name='form_name' maxlength='80' value='<?php echo $row['name'] ?>' style='width:100%' />
274 </td>
275 </tr>
277 <tr>
278 <td valign='top' nowrap><b><?php xl('NDC Number','e'); ?>:</b></td>
279 <td>
280 <input type='text' size='40' name='form_ndc_number' maxlength='20' value='<?php echo $row['ndc_number'] ?>' style='width:100%' />
281 </td>
282 </tr>
284 <tr>
285 <td valign='top' nowrap><b><?php xl('On Order','e'); ?>:</b></td>
286 <td>
287 <input type='text' size='5' name='form_on_order' maxlength='7' value='<?php echo $row['on_order'] ?>' />
288 </td>
289 </tr>
291 <tr>
292 <td valign='top' nowrap><b><?php xl('Reorder At','e'); ?>:</b></td>
293 <td>
294 <input type='text' size='5' name='form_reorder_point' maxlength='7' value='<?php echo $row['reorder_point'] ?>' />
295 </td>
296 </tr>
298 <tr class='drugsonly'>
299 <td valign='top' nowrap><b><?php xl('Form','e'); ?>:</b></td>
300 <td>
301 <select name='form_form'>
302 <?php
303 foreach ($form_array as $key => $value) {
304 echo " <option value='$key'";
305 if ($key == $row['form']) echo " selected";
306 echo ">$value\n";
309 </select>
310 </td>
311 </tr>
313 <tr class='drugsonly'>
314 <td valign='top' nowrap><b><?php xl('Pill Size','e'); ?>:</b></td>
315 <td>
316 <input type='text' size='5' name='form_size' maxlength='7' value='<?php echo $row['size'] ?>' />
317 </td>
318 </tr>
320 <tr class='drugsonly'>
321 <td valign='top' nowrap><b><?php xl('Units','e'); ?>:</b></td>
322 <td>
323 <select name='form_unit'>
324 <?php
325 foreach ($unit_array as $key => $value) {
326 echo " <option value='$key'";
327 if ($key == $row['unit']) echo " selected";
328 echo ">$value\n";
331 </select>
332 </td>
333 </tr>
335 <tr class='drugsonly'>
336 <td valign='top' nowrap><b><?php xl('Route','e'); ?>:</b></td>
337 <td>
338 <select name='form_route'>
339 <?php
340 foreach ($route_array as $key => $value) {
341 echo " <option value='$key'";
342 if ($key == $row['route']) echo " selected";
343 echo ">$value\n";
346 </select>
347 </td>
348 </tr>
350 <tr class='ippfonly'>
351 <td valign='top' nowrap><b><?php xl('CYP Factor','e'); ?>:</b></td>
352 <td>
353 <input type='text' size='10' name='form_cyp_factor' maxlength='20' value='<?php echo $row['cyp_factor'] ?>' />
354 </td>
355 </tr>
357 <tr>
358 <td valign='top' nowrap><b><?php xl('Relate To','e'); ?>:</b></td>
359 <td>
360 <input type='text' size='50' name='form_related_code'
361 value='<?php echo $row['related_code'] ?>' onclick='sel_related()'
362 title='<?php xl('Click to select related code','e'); ?>'
363 style='width:100%' readonly />
364 </td>
365 </tr>
367 <tr>
368 <td valign='top' nowrap>
369 <b><?php xl($GLOBALS['sell_non_drug_products'] == 2 ? 'Fees' : 'Templates','e'); ?>:</b>
370 </td>
371 <td>
372 <table border='0' width='100%'>
373 <tr>
374 <td class='drugsonly'><b><?php xl('Name' ,'e'); ?></b></td>
375 <td class='drugsonly'><b><?php xl('Schedule','e'); ?></b></td>
376 <td class='drugsonly'><b><?php xl('Interval','e'); ?></b></td>
377 <td class='drugsonly'><b><?php xl('Qty' ,'e'); ?></b></td>
378 <td class='drugsonly'><b><?php xl('Refills' ,'e'); ?></b></td>
379 <?php
380 // Show a heading for each price level. Also create an array of prices
381 // for new template lines.
382 $emptyPrices = array();
383 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
384 "WHERE list_id = 'pricelevel' ORDER BY seq");
385 while ($prow = sqlFetchArray($pres)) {
386 $emptyPrices[$prow['option_id']] = '';
387 echo " <td><b>" . $prow['title'] . "</b></td>\n";
389 // Show a heading for each tax rate.
390 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
391 "WHERE list_id = 'taxrate' ORDER BY seq");
392 while ($prow = sqlFetchArray($pres)) {
393 echo " <td><b>" . $prow['title'] . "</b></td>\n";
396 </tr>
397 <?php
398 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ? 1 : 3;
399 if ($tres) {
400 while ($trow = sqlFetchArray($tres)) {
401 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ? 0 : 1;
402 $selector = $trow['selector'];
403 // Get array of prices.
404 $prices = array();
405 $pres = sqlStatement("SELECT lo.option_id, p.pr_price " .
406 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
407 "p.pr_id = '$drug_id' AND p.pr_selector = '$selector' AND " .
408 "p.pr_level = lo.option_id " .
409 "WHERE list_id = 'pricelevel' ORDER BY lo.seq");
410 while ($prow = sqlFetchArray($pres)) {
411 $prices[$prow['option_id']] = $prow['pr_price'];
413 writeTemplateLine($selector, $trow['dosage'], $trow['period'],
414 $trow['quantity'], $trow['refills'], $prices, $trow['taxrates']);
417 for ($i = 0; $i < $blank_lines; ++$i) {
418 $selector = $GLOBALS['sell_non_drug_products'] == 2 ? $row['name'] : '';
419 writeTemplateLine($selector, '', '', '', '', $emptyPrices, '');
422 </table>
423 </td>
424 </tr>
426 </table>
429 <input type='submit' name='form_save' value='<?php xl('Save','e'); ?>' />
431 <?php if (acl_check('admin', 'super')) { ?>
432 &nbsp;
433 <input type='submit' name='form_delete' value='<?php xl('Delete','e'); ?>' style='color:red' />
434 <?php } ?>
436 &nbsp;
437 <input type='button' value='<?php xl('Cancel','e'); ?>' onclick='window.close()' />
439 </p>
441 </center>
442 </form>
444 <script language="JavaScript">
445 <?php
446 if ($alertmsg) {
447 echo "alert('" . htmlentities($alertmsg) . "');\n";
450 </script>
452 </body>
453 </html>