2 // Copyright (C) 2006-2011 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 $sanitize_all_escapes = true;
10 $fake_register_globals = false;
12 require_once("../globals.php");
13 require_once("$srcdir/acl.inc");
14 require_once("drugs.inc.php");
15 require_once("$srcdir/options.inc.php");
16 require_once("$srcdir/formdata.inc.php");
17 require_once("$srcdir/htmlspecialchars.inc.php");
20 $drug_id = $_REQUEST['drug'];
24 if (!acl_check('admin', 'drugs')) die(xlt('Not authorized'));
26 // Format dollars for display.
28 function bucks($amount) {
30 $amount = sprintf("%.2f", $amount);
31 if ($amount != 0.00) return $amount;
36 // Write a line of data for one template to the form.
38 function writeTemplateLine($selector, $dosage, $period, $quantity, $refills, $prices, $taxrates) {
43 echo " <td class='tmplcell drugsonly'>";
44 echo "<input type='text' name='form_tmpl[$tmpl_line_no][selector]' value='" . attr($selector) . "' size='8' maxlength='100'>";
46 echo " <td class='tmplcell drugsonly'>";
47 echo "<input type='text' name='form_tmpl[$tmpl_line_no][dosage]' value='" . attr($dosage) . "' size='6' maxlength='10'>";
49 echo " <td class='tmplcell drugsonly'>";
50 generate_form_field(array(
52 'field_id' => 'tmpl[' . $tmpl_line_no . '][period]',
53 'list_id' => 'drug_interval',
54 'empty_title' => 'SKIP'
57 echo " <td class='tmplcell drugsonly'>";
58 echo "<input type='text' name='form_tmpl[$tmpl_line_no][quantity]' value='" . attr($quantity) . "' size='3' maxlength='7'>";
60 echo " <td class='tmplcell drugsonly'>";
61 echo "<input type='text' name='form_tmpl[$tmpl_line_no][refills]' value='" . attr($refills) . "' size='3' maxlength='5'>";
63 foreach ($prices as $pricelevel => $price) {
64 echo " <td class='tmplcell'>";
65 echo "<input type='text' name='form_tmpl[$tmpl_line_no][price][" . attr($pricelevel) . "]' value='" . attr($price) . "' size='6' maxlength='12'>";
68 $pres = sqlStatement("SELECT option_id FROM list_options " .
69 "WHERE list_id = 'taxrate' ORDER BY seq");
70 while ($prow = sqlFetchArray($pres)) {
71 echo " <td class='tmplcell'>";
72 echo "<input type='checkbox' name='form_tmpl[$tmpl_line_no][taxrate][" . attr($prow['option_id']) . "]' value='1'";
73 if (strpos(":$taxrates", $prow['option_id']) !== false) echo " checked";
79 // Translation for form fields used in SQL queries.
81 function escapedff($name) {
82 return add_escape_custom(trim($_POST[$name]));
84 function numericff($name) {
85 $field = trim($_POST[$name]) +
0;
86 return add_escape_custom($field);
91 <?php
html_header_show(); ?
>
92 <title
><?php
echo $drug_id ?
xlt("Edit") : xlt("Add New"); echo ' ' . xlt('Drug'); ?
></title
>
93 <link rel
="stylesheet" href
='<?php echo $css_header ?>' type
='text/css'>
96 td
{ font
-size
:10pt
; }
98 <?php
if ($GLOBALS['sell_non_drug_products'] == 2) { ?
>
99 .drugsonly
{ display
:none
; }
104 <?php
if (empty($GLOBALS['ippf_specific'])) { ?
>
105 .ippfonly
{ display
:none
; }
112 <script type
="text/javascript" src
="../../library/topdialog.js"></script
>
113 <script type
="text/javascript" src
="../../library/dialog.js"></script
>
114 <script type
="text/javascript" src
="../../library/textformat.js"></script
>
116 <script language
="JavaScript">
118 <?php
require($GLOBALS['srcdir'] . "/restoreSession.php"); ?
>
120 // This is for callback by the find-code popup.
121 // Appends to or erases the current list of related codes.
122 function set_related(codetype
, code
, selector
, codedesc
) {
123 var f
= document
.forms
[0];
124 var s
= f
.form_related_code
.value
;
126 if (s
.length
> 0) s +
= ';';
127 s +
= codetype +
':' + code
;
131 f
.form_related_code
.value
= s
;
134 // This invokes the find-code popup.
135 function sel_related() {
136 dlgopen('../patient_file/encounter/find_code_popup.php', '_blank', 500, 400);
143 <body
class="body_top">
145 // If we are saving, then save and close the window.
146 // First check for duplicates.
148 if ($_POST['form_save']) {
149 $crow = sqlQuery("SELECT COUNT(*) AS count FROM drugs WHERE " .
150 "name = '" . escapedff('form_name') . "' AND " .
151 "form = '" . escapedff('form_form') . "' AND " .
152 "size = '" . escapedff('form_size') . "' AND " .
153 "unit = '" . escapedff('form_unit') . "' AND " .
154 "route = '" . escapedff('form_route') . "' AND " .
155 "drug_id != ?", array($drug_id));
156 if ($crow['count']) {
157 $alertmsg = addslashes(xl('Cannot add this entry because it already exists!'));
161 if (($_POST['form_save'] ||
$_POST['form_delete']) && !$alertmsg) {
164 if ($_POST['form_save']) { // updating an existing drug
165 sqlStatement("UPDATE drugs SET " .
166 "name = '" . escapedff('form_name') . "', " .
167 "ndc_number = '" . escapedff('form_ndc_number') . "', " .
168 "on_order = '" . escapedff('form_on_order') . "', " .
169 "reorder_point = '" . escapedff('form_reorder_point') . "', " .
170 "max_level = '" . escapedff('form_max_level') . "', " .
171 "form = '" . escapedff('form_form') . "', " .
172 "size = '" . escapedff('form_size') . "', " .
173 "unit = '" . escapedff('form_unit') . "', " .
174 "route = '" . escapedff('form_route') . "', " .
175 "cyp_factor = '" . numericff('form_cyp_factor') . "', " .
176 "related_code = '" . escapedff('form_related_code') . "', " .
177 "allow_multiple = " . (empty($_POST['form_allow_multiple' ]) ?
0 : 1) . ", " .
178 "allow_combining = " . (empty($_POST['form_allow_combining']) ?
0 : 1) . ", " .
179 "active = " . (empty($_POST['form_active']) ?
0 : 1) . " " .
180 "WHERE drug_id = ?", array($drug_id));
181 sqlStatement("DELETE FROM drug_templates WHERE drug_id = ?", array($drug_id));
184 if (acl_check('admin', 'super')) {
185 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = ?", array($drug_id));
186 sqlStatement("DELETE FROM drug_templates WHERE drug_id = ?", array($drug_id));
187 sqlStatement("DELETE FROM drugs WHERE drug_id = ?", array($drug_id));
188 sqlStatement("DELETE FROM prices WHERE pr_id = ? AND pr_selector != ''", array($drug_id));
192 else if ($_POST['form_save']) { // saving a new drug
194 $drug_id = sqlInsert("INSERT INTO drugs ( " .
195 "name, ndc_number, on_order, reorder_point, max_level, form, " .
196 "size, unit, route, cyp_factor, related_code, " .
197 "allow_multiple, allow_combining, active " .
199 "'" . escapedff('form_name') . "', " .
200 "'" . escapedff('form_ndc_number') . "', " .
201 "'" . escapedff('form_on_order') . "', " .
202 "'" . escapedff('form_reorder_point') . "', " .
203 "'" . escapedff('form_max_level') . "', " .
204 "'" . escapedff('form_form') . "', " .
205 "'" . escapedff('form_size') . "', " .
206 "'" . escapedff('form_unit') . "', " .
207 "'" . escapedff('form_route') . "', " .
208 "'" . numericff('form_cyp_factor') . "', " .
209 "'" . escapedff('form_related_code') . "', " .
210 (empty($_POST['form_allow_multiple' ]) ?
0 : 1) . ", " .
211 (empty($_POST['form_allow_combining']) ?
0 : 1) . ", " .
212 (empty($_POST['form_active']) ?
0 : 1) .
216 if ($_POST['form_save'] && $drug_id) {
217 $tmpl = $_POST['form_tmpl'];
218 // If using the simplified drug form, then force the one and only
219 // selector name to be the same as the product name.
220 if ($GLOBALS['sell_non_drug_products'] == 2) {
221 $tmpl["1"]['selector'] = $_POST['form_name'];
223 sqlStatement("DELETE FROM prices WHERE pr_id = ? AND pr_selector != ''", array($drug_id));
224 for ($lino = 1; isset($tmpl["$lino"]['selector']); ++
$lino) {
225 $iter = $tmpl["$lino"];
226 $selector = trim($iter['selector']);
229 if (!empty($iter['taxrate'])) {
230 foreach ($iter['taxrate'] as $key => $value) {
231 $taxrates .= "$key:";
234 sqlInsert("INSERT INTO drug_templates ( " .
235 "drug_id, selector, dosage, period, quantity, refills, taxrates " .
236 ") VALUES ( ?, ?, ?, ?, ?, ?, ? )",
237 array($drug_id, $selector, trim($iter['dosage']), trim($iter['period']),
238 trim($iter['quantity']), trim($iter['refills']), $taxrates));
240 // Add prices for this drug ID and selector.
241 foreach ($iter['price'] as $key => $value) {
244 sqlStatement("INSERT INTO prices ( " .
245 "pr_id, pr_selector, pr_level, pr_price ) VALUES ( " .
247 array($drug_id, $selector, $key, $value));
249 } // end foreach price
250 } // end if selector is present
251 } // end for each selector
252 // Save warehouse-specific mins and maxes for this drug.
253 sqlStatement("DELETE FROM product_warehouse WHERE pw_drug_id = ?", array($drug_id));
254 foreach ($_POST['form_wh_min'] as $whid => $whmin) {
256 $whmax = 0 +
$_POST['form_wh_max'][$whid];
257 if ($whmin != 0 ||
$whmax != 0) {
258 sqlStatement("INSERT INTO product_warehouse ( " .
259 "pw_drug_id, pw_warehouse, pw_min_level, pw_max_level ) VALUES ( " .
260 "?, ?, ?, ? )", array($drug_id, $whid, $whmin, $whmax));
263 } // end if saving a drug
265 // Close this window and redisplay the updated list of drugs.
267 echo "<script language='JavaScript'>\n";
268 if ($info_msg) echo " alert('$info_msg');\n";
269 echo " if (opener.refreshme) opener.refreshme();\n";
271 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
273 echo " window.close();\n";
275 echo "</script></body></html>\n";
280 $row = sqlQuery("SELECT * FROM drugs WHERE drug_id = ?", array($drug_id));
281 $tres = sqlStatement("SELECT * FROM drug_templates WHERE " .
282 "drug_id = ? ORDER BY selector", array($drug_id));
288 'allow_multiple' => '1',
289 'allow_combining' => '',
292 'reorder_point' => '0',
299 'related_code' => '',
304 <form method
='post' name
='theform' action
='add_edit_drug.php?drug=<?php echo $drug_id; ?>'>
307 <table border
='0' width
='100%'>
310 <td valign
='top' nowrap
><b
><?php
echo xlt('Name'); ?
>:</b
></td
>
312 <input type
='text' size
='40' name
='form_name' maxlength
='80' value
='<?php echo attr($row['name
']) ?>' style
='width:100%' />
317 <td valign
='top' nowrap
><b
><?php
echo xlt('Active'); ?
>:</b
></td
>
319 <input type
='checkbox' name
='form_active' value
='1'<?php
if ($row['active']) echo ' checked'; ?
> />
324 <td valign
='top' nowrap
><b
><?php
echo xlt('Allow'); ?
>:</b
></td
>
326 <input type
='checkbox' name
='form_allow_multiple' value
='1'<?php
if ($row['allow_multiple']) echo ' checked'; ?
> />
327 <?php
echo xlt('Multiple Lots'); ?
>  
;
328 <input type
='checkbox' name
='form_allow_combining' value
='1'<?php
if ($row['allow_combining']) echo ' checked'; ?
> />
329 <?php
echo xlt('Combining Lots'); ?
>
334 <td valign
='top' nowrap
><b
><?php
echo xlt('NDC Number'); ?
>:</b
></td
>
336 <input type
='text' size
='40' name
='form_ndc_number' maxlength
='20'
337 value
='<?php echo attr($row['ndc_number
']) ?>' style
='width:100%'
338 onkeyup
='maskkeyup(this,"<?php echo addslashes($GLOBALS['gbl_mask_product_id
']); ?>")'
339 onblur
='maskblur(this,"<?php echo addslashes($GLOBALS['gbl_mask_product_id
']); ?>")'
345 <td valign
='top' nowrap
><b
><?php
echo xlt('On Order'); ?
>:</b
></td
>
347 <input type
='text' size
='5' name
='form_on_order' maxlength
='7' value
='<?php echo attr($row['on_order
']) ?>' />
352 <td valign
='top' nowrap
><b
><?php
echo xlt('Limits'); ?
>:</b
></td
>
356 <td valign
='top' nowrap
> 
;</td
>
357 <td valign
='top' nowrap
><?php
echo xlt('Global'); ?
></td
>
359 // One column header per warehouse title.
361 $pwres = sqlStatement("SELECT lo.option_id, lo.title, " .
362 "pw.pw_min_level, pw.pw_max_level " .
363 "FROM list_options AS lo " .
364 "LEFT JOIN product_warehouse AS pw ON " .
365 "pw.pw_drug_id = ? AND " .
366 "pw.pw_warehouse = lo.option_id WHERE " .
367 "lo.list_id = 'warehouse' ORDER BY lo.seq, lo.title",
369 while ($pwrow = sqlFetchArray($pwres)) {
371 echo " <td valign='top' nowrap>" .
372 text($pwrow['title']) . "</td>\n";
377 <td valign
='top' nowrap
><?php
echo xlt('Min'); ?
> 
;</td
>
379 <input type
='text' size
='5' name
='form_reorder_point' maxlength
='7'
380 value
='<?php echo attr($row['reorder_point
']) ?>'
381 title
='<?php echo xla('Reorder point
, 0 if not applicable
'); ?>'
385 foreach ($pwarr as $pwrow) {
386 echo " <td valign='top'>";
387 echo "<input type='text' name='form_wh_min[" .
388 attr($pwrow['option_id']) .
389 "]' value='" . attr(0 +
$pwrow['pw_min_level']) . "' size='5' " .
390 "title='" . xla('Warehouse minimum, 0 if not applicable') . "' />";
391 echo " </td>\n";
396 <td valign
='top' nowrap
><?php
echo xlt('Max'); ?
> 
;</td
>
398 <input type
='text' size
='5' name
='form_max_level' maxlength
='7'
399 value
='<?php echo attr($row['max_level
']) ?>'
400 title
='<?php echo xla('Maximum reasonable inventory
, 0 if not applicable
'); ?>'
404 foreach ($pwarr as $pwrow) {
405 echo " <td valign='top'>";
406 echo "<input type='text' name='form_wh_max[" .
407 htmlspecialchars($pwrow['option_id']) .
408 "]' value='" . attr(0 +
$pwrow['pw_max_level']) . "' size='5' " .
409 "title='" . xla('Warehouse maximum, 0 if not applicable') . "' />";
418 <tr
class='drugsonly'>
419 <td valign
='top' nowrap
><b
><?php
echo xlt('Form'); ?
>:</b
></td
>
422 generate_form_field(array('data_type'=>1,'field_id'=>'form','list_id'=>'drug_form','empty_title'=>'SKIP'), $row['form']);
427 <tr
class='drugsonly'>
428 <td valign
='top' nowrap
><b
><?php
echo xlt('Pill Size'); ?
>:</b
></td
>
430 <input type
='text' size
='5' name
='form_size' maxlength
='7' value
='<?php echo attr($row['size
']) ?>' />
434 <tr
class='drugsonly'>
435 <td valign
='top' nowrap
><b
><?php
echo xlt('Units'); ?
>:</b
></td
>
438 generate_form_field(array('data_type'=>1,'field_id'=>'unit','list_id'=>'drug_units','empty_title'=>'SKIP'), $row['unit']);
443 <tr
class='drugsonly'>
444 <td valign
='top' nowrap
><b
><?php
echo xlt('Route'); ?
>:</b
></td
>
447 generate_form_field(array('data_type'=>1,'field_id'=>'route','list_id'=>'drug_route','empty_title'=>'SKIP'), $row['route']);
452 <tr
class='ippfonly'>
453 <td valign
='top' nowrap
><b
><?php
echo xlt('CYP Factor'); ?
>:</b
></td
>
455 <input type
='text' size
='10' name
='form_cyp_factor' maxlength
='20' value
='<?php echo attr($row['cyp_factor
']) ?>' />
460 <td valign
='top' nowrap
><b
><?php
echo xlt('Relate To'); ?
>:</b
></td
>
462 <input type
='text' size
='50' name
='form_related_code'
463 value
='<?php echo attr($row['related_code
']) ?>' onclick
='sel_related()'
464 title
='<?php echo xla('Click to select related code
'); ?>'
465 style
='width:100%' readonly
/>
470 <td valign
='top' nowrap
>
471 <b
><?php
echo $GLOBALS['sell_non_drug_products'] == 2 ?
xlt('Fees') : xlt('Templates'); ?
>:</b
>
474 <table border
='0' width
='100%'>
476 <td
class='drugsonly'><b
><?php
echo xlt('Name' ); ?
></b
></td
>
477 <td
class='drugsonly'><b
><?php
echo xlt('Schedule'); ?
></b
></td
>
478 <td
class='drugsonly'><b
><?php
echo xlt('Interval'); ?
></b
></td
>
479 <td
class='drugsonly'><b
><?php
echo xlt('Qty' ); ?
></b
></td
>
480 <td
class='drugsonly'><b
><?php
echo xlt('Refills' ); ?
></b
></td
>
482 // Show a heading for each price level. Also create an array of prices
483 // for new template lines.
484 $emptyPrices = array();
485 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
486 "WHERE list_id = 'pricelevel' ORDER BY seq");
487 while ($prow = sqlFetchArray($pres)) {
488 $emptyPrices[$prow['option_id']] = '';
490 generate_display_field(array('data_type'=>'1','list_id'=>'pricelevel'), $prow['option_id']) .
493 // Show a heading for each tax rate.
494 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
495 "WHERE list_id = 'taxrate' ORDER BY seq");
496 while ($prow = sqlFetchArray($pres)) {
498 generate_display_field(array('data_type'=>'1','list_id'=>'taxrate'), $prow['option_id']) .
504 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ?
1 : 3;
506 while ($trow = sqlFetchArray($tres)) {
507 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ?
0 : 1;
508 $selector = $trow['selector'];
509 // Get array of prices.
511 $pres = sqlStatement("SELECT lo.option_id, p.pr_price " .
512 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
513 "p.pr_id = ? AND p.pr_selector = ? AND " .
514 "p.pr_level = lo.option_id " .
515 "WHERE list_id = 'pricelevel' ORDER BY lo.seq",
516 array($drug_id, $selector));
517 while ($prow = sqlFetchArray($pres)) {
518 $prices[$prow['option_id']] = $prow['pr_price'];
520 writeTemplateLine($selector, $trow['dosage'], $trow['period'],
521 $trow['quantity'], $trow['refills'], $prices, $trow['taxrates']);
524 for ($i = 0; $i < $blank_lines; ++
$i) {
525 $selector = $GLOBALS['sell_non_drug_products'] == 2 ?
$row['name'] : '';
526 writeTemplateLine($selector, '', '', '', '', $emptyPrices, '');
536 <input type
='submit' name
='form_save' value
='<?php echo xla('Save
'); ?>' />
538 <?php
if (acl_check('admin', 'super')) { ?
>
540 <input type
='submit' name
='form_delete' value
='<?php echo xla('Delete
'); ?>' style
='color:red' />
544 <input type
='button' value
='<?php echo xla('Cancel
'); ?>' onclick
='window.close()' />
551 <script language
="JavaScript">
554 echo "alert('" . htmlentities($alertmsg) . "');\n";