support for multiple price levels, tax rates, related codes, non-prescription products
[openemr.git] / interface / drugs / add_edit_drug.php
blob1400303b1e01360071a1cf18044b26cc23056c85
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 $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, $taxrates) {
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='8' 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='6' 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='3' 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='6' maxlength='12'>";
59 echo "</td>\n";
61 $pres = sqlStatement("SELECT option_id FROM list_options " .
62 "WHERE list_id = 'taxrate' ORDER BY seq");
63 while ($prow = sqlFetchArray($pres)) {
64 echo " <td class='tmplcell'>";
65 echo "<input type='checkbox' name='tmpl[$tmpl_line_no][taxrate][" . $prow['option_id'] . "]' value='1'";
66 if (strpos(":$taxrates", $prow['option_id']) !== false) echo " checked";
67 echo " /></td>\n";
69 echo " </tr>\n";
72 <html>
73 <head>
74 <title><?php echo $drug_id ? xl("Edit") : xl("Add New"); xl (' Drug','e'); ?></title>
75 <link rel=stylesheet href='<?php echo $css_header ?>' type='text/css'>
77 <style>
78 td { font-size:10pt; }
79 </style>
81 <script language="JavaScript">
82 </script>
84 </head>
86 <body <?php echo $top_bg_line;?>>
87 <?php
88 // If we are saving, then save and close the window.
90 if ($_POST['form_save'] || $_POST['form_delete']) {
91 $new_drug = false;
92 if ($drug_id) {
93 if ($_POST['form_save']) { // updating an existing drug
94 sqlStatement("UPDATE drugs SET " .
95 "name = '" . $_POST['form_name'] . "', " .
96 "ndc_number = '" . $_POST['form_ndc_number'] . "', " .
97 "on_order = '" . $_POST['form_on_order'] . "', " .
98 "reorder_point = '" . $_POST['form_reorder_point'] . "', " .
99 "form = '" . $_POST['form_form'] . "', " .
100 "size = '" . $_POST['form_size'] . "', " .
101 "unit = '" . $_POST['form_unit'] . "', " .
102 "route = '" . $_POST['form_route'] . "' " .
103 "WHERE drug_id = '$drug_id'");
104 sqlStatement("DELETE FROM drug_templates WHERE drug_id = '$drug_id'");
106 else { // deleting
107 if (acl_check('admin', 'super')) {
108 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = '$drug_id'");
109 sqlStatement("DELETE FROM drug_templates WHERE drug_id = '$drug_id'");
110 sqlStatement("DELETE FROM drugs WHERE drug_id = '$drug_id'");
111 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
115 else if ($_POST['form_save']) { // saving a new drug
116 $new_drug = true;
117 $drug_id = sqlInsert("INSERT INTO drugs ( " .
118 "name, ndc_number, on_order, reorder_point, form, " .
119 "size, unit, route " .
120 ") VALUES ( " .
121 "'" . $_POST['form_name'] . "', " .
122 "'" . $_POST['form_ndc_number'] . "', " .
123 "'" . $_POST['form_on_order'] . "', " .
124 "'" . $_POST['form_reorder_point'] . "', " .
125 "'" . $_POST['form_form'] . "', " .
126 "'" . $_POST['form_size'] . "', " .
127 "'" . $_POST['form_unit'] . "', " .
128 "'" . $_POST['form_route'] . "' " .
129 ")");
132 if ($_POST['form_save'] && $drug_id) {
133 $tmpl = $_POST['tmpl'];
134 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
135 for ($lino = 1; isset($tmpl["$lino"]['selector']); ++$lino) {
136 $iter = $tmpl["$lino"];
137 $selector = trim($iter['selector']);
138 if ($selector) {
139 $taxrates = "";
140 if (!empty($iter['taxrate'])) {
141 foreach ($iter['taxrate'] as $key => $value) {
142 $taxrates .= "$key:";
145 sqlInsert("INSERT INTO drug_templates ( " .
146 "drug_id, selector, dosage, period, quantity, refills, taxrates " .
147 ") VALUES ( " .
148 "$drug_id, " .
149 "'" . $selector . "', " .
150 "'" . trim($iter['dosage']) . "', " .
151 "'" . trim($iter['period']) . "', " .
152 "'" . trim($iter['quantity']) . "', " .
153 "'" . trim($iter['refills']) . "', " .
154 "'" . $taxrates . "' " .
155 ")");
157 // Add prices for this drug ID and selector.
158 foreach ($iter['price'] as $key => $value) {
159 $value = $value + 0;
160 if ($value) {
161 sqlStatement("INSERT INTO prices ( " .
162 "pr_id, pr_selector, pr_level, pr_price ) VALUES ( " .
163 "'$drug_id', '$selector', '$key', '$value' )");
165 } // end foreach price
166 } // end if selector is present
167 } // end for each selector
168 } // end if saving a drug
170 // Close this window and redisplay the updated list of drugs.
172 echo "<script language='JavaScript'>\n";
173 if ($info_msg) echo " alert('$info_msg');\n";
174 echo " if (opener.refreshme) opener.refreshme();\n";
175 if ($new_drug) {
176 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
177 } else {
178 echo " window.close();\n";
180 echo "</script></body></html>\n";
181 exit();
184 if ($drug_id) {
185 $row = sqlQuery("SELECT * FROM drugs WHERE drug_id = '$drug_id'");
186 $tres = sqlStatement("SELECT * FROM drug_templates WHERE " .
187 "drug_id = '$drug_id' ORDER BY selector");
191 <form method='post' name='theform' action='add_edit_drug.php?drug=<?php echo $drug_id; ?>'>
192 <center>
194 <table border='0' width='100%'>
196 <tr>
197 <td valign='top' nowrap><b><?php xl('Name','e'); ?>:</b></td>
198 <td>
199 <input type='text' size='40' name='form_name' maxlength='80' value='<?php echo $row['name'] ?>' style='width:100%' />
200 </td>
201 </tr>
203 <tr>
204 <td valign='top' nowrap><b><?php xl('NDC Number','e'); ?>:</b></td>
205 <td>
206 <input type='text' size='40' name='form_ndc_number' maxlength='20' value='<?php echo $row['ndc_number'] ?>' style='width:100%' />
207 </td>
208 </tr>
210 <tr>
211 <td valign='top' nowrap><b><?php xl('On Order','e'); ?>:</b></td>
212 <td>
213 <input type='text' size='5' name='form_on_order' maxlength='7' value='<?php echo $row['on_order'] ?>' />
214 </td>
215 </tr>
217 <tr>
218 <td valign='top' nowrap><b><?php xl('Reorder At','e'); ?>:</b></td>
219 <td>
220 <input type='text' size='5' name='form_reorder_point' maxlength='7' value='<?php echo $row['reorder_point'] ?>' />
221 </td>
222 </tr>
224 <tr>
225 <td valign='top' nowrap><b><?php xl('Form','e'); ?>:</b></td>
226 <td>
227 <select name='form_form'>
228 <?php
229 foreach ($form_array as $key => $value) {
230 echo " <option value='$key'";
231 if ($key == $row['form']) echo " selected";
232 echo ">$value\n";
235 </select>
236 </td>
237 </tr>
239 <tr>
240 <td valign='top' nowrap><b><?php xl('Pill Size','e'); ?>:</b></td>
241 <td>
242 <input type='text' size='5' name='form_size' maxlength='7' value='<?php echo $row['size'] ?>' />
243 </td>
244 </tr>
246 <tr>
247 <td valign='top' nowrap><b><?php xl('Units','e'); ?>:</b></td>
248 <td>
249 <select name='form_unit'>
250 <?php
251 foreach ($unit_array as $key => $value) {
252 echo " <option value='$key'";
253 if ($key == $row['unit']) echo " selected";
254 echo ">$value\n";
257 </select>
258 </td>
259 </tr>
261 <tr>
262 <td valign='top' nowrap><b><?php xl('Route','e'); ?>:</b></td>
263 <td>
264 <select name='form_route'>
265 <?php
266 foreach ($route_array as $key => $value) {
267 echo " <option value='$key'";
268 if ($key == $row['route']) echo " selected";
269 echo ">$value\n";
272 </select>
273 </td>
274 </tr>
276 <tr>
277 <td valign='top' nowrap><b><?php xl('Templates','e'); ?>:</b></td>
278 <td>
279 <table border='0' width='100%'>
280 <tr>
281 <td><b><?php xl('Name' ,'e'); ?></b></td>
282 <td><b><?php xl('Schedule','e'); ?></b></td>
283 <td><b><?php xl('Interval','e'); ?></b></td>
284 <td><b><?php xl('Qty' ,'e'); ?></b></td>
285 <td><b><?php xl('Refills' ,'e'); ?></b></td>
286 <?php
287 // Show a heading for each price level. Also create an array of prices
288 // for new template lines.
289 $emptyPrices = array();
290 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
291 "WHERE list_id = 'pricelevel' ORDER BY seq");
292 while ($prow = sqlFetchArray($pres)) {
293 $emptyPrices[$prow['option_id']] = '';
294 echo " <td><b>" . $prow['title'] . "</b></td>\n";
296 // Show a heading for each tax rate.
297 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
298 "WHERE list_id = 'taxrate' ORDER BY seq");
299 while ($prow = sqlFetchArray($pres)) {
300 echo " <td><b>" . $prow['title'] . "</b></td>\n";
303 </tr>
304 <?php
305 $blank_lines = 3;
306 if ($tres) {
307 $blank_lines = 1;
308 while ($trow = sqlFetchArray($tres)) {
309 $selector = $trow['selector'];
310 // Get array of prices.
311 $prices = array();
312 $pres = sqlStatement("SELECT lo.option_id, p.pr_price " .
313 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
314 "p.pr_id = '$drug_id' AND p.pr_selector = '$selector' AND " .
315 "p.pr_level = lo.option_id " .
316 "WHERE list_id = 'pricelevel' ORDER BY lo.seq");
317 while ($prow = sqlFetchArray($pres)) {
318 $prices[$prow['option_id']] = $prow['pr_price'];
320 writeTemplateLine($selector, $trow['dosage'], $trow['period'],
321 $trow['quantity'], $trow['refills'], $prices, $trow['taxrates']);
324 for ($i = 0; $i < $blank_lines; ++$i) {
325 writeTemplateLine('', '', '', '', '', $emptyPrices, '');
328 </table>
329 </td>
330 </tr>
332 </table>
335 <input type='submit' name='form_save' value='<?php xl('Save','e'); ?>' />
337 <?php if (acl_check('admin', 'super')) { ?>
338 &nbsp;
339 <input type='submit' name='form_delete' value='<?php xl('Delete','e'); ?>' style='color:red' />
340 <?php } ?>
342 &nbsp;
343 <input type='button' value='<?php xl('Cancel','e'); ?>' onclick='window.close()' />
345 </p>
347 </center>
348 </form>
349 </body>
350 </html>