Added ability to click a column heading to sort on that column.
[openemr.git] / sites / default / LBF / LBFgcac.plugin.php
blobe5849a27482b4c47f6497ca0343ea15d0089f915
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; // true to disable complications
136 var dis2 = false; // true to disable procedures
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' || csval == 'defer') {
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 == 'form_ab_location') {
168 if (csval == 'maaa') {
169 e.disabled = (e.value == 'part' || e.value == 'oth' || e.value == 'na');
171 else if (csval == 'mara' || csval == 'defer' || csval == 'self') {
172 e.disabled = true; // (e.value != 'na');
174 // else if (csval == 'refout') {
175 // e.disabled = (e.value == 'proc' || e.value == 'ma');
176 // }
177 else { // inbound referral
178 e.disabled = (e.value == 'na' || e.value == 'proc' || e.value == 'ma');
181 else if (e.name == 'form_gc_rreason') {
182 e.disabled = (csval != 'mara' && csval != 'refout');
188 echo "
189 // Enable some form fields before submitting.
190 // This is because disabled fields do not submit their data, however
191 // we do want to save the default values that were set for them.
192 function mysubmit() {
193 var f = document.forms[0];
194 for (var i = 0; i < f.elements.length; ++i) {
195 var e = f.elements[i];
196 if (e.name == 'form_in_ab_proc') {
197 e.disabled = false;
200 top.restoreSession();
201 return true;
207 // The purpose of this function is to create JavaScript that is run
208 // once when the page is loaded.
210 function LBFgcac_javascript_onload() {
211 echo "
212 set_main_compl_list();
213 client_status_changed();
214 var f = document.forms[0];
215 for (var i = 0; i < f.elements.length; ++i) {
216 var e = f.elements[i];
217 if (e.name.substring(0,18) == 'form_complications')
218 e.onclick = function () { set_main_compl_list(); };
220 var cs = f.form_client_status;
221 if (cs.type) { // cs = select list
222 cs.onchange = function () { client_status_changed(); };
224 else { // cs = array of radio buttons
225 for (var i = 0; i < cs.length; ++i) {
226 cs[i].onclick = function () { client_status_changed(); };
229 f.onsubmit = function () { return mysubmit(); };
233 // Generate default for client status.
235 function LBFgcac_default_client_status() {
236 return _LBFgcac_recent_default('client_status');
239 // Generate default for visit type. If there are no recent prior visits,
240 // then default to new procedure.
242 function LBFgcac_default_ab_location() {
243 global $formid;
244 if ($formid) return '';
245 $vt = _LBFgcac_recent_default('ab_location');
246 if (empty($vt)) return 'proc';
247 return $vt;
250 // Generate default for the induced procedure type.
252 function LBFgcac_default_in_ab_proc() {
254 // Check previous GCAC visit forms for this setting.
255 $default = _LBFgcac_recent_default('in_ab_proc');
256 if ($default !== '') return $default;
258 // If none, query services from recent visits to see if an IPPF code
259 // matches that of a procedure type in the list.
260 $res = sqlStatement(_LBFgcac_query_recent_services());
261 while ($row = sqlFetchArray($res)) {
262 if (empty($row['related_code'])) continue;
263 $relcodes = explode(';', $row['related_code']);
264 foreach ($relcodes as $codestring) {
265 if ($codestring === '') continue;
266 list($codetype, $code) = explode(':', $codestring);
267 if ($codetype !== 'IPPF') continue;
268 $lres = sqlStatement("SELECT option_id, mapping FROM list_options " .
269 "WHERE list_id = 'in_ab_proc'");
270 while ($lrow = sqlFetchArray($lres)) {
271 $maparr = explode(':', $lrow['mapping']);
272 if (empty($maparr[1])) continue;
273 if (preg_match('/^' . $maparr[1] . '/', $code)) {
274 return $lrow['option_id'];
277 } // end foreach
280 return '';
283 // Generate default for the main complication.
285 function LBFgcac_default_main_compl() {
286 return _LBFgcac_recent_default('main_compl');