2 // Copyright (C) 2006-2017 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");
12 require_once("$srcdir/options.inc.php");
15 $drug_id = $_REQUEST['drug'];
19 if (!acl_check('admin', 'drugs')) {
20 die(xlt('Not authorized'));
23 // Format dollars for display.
25 function bucks($amount)
28 $amount = sprintf("%.2f", $amount);
29 if ($amount != 0.00) {
37 // Write a line of data for one template to the form.
39 function writeTemplateLine($selector, $dosage, $period, $quantity, $refills, $prices, $taxrates)
45 echo " <td class='tmplcell drugsonly'>";
46 echo "<input type='text' name='form_tmpl[$tmpl_line_no][selector]' value='" . attr($selector) . "' size='8' maxlength='100'>";
48 echo " <td class='tmplcell drugsonly'>";
49 echo "<input type='text' name='form_tmpl[$tmpl_line_no][dosage]' value='" . attr($dosage) . "' size='6' maxlength='10'>";
51 echo " <td class='tmplcell drugsonly'>";
52 generate_form_field(array(
54 'field_id' => 'tmpl[' . $tmpl_line_no . '][period]',
55 'list_id' => 'drug_interval',
56 'empty_title' => 'SKIP'
59 echo " <td class='tmplcell drugsonly'>";
60 echo "<input type='text' name='form_tmpl[$tmpl_line_no][quantity]' value='" . attr($quantity) . "' size='3' maxlength='7'>";
62 echo " <td class='tmplcell drugsonly'>";
63 echo "<input type='text' name='form_tmpl[$tmpl_line_no][refills]' value='" . attr($refills) . "' size='3' maxlength='5'>";
65 foreach ($prices as $pricelevel => $price) {
66 echo " <td class='tmplcell'>";
67 echo "<input type='text' name='form_tmpl[$tmpl_line_no][price][" . attr($pricelevel) . "]' value='" . attr($price) . "' size='6' maxlength='12'>";
71 $pres = sqlStatement("SELECT option_id FROM list_options " .
72 "WHERE list_id = 'taxrate' AND activity = 1 ORDER BY seq");
73 while ($prow = sqlFetchArray($pres)) {
74 echo " <td class='tmplcell'>";
75 echo "<input type='checkbox' name='form_tmpl[$tmpl_line_no][taxrate][" . attr($prow['option_id']) . "]' value='1'";
76 if (strpos(":$taxrates", $prow['option_id']) !== false) {
86 // Translation for form fields used in SQL queries.
88 function escapedff($name)
90 return add_escape_custom(trim($_POST[$name]));
92 function numericff($name)
94 $field = trim($_POST[$name]) +
0;
95 return add_escape_custom($field);
100 <?php
html_header_show(); ?
>
101 <title
><?php
echo $drug_id ?
xlt("Edit") : xlt("Add New");
102 echo ' ' . xlt('Drug'); ?
></title
>
103 <link rel
="stylesheet" href
='<?php echo $css_header ?>' type
='text/css'>
106 td
{ font
-size
:10pt
; }
108 <?php
if ($GLOBALS['sell_non_drug_products'] == 2) { ?
>
109 .drugsonly
{ display
:none
; }
114 <?php
if (empty($GLOBALS['ippf_specific'])) { ?
>
115 .ippfonly
{ display
:none
; }
122 <script type
="text/javascript" src
="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js"></script
>
123 <script type
="text/javascript" src
="../../library/topdialog.js"></script
>
124 <script type
="text/javascript" src
="../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script
>
125 <script type
="text/javascript" src
="../../library/textformat.js"></script
>
127 <script language
="JavaScript">
129 <?php
require($GLOBALS['srcdir'] . "/restoreSession.php"); ?
>
131 // This is for callback by the find-code popup.
132 // Appends to or erases the current list of related codes.
133 function set_related(codetype
, code
, selector
, codedesc
) {
134 var f
= document
.forms
[0];
135 var s
= f
.form_related_code
.value
;
137 if (s
.length
> 0) s +
= ';';
138 s +
= codetype +
':' + code
;
142 f
.form_related_code
.value
= s
;
145 // This is for callback by the find-code popup.
146 // Returns the array of currently selected codes with each element in codetype:code format.
147 function get_related() {
148 return document
.forms
[0].form_related_code
.value
.split(';');
151 // This is for callback by the find-code popup.
152 // Deletes the specified codetype:code from the currently selected list.
153 function del_related(s
) {
154 my_del_related(s
, document
.forms
[0].form_related_code
, false);
157 // This invokes the find-code popup.
158 function sel_related() {
159 dlgopen('../patient_file/encounter/find_code_dynamic.php', '_blank', 900, 600);
166 <body
class="body_top">
168 // If we are saving, then save and close the window.
169 // First check for duplicates.
171 if ($_POST['form_save']) {
172 $crow = sqlQuery("SELECT COUNT(*) AS count FROM drugs WHERE " .
173 "name = '" . escapedff('form_name') . "' AND " .
174 "form = '" . escapedff('form_form') . "' AND " .
175 "size = '" . escapedff('form_size') . "' AND " .
176 "unit = '" . escapedff('form_unit') . "' AND " .
177 "route = '" . escapedff('form_route') . "' AND " .
178 "drug_id != ?", array($drug_id));
179 if ($crow['count']) {
180 $alertmsg = addslashes(xl('Cannot add this entry because it already exists!'));
184 if (($_POST['form_save'] ||
$_POST['form_delete']) && !$alertmsg) {
187 if ($_POST['form_save']) { // updating an existing drug
188 sqlStatement("UPDATE drugs SET " .
189 "name = '" . escapedff('form_name') . "', " .
190 "ndc_number = '" . escapedff('form_ndc_number') . "', " .
191 "drug_code = '" . escapedff('form_drug_code') . "', " .
192 "on_order = '" . escapedff('form_on_order') . "', " .
193 "reorder_point = '" . escapedff('form_reorder_point') . "', " .
194 "max_level = '" . escapedff('form_max_level') . "', " .
195 "form = '" . escapedff('form_form') . "', " .
196 "size = '" . escapedff('form_size') . "', " .
197 "unit = '" . escapedff('form_unit') . "', " .
198 "route = '" . escapedff('form_route') . "', " .
199 "cyp_factor = '" . numericff('form_cyp_factor') . "', " .
200 "related_code = '" . escapedff('form_related_code') . "', " .
201 "allow_multiple = " . (empty($_POST['form_allow_multiple' ]) ?
0 : 1) . ", " .
202 "allow_combining = " . (empty($_POST['form_allow_combining']) ?
0 : 1) . ", " .
203 "active = " . (empty($_POST['form_active']) ?
0 : 1) . " " .
204 "WHERE drug_id = ?", array($drug_id));
205 sqlStatement("DELETE FROM drug_templates WHERE drug_id = ?", array($drug_id));
207 if (acl_check('admin', 'super')) {
208 sqlStatement("DELETE FROM drug_inventory WHERE drug_id = ?", array($drug_id));
209 sqlStatement("DELETE FROM drug_templates WHERE drug_id = ?", array($drug_id));
210 sqlStatement("DELETE FROM drugs WHERE drug_id = ?", array($drug_id));
211 sqlStatement("DELETE FROM prices WHERE pr_id = ? AND pr_selector != ''", array($drug_id));
214 } else if ($_POST['form_save']) { // saving a new drug
216 $drug_id = sqlInsert("INSERT INTO drugs ( " .
217 "name, ndc_number, drug_code, on_order, reorder_point, max_level, form, " .
218 "size, unit, route, cyp_factor, related_code, " .
219 "allow_multiple, allow_combining, active " .
221 "'" . escapedff('form_name') . "', " .
222 "'" . escapedff('form_ndc_number') . "', " .
223 "'" . escapedff('form_drug_code') . "', " .
224 "'" . escapedff('form_on_order') . "', " .
225 "'" . escapedff('form_reorder_point') . "', " .
226 "'" . escapedff('form_max_level') . "', " .
227 "'" . escapedff('form_form') . "', " .
228 "'" . escapedff('form_size') . "', " .
229 "'" . escapedff('form_unit') . "', " .
230 "'" . escapedff('form_route') . "', " .
231 "'" . numericff('form_cyp_factor') . "', " .
232 "'" . escapedff('form_related_code') . "', " .
233 (empty($_POST['form_allow_multiple' ]) ?
0 : 1) . ", " .
234 (empty($_POST['form_allow_combining']) ?
0 : 1) . ", " .
235 (empty($_POST['form_active']) ?
0 : 1) .
239 if ($_POST['form_save'] && $drug_id) {
240 $tmpl = $_POST['form_tmpl'];
241 // If using the simplified drug form, then force the one and only
242 // selector name to be the same as the product name.
243 if ($GLOBALS['sell_non_drug_products'] == 2) {
244 $tmpl["1"]['selector'] = $_POST['form_name'];
247 sqlStatement("DELETE FROM prices WHERE pr_id = ? AND pr_selector != ''", array($drug_id));
248 for ($lino = 1; isset($tmpl["$lino"]['selector']); ++
$lino) {
249 $iter = $tmpl["$lino"];
250 $selector = trim($iter['selector']);
253 if (!empty($iter['taxrate'])) {
254 foreach ($iter['taxrate'] as $key => $value) {
255 $taxrates .= "$key:";
260 "INSERT INTO drug_templates ( " .
261 "drug_id, selector, dosage, period, quantity, refills, taxrates " .
262 ") VALUES ( ?, ?, ?, ?, ?, ?, ? )",
263 array($drug_id, $selector, trim($iter['dosage']), trim($iter['period']),
264 trim($iter['quantity']),
265 trim($iter['refills']),
269 // Add prices for this drug ID and selector.
270 foreach ($iter['price'] as $key => $value) {
274 "INSERT INTO prices ( " .
275 "pr_id, pr_selector, pr_level, pr_price ) VALUES ( " .
277 array($drug_id, $selector, $key, $value)
280 } // end foreach price
281 } // end if selector is present
282 } // end for each selector
283 // Save warehouse-specific mins and maxes for this drug.
284 sqlStatement("DELETE FROM product_warehouse WHERE pw_drug_id = ?", array($drug_id));
285 foreach ($_POST['form_wh_min'] as $whid => $whmin) {
287 $whmax = 0 +
$_POST['form_wh_max'][$whid];
288 if ($whmin != 0 ||
$whmax != 0) {
289 sqlStatement("INSERT INTO product_warehouse ( " .
290 "pw_drug_id, pw_warehouse, pw_min_level, pw_max_level ) VALUES ( " .
291 "?, ?, ?, ? )", array($drug_id, $whid, $whmin, $whmax));
294 } // end if saving a drug
296 // Close this window and redisplay the updated list of drugs.
298 echo "<script language='JavaScript'>\n";
300 echo " alert('$info_msg');\n";
303 echo " if (opener.refreshme) opener.refreshme();\n";
305 echo " window.location.href='add_edit_lot.php?drug=$drug_id&lot=0'\n";
307 echo " window.close();\n";
310 echo "</script></body></html>\n";
315 $row = sqlQuery("SELECT * FROM drugs WHERE drug_id = ?", array($drug_id));
316 $tres = sqlStatement("SELECT * FROM drug_templates WHERE " .
317 "drug_id = ? ORDER BY selector", array($drug_id));
322 'allow_multiple' => '1',
323 'allow_combining' => '',
326 'reorder_point' => '0',
333 'related_code' => '',
338 <form method
='post' name
='theform' action
='add_edit_drug.php?drug=<?php echo $drug_id; ?>'>
341 <table border
='0' width
='100%'>
344 <td valign
='top' nowrap
><b
><?php
echo xlt('Name'); ?
>:</b
></td
>
346 <input type
='text' size
='40' name
='form_name' maxlength
='80' value
='<?php echo attr($row['name
']) ?>' style
='width:100%' />
351 <td valign
='top' nowrap
><b
><?php
echo xlt('Active'); ?
>:</b
></td
>
353 <input type
='checkbox' name
='form_active' value
='1'<?php
354 if ($row['active']) {
361 <td valign
='top' nowrap
><b
><?php
echo xlt('Allow'); ?
>:</b
></td
>
363 <input type
='checkbox' name
='form_allow_multiple' value
='1'<?php
364 if ($row['allow_multiple']) {
367 <?php
echo xlt('Multiple Lots'); ?
>  
;
368 <input type
='checkbox' name
='form_allow_combining' value
='1'<?php
369 if ($row['allow_combining']) {
372 <?php
echo xlt('Combining Lots'); ?
>
377 <td valign
='top' nowrap
><b
><?php
echo xlt('NDC Number'); ?
>:</b
></td
>
379 <input type
='text' size
='40' name
='form_ndc_number' maxlength
='20'
380 value
='<?php echo attr($row['ndc_number
']) ?>' style
='width:100%'
381 onkeyup
='maskkeyup(this,"<?php echo addslashes($GLOBALS['gbl_mask_product_id
']); ?>")'
382 onblur
='maskblur(this,"<?php echo addslashes($GLOBALS['gbl_mask_product_id
']); ?>")'
387 <td valign
='top' nowrap
><b
><?php
echo xlt('Drug Code'); ?
>:</b
></td
>
389 <input type
='text' size
='5' name
='form_drug_code' maxlength
='10'
390 value
='<?php echo attr($row['drug_code
']) ?>'
395 <td valign
='top' nowrap
><b
><?php
echo xlt('On Order'); ?
>:</b
></td
>
397 <input type
='text' size
='5' name
='form_on_order' maxlength
='7' value
='<?php echo attr($row['on_order
']) ?>' />
402 <td valign
='top' nowrap
><b
><?php
echo xlt('Limits'); ?
>:</b
></td
>
406 <td valign
='top' nowrap
> 
;</td
>
407 <td valign
='top' nowrap
><?php
echo xlt('Global'); ?
></td
>
409 // One column header per warehouse title.
411 $pwres = sqlStatement(
412 "SELECT lo.option_id, lo.title, " .
413 "pw.pw_min_level, pw.pw_max_level " .
414 "FROM list_options AS lo " .
415 "LEFT JOIN product_warehouse AS pw ON " .
416 "pw.pw_drug_id = ? AND " .
417 "pw.pw_warehouse = lo.option_id WHERE " .
418 "lo.list_id = 'warehouse' AND lo.activity = 1 ORDER BY lo.seq, lo.title",
421 while ($pwrow = sqlFetchArray($pwres)) {
423 echo " <td valign='top' nowrap>" .
424 text($pwrow['title']) . "</td>\n";
429 <td valign
='top' nowrap
><?php
echo xlt('Min'); ?
> 
;</td
>
431 <input type
='text' size
='5' name
='form_reorder_point' maxlength
='7'
432 value
='<?php echo attr($row['reorder_point
']) ?>'
433 title
='<?php echo xla('Reorder point
, 0 if not applicable
'); ?>'
437 foreach ($pwarr as $pwrow) {
438 echo " <td valign='top'>";
439 echo "<input type='text' name='form_wh_min[" .
440 attr($pwrow['option_id']) .
441 "]' value='" . attr(0 +
$pwrow['pw_min_level']) . "' size='5' " .
442 "title='" . xla('Warehouse minimum, 0 if not applicable') . "' />";
443 echo " </td>\n";
448 <td valign
='top' nowrap
><?php
echo xlt('Max'); ?
> 
;</td
>
450 <input type
='text' size
='5' name
='form_max_level' maxlength
='7'
451 value
='<?php echo attr($row['max_level
']) ?>'
452 title
='<?php echo xla('Maximum reasonable inventory
, 0 if not applicable
'); ?>'
456 foreach ($pwarr as $pwrow) {
457 echo " <td valign='top'>";
458 echo "<input type='text' name='form_wh_max[" .
459 htmlspecialchars($pwrow['option_id']) .
460 "]' value='" . attr(0 +
$pwrow['pw_max_level']) . "' size='5' " .
461 "title='" . xla('Warehouse maximum, 0 if not applicable') . "' />";
470 <tr
class='drugsonly'>
471 <td valign
='top' nowrap
><b
><?php
echo xlt('Form'); ?
>:</b
></td
>
474 generate_form_field(array('data_type'=>1,'field_id'=>'form','list_id'=>'drug_form','empty_title'=>'SKIP'), $row['form']);
479 <tr
class='drugsonly'>
480 <td valign
='top' nowrap
><b
><?php
echo xlt('Pill Size'); ?
>:</b
></td
>
482 <input type
='text' size
='5' name
='form_size' maxlength
='7' value
='<?php echo attr($row['size
']) ?>' />
486 <tr
class='drugsonly'>
487 <td valign
='top' nowrap
><b
><?php
echo xlt('Units'); ?
>:</b
></td
>
490 generate_form_field(array('data_type'=>1,'field_id'=>'unit','list_id'=>'drug_units','empty_title'=>'SKIP'), $row['unit']);
495 <tr
class='drugsonly'>
496 <td valign
='top' nowrap
><b
><?php
echo xlt('Route'); ?
>:</b
></td
>
499 generate_form_field(array('data_type'=>1,'field_id'=>'route','list_id'=>'drug_route','empty_title'=>'SKIP'), $row['route']);
504 <tr
class='ippfonly'>
505 <td valign
='top' nowrap
><b
><?php
echo xlt('CYP Factor'); ?
>:</b
></td
>
507 <input type
='text' size
='10' name
='form_cyp_factor' maxlength
='20' value
='<?php echo attr($row['cyp_factor
']) ?>' />
512 <td valign
='top' nowrap
><b
><?php
echo xlt('Relate To'); ?
>:</b
></td
>
514 <input type
='text' size
='50' name
='form_related_code'
515 value
='<?php echo attr($row['related_code
']) ?>' onclick
='sel_related()'
516 title
='<?php echo xla('Click to select related code
'); ?>'
517 style
='width:100%' readonly
/>
522 <td valign
='top' nowrap
>
523 <b
><?php
echo $GLOBALS['sell_non_drug_products'] == 2 ?
xlt('Fees') : xlt('Templates'); ?
>:</b
>
526 <table border
='0' width
='100%'>
528 <td
class='drugsonly'><b
><?php
echo xlt('Name'); ?
></b
></td
>
529 <td
class='drugsonly'><b
><?php
echo xlt('Schedule'); ?
></b
></td
>
530 <td
class='drugsonly'><b
><?php
echo xlt('Interval'); ?
></b
></td
>
531 <td
class='drugsonly'><b
><?php
echo xlt('Qty'); ?
></b
></td
>
532 <td
class='drugsonly'><b
><?php
echo xlt('Refills'); ?
></b
></td
>
534 // Show a heading for each price level. Also create an array of prices
535 // for new template lines.
536 $emptyPrices = array();
537 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
538 "WHERE list_id = 'pricelevel' AND activity = 1 ORDER BY seq");
539 while ($prow = sqlFetchArray($pres)) {
540 $emptyPrices[$prow['option_id']] = '';
542 generate_display_field(array('data_type'=>'1','list_id'=>'pricelevel'), $prow['option_id']) .
546 // Show a heading for each tax rate.
547 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
548 "WHERE list_id = 'taxrate' AND activity = 1 ORDER BY seq");
549 while ($prow = sqlFetchArray($pres)) {
551 generate_display_field(array('data_type'=>'1','list_id'=>'taxrate'), $prow['option_id']) .
557 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ?
1 : 3;
559 while ($trow = sqlFetchArray($tres)) {
560 $blank_lines = $GLOBALS['sell_non_drug_products'] == 2 ?
0 : 1;
561 $selector = $trow['selector'];
562 // Get array of prices.
564 $pres = sqlStatement(
565 "SELECT lo.option_id, p.pr_price " .
566 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
567 "p.pr_id = ? AND p.pr_selector = ? AND " .
568 "p.pr_level = lo.option_id " .
569 "WHERE lo.list_id = 'pricelevel' AND lo.activity = 1 ORDER BY lo.seq",
570 array($drug_id, $selector)
572 while ($prow = sqlFetchArray($pres)) {
573 $prices[$prow['option_id']] = $prow['pr_price'];
588 for ($i = 0; $i < $blank_lines; ++
$i) {
589 $selector = $GLOBALS['sell_non_drug_products'] == 2 ?
$row['name'] : '';
590 writeTemplateLine($selector, '', '', '', '', $emptyPrices, '');
600 <input type
='submit' name
='form_save' value
='<?php echo xla('Save
'); ?>' />
602 <?php
if (acl_check('admin', 'super')) { ?
>
604 <input type
='submit' name
='form_delete' value
='<?php echo xla('Delete
'); ?>' style
='color:red' />
608 <input type
='button' value
='<?php echo xla('Cancel
'); ?>' onclick
='window.close()' />
615 <script language
="JavaScript">
618 echo "alert('" . htmlentities($alertmsg) . "');\n";