some portal work
[openemr.git] / library / FeeSheetHtml.class.php
blobbee4bfc5d7bcc9cf19d3a087ee02db000253485d
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 } else { // If not restricting then sort the matching providers first.
51 $query .= " ORDER BY (facility_id = ?) DESC, lname, fname";
54 $res = sqlStatement($query, $sqlarr);
55 $s .= "<option value=''>" . text($toptext) . "</option>";
56 while ($row = sqlFetchArray($res)) {
57 $provid = $row['id'];
58 $s .= "<option value='" . attr($provid) . "'";
59 if ($provid == $default) {
60 $s .= " selected";
63 $s .= ">";
64 if (!$GLOBALS['gbl_restrict_provider_facility'] && $def_facility && $row['facility_id'] == $def_facility) {
65 // Mark providers in the matching facility with an asterisk.
66 $s .= "* ";
69 $s .= text($row['lname'] . ", " . $row['fname']) . "</option>";
72 return $s;
75 // Does the above but including <select> ... </select>.
77 public static function genProviderSelect($tagname, $toptext, $default = 0, $disabled = false)
79 $s = " <select name='" . attr($tagname) . "'";
80 if ($disabled) {
81 $s .= " disabled";
84 $s .= ">";
85 $s .= self::genProviderOptionList($toptext, $default);
86 $s .= "</select>\n";
87 return $s;
90 // Build a drop-down list of warehouses.
92 public function genWarehouseSelect($tagname, $toptext, $default = '', $disabled = false, $drug_id = 0, $is_sold = 0)
94 $s = '';
95 if ($this->got_warehouses) {
96 // Normally would use generate_select_list() but it's not flexible enough here.
97 $s .= "<select name='" . attr($tagname) . "'";
98 if (!$disabled) {
99 $s .= " onchange='warehouse_changed(this);'";
102 if ($disabled) {
103 $s .= " disabled";
106 $s .= ">";
107 $s .= "<option value=''>" . text($toptext) . "</option>";
108 $lres = sqlStatement("SELECT * FROM list_options " .
109 "WHERE list_id = 'warehouse' AND activity = 1 ORDER BY seq, title");
110 while ($lrow = sqlFetchArray($lres)) {
111 $s .= "<option value='" . attr($lrow['option_id']) . "'";
112 if ($disabled) {
113 if ($lrow['option_id'] == $default) {
114 $s .= " selected";
116 } else {
117 $has_inventory = sellDrug($drug_id, 1, 0, 0, 0, 0, '', '', $lrow['option_id'], true);
118 if (((strlen($default) == 0 && $lrow['is_default']) ||
119 (strlen($default) > 0 && $lrow['option_id'] == $default)) &&
120 ($is_sold || $has_inventory)) {
121 $s .= " selected";
122 } else {
123 // Disable this warehouse option if not selected and has no inventory.
124 if (!$has_inventory) {
125 $s .= " disabled";
130 $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
133 $s .= "</select>";
136 return $s;
139 // Build a drop-down list of price levels.
140 // Includes the specified item's price in the "id" of each option.
142 public function genPriceLevelSelect($tagname, $toptext, $pr_id, $pr_selector = '', $default = '', $disabled = false)
144 // echo "<!-- pr_id = '$pr_id', pr_selector = '$pr_selector' -->\n"; // debugging
145 $s = "<select name='" . attr($tagname) . "'";
146 if (!$disabled) {
147 $s .= " onchange='pricelevel_changed(this);'";
150 if ($disabled) {
151 $s .= " disabled";
154 $s .= ">";
155 $s .= "<option value=''>" . text($toptext) . "</option>";
156 $lres = sqlStatement(
157 "SELECT lo.*, p.pr_price " .
158 "FROM list_options AS lo " .
159 "LEFT JOIN prices AS p ON p.pr_id = ? AND p.pr_selector = ? AND p.pr_level = lo.option_id " .
160 "WHERE lo.list_id = 'pricelevel' AND lo.activity = 1 ORDER BY lo.seq, lo.title",
161 array($pr_id, $pr_selector)
163 $standardPrice = 0;
164 while ($lrow = sqlFetchArray($lres)) {
165 $price = empty($lrow['pr_price']) ? 0 : $lrow['pr_price'];
167 // if percent-based pricing is enabled...
168 if ($GLOBALS['enable_percent_pricing']) {
169 // Set standardPrice as the first price level (sorted by seq)
170 if ($standardPrice === 0) {
171 $standardPrice = $price;
174 // If price level notes contains a percentage,
175 // calculate price as percentage of standard price
176 $notes = $lrow['notes'];
177 if (!empty($notes) && strpos($notes, '%') > -1) {
178 $percent = intval(str_replace('%', '', $notes));
179 if ($percent > 0) {
180 $price = $standardPrice * ((100 - $percent) / 100);
185 $s .= "<option value='" . attr($lrow['option_id']) . "'";
186 $s .= " id='prc_$price'";
187 if ((strlen($default) == 0 && $lrow['is_default'] && !$disabled) ||
188 (strlen($default) > 0 && $lrow['option_id'] == $default)
190 $s .= " selected";
193 $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
196 $s .= "</select>";
197 return $s;
200 // If Contraception forms can be auto-created by the Fee Sheet we might need
201 // to ask about the client's prior contraceptive use.
203 public function generateContraceptionSelector($tagname = 'newmauser')
205 $s = '';
206 if ($GLOBALS['gbl_new_acceptor_policy'] == '1') {
207 $csrow = sqlQuery(
208 "SELECT COUNT(*) AS count FROM forms AS f WHERE " .
209 "f.pid = ? AND f.encounter = ? AND " .
210 "f.formdir = 'LBFccicon' AND f.deleted = 0",
211 array($this->pid, $this->encounter)
213 // Do it only if a contraception form does not already exist for this visit.
214 // Otherwise assume that whoever created it knows what they were doing.
215 if ($csrow['count'] == 0) {
216 // Determine if this client ever started contraception with the MA.
217 // Even if only a method change, we assume they have.
218 $query = "SELECT f.form_id FROM forms AS f " .
219 "JOIN form_encounter AS fe ON fe.pid = f.pid AND fe.encounter = f.encounter " .
220 "WHERE f.formdir = 'LBFccicon' AND f.deleted = 0 AND f.pid = ? " .
221 "ORDER BY fe.date DESC LIMIT 1";
222 $csrow = sqlQuery($query, array($this->pid));
223 if (empty($csrow)) {
224 $s .= "<select name='$tagname'>\n";
225 $s .= " <option value='2'>" . xlt('First Modern Contraceptive Use (Lifetime)') . "</option>\n";
226 $s .= " <option value='1'>" . xlt('First Modern Contraception at this Clinic (with Prior Contraceptive Use)') . "</option>\n";
227 $s .= " <option value='0'>" . xlt('Method Change at this Clinic') . "</option>\n";
228 $s .= "</select>\n";
233 return $s;
236 // Generate a price level drop-down defaulting to the patient's current price level.
238 public function generatePriceLevelSelector($tagname = 'pricelevel', $disabled = false)
240 $s = "<select name='" . attr($tagname) . "'";
241 if ($disabled) {
242 $s .= " disabled";
245 $s .= ">";
246 $pricelevel = $this->getPriceLevel();
247 $plres = sqlStatement("SELECT option_id, title FROM list_options " .
248 "WHERE list_id = 'pricelevel' AND activity = 1 ORDER BY seq");
249 while ($plrow = sqlFetchArray($plres)) {
250 $key = $plrow['option_id'];
251 $val = $plrow['title'];
252 $s .= "<option value='" . attr($key) . "'";
253 if ($key == $pricelevel) {
254 $s .= ' selected';
257 $s .= ">" . text(xl_list_label($val)) . "</option>";
260 $s .= "</select>";
261 return $s;
264 // Return Javascript that defines a function to validate the line items.
265 // Most of this is currently IPPF-specific, but NDC codes are also validated.
266 // This also computes and sets the form's ippfconmeth value if appropriate.
267 // This does not validate form fields not related to or derived from line items.
268 // Do not call this javascript function if you are just refreshing the form.
269 // The arguments are the names of the form arrays for services and products.
271 public function jsLineItemValidation($bill = 'bill', $prod = 'prod')
273 $s = "
274 function jsLineItemValidation(f) {
275 var max_contra_cyp = 0;
276 var max_contra_code = '';
277 var required_code_count = 0;
278 // Loop thru the services.
279 for (var lino = 0; f['{$bill}['+lino+'][code_type]']; ++lino) {
280 var pfx = '{$bill}[' + lino + ']';
281 if (f[pfx + '[del]'] && f[pfx + '[del]'].checked) continue;
282 if (f[pfx + '[ndcnum]'] && f[pfx + '[ndcnum]'].value) {
283 // Check NDC number format.
284 var ndcok = true;
285 var ndc = f[pfx + '[ndcnum]'].value;
286 var a = ndc.split('-');
287 if (a.length != 3) {
288 ndcok = false;
290 else if (a[0].length < 1 || a[1].length < 1 || a[2].length < 1 ||
291 a[0].length > 5 || a[1].length > 4 || a[2].length > 2) {
292 ndcok = false;
294 else {
295 for (var i = 0; i < 3; ++i) {
296 for (var j = 0; j < a[i].length; ++j) {
297 var c = a[i].charAt(j);
298 if (c < '0' || c > '9') ndcok = false;
302 if (!ndcok) {
303 alert('" . xls('Format incorrect for NDC') . "\"' + ndc +
304 '\", " . xls('should be like nnnnn-nnnn-nn') . "');
305 if (f[pfx+'[ndcnum]'].focus) f[pfx+'[ndcnum]'].focus();
306 return false;
308 // Check for valid quantity.
309 var qty = f[pfx+'[ndcqty]'].value - 0;
310 if (isNaN(qty) || qty <= 0) {
311 alert('" . xls('Quantity for NDC') . " \"' + ndc +
312 '\" " . xls('is not valid (decimal fractions are OK).') . "');
313 if (f[pfx+'[ndcqty]'].focus) f[pfx+'[ndcqty]'].focus();
314 return false;
317 if (f[pfx+'[method]'] && f[pfx+'[method]'].value) {
318 // The following applies to contraception for family planning clinics.
319 var tmp_cyp = parseFloat(f[pfx+'[cyp]'].value);
320 var tmp_meth = f[pfx+'[method]'].value;
321 var tmp_methtype = parseInt(f[pfx+'[methtype]'].value);
322 if (tmp_cyp > max_contra_cyp && tmp_methtype == 2) {
323 // max_contra_* tracks max cyp for initial consults only.
324 max_contra_cyp = tmp_cyp;
325 max_contra_code = tmp_meth;
328 if ($this->patient_male) {
329 $s .= "
330 var male_compatible_method = (
331 // TBD: Fix hard coded dependency on IPPFCM codes here.
332 tmp_meth == '4450' || // male condoms
333 tmp_meth == '4570'); // male vasectomy
334 if (!male_compatible_method) {
335 if (!confirm('" . xls('Warning: Contraceptive method is not compatible with a male patient.') . "'))
336 return false;
339 } // end if male patient
340 if ($this->patient_age < 10 || $this->patient_age > 50) {
341 $s .= "
342 if (!confirm('" . xls('Warning: Contraception for a patient under 10 or over 50.') . "'))
343 return false;
345 } // end if improper age
346 if ($this->match_services_to_products) {
347 $s .= "
348 // Nonsurgical methods should normally include a corresponding product.
349 // This takes advantage of the fact that only nonsurgical methods have CYP
350 // less than 10, in both the old and new frameworks.
351 if (tmp_cyp < 10.0) {
352 // Was: if (tmp_meth.substring(0, 2) != '12') {
353 var got_prod = false;
354 for (var plino = 0; f['{$prod}['+plino+'][drug_id]']; ++plino) {
355 var ppfx = '{$prod}[' + plino + ']';
356 if (f[ppfx+'[del]'] && f[ppfx+'[del]'].checked) continue;
357 if (f[ppfx+'[method]'] && f[ppfx+'[method]'].value) {
358 if (f[ppfx+'[method]'].value == tmp_meth) got_prod = true;
361 if (!got_prod) {
362 if (!confirm('" . xls('Warning: There is no product matching the contraceptive service.') . "'))
363 return false;
367 } // end match services to products
368 $s .= "
370 ++required_code_count;
373 if ($this->match_services_to_products) {
374 $s .= "
375 // The following applies to contraception for family planning clinics.
376 // Loop thru the products.
377 for (var lino = 0; f['{$prod}['+lino+'][drug_id]']; ++lino) {
378 var pfx = '{$prod}[' + lino + ']';
379 if (f[pfx + '[del]'] && f[pfx + '[del]'].checked) continue;
380 if (f[pfx + '[method]'] && f[pfx + '[method]'].value) {
381 var tmp_meth = f[pfx + '[method]'].value;
382 // Contraceptive products should normally include a corresponding method.
383 var got_svc = false;
384 for (var slino = 0; f['{$bill}[' + slino + '][code_type]']; ++slino) {
385 var spfx = '{$bill}[' + slino + ']';
386 if (f[spfx + '[del]'] && f[spfx + '[del]'].checked) continue;
387 if (f[spfx + '[method]'] && f[spfx + '[method]'].value) {
388 if (f[spfx + '[method]'].value == tmp_meth) got_svc = true;
391 if (!got_svc) {
392 if (!confirm('" . xls('Warning: There is no service matching the contraceptive product.') . "'))
393 return false;
396 ++required_code_count;
399 } // end match services to products
400 if (isset($GLOBALS['code_types']['MA'])) {
401 $s .= "
402 if (required_code_count == 0) {
403 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.') . "')) {
404 return false;
410 $s .= "
411 // End contraception validation.
412 if (f.ippfconmeth) {
413 // Save the primary contraceptive method to its hidden form field.
414 f.ippfconmeth.value = max_contra_code;
416 return true;
419 return $s;