cart->id; } $scopes = array(); /* supplier overrides */ $ddw_scope_helper = DDWScopeHelper::getInstance('supplier', Context::getContext()->language->id, $id_shop); $supplier_scopes = $ddw_scope_helper->getCartCollection($id_cart, $id_carrier); if (is_array($supplier_scopes) && count($supplier_scopes) > 0) { $scopes[] = $supplier_scopes[0]; } /* category overrides */ $ddw_scope_helper = DDWScopeHelper::getInstance('category', Context::getContext()->language->id, $id_shop); $category_scopes = $ddw_scope_helper->getCartCollection($id_cart, $id_carrier); if (is_array($category_scopes) && count($category_scopes) > 0) { $scopes[] = $category_scopes[0]; } /* product overrides */ $ddw_scope_helper = DDWScopeHelper::getInstance('product', Context::getContext()->language->id, $id_shop); $product_scopes = $ddw_scope_helper->getCartCollection($id_cart, $id_carrier); if (is_array($product_scopes) && count($product_scopes) > 0) { $scopes[] = $product_scopes[0]; } $weekdays_final = DDWScopeHelper::uniqueWeekDays(array_merge($category_scopes, $product_scopes, $supplier_scopes)); if (!empty($scopes)) { usort($scopes, function ($a, $b) { return $b['min_days'] - $a['min_days']; }); $scopes[0]['weekdays'] = json_encode($weekdays_final); return $scopes[0]; } else { return false; } } /** * determine if a date is blocked * @param $blocked_dates * @param $weekdays * @param $date * @return bool */ public static function isDateBlocked($blocked_dates, $weekdays, $date) { $blocked = false; // Weekdays Blocked check $current_week_day = date('w', $date); if (!in_array($current_week_day, $weekdays)) { return true; } if (!is_array($blocked_dates)) { return false; } foreach ($blocked_dates as $blocked_date) { if ($blocked_date->recurring == 0) { $y = date('Y', strtotime($blocked_date->start_date)); $m = date('m', strtotime($blocked_date->start_date)); $d = date('d', strtotime($blocked_date->start_date)); if ($blocked_date->start_date == $blocked_date->end_date) { $blocked_date->end_date = date('Y-m-d H:i:s', strtotime("$y-$m-$d 23:59:59")); } } $timestamp_start = strtotime($blocked_date->start_date); $timestamp_end = strtotime($blocked_date->end_date); if ($date >= $timestamp_start && $date <= $timestamp_end) { $blocked = true; } /* Recurring block check */ if ($blocked_date->recurring == 1) { $recurring_timestamp_start = date('Y-m-d H:i:s', strtotime( date('Y') . '-' . date('m', $timestamp_start) . '-' . date('d', $timestamp_start) . ' 00:00:00' )); $recurring_timestamp_end = date('Y-m-d H:i:s', strtotime( date('Y') . '-' . date('m', $timestamp_end) . '-' . date('d', $timestamp_end) . ' 23:59:00' )); if ($date >= $recurring_timestamp_start && $date <= $recurring_timestamp_end) { $blocked = true; } } } return $blocked; } /** * @param $override_scope * @param $start_date * @param $end_date * @param $id_carrier * @param $id_shop * @param int $id_cart * @return array * @throws PrestaShopDatabaseException * @throws PrestaShopException */ public static function getBlockedDates($override_scope, $start_date, $end_date, $id_carrier, $id_shop, $id_cart = 0) { if ($id_cart == 0) { $id_cart = Context::getContext()->cart->id; } $return = array(); $weekdays = array(); if (empty($override_scope['id_carrier'])) { $id_carrier = $id_carrier; } else { $id_carrier = (int)$override_scope['id_carrier']; } $calendar_blocked_dates = array(); $ddw_model = new DDWModel(); $ddw_model->loadByCarrier($id_carrier, $id_shop); $cutofftime_enabled = $ddw_model->cutofftime_enabled; $cutofftime_hours = $ddw_model->cutofftime_hours; $cutofftime_minutes = $ddw_model->cutofftime_minutes; $min_days = $ddw_model->min_days; // Load available weekdays $ddw_weekdays = new DDWWeekdayModel(); $ddw_weekdays = $ddw_weekdays->getAvailableWeekdaysCollection($id_carrier, $id_shop); $weekdays = array(); if (!empty($ddw_weekdays)) { foreach ($ddw_weekdays as $ddw_weekday) { $weekdays[] = $ddw_weekday->id_weekday; } } $blocked_dates = new DDWBlockedDate(); $blocked_dates_collection = $blocked_dates->getCollection($id_carrier, $id_shop); $today_is_blocked = self::isDateBlocked($blocked_dates_collection, $weekdays, time()); // week day settings $current_week_day = date('w'); $ddw_weekday = new DDWWeekdayModel(); $ddw_weekday->loadWeekday($id_carrier, $current_week_day, $id_shop); if ($ddw_weekday->cutofftime_enabled) { $cutofftime_enabled = $ddw_weekday->cutofftime_enabled; $cutofftime_hours = $ddw_weekday->cutofftime_hours; $cutofftime_minutes = $ddw_weekday->cutofftime_minutes; } /* ovverride the min / max day for the current weekday if setting enabled */ if ($ddw_weekday->minmax_enabled) { $min_days = $ddw_weekday->min_days; $max_days = $ddw_weekday->max_days; } /* Specific Dates Settings */ $ddw_specificdate_model = new DDWSpecificDateModel(); $ddw_specificdate_model->loadByDate(date('Y-m-d'), $id_carrier, $id_shop); if ($ddw_specificdate_model->id_ddw_specificdate > 0) { if ($ddw_specificdate_model->cutofftime_enabled) { $cutofftime_enabled = $ddw_specificdate_model->cutofftime_enabled; $cutofftime_hours = $ddw_specificdate_model->cutofftime_hours; $cutofftime_minutes = $ddw_specificdate_model->cutofftime_minutes; } if ($ddw_specificdate_model->minmax_enabled) { $min_days = $ddw_specificdate_model->min_days; $max_days = $ddw_specificdate_model->max_days; } } // override min / max based on scope exceptions (supplier, category and product) if ($override_scope == null) { $scope = self::getScopeExceptions($id_carrier, $id_shop, $id_cart); if ($scope != false) { // override weekdays available $scope_weekdays = json_decode($scope['weekdays']); $weekdays = $scope_weekdays; if ($scope['min_days'] > $min_days) { $min_days = $scope['min_days']; } $max_days = $scope['max_days']; if ($scope['cutofftime_enabled']) { $cutofftime_hours = $scope['cutofftime_hours']; $cutofftime_minutes = $scope['cutofftime_minutes']; } else { $cutofftime_enabled = false; } } } else { if ($override_scope['min_days'] > $min_days) { $min_days = $override_scope['min_days']; } $max_days = $override_scope['max_days']; if ($override_scope['cutofftime_enabled']) { $cutofftime_hours = $override_scope['cutofftime_hours']; $cutofftime_minutes = $override_scope['cutofftime_minutes']; } else { $cutofftime_enabled = false; } } if (empty($max_days)) { $max_days = ($ddw_model->max_days == 0 ? 365 : $ddw_model->max_days); } // block the next day after max days. not the day itself if ($max_days > 0) { $max_days++; } // Determine if cut off time requires Min Days to be blocked from today onwards $cutofftime_passed = false; if (($cutofftime_enabled == 1 && !$today_is_blocked) || $ddw_weekday->cutofftime_enabled == 1) { $hours = date('H'); $minutes = date('i'); if ($hours > $cutofftime_hours) { $cutofftime_passed = true; } elseif ($hours == $cutofftime_hours && $minutes >= $cutofftime_minutes) { $cutofftime_passed = true; } if ($cutofftime_passed) { $min_days++; } } // check order limit has not passed for the day $date = date('Y-m-d'); if (DDWOrderHelper::orderLimitExceeded($date, $id_carrier, $id_shop) && $min_days == 0) { $today_is_blocked = true; } if ($today_is_blocked) { $min_days++; } // Adjust min days to post cut off time for weekday if necessary if ($ddw_weekday->minmax_enabled && $cutofftime_passed && $ddw_weekday->cutofftime_enabled && $ddw_weekday->min_days_postcutoff > 0) { $min_days = $ddw_weekday->min_days_postcutoff + 1; } // Adjust min days to post cut off time for specific date if necessary if ($ddw_specificdate_model->minmax_enabled && $cutofftime_passed && $ddw_specificdate_model->cutofftime_enabled && $ddw_specificdate_model->min_days_postcutoff > 0) { $min_days = $ddw_specificdate_model->min_days_postcutoff + 1; } /* If any of the days within min days falls on a block date, increment min days accordingly */ $j = $min_days; for ($i = 0; $i < $j; $i++) { $loop_date = strtotime("+$i day", strtotime($start_date)); $today = date('Y-m-d'); $loop_date_compare = date('Y-m-d', $loop_date); /* if today is blocked and cut off time, do not add today to min days offset */ if ($today == $loop_date_compare && $today_is_blocked) { continue; } else { if (self::isDateBlocked($blocked_dates_collection, $weekdays, $loop_date)) { $min_days++; break; } } } // Block all days up to min_days from order date (today) for ($i = 0; $i < $min_days; $i++) { $loop_date = strtotime("+$i day", strtotime($start_date)); $calendarBlockedDate = new TDDWCalendarBlockedDate(); $calendarBlockedDate->date = date('Y-m-d', $loop_date); $calendarBlockedDate->blocked = true; $calendarBlockedDate->reason = 'min_day'; $calendar_blocked_dates[] = $calendarBlockedDate; } // Loop through dates for ($i = 0; $i < $max_days; $i++) { $loop_date = strtotime("+$i day", strtotime($start_date)); $loop_blocked = false; if (DDWOrderHelper::orderLimitExceeded(date('Y-m-d', $loop_date), $id_carrier, $id_shop)) { $loop_blocked = true; } if (self::isDateBlocked($blocked_dates_collection, $weekdays, $loop_date) || $loop_blocked) { $calendarBlockedDate = new TDDWCalendarBlockedDate(); $calendarBlockedDate->date = date('Y-m-d', $loop_date); $calendarBlockedDate->blocked = true; $calendar_blocked_dates[] = $calendarBlockedDate; } } // remove calendar blocked dates which are allowed in specific dates $allowed_dates = $ddw_specificdate_model->getAll($id_carrier, $id_shop); $allowed_dates_arr = array(); foreach ($allowed_dates as $key => $allowed_date) { if ($allowed_date->enabled) { $allowed_dates_arr[$allowed_date->start_date] = $allowed_date->start_date; } } $calendar_blocked_dates_tmp = array(); foreach ($calendar_blocked_dates as $blocked_date) { if (empty($allowed_dates_arr[$blocked_date->date])) { $calendar_blocked_dates_tmp[] = $blocked_date; } elseif (!empty($allowed_dates_arr[$blocked_date->date]) && $blocked_date->reason == 'min_day') { $calendar_blocked_dates_tmp[] = $blocked_date; } } $calendar_blocked_dates = $calendar_blocked_dates_tmp; // Fix bug which caused last day of max date to become selecatble when it fell within a blocked date range $calendarBlockedDate = new TDDWCalendarBlockedDate(); $calendarBlockedDate->date = date('Y-m-d', strtotime('+' . $max_days . ' days')); $calendarBlockedDate->blocked = true; $calendar_blocked_dates[] = $calendarBlockedDate; // If enabled/disabled if ($ddw_model->enabled != 1) { $calendar_blocked_dates = array(); $calendar_blocked_dates['enabled'] = false; $return['enabled'] = false; return $return; } // Create return data $return['min_date'] = date('Y-m-d'); $return['max_date'] = date('Y-m-d', strtotime('+' . $max_days . ' days')); $return['calendar_blocked_dates'] = $calendar_blocked_dates; $return['enabled'] = $ddw_model->enabled; $return['required'] = $ddw_model->required; // get first available day for delivery, allowing the calendar to change months if necessary $start_date = date('Y-m-d'); $haystack = array(); foreach ($calendar_blocked_dates as $date_obj) { $haystack[] = $date_obj->date; } // now get first available day for delivery and check if that day has timeslots available $loop_date = $start_date; $ddw_timeslots = new DDWTimeslotModel(); $timeslot_check_complete = false; $recheck_timeslots = false; for ($i = 0; $i < 9999; $i++) { $loop_date = date('Y-m-d', strtotime("+$i day", strtotime($start_date))); if (!in_array($loop_date, $haystack)) { if (!$timeslot_check_complete) { $day_of_week = date('w', strtotime($loop_date)); $ddw_specificdate_model = new DDWSpecificDateModel(); $ddw_specificdate_model->loadByDate(date('Y-m-d', strtotime($loop_date)), $id_carrier, $id_shop); if ($ddw_specificdate_model->id_ddw_specificdate > 0) { $timeslots_collection_unfiltered = $ddw_timeslots->getTimeSlotsForSpecificDate($ddw_specificdate_model->id_ddw_specificdate, $id_carrier, $id_shop); } else { $timeslots_collection_unfiltered = $ddw_timeslots->getTimeSlotsForWeekday($id_carrier, $day_of_week, $id_shop); } $ddw_override_helper = new DDWOverrideHelper($loop_date, $id_carrier, $id_shop); $timeslots = self::filterTimeslots($timeslots_collection_unfiltered, $loop_date, $ddw_override_helper->timeslots_prep_minutes, $id_carrier, $id_shop); if (count($timeslots_collection_unfiltered) > 0 && empty($timeslots)) { $timeslot_check_complete = true; $calendarBlockedDate = new TDDWCalendarBlockedDate(); $calendarBlockedDate->date = date($loop_date); $calendarBlockedDate->blocked = true; array_unshift($calendar_blocked_dates, $calendarBlockedDate); } else { break; } } else { break; } } } // Get default timeslots for first available day if ($recheck_timeslots) { $haystack = array(); foreach ($calendar_blocked_dates as $date_obj) { $haystack[] = $date_obj->date; } for ($i = 0; $i < 9999; $i++) { $loop_date = date('Y-m-d', strtotime("+$i day", strtotime($start_date))); if (!in_array($loop_date, $haystack)) { $day_of_week = date('w', strtotime($loop_date)); $ddw_specificdate_model = new DDWSpecificDateModel(); $ddw_specificdate_model->loadByDate(date('Y-m-d', strtotime($loop_date)), $id_carrier, $id_shop); if ($ddw_specificdate_model->id_ddw_specificdate > 0) { $timeslots_collection_unfiltered = $ddw_timeslots->getTimeSlotsForSpecificDate($ddw_specificdate_model->id_ddw_specificdate, $id_carrier, $id_shop); } else { $timeslots_collection_unfiltered = $ddw_timeslots->getTimeSlotsForWeekday($id_carrier, $day_of_week, $id_shop); } $ddw_override_helper = new DDWOverrideHelper($loop_date, $id_carrier, $id_shop); $timeslots = self::filterTimeslots($timeslots_collection_unfiltered, $loop_date, $ddw_override_helper->timeslots_prep_minutes, $id_carrier, $id_shop); break; } } } $return['calendar_blocked_dates'] = $calendar_blocked_dates; $return['defaults']['calendar_default_day'] = date('d', strtotime($loop_date)); $return['defaults']['calendar_default_month'] = date('m', strtotime($loop_date)); $return['defaults']['calendar_default_year'] = date('Y', strtotime($loop_date)); $return['defaults']['timeslots'] = $timeslots; return $return; } /** * Get available days for delivery * @param $override_scope * @param $start_date * @param $day_count * @param $id_carrier * @param $id_shop * @param int $id_cart * @return array */ public static function getAvailableDates($override_scope, $start_date, $day_count, $id_carrier, $id_shop, $id_cart = 0) { $available_dates = array(); $end_date = date('Y-m-d', strtotime($start_date . ' + 99 days')); $blocked_dates = self::getBlockedDates($override_scope, $start_date, $end_date, $id_carrier, $id_shop, $id_cart); $blocked_dates_flat = array(); foreach ($blocked_dates['calendar_blocked_dates'] as $date_obj) { $blocked_dates_flat[] = $date_obj->date; } for ($i = 0; $i < 60; $i++) { $loop_date = date('Y-m-d', strtotime("+$i day", strtotime($start_date))); if (!in_array($loop_date, $blocked_dates_flat)) { $available_dates[] = $loop_date; } if ($i >= $day_count) { break; } } return $available_dates; } /** * Gets a list of days between start and end date with time slots for each day and indication if day and timeslot is available * @param $blocked_dates * @param $start_date * @param $end_date */ public static function getDatesAndTimeslots($blocked_dates, $start_date, $end_date, $id_carrier, $id_shop) { $blocked_days_hash = array(); $days = array(); $ddw_timeslots = new DDWTimeslotModel(); $ddw_model = new DDWModel(); $ddw_model->loadByCarrier($id_carrier, $id_shop); if (!$ddw_model->enabled) { return ''; } // create flat look up array for blocked dates if (!empty($blocked_dates['calendar_blocked_dates'])) { foreach ($blocked_dates['calendar_blocked_dates'] as $blocked_date) { $blocked_days_hash[$blocked_date->date] = '0'; } } $i = 0; $loop = true; while ($loop == true) { $loop_date = strtotime("+$i day", strtotime($start_date)); $loop_date_readable = date('Y-m-d', $loop_date); if (!isset($blocked_days_hash[$loop_date_readable])) { $days[$loop_date_readable] = array( 'date' => $loop_date_readable, 'available' => 1 ); } else { $days[$loop_date_readable] = array( 'date' => $loop_date_readable, 'available' => 0 ); } if ($loop_date >= strtotime($end_date)) { $loop = false; } $i++; if ($loop > 1000) { $loop = false; } } foreach ($days as $day) { $loop_date = $day['date']; $day_of_week = date('w', strtotime($loop_date)); $ddw_specificdate_model = new DDWSpecificDateModel(); if ($ddw_specificdate_model->id_ddw_specificdate > 0) { $timeslots_collection_unfiltered = $ddw_timeslots->getTimeSlotsForSpecificDate($ddw_specificdate_model->id_ddw_specificdate, $id_carrier, $id_shop); } else { $timeslots_collection_unfiltered = $ddw_timeslots->getTimeSlotsForWeekday($id_carrier, $day_of_week, $id_shop); } $ddw_override_helper = new DDWOverrideHelper($loop_date, $id_carrier, $id_shop); $timeslots = self::filterTimeslots($timeslots_collection_unfiltered, $loop_date, $ddw_override_helper->timeslots_prep_minutes, $id_carrier, $id_shop); // create hash arrays $timeslots_unfiltered = array(); foreach ($timeslots_collection_unfiltered as $timeslots_collection) { $timeslots_unfiltered[$timeslots_collection->id] = array( 'id' => $timeslots_collection->id, 'id_carrier' => $timeslots_collection->id_carrier, 'id_weekday' => $timeslots_collection->id_weekday, 'id_ddw_specificdate' => $timeslots_collection->id_ddw_specificdate, 'enabled' => $timeslots_collection->enabled, 'time_start' => $timeslots_collection->time_start, 'time_end' => $timeslots_collection->time_end, 'label' => $timeslots_collection->time_start . ' - ' . $timeslots_collection->time_end ); } $timeslots_filtered = array(); foreach ($timeslots as $timeslot) { $timeslots_filtered[$timeslot->id] = array( 'id' => $timeslot->id, 'id_carrier' => $timeslot->id_carrier, 'id_weekday' => $timeslot->id_weekday, 'id_ddw_specificdate' => $timeslot->id_ddw_specificdate, 'enabled' => $timeslot->enabled, 'available' => $timeslot->available, 'time_start' => $timeslot->time_start, 'time_end' => $timeslot->time_end, 'label' => $timeslot->time_start . ' - ' . $timeslot->time_end ); } foreach ($timeslots_unfiltered as $timeslot_unfiltered) { $tmp_timeslot = $timeslot_unfiltered; if (!isset($timeslots_filtered[$timeslot_unfiltered['id']]) || $tmp_timeslot['enabled'] == 0) { $tmp_timeslot['available'] = 0; } elseif (isset($timeslots_filtered[$timeslot_unfiltered['id']])) { $tmp_timeslot['available'] = $timeslots_filtered[$timeslot_unfiltered['id']]['available']; } $days[$loop_date]['timeslots'][$timeslot_unfiltered['label']] = $tmp_timeslot; } // if time slots not available on this day if (count($timeslots_collection_unfiltered) > 0 && empty($timeslots)) { $days[$loop_date]['available'] = 0; } } //get unique time columns $time_columns = array(); foreach ($days as $day) { if (!empty($day['timeslots'])) { foreach ($day['timeslots'] as $day_timeslot) { $time_columns[$day_timeslot['label']] = array( 'time_start' => $day_timeslot['time_start'], 'time_end' => $day_timeslot['time_end'], 'label' => $day_timeslot['label'] ); } } } // make sure each day has timeslots (even if unavailable) foreach ($days as &$day) { foreach ($time_columns as $time_column) { $tmp = $time_column; if (empty($day['timeslots'][$time_column['label']])) { $tmp['available'] = 0; $day['timeslots'][$time_column['label']] = $tmp; } } $found = 0; foreach ($day['timeslots'] as $day_timeslot) { if ($day_timeslot['available'] == 1) { $found++; } } $day['timeslot_available_count'] = $found; $day['weekday'] = date('w', strtotime($day['date'])); } //now sort the timeslots in time order foreach ($days as &$day) { if (!empty($day['timeslots'])) { usort($day['timeslots'], function ($a, $b) { return str_replace(':', '.', $a['time_start']) - str_replace(':', '.', $b['time_start']); }); } } return array( 'days' => $days, 'column_count' => count($time_columns) + 1 ); } /** * Filter the timeslots by time of day and orders placed * @param $timeslot_collection * @param $date * @param $timeslots_prep_minutes * @param $id_carrier * @param $id_shop * @return array */ public static function filterTimeslots($timeslot_collection, $date, $timeslots_prep_minutes, $id_carrier, $id_shop) { $timeslots = array(); //only filter todays timeslots if (!empty($timeslot_collection) && $date == date('Y-m-d')) { foreach ($timeslot_collection as $timeslot) { $seconds_timeslot_start = strtotime(date('Ymd') . $timeslot->time_start . ':00'); $seconds_timeslot_end = strtotime(date('Ymd') . $timeslot->time_end . ':00'); $seconds_now = time(); $min = ($seconds_timeslot_start - $seconds_now) / 60; if ($min > 0) { if ((int)$timeslots_prep_minutes > 0 && $min >= $timeslots_prep_minutes) { $timeslots[] = $timeslot; } elseif ($timeslots_prep_minutes == 0) { $timeslots[] = $timeslot; } } } } else { $timeslots = $timeslot_collection; } if (!empty($timeslots)) { foreach ($timeslots as &$timeslot) { $count = DDWOrderHelper::getOrderCountInTimeWindow($date, $timeslot->time_start . ' - ' . $timeslot->time_end, $id_shop, $id_carrier); if ($timeslot->order_limit > 0) { if ($count < $timeslot->order_limit) { $timeslot->available = 1; } else { $timeslot->available = 0; } } else { $timeslot->available = 1; } if (!$timeslot->enabled) { $timeslot->available = 0; } } } return $timeslots; } /** * Get timeslots for a specific date * @param $date * @param $id_carrier * @param $id_shop * @throws PrestaShopDatabaseException * @throws PrestaShopException */ public static function getTimeslotsForDate($date, $id_carrier, $id_shop) { $ddw_timeslots = new DDWTimeslotModel(); $day_of_week = date('w', strtotime($date)); $ddw_specificdate_model = new DDWSpecificDateModel(); $ddw_specificdate_model->loadByDate($date, $id_carrier, $id_shop); $timeslots = array(); if ($ddw_specificdate_model->id_ddw_specificdate > 0) { $timeslots_collection_unfiltered = $ddw_timeslots->getTimeSlotsForSpecificDate($ddw_specificdate_model->id_ddw_specificdate, $id_carrier, $id_shop); } else { $timeslots_collection_unfiltered = $ddw_timeslots->getTimeSlotsForWeekday($id_carrier, $day_of_week, $id_shop); } if (empty($timeslots_collection_unfiltered)) { $timeslots[$date] = array( 'timeslots' => array(), 'realcount' => 0, 'available' => 0 ); return $timeslots; } $ddw_override_helper = new DDWOverrideHelper($date, $id_carrier, $id_shop); $timeslots = self::filterTimeslots($timeslots_collection_unfiltered, $date, $ddw_override_helper->timeslots_prep_minutes, $id_carrier, $id_shop); // create hash arrays $timeslots_unfiltered = array(); foreach ($timeslots_collection_unfiltered as $timeslots_collection) { $timeslots_unfiltered[$timeslots_collection->id] = array( 'id' => $timeslots_collection->id, 'id_carrier' => $timeslots_collection->id_carrier, 'id_weekday' => $timeslots_collection->id_weekday, 'id_ddw_specificdate' => $timeslots_collection->id_ddw_specificdate, 'enabled' => $timeslots_collection->enabled, 'time_start' => $timeslots_collection->time_start, 'time_end' => $timeslots_collection->time_end, 'label' => $timeslots_collection->time_start . ' - ' . $timeslots_collection->time_end ); } $timeslots_filtered = array(); foreach ($timeslots as $timeslot) { $timeslots_filtered[$timeslot->id] = array( 'id' => $timeslot->id, 'id_carrier' => $timeslot->id_carrier, 'id_weekday' => $timeslot->id_weekday, 'id_ddw_specificdate' => $timeslot->id_ddw_specificdate, 'enabled' => $timeslot->enabled, 'available' => $timeslot->available, 'time_start' => $timeslot->time_start, 'time_end' => $timeslot->time_end, 'label' => $timeslot->time_start . ' - ' . $timeslot->time_end ); } $timeslots = array(); foreach ($timeslots_unfiltered as $timeslot_unfiltered) { $tmp_timeslot = $timeslot_unfiltered; if (!isset($timeslots_filtered[$timeslot_unfiltered['id']]) || $tmp_timeslot['enabled'] == 0) { $tmp_timeslot['available'] = 0; } elseif (isset($timeslots_filtered[$timeslot_unfiltered['id']])) { $tmp_timeslot['available'] = $timeslots_filtered[$timeslot_unfiltered['id']]['available']; } $timeslots[$date]['timeslots'][$timeslot_unfiltered['label']] = $tmp_timeslot; } unset($timeslot); $timeslot_realcount = 0; foreach ($timeslots[$date]['timeslots'] as $timeslot) { if ($timeslot['enabled']) { $timeslot_realcount++; } } $timeslots[$date]['realcount'] = $timeslot_realcount; // if time slots not available on this day if ((count($timeslots_collection_unfiltered) > 0 && empty($timeslots)) || count($timeslots_collection_unfiltered) == 0) { $timeslots[$date]['available'] = 0; } else { $timeslots[$date]['available'] = 1; } return $timeslots; } /** * Check if a specific date and time is available for delivery * @param $date * @param $time * @param $id_timeslot * @param $id_carrier * @param $id_cart * @return bool|string * @throws PrestaShopDatabaseException * @throws PrestaShopException */ public static function isDateTimeAvailable($date, $time, $id_timeslot, $id_carrier, $id_cart) { $id_shop = Context::getContext()->shop->id; $day_of_week = date('w', strtotime($date)); $ddw_timeslots = new DDWTimeslotModel(); $ddw_model = new DDWModel(); $ddw_model->loadByCarrier($id_carrier, Context::getContext()->shop->id); if (DDWDateTimeHelper::isDateInPast($date)) { return false; } if (!$ddw_model->enabled) { return ''; } if ((int)$id_timeslot > 0) { $timeslot = new DDWTimeslotModel($id_timeslot); if (empty($timeslot->id)) { return false; } } if ((int)$ddw_model->max_days == 0) { $max_days = 14; } else { $max_days = $ddw_model->max_days; } $table_start_date = $date; $table_end_date = date('Y-m-d', strtotime($table_start_date . ' + ' . $max_days . ' days')); $blocked_dates = self::getBlockedDates(null, date('Y-m-d'), $table_end_date, $id_carrier, $id_shop, $id_cart); foreach ($blocked_dates['calendar_blocked_dates'] as $blocked_date) { if ($blocked_date->date == $date && $blocked_date->blocked == 1) { return false; } } $ddw_specificdate_model = new DDWSpecificDateModel(); $ddw_specificdate_model->loadByDate($date, $id_carrier, $id_shop); if ($ddw_specificdate_model->id_ddw_specificdate > 0) { $timeslots_collection_unfiltered = $ddw_timeslots->getTimeSlotsForSpecificDate($ddw_specificdate_model->id_ddw_specificdate, $id_carrier, $id_shop); } else { $timeslots_collection_unfiltered = $ddw_timeslots->getTimeSlotsForWeekday($id_carrier, $day_of_week, $id_shop); } $ddw_override_helper = new DDWOverrideHelper($date, $id_carrier, $id_shop); if ($id_timeslot > 0) { if (strtotime($date) == strtotime(date('Y-m-d'))) { // if the date being checked today, then check timeslot have not passed $time_start = DDWTimeslotHelper::getTimeStartFromTimeslot($time); $time_end = DDWTimeslotHelper::getTimeEndFromTimeslot($time); return DDWTimeslotHelper::hasTimeslotPassed($date, $time_start, $time_end, $id_carrier, $id_shop); } else { // check if future time slots is available $timeslot_available = false; $timeslots = self::filterTimeslots($timeslots_collection_unfiltered, $date, $ddw_override_helper->timeslots_prep_minutes, $id_carrier, $id_shop); if (!empty($timeslots)) { foreach ($timeslots as $timeslot) { $time_start = DDWTimeslotHelper::getTimeStartFromTimeslot($time); $time_end = DDWTimeslotHelper::getTimeEndFromTimeslot($time); if ($time_start == $timeslot->time_start && $time_end == $timeslot->time_end && $timeslot->enabled == true) { $timeslot_available = true; break; } } } return $timeslot_available; } } else { return true; } } }