forked from LibreHealthIO/lh-ehr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencounter_events.inc.php
398 lines (380 loc) · 17.4 KB
/
encounter_events.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
// +-----------------------------------------------------------------------------+
// Copyright (C) 2010 Z&H Consultancy Services Private Limited <[email protected]>
//
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// A copy of the GNU General Public License is included along with this program:
// libreehr/interface/login/GnuGPL.html
// For more information write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// Author: Eldho Chacko <[email protected]>
// Paul Simon K <[email protected]>
//
// +------------------------------------------------------------------------------+
require_once(dirname(__FILE__) . '/patient_tracker.inc.php');
//===============================================================================
//This section handles the events of payment screen.
//===============================================================================
define('REPEAT_EVERY_DAY', 0);
define('REPEAT_EVERY_WEEK', 1);
define('REPEAT_EVERY_MONTH', 2);
define('REPEAT_EVERY_YEAR', 3);
define('REPEAT_EVERY_WORK_DAY',4);
//===============================================================================
//Create event in calender as arrived
function calendar_arrived($form_pid) {
$Today=date('Y-m-d');
//Take all recurring events relevent for today.
$result_event=sqlStatement("SELECT * FROM libreehr_postcalendar_events WHERE pc_recurrtype != '0' and pc_pid = ? and pc_endDate != '0000-00-00'
and pc_eventDate < ? and pc_endDate >= ? ",
array($form_pid,$Today,$Today));
if(sqlNumRows($result_event)==0)//no repeating appointment
{
$result_event=sqlStatement("SELECT * FROM libreehr_postcalendar_events WHERE pc_pid =? and pc_eventDate = ?",
array($form_pid,$Today));
if(sqlNumRows($result_event)==0)//no appointment
{
echo "<br><br><br>".htmlspecialchars( xl('Sorry No Appointment is Fixed'), ENT_QUOTES ).". ".htmlspecialchars( xl('No Encounter could be created'), ENT_QUOTES ).".";
die;
}
else//one appointment
{
$enc = todaysEncounterCheck($form_pid);//create encounter
$zero_enc=0;
sqlStatement("UPDATE libreehr_postcalendar_events SET pc_apptstatus ='@' WHERE pc_pid =? and pc_eventDate = ?",
array($form_pid,$Today));
}
}
else//repeating appointment set
{
while($row_event=sqlFetchArray($result_event))
{
$pc_eid = $row_event['pc_eid'];
$pc_eventDate = $row_event['pc_eventDate'];
$pc_recurrspec_array = unserialize($row_event['pc_recurrspec']);
while(1)
{
if($pc_eventDate==$Today)//Matches so insert.
{
if(!$exist_eid=check_event_exist($pc_eid))
{
update_event($pc_eid);
}
else
{
sqlStatement("UPDATE libreehr_postcalendar_events SET pc_apptstatus = '@' WHERE pc_eid = ?",
array($exist_eid));
}
$enc = todaysEncounterCheck($form_pid);//create encounter
$zero_enc=0;
break;
}
elseif($pc_eventDate>$Today)//the frequency does not match today,no need to increment furthur.
{
echo "<br><br><br>".htmlspecialchars( xl('Sorry No Appointment is Fixed'), ENT_QUOTES ).". ".htmlspecialchars( xl('No Encounter could be created'), ENT_QUOTES ).".";
die;
break;
}
// Added by Rod to handle repeats on nth or last given weekday of a month:
if ($row_event['pc_recurrtype'] == 2) {
$my_repeat_on_day = $pc_recurrspec_array['event_repeat_on_day'];
$my_repeat_on_num = $pc_recurrspec_array['event_repeat_on_num'];
$adate = getdate(strtotime($pc_eventDate));
$adate['mon'] += 1;
if ($adate['mon'] > 12) {
$adate['year'] += 1;
$adate['mon'] -= 12;
}
if ($my_repeat_on_num < 5) { // not last
$adate['mday'] = 1;
$dow = jddayofweek(cal_to_jd(CAL_GREGORIAN, $adate['mon'], $adate['mday'], $adate['year']));
if ($dow > $my_repeat_on_day) $dow -= 7;
$adate['mday'] += ($my_repeat_on_num - 1) * 7 + $my_repeat_on_day - $dow;
}
else { // last weekday of month
$adate['mday'] = cal_days_in_month(CAL_GREGORIAN, $adate['mon'], $adate['year']);
$dow = jddayofweek(cal_to_jd(CAL_GREGORIAN, $adate['mon'], $adate['mday'], $adate['year']));
if ($dow < $my_repeat_on_day) $dow += 7;
$adate['mday'] += $my_repeat_on_day - $dow;
}
$pc_eventDate = date('Y-m-d', mktime(0, 0, 0, $adate['mon'], $adate['mday'], $adate['year']));
} // end recurrtype 2
else { // pc_recurrtype is 1
$pc_eventDate_array = explode('-', $pc_eventDate);
// Find the next day as per the frequency definition.
$pc_eventDate =& __increment($pc_eventDate_array[2], $pc_eventDate_array[1], $pc_eventDate_array[0],
$pc_recurrspec_array['event_repeat_freq'], $pc_recurrspec_array['event_repeat_freq_type']);
}
}
}
}
return $enc;
}
//===============================================================================
// Checks for the patient's encounter ID for today, creating it if there is none.
//
function todaysEncounterCheck($patient_id, $enc_date = '', $reason = '', $fac_id = '', $billing_fac = '', $provider = '', $cat = '', $return_existing = true){
global $today;
$encounter = todaysEncounterIf($patient_id);
if($encounter){
if($return_existing){
return $encounter;
}else{
return 0;
}
}
$dos = $enc_date ? $enc_date : $today;
$visit_reason = $reason ? $reason : 'Please indicate visit reason';
$tmprow = sqlQuery("SELECT username, facility, facility_id FROM users WHERE id = ?", array($_SESSION["authUserID"]) );
$username = $tmprow['username'];
$facility = $tmprow['facility'];
$facility_id = $fac_id ? (int)$fac_id : $tmprow['facility_id'];
$billing_facility = $billing_fac ? (int)$billing_fac : $tmprow['facility_id'];
$visit_provider = $provider ? (int)$provider : '(NULL)';
$visit_cat = $cat ? $cat : '(NULL)';
$conn = $GLOBALS['adodb']['db'];
$encounter = $conn->GenID("sequences");
addForm($encounter, "Patient Encounter",
sqlInsert("INSERT INTO form_encounter SET " .
"date = ?, " .
"reason = ?, " .
"facility = ?, " .
"facility_id = ?, " .
"billing_facility = ?, " .
"provider_id = ?, " .
"pid = ?, " .
"encounter = ?," .
"pc_catid = ?",
array($dos,$visit_reason,$facility,$facility_id,$billing_facility,$visit_provider,$patient_id,$encounter,$visit_cat)
),
"patient_encounter", $patient_id, "1", "NOW()", $username
);
return $encounter;
}
//===============================================================================
// Get the patient's encounter ID for today, if it exists.
// In the case of more than one encounter today, pick the last one.
//
function todaysEncounterIf($patient_id) {
global $today;
$tmprow = sqlQuery("SELECT encounter FROM form_encounter WHERE " .
"pid = ? AND date = ? " .
"ORDER BY encounter DESC LIMIT 1",array($patient_id,"$today 00:00:00"));
return empty($tmprow['encounter']) ? 0 : $tmprow['encounter'];
}
//===============================================================================
// Get the patient's encounter ID for today, creating it if there is none.
//
function todaysEncounter($patient_id, $reason='') {
global $today, $userauthorized;
if (empty($reason)) $reason = xl('Please indicate visit reason');
// Was going to use the existing encounter for today if there is one, but
// decided it's right to always create a new one. Leaving the code here
// (and corresponding function above) in case it is ever wanted later.
/*******************************************************************
$encounter = todaysEncounterIf($patient_id);
if ($encounter) return $encounter;
*******************************************************************/
$tmprow = sqlQuery("SELECT username, facility, facility_id FROM users " .
"WHERE id = ?", array($_SESSION["authUserID"]));
$username = $tmprow['username'];
$facility = $tmprow['facility'];
$facility_id = $tmprow['facility_id'];
$conn = $GLOBALS['adodb']['db'];
$encounter = $conn->GenID("sequences");
$provider_id = $userauthorized ? $_SESSION['authUserID'] : 0;
addForm($encounter, "Patient Encounter",
sqlInsert("INSERT INTO form_encounter SET date = ?, onset_date = ?, " .
"reason = ?, facility = ?, facility_id = ?, pid = ?, encounter = ?, " .
"provider_id = ?",
array($today, $today, $reason, $facility, $facility_id, $patient_id,
$encounter, $provider_id)
),
"patient_encounter", $patient_id, $userauthorized, "NOW()", $username
);
return $encounter;
}
//===============================================================================
// get the original event's repeat specs
function update_event($eid)
{
$origEventRes = sqlStatement("SELECT * FROM libreehr_postcalendar_events WHERE pc_eid = ?",array($eid));
$origEvent=sqlFetchArray($origEventRes);
$oldRecurrspec = unserialize($origEvent['pc_recurrspec']);
$duration=$origEvent['pc_duration'];
$starttime=$origEvent['pc_startTime'];
$endtime=$origEvent['pc_endTime'];
$selected_date = date("Ymd");
if ($oldRecurrspec['exdate'] != "") { $oldRecurrspec['exdate'] .= ",".$selected_date; }
else { $oldRecurrspec['exdate'] .= $selected_date; }
// mod original event recur specs to exclude this date
sqlStatement("UPDATE libreehr_postcalendar_events SET pc_recurrspec = ? WHERE pc_eid = ?",array(serialize($oldRecurrspec),$eid));
// specify some special variables needed for the INSERT
// no recurr specs, this is used for adding a new non-recurring event
$noRecurrspec = array("event_repeat_freq" => "",
"event_repeat_freq_type" => "",
"event_repeat_on_num" => "1",
"event_repeat_on_day" => "0",
"event_repeat_on_freq" => "0",
"exdate" => ""
);
// Useless garbage that we must save.
$locationspecs = array("event_location" => "",
"event_street1" => "",
"event_street2" => "",
"event_city" => "",
"event_state" => "",
"event_postal" => ""
);
$locationspec = serialize($locationspecs);
$args['event_date'] = date('Y-m-d');
$args['duration'] = $duration;
// this event is forced to NOT REPEAT
$args['form_repeat'] = "0";
$args['recurrspec'] = $noRecurrspec;
$args['form_enddate'] = "0000-00-00";
$args['starttime'] = $starttime;
$args['endtime'] = $endtime;
$args['locationspec'] = $locationspec;
$args['form_category']=$origEvent['pc_catid'];
$args['new_multiple_value']=$origEvent['pc_multiple'];
$args['form_provider']=$origEvent['pc_aid'];
$args['form_pid']=$origEvent['pc_pid'];
$args['form_title']=$origEvent['pc_title'];
$args['form_allday']=$origEvent['pc_alldayevent'];
$args['form_apptstatus']='@';
$args['form_prefcat']=$origEvent['pc_prefcatid'];
$args['facility']=$origEvent['pc_facility'];
$args['billing_facility']=$origEvent['pc_billing_location'];
InsertEvent($args,'payment');
}
//===============================================================================
// check if event exists
function check_event_exist($eid)
{
$origEventRes = sqlStatement("SELECT * FROM libreehr_postcalendar_events WHERE pc_eid = ?",array($eid));
$origEvent=sqlFetchArray($origEventRes);
$pc_catid=$origEvent['pc_catid'];
$pc_aid=$origEvent['pc_aid'];
$pc_pid=$origEvent['pc_pid'];
$pc_eventDate=date('Y-m-d');
$pc_startTime=$origEvent['pc_startTime'];
$pc_endTime=$origEvent['pc_endTime'];
$pc_facility=$origEvent['pc_facility'];
$pc_billing_location=$origEvent['pc_billing_location'];
$pc_recurrspec_array = unserialize($origEvent['pc_recurrspec']);
$origEvent = sqlStatement("SELECT * FROM libreehr_postcalendar_events WHERE pc_eid != ? and pc_catid=? and pc_aid=? ".
"and pc_pid=? and pc_eventDate=? and pc_startTime=? and pc_endTime=? and pc_facility=? and pc_billing_location=?",
array($eid,$pc_catid,$pc_aid,$pc_pid,$pc_eventDate,$pc_startTime,$pc_endTime,$pc_facility,$pc_billing_location));
if(sqlNumRows($origEvent)>0)
{
$origEventRow=sqlFetchArray($origEvent);
return $origEventRow['pc_eid'];
}
else
{
if(strpos($pc_recurrspec_array['exdate'],date('Ymd')) === false)//;'20110228'
{
return false;
}
else
{//this happens in delete case
return true;
}
}
}
//===============================================================================
// insert an event
// $args is mainly filled with content from the POST http var
function InsertEvent($args,$from = 'general') {
$pc_recurrtype = '0';
if ($args['form_repeat']) {
$pc_recurrtype = $args['recurrspec']['event_repeat_on_freq'] ? '2' : '1';
}
$form_pid = empty($args['form_pid']) ? '' : $args['form_pid'];
$form_room = empty($args['form_room']) ? '' : $args['form_room'];
if($from == 'general'){
$pc_eid = sqlInsert("INSERT INTO libreehr_postcalendar_events ( " .
"pc_catid, pc_multiple, pc_aid, pc_pid, pc_title, pc_time, pc_hometext, " .
"pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
"pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
"pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility,pc_billing_location,pc_room " .
") VALUES (?,?,?,?,?,NOW(),?,?,?,?,?,?,?,?,?,?,?,?,?,1,1,?,?,?)",
array($args['form_category'],(isset($args['new_multiple_value']) ? $args['new_multiple_value'] : ''),$args['form_provider'],$form_pid,
$args['form_title'],$args['form_comments'],$_SESSION['authUserID'],$args['event_date'],
fixDate($args['form_enddate']),$args['duration'],$pc_recurrtype,serialize($args['recurrspec']),
$args['starttime'],$args['endtime'],$args['form_allday'],$args['form_apptstatus'],$args['form_prefcat'],
$args['locationspec'],(int)$args['facility'],(int)$args['billing_facility'],$form_room)
);
//Manage tracker status.
if (!empty($form_pid)) {
manage_tracker_status($args['event_date'],$args['starttime'],$pc_eid,$form_pid,$_SESSION['authUser'],$args['form_apptstatus'],$args['form_room']);
}
$GLOBALS['temporary-eid-for-manage-tracker'] = $pc_eid; //used by manage tracker module to set correct encounter in tracker when check in
return $pc_eid;
}elseif($from == 'payment'){
sqlStatement("INSERT INTO libreehr_postcalendar_events ( " .
"pc_catid, pc_multiple, pc_aid, pc_pid, pc_title, pc_time, " .
"pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
"pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
"pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility,pc_billing_location " .
") VALUES (?,?,?,?,?,NOW(),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
array($args['form_category'],$args['new_multiple_value'],$args['form_provider'],$form_pid,$args['form_title'],
$args['event_date'],$args['form_enddate'],$args['duration'],$pc_recurrtype,serialize($args['recurrspec']),
$args['starttime'],$args['endtime'],$args['form_allday'],$args['form_apptstatus'],$args['form_prefcat'], $args['locationspec'],
1,1,(int)$args['facility'],(int)$args['billing_facility']));
}
}
//================================================================================================================
/**
* __increment()
* returns the next valid date for an event based on the
* current day,month,year,freq and type
* @private
* @returns string YYYY-MM-DD
*/
function &__increment($d,$m,$y,$f,$t)
{
if($t == REPEAT_EVERY_DAY) {
return date('Y-m-d',mktime(0,0,0,$m,($d+$f),$y));
} elseif($t == REPEAT_EVERY_WORK_DAY) {
// a workday is defined as Mon,Tue,Wed,Thu,Fri
// repeating on every or Nth work day means to not include
// weekends (Sat/Sun) in the increment... tricky
// ugh, a day-by-day loop seems necessary here, something where
// we can check to see if the day is a Sat/Sun and increment
// the frequency count so as to ignore the weekend. hmmmm....
$orig_freq = $f;
for ($daycount=1; $daycount<=$orig_freq; $daycount++) {
$nextWorkDOW = date('D',mktime(0,0,0,$m,($d+$daycount),$y));
if ($nextWorkDOW == "Sat") { $f++; }
else if ($nextWorkDOW == "Sun") { $f++; }
}
// and finally make sure we haven't landed on a Sat/Sun
// adjust as necessary
$nextWorkDOW = date('D',mktime(0,0,0,$m,($d+$f),$y));
if ($nextWorkDOW == "Sat") { $f+=2; }
else if ($nextWorkDOW == "Sun") { $f++; }
return date('Y-m-d',mktime(0,0,0,$m,($d+$f),$y));
} elseif($t == REPEAT_EVERY_WEEK) {
return date('Y-m-d',mktime(0,0,0,$m,($d+(7*$f)),$y));
} elseif($t == REPEAT_EVERY_MONTH) {
return date('Y-m-d',mktime(0,0,0,($m+$f),$d,$y));
} elseif($t == REPEAT_EVERY_YEAR) {
return date('Y-m-d',mktime(0,0,0,$m,$d,($y+$f)));
}
}
//================================================================================================================