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
{
30 // Dynamically generated JavaScript to maintain justification codes.
31 public $justinit = "var f = document.forms[0];\n";
33 function __construct($pid=0, $encounter=0) {
34 parent
::__construct($pid, $encounter);
37 // Build a drop-down list of providers. This includes users who
38 // have the word "provider" anywhere in their "additional info"
39 // field, so that we can define providers (for billing purposes)
40 // who do not appear in the calendar.
42 public static function genProviderOptionList($toptext, $default=0) {
44 // Get user's default facility, or 0 if none.
45 $drow = sqlQuery("SELECT facility_id FROM users where username = '" . $_SESSION['authUser'] . "'");
46 $def_facility = 0 +
$drow['facility_id'];
48 $sqlarr = array($def_facility);
49 $query = "SELECT id, lname, fname, facility_id FROM users WHERE " .
50 "( authorized = 1 OR info LIKE '%provider%' ) AND username != '' " .
51 "AND active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' )";
52 // If restricting to providers matching user facility...
53 if ($GLOBALS['gbl_restrict_provider_facility']) {
54 $query .= " AND ( facility_id = 0 OR facility_id = ? )";
55 $query .= " ORDER BY lname, fname";
57 // If not restricting then sort the matching providers first.
59 $query .= " ORDER BY (facility_id = ?) DESC, lname, fname";
61 $res = sqlStatement($query, $sqlarr);
62 $s .= "<option value=''>" . text($toptext) . "</option>";
63 while ($row = sqlFetchArray($res)) {
65 $s .= "<option value='" . attr($provid) . "'";
66 if ($provid == $default) $s .= " selected";
68 if (!$GLOBALS['gbl_restrict_provider_facility'] && $def_facility && $row['facility_id'] == $def_facility) {
69 // Mark providers in the matching facility with an asterisk.
72 $s .= text($row['lname'] . ", " . $row['fname']) . "</option>";
77 // Does the above but including <select> ... </select>.
79 public static function genProviderSelect($tagname, $toptext, $default=0, $disabled=false) {
80 $s = " <select name='" . attr($tagname) . "'";
81 if ($disabled) $s .= " disabled";
83 $s .= self
::genProviderOptionList($toptext, $default);
88 // Build a drop-down list of warehouses.
90 public function genWarehouseSelect($tagname, $toptext, $default='', $disabled=false, $drug_id=0, $is_sold=0) {
92 if ($this->got_warehouses
) {
93 // Normally would use generate_select_list() but it's not flexible enough here.
94 $s .= "<select name='" . attr($tagname) . "'";
95 if (!$disabled) $s .= " onchange='warehouse_changed(this);'";
96 if ($disabled ) $s .= " disabled";
98 $s .= "<option value=''>" . text($toptext) . "</option>";
99 $lres = sqlStatement("SELECT * FROM list_options " .
100 "WHERE list_id = 'warehouse' AND activity = 1 ORDER BY seq, title");
101 while ($lrow = sqlFetchArray($lres)) {
102 $s .= "<option value='" . attr($lrow['option_id']) . "'";
104 if ($lrow['option_id'] == $default) $s .= " selected";
107 $has_inventory = sellDrug($drug_id, 1, 0, 0, 0, 0, '', '', $lrow['option_id'], true);
108 if (((strlen($default) == 0 && $lrow['is_default']) ||
109 (strlen($default) > 0 && $lrow['option_id'] == $default)) &&
110 ($is_sold ||
$has_inventory))
115 // Disable this warehouse option if not selected and has no inventory.
116 if (!$has_inventory) $s .= " disabled";
119 $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
126 // Build a drop-down list of price levels.
127 // Includes the specified item's price in the "id" of each option.
129 public function genPriceLevelSelect($tagname, $toptext, $pr_id, $pr_selector='', $default='', $disabled=false) {
130 // echo "<!-- pr_id = '$pr_id', pr_selector = '$pr_selector' -->\n"; // debugging
131 $s = "<select name='" . attr($tagname) . "'";
132 if (!$disabled) $s .= " onchange='pricelevel_changed(this);'";
133 if ($disabled ) $s .= " disabled";
135 $s .= "<option value=''>" . text($toptext) . "</option>";
136 $lres = sqlStatement("SELECT lo.*, p.pr_price " .
137 "FROM list_options AS lo " .
138 "LEFT JOIN prices AS p ON p.pr_id = ? AND p.pr_selector = ? AND p.pr_level = lo.option_id " .
139 "WHERE lo.list_id = 'pricelevel' AND lo.activity = 1 ORDER BY lo.seq, lo.title",
140 array($pr_id, $pr_selector));
141 while ($lrow = sqlFetchArray($lres)) {
142 $price = empty($lrow['pr_price']) ?
0 : $lrow['pr_price'];
143 $s .= "<option value='" . attr($lrow['option_id']) . "'";
144 $s .= " id='prc_$price'";
145 if ((strlen($default) == 0 && $lrow['is_default'] && !$disabled) ||
146 (strlen($default) > 0 && $lrow['option_id'] == $default)
150 $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n";
156 // If Contraception forms can be auto-created by the Fee Sheet we might need
157 // to ask about the client's prior contraceptive use.
159 public function generateContraceptionSelector($tagname='newmauser') {
161 if ($GLOBALS['gbl_new_acceptor_policy'] == '1') {
162 $csrow = sqlQuery("SELECT COUNT(*) AS count FROM forms AS f WHERE " .
163 "f.pid = ? AND f.encounter = ? AND " .
164 "f.formdir = 'LBFccicon' AND f.deleted = 0",
165 array($this->pid
, $this->encounter
));
166 // Do it only if a contraception form does not already exist for this visit.
167 // Otherwise assume that whoever created it knows what they were doing.
168 if ($csrow['count'] == 0) {
169 // Determine if this client ever started contraception with the MA.
170 // Even if only a method change, we assume they have.
171 $query = "SELECT f.form_id FROM forms AS f " .
172 "JOIN form_encounter AS fe ON fe.pid = f.pid AND fe.encounter = f.encounter " .
173 "WHERE f.formdir = 'LBFccicon' AND f.deleted = 0 AND f.pid = ? " .
174 "ORDER BY fe.date DESC LIMIT 1";
175 $csrow = sqlQuery($query, array($this->pid
));
177 $s .= "<select name='$tagname'>\n";
178 $s .= " <option value='2'>" . xlt('First Modern Contraceptive Use (Lifetime)') . "</option>\n";
179 $s .= " <option value='1'>" . xlt('First Modern Contraception at this Clinic (with Prior Contraceptive Use)') . "</option>\n";
180 $s .= " <option value='0'>" . xlt('Method Change at this Clinic') . "</option>\n";
188 // Generate a price level drop-down defaulting to the patient's current price level.
190 public function generatePriceLevelSelector($tagname='pricelevel', $disabled=false) {
191 $s = "<select name='" . attr($tagname) . "'";
192 if ($disabled) $s .= " disabled";
194 $pricelevel = $this->getPriceLevel();
195 $plres = sqlStatement("SELECT option_id, title FROM list_options " .
196 "WHERE list_id = 'pricelevel' AND activity = 1 ORDER BY seq");
197 while ($plrow = sqlFetchArray($plres)) {
198 $key = $plrow['option_id'];
199 $val = $plrow['title'];
200 $s .= "<option value='" . attr($key) . "'";
201 if ($key == $pricelevel) $s .= ' selected';
202 $s .= ">" . text(xl_list_label($val)) . "</option>";
208 // Return Javascript that defines a function to validate the line items.
209 // Most of this is currently IPPF-specific, but NDC codes are also validated.
210 // This also computes and sets the form's ippfconmeth value if appropriate.
211 // This does not validate form fields not related to or derived from line items.
212 // Do not call this javascript function if you are just refreshing the form.
213 // The arguments are the names of the form arrays for services and products.
215 public function jsLineItemValidation($bill='bill', $prod='prod') {
217 function jsLineItemValidation(f) {
218 var max_contra_cyp = 0;
219 var max_contra_code = '';
220 var required_code_count = 0;
221 // Loop thru the services.
222 for (var lino = 0; f['{$bill}['+lino+'][code_type]']; ++lino) {
223 var pfx = '{$bill}[' + lino + ']';
224 if (f[pfx + '[del]'] && f[pfx + '[del]'].checked) continue;
225 if (f[pfx + '[ndcnum]'] && f[pfx + '[ndcnum]'].value) {
226 // Check NDC number format.
228 var ndc = f[pfx + '[ndcnum]'].value;
229 var a = ndc.split('-');
233 else if (a[0].length < 1 || a[1].length < 1 || a[2].length < 1 ||
234 a[0].length > 5 || a[1].length > 4 || a[2].length > 2) {
238 for (var i = 0; i < 3; ++i) {
239 for (var j = 0; j < a[i].length; ++j) {
240 var c = a[i].charAt(j);
241 if (c < '0' || c > '9') ndcok = false;
246 alert('" . xls('Format incorrect for NDC') . "\"' + ndc +
247 '\", " . xls('should be like nnnnn-nnnn-nn') . "');
248 if (f[pfx+'[ndcnum]'].focus) f[pfx+'[ndcnum]'].focus();
251 // Check for valid quantity.
252 var qty = f[pfx+'[ndcqty]'].value - 0;
253 if (isNaN(qty) || qty <= 0) {
254 alert('" . xls('Quantity for NDC') . " \"' + ndc +
255 '\" " . xls('is not valid (decimal fractions are OK).') . "');
256 if (f[pfx+'[ndcqty]'].focus) f[pfx+'[ndcqty]'].focus();
260 if (f[pfx+'[method]'] && f[pfx+'[method]'].value) {
261 // The following applies to contraception for family planning clinics.
262 var tmp_cyp = parseFloat(f[pfx+'[cyp]'].value);
263 var tmp_meth = f[pfx+'[method]'].value;
264 var tmp_methtype = parseInt(f[pfx+'[methtype]'].value);
265 if (tmp_cyp > max_contra_cyp && tmp_methtype == 2) {
266 // max_contra_* tracks max cyp for initial consults only.
267 max_contra_cyp = tmp_cyp;
268 max_contra_code = tmp_meth;
271 if ($this->patient_male
) {
273 var male_compatible_method = (
274 // TBD: Fix hard coded dependency on IPPFCM codes here.
275 tmp_meth == '4450' || // male condoms
276 tmp_meth == '4570'); // male vasectomy
277 if (!male_compatible_method) {
278 if (!confirm('" . xls('Warning: Contraceptive method is not compatible with a male patient.') . "'))
282 } // end if male patient
283 if ($this->patient_age
< 10 ||
$this->patient_age
> 50) {
285 if (!confirm('" . xls('Warning: Contraception for a patient under 10 or over 50.') . "'))
288 } // end if improper age
289 if ($this->match_services_to_products
) {
291 // Nonsurgical methods should normally include a corresponding product.
292 // This takes advantage of the fact that only nonsurgical methods have CYP
293 // less than 10, in both the old and new frameworks.
294 if (tmp_cyp < 10.0) {
295 // Was: if (tmp_meth.substring(0, 2) != '12') {
296 var got_prod = false;
297 for (var plino = 0; f['{$prod}['+plino+'][drug_id]']; ++plino) {
298 var ppfx = '{$prod}[' + plino + ']';
299 if (f[ppfx+'[del]'] && f[ppfx+'[del]'].checked) continue;
300 if (f[ppfx+'[method]'] && f[ppfx+'[method]'].value) {
301 if (f[ppfx+'[method]'].value == tmp_meth) got_prod = true;
305 if (!confirm('" . xls('Warning: There is no product matching the contraceptive service.') . "'))
310 } // end match services to products
313 ++required_code_count;
316 if ($this->match_services_to_products
) {
318 // The following applies to contraception for family planning clinics.
319 // Loop thru the products.
320 for (var lino = 0; f['{$prod}['+lino+'][drug_id]']; ++lino) {
321 var pfx = '{$prod}[' + lino + ']';
322 if (f[pfx + '[del]'] && f[pfx + '[del]'].checked) continue;
323 if (f[pfx + '[method]'] && f[pfx + '[method]'].value) {
324 var tmp_meth = f[pfx + '[method]'].value;
325 // Contraceptive products should normally include a corresponding method.
327 for (var slino = 0; f['{$bill}[' + slino + '][code_type]']; ++slino) {
328 var spfx = '{$bill}[' + slino + ']';
329 if (f[spfx + '[del]'] && f[spfx + '[del]'].checked) continue;
330 if (f[spfx + '[method]'] && f[spfx + '[method]'].value) {
331 if (f[spfx + '[method]'].value == tmp_meth) got_svc = true;
335 if (!confirm('" . xls('Warning: There is no service matching the contraceptive product.') . "'))
339 ++required_code_count;
342 } // end match services to products
343 if (isset($GLOBALS['code_types']['MA'])) {
345 if (required_code_count == 0) {
346 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.') . "')) {
353 // End contraception validation.
355 // Save the primary contraceptive method to its hidden form field.
356 f.ippfconmeth.value = max_contra_code;