Skip to content

Instantly share code, notes, and snippets.

@iaditya
Created May 25, 2017 12:15
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/197a39045e81476d0a14180eb7d1d0d0 to your computer and use it in GitHub Desktop.
Save iaditya/197a39045e81476d0a14180eb7d1d0d0 to your computer and use it in GitHub Desktop.
public function drugAlert() {
/**
* run on terminal
*
* bin/cake Alerts drugAlert
*
*/
// send notification to patient/caregiver/provider/fAdmin
$after_two_day = 2880;
$duration = 60;
$start = Time::now()->addMinutes($after_two_day)->toDateTimeString();
$end = Time::now()->addMinutes($duration + $after_two_day)->toDateTimeString();
//$this->out(print_r($start, true));
//$this->out(print_r($end, true)); exit;
$query = $this->PrescriptionItems->find('all')
->where(['PrescriptionItems.days IS NOT' => null])
->andWhere(['PrescriptionItems.days >' => 0])
->andWhere(['ADDDATE(PrescriptionItems.created, PrescriptionItems.days) BETWEEN :now AND :then'])
->bind(':now', $start, 'time')
->bind(':then', $end, 'time');
//$this->out(print_r($query->toArray(), true)); exit;
$prescription_items = $query->select([
'PrescriptionItems.prescription_id',
'maxdays' => $query->func()->max('PrescriptionItems.days'),
])
->group(['PrescriptionItems.prescription_id'])
->toArray();
//$this->out(print_r($prescription_items, true)); exit;
$notificationManager = new NotificationManager();
$prescription_ids = array();
if(count($prescription_items) > 0) {
foreach ($prescription_items as $prescription_item) {
array_push($prescription_ids, $prescription_item->prescription_id);
//$this->out(print_r($prescription_ids, true)); exit;
}
if(count($prescription_ids) > 0 ) {
$prescriptions = $this->Prescriptions->find('all')
->where(['Prescriptions.id IN ' => $prescription_ids]);
//$this->out(print_r($prescriptions, true)); exit;
foreach ($prescriptions as $prescription) {
if($prescription->patient_id) {
// send to patient drugAlert
$patient = $this->Patients->findById($prescription->patient_id)->first();
$notificationManager->addRecipientList('drugAlert', [$patient->user_id]);
$this->Notifier->notify([
'recipientLists' => 'drugAlert',
'template' => 'drugAlert',
'vars' => [
'name' => $patient->id,
],
], $patient->user_id);
$this->out(print_r("send drugAlert to Patient : " . $patient->user_id , true));
//if patient has caregiver ?
$caregivers = $this->Caregivers->findByPatientId($patient->id)->select('caregiver_patient_id');
if($caregivers->count() > 0) {
$caregiver_user_ids = array();
foreach ($caregivers as $caregiver) {
$caregiver_patient = $this->Patients->findById($caregiver->caregiver_patient_id)->first();
array_push($caregiver_user_ids, $caregiver_patient->user_id);
}
//$this->out(print_r($caregiver_user_ids , true)); exit;
// send notifications to caregivers
$notificationManager->addRecipientList('drugAlert', $caregiver_user_ids);
$this->Notifier->notify([
'recipientLists' => 'drugAlert',
'template' => 'drugAlert',
'vars' => [
'name' => "Caregiver",
],
], true);
$this->out(print_r("send drugAlert to Caregiver : " . $patient->user_id , true));
}
}
//$this->out(print_r("Caregiver done." , true)); exit;
//send to provider drugAlert
if($prescription->provider_id) {
$provider = $this->Providers->findById($prescription->provider_id)->first();
$notificationManager->addRecipientList('drugAlert', [$provider->user_id]);
$this->Notifier->notify([
'recipientLists' => 'drugAlert',
'template' => 'drugAlert',
'vars' => [
'name' => $provider->id,
],
], $provider->user_id);
$this->out(print_r("send drugAlert to Provider : " . $provider->user_id , true));
}
//send to provider FAdmin
if($prescription->facility_id) {
$f_admin_user_ids = array();
$f_admins = $this->FacilityAdmins->findByFacilityId($prescription->facility_id);
if($f_admins->count() > 0) {
foreach ($f_admins as $f_admin) {
array_push($f_admin_user_ids, $f_admin->user_id);
}
//send notification to FAdmins
// send notifications to caregivers
$notificationManager->addRecipientList('drugAlert', $f_admin_user_ids);
$this->Notifier->notify([
'recipientLists' => 'drugAlert',
'template' => 'drugAlert',
'vars' => [
'name' => $f_admin_user_ids[0],
],
], true);
$this->out(print_r("send drugAlert to FAdmins, Facility ID : " . $prescription->facility_id , true));
}
}
}
}
}
$this->out(print_r($start, true));
$this->out(print_r($end, true));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment