Improved Code Sniffing (#928)
[openemr.git] / library / FeeSheetHtml.class.php
blob5da0a73d19b7c3c2fd9c9104498fed4cc1ad438a
1 <?php
2 /**
3 * library/FeeSheetHtml.class.php
5 * Class for HTML-specific implementations of the Fee Sheet.
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see
17 * http://www.gnu.org/licenses/licenses.html#GPL .
19 * @package OpenEMR
20 * @license http://www.gnu.org/licenses/licenses.html#GPL GNU GPL V3+
21 * @author Rod Roark <rod@sunsetsystems.com>
22 * @link http://www.open-emr.org
25 require_once(dirname(__FILE__) . "/FeeSheet.class.php");
26 require_once(dirname(__FILE__) . "/api.inc");
28 class FeeSheetHtml extends FeeSheet {
30 // Dynamically generated JavaScript to maintain justification codes.
31 public $justinit = "var f = document.forms[0];\n";
33 function __construct($pid=0, $encounter=0)
35 parent::__construct($pid, $encounter);
38 // Build a drop-down list of providers. This includes users who
39 // have the word "provider" anywhere in their "additional info"
40 // field, so that we can define providers (for billing purposes)
41 // who do not appear in the calendar.
43 public static function genProviderOptionList($toptext, $default=0)
45 $s = '';
46 // Get user's default facility, or 0 if none.
47 $drow = sqlQuery("SELECT facility_id FROM users where username = '" . $_SESSION['authUser'] . "'");
48 $def_facility = 0 + $drow['facility_id'];
50 $sqlarr = array($def_facility);
51 $query = "SELECT id, lname, fname, facility_id FROM users WHERE " .
52 "( authorized = 1 OR info LIKE '%provider%' ) AND username != '' " .
53 "AND active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' )";
54 // If restricting to providers matching user facility...
55 if ($GLOBALS['gbl_restrict_provider_facility']) {
56 $query .= " AND ( facility_id = 0 OR facility_id = ? )";
57 $query .= " ORDER BY lname, fname";
59 // If not restricting then sort the matching providers first.
60 else {
61 $query .= " ORDER BY (facility_id = ?) DESC, lname, fname";
63 $res = sqlStatement($query, $sqlarr);
64 $s .= "<option value=''>" . text($toptext) . "</option>";
65 while ($row = sqlFetchArray($res)) {
66 $provid = $row['id'];
67 $s .= "<option value='" . attr($provid) . "'";
68 if ($provid == $default) $s .= " selected";
69 $s .= ">";
70 if (!$GLOBALS['gbl_restrict_provider_facility'] && $def_facility && $row['facility_id'] == $def_facility) {
71 // Mark providers in the matching facility with an asterisk.
72 $s .= "* ";
74 $s .= text($row['lname'] . ", " . $row['fname']) . "</option>";
76 return $s;
79 // Does the above but including <select> ... </select>.
81 public static function genProviderSelect($tagname, $toptext, $default=0, $disabled=false)
83 $s = " <select name='" . attr($tagname) . "'";
84 if ($disabled) $s .= " disabled";
85 $s .= ">";
86 $s .= self::genProviderOptionList($toptext, $default);
87 $s .= "</select>\n";
88 return $s;
91 // Build a drop-down list of warehouses.
93 public function genWarehouseSelect($tagname, $toptext, $default='', $disabled=false, $drug_id=0, $is_sold=0)
95 $s = '';
96 if ($this->got_warehouses) {
97 // Normally would use generate_select_list() but it's not flexible enough here.
98 $s .= "<select name='" . attr($tagname) . "'";
99 if (!$disabled) $s .= " onchange='warehouse_changed(this);'";
100 if ($disabled ) $s .= " disabled";
101 $s .= ">";
102 $s .= "<option value=''>" . text($toptext) . "</option>";
103 $lres = sqlStatement("SELECT * FROM list_options " .
104 "WHERE list_id = 'warehouse' AND activity = 1 ORDER BY seq, title");
105 while ($lrow = sqlFetchArray($lres)) {
106 $s .= "<option value='" . attr($lrow['option_id']) . "'";
107 if ($disabled) {
108 if ($lrow['option_id'] == $default) $s .= " selected";
110 else {
111 $has_inventory = sellDrug($drug_id, 1, 0, 0, 0, 0, '', '', $lrow['option_id'], true);
112 if (((strlen($default) == 0 && $lrow['is_default']) ||
113 (strlen($default) > 0 && $lrow['option_id'] == $default)) &&
114 ($is_sold || $has_inventory))
116 $s .= " selected";
118 else {
119 // Disable this warehouse option if not selected and has no inventory.
120 if (!$has_inventory) $s .= " disabled";
123 $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
125 $s .= "</select>";
127 return $s;
130 // Build a drop-down list of price levels.
131 // Includes the specified item's price in the "id" of each option.
133 public function genPriceLevelSelect($tagname, $toptext, $pr_id, $pr_selector='', $default='', $disabled=false)
135 // echo "<!-- pr_id = '$pr_id', pr_selector = '$pr_selector' -->\n"; // debugging
136 $s = "<select name='" . attr($tagname) . "'";
137 if (!$disabled) $s .= " onchange='pricelevel_changed(this);'";
138 if ($disabled ) $s .= " disabled";
139 $s .= ">";
140 $s .= "<option value=''>" . text($toptext) . "</option>";
141 $lres = sqlStatement("SELECT lo.*, p.pr_price " .
142 "FROM list_options AS lo " .
143 "LEFT JOIN prices AS p ON p.pr_id = ? AND p.pr_selector = ? AND p.pr_level = lo.option_id " .
144 "WHERE lo.list_id = 'pricelevel' AND lo.activity = 1 ORDER BY lo.seq, lo.title",
145 array($pr_id, $pr_selector));
146 while ($lrow = sqlFetchArray($lres)) {
147 $price = empty($lrow['pr_price']) ? 0 : $lrow['pr_price'];
148 $s .= "<option value='" . attr($lrow['option_id']) . "'";
149 $s .= " id='prc_$price'";
150 if ((strlen($default) == 0 && $lrow['is_default'] && !$disabled) ||
151 (strlen($default) > 0 && $lrow['option_id'] == $default)
153 $s .= " selected";
155 $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
157 $s .= "</select>";
158 return $s;
161 // If Contraception forms can be auto-created by the Fee Sheet we might need
162 // to ask about the client's prior contraceptive use.
164 public function generateContraceptionSelector($tagname='newmauser')
166 $s = '';
167 if ($GLOBALS['gbl_new_acceptor_policy'] == '1') {
168 $csrow = sqlQuery("SELECT COUNT(*) AS count FROM forms AS f WHERE " .
169 "f.pid = ? AND f.encounter = ? AND " .
170 "f.formdir = 'LBFccicon' AND f.deleted = 0",
171 array($this->pid, $this->encounter));
172 // Do it only if a contraception form does not already exist for this visit.
173 // Otherwise assume that whoever created it knows what they were doing.
174 if ($csrow['count'] == 0) {
175 // Determine if this client ever started contraception with the MA.
176 // Even if only a method change, we assume they have.
177 $query = "SELECT f.form_id FROM forms AS f " .
178 "JOIN form_encounter AS fe ON fe.pid = f.pid AND fe.encounter = f.encounter " .
179 "WHERE f.formdir = 'LBFccicon' AND f.deleted = 0 AND f.pid = ? " .
180 "ORDER BY fe.date DESC LIMIT 1";
181 $csrow = sqlQuery($query, array($this->pid));
182 if (empty($csrow)) {
183 $s .= "<select name='$tagname'>\n";
184 $s .= " <option value='2'>" . xlt('First Modern Contraceptive Use (Lifetime)') . "</option>\n";
185 $s .= " <option value='1'>" . xlt('First Modern Contraception at this Clinic (with Prior Contraceptive Use)') . "</option>\n";
186 $s .= " <option value='0'>" . xlt('Method Change at this Clinic') . "</option>\n";
187 $s .= "</select>\n";
191 return $s;
194 // Generate a price level drop-down defaulting to the patient's current price level.
196 public function generatePriceLevelSelector($tagname='pricelevel', $disabled=false)
198 $s = "<select name='" . attr($tagname) . "'";
199 if ($disabled) $s .= " disabled";
200 $s .= ">";
201 $pricelevel = $this->getPriceLevel();
202 $plres = sqlStatement("SELECT option_id, title FROM list_options " .
203 "WHERE list_id = 'pricelevel' AND activity = 1 ORDER BY seq");
204 while ($plrow = sqlFetchArray($plres)) {
205 $key = $plrow['option_id'];
206 $val = $plrow['title'];
207 $s .= "<option value='" . attr($key) . "'";
208 if ($key == $pricelevel) $s .= ' selected';
209 $s .= ">" . text(xl_list_label($val)) . "</option>";
211 $s .= "</select>";
212 return $s;
215 // Return Javascript that defines a function to validate the line items.
216 // Most of this is currently IPPF-specific, but NDC codes are also validated.
217 // This also computes and sets the form's ippfconmeth value if appropriate.
218 // This does not validate form fields not related to or derived from line items.
219 // Do not call this javascript function if you are just refreshing the form.
220 // The arguments are the names of the form arrays for services and products.
222 public function jsLineItemValidation($bill='bill', $prod='prod')
224 $s = "
225 function jsLineItemValidation(f) {
226 var max_contra_cyp = 0;
227 var max_contra_code = '';
228 var required_code_count = 0;
229 // Loop thru the services.
230 for (var lino = 0; f['{$bill}['+lino+'][code_type]']; ++lino) {
231 var pfx = '{$bill}[' + lino + ']';
232 if (f[pfx + '[del]'] && f[pfx + '[del]'].checked) continue;
233 if (f[pfx + '[ndcnum]'] && f[pfx + '[ndcnum]'].value) {
234 // Check NDC number format.
235 var ndcok = true;
236 var ndc = f[pfx + '[ndcnum]'].value;
237 var a = ndc.split('-');
238 if (a.length != 3) {
239 ndcok = false;
241 else if (a[0].length < 1 || a[1].length < 1 || a[2].length < 1 ||
242 a[0].length > 5 || a[1].length > 4 || a[2].length > 2) {
243 ndcok = false;
245 else {
246 for (var i = 0; i < 3; ++i) {
247 for (var j = 0; j < a[i].length; ++j) {
248 var c = a[i].charAt(j);
249 if (c < '0' || c > '9') ndcok = false;
253 if (!ndcok) {
254 alert('" . xls('Format incorrect for NDC') . "\"' + ndc +
255 '\", " . xls('should be like nnnnn-nnnn-nn') . "');
256 if (f[pfx+'[ndcnum]'].focus) f[pfx+'[ndcnum]'].focus();
257 return false;
259 // Check for valid quantity.
260 var qty = f[pfx+'[ndcqty]'].value - 0;
261 if (isNaN(qty) || qty <= 0) {
262 alert('" . xls('Quantity for NDC') . " \"' + ndc +
263 '\" " . xls('is not valid (decimal fractions are OK).') . "');
264 if (f[pfx+'[ndcqty]'].focus) f[pfx+'[ndcqty]'].focus();
265 return false;
268 if (f[pfx+'[method]'] && f[pfx+'[method]'].value) {
269 // The following applies to contraception for family planning clinics.
270 var tmp_cyp = parseFloat(f[pfx+'[cyp]'].value);
271 var tmp_meth = f[pfx+'[method]'].value;
272 var tmp_methtype = parseInt(f[pfx+'[methtype]'].value);
273 if (tmp_cyp > max_contra_cyp && tmp_methtype == 2) {
274 // max_contra_* tracks max cyp for initial consults only.
275 max_contra_cyp = tmp_cyp;
276 max_contra_code = tmp_meth;
279 if ($this->patient_male) {
280 $s .= "
281 var male_compatible_method = (
282 // TBD: Fix hard coded dependency on IPPFCM codes here.
283 tmp_meth == '4450' || // male condoms
284 tmp_meth == '4570'); // male vasectomy
285 if (!male_compatible_method) {
286 if (!confirm('" . xls('Warning: Contraceptive method is not compatible with a male patient.') . "'))
287 return false;
290 } // end if male patient
291 if ($this->patient_age < 10 || $this->patient_age > 50) {
292 $s .= "
293 if (!confirm('" . xls('Warning: Contraception for a patient under 10 or over 50.') . "'))
294 return false;
296 } // end if improper age
297 if ($this->match_services_to_products) {
298 $s .= "
299 // Nonsurgical methods should normally include a corresponding product.
300 // This takes advantage of the fact that only nonsurgical methods have CYP
301 // less than 10, in both the old and new frameworks.
302 if (tmp_cyp < 10.0) {
303 // Was: if (tmp_meth.substring(0, 2) != '12') {
304 var got_prod = false;
305 for (var plino = 0; f['{$prod}['+plino+'][drug_id]']; ++plino) {
306 var ppfx = '{$prod}[' + plino + ']';
307 if (f[ppfx+'[del]'] && f[ppfx+'[del]'].checked) continue;
308 if (f[ppfx+'[method]'] && f[ppfx+'[method]'].value) {
309 if (f[ppfx+'[method]'].value == tmp_meth) got_prod = true;
312 if (!got_prod) {
313 if (!confirm('" . xls('Warning: There is no product matching the contraceptive service.') . "'))
314 return false;
318 } // end match services to products
319 $s .= "
321 ++required_code_count;
324 if ($this->match_services_to_products) {
325 $s .= "
326 // The following applies to contraception for family planning clinics.
327 // Loop thru the products.
328 for (var lino = 0; f['{$prod}['+lino+'][drug_id]']; ++lino) {
329 var pfx = '{$prod}[' + lino + ']';
330 if (f[pfx + '[del]'] && f[pfx + '[del]'].checked) continue;
331 if (f[pfx + '[method]'] && f[pfx + '[method]'].value) {
332 var tmp_meth = f[pfx + '[method]'].value;
333 // Contraceptive products should normally include a corresponding method.
334 var got_svc = false;
335 for (var slino = 0; f['{$bill}[' + slino + '][code_type]']; ++slino) {
336 var spfx = '{$bill}[' + slino + ']';
337 if (f[spfx + '[del]'] && f[spfx + '[del]'].checked) continue;
338 if (f[spfx + '[method]'] && f[spfx + '[method]'].value) {
339 if (f[spfx + '[method]'].value == tmp_meth) got_svc = true;
342 if (!got_svc) {
343 if (!confirm('" . xls('Warning: There is no service matching the contraceptive product.') . "'))
344 return false;
347 ++required_code_count;
350 } // end match services to products
351 if (isset($GLOBALS['code_types']['MA'])) {
352 $s .= "
353 if (required_code_count == 0) {
354 if (!confirm('" . xls('You have not entered any clinical services or products. Click Cancel to add them. Or click OK if you want to save as-is.') . "')) {
355 return false;
360 $s .= "
361 // End contraception validation.
362 if (f.ippfconmeth) {
363 // Save the primary contraceptive method to its hidden form field.
364 f.ippfconmeth.value = max_contra_code;
366 return true;
369 return $s;