Skip to content

Instantly share code, notes, and snippets.

@iaditya
Last active April 28, 2017 17:09
Show Gist options
  • Save iaditya/4d98419cc974fc9e0f0fba20a5453229 to your computer and use it in GitHub Desktop.
Save iaditya/4d98419cc974fc9e0f0fba20a5453229 to your computer and use it in GitHub Desktop.
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
} else {
$date1 = null;
}
$encounters = TableRegistry::get('encounters');
$encounters_data = $encounters->find()
->where(['patient_id' => $patient_id])
->andWhere(['facility_id' => $facility_id])
->andWhere(['encounters.visitdate < :now']) // returns the past encounters (not check today)
->bind(':now', $start, 'time');
$encounter_date = $encounters_data->last() ? $encounters_data->last()->visitdate : null;
$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])
->andWhere(['prescriptions.created < :now']) // returns the past encounters (not check today)
->bind(':now', $start, 'time');
$prescription_date = $prescriptions_data->last() ? $prescriptions_data->last()->created : null;
$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