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");
18 $drug_id = $_REQUEST['drug'];
22 if (!acl_check('admin', 'drugs')) die(xlt('Not authorized'));
24 // Format dollars for display.
26 function bucks($amount) {
28 $amount = sprintf("%.2f", $amount);
29 if ($amount != 0.00) return $amount;
34 // Write a line of data for one template to the form.
36 function writeTemplateLine($selector, $dosage, $period, $quantity, $refills, $prices, $taxrates) {
41 echo " <td class='tmplcell drugsonly'>";
42 echo "<input type='text' name='form_tmpl[$tmpl_line_no][selector]' value='" . attr($selector) . "' size='8' maxlength='100'>";
44 echo " <td class='tmplcell drugsonly'>";
45 echo "<input type='text' name='form_tmpl[$tmpl_line_no][dosage]' value='" . attr($dosage) . "' size='6' maxlength='10'>";
47 echo " <td class='tmplcell drugsonly'>";
48 generate_form_field(array(
50 'field_id' => 'tmpl[' . $tmpl_line_no . '][period]',
51 'list_id' => 'drug_interval',
52 'empty_title' => 'SKIP'
55 echo " <td class='tmplcell drugsonly'>";
56 echo "<input type='text' name='form_tmpl[$tmpl_line_no][quantity]' value='" . attr($quantity) . "' size='3' maxlength='7'>";
58 echo " <td class='tmplcell drugsonly'>";
59 echo "<input type='text' name='form_tmpl[$tmpl_line_no][refills]' value='" . attr($refills) . "' size='3' maxlength='5'>";
61 foreach ($prices as $pricelevel => $price) {
62 echo " <td class='tmplcell'>";
63 echo "<input type='text' name='form_tmpl[$tmpl_line_no][price][" . attr($pricelevel) . "]' value='" . attr($price) . "' size='6' maxlength='12'>";
66 $pres = sqlStatement("SELECT option_id FROM list_options " .
67 "WHERE list_id = 'taxrate' AND activity = 1 ORDER BY seq");
68 while ($prow = sqlFetchArray($pres)) {
69 echo " <td class='tmplcell'>";
70 echo "<input type='checkbox' name='form_tmpl[$tmpl_line_no][taxrate][" . attr($prow['option_id']) . "]' value='1'";
71 if (strpos(":$taxrates", $prow['option_id']) !== false) echo " checked";
77 // Translation for form fields used in SQL queries.
79 function escapedff($name) {
80 return add_escape_custom(trim($_POST[$name]));
82 function numericff($name) {
83 $field = trim($_POST[$name]) +
0;
84 return add_escape_custom($field);
89 <?php
html_header_show(); ?
>
90 <title
><?php
echo $drug_id ?
xlt("Edit") : xlt("Add New"); echo ' ' . xlt('Drug'); ?
></title
>
91 <link rel
="stylesheet" href
='<?php echo $css_header ?>' type
='text/css'>
94 td
{ font
-size
:10pt
; }
96 <?php
if ($GLOBALS['sell_non_drug_products'] == 2) { ?
>
97 .drugsonly
{ display
:none
; }
102 <?php
if (empty($GLOBALS['ippf_specific'])) { ?
>
103 .ippfonly
{ display
:none
; }
110 <script type
="text/javascript" src
="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js"></script
>
111 <script type
="text/javascript" src
="../../library/topdialog.js"></script
>
112 <script type
="text/javascript" src
="../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script
>
113 <script type
="text/javascript" src
="../../library/textformat.js"></script
>
115 <script language
="JavaScript">
117 <?php
require($GLOBALS['srcdir'] . "/restoreSession.php"); ?
>
119 // This is for callback by the find-code popup.
120 // Appends to or erases the current list of related codes.
121 function set_related(codetype
, code
, selector
, codedesc
) {
122 var f
= document
.forms
[0];
123 var s
= f
.form_related_code
.value
;
125 if (s
.length
> 0) s +
= ';';
126 s +
= codetype +
':' + code
;
130 f
.form_related_code
.value
= s
;
133 // This invokes the find-code popup.
134 function sel_related() {
135 dlgopen('../patient_file/encounter/find_code_popup.php', '_blank', 500, 400);
142 <body
class="body_top">
144 // If we are saving, then save and close the window.
145 // First check for duplicates.
147 if ($_POST['form_save']) {
148 $crow = sqlQuery("SELECT COUNT(*) AS count FROM drugs WHERE " .
149 "name = '" . escapedff('form_name') . "' AND " .
150 "form = '" . escapedff('form_form') . "' AND " .
151 "size = '" . escapedff('form_size') . "' AND " .
152 "unit = '" . escapedff('form_unit') . "' AND " .
153 "route = '" . escapedff('form_route') . "' AND " .
154 "drug_id != ?", array($drug_id));
155 if ($crow['count']) {
156 $alertmsg = addslashes(xl('Cannot add this entry because it already exists!'));
160 if (($_POST['form_save'] ||
$_POST['form_delete']) && !$alertmsg) {
163 if ($_POST['form_save']) { // updating an existing drug
164 sqlStatement("UPDATE drugs SET " .
165 "name = '" . escapedff('form_name') . "', " .
166 "ndc_number = '" . escapedff('form_ndc_number') . "', " .
167 "drug_code = '" . escapedff('form_drug_code') . "', " .
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, drug_code, 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_drug_code') . "', " .
202 "'" . escapedff('form_on_order') . "', " .
203 "'" . escapedff('form_reorder_point') . "', " .
204 "'" . escapedff('form_max_level') . "', " .
205 "'" . escapedff('form_form') . "', " .
206 "'" . escapedff('form_size') . "', " .
207 "'" . escapedff('form_unit') . "', " .
208 "'" . escapedff('form_route') . "', " .
209 "'" . numericff('form_cyp_factor') . "', " .
210 "'" . escapedff('form_related_code') . "', " .
211 (empty($_POST['form_allow_multiple' ]) ?
0 : 1) . ", " .
212 (empty($_POST['form_allow_combining']) ?
0 : 1) . ", " .
213 (empty($_POST['form_active']) ?
0 : 1) .
217 if ($_POST['form_save'] && $drug_id) {
218 $tmpl = $_POST['form_tmpl'];
219 // If using the simplified drug form, then force the one and only
220 // selector name to be the same as the product name.
221 if ($GLOBALS['sell_non_drug_products'] == 2) {
222 $tmpl["1"]['selector'] = $_POST['form_name'];
224 sqlStatement("DELETE FROM prices WHERE pr_id = ? AND pr_selector != ''", array($drug_id));
225 for ($lino = 1; isset($tmpl["$lino"]['selector']); ++
$lino) {
226 $iter = $tmpl["$lino"];
227 $selector = trim($iter['selector']);
230 if (!empty($iter['taxrate'])) {
231 foreach ($iter['taxrate'] as $key => $value) {
232 $taxrates .= "$key:";
235 sqlInsert("INSERT INTO drug_templates ( " .
236 "drug_id, selector, dosage, period, quantity, refills, taxrates " .
237 ") VALUES ( ?, ?, ?, ?, ?, ?, ? )",
238 array($drug_id, $selector, trim($iter['dosage']), trim($iter['period']),
239 trim($iter['quantity']), trim($iter['refills']), $taxrates));
241 // Add prices for this drug ID and selector.
242 foreach ($iter['price'] as $key => $value) {
245 sqlStatement("INSERT INTO prices ( " .
246 "pr_id, pr_selector, pr_level, pr_price ) VALUES ( " .
248 array($drug_id, $selector, $key, $value));
250 } // end foreach price
251 } // end if selector is present
252 } // end for each selector
253 // Save warehouse-specific mins and maxes for this drug.
254 sqlStatement("DELETE FROM product_warehouse WHERE pw_drug_id = ?", array($drug_id));
255 foreach ($_POST['form_wh_min'] as $whid => $whmin) {
257 $whmax = 0 +
$_POST['form_wh_max'][$whid];
258 if ($whmin != 0 ||
$whmax != 0) {
259 sqlStatement("INSERT INTO product_warehouse ( " .
260 "pw_drug_id, pw_warehouse, pw_min_level, pw_max_level ) VALUES ( " .
261 "?, ?, ?, ? )", array($drug_id, $whid, $whmin, $whmax));
264 } // end if saving a drug
266 // Close this window and redisplay the updated list of drugs.
268 echo "<script language='JavaScript'>\n";
269 if ($info_msg) echo " alert('$info_msg');\n";
270 echo " if (opener.refreshme) opener.refreshme();\n";
272 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
274 echo " window.close();\n";
276 echo "</script></body></html>\n";
281 $row = sqlQuery("SELECT * FROM drugs WHERE drug_id = ?", array($drug_id));
282 $tres = sqlStatement("SELECT * FROM drug_templates WHERE " .
283 "drug_id = ? ORDER BY selector", array($drug_id));
289 'allow_multiple' => '1',
290 'allow_combining' => '',
293 'reorder_point' => '0',
300 'related_code' => '',
305 <form method
='post' name
='theform' action
='add_edit_drug.php?drug=<?php echo $drug_id; ?>'>
308 <table border
='0' width
='100%'>
311 <td valign
='top' nowrap
><b
><?php
echo xlt('Name'); ?
>:</b
></td
>
313 <input type
='text' size
='40' name
='form_name' maxlength
='80' value
='<?php echo attr($row['name
']) ?>' style
='width:100%' />
318 <td valign
='top' nowrap
><b
><?php
echo xlt('Active'); ?
>:</b
></td
>
320 <input type
='checkbox' name
='form_active' value
='1'<?php
if ($row['active']) echo ' checked'; ?
> />
325 <td valign
='top' nowrap
><b
><?php
echo xlt('Allow'); ?
>:</b
></td
>
327 <input type
='checkbox' name
='form_allow_multiple' value
='1'<?php
if ($row['allow_multiple']) echo ' checked'; ?
> />
328 <?php
echo xlt('Multiple Lots'); ?
>  
;
329 <input type
='checkbox' name
='form_allow_combining' value
='1'<?php
if ($row['allow_combining']) echo ' checked'; ?
> />
330 <?php
echo xlt('Combining Lots'); ?
>
335 <td valign
='top' nowrap
><b
><?php
echo xlt('NDC Number'); ?
>:</b
></td
>
337 <input type
='text' size
='40' name
='form_ndc_number' maxlength
='20'
338 value
='<?php echo attr($row['ndc_number
']) ?>' style
='width:100%'
339 onkeyup
='maskkeyup(this,"<?php echo addslashes($GLOBALS['gbl_mask_product_id
']); ?>")'
340 onblur
='maskblur(this,"<?php echo addslashes($GLOBALS['gbl_mask_product_id
']); ?>")'
345 <td valign
='top' nowrap
><b
><?php
echo xlt('Drug Code'); ?
>:</b
></td
>
347 <input type
='text' size
='5' name
='form_drug_code' maxlength
='10'
348 value
='<?php echo attr($row['drug_code
']) ?>'
353 <td valign
='top' nowrap
><b
><?php
echo xlt('On Order'); ?
>:</b
></td
>
355 <input type
='text' size
='5' name
='form_on_order' maxlength
='7' value
='<?php echo attr($row['on_order
']) ?>' />
360 <td valign
='top' nowrap
><b
><?php
echo xlt('Limits'); ?
>:</b
></td
>
364 <td valign
='top' nowrap
> 
;</td
>
365 <td valign
='top' nowrap
><?php
echo xlt('Global'); ?
></td
>
367 // One column header per warehouse title.
369 $pwres = sqlStatement("SELECT lo.option_id, lo.title, " .
370 "pw.pw_min_level, pw.pw_max_level " .
371 "FROM list_options AS lo " .
372 "LEFT JOIN product_warehouse AS pw ON " .
373 "pw.pw_drug_id = ? AND " .
374 "pw.pw_warehouse = lo.option_id WHERE " .
375 "lo.list_id = 'warehouse' AND lo.activity = 1 ORDER BY lo.seq, lo.title",
377 while ($pwrow = sqlFetchArray($pwres)) {
379 echo " <td valign='top' nowrap>" .
380 text($pwrow['title']) . "</td>\n";
385 <td valign
='top' nowrap
><?php
echo xlt('Min'); ?
> 
;</td
>
387 <input type
='text' size
='5' name
='form_reorder_point' maxlength
='7'
388 value
='<?php echo attr($row['reorder_point
']) ?>'
389 title
='<?php echo xla('Reorder point
, 0 if not applicable
'); ?>'
393 foreach ($pwarr as $pwrow) {
394 echo " <td valign='top'>";
395 echo "<input type='text' name='form_wh_min[" .
396 attr($pwrow['option_id']) .
397 "]' value='" . attr(0 +
$pwrow['pw_min_level']) . "' size='5' " .
398 "title='" . xla('Warehouse minimum, 0 if not applicable') . "' />";
399 echo " </td>\n";
404 <td valign
='top' nowrap
><?php
echo xlt('Max'); ?
> 
;</td
>
406 <input type
='text' size
='5' name
='form_max_level' maxlength
='7'
407 value
='<?php echo attr($row['max_level
']) ?>'
408 title
='<?php echo xla('Maximum reasonable inventory
, 0 if not applicable
'); ?>'
412 foreach ($pwarr as $pwrow) {
413 echo " <td valign='top'>";
414 echo "<input type='text' name='form_wh_max[" .
415 htmlspecialchars($pwrow['option_id']) .
416 "]' value='" . attr(0 +
$pwrow['pw_max_level']) . "' size='5' " .
417 "title='" . xla('Warehouse maximum, 0 if not applicable') . "' />";
426 <tr
class='drugsonly'>
427 <td valign
='top' nowrap
><b
><?php
echo xlt('Form'); ?
>:</b
></td
>
430 generate_form_field(array('data_type'=>1,'field_id'=>'form','list_id'=>'drug_form','empty_title'=>'SKIP'), $row['form']);
435 <tr
class='drugsonly'>
436 <td valign
='top' nowrap
><b
><?php
echo xlt('Pill Size'); ?
>:</b
></td
>
438 <input type
='text' size
='5' name
='form_size' maxlength
='7' value
='<?php echo attr($row['size
']) ?>' />
442 <tr
class='drugsonly'>
443 <td valign
='top' nowrap
><b
><?php
echo xlt('Units'); ?
>:</b
></td
>
446 generate_form_field(array('data_type'=>1,'field_id'=>'unit','list_id'=>'drug_units','empty_title'=>'SKIP'), $row['unit']);
451 <tr
class='drugsonly'>
452 <td valign
='top' nowrap
><b
><?php
echo xlt('Route'); ?
>:</b
></td
>
455 generate_form_field(array('data_type'=>1,'field_id'=>'route','list_id'=>'drug_route','empty_title'=>'SKIP'), $row['route']);
460 <tr
class='ippfonly'>
461 <td valign
='top' nowrap
><b
><?php
echo xlt('CYP Factor'); ?
>:</b
></td
>
463 <input type
='text' size
='10' name
='form_cyp_factor' maxlength
='20' value
='<?php echo attr($row['cyp_factor
']) ?>' />
468 <td valign
='top' nowrap
><b
><?php
echo xlt('Relate To'); ?
>:</b
></td
>
470 <input type
='text' size
='50' name
='form_related_code'
471 value
='<?php echo attr($row['related_code
']) ?>' onclick
='sel_related()'
472 title
='<?php echo xla('Click to select related code
'); ?>'
473 style
='width:100%' readonly
/>
478 <td valign
='top' nowrap
>
479 <b
><?php
echo $GLOBALS['sell_non_drug_products'] == 2 ?
xlt('Fees') : xlt('Templates'); ?
>:</b
>
482 <table border
='0' width
='100%'>
484 <td
class='drugsonly'><b
><?php
echo xlt('Name' ); ?
></b
></td
>
485 <td
class='drugsonly'><b
><?php
echo xlt('Schedule'); ?
></b
></td
>
486 <td
class='drugsonly'><b
><?php
echo xlt('Interval'); ?
></b
></td
>
487 <td
class='drugsonly'><b
><?php
echo xlt('Qty' ); ?
></b
></td
>
488 <td
class='drugsonly'><b
><?php
echo xlt('Refills' ); ?
></b
></td
>
490 // Show a heading for each price level. Also create an array of prices
491 // for new template lines.
492 $emptyPrices = array();
493 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
494 "WHERE list_id = 'pricelevel' AND activity = 1 ORDER BY seq");
495 while ($prow = sqlFetchArray($pres)) {
496 $emptyPrices[$prow['option_id']] = '';
498 generate_display_field(array('data_type'=>'1','list_id'=>'pricelevel'), $prow['option_id']) .
501 // Show a heading for each tax rate.
502 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
503 "WHERE list_id = 'taxrate' AND activity = 1 ORDER BY seq");
504 while ($prow = sqlFetchArray($pres)) {
506 generate_display_field(array('data_type'=>'1','list_id'=>'taxrate'), $prow['option_id']) .
512 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ?
1 : 3;
514 while ($trow = sqlFetchArray($tres)) {
515 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ?
0 : 1;
516 $selector = $trow['selector'];
517 // Get array of prices.
519 $pres = sqlStatement("SELECT lo.option_id, p.pr_price " .
520 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
521 "p.pr_id = ? AND p.pr_selector = ? AND " .
522 "p.pr_level = lo.option_id " .
523 "WHERE lo.list_id = 'pricelevel' AND lo.activity = 1 ORDER BY lo.seq",
524 array($drug_id, $selector));
525 while ($prow = sqlFetchArray($pres)) {
526 $prices[$prow['option_id']] = $prow['pr_price'];
528 writeTemplateLine($selector, $trow['dosage'], $trow['period'],
529 $trow['quantity'], $trow['refills'], $prices, $trow['taxrates']);
532 for ($i = 0; $i < $blank_lines; ++
$i) {
533 $selector = $GLOBALS['sell_non_drug_products'] == 2 ?
$row['name'] : '';
534 writeTemplateLine($selector, '', '', '', '', $emptyPrices, '');
544 <input type
='submit' name
='form_save' value
='<?php echo xla('Save
'); ?>' />
546 <?php
if (acl_check('admin', 'super')) { ?
>
548 <input type
='submit' name
='form_delete' value
='<?php echo xla('Delete
'); ?>' style
='color:red' />
552 <input type
='button' value
='<?php echo xla('Cancel
'); ?>' onclick
='window.close()' />
559 <script language
="JavaScript">
562 echo "alert('" . htmlentities($alertmsg) . "');\n";