private function _get_appointments_by_interval($start, $end, $schedule_key)
 {
     $apps = wp_cache_get('app-show_users-' . $schedule_key);
     if (!$apps) {
         $apps = $this->_get_appointments_for_scheduled_interval($schedule_key);
     }
     if (!$apps) {
         return false;
     }
     $ret = array();
     $period = new App_Period($start, $end);
     foreach ($apps as $app) {
         //if (mysql2date('U',$app->start) >= $start && mysql2date('U',$app->end) <= $end) $ret[] = $app;
         if ($period->contains($app->start, $app->end)) {
             $ret[] = $app;
         }
     }
     return $ret;
 }
Exemplo n.º 2
0
 /**
  * Check if a cell is not available, i.e. all appointments taken OR we dont have workers for this time slot
  * @return bool
  */
 function is_busy($start, $end, $capacity)
 {
     $week = date("W", $start);
     $period = new App_Period($start, $end);
     // If a specific worker is selected, we will look at his schedule first.
     if (0 != $this->worker) {
         $apps = $this->get_reserve_apps_by_worker($this->location, $this->worker, $week);
         if ($apps) {
             foreach ($apps as $app) {
                 //if ( $start >= strtotime( $app->start ) && $end <= strtotime( $app->end ) ) return true;
                 if ($period->contains($app->start, $app->end)) {
                     return true;
                 }
             }
         }
     }
     // If we're here, no worker is set or (s)he's not busy by default. Let's go for quick filter trip.
     $is_busy = apply_filters('app-is_busy', false, $period, $capacity);
     if ($is_busy) {
         return true;
     }
     // If we are here, no preference is selected (provider_id=0) or selected provider is not busy. There are 2 cases here:
     // 1) There are several providers: Look for reserve apps for the workers giving this service.
     // 2) No provider defined: Look for reserve apps for worker=0, because he will carry out all services
     if ($this->get_workers() != null) {
         $workers = $this->get_workers_by_service($this->service);
         $apps = array();
         if ($workers) {
             foreach ($workers as $worker) {
                 if ($this->is_working($start, $end, $worker->ID)) {
                     $app_worker = $this->get_reserve_apps_by_worker($this->location, $worker->ID, $week);
                     if ($app_worker && is_array($app_worker)) {
                         $apps = array_merge($apps, $app_worker);
                     }
                     // Also include appointments by general staff for services that can be given by this worker
                     $services_provided = $this->_explode($worker->services_provided);
                     if ($services_provided && is_array($services_provided) && !empty($services_provided)) {
                         foreach ($services_provided as $service_ID) {
                             $apps_service_0 = $this->get_reserve_apps($this->location, $service_ID, 0, $week);
                             if ($apps_service_0 && is_array($apps_service_0)) {
                                 $apps = array_merge($apps, $apps_service_0);
                             }
                         }
                     }
                 }
             }
             // Remove duplicates
             $apps = $this->array_unique_object_by_ID($apps);
         }
     } else {
         $apps = $this->get_reserve_apps_by_worker($this->location, 0, $week);
     }
     $n = 0;
     foreach ($apps as $app) {
         // @FIX: this will allow for "only one service and only one provider per time slot"
         if ($this->worker && $this->service && $app->service != $this->service) {
             continue;
             // This is for the following scenario:
             // 1) any number of providers per service
             // 2) any number of services
             // 3) only one service and only one provider per time slot:
             // 	- selecting one provider+service makes this provider and selected service unavailable in a time slot
             // 	- other providers are unaffected, other services are available
         }
         // End @FIX
         //if ( $start >= strtotime( $app->start ) && $end <= strtotime( $app->end ) ) $n++;
         if ($period->contains($app->start, $app->end)) {
             $n++;
         }
     }
     if ($n >= $this->available_workers($start, $end)) {
         return true;
     }
     // Nothing found, so this time slot is not busy
     return false;
 }
 /**
  * @param array $service_ids
  * @param App_Period $period
  *
  * @return int
  */
 private function _get_booked_appointments_for_period($service_ids, $period)
 {
     $start = date('Y-m-d H:i:s', $period->get_start());
     $end = date('Y-m-d H:i:s', $period->get_end());
     $services = join(',', array_filter(array_map('intval', $service_ids)));
     $sql = "SELECT COUNT(*) FROM {$this->_core->app_table} WHERE service IN ({$services}) AND end > '{$start}' AND start < '{$end}' AND status IN ('paid', 'confirmed')";
     $cnt = (int) $this->_core->db->get_var($sql);
     return $cnt;
 }