Skip to content

Instantly share code, notes, and snippets.

@iaditya
Last active April 28, 2017 17:09
Show Gist options
  • Save iaditya/24ce089b90fae31c86ad3999cec8d976 to your computer and use it in GitHub Desktop.
Save iaditya/24ce089b90fae31c86ad3999cec8d976 to your computer and use it in GitHub Desktop.
public function lastVisitDate($patient_id, $facility_id) {
$start = Time::now()->toDateString(); // today
$events = $this->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->count() > 1) {
$event_date = $events->limit(2)->orderDesc('id')->first()->start;
$date1 = $event_date ? strtotime($event_date) : null; //unix timestamps
} else {
$date1 = null;
}
$encounters = $this->Events->Encounters->find()
->where(['patient_id' => $patient_id])
->andWhere(['facility_id' => $facility_id])
->andWhere(['Encounters.visitdate < :now']) // returns the past casesheets (not check today)
->bind(':now', $start, 'time');
$encounter_date = $encounters->last() ? $encounters->last()->visitdate : null;
$date2 = $encounter_date ? strtotime($encounter_date) : null;
$prescriptions = $this->Events->Encounters->Prescriptions->find()
->where(['patient_id' => $patient_id])
->andWhere(['facility_id' => $facility_id])
->andWhere(['Prescriptions.created < :now']) // returns the past prescriptions (not check today)
->bind(':now', $start, 'time');
$prescription_date = $prescriptions->last() ? $prescriptions->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