Fully responsive globals.php with vertical menu (#2460)
[openemr.git] / library / FeeSheetHtml.class.php
blob723dfa33029ba7a8ca031b773c90f7c915d2ff63
1 <?php
2 /**
3 * library/FeeSheetHtml.class.php
5 * Class for HTML-specific implementations of the Fee Sheet.
7 * @package OpenEMR
8 * @link https://www.open-emr.org
9 * @author Rod Roark <rod@sunsetsystems.com>
10 * @author Brady Miller <brady.g.miller@gmail.com>
11 * @copyright Copyright (c) 2019 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 require_once(dirname(__FILE__) . "/FeeSheet.class.php");
17 require_once(dirname(__FILE__) . "/api.inc");
19 class FeeSheetHtml extends FeeSheet
22 // Dynamically generated JavaScript to maintain justification codes.
23 public $justinit = "var f = document.forms[0];\n";
25 function __construct($pid = 0, $encounter = 0)
27 parent::__construct($pid, $encounter);
30 // Build a drop-down list of providers. This includes users who
31 // have the word "provider" anywhere in their "additional info"
32 // field, so that we can define providers (for billing purposes)
33 // who do not appear in the calendar.
35 public static function genProviderOptionList($toptext, $default = 0)
37 $s = '';
38 // Get user's default facility, or 0 if none.
39 $drow = sqlQuery("SELECT facility_id FROM users where username = ?", [$_SESSION['authUser']]);
40 $def_facility = 0 + $drow['facility_id'];
42 $sqlarr = array($def_facility);
43 $query = "SELECT id, lname, fname, facility_id FROM users WHERE " .
44 "( authorized = 1 OR info LIKE '%provider%' ) AND username != '' " .
45 "AND active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' )";
46 // If restricting to providers matching user facility...
47 if ($GLOBALS['gbl_restrict_provider_facility']) {
48 $query .= " AND ( facility_id = 0 OR facility_id = ? )";
49 $query .= " ORDER BY lname, fname";
50 } // If not restricting then sort the matching providers first.
51 else {
52 $query .= " ORDER BY (facility_id = ?) DESC, lname, fname";
55 $res = sqlStatement($query, $sqlarr);
56 $s .= "<option value=''>" . text($toptext) . "</option>";
57 while ($row = sqlFetchArray($res)) {
58 $provid = $row['id'];
59 $s .= "<option value='" . attr($provid) . "'";
60 if ($provid == $default) {
61 $s .= " selected";
64 $s .= ">";
65 if (!$GLOBALS['gbl_restrict_provider_facility'] && $def_facility && $row['facility_id'] == $def_facility) {
66 // Mark providers in the matching facility with an asterisk.
67 $s .= "* ";
70 $s .= text($row['lname'] . ", " . $row['fname']) . "</option>";
73 return $s;
76 // Does the above but including <select> ... </select>.
78 public static function genProviderSelect($tagname, $toptext, $default = 0, $disabled = false)
80 $s = " <select name='" . attr($tagname) . "'";
81 if ($disabled) {
82 $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) {
100 $s .= " onchange='warehouse_changed(this);'";
103 if ($disabled) {
104 $s .= " disabled";
107 $s .= ">";
108 $s .= "<option value=''>" . text($toptext) . "</option>";
109 $lres = sqlStatement("SELECT * FROM list_options " .
110 "WHERE list_id = 'warehouse' AND activity = 1 ORDER BY seq, title");
111 while ($lrow = sqlFetchArray($lres)) {
112 $s .= "<option value='" . attr($lrow['option_id']) . "'";
113 if ($disabled) {
114 if ($lrow['option_id'] == $default) {
115 $s .= " selected";
117 } else {
118 $has_inventory = sellDrug($drug_id, 1, 0, 0, 0, 0, '', '', $lrow['option_id'], true);
119 if (((strlen($default) == 0 && $lrow['is_default']) ||
120 (strlen($default) > 0 && $lrow['option_id'] == $default)) &&
121 ($is_sold || $has_inventory)) {
122 $s .= " selected";
123 } else {
124 // Disable this warehouse option if not selected and has no inventory.
125 if (!$has_inventory) {
126 $s .= " disabled";
131 $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
134 $s .= "</select>";
137 return $s;
140 // Build a drop-down list of price levels.
141 // Includes the specified item's price in the "id" of each option.
143 public function genPriceLevelSelect($tagname, $toptext, $pr_id, $pr_selector = '', $default = '', $disabled = false)
145 // echo "<!-- pr_id = '$pr_id', pr_selector = '$pr_selector' -->\n"; // debugging
146 $s = "<select name='" . attr($tagname) . "'";
147 if (!$disabled) {
148 $s .= " onchange='pricelevel_changed(this);'";
151 if ($disabled) {
152 $s .= " disabled";
155 $s .= ">";
156 $s .= "<option value=''>" . text($toptext) . "</option>";
157 $lres = sqlStatement(
158 "SELECT lo.*, p.pr_price " .
159 "FROM list_options AS lo " .
160 "LEFT JOIN prices AS p ON p.pr_id = ? AND p.pr_selector = ? AND p.pr_level = lo.option_id " .
161 "WHERE lo.list_id = 'pricelevel' AND lo.activity = 1 ORDER BY lo.seq, lo.title",
162 array($pr_id, $pr_selector)
164 while ($lrow = sqlFetchArray($lres)) {
165 $price = empty($lrow['pr_price']) ? 0 : $lrow['pr_price'];
166 $s .= "<option value='" . attr($lrow['option_id']) . "'";
167 $s .= " id='prc_$price'";
168 if ((strlen($default) == 0 && $lrow['is_default'] && !$disabled) ||
169 (strlen($default) > 0 && $lrow['option_id'] == $default)
171 $s .= " selected";
174 $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
177 $s .= "</select>";
178 return $s;
181 // If Contraception forms can be auto-created by the Fee Sheet we might need
182 // to ask about the client's prior contraceptive use.
184 public function generateContraceptionSelector($tagname = 'newmauser')
186 $s = '';
187 if ($GLOBALS['gbl_new_acceptor_policy'] == '1') {
188 $csrow = sqlQuery(
189 "SELECT COUNT(*) AS count FROM forms AS f WHERE " .
190 "f.pid = ? AND f.encounter = ? AND " .
191 "f.formdir = 'LBFccicon' AND f.deleted = 0",
192 array($this->pid, $this->encounter)
194 // Do it only if a contraception form does not already exist for this visit.
195 // Otherwise assume that whoever created it knows what they were doing.
196 if ($csrow['count'] == 0) {
197 // Determine if this client ever started contraception with the MA.
198 // Even if only a method change, we assume they have.
199 $query = "SELECT f.form_id FROM forms AS f " .
200 "JOIN form_encounter AS fe ON fe.pid = f.pid AND fe.encounter = f.encounter " .
201 "WHERE f.formdir = 'LBFccicon' AND f.deleted = 0 AND f.pid = ? " .
202 "ORDER BY fe.date DESC LIMIT 1";
203 $csrow = sqlQuery($query, array($this->pid));
204 if (empty($csrow)) {
205 $s .= "<select name='$tagname'>\n";
206 $s .= " <option value='2'>" . xlt('First Modern Contraceptive Use (Lifetime)') . "</option>\n";
207 $s .= " <option value='1'>" . xlt('First Modern Contraception at this Clinic (with Prior Contraceptive Use)') . "</option>\n";
208 $s .= " <option value='0'>" . xlt('Method Change at this Clinic') . "</option>\n";
209 $s .= "</select>\n";
214 return $s;
217 // Generate a price level drop-down defaulting to the patient's current price level.
219 public function generatePriceLevelSelector($tagname = 'pricelevel', $disabled = false)
221 $s = "<select name='" . attr($tagname) . "'";
222 if ($disabled) {
223 $s .= " disabled";
226 $s .= ">";
227 $pricelevel = $this->getPriceLevel();
228 $plres = sqlStatement("SELECT option_id, title FROM list_options " .
229 "WHERE list_id = 'pricelevel' AND activity = 1 ORDER BY seq");
230 while ($plrow = sqlFetchArray($plres)) {
231 $key = $plrow['option_id'];
232 $val = $plrow['title'];
233 $s .= "<option value='" . attr($key) . "'";
234 if ($key == $pricelevel) {
235 $s .= ' selected';
238 $s .= ">" . text(xl_list_label($val)) . "</option>";
241 $s .= "</select>";
242 return $s;
245 // Return Javascript that defines a function to validate the line items.
246 // Most of this is currently IPPF-specific, but NDC codes are also validated.
247 // This also computes and sets the form's ippfconmeth value if appropriate.
248 // This does not validate form fields not related to or derived from line items.
249 // Do not call this javascript function if you are just refreshing the form.
250 // The arguments are the names of the form arrays for services and products.
252 public function jsLineItemValidation($bill = 'bill', $prod = 'prod')
254 $s = "
255 function jsLineItemValidation(f) {
256 var max_contra_cyp = 0;
257 var max_contra_code = '';
258 var required_code_count = 0;
259 // Loop thru the services.
260 for (var lino = 0; f['{$bill}['+lino+'][code_type]']; ++lino) {
261 var pfx = '{$bill}[' + lino + ']';
262 if (f[pfx + '[del]'] && f[pfx + '[del]'].checked) continue;
263 if (f[pfx + '[ndcnum]'] && f[pfx + '[ndcnum]'].value) {
264 // Check NDC number format.
265 var ndcok = true;
266 var ndc = f[pfx + '[ndcnum]'].value;
267 var a = ndc.split('-');
268 if (a.length != 3) {
269 ndcok = false;
271 else if (a[0].length < 1 || a[1].length < 1 || a[2].length < 1 ||
272 a[0].length > 5 || a[1].length > 4 || a[2].length > 2) {
273 ndcok = false;
275 else {
276 for (var i = 0; i < 3; ++i) {
277 for (var j = 0; j < a[i].length; ++j) {
278 var c = a[i].charAt(j);
279 if (c < '0' || c > '9') ndcok = false;
283 if (!ndcok) {
284 alert('" . xls('Format incorrect for NDC') . "\"' + ndc +
285 '\", " . xls('should be like nnnnn-nnnn-nn') . "');
286 if (f[pfx+'[ndcnum]'].focus) f[pfx+'[ndcnum]'].focus();
287 return false;
289 // Check for valid quantity.
290 var qty = f[pfx+'[ndcqty]'].value - 0;
291 if (isNaN(qty) || qty <= 0) {
292 alert('" . xls('Quantity for NDC') . " \"' + ndc +
293 '\" " . xls('is not valid (decimal fractions are OK).') . "');
294 if (f[pfx+'[ndcqty]'].focus) f[pfx+'[ndcqty]'].focus();
295 return false;
298 if (f[pfx+'[method]'] && f[pfx+'[method]'].value) {
299 // The following applies to contraception for family planning clinics.
300 var tmp_cyp = parseFloat(f[pfx+'[cyp]'].value);
301 var tmp_meth = f[pfx+'[method]'].value;
302 var tmp_methtype = parseInt(f[pfx+'[methtype]'].value);
303 if (tmp_cyp > max_contra_cyp && tmp_methtype == 2) {
304 // max_contra_* tracks max cyp for initial consults only.
305 max_contra_cyp = tmp_cyp;
306 max_contra_code = tmp_meth;
309 if ($this->patient_male) {
310 $s .= "
311 var male_compatible_method = (
312 // TBD: Fix hard coded dependency on IPPFCM codes here.
313 tmp_meth == '4450' || // male condoms
314 tmp_meth == '4570'); // male vasectomy
315 if (!male_compatible_method) {
316 if (!confirm('" . xls('Warning: Contraceptive method is not compatible with a male patient.') . "'))
317 return false;
320 } // end if male patient
321 if ($this->patient_age < 10 || $this->patient_age > 50) {
322 $s .= "
323 if (!confirm('" . xls('Warning: Contraception for a patient under 10 or over 50.') . "'))
324 return false;
326 } // end if improper age
327 if ($this->match_services_to_products) {
328 $s .= "
329 // Nonsurgical methods should normally include a corresponding product.
330 // This takes advantage of the fact that only nonsurgical methods have CYP
331 // less than 10, in both the old and new frameworks.
332 if (tmp_cyp < 10.0) {
333 // Was: if (tmp_meth.substring(0, 2) != '12') {
334 var got_prod = false;
335 for (var plino = 0; f['{$prod}['+plino+'][drug_id]']; ++plino) {
336 var ppfx = '{$prod}[' + plino + ']';
337 if (f[ppfx+'[del]'] && f[ppfx+'[del]'].checked) continue;
338 if (f[ppfx+'[method]'] && f[ppfx+'[method]'].value) {
339 if (f[ppfx+'[method]'].value == tmp_meth) got_prod = true;
342 if (!got_prod) {
343 if (!confirm('" . xls('Warning: There is no product matching the contraceptive service.') . "'))
344 return false;
348 } // end match services to products
349 $s .= "
351 ++required_code_count;
354 if ($this->match_services_to_products) {
355 $s .= "
356 // The following applies to contraception for family planning clinics.
357 // Loop thru the products.
358 for (var lino = 0; f['{$prod}['+lino+'][drug_id]']; ++lino) {
359 var pfx = '{$prod}[' + lino + ']';
360 if (f[pfx + '[del]'] && f[pfx + '[del]'].checked) continue;
361 if (f[pfx + '[method]'] && f[pfx + '[method]'].value) {
362 var tmp_meth = f[pfx + '[method]'].value;
363 // Contraceptive products should normally include a corresponding method.
364 var got_svc = false;
365 for (var slino = 0; f['{$bill}[' + slino + '][code_type]']; ++slino) {
366 var spfx = '{$bill}[' + slino + ']';
367 if (f[spfx + '[del]'] && f[spfx + '[del]'].checked) continue;
368 if (f[spfx + '[method]'] && f[spfx + '[method]'].value) {
369 if (f[spfx + '[method]'].value == tmp_meth) got_svc = true;
372 if (!got_svc) {
373 if (!confirm('" . xls('Warning: There is no service matching the contraceptive product.') . "'))
374 return false;
377 ++required_code_count;
380 } // end match services to products
381 if (isset($GLOBALS['code_types']['MA'])) {
382 $s .= "
383 if (required_code_count == 0) {
384 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.') . "')) {
385 return false;
391 $s .= "
392 // End contraception validation.
393 if (f.ippfconmeth) {
394 // Save the primary contraceptive method to its hidden form field.
395 f.ippfconmeth.value = max_contra_code;
397 return true;
400 return $s;