Created
April 28, 2017 09:33
-
-
Save iaditya/6971da17a056202847a08275f3102b7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function lastVisitDate($patient_id, $facility_id) { | |
$start = Time::now()->toDateString(); // today | |
$events = TableRegistry::get('events'); | |
$events_data = $events->find() | |
->where(['patient_id' => $patient_id]) | |
->andWhere(['facility_id' => $facility_id]) | |
->andWhere(['events.start < :now']) // returns the past appointments (not check today) | |
->bind(':now', $start, 'time'); | |
if($events_data->count() > 1) { | |
$event_date = $events_data->limit(2)->orderDesc('id')->first()->start; | |
} | |
$date1 = $event_date ? strtotime($event_date) : null; //unix timestamps | |
$encounters = TableRegistry::get('encounters'); | |
$encounters_data = $encounters->find() | |
->where(['patient_id' => $patient_id]) | |
->andWhere(['facility_id' => $facility_id]); | |
$encounter_date = $encounters_data->last()->visitdate; | |
$date2 = $encounter_date ? strtotime($encounter_date) : null; | |
$prescriptions = TableRegistry::get('prescriptions'); | |
$prescriptions_data = $prescriptions->find() | |
->where(['patient_id' => $patient_id]) | |
->andWhere(['facility_id' => $facility_id]); | |
$prescription_date = $prescriptions_data->last()->created; | |
$date3 = $prescription_date ? strtotime($prescription_date) : null; | |
if( ($date1 > $date2) && ($date1 > $date3) ) { | |
//debug((new Time($date1))->timeAgoInWords()); exit; | |
return $date1; | |
} elseif ( ($date2 > $date3) && ($date2 > $date1) ) { | |
//debug($date2); exit; | |
return $date2; | |
} else { | |
//debug($date3); exit; | |
return $date3; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment