fix new encounter RTL (#1811)
[openemr.git] / library / ajax / code_attributes_ajax.php
blobb138cafb1ec83223d1b776ac3835daff4c87979c
1 <?php
2 /**
3 * Given a code type, code, selector and price level for a service or product, this creates
4 * JavaScript that will call the user's handler passing the following arguments:
5 * code type, code, description, price, warehouse options.
6 * Upload designated service codes as "services=" attributes for designated layouts.
7 * This supports specifying related codes to determine the service codes to be used.
9 * @package OpenEMR
10 * @link http://www.open-emr.org
11 * @author Rod Roark <rod@sunsetsystems.com>
12 * @copyright Copyright (c) 2015-2017 Rod Roark <rod@sunsetsystems.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 require_once("../../interface/globals.php");
17 require_once("$fileroot/custom/code_types.inc.php");
18 require_once("$fileroot/interface/drugs/drugs.inc.php");
20 function write_code_info($codetype, $code, $selector, $pricelevel)
22 global $code_types;
24 $wh = ''; // options for warehouse selection
26 if ($codetype == 'PROD') {
27 $wrow = sqlQuery(
28 "SELECT default_warehouse FROM users WHERE username = ?",
29 array($_SESSION['authUser'])
31 $defaultwh = empty($wrow['default_warehouse']) ? '' : $wrow['default_warehouse'];
33 $crow = sqlQuery(
34 "SELECT d.name, p.pr_price " .
35 "FROM drugs AS d " .
36 "LEFT JOIN prices AS p ON p.pr_id = d.drug_id AND p.pr_selector = ? AND p.pr_level = ? " .
37 "WHERE d.drug_id = ?",
38 array($selector, $pricelevel, $code)
40 $desc = $crow['name'];
41 $price = empty($crow['pr_price']) ? 0 : (0 + $crow['pr_price']);
43 $lres = sqlStatement("SELECT * FROM list_options " .
44 "WHERE list_id = 'warehouse' AND activity = 1 ORDER BY seq, title");
45 $wh .= "<option value=''></option>";
46 while ($lrow = sqlFetchArray($lres)) {
47 $wh .= "<option value='" . attr($lrow['option_id']) . "'";
48 $has_inventory = sellDrug($code, 1, 0, 0, 0, 0, '', '', $lrow['option_id'], true);
49 if ($has_inventory && (
50 (strlen($defaultwh) == 0 && $lrow['is_default'] ) ||
51 (strlen($defaultwh) > 0 && $lrow['option_id'] == $default))) {
52 $wh .= " selected";
53 } else {
54 // Disable this warehouse option if not selected and has no inventory.
55 if (!$has_inventory) {
56 $wh .= " disabled";
59 $wh .= ">" . text(xl_list_label($lrow['title'])) . "</option>";
61 } else {
62 // not PROD
63 $cres = return_code_information($codetype, $code, false);
64 $desc = '';
65 $price = 0;
66 if ($crow = sqlFetchArray($cres)) {
67 $desc = trim($crow['code_text']);
68 if ($code_types[$codetype]['fee']) {
69 if ($code_types[$codetype]['external'] == 0) {
70 $prow = sqlQuery(
71 "SELECT pr_price " .
72 "FROM prices WHERE pr_id = ? AND pr_selector = '' AND pr_level = ? " .
73 "LIMIT 1",
74 array($crow['id'], $pricelevel)
76 if (!empty($prow['pr_price'])) {
77 $price = 0 + $prow['pr_price'];
79 } else {
80 // external code set with fees, prices table not supported
81 $price = 0 + $crow['fee'];
87 // error_log("Warehouse string is: " . $wh); // debugging
89 echo "code_attributes_handler(" .
90 "'" . addslashes($codetype) . "'," .
91 "'" . addslashes($code) . "'," .
92 "'" . addslashes($desc) . "'," .
93 "'" . addslashes($price) . "'," .
94 "'" . addslashes($wh) . "');";
97 $pricelevel = isset($_GET['pricelevel']) ? $_GET['pricelevel'] : '';
99 if (!empty($_GET['list'])) {
100 // This case supports packages of codes.
101 $arrcodes = explode('~', $_GET['list']);
102 foreach ($arrcodes as $codestring) {
103 if ($codestring === '') {
104 continue;
106 $arrcode = explode('|', $codestring);
107 $codetype = $arrcode[0];
108 list($code, $modifier) = explode(":", $arrcode[1]);
109 $selector = isset($arrcode[2]) ? $arrcode[2] : '';
110 write_code_info($codetype, $code, $selector, $pricelevel);
112 } else {
113 // This is the normal case of adding a single code.
114 $codetype = isset($_GET['codetype' ]) ? $_GET['codetype' ] : '';
115 $code = isset($_GET['code' ]) ? $_GET['code' ] : '';
116 $selector = isset($_GET['selector' ]) ? $_GET['selector' ] : '';
117 write_code_info($codetype, $code, $selector, $pricelevel);