Skip to content

Instantly share code, notes, and snippets.

@iaditya
Created June 5, 2017 08:40
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/cdac7ad5efe3dfead988ee736eefc808 to your computer and use it in GitHub Desktop.
Save iaditya/cdac7ad5efe3dfead988ee736eefc808 to your computer and use it in GitHub Desktop.
public function getAvailableSlotByDay($provider_id, $facility_id, $sdate, $slot_type)
{
$data = array();
$day = $this->getDayIndex($sdate);
$date = new Date($sdate);
//$events_start = $date->toDateTimeString();
//$events_end = $date->addDays(1)->toDateTimeString();
$provider_slots = $this->ProviderSlots->find('all')
->contain(['Facilities', 'Providers' => ['Users']])
->where(['provider_id' => $provider_id,
'facility_id' => $facility_id])
->where(['OR' => [['alldays' => 1], ['day' => $day]]])
->andWhere(['OR' => [['slot_type' => $slot_type], ['slot_type' => 'both']]])
->order(['start' => 'ASC']);
//return $provider_slots;
//debug($provider_slots->count()); exit;
foreach ($provider_slots as $provider_slot) {
$start = new Time( (new Date($sdate)) . " " . (new Time($provider_slot->start))->toTimeString() );
if ($slot_type == "inhouse") {
$slotTime = $provider_slot->provider->inhouse_slot_time;
} else if ($slot_type == "telecon") {
$slotTime = $provider_slot->provider->telecon_slot_time;
}
$finish = new Time((new Date($sdate)) . " " . (new Time($provider_slot->end))->toTimeString());
//debug("slot start {$start} \n");
//debug("slot end {$finish} \n"); exit;
$events = $this->Events->find('all')
->where(['facility_id' => $facility_id])
->andWhere(['provider_id' => $provider_id])
->andWhere(['start BETWEEN :start AND :end'])
->bind(':start', $start, 'datetime')
->bind(':end', $finish, 'datetime')
->order(['start' => 'ASC']);
//debug($events->count()); exit;
$s = $start;
if ($events->count() > 0) {
debug("hh"); exit;
echo "Events found \n";
foreach ($events as $event) {
$e = $event->start;
//echo $s . "\n";
//echo $e . "\n";
//echo "-------\n";
while (($e->diffInMinutes($s)) / $slotTime >= 1) {
$get_data_label = true;
$message = "Availablee between [events]\n {$s}\n to {$e} for {$e->diffInMinutes($s)} minutes. ";
//echo $message . "\n";
$e = $s->addMinute($slotTime);
$data[] = array(
'status' => true,
'start' => $s->toDateTimeString(),
'end' => $e->toDateTimeString(),
);
//return $data;
}
$s = $event->end;
}
}
//debug(($finish->diffInMinutes($s)) / $slotTime >= 1 ); exit;
while ( ($finish->diffInMinutes($s)) / $slotTime >= 1 ) {
//echo "No Events found \n";
//debug($st); exit;
$e = $s->addMinutes($slotTime);
debug("start". $s . "\n");
debug("end" . $e . "\n");
$data[] = array(
'status' => true,
'start' => $s->toDateTimeString(),
'end' => $e->toDateTimeString(),
);
$s = $e;
//return $data;
}
unset($start);
debug("----------------------------------------");
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment