fix: set default x12 partner for item in billing manager (#7513)
[openemr.git] / sites / default / LBF / LBFgcac.plugin.php
blob4218490b5d23071ab0e8a7dee2114071a434affb
1 <?php
3 // Copyright (C) 2009-2011, 2016 Rod Roark <rod@sunsetsystems.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This provides enhancement functions for the LBFgcac visit form.
11 // It is invoked by interface/forms/LBF/new.php.
13 // Private function. Constructs a query to find a given lbf_data field's
14 // values from visits within the past 2 weeks.
16 function _LBFgcac_query_recent($more)
18 global $pid, $encounter, $formname, $formid;
20 // Get the date of this visit.
21 $encrow = sqlQuery("SELECT date FROM form_encounter WHERE " .
22 "pid = ? AND encounter = ?", [$pid, $encounter]);
23 $encdate = $encrow['date'];
25 // Query complications from the two weeks prior to this visit.
26 $query = "SELECT d.field_value " .
27 "FROM forms AS f, form_encounter AS fe, lbf_data AS d " .
28 "WHERE f.pid = '" . add_escape_custom($pid) . "' AND " .
29 "f.formdir = '" . add_escape_custom($formname) . "' AND " .
30 "f.deleted = 0 AND " .
31 "fe.pid = f.pid AND fe.encounter = f.encounter AND " .
32 "fe.date <= '" . add_escape_custom($encdate) . "' AND " .
33 "DATE_ADD(fe.date, INTERVAL 14 DAY) > '" . add_escape_custom($encdate) . "' AND " .
34 "d.form_id = f.form_id AND $more";
36 return $query;
39 // Private function. Given a field name, gets its value from the most
40 // recent instance of this form type that is not more than 2 weeks old.
42 function _LBFgcac_recent_default($name)
44 global $formid;
46 // This logic only makes sense for a new form.
47 if ($formid) {
48 return '';
51 $query = _LBFgcac_query_recent(
52 "d.field_id = ? " .
53 "ORDER BY f.form_id DESC LIMIT 1"
55 $row = sqlQuery($query, [$name]);
57 if (empty($row['field_value'])) {
58 return '';
61 return $row['field_value'];
64 // Private function. Query services within 2 weeks of this encounter.
66 function _LBFgcac_query_recent_services()
68 global $pid, $encounter;
70 // Get the date of this visit.
71 $encrow = sqlQuery("SELECT date FROM form_encounter WHERE " .
72 "pid = ? AND encounter = ?", [$pid, $encounter]);
73 $encdate = $encrow['date'];
75 // Query services from the two weeks prior to this visit.
76 $query = "SELECT c.related_code " .
77 "FROM form_encounter AS fe, billing AS b, codes AS c " .
78 "WHERE fe.pid = '" . add_escape_custom($pid) . "' AND fe.date <= '" . add_escape_custom($encdate) . "' AND " .
79 "DATE_ADD(fe.date, INTERVAL 14 DAY) > '" . add_escape_custom($encdate) . "' AND " .
80 "b.pid = fe.pid AND b.encounter = fe.encounter AND b.activity = 1 AND " .
81 "b.code_type = 'MA' AND c.code_type = '12' AND " .
82 "c.code = b.code AND c.modifier = b.modifier " .
83 "ORDER BY fe.date DESC, b.id DESC";
85 return $query;
88 // Private function. Query services from this encounter.
90 function _LBFgcac_query_current_services()
92 global $pid, $encounter;
94 $query = "SELECT c.related_code " .
95 "FROM billing AS b, codes AS c WHERE " .
96 "b.pid = '" . add_escape_custom($pid) . "' AND b.encounter = '" . add_escape_custom($encounter) . "' AND b.activity = 1 AND " .
97 "b.code_type = 'MA' AND c.code_type = '12' AND " .
98 "c.code = b.code AND c.modifier = b.modifier " .
99 "ORDER BY b.id DESC";
101 return $query;
104 // The purpose of this function is to create JavaScript for the <head>
105 // section of the page. This in turn defines desired javaScript
106 // functions.
108 function LBFgcac_javascript()
110 global $formid;
112 // Query complications from the two weeks prior to this visit.
113 $res = sqlStatement(_LBFgcac_query_recent(
114 "f.form_id != '" . add_escape_custom($formid) . "' AND " .
115 "d.field_id = 'complications'"
118 // This JavaScript function is to enable items in the "Main complications"
119 // list that have been selected, and to disable all others.
120 // Option.disabled seems to work for Firefox 3 and IE8 but not IE6.
121 echo "// Enable recent complications and disable all others.
122 function set_main_compl_list() {
123 var f = document.forms[0];
124 var sel = f.form_main_compl;
125 var n = '';
127 // We use the checkbox object values as a scratch area to note which
128 // complications were already selected from other forms.
129 while ($row = sqlFetchArray($res)) {
130 $a = explode('|', $row['field_value']);
131 foreach ($a as $complid) {
132 if (empty($complid)) {
133 continue;
136 echo " n = 'form_complications[" . attr($complid) . "]'; if (f[n]) f[n].value = 2;\n";
140 echo " // Scan the list items and set their disabled flags.
141 for (var i = 1; i < sel.options.length; ++i) {
142 n = 'form_complications[' + sel.options[i].value + ']';
143 sel.options[i].disabled = (f[n] && (f[n].checked || f[n].value == '2')) ? false : true;
148 echo "
149 // Disable most form fields if refusing abortion.
150 function client_status_changed() {
151 var f = document.forms[0];
152 var dis1 = false; // true to disable complications
153 var dis2 = false; // true to disable procedures
154 var cs = f.form_client_status;
155 var csval = '';
156 if (cs.type) { // cs = select list
157 if (cs.selectedIndex >= 0) {
158 csval = cs.options[cs.selectedIndex].value;
161 else { // cs = array of radio buttons
162 for (var i = 0; i < cs.length; ++i) {
163 if (cs[i].checked) {
164 csval = cs[i].value;
165 break;
169 if (csval == 'mara' || csval == 'defer' || csval == 'refin') {
170 dis1 = true;
171 dis2 = true;
173 else if (csval == 'maaa') {
174 dis2 = true;
176 for (var i = 0; i < f.elements.length; ++i) {
177 var e = f.elements[i];
178 if (e.name.substring(0,18) == 'form_complications' || e.name == 'form_main_compl') {
179 e.disabled = dis1;
181 else if (e.name == 'form_in_ab_proc') {
182 e.disabled = dis2;
184 else if (e.name == 'form_ab_location') {
185 if (csval == 'maaa') {
186 e.disabled = (e.value == 'part' || e.value == 'oth' || e.value == 'na');
188 else if (csval == 'mara' || csval == 'defer' || csval == 'self') {
189 e.disabled = true; // (e.value != 'na');
191 // else if (csval == 'refout') {
192 // e.disabled = (e.value == 'proc' || e.value == 'ma');
193 // }
194 else { // inbound referral
195 e.disabled = (e.value == 'na' || e.value == 'proc' || e.value == 'ma');
198 else if (e.name == 'form_gc_rreason') {
199 e.disabled = (csval != 'mara' && csval != 'refout');
205 echo "
206 // Enable some form fields before submitting.
207 // This is because disabled fields do not submit their data, however
208 // we do want to save the default values that were set for them.
209 function mysubmit() {
210 var f = document.forms[0];
211 for (var i = 0; i < f.elements.length; ++i) {
212 var e = f.elements[i];
213 if (e.name == 'form_in_ab_proc') {
214 e.disabled = false;
217 top.restoreSession();
218 return true;
223 // The purpose of this function is to create JavaScript that is run
224 // once when the page is loaded.
226 function LBFgcac_javascript_onload()
228 echo "
229 set_main_compl_list();
230 client_status_changed();
231 var f = document.forms[0];
232 for (var i = 0; i < f.elements.length; ++i) {
233 var e = f.elements[i];
234 if (e.name.substring(0,18) == 'form_complications')
235 e.onclick = function () { set_main_compl_list(); };
237 var cs = f.form_client_status;
238 if (cs.type) { // cs = select list
239 cs.onchange = function () { client_status_changed(); };
241 else { // cs = array of radio buttons
242 for (var i = 0; i < cs.length; ++i) {
243 cs[i].onclick = function () { client_status_changed(); };
246 f.onsubmit = function () { return mysubmit(); };
250 // Generate default for client status.
252 function LBFgcac_default_client_status()
254 return _LBFgcac_recent_default('client_status');
257 // Generate default for visit type. If there are no recent prior visits,
258 // then default to new procedure.
260 function LBFgcac_default_ab_location()
262 global $formid;
263 if ($formid) {
264 return '';
267 $vt = _LBFgcac_recent_default('ab_location');
268 if (empty($vt)) {
269 return 'proc';
272 return $vt;
275 // Generate default for the induced procedure type.
277 function LBFgcac_default_in_ab_proc()
280 // Check previous GCAC visit forms for this setting.
281 $default = _LBFgcac_recent_default('in_ab_proc');
282 if ($default !== '') {
283 return $default;
286 // If none, query services from recent visits to see if an IPPF code
287 // matches that of a procedure type in the list.
288 $res = sqlStatement(_LBFgcac_query_recent_services());
289 while ($row = sqlFetchArray($res)) {
290 if (empty($row['related_code'])) {
291 continue;
294 $relcodes = explode(';', $row['related_code']);
295 foreach ($relcodes as $codestring) {
296 if ($codestring === '') {
297 continue;
300 list($codetype, $code) = explode(':', $codestring);
301 if ($codetype !== 'IPPF') {
302 continue;
305 $lres = sqlStatement("SELECT option_id, mapping FROM list_options " .
306 "WHERE list_id = 'in_ab_proc' AND activity = 1");
307 while ($lrow = sqlFetchArray($lres)) {
308 $maparr = explode(':', $lrow['mapping']);
309 if (empty($maparr[1])) {
310 continue;
313 if (preg_match('/^' . $maparr[1] . '/', $code)) {
314 return $lrow['option_id'];
317 } // end foreach
320 return '';
323 // Generate default for the main complication.
325 function LBFgcac_default_main_compl()
327 return _LBFgcac_recent_default('main_compl');