Integrating the problem list improvements with the recent fee sheet improvements...
[openemr.git] / interface / forms / fee_sheet / review / fee_sheet_queries.php
blob2b074d99ac676aaf951412ad058b3acbb1faf2d9
1 <?php
2 /**
3 * library of functions useful for searching and updating fee sheet related
4 * information
5 *
6 * Copyright (C) 2013 Kevin Yeh <kevin.y@integralemr.com> and OEMR <www.oemr.org>
8 * LICENSE: This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
19 * @package OpenEMR
20 * @author Kevin Yeh <kevin.y@integralemr.com>
21 * @link http://www.open-emr.org
23 require_once('fee_sheet_classes.php');
24 require_once("$srcdir/../custom/code_types.inc.php");
25 require_once("$srcdir/../library/lists.inc");
26 require_once("code_check.php");
28 /**
29 * update issues from list of diagnosis
31 * This function checks to see if a given list of diagnoses is already
32 * associated with the given encounter. Each diagnoses which has a
33 * corresponding entry in the problem list, but isn't already associated gets
34 * flagged. (update to issue_encounter table)
36 * If the $create parameter is true, any diagnosis which doesn't have a
37 * corresponding problem list entry has one created.
39 * @param int $pid the ID of the patient
40 * @param int $encounter the encounter ID
41 * @param array $diags a list of diagnoses
42 * @param bool $create if set issue not already in the patient problem list will be created
44 function update_issues($pid,$encounter,$diags)
46 $list_touched=false; // flag to determine if we have actually affected the medical_problem list.
47 $sqlEncounterDate=('select date FROM form_encounter where encounter=?');
48 $res=sqlQuery($sqlEncounterDate,array($encounter));
49 $target_date=$res['date'];
50 $lists_params=array();
51 $encounter_params=array();
52 $sqlUpdateIssueDescription="UPDATE lists SET title=?, modifydate=NOW() WHERE id=? AND TITLE!=?";
54 $sqlFindProblem = "SELECT id, title FROM lists WHERE ";
55 $sqlFindProblem.= " ( (`begdate` IS NULL) OR (`begdate` IS NOT NULL AND `begdate`<=?) ) AND " ; array_push($lists_params,$target_date);
56 $sqlFindProblem.= " ( (`enddate` IS NULL) OR (`enddate` IS NOT NULL AND `enddate`>=?) ) "; array_push($lists_params,$target_date);
57 $sqlFindProblem.= " AND pid=? AND diagnosis like ? "; array_push($lists_params,$pid);
58 array_push($lists_params,"");
60 $idx_diagnosis=count($lists_params)-1;
62 $sqlFindIssueEncounter= "SELECT encounter FROM issue_encounter WHERE pid=? AND encounter=? AND list_id=?";array_push($encounter_params,$pid,$encounter);
63 array_push($encounter_params,"");
65 $sqlCreateIssueEncounter = " INSERT into issue_encounter(pid,list_id,encounter)values (?,?,?) ";
67 $sqlCreateProblem = " INSERT into lists(date,begdate,type,occurrence,classification,pid,diagnosis,title,modifydate) values(?,?,'medical_problem',0,0,?,?,?,NOW())";
68 $idx_list_id=count($encounter_params)-1;
69 foreach($diags as $diags)
71 // ensure that a problem is allowed to be created from the diagnostic element
72 if ($diags->allowed_to_create_problem_from_diagnosis != "TRUE") continue;
74 $diagnosis_key=$diags->code_type.":".$diags->code;
75 $list_id=null;
76 if($diags->db_id!=null)
78 // If we got a the id for the problem passed in, then use it.
79 $list_id=$diags->db_id;
81 else
83 // If not, then search the database for it.
84 $lists_params[$idx_diagnosis]='%'.$diagnosis_key.'%';
85 $res=sqlStatement($sqlFindProblem,$lists_params);
86 if(sqlNumRows($res)>0)
88 $list_id=$res->fields['id'];
92 if(!($list_id==null))
94 // We found a problem corresponding to this diagnosis
95 $encounter_params[$idx_list_id]=$list_id;
96 $issue_encounter=sqlStatement($sqlFindIssueEncounter,$encounter_params);
97 if(sqlNumRows($issue_encounter)==0)
99 // An issue encounter entry didn't exist, so create it
100 sqlInsert($sqlCreateIssueEncounter,array($pid,$list_id,$encounter));
102 // Check the description in the problem
103 sqlStatement($sqlUpdateIssueDescription,array($diags->description,$list_id,$diags->description));
104 $list_touched=true; // Since there is already medical_problem listed, then the list has been touched in the past, so make sure it's flagged correctly
106 else
108 // No Problem found for this diagnosis
109 if($diags->create_problem) // TODO: per entry create
111 // If the create flag is set, then create an entry for this diagnosis.
112 sqlInsert($sqlCreateProblem,array($target_date,$target_date,$pid,$diagnosis_key,$diags->description));
113 $newProblem=sqlStatement($sqlFindProblem,$lists_params); // requerying the database for the newly created ID, instead of using the sqlInsert return value for backwards compatbility with 4.1.0 and earlier insert ID bug.
114 if(sqlNumRows($newProblem)>0)
116 $list_id=$newProblem->fields['id'];
117 if($list_id>0)
119 sqlInsert($sqlCreateIssueEncounter,array($pid,$list_id,$encounter));
122 $list_touched=true; // Since we are creating a new problem, the list has been touched
126 if($list_touched)
128 // If the list was touched at any point by this code, then flag it in the DB.
129 setListTouch($pid,'medical_problem');
134 * create diagnosis entries in the billing table
136 * this function checks for billing table entries corresponding to the given
137 * list of diagnoses. It creates an entry for any diagnosis not already listed
138 * on the fee sheet for the given encounter.
140 * @param int $req_pid the ID of the patient
141 * @param int $req_encounter the encounter ID
142 * @param array $diags a list of diagnoses
144 function create_diags($req_pid,$req_encounter,$diags)
146 $authorized=1;// Need to fix this. hard coded for now
147 $provid="";
148 $rowParams="(NOW(), ?, ?, ?, ?,". // date, encounter, code_type,code, code_text
149 " ?, ?, ?, ?,". // pid, authorized, user, groupname
150 "1, 0, ?," . // activity, billed, provider_id
151 " '', '', '0.00', '', '', '')"; // modifier, units,fee,ndc_info,justify,notecodes
152 $sqlCreateDiag = "insert into billing (date, encounter, code_type, code, code_text, " .
153 "pid, authorized, user, groupname, activity, billed, provider_id, " .
154 "modifier, units, fee, ndc_info, justify, notecodes) values ";
155 $sqlCreateDiag.=$rowParams;
157 $sqlUpdateDescription = "UPDATE billing SET code_text=? WHERE id=?";
158 $findRow= " SELECT id,code_text FROM billing where activity=1 AND encounter=? AND pid=? and code_type=? and code=?";
159 foreach($diags as $diag)
161 $find_params=array($req_encounter,$req_pid,$diag->getCode_type(),$diag->getCode());
162 $search=sqlStatement($findRow,$find_params);
163 $count=sqlNumRows($search);
164 if($count==0)
166 $bound_params=array();
167 array_push($bound_params,$req_encounter);
168 $diag->addArrayParams($bound_params);
169 array_push($bound_params,$req_pid,$authorized,$_SESSION['authId'],$_SESSION['authProvider'],$provid);
170 $res=sqlInsert($sqlCreateDiag,$bound_params);
172 else
174 // update the code_text;
175 $billing_entry=sqlFetchArray($search);
176 $code_text=$billing_entry['code_text'];
177 if($code_text!=$diag->description)
179 sqlStatement($sqlUpdateDescription,array($diag->description,$billing_entry['id']));
186 * create procedure entries in the billing table
188 * this function checks for billing table entries corresponding to the given
189 * list of procedures. It creates an entry for any procedure not already listed
190 * on the fee sheet for the given encounter
192 * @param int $req_pid the ID of the patient
193 * @param int $req_encounter the encounter ID
194 * @param array $procs a list of procedures
196 function create_procs($req_pid,$req_encounter,$procs)
198 $authorized=1;// Need to fix this. hard coded for now
199 $provid="";
200 $sql = "insert into billing (".
201 "date, encounter, code_type, code,".
202 "code_text, pid, authorized, user,".
203 "groupname, activity, billed, provider_id, " .
204 "modifier, units, fee, ndc_info, ".
205 "justify, notecodes".
206 ") values ";
207 $param="(NOW(),?,?,?,". // date, encounter, code_type, code
208 "?,?,?,?,". // code_text,pid,authorized,user
209 "?,1,0,?,". // groupname,activity,billed,provider_id
210 "?,?,?,'',". // modifier, units, fee, ndc_info
211 "?,'')"; // justify, notecodes
212 foreach($procs as $proc)
214 $insert_params=array();
215 array_push($insert_params,$req_encounter);
216 $proc->addArrayParams($insert_params);
217 array_push($insert_params,$req_pid,$authorized,$_SESSION['authId'],$_SESSION['authProvider'],$provid);
218 $proc->addProcParameters($insert_params);
219 sqlInsert($sql.$param,$insert_params);
224 * retrieve the diagnoses from the given patient's problem list
226 * All the problems are included, but problems which have been
227 * flagged as part of the given encounter are marked as such and
228 * also sorted earlier in the list.
230 * @param int $pid the ID of the patient
231 * @param int $encounter the encounter ID
232 * @return array - returns an array of the diagnoses
234 function issue_diagnoses($pid,$encounter)
236 $retval=array();
237 $parameters=array($encounter,$pid);
238 $sql= "SELECT l.diagnosis as diagnosis,l.title as title, NOT ISNULL(ie.encounter) as selected, l.id ".
239 " FROM lists as l" .
240 " LEFT JOIN issue_encounter as ie ON ie.list_id=l.id AND ie.encounter=?".
241 " WHERE l.type='medical_problem'".
242 " AND l.pid=?" . // 1st parameter pid
243 " AND ( (l.begdate IS NULL) OR (l.begdate IS NOT NULL AND l.begdate<=NOW()) ) AND ".
244 " ( ( l.enddate IS NULL) OR (l.enddate IS NOT NULL AND l.enddate>=NOW()) ) ".
245 " ORDER BY ie.encounter DESC,l.id";
246 $results=sqlStatement($sql,$parameters);
247 while( $res=sqlFetchArray($results))
249 $title=$res['title'];
250 $db_id=$res['id'];
251 $codes=explode(";",$res['diagnosis']);
252 foreach($codes as $code_key)
254 $diagnosis=explode(":",$code_key);
255 $code=$diagnosis[1];
256 $code_type=$diagnosis[0];
257 $new_info=new code_info($code,$code_type,$title,$res['selected']!=0);
259 //ensure that a diagnostic element is allowed to be created from a problem element
260 if ($new_info->allowed_to_create_diagnosis_from_problem != "TRUE") continue;
262 $new_info->db_id=$db_id;
263 $retval[]=$new_info;
267 return $retval;
271 * retrieve the most common diagnoses
273 * queries the billing table for the most frequently used diagnosis codes.
275 * @param int $limit the max number of rows to return
276 * @return array - returns an array of the diagnoses
280 function common_diagnoses($limit=10)
282 $retval=array();
283 $parameters=array($limit);
284 $sql="SELECT code_type, code, code_text,count(code) as num " .
285 " FROM billing WHERE code_type in (".diag_code_types('keylist',true).")" . // include all code types
286 " GROUP BY code_type,code,code_text ORDER BY num desc LIMIT ?";
287 $results=sqlStatement($sql,$parameters);
288 while($res=sqlFetchArray($results))
290 $title=$res['code_text'];
291 $code=$res['code'];
292 $code_type=$res['code_type'];
293 $retval[]=new code_info($code,$code_type,$title,0);
295 return $retval;
299 * retrieve the entries for the specified encounter's fee sheet
302 * @param int $pid the ID of the patient
303 * @param int $encounter the encounter ID
304 * @param array &$diagnoses return by reference of all the diagnoses
305 * @param array &$procedures return by reference of all the procedures
308 function fee_sheet_items($pid,$encounter, &$diagnoses,&$procedures)
310 $param=array($encounter);
311 $sql="SELECT code,code_type,code_text,fee,modifier,justify,units,ct_diag,ct_fee,ct_mod "
312 ." FROM billing, code_types as ct "
313 ." WHERE encounter=? AND billing.activity>0 AND ct.ct_key=billing.code_type "
314 ." ORDER BY id";
315 $results=sqlStatement($sql,$param);
316 while($res=sqlFetchArray($results))
318 $code=$res['code'];
319 $code_type=$res['code_type'];
320 $code_text=$res['code_text'];
321 if($res['ct_diag']=='1')
323 $diagnoses[]=new code_info($code,$code_type,$code_text);
325 else if($res['ct_fee']==1)
327 $fee=$res['fee'];
328 $justify=$res['justify'];
329 $modifiers=$res['modifier'];
330 $units=$res['units'];
331 $selected=true;
332 $mod_size=$res['ct_mod'];
333 $procedures[]=new procedure($code,$code_type,$code_text,$fee,$justify,$modifiers,$units,$mod_size,$selected);
340 * retrieve the details of the specified patient's encounters, except for the
341 * current (specified by $encounter)
343 * @param int $pid the ID of the patient
344 * @param int $encounter the encounter ID
346 function select_encounters($pid,$encounter)
348 $retval=array();
349 $parameters=array($pid,$encounter);
350 $sql="SELECT DATE(date) as date,encounter " .
351 " FROM form_encounter " .
352 " WHERE pid=? and encounter!=? " .
353 " ORDER BY date DESC";
354 $results=sqlStatement($sql,$parameters);
355 while($res=sqlFetchArray($results))
357 $retval[]=new encounter_info($res['encounter'],$res['date']);
359 return $retval;
363 * Update the justify field for the given billing entry
367 * @param int $pid the ID of the patient
368 * @param int $enc the encounter ID
369 * @param array $diags the list of justification codes
370 * @param int $billing_id the identifier in the billing table of the
371 * row to update
373 function update_justify($pid,$enc,$diags,$billing_id)
375 $justify="";
376 foreach($diags as $diag)
378 $justify.=$diag->getKey().":";
380 $sqlUpdate=" UPDATE billing SET justify=? "
381 ." WHERE id=?";
382 $params=array($justify,$billing_id);
383 sqlStatement($sqlUpdate,$params);