fixed display of non-calendar providers in the Billing Report page
[openemr.git] / interface / drugs / add_edit_drug.php
blob63dea2681a8e001b21eb0772e5d1a2c7872a3dfe
1 <?php
2 // Copyright (C) 2006-2009 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>
106 <script language="JavaScript">
108 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
110 // This is for callback by the find-code popup.
111 // Appends to or erases the current list of related codes.
112 function set_related(codetype, code, selector, codedesc) {
113 var f = document.forms[0];
114 var s = f.form_related_code.value;
115 if (code) {
116 if (s.length > 0) s += ';';
117 s += codetype + ':' + code;
118 } else {
119 s = '';
121 f.form_related_code.value = s;
124 // This invokes the find-code popup.
125 function sel_related() {
126 dlgopen('../patient_file/encounter/find_code_popup.php', '_blank', 500, 400);
129 </script>
131 </head>
133 <body class="body_top">
134 <?php
135 // If we are saving, then save and close the window.
136 // First check for duplicates.
138 if ($_POST['form_save']) {
139 $crow = sqlQuery("SELECT COUNT(*) AS count FROM drugs WHERE " .
140 "name = '" . escapedff('form_name') . "' AND " .
141 "form = '" . escapedff('form_form') . "' AND " .
142 "size = '" . escapedff('form_size') . "' AND " .
143 "unit = '" . escapedff('form_unit') . "' AND " .
144 "route = '" . escapedff('form_route') . "' AND " .
145 "drug_id != '$drug_id'");
146 if ($crow['count']) {
147 $alertmsg = xl('Cannot add this entry because it already exists!');
151 if (($_POST['form_save'] || $_POST['form_delete']) && !$alertmsg) {
152 $new_drug = false;
153 if ($drug_id) {
154 if ($_POST['form_save']) { // updating an existing drug
155 sqlStatement("UPDATE drugs SET " .
156 "name = '" . escapedff('form_name') . "', " .
157 "ndc_number = '" . escapedff('form_ndc_number') . "', " .
158 "on_order = '" . escapedff('form_on_order') . "', " .
159 "reorder_point = '" . escapedff('form_reorder_point') . "', " .
160 "form = '" . escapedff('form_form') . "', " .
161 "size = '" . escapedff('form_size') . "', " .
162 "unit = '" . escapedff('form_unit') . "', " .
163 "route = '" . escapedff('form_route') . "', " .
164 "cyp_factor = '" . numericff('form_cyp_factor') . "', " .
165 "related_code = '" . escapedff('form_related_code') . "', " .
166 "active = " . (empty($_POST['form_active']) ? 0 : 1) . " " .
167 "WHERE drug_id = '$drug_id'");
168 sqlStatement("DELETE FROM drug_templates WHERE drug_id = '$drug_id'");
170 else { // deleting
171 if (acl_check('admin', 'super')) {
172 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = '$drug_id'");
173 sqlStatement("DELETE FROM drug_templates WHERE drug_id = '$drug_id'");
174 sqlStatement("DELETE FROM drugs WHERE drug_id = '$drug_id'");
175 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
179 else if ($_POST['form_save']) { // saving a new drug
180 $new_drug = true;
181 $drug_id = sqlInsert("INSERT INTO drugs ( " .
182 "name, ndc_number, on_order, reorder_point, form, " .
183 "size, unit, route, cyp_factor, related_code, active " .
184 ") VALUES ( " .
185 "'" . escapedff('form_name') . "', " .
186 "'" . escapedff('form_ndc_number') . "', " .
187 "'" . escapedff('form_on_order') . "', " .
188 "'" . escapedff('form_reorder_point') . "', " .
189 "'" . escapedff('form_form') . "', " .
190 "'" . escapedff('form_size') . "', " .
191 "'" . escapedff('form_unit') . "', " .
192 "'" . escapedff('form_route') . "', " .
193 "'" . numericff('form_cyp_factor') . "', " .
194 "'" . escapedff('form_related_code') . "', " .
195 (empty($_POST['form_active']) ? 0 : 1) .
196 ")");
199 if ($_POST['form_save'] && $drug_id) {
200 $tmpl = $_POST['form_tmpl'];
201 // If using the simplified drug form, then force the one and only
202 // selector name to be the same as the product name.
203 if ($GLOBALS['sell_non_drug_products'] == 2) {
204 $tmpl["1"]['selector'] = escapedff('form_name');
206 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
207 for ($lino = 1; isset($tmpl["$lino"]['selector']); ++$lino) {
208 $iter = $tmpl["$lino"];
209 $selector = trim($iter['selector']);
210 if ($selector) {
211 $taxrates = "";
212 if (!empty($iter['taxrate'])) {
213 foreach ($iter['taxrate'] as $key => $value) {
214 $taxrates .= "$key:";
217 sqlInsert("INSERT INTO drug_templates ( " .
218 "drug_id, selector, dosage, period, quantity, refills, taxrates " .
219 ") VALUES ( " .
220 "$drug_id, " .
221 "'" . $selector . "', " .
222 "'" . trim($iter['dosage']) . "', " .
223 "'" . trim($iter['period']) . "', " .
224 "'" . trim($iter['quantity']) . "', " .
225 "'" . trim($iter['refills']) . "', " .
226 "'" . $taxrates . "' " .
227 ")");
229 // Add prices for this drug ID and selector.
230 foreach ($iter['price'] as $key => $value) {
231 $value = $value + 0;
232 if ($value) {
233 sqlStatement("INSERT INTO prices ( " .
234 "pr_id, pr_selector, pr_level, pr_price ) VALUES ( " .
235 "'$drug_id', '$selector', '$key', '$value' )");
237 } // end foreach price
238 } // end if selector is present
239 } // end for each selector
240 } // end if saving a drug
242 // Close this window and redisplay the updated list of drugs.
244 echo "<script language='JavaScript'>\n";
245 if ($info_msg) echo " alert('$info_msg');\n";
246 echo " if (opener.refreshme) opener.refreshme();\n";
247 if ($new_drug) {
248 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
249 } else {
250 echo " window.close();\n";
252 echo "</script></body></html>\n";
253 exit();
256 if ($drug_id) {
257 $row = sqlQuery("SELECT * FROM drugs WHERE drug_id = '$drug_id'");
258 $tres = sqlStatement("SELECT * FROM drug_templates WHERE " .
259 "drug_id = '$drug_id' ORDER BY selector");
263 <form method='post' name='theform' action='add_edit_drug.php?drug=<?php echo $drug_id; ?>'>
264 <center>
266 <table border='0' width='100%'>
268 <tr>
269 <td valign='top' nowrap><b><?php xl('Name','e'); ?>:</b></td>
270 <td>
271 <input type='text' size='40' name='form_name' maxlength='80' value='<?php echo $row['name'] ?>' style='width:100%' />
272 </td>
273 </tr>
275 <tr>
276 <td valign='top' nowrap><b><?php xl('Active','e'); ?>:</b></td>
277 <td>
278 <input type='checkbox' name='form_active' value='1'<?php if ($row['active']) echo ' checked'; ?> />
279 </td>
280 </tr>
282 <tr>
283 <td valign='top' nowrap><b><?php xl('NDC Number','e'); ?>:</b></td>
284 <td>
285 <input type='text' size='40' name='form_ndc_number' maxlength='20' value='<?php echo $row['ndc_number'] ?>' style='width:100%' />
286 </td>
287 </tr>
289 <tr>
290 <td valign='top' nowrap><b><?php xl('On Order','e'); ?>:</b></td>
291 <td>
292 <input type='text' size='5' name='form_on_order' maxlength='7' value='<?php echo $row['on_order'] ?>' />
293 </td>
294 </tr>
296 <tr>
297 <td valign='top' nowrap><b><?php xl('Reorder At','e'); ?>:</b></td>
298 <td>
299 <input type='text' size='5' name='form_reorder_point' maxlength='7' value='<?php echo $row['reorder_point'] ?>' />
300 </td>
301 </tr>
303 <tr class='drugsonly'>
304 <td valign='top' nowrap><b><?php xl('Form','e'); ?>:</b></td>
305 <td>
306 <?php
307 generate_form_field(array('data_type'=>1,'field_id'=>'form','list_id'=>'drug_form','empty_title'=>'SKIP'), $row['form']);
309 </td>
310 </tr>
312 <tr class='drugsonly'>
313 <td valign='top' nowrap><b><?php xl('Pill Size','e'); ?>:</b></td>
314 <td>
315 <input type='text' size='5' name='form_size' maxlength='7' value='<?php echo $row['size'] ?>' />
316 </td>
317 </tr>
319 <tr class='drugsonly'>
320 <td valign='top' nowrap><b><?php xl('Units','e'); ?>:</b></td>
321 <td>
322 <?php
323 generate_form_field(array('data_type'=>1,'field_id'=>'unit','list_id'=>'drug_units','empty_title'=>'SKIP'), $row['unit']);
325 </td>
326 </tr>
328 <tr class='drugsonly'>
329 <td valign='top' nowrap><b><?php xl('Route','e'); ?>:</b></td>
330 <td>
331 <?php
332 generate_form_field(array('data_type'=>1,'field_id'=>'route','list_id'=>'drug_route','empty_title'=>'SKIP'), $row['route']);
334 </td>
335 </tr>
337 <tr class='ippfonly'>
338 <td valign='top' nowrap><b><?php xl('CYP Factor','e'); ?>:</b></td>
339 <td>
340 <input type='text' size='10' name='form_cyp_factor' maxlength='20' value='<?php echo $row['cyp_factor'] ?>' />
341 </td>
342 </tr>
344 <tr>
345 <td valign='top' nowrap><b><?php xl('Relate To','e'); ?>:</b></td>
346 <td>
347 <input type='text' size='50' name='form_related_code'
348 value='<?php echo $row['related_code'] ?>' onclick='sel_related()'
349 title='<?php xl('Click to select related code','e'); ?>'
350 style='width:100%' readonly />
351 </td>
352 </tr>
354 <tr>
355 <td valign='top' nowrap>
356 <b><?php $GLOBALS['sell_non_drug_products'] == 2 ? xl('Fees','e') : xl('Templates','e'); ?>:</b>
357 </td>
358 <td>
359 <table border='0' width='100%'>
360 <tr>
361 <td class='drugsonly'><b><?php xl('Name' ,'e'); ?></b></td>
362 <td class='drugsonly'><b><?php xl('Schedule','e'); ?></b></td>
363 <td class='drugsonly'><b><?php xl('Interval','e'); ?></b></td>
364 <td class='drugsonly'><b><?php xl('Qty' ,'e'); ?></b></td>
365 <td class='drugsonly'><b><?php xl('Refills' ,'e'); ?></b></td>
366 <?php
367 // Show a heading for each price level. Also create an array of prices
368 // for new template lines.
369 $emptyPrices = array();
370 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
371 "WHERE list_id = 'pricelevel' ORDER BY seq");
372 while ($prow = sqlFetchArray($pres)) {
373 $emptyPrices[$prow['option_id']] = '';
374 echo " <td><b>" .
375 generate_display_field(array('data_type'=>'1','list_id'=>'pricelevel'), $prow['option_id']) .
376 "</b></td>\n";
378 // Show a heading for each tax rate.
379 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
380 "WHERE list_id = 'taxrate' ORDER BY seq");
381 while ($prow = sqlFetchArray($pres)) {
382 echo " <td><b>" .
383 generate_display_field(array('data_type'=>'1','list_id'=>'taxrate'), $prow['option_id']) .
384 "</b></td>\n";
387 </tr>
388 <?php
389 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ? 1 : 3;
390 if ($tres) {
391 while ($trow = sqlFetchArray($tres)) {
392 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ? 0 : 1;
393 $selector = $trow['selector'];
394 // Get array of prices.
395 $prices = array();
396 $pres = sqlStatement("SELECT lo.option_id, p.pr_price " .
397 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
398 "p.pr_id = '$drug_id' AND p.pr_selector = '$selector' AND " .
399 "p.pr_level = lo.option_id " .
400 "WHERE list_id = 'pricelevel' ORDER BY lo.seq");
401 while ($prow = sqlFetchArray($pres)) {
402 $prices[$prow['option_id']] = $prow['pr_price'];
404 writeTemplateLine($selector, $trow['dosage'], $trow['period'],
405 $trow['quantity'], $trow['refills'], $prices, $trow['taxrates']);
408 for ($i = 0; $i < $blank_lines; ++$i) {
409 $selector = $GLOBALS['sell_non_drug_products'] == 2 ? $row['name'] : '';
410 writeTemplateLine($selector, '', '', '', '', $emptyPrices, '');
413 </table>
414 </td>
415 </tr>
417 </table>
420 <input type='submit' name='form_save' value='<?php xl('Save','e'); ?>' />
422 <?php if (acl_check('admin', 'super')) { ?>
423 &nbsp;
424 <input type='submit' name='form_delete' value='<?php xl('Delete','e'); ?>' style='color:red' />
425 <?php } ?>
427 &nbsp;
428 <input type='button' value='<?php xl('Cancel','e'); ?>' onclick='window.close()' />
430 </p>
432 </center>
433 </form>
435 <script language="JavaScript">
436 <?php
437 if ($alertmsg) {
438 echo "alert('" . htmlentities($alertmsg) . "');\n";
441 </script>
443 </body>
444 </html>