Highway to PSR2
[openemr.git] / sites / default / LBF / LBFgcac.plugin.php
blobda8082498da395903d48ce48fe26303e960431eb
1 <?php
2 // Copyright (C) 2009-2011, 2016 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)
17 global $pid, $encounter, $formname, $formid;
19 // Get the date of this visit.
20 $encrow = sqlQuery("SELECT date FROM form_encounter WHERE " .
21 "pid = '$pid' AND encounter = '$encounter'");
22 $encdate = $encrow['date'];
24 // Query complications from the two weeks prior to this visit.
25 $query = "SELECT d.field_value " .
26 "FROM forms AS f, form_encounter AS fe, lbf_data AS d " .
27 "WHERE f.pid = '$pid' AND " .
28 "f.formdir = '$formname' AND " .
29 "f.deleted = 0 AND " .
30 "fe.pid = f.pid AND fe.encounter = f.encounter AND " .
31 "fe.date <= '$encdate' AND " .
32 "DATE_ADD(fe.date, INTERVAL 14 DAY) > '$encdate' AND " .
33 "d.form_id = f.form_id AND $more";
35 return $query;
38 // Private function. Given a field name, gets its value from the most
39 // recent instance of this form type that is not more than 2 weeks old.
41 function _LBFgcac_recent_default($name)
43 global $formid;
45 // This logic only makes sense for a new form.
46 if ($formid) {
47 return '';
50 $query = _LBFgcac_query_recent(
51 "d.field_id = '$name' " .
52 "ORDER BY f.form_id DESC LIMIT 1"
54 $row = sqlQuery($query);
56 if (empty($row['field_value'])) {
57 return '';
60 return $row['field_value'];
63 // Private function. Query services within 2 weeks of this encounter.
65 function _LBFgcac_query_recent_services()
67 global $pid, $encounter;
69 // Get the date of this visit.
70 $encrow = sqlQuery("SELECT date FROM form_encounter WHERE " .
71 "pid = '$pid' AND encounter = '$encounter'");
72 $encdate = $encrow['date'];
74 // Query services from the two weeks prior to this visit.
75 $query = "SELECT c.related_code " .
76 "FROM form_encounter AS fe, billing AS b, codes AS c " .
77 "WHERE fe.pid = '$pid' AND fe.date <= '$encdate' AND " .
78 "DATE_ADD(fe.date, INTERVAL 14 DAY) > '$encdate' AND " .
79 "b.pid = fe.pid AND b.encounter = fe.encounter AND b.activity = 1 AND " .
80 "b.code_type = 'MA' AND c.code_type = '12' AND " .
81 "c.code = b.code AND c.modifier = b.modifier " .
82 "ORDER BY fe.date DESC, b.id DESC";
84 return $query;
87 // Private function. Query services from this encounter.
89 function _LBFgcac_query_current_services()
91 global $pid, $encounter;
93 $query = "SELECT c.related_code " .
94 "FROM billing AS b, codes AS c WHERE " .
95 "b.pid = '$pid' AND b.encounter = '$encounter' AND b.activity = 1 AND " .
96 "b.code_type = 'MA' AND c.code_type = '12' AND " .
97 "c.code = b.code AND c.modifier = b.modifier " .
98 "ORDER BY b.id DESC";
100 return $query;
103 // The purpose of this function is to create JavaScript for the <head>
104 // section of the page. This in turn defines desired javaScript
105 // functions.
107 function LBFgcac_javascript()
109 global $formid;
111 // Query complications from the two weeks prior to this visit.
112 $res = sqlStatement(_LBFgcac_query_recent(
113 "f.form_id != '$formid' AND " .
114 "d.field_id = 'complications'"
117 // This JavaScript function is to enable items in the "Main complications"
118 // list that have been selected, and to disable all others.
119 // Option.disabled seems to work for Firefox 3 and IE8 but not IE6.
120 echo "// Enable recent complications and disable all others.
121 function set_main_compl_list() {
122 var f = document.forms[0];
123 var sel = f.form_main_compl;
124 var n = '';
126 // We use the checkbox object values as a scratch area to note which
127 // complications were already selected from other forms.
128 while ($row = sqlFetchArray($res)) {
129 $a = explode('|', $row['field_value']);
130 foreach ($a as $complid) {
131 if (empty($complid)) {
132 continue;
135 echo " n = 'form_complications[$complid]'; if (f[n]) f[n].value = 2;\n";
139 echo " // Scan the list items and set their disabled flags.
140 for (var i = 1; i < sel.options.length; ++i) {
141 n = 'form_complications[' + sel.options[i].value + ']';
142 sel.options[i].disabled = (f[n] && (f[n].checked || f[n].value == '2')) ? false : true;
147 echo "
148 // Disable most form fields if refusing abortion.
149 function client_status_changed() {
150 var f = document.forms[0];
151 var dis1 = false; // true to disable complications
152 var dis2 = false; // true to disable procedures
153 var cs = f.form_client_status;
154 var csval = '';
155 if (cs.type) { // cs = select list
156 if (cs.selectedIndex >= 0) {
157 csval = cs.options[cs.selectedIndex].value;
160 else { // cs = array of radio buttons
161 for (var i = 0; i < cs.length; ++i) {
162 if (cs[i].checked) {
163 csval = cs[i].value;
164 break;
168 if (csval == 'mara' || csval == 'defer' || csval == 'refin') {
169 dis1 = true;
170 dis2 = true;
172 else if (csval == 'maaa') {
173 dis2 = true;
175 for (var i = 0; i < f.elements.length; ++i) {
176 var e = f.elements[i];
177 if (e.name.substring(0,18) == 'form_complications' || e.name == 'form_main_compl') {
178 e.disabled = dis1;
180 else if (e.name == 'form_in_ab_proc') {
181 e.disabled = dis2;
183 else if (e.name == 'form_ab_location') {
184 if (csval == 'maaa') {
185 e.disabled = (e.value == 'part' || e.value == 'oth' || e.value == 'na');
187 else if (csval == 'mara' || csval == 'defer' || csval == 'self') {
188 e.disabled = true; // (e.value != 'na');
190 // else if (csval == 'refout') {
191 // e.disabled = (e.value == 'proc' || e.value == 'ma');
192 // }
193 else { // inbound referral
194 e.disabled = (e.value == 'na' || e.value == 'proc' || e.value == 'ma');
197 else if (e.name == 'form_gc_rreason') {
198 e.disabled = (csval != 'mara' && csval != 'refout');
204 echo "
205 // Enable some form fields before submitting.
206 // This is because disabled fields do not submit their data, however
207 // we do want to save the default values that were set for them.
208 function mysubmit() {
209 var f = document.forms[0];
210 for (var i = 0; i < f.elements.length; ++i) {
211 var e = f.elements[i];
212 if (e.name == 'form_in_ab_proc') {
213 e.disabled = false;
216 top.restoreSession();
217 return true;
222 // The purpose of this function is to create JavaScript that is run
223 // once when the page is loaded.
225 function LBFgcac_javascript_onload()
227 echo "
228 set_main_compl_list();
229 client_status_changed();
230 var f = document.forms[0];
231 for (var i = 0; i < f.elements.length; ++i) {
232 var e = f.elements[i];
233 if (e.name.substring(0,18) == 'form_complications')
234 e.onclick = function () { set_main_compl_list(); };
236 var cs = f.form_client_status;
237 if (cs.type) { // cs = select list
238 cs.onchange = function () { client_status_changed(); };
240 else { // cs = array of radio buttons
241 for (var i = 0; i < cs.length; ++i) {
242 cs[i].onclick = function () { client_status_changed(); };
245 f.onsubmit = function () { return mysubmit(); };
249 // Generate default for client status.
251 function LBFgcac_default_client_status()
253 return _LBFgcac_recent_default('client_status');
256 // Generate default for visit type. If there are no recent prior visits,
257 // then default to new procedure.
259 function LBFgcac_default_ab_location()
261 global $formid;
262 if ($formid) {
263 return '';
266 $vt = _LBFgcac_recent_default('ab_location');
267 if (empty($vt)) {
268 return 'proc';
271 return $vt;
274 // Generate default for the induced procedure type.
276 function LBFgcac_default_in_ab_proc()
279 // Check previous GCAC visit forms for this setting.
280 $default = _LBFgcac_recent_default('in_ab_proc');
281 if ($default !== '') {
282 return $default;
285 // If none, query services from recent visits to see if an IPPF code
286 // matches that of a procedure type in the list.
287 $res = sqlStatement(_LBFgcac_query_recent_services());
288 while ($row = sqlFetchArray($res)) {
289 if (empty($row['related_code'])) {
290 continue;
293 $relcodes = explode(';', $row['related_code']);
294 foreach ($relcodes as $codestring) {
295 if ($codestring === '') {
296 continue;
299 list($codetype, $code) = explode(':', $codestring);
300 if ($codetype !== 'IPPF') {
301 continue;
304 $lres = sqlStatement("SELECT option_id, mapping FROM list_options " .
305 "WHERE list_id = 'in_ab_proc' AND activity = 1");
306 while ($lrow = sqlFetchArray($lres)) {
307 $maparr = explode(':', $lrow['mapping']);
308 if (empty($maparr[1])) {
309 continue;
312 if (preg_match('/^' . $maparr[1] . '/', $code)) {
313 return $lrow['option_id'];
316 } // end foreach
319 return '';
322 // Generate default for the main complication.
324 function LBFgcac_default_main_compl()
326 return _LBFgcac_recent_default('main_compl');