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 // +------------------------------------------------------------------------------+
28 //SANITIZE ALL ESCAPES
29 $sanitize_all_escapes=true;
32 //STOP FAKE REGISTER GLOBALS
33 $fake_register_globals=false;
36 class existingpatient
{
39 //this will return the query string along with the parameter array, according to the case case.
40 //actual execution is done in the select_query function in Server_side
42 public function query_formation($data){
46 //Select list of encounters for the patients
47 $query="select f.id, f.date, f.pid, f.encounter, f.stmt_count, f.last_stmt_date, f.facility_id,f.billing_facility, " .
48 "p.fname, p.mname, p.lname, p.street, p.city, p.state, p.postal_code " .
49 " ,u.fname as dfname, u.mname as dmname, u.lname as dlname".
50 " from ((form_encounter AS f, patient_data AS p) " .
51 " left join users as u on f.provider_id =u.id) ".
52 " left join facility as fa on fa.id =f.billing_facility ".
53 "WHERE ( f.pid=? ) AND " .
55 "ORDER BY f.pid,f.billing_facility, f.date desc, f.encounter desc";
56 return array($query,array($pid));
58 //Select list of providers
60 $query="Select distinct u.fname, u.mname, u.lname".
61 " from (form_encounter AS f, billing AS b) " .
62 " left join users as u on f.provider_id =u.id ".
63 " WHERE f.pid = b.pid and f.encounter = b.encounter " .
64 " and f.encounter=? and f.pid=? ".
65 " ORDER BY u.fname, u.lname";
66 array_push($data[1],$pid);
67 return array($query,array($data[1]));
69 //Select list of encounters for the patients
74 foreach($data[1][0] as $k=>$v)
76 $where .= " OR f.id = ?";
79 $where = substr($where, 4);
86 $query= "Select f.id, f.date, f.pid, f.encounter, f.stmt_count, f.last_stmt_date, f.facility_id,f.billing_facility, " .
87 "p.fname, p.mname, p.lname, p.street, p.city, p.state, p.postal_code " .
88 " ,u.fname as dfname, u.mname as dmname, u.lname as dlname".
89 " from ((form_encounter AS f, patient_data AS p) " .
90 " left join users as u on f.provider_id =u.id) ".
91 " left join facility as fa on fa.id =f.billing_facility ".
92 "WHERE ( $where) AND " .
93 "p.pid = f.pid and f.pid=?" .
94 "ORDER BY f.pid,f.billing_facility, f.date desc, f.encounter desc";
95 return array($query,$wherearray);
99 $query="select * from form_encounter where pid=? limit 1 ";
100 return array($query,array($pid));
104 include_once('../../library/formdata.inc.php');
105 $enc_set_array=array();
106 $enc_set_array[]=$pid;
107 if($data[1][1]=='' && $data[1][2]>0)
109 $enc_set= " and encounter=? " ;
110 $enc_set_array[]=$data[1][2];
113 $provider =add_escape_custom($data[1][0]);
114 $query="select fe.id,fe.pid,encounter,date_format(fe.date,'%Y-%m-%d')
115 as date,concat(pd.lname,' ',pd.fname) as patname,concat(u.lname,', ',u.fname)
116 as provname,".$provider." from form_encounter fe left outer join users u
117 on u.id =fe.".$provider." join patient_data pd on pd.pid=fe.pid where
118 fe.pid=?". $enc_set ." order by fe.date desc";
119 return array($query, $enc_set_array);
123 $enc_set_array=array();
124 $enc_set_array[]=$pid;
125 if($data[1][0]=='' && $data[1][1]>0)
127 $enc_set= " and encounter=? ";
128 $enc_set_array[]=$data[1][1];
130 $query="select encounter,sum(fee) as copay ".
131 " from billing where code_type='copay' and pid=? $enc_set group by encounter";
132 return array($query,$enc_set_array);
134 //DEtails of CPT, Diagnosis etc of an encounter
136 $enc_set_array=array();
137 $enc_set_array[]=$pid;
138 if($data[1][0]=='' && $data[1][1]>0)
140 $enc_set= " and encounter=? ";
141 $enc_set_array[]=$data[1][1];
144 $query="select concat(encounter,code,modifier) as ecm,encounter,code,
145 modifier,units,fee,code_text,justify from billing where activity=1 and fee>0 and code_type not in('ICD9','copay') and pid=? $enc_set";
146 return array($query,$enc_set_array);
148 //Payment details of an encounter
150 $enc_set_array=array();
151 $enc_set_array[]=$pid;
152 if($data[1][0]=='' && $data[1][1]>0)
154 $enc_set= " and encounter=? ";
155 $enc_set_array[]=$data[1][1];
157 $query="select concat(encounter,code,modifier) as pecm,encounter,code,
158 modifier,pay_amount,adj_amount,payer_type,post_time,account_code,
159 follow_up_note,memo,date_format(post_time,'%Y-%m-%d') as dtfrom from ar_activity where pid=? $enc_set";
160 return array($query,$enc_set_array);
163 $query = "SELECT sum(pay_total) as pay_total FROM ar_session WHERE patient_id=? AND adjustment_code=?";
164 return array($query,array($pid,'pre_payment'));
167 $query = "SELECT sum(pay_total) as pay_amount FROM ar_session,ar_activity WHERE patient_id=? AND adjustment_code=?
168 AND pid=? AND ar_session.session_id=ar_activity.session_id and pay_amount>0";
169 return array($query,array($pid,'pre_payment',$pid));
172 $query = "SELECT sum(pay_total) as pay_total FROM ar_session WHERE patient_id=? AND adjustment_code!=?";
173 return array($query,array($pid,'pre_payment'));
176 $query = "SELECT sum(pay_total) as pay_amount FROM ar_session,ar_activity WHERE patient_id=? AND adjustment_code!=?
177 AND pid=? AND ar_session.session_id=ar_activity.session_id and pay_amount>0";
178 return array($query,array($pid,'pre_payment',$pid));
181 // Entries pending for approval for Existing Patient and New Patient.
183 $query= "select * from audit_master where pid=? and approval_status='1' and (type='1' or type='2')";
184 return array($query,array($pid));
186 // Entries pending for approval for documents only (no demo change).
188 $query = " select * from audit_master where pid=? and approval_status='1' and type='3' ";
189 return array($query,$data[1]);
193 $query= "select MAX(pid)+1 AS pid from patient_data ";
195 return array($query);
198 //for building patient Demo
200 $query="select * from layout_options " .
201 "WHERE form_id = 'DEM' AND uor > 0 AND field_id != '' " .
202 "ORDER BY group_name, seq";
203 return array($query);
206 //for building patient Demo Date of Birth
208 $query="select *, DATE_FORMAT(DOB,'%Y-%m-%d') as DOB_YMD from patient_data where pid=? order by date DESC limit 0,1 ";
209 return array($query,array($pid));
212 //for building patient Demo Employer Data
214 $query="select * from employer_data where pid=? order by date DESC limit 0,1 ";
215 return array($query,array($pid));
218 //for building patient Demo Insurance company details for Patient
220 $query=" select insd.*, ic.name as provider_name from insurance_data as insd " .
221 "left join insurance_companies as ic on ic.id = insd.provider " .
222 "where pid = ? and type =? order by date DESC limit 1 ";
223 array_unshift($data[1],$pid);
224 return array($query,$data[1]);
227 // Entries pending for approval demo and documents.
229 $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'
230 and (am.type='1' or am.type='2' or am.type='3') order by ad.id";
231 return array($query,array($pid));
233 // Demo building from layout options.
236 $query=" select * from layout_options WHERE form_id = 'DEM' AND uor > 0 AND field_id != '' " .
237 "ORDER BY group_name, seq";
239 return array($query);
242 //Global specific application for building demo.
244 $query="select * from globals where gl_name ='specific_application' ";
245 return array($query);
248 //Global omit employers for building demo.
250 $query=" select * from globals where gl_name ='omit_employers' ";
251 return array($query);
253 //details of COpay and Fees
255 array_push($data[1],$pid);
256 $query= "select date, code_type, code, modifier, code_text, fee , units, justify from billing WHERE encounter =? AND pid = ? AND " .
257 " activity = 1 AND fee != 0.00 ORDER BY fee desc,code,modifier";
258 return array($query,$data[1]);
262 $query = "select code_text from codes WHERE code = ? ";
263 return array($query,$data[1]);
265 //Details of drug sales
267 array_push($data[1],$pid);
268 $query = "select s.drug_id, s.sale_date, s.fee, s.quantity from drug_sales AS s " .
269 "WHERE s.encounter = ? and s.pid = ? AND s.fee != 0 " .
270 "ORDER BY s.sale_id";
271 return array($query,$data[1]);
273 //Details of Payments
275 array_push($data[1],$pid);
276 $query = "Select a.code, a.modifier, a.memo, a.payer_type, a.adj_amount, a.pay_amount, " .
277 "a.post_time, a.session_id, a.sequence_no,a.follow_up, a.follow_up_note, " .
278 "s.payer_id, s.reference, s.check_date, s.deposit_date " .
279 ",i.name from ar_activity AS a " .
280 "LEFT OUTER JOIN ar_session AS s ON s.session_id = a.session_id " .
281 "LEFT OUTER JOIN insurance_companies AS i ON i.id = s.payer_id " .
282 "WHERE a.encounter = ? and a.pid = ? " .
283 "ORDER BY s.check_date, a.sequence_no";
284 return array($query,$data[1]);
286 //Address of Billing Facility
288 $query = "SELECT f.name,f.street,f.city,f.state,f.postal_code,f.phone from facility f " .
290 return array($query,$data[1]);
292 //Encounter status primary,secondary Etc
294 array_push($data[1],$pid);
295 $query = "select last_level_closed from form_encounter where encounter= ? and pid =? ";
296 return array($query,$data[1]);
300 $query = "select COUNT( DISTINCT TYPE ) NumberOfInsurance from insurance_data where pid =? and provider>0 ";
301 return array($query,array($pid));
305 $query = "select date,encounter from form_encounter where pid =? ORDER BY encounter";
306 return array($query,array($pid));
312 $string_query=" and pid !=?";
316 $x=array($data[1][0],$pid);
320 $x=array($data[1][0]);
322 $query="select count(*) AS count from patient_data where pubpid = ? $string_query";
323 return array($query,$x);
326 //getting DOB and SSN for verifying the duplicate patient existance
330 $string_query=" and pid !=?";
334 $x=array($data[1][0],$pid);
338 $x=array($data[1][0]);
340 $query="select ss,DOB from patient_data where DOB=? $string_query ";
341 return array($query,$x);
344 //master data for calendar from Globals
346 //patient appointment
347 if($data[1][0]=='calendar_interval'||
$data[1][0]=='schedule_start'||
$data[1][0]=='schedule_end')
349 $query="select gl_value from globals where gl_name=?";
350 return array($query,$data[1]);
357 //Check whether an entry exist in the form Encounter to decide whether patient is an existing patient.
358 $query="select COUNT(*) AS cnt from form_encounter WHERE pid=?";
359 return array($query,array($pid));
363 //Existing appointments for a patient
364 array_unshift($data[1],$pid);
365 $query="select pc_eid,pc_eventDate,pc_startTime,pc_endTime,fname,lname,name,pc_apptstatus from openemr_postcalendar_events AS c,
366 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";
367 return array($query,$data[1]);
371 //Appointments pending for approval
372 array_push($data[1],$pid);
373 $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,
374 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
375 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')
376 ORDER BY approval_status, am.id desc,ad.id desc";
377 return array($query,$data[1]);
381 //patient appointment history
382 array_unshift($data[1],$pid);
383 $query="select pc_eid,pc_eventDate,pc_startTime,pc_endTime,fname,lname,name,pc_apptstatus from openemr_postcalendar_events AS c,
384 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";
385 return array($query,$data[1]);
389 //List of Service Facility
390 include_once('../../library/formdata.inc.php');
391 $query="select * from facility where service_location != 0 and id in (".add_escape_custom($data[1][0]).") order by name";
392 return array($query);
397 $query="select id, lname, fname from users WHERE authorized = 1 AND username != '' AND username NOT LIKE '%Admin%' AND active = 1
398 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) ORDER BY lname, fname";
399 return array($query);
403 //Calendar default visit time for visit category. value for Admin--->others-->calendar
404 $query="select pc_duration from openemr_postcalendar_categories WHERE pc_catid = ?";
405 return array($query,$data[1]);
409 //patient appointment
410 $query="select pc_eventDate, pc_endDate, pc_startTime, pc_duration, pc_recurrtype, pc_recurrspec, pc_alldayevent, pc_catid,
411 pc_prefcatid from openemr_postcalendar_events WHERE pc_aid = ? AND ((pc_endDate >= ? AND pc_eventDate < ?) OR
412 (pc_endDate = '0000-00-00' AND pc_eventDate >= ? AND pc_eventDate < ?)) AND pc_facility = ?";
413 return array($query,$data[1]);
417 //Appointments pending for approval
418 $query="select * from audit_master WHERE pid =? AND type='10'";
419 return array($query,array($pid));
423 //patient appointment
424 $query="select * from facility where service_location != 0 order by name";
425 return array($query);
428 //C series for mailbox.
430 $query = "SELECT username, fname, lname FROM users WHERE username != '' AND active = 1 AND ( info IS NULL OR info NOT LIKE
431 '%Inactive%' ) ORDER BY lname, fname";
432 return array($query);
436 $query = "SELECT option_id, title FROM list_options WHERE list_id = ? ORDER BY seq";
437 return array($query,$data[1]);
440 //D series for patient.
442 $query = "SELECT forms.encounter, forms.form_id, forms.id, forms.form_name, forms.formdir,forms.date AS fdate,
443 form_encounter.date ,form_encounter.reason FROM forms LEFT OUTER JOIN form_encounter ON forms.pid=form_encounter.pid
444 WHERE forms.pid = ? AND forms.deleted=0 AND forms.formdir<>? GROUP BY id ORDER BY forms.encounter,fdate ASC";
445 array_unshift($data[1],$pid);
446 return array($query,$data[1]);
450 $query = "SELECT name FROM registry ORDER BY priority";
451 return array($query);
455 $query = "select * from lists WHERE pid =? ORDER BY type, begdate";
456 return array($query,array($pid));
460 $query = "select encounter from issue_encounter WHERE pid = ? AND list_id = ?";
461 array_unshift($data[1],$pid);
462 return array($query,$data[1]);
465 //G series for form menu inc
467 $query = "SELECT * FROM `documents_legal_categories` where dlc_category_name=? and dlc_category_type=2";
468 return array($query,$data[1]);
472 $query = "SELECT * FROM documents_legal_master AS dlm WHERE dlm_subcategory <> ? and dlm_effective_date <= now() AND
473 dlm_effective_date<>? AND dlm_document_id Not IN (SELECT distinct(dld_master_docid) FROM documents_legal_detail WHERE
474 dld_id IS NOT NULL AND dld_pid=?)";
475 array_push($data[1],$pid);
476 return array($query,$data[1]);
480 $query = "SELECT * FROM documents_legal_master AS dlm LEFT OUTER JOIN documents_legal_detail as dld ON
481 dlm_document_id=dld_master_docid WHERE dlm_subcategory <> ? and dlm_effective_date <= now() AND dlm_effective_date<>?
482 AND dld_id IS NOT NULL AND dld_signed=? AND dld_pid=? ORDER BY dlm_effective_date DESC";
483 array_push($data[1],$pid);
484 return array($query,$data[1]);
488 $query = "SELECT * FROM documents_legal_master AS dlm JOIN documents_legal_detail as dld ON dlm_document_id=dld_master_docid
489 JOIN form_encounter as fe ON encounter=dld_encounter WHERE dlm_subcategory = ? AND dlm_effective_date <= now() AND
490 dlm_effective_date<>? AND dld_id IS NOT NULL AND dld_signed=? AND dld_signing_person=? AND dld_pid=?
491 ORDER BY dlm_effective_date DESC";
492 array_push($data[1],$pid);
493 return array($query,$data[1]);
497 $query = "SELECT * FROM documents_legal_master AS dlm JOIN documents_legal_detail as dld ON dlm_document_id=dld_master_docid
498 JOIN form_encounter as fe ON encounter=dld_encounter WHERE dlm_subcategory = ? and dlm_effective_date <= now() AND
499 dlm_effective_date<>? AND dld_id IS NOT NULL AND dld_filename != '' AND dld_pid=? GROUP BY dld_encounter,dlm_document_id
500 ORDER BY dld_id DESC";
501 array_push($data[1],$pid);
502 return array($query,$data[1]);
507 $query="select * from patient_data where pid=?";
508 return array($query,array($pid));
512 //PDF forms detail selected
513 $query="select * from documents_legal_master where dlm_document_id=?";
514 return array($query,$data[1]);
519 array_unshift($data[1],$pid);
520 $query="select * from documents_legal_detail where dld_pid = ? and dld_signed='3' and dld_master_docid = ?";
521 return array($query,$data[1]);
526 $query="select * from documents_legal_master where dlm_document_id=?";
527 return array($query,$data[1]);
531 // Entries to be approved demo for new patient, existing patient and only documents
532 $query="select * from audit_master where pid=? and approval_status='1' and (type='1' or type='2' or type='3')";
533 return array($query,array($pid));
538 $query="select * from documents_legal_master WHERE dlm_document_id=?";
539 return array($query,$data[1]);
543 //Documents ready to be signed and documents unsigned
544 array_unshift($data[1],$pid);
545 $query="select * from documents_legal_detail where dld_pid=? and (dld_signed='2' or dld_signed='0') and dld_master_docid=?";
546 return array($query,$data[1]);
551 //Selection from master document for showing to patient
552 $query="select * from documents_legal_master WHERE dlm_document_name=?";
553 return array($query,$data[1]);
557 $query = "SELECT fname FROM patient_data WHERE pid=?";
558 return array($query,array($pid));
560 //Checking whether a new patient entry is pending in the audit master
562 $query = "SELECT pid FROM audit_master WHERE approval_status=1 and type=1 and pid=?";
563 return array($query,array($pid));