Skip to content

Instantly share code, notes, and snippets.

@iaditya
Created April 15, 2017 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iaditya/e4cbadfa51daf925d43a8565a6bf90ae to your computer and use it in GitHub Desktop.
Save iaditya/e4cbadfa51daf925d43a8565a6bf90ae to your computer and use it in GitHub Desktop.
public function lastVisitDate($patient_id, $facility_id) {
$Events = TableRegistry::get('events');
$start = Time::now()->toDateString(); // today
$events = $Events->find()
->where(['patient_id' => $patient_id])
->andWhere(['facility_id' => $facility_id])
->andWhere(['events.start < :now']) // returns the past appointments (not includes today)
->bind(':now', $start, 'time')
->contain(['Encounters' => ['Prescriptions']]);
$event = $events->last(); // return last appointment
if($events->count() == 1) { // only one appointment -> no last visit date
return false;
} else {
$event2 = $events->orderDesc('id')->limit(2)->first(); // getting last second appointment , [If no encounters of first]
}
$last_visit_date = $event->start;
//checking for the last encounter
if($event->encounters) {
//$count = count($event->encounters);
//$encounter = ($event->encounters)[0];
$encounter = end($event->encounters); // return last encounter in given array
$last_visit_date = $encounter->visitdate;
// checking for last prescription
if($encounter->prescriptions) {
$prescription = end($encounter->prescriptions); // return last prescription in given array
//debug("In Prescriptions");
$last_visit_date = $prescription->created;
}
}
if(! $event->encounters) {
//$event = $events->limit(2)->first();
$last_visit_date = $event2->start;
}
//debug($last_visit_date); exit;
return $last_visit_date;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment