4 * Holds library functions used by events
6 * @link https://www.open-emr.org
7 * @author Z&H Consultancy Services Private Limited <sam@zhservices.com>
8 * @author Eldho Chacko <eldho@zhservices.com>
9 * @author Paul Simon K <paul@zhservices.com>
10 * @author Ian Jardine ( github.com/epsdky ) ( Modified calendar_arrived )
11 * @copyright Copyright (c) 2010 Z&H Consultancy Services Private Limited <sam@zhservices.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once(__DIR__
. '/calendar.inc.php');
16 require_once(__DIR__
. '/patient_tracker.inc.php');
18 //===============================================================================
19 //This section handles the events of payment screen.
20 //===============================================================================
21 define('REPEAT_EVERY_DAY', 0);
22 define('REPEAT_EVERY_WEEK', 1);
23 define('REPEAT_EVERY_MONTH', 2);
24 define('REPEAT_EVERY_YEAR', 3);
25 define('REPEAT_EVERY_WORK_DAY', 4);
26 define('REPEAT_DAYS_EVERY_WEEK', 6);
27 //===============================================================================
28 $today = date('Y-m-d');
29 //===============================================================================
30 // If unique current date appointment found update status to arrived and create
33 function calendar_arrived($form_pid)
36 $today = date('Y-m-d');
37 $appts = fetchAppointments($today, $today, $form_pid);
38 $appt_count = count($appts); //
39 if ($appt_count == 0) {
40 echo "<br /><br /><br /><h2 class='text-center'>" . htmlspecialchars(xl('Sorry No Appointment is Fixed'), ENT_QUOTES
) . ". " . htmlspecialchars(xl('No Encounter could be created'), ENT_QUOTES
) . ".</h2>";
42 } elseif ($appt_count == 1) {
43 $enc = todaysEncounterCheck($form_pid);
44 if ($appts[0]['pc_recurrtype'] == 0) {
45 sqlStatement("UPDATE openemr_postcalendar_events SET pc_apptstatus = '@' WHERE pc_eid = ?", array($appts[0]['pc_eid']));
47 update_event($appts[0]['pc_eid']);
49 } elseif ($appt_count > 1) {
50 echo "<br /><br /><br /><h2 class='text-center'>" . htmlspecialchars(xl('More than one appointment was found'), ENT_QUOTES
) . ". " . htmlspecialchars(xl('No Encounter could be created'), ENT_QUOTES
) . ".</h2>";
56 //===============================================================================
57 // Checks for the patient's encounter ID for today, creating it if there is none.
59 function todaysEncounterCheck($patient_id, $enc_date = '', $reason = '', $fac_id = '', $billing_fac = '', $provider = '', $cat = '', $return_existing = true)
62 $encounter = todaysEncounterIf($patient_id);
63 if ($encounter && (int)$GLOBALS['auto_create_new_encounters'] !== 2) {
64 if ($return_existing) {
71 if (is_array($provider)) {
72 $visit_provider = (int)$provider[0];
73 } elseif ($provider) {
74 $visit_provider = (int)$provider;
76 $visit_provider = '(NULL)';
79 $dos = $enc_date ?
$enc_date : $today;
80 $visit_reason = $reason ?
$reason : xl('Please indicate visit reason');
81 $tmprow = sqlQuery("SELECT username, facility, facility_id FROM users WHERE id = ?", array($_SESSION["authUserID"]));
82 $username = $tmprow['username'];
83 $facility = $tmprow['facility'];
84 $facility_id = $fac_id ?
(int)$fac_id : $tmprow['facility_id'];
85 $billing_facility = $billing_fac ?
(int)$billing_fac : $tmprow['facility_id'];
86 $pos_code = sqlQuery("SELECT pos_code FROM facility WHERE id = ?", array($facility_id))['pos_code'];
87 $visit_cat = $cat ?
$cat : '(NULL)';
88 $conn = $GLOBALS['adodb']['db'];
89 $encounter = $conn->GenID("sequences");
92 "New Patient Encounter",
94 "INSERT INTO form_encounter SET " .
99 "billing_facility = ?, " .
100 "provider_id = ?, " .
105 array($dos,$visit_reason,$facility,$facility_id,$billing_facility,$visit_provider,$patient_id,$encounter,$visit_cat, $pos_code)
116 //===============================================================================
117 // Checks for the group's encounter ID for today, creating it if there is none.
119 function todaysTherapyGroupEncounterCheck($group_id, $enc_date = '', $reason = '', $fac_id = '', $billing_fac = '', $provider = '', $cat = '', $return_existing = true, $eid = null)
122 $encounter = todaysTherapyGroupEncounterIf($group_id);
124 if ($return_existing) {
131 if (is_array($provider)) {
132 $visit_provider = (int)$provider[0];
133 $counselors = implode(',', $provider);
134 } elseif ($provider) {
135 $visit_provider = $counselors = (int)$provider;
137 $visit_provider = $counselors = null;
140 $dos = $enc_date ?
$enc_date : $today;
141 $visit_reason = $reason ?
$reason : xl('Please indicate visit reason');
142 $tmprow = sqlQuery("SELECT username, facility, facility_id FROM users WHERE id = ?", array($_SESSION["authUserID"]));
143 $username = $tmprow['username'];
144 $facility = $tmprow['facility'];
145 $facility_id = $fac_id ?
(int)$fac_id : $tmprow['facility_id'];
146 $billing_facility = $billing_fac ?
(int)$billing_fac : $tmprow['facility_id'];
147 $visit_cat = $cat ?
$cat : '(NULL)';
148 $conn = $GLOBALS['adodb']['db'];
149 $encounter = $conn->GenID("sequences");
152 "New Therapy Group Encounter",
154 "INSERT INTO form_groups_encounter SET " .
158 "facility_id = ?, " .
159 "billing_facility = ?, " .
160 "provider_id = ?, " .
166 array($dos,$visit_reason,$facility,$facility_id,$billing_facility,$visit_provider,$group_id,$encounter,$visit_cat, $eid, $counselors)
178 //===============================================================================
179 // Get the patient's encounter ID for today, if it exists.
180 // In the case of more than one encounter today, pick the last one.
182 function todaysEncounterIf($patient_id)
185 $tmprow = sqlQuery("SELECT encounter FROM form_encounter WHERE " .
186 "pid = ? AND date = ? " .
187 "ORDER BY encounter DESC LIMIT 1", array($patient_id,"$today 00:00:00"));
188 return empty($tmprow['encounter']) ?
0 : $tmprow['encounter'];
190 //===============================================================================
191 // Get the group's encounter ID for today, if it exists.
192 // In the case of more than one encounter today, pick the last one.
194 function todaysTherapyGroupEncounterIf($group_id)
197 $tmprow = sqlQuery("SELECT encounter FROM form_groups_encounter WHERE " .
198 "group_id = ? AND date = ? " .
199 "ORDER BY encounter DESC LIMIT 1", array($group_id,"$today 00:00:00"));
200 return empty($tmprow['encounter']) ?
0 : $tmprow['encounter'];
202 //===============================================================================
204 // Get the patient's encounter ID for today, creating it if there is none.
206 function todaysEncounter($patient_id, $reason = '')
208 global $today, $userauthorized;
210 if (empty($reason)) {
211 $reason = xl('Please indicate visit reason');
214 // Was going to use the existing encounter for today if there is one, but
215 // decided it's right to always create a new one. Leaving the code here
216 // (and corresponding function above) in case it is ever wanted later.
217 /*******************************************************************
218 $encounter = todaysEncounterIf($patient_id);
219 if ($encounter) return $encounter;
220 *******************************************************************/
222 $tmprow = sqlQuery("SELECT username, facility, facility_id FROM users " .
223 "WHERE id = ?", array($_SESSION["authUserID"]));
224 $username = $tmprow['username'];
225 $facility = $tmprow['facility'];
226 $facility_id = $tmprow['facility_id'];
227 $conn = $GLOBALS['adodb']['db'];
228 $encounter = $conn->GenID("sequences");
229 $provider_id = $userauthorized ?
$_SESSION['authUserID'] : 0;
232 "New Patient Encounter",
234 "INSERT INTO form_encounter SET date = ?, onset_date = ?, " .
235 "reason = ?, facility = ?, facility_id = ?, pid = ?, encounter = ?, " .
237 array($today, $today, $reason, $facility, $facility_id, $patient_id,
238 $encounter, $provider_id)
248 //===============================================================================
249 // get the original event's repeat specs
250 function update_event($eid)
252 $origEventRes = sqlStatement("SELECT * FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));
253 $origEvent = sqlFetchArray($origEventRes);
254 $oldRecurrspec = unserialize($origEvent['pc_recurrspec'], ['allowed_classes' => false]);
255 $duration = $origEvent['pc_duration'];
256 $starttime = $origEvent['pc_startTime'];
257 $endtime = $origEvent['pc_endTime'];
258 $selected_date = date("Ymd");
259 if ($oldRecurrspec['exdate'] != "") {
260 $oldRecurrspec['exdate'] .= "," . $selected_date;
262 $oldRecurrspec['exdate'] .= $selected_date;
265 // mod original event recur specs to exclude this date
266 sqlStatement("UPDATE openemr_postcalendar_events SET pc_recurrspec = ? WHERE pc_eid = ?", array(serialize($oldRecurrspec),$eid));
267 // specify some special variables needed for the INSERT
268 // no recurr specs, this is used for adding a new non-recurring event
269 $noRecurrspec = array("event_repeat_freq" => "",
270 "event_repeat_freq_type" => "",
271 "event_repeat_on_num" => "1",
272 "event_repeat_on_day" => "0",
273 "event_repeat_on_freq" => "0",
276 // Useless garbage that we must save.
277 $locationspecs = array("event_location" => "",
278 "event_street1" => "",
279 "event_street2" => "",
284 $locationspec = serialize($locationspecs);
285 $args['event_date'] = date('Y-m-d');
286 $args['duration'] = $duration;
287 // this event is forced to NOT REPEAT
288 $args['form_repeat'] = "0";
289 $args['recurrspec'] = $noRecurrspec;
290 $args['form_enddate'] = "0000-00-00";
291 $args['starttime'] = $starttime;
292 $args['endtime'] = $endtime;
293 $args['locationspec'] = $locationspec;
294 $args['form_category'] = $origEvent['pc_catid'];
295 $args['new_multiple_value'] = $origEvent['pc_multiple'];
296 $args['form_provider'] = $origEvent['pc_aid'];
297 $args['form_pid'] = $origEvent['pc_pid'];
298 $args['form_title'] = $origEvent['pc_title'];
299 $args['form_allday'] = $origEvent['pc_alldayevent'];
300 $args['form_apptstatus'] = '@';
301 $args['form_prefcat'] = $origEvent['pc_prefcatid'];
302 $args['facility'] = $origEvent['pc_facility'];
303 $args['billing_facility'] = $origEvent['pc_billing_location'];
304 InsertEvent($args, 'payment');
306 //===============================================================================
307 // check if event exists
308 function check_event_exist($eid)
310 $origEventRes = sqlStatement("SELECT * FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));
311 $origEvent = sqlFetchArray($origEventRes);
312 $pc_catid = $origEvent['pc_catid'];
313 $pc_aid = $origEvent['pc_aid'];
314 $pc_pid = $origEvent['pc_pid'];
315 $pc_eventDate = date('Y-m-d');
316 $pc_startTime = $origEvent['pc_startTime'];
317 $pc_endTime = $origEvent['pc_endTime'];
318 $pc_facility = $origEvent['pc_facility'];
319 $pc_billing_location = $origEvent['pc_billing_location'];
320 $pc_recurrspec_array = unserialize($origEvent['pc_recurrspec'], ['allowed_classes' => false]);
321 $origEvent = sqlStatement(
322 "SELECT * FROM openemr_postcalendar_events WHERE pc_eid != ? and pc_catid=? and pc_aid=? " .
323 "and pc_pid=? and pc_eventDate=? and pc_startTime=? and pc_endTime=? and pc_facility=? and pc_billing_location=?",
324 array($eid,$pc_catid,$pc_aid,$pc_pid,$pc_eventDate,$pc_startTime,$pc_endTime,$pc_facility,$pc_billing_location)
326 if (sqlNumRows($origEvent) > 0) {
327 $origEventRow = sqlFetchArray($origEvent);
328 return $origEventRow['pc_eid'];
330 if (strpos($pc_recurrspec_array['exdate'], date('Ymd')) === false) {//;'20110228'
332 } else {//this happens in delete case
337 //===============================================================================
339 // $args is mainly filled with content from the POST http var
340 function InsertEvent($args, $from = 'general')
342 $pc_recurrtype = '0';
343 if (!empty($args['form_repeat']) ||
!empty($args['days_every_week'])) {
344 if ($args['recurrspec']['event_repeat_freq_type'] == "6") {
347 $pc_recurrtype = $args['recurrspec']['event_repeat_on_freq'] ?
'2' : '1';
351 $form_pid = empty($args['form_pid']) ?
'' : $args['form_pid'];
352 $form_room = empty($args['form_room']) ?
'' : $args['form_room'];
353 $form_gid = empty($args['form_gid']) ?
'' : $args['form_gid'];
355 if ($from == 'general') {
357 "INSERT INTO openemr_postcalendar_events ( " .
358 "pc_catid, pc_multiple, pc_aid, pc_pid, pc_gid, pc_title, pc_time, pc_hometext, " .
359 "pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
360 "pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
361 "pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility,pc_billing_location,pc_room " .
362 ") VALUES (?,?,?,?,?,?,NOW(),?,?,?,?,?,?,?,?,?,?,?,?,?,1,1,?,?,?)",
363 array($args['form_category'],(isset($args['new_multiple_value']) ?
$args['new_multiple_value'] : ''),$args['form_provider'],$form_pid,$form_gid,
364 $args['form_title'],$args['form_comments'],$_SESSION['authUserID'],$args['event_date'],
365 fixDate($args['form_enddate']),$args['duration'],$pc_recurrtype,serialize($args['recurrspec']),
366 $args['starttime'],$args['endtime'],$args['form_allday'],$args['form_apptstatus'],$args['form_prefcat'],
367 $args['locationspec'],(int)$args['facility'],(int)$args['billing_facility'],$form_room)
370 //Manage tracker status.
371 if (!empty($form_pid)) {
372 manage_tracker_status($args['event_date'], $args['starttime'], $pc_eid, $form_pid, $_SESSION['authUser'], $args['form_apptstatus'], $args['form_room']);
375 $GLOBALS['temporary-eid-for-manage-tracker'] = $pc_eid; //used by manage tracker module to set correct encounter in tracker when check in
378 } elseif ($from == 'payment') {
380 "INSERT INTO openemr_postcalendar_events ( " .
381 "pc_catid, pc_multiple, pc_aid, pc_pid, pc_title, pc_time, " .
382 "pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
383 "pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
384 "pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility,pc_billing_location " .
385 ") VALUES (?,?,?,?,?,NOW(),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
386 array($args['form_category'],$args['new_multiple_value'],$args['form_provider'],$form_pid,$args['form_title'],
387 $args['event_date'],$args['form_enddate'],$args['duration'],$pc_recurrtype,serialize($args['recurrspec']),
388 $args['starttime'],$args['endtime'],$args['form_allday'],$args['form_apptstatus'],$args['form_prefcat'], $args['locationspec'],
391 (int)$args['facility'],
392 (int)$args['billing_facility'])
396 //================================================================================================================
399 * returns the next valid date for an event based on the
400 * current day,month,year,freq and type
402 * @returns string YYYY-MM-DD
404 function &__increment($d, $m, $y, $f, $t)
406 if ($t == REPEAT_DAYS_EVERY_WEEK
) {
407 $old_appointment_date = date('Y-m-d', mktime(0, 0, 0, $m, $d, $y));
408 $next_appointment_date = getTheNextAppointment($old_appointment_date, $f);
409 return $next_appointment_date;
412 if ($t == REPEAT_EVERY_DAY
) {
414 } elseif ($t == REPEAT_EVERY_WORK_DAY
) {
415 // a workday is defined as Mon,Tue,Wed,Thu,Fri
416 // repeating on every or Nth work day means to not include
417 // weekends (Sat/Sun) in the increment... tricky
419 // ugh, a day-by-day loop seems necessary here, something where
420 // we can check to see if the day is a Sat/Sun and increment
421 // the frequency count so as to ignore the weekend. hmmmm....
423 for ($daycount = 1; $daycount <= $orig_freq; $daycount++
) {
424 $nextWorkDOW = date('w', mktime(0, 0, 0, $m, ($d +
$daycount), $y));
425 if (is_weekend_day($nextWorkDOW)) {
430 // and finally make sure we haven't landed on a end week days
431 // adjust as necessary
432 $nextWorkDOW = date('w', mktime(0, 0, 0, $m, ($d +
$f), $y));
433 if (count($GLOBALS['weekend_days']) === 2) {
434 if ($nextWorkDOW == $GLOBALS['weekend_days'][0]) {
436 } elseif ($nextWorkDOW == $GLOBALS['weekend_days'][1]) {
439 } elseif (count($GLOBALS['weekend_days']) === 1 && $nextWorkDOW === $GLOBALS['weekend_days'][0]) {
444 } elseif ($t == REPEAT_EVERY_WEEK
) {
446 } elseif ($t == REPEAT_EVERY_MONTH
) {
448 } elseif ($t == REPEAT_EVERY_YEAR
) {
452 $dtYMD = date('Y-m-d', mktime(0, 0, 0, $m, $d, $y));
456 function getTheNextAppointment($appointment_date, $freq)
458 $day_arr = explode(",", $freq);
460 foreach ($day_arr as $day) {
461 $day = getDayName($day);
462 $date = date('Y-m-d', strtotime("next " . $day, strtotime($appointment_date)));
463 array_push($date_arr, $date);
466 $next_appointment = getEarliestDate($date_arr);
467 return $next_appointment;
470 function getDayName($day_num)
472 if ($day_num == "1") {
476 if ($day_num == "2") {
480 if ($day_num == "3") {
484 if ($day_num == "4") {
488 if ($day_num == "5") {
492 if ($day_num == "6") {
496 if ($day_num == "7") {
502 function getEarliestDate($date_arr)
504 $earliest = ($date_arr[0]);
505 foreach ($date_arr as $date) {
506 if (strtotime($date) < strtotime($earliest)) {
513 //================================================================================================================