calendar widget bug fix by mmfsystems
[openemr.git] / custom / LBF / LBFgcac.plugin.php
blob6b66651c5c1647d583415eb7b72496fb04e279d6
1 <?php
2 // Copyright (C) 2009-2010 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This provides enhancement functions for the LBFgcac visit form.
10 // It is invoked by interface/forms/LBF/new.php.
12 // Private function. Constructs a query to find a given lbf_data field's
13 // values from visits within the past 2 weeks.
15 function _LBFgcac_query_recent($more) {
16 global $pid, $encounter, $formname, $formid;
18 // Get the date of this visit.
19 $encrow = sqlQuery("SELECT date FROM form_encounter WHERE " .
20 "pid = '$pid' AND encounter = '$encounter'");
21 $encdate = $encrow['date'];
23 // Query complications from the two weeks prior to this visit.
24 $query = "SELECT d.field_value " .
25 "FROM forms AS f, form_encounter AS fe, lbf_data AS d " .
26 "WHERE f.pid = '$pid' AND " .
27 "f.formdir = '$formname' AND " .
28 "f.deleted = 0 AND " .
29 "fe.pid = f.pid AND fe.encounter = f.encounter AND " .
30 "fe.date <= '$encdate' AND " .
31 "DATE_ADD(fe.date, INTERVAL 14 DAY) > '$encdate' AND " .
32 "d.form_id = f.form_id AND $more";
34 return $query;
37 // Private function. Given a field name, gets its value from the most
38 // recent instance of this form type that is not more than 2 weeks old.
40 function _LBFgcac_recent_default($name) {
41 global $formid;
43 // This logic only makes sense for a new form.
44 if ($formid) return '';
46 $query = _LBFgcac_query_recent(
47 "d.field_id = '$name' " .
48 "ORDER BY f.form_id DESC LIMIT 1");
49 $row = sqlQuery($query);
51 if (empty($row['field_value'])) return '';
52 return $row['field_value'];
55 // Private function. Query services within 2 weeks of this encounter.
57 function _LBFgcac_query_recent_services() {
58 global $pid, $encounter;
60 // Get the date of this visit.
61 $encrow = sqlQuery("SELECT date FROM form_encounter WHERE " .
62 "pid = '$pid' AND encounter = '$encounter'");
63 $encdate = $encrow['date'];
65 // Query services from the two weeks prior to this visit.
66 $query = "SELECT c.related_code " .
67 "FROM form_encounter AS fe, billing AS b, codes AS c " .
68 "WHERE fe.pid = '$pid' AND fe.date <= '$encdate' AND " .
69 "DATE_ADD(fe.date, INTERVAL 14 DAY) > '$encdate' AND " .
70 "b.pid = fe.pid AND b.encounter = fe.encounter AND b.activity = 1 AND " .
71 "b.code_type = 'MA' AND c.code_type = '12' AND " .
72 "c.code = b.code AND c.modifier = b.modifier " .
73 "ORDER BY fe.date DESC, b.id DESC";
75 return $query;
78 // Private function. Query services from this encounter.
80 function _LBFgcac_query_current_services() {
81 global $pid, $encounter;
83 $query = "SELECT c.related_code " .
84 "FROM billing AS b, codes AS c WHERE " .
85 "b.pid = '$pid' AND b.encounter = '$encounter' AND b.activity = 1 AND " .
86 "b.code_type = 'MA' AND c.code_type = '12' AND " .
87 "c.code = b.code AND c.modifier = b.modifier " .
88 "ORDER BY b.id DESC";
90 return $query;
93 // The purpose of this function is to create JavaScript for the <head>
94 // section of the page. This in turn defines desired javaScript
95 // functions.
97 function LBFgcac_javascript() {
98 global $formid;
100 // Query complications from the two weeks prior to this visit.
101 $res = sqlStatement(_LBFgcac_query_recent(
102 "f.form_id != '$formid' AND " .
103 "d.field_id = 'complications'"));
105 // This JavaScript function is to enable items in the "Main complications"
106 // list that have been selected, and to disable all others.
107 // Option.disabled seems to work for Firefox 3 and IE8 but not IE6.
108 echo "// Enable recent complications and disable all others.
109 function set_main_compl_list() {
110 var f = document.forms[0];
111 var sel = f.form_main_compl;
112 var n = '';
114 // We use the checkbox object values as a scratch area to note which
115 // complications were already selected from other forms.
116 while ($row = sqlFetchArray($res)) {
117 $a = explode('|', $row['field_value']);
118 foreach ($a as $complid) {
119 if (empty($complid)) continue;
120 echo " n = 'form_complications[$complid]'; if (f[n]) f[n].value = 2;\n";
123 echo " // Scan the list items and set their disabled flags.
124 for (var i = 1; i < sel.options.length; ++i) {
125 n = 'form_complications[' + sel.options[i].value + ']';
126 sel.options[i].disabled = (f[n] && (f[n].checked || f[n].value == '2')) ? false : true;
131 echo "
132 // Disable most form fields if refusing abortion.
133 function client_status_changed() {
134 var f = document.forms[0];
135 var dis1 = false; // things to show unless refusing abortion
136 var dis2 = false; // things to show if abortion is elsewhere
137 var cs = f.form_client_status;
138 var csval = '';
139 if (cs.type) { // cs = select list
140 if (cs.selectedIndex >= 0) {
141 csval = cs.options[cs.selectedIndex].value;
144 else { // cs = array of radio buttons
145 for (var i = 0; i < cs.length; ++i) {
146 if (cs[i].checked) {
147 csval = cs[i].value;
148 break;
152 if (csval == 'mara') {
153 dis1 = true;
154 dis2 = true;
156 else if (csval == 'maaa') {
157 dis2 = true;
159 for (var i = 0; i < f.elements.length; ++i) {
160 var e = f.elements[i];
161 if (e.name.substring(0,18) == 'form_complications' || e.name == 'form_main_compl') {
162 e.disabled = dis1;
164 else if (e.name == 'form_in_ab_proc') {
165 e.disabled = dis2;
167 else if (e.name.substring(0,15) == 'form_contrameth') {
168 e.disabled = (csval != 'refin');
170 else if (e.name == 'form_ab_location') {
171 if (csval == 'maaa') {
172 e.disabled = (e.value == 'part' || e.value == 'oth');
174 else if (csval == 'mara') {
175 e.disabled = true;
177 else {
178 e.disabled = (e.value == 'proc' || e.value == 'ma');
185 echo "
186 // Enable some form fields before submitting.
187 // This is because disabled fields do not submit their data, however
188 // we do want to save the default values that were set for them.
189 function mysubmit() {
190 var f = document.forms[0];
191 for (var i = 0; i < f.elements.length; ++i) {
192 var e = f.elements[i];
193 if (e.name == 'form_in_ab_proc') {
194 e.disabled = false;
197 top.restoreSession();
198 return true;
204 // The purpose of this function is to create JavaScript that is run
205 // once when the page is loaded.
207 function LBFgcac_javascript_onload() {
208 echo "
209 set_main_compl_list();
210 client_status_changed();
211 var f = document.forms[0];
212 for (var i = 0; i < f.elements.length; ++i) {
213 var e = f.elements[i];
214 if (e.name.substring(0,18) == 'form_complications')
215 e.onclick = function () { set_main_compl_list(); };
217 var cs = f.form_client_status;
218 if (cs.type) { // cs = select list
219 cs.onchange = function () { client_status_changed(); };
221 else { // cs = array of radio buttons
222 for (var i = 0; i < cs.length; ++i) {
223 cs[i].onclick = function () { client_status_changed(); };
226 f.onsubmit = function () { return mysubmit(); };
229 // Query services from this visit to set default contraception choices.
230 $res = sqlStatement(_LBFgcac_query_current_services());
231 while ($row = sqlFetchArray($res)) {
232 if (empty($row['related_code'])) continue;
233 $relcodes = explode(';', $row['related_code']);
234 foreach ($relcodes as $codestring) {
235 if ($codestring === '') continue;
236 list($codetype, $code) = explode(':', $codestring);
237 if ($codetype !== 'IPPF') continue;
238 $lres = sqlStatement("SELECT option_id, mapping FROM list_options " .
239 "WHERE list_id = 'contrameth'");
240 while ($lrow = sqlFetchArray($lres)) {
241 $maparr = explode(':', $lrow['mapping']);
242 if (empty($maparr[1])) continue;
243 $option_id = $lrow['option_id'];
244 if (preg_match('/^' . $maparr[1] . '/', $code)) {
245 echo "f['form_contrameth[$option_id]'].checked = true;\n";
253 // Generate default for client status.
255 function LBFgcac_default_client_status() {
256 return _LBFgcac_recent_default('client_status');
259 // Generate default for visit type. If there are no recent prior visits,
260 // then default to new procedure.
262 function LBFgcac_default_ab_location() {
263 global $formid;
264 if ($formid) return '';
265 $vt = _LBFgcac_recent_default('ab_location');
266 if (empty($vt)) return 'proc';
267 return $vt;
270 // Generate default for the induced procedure type.
272 function LBFgcac_default_in_ab_proc() {
274 // Check previous GCAC visit forms for this setting.
275 $default = _LBFgcac_recent_default('in_ab_proc');
276 if ($default !== '') return $default;
278 // If none, query services from recent visits to see if an IPPF code
279 // matches that of a procedure type in the list.
280 $res = sqlStatement(_LBFgcac_query_recent_services());
281 while ($row = sqlFetchArray($res)) {
282 if (empty($row['related_code'])) continue;
283 $relcodes = explode(';', $row['related_code']);
284 foreach ($relcodes as $codestring) {
285 if ($codestring === '') continue;
286 list($codetype, $code) = explode(':', $codestring);
287 if ($codetype !== 'IPPF') continue;
288 $lres = sqlStatement("SELECT option_id, mapping FROM list_options " .
289 "WHERE list_id = 'in_ab_proc'");
290 while ($lrow = sqlFetchArray($lres)) {
291 $maparr = explode(':', $lrow['mapping']);
292 if (empty($maparr[1])) continue;
293 if (preg_match('/^' . $maparr[1] . '/', $code)) {
294 return $lrow['option_id'];
297 } // end foreach
300 return '';
303 // Generate default for the main complication.
305 function LBFgcac_default_main_compl() {
306 return _LBFgcac_recent_default('main_compl');