2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2011 Z&H Consultancy Services Private Limited <sam@zhservices.com>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // 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.
18 // A copy of the GNU General Public License is included along with this program:
19 // openemr/interface/login/GnuGPL.html
20 // For more information write to the Free Software
21 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 // Author: Eldho Chacko <eldho@zhservices.com>
24 // Jacob T Paul <jacob@zhservices.com>
26 // +------------------------------------------------------------------------------+
34 //this will return the query string along with the parameter array, according to the case case.
35 //actual execution is done in the select_query function in Server_side
38 public function query_formation($data)
42 // Entries pending for approval for Existing Patient and New Patient.
44 $query= "select * from audit_master where pid=? and approval_status='1' and (type='1' or type='2')";
45 return array($query,array($pid));
47 // Entries pending for approval for documents only (no demo change).
49 $query = " select * from audit_master where pid=? and approval_status='1' and type='3' ";
50 return array($query,array($pid));
53 $query = "SELECT fname FROM patient_data WHERE pid=?";
54 return array($query,array($pid));
57 $query= "select MAX(pid)+1 AS pid from patient_data ";
61 //for building patient Demo
63 $query="select * from layout_options " .
64 "WHERE form_id = 'DEM' AND uor > 0 AND field_id != '' " .
65 "ORDER BY group_name, seq";
68 //for building patient Demo Date of Birth
70 $query="select *, DATE_FORMAT(DOB,'%Y-%m-%d') as DOB_YMD from patient_data where pid=? order by date DESC limit 0,1 ";
71 return array($query,array($pid));
73 //for building patient Demo Employer Data
75 $query="select * from employer_data where pid=? order by date DESC limit 0,1 ";
76 return array($query,array($pid));
78 //for building patient Demo Insurance company details for Patient
80 $query=" select insd.*, ic.name as provider_name from insurance_data as insd " .
81 "left join insurance_companies as ic on ic.id = insd.provider " .
82 "where pid = ? and type =? order by date DESC limit 1 ";
83 array_unshift($data[1], $pid);
84 return array($query,$data[1]);
86 // Entries pending for approval demo and documents.
88 $query=" select * from audit_master as am,audit_details as ad WHERE am.id=ad.audit_master_id and am.pid=? and am.approval_status='1'
89 and (am.type='1' or am.type='2' or am.type='3') order by ad.id";
90 return array($query,array($pid));
92 // Demo building from layout options.
94 $query=" select * from layout_options WHERE form_id = 'DEM' AND uor > 0 AND field_id != '' " .
95 " ORDER BY group_name, seq";
99 //Global specific application for building demo.
101 $query="select * from globals where gl_name ='specific_application' ";
102 return array($query);
104 //Global omit employers for building demo.
106 $query=" select * from globals where gl_name ='omit_employers' ";
107 return array($query);
109 //getting the password
111 $query = "select portal_username from patient_access_offsite where portal_username =? ";
112 return array($query,$data[1]);
116 $x=array($data[1][0]);
117 $query="select count(*) AS count from patient_data where pubpid = ?";
118 return array($query,$x);
120 //getting DOB and SSN for verifying the duplicate patient existance
122 $x=array($data[1][0]);
123 $query="select ss,DOB from patient_data where DOB=?";
124 return array($query,$x);
126 //master data for calendar from Globals
128 if ($data[1][0]=='calendar_interval'||
$data[1][0]=='schedule_start'||
$data[1][0]=='schedule_end') {
129 $query="select gl_value from globals where gl_name=?";
130 return array($query,$data[1]);
137 //Check whether an entry exist in the form Encounter to decide whether patient is an existing patient.
138 $query="select COUNT(*) AS cnt from form_encounter WHERE pid=?";
139 return array($query,array($pid));
143 //Existing appointments for a patient
144 array_unshift($data[1], $pid);
145 $query="select pc_eid,pc_eventDate,pc_startTime,pc_endTime,fname,lname,name,pc_apptstatus from openemr_postcalendar_events AS c,
146 users AS u,facility AS f WHERE pc_pid=? AND pc_aid=u.id AND pc_facility=f.id AND pc_apptstatus!=? order by pc_eventDate desc";
147 return array($query,$data[1]);
151 //Appointments pending for approval
152 array_push($data[1], $pid);
153 $query="select am.id,am.approval_status,ad.audit_master_id,ad.field_name,ad.field_value,u.fname,u.lname,f.name from audit_master AS am,
154 audit_details AS ad LEFT JOIN users AS u ON ad.field_value=u.id AND ad.field_name=? LEFT JOIN facility AS f ON ad.field_value=f.id AND
155 ad.field_name=? WHERE am.pid=? AND am.id=ad.audit_master_id AND am.type='10' AND am.approval_status NOT IN ('2','4')
156 ORDER BY approval_status, am.id desc,ad.id desc";
157 return array($query,$data[1]);
161 //patient appointment history
162 array_unshift($data[1], $pid);
163 $query="select pc_eid,pc_eventDate,pc_startTime,pc_endTime,fname,lname,name,pc_apptstatus from openemr_postcalendar_events AS c,
164 users AS u,facility AS f WHERE pc_pid=? AND pc_aid=u.id AND pc_facility=f.id AND pc_apptstatus=? order by pc_eventDate desc";
165 return array($query,$data[1]);
169 //List of Service Facility
170 $query="select * from facility where service_location != 0 and id in (".add_escape_custom($data[1][0]).") order by name";
171 return array($query);
176 $query="select id, lname, fname from users WHERE authorized = 1 AND username != '' AND username NOT LIKE '%Admin%' AND active = 1
177 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) ORDER BY lname, fname";
178 return array($query);
182 //Calendar default visit time for visit category. value for Admin--->others-->calendar
183 $query="select pc_duration from openemr_postcalendar_categories WHERE pc_catid = ?";
184 return array($query,$data[1]);
187 case 'B11'://This was previously a semicolon, assumed it was a bug and replaced with a colon
188 //patient appointment
189 $query="select pc_eventDate, pc_endDate, pc_startTime, pc_duration, pc_recurrtype, pc_recurrspec, pc_alldayevent, pc_catid,
190 pc_prefcatid from openemr_postcalendar_events WHERE pc_aid = ? AND ((pc_endDate >= ? AND pc_eventDate < ?) OR
191 (pc_endDate = '0000-00-00' AND pc_eventDate >= ? AND pc_eventDate < ?)) AND pc_facility = ?";
192 return array($query,$data[1]);
196 //Appointments pending for approval
197 $query="select * from audit_master WHERE pid =? AND type='10'";
198 return array($query,array($pid));
201 //G series for form menu inc
203 $query = "SELECT * FROM `documents_legal_categories` where dlc_category_name=? and dlc_category_type=2";
204 return array($query,$data[1]);
208 $query = "SELECT * FROM documents_legal_master AS dlm WHERE dlm_subcategory <> ? and dlm_effective_date <= now() AND
209 dlm_effective_date<>? AND dlm_upload_type = '0' AND dlm_document_id Not IN (SELECT distinct(dld_master_docid) FROM documents_legal_detail WHERE
210 dld_id IS NOT NULL AND dld_pid=?)";
211 array_push($data[1], $pid);
212 return array($query,$data[1]);
216 $query = "SELECT * FROM documents_legal_master AS dlm LEFT OUTER JOIN documents_legal_detail as dld ON
217 dlm_document_id=dld_master_docid WHERE dlm_subcategory <> ? and dlm_effective_date <= now() AND dlm_effective_date<>?
218 AND dld_id IS NOT NULL AND dld_signed=? AND dld_pid=? ORDER BY dlm_effective_date DESC";
219 array_push($data[1], $pid);
220 return array($query,$data[1]);
224 $query = "SELECT * FROM documents_legal_master AS dlm JOIN documents_legal_detail as dld ON dlm_document_id=dld_master_docid
225 JOIN form_encounter as fe ON encounter=dld_encounter WHERE dlm_subcategory = ? AND dlm_effective_date <= now() AND
226 dlm_effective_date<>? AND dld_id IS NOT NULL AND dld_signed=? AND dld_signing_person=? AND dld_pid=?
227 ORDER BY dlm_effective_date DESC";
228 array_push($data[1], $pid);
229 return array($query,$data[1]);
233 $query = "SELECT * FROM documents_legal_master AS dlm JOIN documents_legal_detail as dld ON dlm_document_id=dld_master_docid
234 JOIN form_encounter as fe ON encounter=dld_encounter WHERE dlm_subcategory = ? and dlm_effective_date <= now() AND
235 dlm_effective_date<>? AND dld_id IS NOT NULL AND dld_filename != '' AND dld_pid=? GROUP BY dld_encounter,dlm_document_id
236 ORDER BY dld_id DESC";
237 array_push($data[1], $pid);
238 return array($query,$data[1]);
242 $query = "SELECT * FROM documents_legal_master AS dlm LEFT OUTER JOIN documents_legal_detail as dld ON
243 dlm_document_id=dld_master_docid WHERE dlm_subcategory <> ? and dlm_effective_date <= now() AND dlm_effective_date<>?
244 AND dld_id IS NOT NULL AND (dld_signed = ? OR dlm_upload_type = '1') AND dld_pid=? ORDER BY dlm_effective_date DESC";
245 array_push($data[1], $pid);
246 return array($query,$data[1]);
251 $query="select * from patient_data where pid=?";
252 return array($query,array($pid));
256 //PDF forms detail selected
257 $query="select * from documents_legal_master where dlm_document_id=?";
258 return array($query,$data[1]);
263 array_unshift($data[1], $pid);
264 $query="select * from documents_legal_detail where dld_pid = ? and dld_signed='3' and dld_master_docid = ?";
265 return array($query,$data[1]);
270 $query="select * from documents_legal_master where dlm_document_id=?";
271 return array($query,$data[1]);
275 // Entries to be approved demo for new patient, existing patient and only documents
276 $query="select * from audit_master where pid=? and approval_status='1' and (type='1' or type='2' or type='3')";
277 return array($query,array($pid));
282 $query="select * from documents_legal_master WHERE dlm_document_id=?";
283 return array($query,$data[1]);
287 //Documents ready to be signed and documents unsigned
288 array_unshift($data[1], $pid);
289 $query="select * from documents_legal_detail where dld_pid=? and (dld_signed='2' or dld_signed='0') and dld_master_docid=?";
290 return array($query,$data[1]);
294 //Selection from master document for showing to patient
295 $query="select * from documents_legal_master WHERE dlm_document_name=?";
296 return array($query,$data[1]);