Fixes #7497 datetimepicker invalid date format (#7499)
[openemr.git] / library / FeeSheetHtml.class.php
blob9f38c05cc2e7364240ba89a948903cc73ac45b39
1 <?php
3 /**
4 * library/FeeSheetHtml.class.php
6 * Class for HTML-specific implementations of the Fee Sheet.
8 * @package OpenEMR
9 * @link https://www.open-emr.org
10 * @author Rod Roark <rod@sunsetsystems.com>
11 * @author Brady Miller <brady.g.miller@gmail.com>
12 * @copyright Copyright (c) 2019 Brady Miller <brady.g.miller@gmail.com>
13 * @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.php");
19 class FeeSheetHtml extends FeeSheet
21 // Dynamically generated JavaScript to maintain justification codes.
22 public $justinit = "var f = document.forms[0];\n";
24 function __construct($pid = 0, $encounter = 0)
26 parent::__construct($pid, $encounter);
29 // Build a drop-down list of providers. This includes users who
30 // have the word "provider" anywhere in their "additional info"
31 // field, so that we can define providers (for billing purposes)
32 // who do not appear in the calendar.
34 public static function genProviderOptionList($toptext, $default = 0, $inactive = false)
36 $s = '';
37 // Get user's default facility, or 0 if none.
38 $drow = sqlQuery("SELECT facility_id FROM users where username = ?", [$_SESSION['authUser']]);
39 $def_facility = 0 + $drow['facility_id'];
41 $sqlarr = array($def_facility);
42 $query = "SELECT id, lname, fname, facility_id FROM users WHERE " .
43 "( authorized = 1 OR info LIKE '%provider%' ) AND username != '' ";
44 if (!$GLOBALS['include_inactive_providers']) {
45 $query .= " AND active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' )";
47 // If restricting to providers matching user facility...
48 if (!empty($GLOBALS['gbl_restrict_provider_facility'])) {
49 $query .= " AND ( facility_id = 0 OR facility_id = ? )";
50 $query .= " ORDER BY lname, fname";
51 } else { // If not restricting then sort the matching providers first.
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 (empty($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, $tooltip = '')
80 $s = " <span><select class='form-control' name='" . attr($tagname) . "'";
81 if ($disabled) {
82 $s .= " disabled";
84 if ($tooltip) {
85 $s .= " title='" . attr($tooltip) . "'";
87 $s .= ">";
88 $s .= self::genProviderOptionList($toptext, $default);
89 $s .= "</select></span>\n";
90 return $s;
93 // Build a drop-down list of warehouses.
95 public function genWarehouseSelect($tagname, $toptext, $default = '', $disabled = false, $drug_id = 0, $is_sold = 0)
97 $s = '';
98 if ($this->got_warehouses) {
99 // Normally would use generate_select_list() but it's not flexible enough here.
100 $s .= "<span class='form-inline'><select class='form-control' name='" . attr($tagname) . "'";
101 if (!$disabled) {
102 $s .= " onchange='warehouse_changed(this);'";
105 if ($disabled) {
106 $s .= " disabled";
109 $s .= ">";
110 $s .= "<option value=''>" . text($toptext) . "</option>";
111 $lres = sqlStatement("SELECT * FROM list_options " .
112 "WHERE list_id = 'warehouse' AND activity = 1 ORDER BY seq, title");
113 while ($lrow = sqlFetchArray($lres)) {
114 $s .= "<option value='" . attr($lrow['option_id']) . "'";
115 if ($disabled) {
116 if ($lrow['option_id'] == $default) {
117 $s .= " selected";
119 } else {
120 $allowed = true;
121 if (isUserRestricted()) {
122 // Check for permission to use this warehouse.
123 if (!isWarehouseAllowed($lrow['option_value'], $lrow['option_id'])) {
124 $allowed = false;
127 if ($allowed) {
128 // OK got permission, check to see if inventory is there.
129 $allowed = sellDrug($drug_id, 1, 0, 0, 0, 0, '', '', $lrow['option_id'], true);
131 if (
132 ((strlen($default) == 0 && $lrow['is_default']) ||
133 (strlen($default) > 0 && $lrow['option_id'] == $default)) &&
134 ($is_sold || $allowed)
136 $s .= " selected";
137 } else {
138 // Hide this warehouse option if not selected and has no permission or no inventory.
139 if (!$allowed) {
140 $s .= " disabled style='display:none'";
145 $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
148 $s .= "</select></span>";
151 return $s;
154 // Build a drop-down list of price levels.
155 // Includes the specified item's price in the "id" of each option.
157 public function genPriceLevelSelect($tagname, $toptext, $pr_id, $pr_selector = '', $default = '', $disabled = false)
159 // echo "<!-- pr_id = '$pr_id', pr_selector = '$pr_selector' -->\n"; // debugging
160 $s = "<span class='form-inline'><select class='form-control' name='" . attr($tagname) . "'";
161 if (!$disabled) {
162 $s .= " onchange='pricelevel_changed(this);'";
165 if ($disabled) {
166 $s .= " disabled";
169 $s .= ">";
170 $s .= "<option value=''>" . text($toptext) . "</option>";
171 $lres = sqlStatement(
172 "SELECT lo.*, p.pr_price " .
173 "FROM list_options AS lo " .
174 "LEFT JOIN prices AS p ON p.pr_id = ? AND p.pr_selector = ? AND p.pr_level = lo.option_id " .
175 "WHERE lo.list_id = 'pricelevel' AND lo.activity = 1 ORDER BY lo.seq, lo.title",
176 array($pr_id, $pr_selector)
178 $standardPrice = 0;
179 while ($lrow = sqlFetchArray($lres)) {
180 $price = empty($lrow['pr_price']) ? 0 : $lrow['pr_price'];
181 // if percent-based pricing is enabled...
182 if ($GLOBALS['enable_percent_pricing']) {
183 // Set standardPrice as the first price level (sorted by seq)
184 if ($standardPrice === 0) {
185 $standardPrice = $price;
187 // If price level notes contains a percentage,
188 // calculate price as percentage of standard price
189 $notes = $lrow['notes'];
190 if (!empty($notes) && strpos($notes, '%') > -1) {
191 $percent = intval(str_replace('%', '', $notes));
192 if ($percent > 0) {
193 $price = $standardPrice * ((100 - $percent) / 100);
197 if ($price != 0 && !$this->pricesAuthorized()) {
198 // This user is not authorized to see nonzero prices.
199 $price = 'X';
201 $s .= "<option value='" . attr($lrow['option_id']) . "'";
202 $s .= " id='prc_$price'";
203 if (
204 (strlen($default) == 0 && $lrow['is_default'] && !$disabled) ||
205 (strlen($default) > 0 && $lrow['option_id'] == $default)
207 $s .= " selected";
210 $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
213 $s .= "</select></span>";
214 return $s;
217 // If Contraception forms can be auto-created by the Fee Sheet we might need
218 // to ask about the client's prior contraceptive use.
220 public function generateContraceptionSelector($tagname = 'newmauser')
222 $s = '';
223 if ($GLOBALS['gbl_new_acceptor_policy'] == '1') {
226 /**********************************************************
227 $csrow = sqlQuery(
228 "SELECT COUNT(*) AS count FROM forms AS f WHERE " .
229 "f.pid = ? AND f.encounter = ? AND " .
230 "f.formdir = 'LBFccicon' AND f.deleted = 0",
231 array($this->pid, $this->encounter)
233 // Do it only if a contraception form does not already exist for this visit.
234 // Otherwise assume that whoever created it knows what they were doing.
235 if ($csrow['count'] == 0) {
236 // Determine if this client ever started contraception with the MA.
237 // Even if only a method change, we assume they have.
238 $query = "SELECT f.form_id FROM forms AS f " .
239 "JOIN form_encounter AS fe ON fe.pid = f.pid AND fe.encounter = f.encounter " .
240 "WHERE f.formdir = 'LBFccicon' AND f.deleted = 0 AND f.pid = ? " .
241 "ORDER BY fe.date DESC LIMIT 1";
242 $csrow = sqlQuery($query, array($this->pid));
243 if (empty($csrow)) {
244 $s .= "<span class='form-inline'><select class='form-control' name='$tagname'>\n";
245 $s .= " <option value='2'>" . xlt('First Modern Contraceptive Use (Lifetime)') . "</option>\n";
246 $s .= " <option value='1'>" . xlt('First Modern Contraception at this Clinic (with Prior Contraceptive Use)') . "</option>\n";
247 $s .= " <option value='0'>" . xlt('Method Change at this Clinic') . "</option>\n";
248 $s .= "</select></span>\n";
251 **********************************************************/
252 $csrow = sqlQuery(
253 "SELECT COUNT(*) AS count FROM shared_attributes WHERE " .
254 "pid = ? AND field_id = 'cgen_newmauser'",
255 array($this->pid)
257 if ($csrow['count'] == 0) {
258 $s .= "<span class='form-inline'><select class='form-control' name='" . attr($tagname) . "'>\n";
259 $s .= " <option value='2'>" . xlt('First Modern Contraceptive Use (Lifetime)') . "</option>\n";
260 $s .= " <option value='1'>" . xlt('First Modern Contraception at this Clinic (with Prior Contraceptive Use)') . "</option>\n";
261 $s .= " <option value='0'>" . xlt('Method Change at this Clinic') . "</option>\n";
262 $s .= "</select></span>\n";
266 return $s;
269 // Generate a price level drop-down defaulting to the patient's current price level.
271 public function generatePriceLevelSelector($tagname = 'pricelevel', $disabled = false)
273 $s = "<span class='form-inline'><select class='form-control' name='" . attr($tagname) . "'";
274 if ($disabled) {
275 $s .= " disabled";
278 $s .= ">";
279 $pricelevel = $this->getPriceLevel();
280 $plres = sqlStatement("SELECT option_id, title FROM list_options " .
281 "WHERE list_id = 'pricelevel' AND activity = 1 ORDER BY seq");
282 while ($plrow = sqlFetchArray($plres)) {
283 $key = $plrow['option_id'];
284 $val = $plrow['title'];
285 $s .= "<option value='" . attr($key) . "'";
286 if ($key == $pricelevel) {
287 $s .= ' selected';
290 $s .= ">" . text(xl_list_label($val)) . "</option>";
293 $s .= "</select></span>";
294 return $s;
297 // Return Javascript that defines a function to validate the line items.
298 // Most of this is currently IPPF-specific, but NDC codes are also validated.
299 // This also computes and sets the form's ippfconmeth value if appropriate.
300 // This does not validate form fields not related to or derived from line items.
301 // Do not call this javascript function if you are just refreshing the form.
302 // The arguments are the names of the form arrays for services and products.
304 public function jsLineItemValidation($bill = 'bill', $prod = 'prod')
306 $s = "
307 function jsLineItemValidation(f) {
308 var max_contra_cyp = 0;
309 var max_contra_code = '';
310 var required_code_count = 0;
311 // Loop thru the services.
312 for (var lino = 0; f['{$bill}['+lino+'][code_type]']; ++lino) {
313 var pfx = '{$bill}[' + lino + ']';
314 if (f[pfx + '[del]'] && f[pfx + '[del]'].checked) continue;
315 if (f[pfx + '[ndcnum]'] && f[pfx + '[ndcnum]'].value) {
316 // Check NDC number format.
317 var ndcok = true;
318 var ndc = f[pfx + '[ndcnum]'].value;
319 var a = ndc.split('-');
320 if (a.length != 3) {
321 ndcok = false;
323 else if (a[0].length < 1 || a[1].length < 1 || a[2].length < 1 ||
324 a[0].length > 5 || a[1].length > 4 || a[2].length > 2) {
325 ndcok = false;
327 else {
328 for (var i = 0; i < 3; ++i) {
329 for (var j = 0; j < a[i].length; ++j) {
330 var c = a[i].charAt(j);
331 if (c < '0' || c > '9') ndcok = false;
335 if (!ndcok) {
336 alert('" . xls('Format incorrect for NDC') . "\"' + ndc +
337 '\", " . xls('should be like nnnnn-nnnn-nn') . "');
338 if (f[pfx+'[ndcnum]'].focus) f[pfx+'[ndcnum]'].focus();
339 return false;
341 // Check for valid quantity.
342 var qty = f[pfx+'[ndcqty]'].value - 0;
343 if (isNaN(qty) || qty <= 0) {
344 alert('" . xls('Quantity for NDC') . " \"' + ndc +
345 '\" " . xls('is not valid (decimal fractions are OK).') . "');
346 if (f[pfx+'[ndcqty]'].focus) f[pfx+'[ndcqty]'].focus();
347 return false;
350 if (f[pfx+'[method]'] && f[pfx+'[method]'].value) {
351 // The following applies to contraception for family planning clinics.
352 var tmp_cyp = parseFloat(f[pfx+'[cyp]'].value);
353 var tmp_meth = f[pfx+'[method]'].value;
354 var tmp_methtype = parseInt(f[pfx+'[methtype]'].value);
355 if (tmp_cyp > max_contra_cyp && tmp_methtype == 2) {
356 // max_contra_* tracks max cyp for initial consults only.
357 max_contra_cyp = tmp_cyp;
358 max_contra_code = tmp_meth;
361 if ($this->patient_male) {
362 $s .= "
363 var male_compatible_method = (
364 // TBD: Fix hard coded dependency on IPPFCM codes here.
365 tmp_meth == '4450' || // male condoms
366 tmp_meth == '4570'); // male vasectomy
367 if (!male_compatible_method) {
368 if (!confirm('" . xls('Warning: Contraceptive method is not compatible with a male patient.') . "'))
369 return false;
372 } // end if male patient
373 if ($this->patient_age < 10 || $this->patient_age > 65) {
374 $s .= "
375 if (!confirm(" . xlj('Warning: Contraception for a patient under 10 or over 65.') . "))
376 return false;
378 } // end if improper age
379 if ($this->match_services_to_products) {
380 $s .= "
381 // Nonsurgical methods should normally include a corresponding product.
382 // This takes advantage of the fact that only nonsurgical methods have CYP
383 // less than 10, in both the old and new frameworks.
384 if (tmp_cyp < 10.0) {
385 // Was: if (tmp_meth.substring(0, 2) != '12') {
386 var got_prod = false;
387 for (var plino = 0; f['{$prod}['+plino+'][drug_id]']; ++plino) {
388 var ppfx = '{$prod}[' + plino + ']';
389 if (f[ppfx+'[del]'] && f[ppfx+'[del]'].checked) continue;
390 if (f[ppfx+'[method]'] && f[ppfx+'[method]'].value) {
391 if (f[ppfx+'[method]'].value == tmp_meth) got_prod = true;
394 if (!got_prod) {
395 if (!confirm(" . xlj('Warning: There is no product matching the contraceptive service.') . "))
396 return false;
400 } // end match services to products
401 $s .= "
403 ++required_code_count;
406 if ($this->match_services_to_products) {
407 $s .= "
408 // The following applies to contraception for family planning clinics.
409 // Loop thru the products.
410 for (var lino = 0; f['{$prod}['+lino+'][drug_id]']; ++lino) {
411 var pfx = '{$prod}[' + lino + ']';
412 if (f[pfx + '[del]'] && f[pfx + '[del]'].checked) continue;
413 if (f[pfx + '[method]'] && f[pfx + '[method]'].value) {
414 var tmp_meth = f[pfx + '[method]'].value;
415 // Contraceptive products should normally include a corresponding method.
416 var got_svc = false;
417 for (var slino = 0; f['{$bill}[' + slino + '][code_type]']; ++slino) {
418 var spfx = '{$bill}[' + slino + ']';
419 if (f[spfx + '[del]'] && f[spfx + '[del]'].checked) continue;
420 if (f[spfx + '[method]'] && f[spfx + '[method]'].value) {
421 if (f[spfx + '[method]'].value == tmp_meth) got_svc = true;
424 if (!got_svc) {
425 if (!confirm('" . xls('Warning: There is no service matching the contraceptive product.') . "'))
426 return false;
429 ++required_code_count;
432 } // end match services to products
433 if (isset($GLOBALS['code_types']['MA'])) {
434 $s .= "
435 if (required_code_count == 0) {
436 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.') . "')) {
437 return false;
443 $s .= "
444 // End contraception validation.
445 if (f.ippfconmeth) {
446 // Save the primary contraceptive method to its hidden form field.
447 f.ippfconmeth.value = max_contra_code;
449 return true;
452 return $s;