2 // Copyright (C) 2006, 2008 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, $taxrates) {
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='8' maxlength='100'>";
39 echo " <td class='tmplcell'>";
40 echo "<input type='text' name='tmpl[$tmpl_line_no][dosage]' value='$dosage' size='6' 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='3' 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='6' maxlength='12'>";
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";
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'>
78 td
{ font
-size
:10pt
; }
81 <script language
="JavaScript">
86 <body
<?php
echo $top_bg_line;?
>>
88 // If we are saving, then save and close the window.
90 if ($_POST['form_save'] ||
$_POST['form_delete']) {
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'");
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
117 $drug_id = sqlInsert("INSERT INTO drugs ( " .
118 "name, ndc_number, on_order, reorder_point, form, " .
119 "size, unit, route " .
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'] . "' " .
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']);
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 " .
149 "'" . $selector . "', " .
150 "'" . trim($iter['dosage']) . "', " .
151 "'" . trim($iter['period']) . "', " .
152 "'" . trim($iter['quantity']) . "', " .
153 "'" . trim($iter['refills']) . "', " .
154 "'" . $taxrates . "' " .
157 // Add prices for this drug ID and selector.
158 foreach ($iter['price'] as $key => $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";
176 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
178 echo " window.close();\n";
180 echo "</script></body></html>\n";
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; ?>'>
194 <table border
='0' width
='100%'>
197 <td valign
='top' nowrap
><b
><?php
xl('Name','e'); ?
>:</b
></td
>
199 <input type
='text' size
='40' name
='form_name' maxlength
='80' value
='<?php echo $row['name
'] ?>' style
='width:100%' />
204 <td valign
='top' nowrap
><b
><?php
xl('NDC Number','e'); ?
>:</b
></td
>
206 <input type
='text' size
='40' name
='form_ndc_number' maxlength
='20' value
='<?php echo $row['ndc_number
'] ?>' style
='width:100%' />
211 <td valign
='top' nowrap
><b
><?php
xl('On Order','e'); ?
>:</b
></td
>
213 <input type
='text' size
='5' name
='form_on_order' maxlength
='7' value
='<?php echo $row['on_order
'] ?>' />
218 <td valign
='top' nowrap
><b
><?php
xl('Reorder At','e'); ?
>:</b
></td
>
220 <input type
='text' size
='5' name
='form_reorder_point' maxlength
='7' value
='<?php echo $row['reorder_point
'] ?>' />
225 <td valign
='top' nowrap
><b
><?php
xl('Form','e'); ?
>:</b
></td
>
227 <select name
='form_form'>
229 foreach ($form_array as $key => $value) {
230 echo " <option value='$key'";
231 if ($key == $row['form']) echo " selected";
240 <td valign
='top' nowrap
><b
><?php
xl('Pill Size','e'); ?
>:</b
></td
>
242 <input type
='text' size
='5' name
='form_size' maxlength
='7' value
='<?php echo $row['size
'] ?>' />
247 <td valign
='top' nowrap
><b
><?php
xl('Units','e'); ?
>:</b
></td
>
249 <select name
='form_unit'>
251 foreach ($unit_array as $key => $value) {
252 echo " <option value='$key'";
253 if ($key == $row['unit']) echo " selected";
262 <td valign
='top' nowrap
><b
><?php
xl('Route','e'); ?
>:</b
></td
>
264 <select name
='form_route'>
266 foreach ($route_array as $key => $value) {
267 echo " <option value='$key'";
268 if ($key == $row['route']) echo " selected";
277 <td valign
='top' nowrap
><b
><?php
xl('Templates','e'); ?
>:</b
></td
>
279 <table border
='0' width
='100%'>
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
>
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";
308 while ($trow = sqlFetchArray($tres)) {
309 $selector = $trow['selector'];
310 // Get array of prices.
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, '');
335 <input type
='submit' name
='form_save' value
='<?php xl('Save
','e
'); ?>' />
337 <?php
if (acl_check('admin', 'super')) { ?
>
339 <input type
='submit' name
='form_delete' value
='<?php xl('Delete
','e
'); ?>' style
='color:red' />
343 <input type
='button' value
='<?php xl('Cancel
','e
'); ?>' onclick
='window.close()' />