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");
14 $drug_id = $_REQUEST['drug'];
18 if (!acl_check('admin', 'drugs')) die("Not authorized!");
20 // Format dollars for display.
22 function bucks($amount) {
24 $amount = sprintf("%.2f", $amount);
25 if ($amount != 0.00) return $amount;
30 // Write a line of data for one template to the form.
32 function writeTemplateLine($selector, $dosage, $period, $quantity, $refills, $prices, $taxrates) {
33 global $tmpl_line_no, $interval_array;
37 echo " <td class='tmplcell drugsonly'>";
38 echo "<input type='text' name='tmpl[$tmpl_line_no][selector]' value='$selector' size='8' maxlength='100'>";
40 echo " <td class='tmplcell drugsonly'>";
41 echo "<input type='text' name='tmpl[$tmpl_line_no][dosage]' value='$dosage' size='6' maxlength='10'>";
43 echo " <td class='tmplcell drugsonly'>";
44 echo "<select name='tmpl[$tmpl_line_no][period]'>";
45 foreach ($interval_array as $key => $value) {
46 echo "<option value='$key'";
47 if ($key == $period) echo " selected";
48 echo ">$value</option>";
51 echo " <td class='tmplcell drugsonly'>";
52 echo "<input type='text' name='tmpl[$tmpl_line_no][quantity]' value='$quantity' size='3' maxlength='7'>";
54 echo " <td class='tmplcell drugsonly'>";
55 echo "<input type='text' name='tmpl[$tmpl_line_no][refills]' value='$refills' size='3' maxlength='5'>";
57 foreach ($prices as $pricelevel => $price) {
58 echo " <td class='tmplcell'>";
59 echo "<input type='text' name='tmpl[$tmpl_line_no][price][$pricelevel]' value='$price' size='6' maxlength='12'>";
62 $pres = sqlStatement("SELECT option_id FROM list_options " .
63 "WHERE list_id = 'taxrate' ORDER BY seq");
64 while ($prow = sqlFetchArray($pres)) {
65 echo " <td class='tmplcell'>";
66 echo "<input type='checkbox' name='tmpl[$tmpl_line_no][taxrate][" . $prow['option_id'] . "]' value='1'";
67 if (strpos(":$taxrates", $prow['option_id']) !== false) echo " checked";
73 // Translation for form fields.
74 function escapedff($name) {
75 $field = trim($_POST[$name]);
76 if (!get_magic_quotes_gpc()) return addslashes($field);
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'>
87 td
{ font
-size
:10pt
; }
89 <?php
if ($GLOBALS['sell_non_drug_products'] == 2) { ?
>
90 .drugsonly
{ display
:none
; }
97 <script type
="text/javascript" src
="../../library/topdialog.js"></script
>
98 <script type
="text/javascript" src
="../../library/dialog.js"></script
>
100 <script language
="JavaScript">
102 <?php
require($GLOBALS['srcdir'] . "/restoreSession.php"); ?
>
104 // This is for callback by the find-code popup.
105 // Appends to or erases the current list of related codes.
106 function set_related(codetype
, code
, selector
, codedesc
) {
107 var f
= document
.forms
[0];
108 var s
= f
.form_related_code
.value
;
110 if (s
.length
> 0) s +
= ';';
111 s +
= codetype +
':' + code
;
115 f
.form_related_code
.value
= s
;
118 // This invokes the find-code popup.
119 function sel_related() {
120 dlgopen('../patient_file/encounter/find_code_popup.php', '_blank', 500, 400);
127 <body
class="body_top">
129 // If we are saving, then save and close the window.
130 // First check for duplicates.
132 if ($_POST['form_save']) {
133 $crow = sqlQuery("SELECT COUNT(*) AS count FROM drugs WHERE " .
134 "name = '" . escapedff('form_name') . "' AND " .
135 "form = '" . escapedff('form_form') . "' AND " .
136 "size = '" . escapedff('form_size') . "' AND " .
137 "unit = '" . escapedff('form_unit') . "' AND " .
138 "route = '" . escapedff('form_route') . "' AND " .
139 "drug_id != '$drug_id'");
140 if ($crow['count']) {
141 $alertmsg = "Cannot add this entry because it already exists!";
145 if (($_POST['form_save'] ||
$_POST['form_delete']) && !$alertmsg) {
148 if ($_POST['form_save']) { // updating an existing drug
149 sqlStatement("UPDATE drugs SET " .
150 "name = '" . escapedff('form_name') . "', " .
151 "ndc_number = '" . escapedff('form_ndc_number') . "', " .
152 "on_order = '" . escapedff('form_on_order') . "', " .
153 "reorder_point = '" . escapedff('form_reorder_point') . "', " .
154 "form = '" . escapedff('form_form') . "', " .
155 "size = '" . escapedff('form_size') . "', " .
156 "unit = '" . escapedff('form_unit') . "', " .
157 "route = '" . escapedff('form_route') . "', " .
158 "related_code = '" . escapedff('form_related_code') . "' " .
159 "WHERE drug_id = '$drug_id'");
160 sqlStatement("DELETE FROM drug_templates WHERE drug_id = '$drug_id'");
163 if (acl_check('admin', 'super')) {
164 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = '$drug_id'");
165 sqlStatement("DELETE FROM drug_templates WHERE drug_id = '$drug_id'");
166 sqlStatement("DELETE FROM drugs WHERE drug_id = '$drug_id'");
167 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
171 else if ($_POST['form_save']) { // saving a new drug
173 $drug_id = sqlInsert("INSERT INTO drugs ( " .
174 "name, ndc_number, on_order, reorder_point, form, " .
175 "size, unit, route, related_code " .
177 "'" . escapedff('form_name') . "', " .
178 "'" . escapedff('form_ndc_number') . "', " .
179 "'" . escapedff('form_on_order') . "', " .
180 "'" . escapedff('form_reorder_point') . "', " .
181 "'" . escapedff('form_form') . "', " .
182 "'" . escapedff('form_size') . "', " .
183 "'" . escapedff('form_unit') . "', " .
184 "'" . escapedff('form_route') . "', " .
185 "'" . escapedff('form_related_code') . "' " .
189 if ($_POST['form_save'] && $drug_id) {
190 $tmpl = $_POST['tmpl'];
191 // If using the simplified drug form, then force the one and only
192 // selector name to be the same as the product name.
193 if ($GLOBALS['sell_non_drug_products'] == 2) {
194 $tmpl["1"]['selector'] = escapedff('form_name');
196 sqlStatement("DELETE FROM prices WHERE pr_id = '$drug_id' AND pr_selector != ''");
197 for ($lino = 1; isset($tmpl["$lino"]['selector']); ++
$lino) {
198 $iter = $tmpl["$lino"];
199 $selector = trim($iter['selector']);
202 if (!empty($iter['taxrate'])) {
203 foreach ($iter['taxrate'] as $key => $value) {
204 $taxrates .= "$key:";
207 sqlInsert("INSERT INTO drug_templates ( " .
208 "drug_id, selector, dosage, period, quantity, refills, taxrates " .
211 "'" . $selector . "', " .
212 "'" . trim($iter['dosage']) . "', " .
213 "'" . trim($iter['period']) . "', " .
214 "'" . trim($iter['quantity']) . "', " .
215 "'" . trim($iter['refills']) . "', " .
216 "'" . $taxrates . "' " .
219 // Add prices for this drug ID and selector.
220 foreach ($iter['price'] as $key => $value) {
223 sqlStatement("INSERT INTO prices ( " .
224 "pr_id, pr_selector, pr_level, pr_price ) VALUES ( " .
225 "'$drug_id', '$selector', '$key', '$value' )");
227 } // end foreach price
228 } // end if selector is present
229 } // end for each selector
230 } // end if saving a drug
232 // Close this window and redisplay the updated list of drugs.
234 echo "<script language='JavaScript'>\n";
235 if ($info_msg) echo " alert('$info_msg');\n";
236 echo " if (opener.refreshme) opener.refreshme();\n";
238 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
240 echo " window.close();\n";
242 echo "</script></body></html>\n";
247 $row = sqlQuery("SELECT * FROM drugs WHERE drug_id = '$drug_id'");
248 $tres = sqlStatement("SELECT * FROM drug_templates WHERE " .
249 "drug_id = '$drug_id' ORDER BY selector");
253 <form method
='post' name
='theform' action
='add_edit_drug.php?drug=<?php echo $drug_id; ?>'>
256 <table border
='0' width
='100%'>
259 <td valign
='top' nowrap
><b
><?php
xl('Name','e'); ?
>:</b
></td
>
261 <input type
='text' size
='40' name
='form_name' maxlength
='80' value
='<?php echo $row['name
'] ?>' style
='width:100%' />
266 <td valign
='top' nowrap
><b
><?php
xl('NDC Number','e'); ?
>:</b
></td
>
268 <input type
='text' size
='40' name
='form_ndc_number' maxlength
='20' value
='<?php echo $row['ndc_number
'] ?>' style
='width:100%' />
273 <td valign
='top' nowrap
><b
><?php
xl('On Order','e'); ?
>:</b
></td
>
275 <input type
='text' size
='5' name
='form_on_order' maxlength
='7' value
='<?php echo $row['on_order
'] ?>' />
280 <td valign
='top' nowrap
><b
><?php
xl('Reorder At','e'); ?
>:</b
></td
>
282 <input type
='text' size
='5' name
='form_reorder_point' maxlength
='7' value
='<?php echo $row['reorder_point
'] ?>' />
286 <tr
class='drugsonly'>
287 <td valign
='top' nowrap
><b
><?php
xl('Form','e'); ?
>:</b
></td
>
289 <select name
='form_form'>
291 foreach ($form_array as $key => $value) {
292 echo " <option value='$key'";
293 if ($key == $row['form']) echo " selected";
301 <tr
class='drugsonly'>
302 <td valign
='top' nowrap
><b
><?php
xl('Pill Size','e'); ?
>:</b
></td
>
304 <input type
='text' size
='5' name
='form_size' maxlength
='7' value
='<?php echo $row['size
'] ?>' />
308 <tr
class='drugsonly'>
309 <td valign
='top' nowrap
><b
><?php
xl('Units','e'); ?
>:</b
></td
>
311 <select name
='form_unit'>
313 foreach ($unit_array as $key => $value) {
314 echo " <option value='$key'";
315 if ($key == $row['unit']) echo " selected";
323 <tr
class='drugsonly'>
324 <td valign
='top' nowrap
><b
><?php
xl('Route','e'); ?
>:</b
></td
>
326 <select name
='form_route'>
328 foreach ($route_array as $key => $value) {
329 echo " <option value='$key'";
330 if ($key == $row['route']) echo " selected";
339 <td valign
='top' nowrap
><b
><?php
xl('Relate To','e'); ?
>:</b
></td
>
341 <input type
='text' size
='50' name
='form_related_code'
342 value
='<?php echo $row['related_code
'] ?>' onclick
='sel_related()'
343 title
='<?php xl('Click to select related code
','e
'); ?>'
344 style
='width:100%' readonly
/>
349 <td valign
='top' nowrap
>
350 <b
><?php
xl($GLOBALS['sell_non_drug_products'] == 2 ?
'Fees' : 'Templates','e'); ?
>:</b
>
353 <table border
='0' width
='100%'>
355 <td
class='drugsonly'><b
><?php
xl('Name' ,'e'); ?
></b
></td
>
356 <td
class='drugsonly'><b
><?php
xl('Schedule','e'); ?
></b
></td
>
357 <td
class='drugsonly'><b
><?php
xl('Interval','e'); ?
></b
></td
>
358 <td
class='drugsonly'><b
><?php
xl('Qty' ,'e'); ?
></b
></td
>
359 <td
class='drugsonly'><b
><?php
xl('Refills' ,'e'); ?
></b
></td
>
361 // Show a heading for each price level. Also create an array of prices
362 // for new template lines.
363 $emptyPrices = array();
364 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
365 "WHERE list_id = 'pricelevel' ORDER BY seq");
366 while ($prow = sqlFetchArray($pres)) {
367 $emptyPrices[$prow['option_id']] = '';
368 echo " <td><b>" . $prow['title'] . "</b></td>\n";
370 // Show a heading for each tax rate.
371 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
372 "WHERE list_id = 'taxrate' ORDER BY seq");
373 while ($prow = sqlFetchArray($pres)) {
374 echo " <td><b>" . $prow['title'] . "</b></td>\n";
379 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ?
1 : 3;
381 while ($trow = sqlFetchArray($tres)) {
382 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ?
0 : 1;
383 $selector = $trow['selector'];
384 // Get array of prices.
386 $pres = sqlStatement("SELECT lo.option_id, p.pr_price " .
387 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
388 "p.pr_id = '$drug_id' AND p.pr_selector = '$selector' AND " .
389 "p.pr_level = lo.option_id " .
390 "WHERE list_id = 'pricelevel' ORDER BY lo.seq");
391 while ($prow = sqlFetchArray($pres)) {
392 $prices[$prow['option_id']] = $prow['pr_price'];
394 writeTemplateLine($selector, $trow['dosage'], $trow['period'],
395 $trow['quantity'], $trow['refills'], $prices, $trow['taxrates']);
398 for ($i = 0; $i < $blank_lines; ++
$i) {
399 $selector = $GLOBALS['sell_non_drug_products'] == 2 ?
$row['name'] : '';
400 writeTemplateLine($selector, '', '', '', '', $emptyPrices, '');
410 <input type
='submit' name
='form_save' value
='<?php xl('Save
','e
'); ?>' />
412 <?php
if (acl_check('admin', 'super')) { ?
>
414 <input type
='submit' name
='form_delete' value
='<?php xl('Delete
','e
'); ?>' style
='color:red' />
418 <input type
='button' value
='<?php xl('Cancel
','e
'); ?>' onclick
='window.close()' />
425 <script language
="JavaScript">
428 echo "alert('" . htmlentities($alertmsg) . "');\n";