Fixed patient phone number problem.
[openemr.git] / interface / drugs / add_edit_drug.php
blob36aa08a6a953d419c995b001eb9e2c977ae4413f
1 <?php
2 // Copyright (C) 2006 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 $drug_id = $_REQUEST['drug'];
14 $info_msg = "";
15 $tmpl_line_no = 0;
17 if (!acl_check('admin', 'drugs')) die("Not authorized!");
19 // Format dollars for display.
21 function bucks($amount) {
22 if ($amount) {
23 $amount = sprintf("%.2f", $amount);
24 if ($amount != 0.00) return $amount;
26 return '';
29 // Write a line of data for one template to the form.
31 function writeTemplateLine($selector, $dosage, $period, $quantity, $refills, $prices) {
32 global $tmpl_line_no, $interval_array;
33 ++$tmpl_line_no;
35 echo " <tr>\n";
36 echo " <td class='tmplcell'>";
37 echo "<input type='text' name='tmpl[$tmpl_line_no][selector]' value='$selector' size='10' maxlength='100'>";
38 echo "</td>\n";
39 echo " <td class='tmplcell'>";
40 echo "<input type='text' name='tmpl[$tmpl_line_no][dosage]' value='$dosage' size='10' maxlength='10'>";
41 echo "</td>\n";
42 echo " <td class='tmplcell'>";
43 echo "<select name='tmpl[$tmpl_line_no][period]'>";
44 foreach ($interval_array as $key => $value) {
45 echo "<option value='$key'";
46 if ($key == $period) echo " selected";
47 echo ">$value</option>";
49 echo "</td>\n";
50 echo " <td class='tmplcell'>";
51 echo "<input type='text' name='tmpl[$tmpl_line_no][quantity]' value='$quantity' size='5' maxlength='7'>";
52 echo "</td>\n";
53 echo " <td class='tmplcell'>";
54 echo "<input type='text' name='tmpl[$tmpl_line_no][refills]' value='$refills' size='3' maxlength='5'>";
55 echo "</td>\n";
56 foreach ($prices as $pricelevel => $price) {
57 echo " <td class='tmplcell'>";
58 echo "<input type='text' name='tmpl[$tmpl_line_no][price][$pricelevel]' value='$price' size='7' maxlength='12'>";
59 echo "</td>\n";
62 echo " </tr>\n";
65 <html>
66 <head>
67 <title><?php echo $drug_id ? xl("Edit") : xl("Add New"); xl (' Drug','e'); ?></title>
68 <link rel=stylesheet href='<?php echo $css_header ?>' type='text/css'>
70 <style>
71 td { font-size:10pt; }
72 </style>
74 <script language="JavaScript">
75 </script>
77 </head>
79 <body <?php echo $top_bg_line;?>>
80 <?php
81 // If we are saving, then save and close the window.
83 if ($_POST['form_save'] || $_POST['form_delete']) {
84 $new_drug = false;
85 if ($drug_id) {
86 if ($_POST['form_save']) { // updating an existing drug
87 sqlStatement("UPDATE drugs SET " .
88 "name = '" . $_POST['form_name'] . "', " .
89 "ndc_number = '" . $_POST['form_ndc_number'] . "', " .
90 "on_order = '" . $_POST['form_on_order'] . "', " .
91 "reorder_point = '" . $_POST['form_reorder_point'] . "', " .
92 "form = '" . $_POST['form_form'] . "', " .
93 "size = '" . $_POST['form_size'] . "', " .
94 "unit = '" . $_POST['form_unit'] . "', " .
95 "route = '" . $_POST['form_route'] . "' " .
96 "WHERE drug_id = '$drug_id'");
97 sqlStatement("DELETE FROM drug_templates WHERE drug_id = '$drug_id'");
99 else { // deleting
100 if (acl_check('admin', 'super')) {
101 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = '$drug_id'");
102 sqlStatement("DELETE FROM drug_templates WHERE drug_id = '$drug_id'");
103 sqlStatement("DELETE FROM drugs WHERE drug_id = '$drug_id'");
104 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
108 else if ($_POST['form_save']) { // saving a new drug
109 $new_drug = true;
110 $drug_id = sqlInsert("INSERT INTO drugs ( " .
111 "name, ndc_number, on_order, reorder_point, form, " .
112 "size, unit, route " .
113 ") VALUES ( " .
114 "'" . $_POST['form_name'] . "', " .
115 "'" . $_POST['form_ndc_number'] . "', " .
116 "'" . $_POST['form_on_order'] . "', " .
117 "'" . $_POST['form_reorder_point'] . "', " .
118 "'" . $_POST['form_form'] . "', " .
119 "'" . $_POST['form_size'] . "', " .
120 "'" . $_POST['form_unit'] . "', " .
121 "'" . $_POST['form_route'] . "' " .
122 ")");
125 if ($_POST['form_save'] && $drug_id) {
126 $tmpl = $_POST['tmpl'];
127 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
128 for ($lino = 1; isset($tmpl["$lino"]['selector']); ++$lino) {
129 $iter = $tmpl["$lino"];
130 $selector = trim($iter['selector']);
131 if ($selector) {
132 sqlInsert("INSERT INTO drug_templates ( " .
133 "drug_id, selector, dosage, period, quantity, refills " .
134 ") VALUES ( " .
135 "$drug_id, " .
136 "'" . $selector . "', " .
137 "'" . trim($iter['dosage']) . "', " .
138 "'" . trim($iter['period']) . "', " .
139 "'" . trim($iter['quantity']) . "', " .
140 "'" . trim($iter['refills']) . "' " .
141 ")");
143 // Add prices for this drug ID and selector.
144 foreach ($iter['price'] as $key => $value) {
145 $value = $value + 0;
146 if ($value) {
147 sqlStatement("INSERT INTO prices ( " .
148 "pr_id, pr_selector, pr_level, pr_price ) VALUES ( " .
149 "'$drug_id', '$selector', '$key', '$value' )");
151 } // end foreach price
152 } // end if selector is present
153 } // end for each selector
154 } // end if saving a drug
156 // Close this window and redisplay the updated list of drugs.
158 echo "<script language='JavaScript'>\n";
159 if ($info_msg) echo " alert('$info_msg');\n";
160 echo " if (opener.refreshme) opener.refreshme();\n";
161 if ($new_drug) {
162 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
163 } else {
164 echo " window.close();\n";
166 echo "</script></body></html>\n";
167 exit();
170 if ($drug_id) {
171 $row = sqlQuery("SELECT * FROM drugs WHERE drug_id = '$drug_id'");
172 $tres = sqlStatement("SELECT * FROM drug_templates WHERE " .
173 "drug_id = '$drug_id' ORDER BY selector");
177 <form method='post' name='theform' action='add_edit_drug.php?drug=<?php echo $drug_id; ?>'>
178 <center>
180 <table border='0' width='100%'>
182 <tr>
183 <td valign='top' nowrap><b><?php xl('Name','e'); ?>:</b></td>
184 <td>
185 <input type='text' size='40' name='form_name' maxlength='80' value='<?php echo $row['name'] ?>' style='width:100%' />
186 </td>
187 </tr>
189 <tr>
190 <td valign='top' nowrap><b><?php xl('NDC Number','e'); ?>:</b></td>
191 <td>
192 <input type='text' size='40' name='form_ndc_number' maxlength='20' value='<?php echo $row['ndc_number'] ?>' style='width:100%' />
193 </td>
194 </tr>
196 <tr>
197 <td valign='top' nowrap><b><?php xl('On Order','e'); ?>:</b></td>
198 <td>
199 <input type='text' size='5' name='form_on_order' maxlength='7' value='<?php echo $row['on_order'] ?>' />
200 </td>
201 </tr>
203 <tr>
204 <td valign='top' nowrap><b><?php xl('Reorder At','e'); ?>:</b></td>
205 <td>
206 <input type='text' size='5' name='form_reorder_point' maxlength='7' value='<?php echo $row['reorder_point'] ?>' />
207 </td>
208 </tr>
210 <tr>
211 <td valign='top' nowrap><b><?php xl('Form','e'); ?>:</b></td>
212 <td>
213 <select name='form_form'>
214 <?php
215 foreach ($form_array as $key => $value) {
216 echo " <option value='$key'";
217 if ($key == $row['form']) echo " selected";
218 echo ">$value\n";
221 </select>
222 </td>
223 </tr>
225 <tr>
226 <td valign='top' nowrap><b><?php xl('Pill Size','e'); ?>:</b></td>
227 <td>
228 <input type='text' size='5' name='form_size' maxlength='7' value='<?php echo $row['size'] ?>' />
229 </td>
230 </tr>
232 <tr>
233 <td valign='top' nowrap><b><?php xl('Units','e'); ?>:</b></td>
234 <td>
235 <select name='form_unit'>
236 <?php
237 foreach ($unit_array as $key => $value) {
238 echo " <option value='$key'";
239 if ($key == $row['unit']) echo " selected";
240 echo ">$value\n";
243 </select>
244 </td>
245 </tr>
247 <tr>
248 <td valign='top' nowrap><b><?php xl('Route','e'); ?>:</b></td>
249 <td>
250 <select name='form_route'>
251 <?php
252 foreach ($route_array as $key => $value) {
253 echo " <option value='$key'";
254 if ($key == $row['route']) echo " selected";
255 echo ">$value\n";
258 </select>
259 </td>
260 </tr>
262 <tr>
263 <td valign='top' nowrap><b><?php xl('Templates','e'); ?>:</b></td>
264 <td>
265 <table border='0' width='100%'>
266 <tr>
267 <td><b><?php xl('Name' ,'e'); ?></b></td>
268 <td><b><?php xl('Schedule','e'); ?></b></td>
269 <td><b><?php xl('Interval','e'); ?></b></td>
270 <td><b><?php xl('Qty' ,'e'); ?></b></td>
271 <td><b><?php xl('Refills' ,'e'); ?></b></td>
272 <?php
273 // Show a heading for each price level. Also create an array of prices
274 // for new template lines.
275 $emptyPrices = array();
276 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
277 "WHERE list_id = 'pricelevel' ORDER BY seq");
278 while ($prow = sqlFetchArray($pres)) {
279 $emptyPrices[$prow['option_id']] = '';
280 echo " <td nowrap><b>" . $prow['title'] . "</b></td>\n";
283 </tr>
284 <?php
285 $blank_lines = 3;
286 if ($tres) {
287 $blank_lines = 1;
288 while ($trow = sqlFetchArray($tres)) {
289 $selector = $trow['selector'];
290 // Get array of prices.
291 $prices = array();
292 $pres = sqlStatement("SELECT lo.option_id, p.pr_price " .
293 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
294 "p.pr_id = '$drug_id' AND p.pr_selector = '$selector' AND " .
295 "p.pr_level = lo.option_id " .
296 "WHERE list_id = 'pricelevel' ORDER BY lo.seq");
297 while ($prow = sqlFetchArray($pres)) {
298 $prices[$prow['option_id']] = $prow['pr_price'];
300 writeTemplateLine($selector, $trow['dosage'], $trow['period'],
301 $trow['quantity'], $trow['refills'], $prices);
304 for ($i = 0; $i < $blank_lines; ++$i) {
305 writeTemplateLine('', '', '', '', '', $emptyPrices);
308 </table>
309 </td>
310 </tr>
312 </table>
315 <input type='submit' name='form_save' value='<?php xl('Save','e'); ?>' />
317 <?php if (acl_check('admin', 'super')) { ?>
318 &nbsp;
319 <input type='submit' name='form_delete' value='<?php xl('Delete','e'); ?>' style='color:red' />
320 <?php } ?>
322 &nbsp;
323 <input type='button' value='<?php xl('Cancel','e'); ?>' onclick='window.close()' />
325 </p>
327 </center>
328 </form>
329 </body>
330 </html>