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 .
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
31 // Dynamically generated JavaScript to maintain justification codes.
32 public $justinit = "var f = document.forms[0];\n";
34 function __construct($pid = 0, $encounter = 0)
36 parent
::__construct($pid, $encounter);
39 // Build a drop-down list of providers. This includes users who
40 // have the word "provider" anywhere in their "additional info"
41 // field, so that we can define providers (for billing purposes)
42 // who do not appear in the calendar.
44 public static function genProviderOptionList($toptext, $default = 0)
47 // Get user's default facility, or 0 if none.
48 $drow = sqlQuery("SELECT facility_id FROM users where username = '" . $_SESSION['authUser'] . "'");
49 $def_facility = 0 +
$drow['facility_id'];
51 $sqlarr = array($def_facility);
52 $query = "SELECT id, lname, fname, facility_id FROM users WHERE " .
53 "( authorized = 1 OR info LIKE '%provider%' ) AND username != '' " .
54 "AND active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' )";
55 // If restricting to providers matching user facility...
56 if ($GLOBALS['gbl_restrict_provider_facility']) {
57 $query .= " AND ( facility_id = 0 OR facility_id = ? )";
58 $query .= " ORDER BY lname, fname";
59 } // If not restricting then sort the matching providers first.
61 $query .= " ORDER BY (facility_id = ?) DESC, lname, fname";
64 $res = sqlStatement($query, $sqlarr);
65 $s .= "<option value=''>" . text($toptext) . "</option>";
66 while ($row = sqlFetchArray($res)) {
68 $s .= "<option value='" . attr($provid) . "'";
69 if ($provid == $default) {
74 if (!$GLOBALS['gbl_restrict_provider_facility'] && $def_facility && $row['facility_id'] == $def_facility) {
75 // Mark providers in the matching facility with an asterisk.
79 $s .= text($row['lname'] . ", " . $row['fname']) . "</option>";
85 // Does the above but including <select> ... </select>.
87 public static function genProviderSelect($tagname, $toptext, $default = 0, $disabled = false)
89 $s = " <select name='" . attr($tagname) . "'";
95 $s .= self
::genProviderOptionList($toptext, $default);
100 // Build a drop-down list of warehouses.
102 public function genWarehouseSelect($tagname, $toptext, $default = '', $disabled = false, $drug_id = 0, $is_sold = 0)
105 if ($this->got_warehouses
) {
106 // Normally would use generate_select_list() but it's not flexible enough here.
107 $s .= "<select name='" . attr($tagname) . "'";
109 $s .= " onchange='warehouse_changed(this);'";
117 $s .= "<option value=''>" . text($toptext) . "</option>";
118 $lres = sqlStatement("SELECT * FROM list_options " .
119 "WHERE list_id = 'warehouse' AND activity = 1 ORDER BY seq, title");
120 while ($lrow = sqlFetchArray($lres)) {
121 $s .= "<option value='" . attr($lrow['option_id']) . "'";
123 if ($lrow['option_id'] == $default) {
127 $has_inventory = sellDrug($drug_id, 1, 0, 0, 0, 0, '', '', $lrow['option_id'], true);
128 if (((strlen($default) == 0 && $lrow['is_default']) ||
129 (strlen($default) > 0 && $lrow['option_id'] == $default)) &&
130 ($is_sold ||
$has_inventory)) {
133 // Disable this warehouse option if not selected and has no inventory.
134 if (!$has_inventory) {
140 $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
149 // Build a drop-down list of price levels.
150 // Includes the specified item's price in the "id" of each option.
152 public function genPriceLevelSelect($tagname, $toptext, $pr_id, $pr_selector = '', $default = '', $disabled = false)
154 // echo "<!-- pr_id = '$pr_id', pr_selector = '$pr_selector' -->\n"; // debugging
155 $s = "<select name='" . attr($tagname) . "'";
157 $s .= " onchange='pricelevel_changed(this);'";
165 $s .= "<option value=''>" . text($toptext) . "</option>";
166 $lres = sqlStatement(
167 "SELECT lo.*, p.pr_price " .
168 "FROM list_options AS lo " .
169 "LEFT JOIN prices AS p ON p.pr_id = ? AND p.pr_selector = ? AND p.pr_level = lo.option_id " .
170 "WHERE lo.list_id = 'pricelevel' AND lo.activity = 1 ORDER BY lo.seq, lo.title",
171 array($pr_id, $pr_selector)
173 while ($lrow = sqlFetchArray($lres)) {
174 $price = empty($lrow['pr_price']) ?
0 : $lrow['pr_price'];
175 $s .= "<option value='" . attr($lrow['option_id']) . "'";
176 $s .= " id='prc_$price'";
177 if ((strlen($default) == 0 && $lrow['is_default'] && !$disabled) ||
178 (strlen($default) > 0 && $lrow['option_id'] == $default)
183 $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
190 // If Contraception forms can be auto-created by the Fee Sheet we might need
191 // to ask about the client's prior contraceptive use.
193 public function generateContraceptionSelector($tagname = 'newmauser')
196 if ($GLOBALS['gbl_new_acceptor_policy'] == '1') {
198 "SELECT COUNT(*) AS count FROM forms AS f WHERE " .
199 "f.pid = ? AND f.encounter = ? AND " .
200 "f.formdir = 'LBFccicon' AND f.deleted = 0",
201 array($this->pid
, $this->encounter
)
203 // Do it only if a contraception form does not already exist for this visit.
204 // Otherwise assume that whoever created it knows what they were doing.
205 if ($csrow['count'] == 0) {
206 // Determine if this client ever started contraception with the MA.
207 // Even if only a method change, we assume they have.
208 $query = "SELECT f.form_id FROM forms AS f " .
209 "JOIN form_encounter AS fe ON fe.pid = f.pid AND fe.encounter = f.encounter " .
210 "WHERE f.formdir = 'LBFccicon' AND f.deleted = 0 AND f.pid = ? " .
211 "ORDER BY fe.date DESC LIMIT 1";
212 $csrow = sqlQuery($query, array($this->pid
));
214 $s .= "<select name='$tagname'>\n";
215 $s .= " <option value='2'>" . xlt('First Modern Contraceptive Use (Lifetime)') . "</option>\n";
216 $s .= " <option value='1'>" . xlt('First Modern Contraception at this Clinic (with Prior Contraceptive Use)') . "</option>\n";
217 $s .= " <option value='0'>" . xlt('Method Change at this Clinic') . "</option>\n";
226 // Generate a price level drop-down defaulting to the patient's current price level.
228 public function generatePriceLevelSelector($tagname = 'pricelevel', $disabled = false)
230 $s = "<select name='" . attr($tagname) . "'";
236 $pricelevel = $this->getPriceLevel();
237 $plres = sqlStatement("SELECT option_id, title FROM list_options " .
238 "WHERE list_id = 'pricelevel' AND activity = 1 ORDER BY seq");
239 while ($plrow = sqlFetchArray($plres)) {
240 $key = $plrow['option_id'];
241 $val = $plrow['title'];
242 $s .= "<option value='" . attr($key) . "'";
243 if ($key == $pricelevel) {
247 $s .= ">" . text(xl_list_label($val)) . "</option>";
254 // Return Javascript that defines a function to validate the line items.
255 // Most of this is currently IPPF-specific, but NDC codes are also validated.
256 // This also computes and sets the form's ippfconmeth value if appropriate.
257 // This does not validate form fields not related to or derived from line items.
258 // Do not call this javascript function if you are just refreshing the form.
259 // The arguments are the names of the form arrays for services and products.
261 public function jsLineItemValidation($bill = 'bill', $prod = 'prod')
264 function jsLineItemValidation(f) {
265 var max_contra_cyp = 0;
266 var max_contra_code = '';
267 var required_code_count = 0;
268 // Loop thru the services.
269 for (var lino = 0; f['{$bill}['+lino+'][code_type]']; ++lino) {
270 var pfx = '{$bill}[' + lino + ']';
271 if (f[pfx + '[del]'] && f[pfx + '[del]'].checked) continue;
272 if (f[pfx + '[ndcnum]'] && f[pfx + '[ndcnum]'].value) {
273 // Check NDC number format.
275 var ndc = f[pfx + '[ndcnum]'].value;
276 var a = ndc.split('-');
280 else if (a[0].length < 1 || a[1].length < 1 || a[2].length < 1 ||
281 a[0].length > 5 || a[1].length > 4 || a[2].length > 2) {
285 for (var i = 0; i < 3; ++i) {
286 for (var j = 0; j < a[i].length; ++j) {
287 var c = a[i].charAt(j);
288 if (c < '0' || c > '9') ndcok = false;
293 alert('" . xls('Format incorrect for NDC') . "\"' + ndc +
294 '\", " . xls('should be like nnnnn-nnnn-nn') . "');
295 if (f[pfx+'[ndcnum]'].focus) f[pfx+'[ndcnum]'].focus();
298 // Check for valid quantity.
299 var qty = f[pfx+'[ndcqty]'].value - 0;
300 if (isNaN(qty) || qty <= 0) {
301 alert('" . xls('Quantity for NDC') . " \"' + ndc +
302 '\" " . xls('is not valid (decimal fractions are OK).') . "');
303 if (f[pfx+'[ndcqty]'].focus) f[pfx+'[ndcqty]'].focus();
307 if (f[pfx+'[method]'] && f[pfx+'[method]'].value) {
308 // The following applies to contraception for family planning clinics.
309 var tmp_cyp = parseFloat(f[pfx+'[cyp]'].value);
310 var tmp_meth = f[pfx+'[method]'].value;
311 var tmp_methtype = parseInt(f[pfx+'[methtype]'].value);
312 if (tmp_cyp > max_contra_cyp && tmp_methtype == 2) {
313 // max_contra_* tracks max cyp for initial consults only.
314 max_contra_cyp = tmp_cyp;
315 max_contra_code = tmp_meth;
318 if ($this->patient_male
) {
320 var male_compatible_method = (
321 // TBD: Fix hard coded dependency on IPPFCM codes here.
322 tmp_meth == '4450' || // male condoms
323 tmp_meth == '4570'); // male vasectomy
324 if (!male_compatible_method) {
325 if (!confirm('" . xls('Warning: Contraceptive method is not compatible with a male patient.') . "'))
329 } // end if male patient
330 if ($this->patient_age
< 10 ||
$this->patient_age
> 50) {
332 if (!confirm('" . xls('Warning: Contraception for a patient under 10 or over 50.') . "'))
335 } // end if improper age
336 if ($this->match_services_to_products
) {
338 // Nonsurgical methods should normally include a corresponding product.
339 // This takes advantage of the fact that only nonsurgical methods have CYP
340 // less than 10, in both the old and new frameworks.
341 if (tmp_cyp < 10.0) {
342 // Was: if (tmp_meth.substring(0, 2) != '12') {
343 var got_prod = false;
344 for (var plino = 0; f['{$prod}['+plino+'][drug_id]']; ++plino) {
345 var ppfx = '{$prod}[' + plino + ']';
346 if (f[ppfx+'[del]'] && f[ppfx+'[del]'].checked) continue;
347 if (f[ppfx+'[method]'] && f[ppfx+'[method]'].value) {
348 if (f[ppfx+'[method]'].value == tmp_meth) got_prod = true;
352 if (!confirm('" . xls('Warning: There is no product matching the contraceptive service.') . "'))
357 } // end match services to products
360 ++required_code_count;
363 if ($this->match_services_to_products
) {
365 // The following applies to contraception for family planning clinics.
366 // Loop thru the products.
367 for (var lino = 0; f['{$prod}['+lino+'][drug_id]']; ++lino) {
368 var pfx = '{$prod}[' + lino + ']';
369 if (f[pfx + '[del]'] && f[pfx + '[del]'].checked) continue;
370 if (f[pfx + '[method]'] && f[pfx + '[method]'].value) {
371 var tmp_meth = f[pfx + '[method]'].value;
372 // Contraceptive products should normally include a corresponding method.
374 for (var slino = 0; f['{$bill}[' + slino + '][code_type]']; ++slino) {
375 var spfx = '{$bill}[' + slino + ']';
376 if (f[spfx + '[del]'] && f[spfx + '[del]'].checked) continue;
377 if (f[spfx + '[method]'] && f[spfx + '[method]'].value) {
378 if (f[spfx + '[method]'].value == tmp_meth) got_svc = true;
382 if (!confirm('" . xls('Warning: There is no service matching the contraceptive product.') . "'))
386 ++required_code_count;
389 } // end match services to products
390 if (isset($GLOBALS['code_types']['MA'])) {
392 if (required_code_count == 0) {
393 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.') . "')) {
401 // End contraception validation.
403 // Save the primary contraceptive method to its hidden form field.
404 f.ippfconmeth.value = max_contra_code;