2 // Copyright (C) 2006 Rod Roark <rod@sunsetsystems.com>
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'];
17 if (!acl_check('admin', 'drugs')) die("Not authorized!");
19 // Format dollars for display.
21 function bucks($amount) {
23 $amount = sprintf("%.2f", $amount);
24 if ($amount != 0.00) return $amount;
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;
36 echo " <td class='tmplcell'>";
37 echo "<input type='text' name='tmpl[$tmpl_line_no][selector]' value='$selector' size='10' maxlength='100'>";
39 echo " <td class='tmplcell'>";
40 echo "<input type='text' name='tmpl[$tmpl_line_no][dosage]' value='$dosage' size='10' maxlength='10'>";
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>";
50 echo " <td class='tmplcell'>";
51 echo "<input type='text' name='tmpl[$tmpl_line_no][quantity]' value='$quantity' size='5' maxlength='7'>";
53 echo " <td class='tmplcell'>";
54 echo "<input type='text' name='tmpl[$tmpl_line_no][refills]' value='$refills' size='3' maxlength='5'>";
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'>";
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'>
71 td
{ font
-size
:10pt
; }
74 <script language
="JavaScript">
79 <body
<?php
echo $top_bg_line;?
>>
81 // If we are saving, then save and close the window.
83 if ($_POST['form_save'] ||
$_POST['form_delete']) {
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'");
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
110 $drug_id = sqlInsert("INSERT INTO drugs ( " .
111 "name, ndc_number, on_order, reorder_point, form, " .
112 "size, unit, route " .
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'] . "' " .
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']);
132 sqlInsert("INSERT INTO drug_templates ( " .
133 "drug_id, selector, dosage, period, quantity, refills " .
136 "'" . $selector . "', " .
137 "'" . trim($iter['dosage']) . "', " .
138 "'" . trim($iter['period']) . "', " .
139 "'" . trim($iter['quantity']) . "', " .
140 "'" . trim($iter['refills']) . "' " .
143 // Add prices for this drug ID and selector.
144 foreach ($iter['price'] as $key => $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";
162 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
164 echo " window.close();\n";
166 echo "</script></body></html>\n";
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; ?>'>
180 <table border
='0' width
='100%'>
183 <td valign
='top' nowrap
><b
><?php
xl('Name','e'); ?
>:</b
></td
>
185 <input type
='text' size
='40' name
='form_name' maxlength
='80' value
='<?php echo $row['name
'] ?>' style
='width:100%' />
190 <td valign
='top' nowrap
><b
><?php
xl('NDC Number','e'); ?
>:</b
></td
>
192 <input type
='text' size
='40' name
='form_ndc_number' maxlength
='20' value
='<?php echo $row['ndc_number
'] ?>' style
='width:100%' />
197 <td valign
='top' nowrap
><b
><?php
xl('On Order','e'); ?
>:</b
></td
>
199 <input type
='text' size
='5' name
='form_on_order' maxlength
='7' value
='<?php echo $row['on_order
'] ?>' />
204 <td valign
='top' nowrap
><b
><?php
xl('Reorder At','e'); ?
>:</b
></td
>
206 <input type
='text' size
='5' name
='form_reorder_point' maxlength
='7' value
='<?php echo $row['reorder_point
'] ?>' />
211 <td valign
='top' nowrap
><b
><?php
xl('Form','e'); ?
>:</b
></td
>
213 <select name
='form_form'>
215 foreach ($form_array as $key => $value) {
216 echo " <option value='$key'";
217 if ($key == $row['form']) echo " selected";
226 <td valign
='top' nowrap
><b
><?php
xl('Pill Size','e'); ?
>:</b
></td
>
228 <input type
='text' size
='5' name
='form_size' maxlength
='7' value
='<?php echo $row['size
'] ?>' />
233 <td valign
='top' nowrap
><b
><?php
xl('Units','e'); ?
>:</b
></td
>
235 <select name
='form_unit'>
237 foreach ($unit_array as $key => $value) {
238 echo " <option value='$key'";
239 if ($key == $row['unit']) echo " selected";
248 <td valign
='top' nowrap
><b
><?php
xl('Route','e'); ?
>:</b
></td
>
250 <select name
='form_route'>
252 foreach ($route_array as $key => $value) {
253 echo " <option value='$key'";
254 if ($key == $row['route']) echo " selected";
263 <td valign
='top' nowrap
><b
><?php
xl('Templates','e'); ?
>:</b
></td
>
265 <table border
='0' width
='100%'>
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
>
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";
288 while ($trow = sqlFetchArray($tres)) {
289 $selector = $trow['selector'];
290 // Get array of prices.
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);
315 <input type
='submit' name
='form_save' value
='<?php xl('Save
','e
'); ?>' />
317 <?php
if (acl_check('admin', 'super')) { ?
>
319 <input type
='submit' name
='form_delete' value
='<?php xl('Delete
','e
'); ?>' style
='color:red' />
323 <input type
='button' value
='<?php xl('Cancel
','e
'); ?>' onclick
='window.close()' />