public function ajaxshowCalendar() { $id_lang = Context::getContext()->language->id; $id_shop = Tools::getValue('ids'); $shop = new AphStore($id_shop, Context::getContext()->language->id); $shop->hours = json_decode($shop->hours, true); $id_currency = Validate::isLoadedObject(Context::getContext()->currency) ? (int) Context::getContext()->currency->id : (int) Configuration::get('PS_CURRENCY_DEFAULT'); $context = Context::getContext(); $context->shop->id = $id_shop; $dates = array(); $dday = Tools::getValue('date'); for ($i = 2; $i > 0; $i--) { if (date('Y-m-d', strtotime('-' . $i . ' day', strtotime($dday))) >= date('Y-m-d')) { $dates[] = date('Y-m-d', strtotime('-' . $i . ' day', strtotime($dday))); } } $dates[] = $dday; for ($d = sizeof($dates), $i = 1; $d < 5; $d++, $i++) { $dates[] = date('Y-m-d', strtotime('+' . $i . ' day', strtotime($dday))); } $id_product = Tools::getValue('id_product'); $product = new Product($id_product, true, $id_lang, $id_shop); $product_need_tools = false; $product_need_employees = true; $service_duration = 0; $_features = Product::getFrontFeaturesStatic($id_lang, $id_product); foreach ($_features as &$feature) { if ($product->id_service_duration > 0 && $feature['id_feature'] == $product->id_service_duration) { $service_duration = (int) substr($feature['value'], 0, -1); } if ($product->id_service_duration < 1 && $feature['id_feature'] == Configuration::get('APH_FEATURE_DURATION')) { $service_duration = (int) substr($feature['value'], 0, -1); } if ($feature['id_feature'] == Configuration::get('APH_FEATURE_SERVICE_BY')) { if ((int) $feature['value'] == (int) Configuration::get('APH_FEATURE_SERVICE_BY_NO_EMPLOYEE')) { $product_need_employees = false; } } $service_duration = (int) substr($feature['value'], 0, -1); } $id_tool = 0; $necessary_quantity_of_tool = 0; $products_use_this_tool = array(); $tool = AphTool::getByShopAndProduct($id_shop, $id_product); if (!empty($tool)) { $id_tool = $tool[0]['id_tool']; $necessary_quantity_of_tool = $tool[0]['quantity']; $product_need_tools = true; $_products = AphTool::getProducts($id_tool); if (!empty($_products)) { foreach ($_products as &$_product) { $products_use_this_tool[] = $_product['id_product']; } } } $employees = array(); if ($product_need_employees) { $_employees = AphEmployeeProduct::getEmployeesByShopAndProduct($id_shop, $id_product); foreach ($_employees as &$employee) { $employees[] = $employee['id_employee']; } } $time_slice = (int) Configuration::get('APH_CALENDAR_TIME_SLICE_ONLINE'); if ($service_duration > $time_slice) { for ($t = 1; $t <= 4; $t++) { if ($time_slice * $t >= $service_duration) { $time_slice = $time_slice * $t; break; } } } /*$attributues_by_value = array(); $attributues = $product->getAttributesResume($id_lang, ': ', ', '); foreach ($attributues as &$attribute) { list($giorno, $orario) = explode(', ', $attribute['attribute_designation']); list(, $giorno) = explode(': ', $giorno); list(, $giorno) = explode(' ', $giorno); list(, $orario) = explode(': ', $orario); list(, $orario) = explode(' ', $orario); if (!array_key_exists($giorno, $attributues_by_value)) $attributues_by_value[$giorno] = array(); $attributues_by_value[$giorno][strval($orario)] = $attribute['id_product_attribute']; }*/ $first_day = $last_day = NULL; $days = array(); foreach ($dates as $day) { if (empty($first_day)) { $first_day = $day; } $last_day = $day; $days[$day] = array('date' => $day, 'date_ts' => strtotime($day), 'class' => 'store_opened', 'status' => 'opened', 'available' => true, 'price_from' => $product->price, 'times_tot' => 0, 'times_not_available' => 0, 'times_full' => 0, 'times' => array()); $days[$day]['date_lang'] = date('d/m/Y', $days[$day]['date_ts']); $days[$day]['week_day'] = date('w', $days[$day]['date_ts']); $days[$day]['week_day_lang'] = $this->week_days[(int) date('w', $days[$day]['date_ts'])]; $days[$day]['month_lang'] = $this->months[(int) date('w', $days[$day]['date_ts'])]; if (!empty($shop->hours[date('w', $days[$day]['date_ts'])])) { $hour_1 = $shop->hours[date('w', $days[$day]['date_ts'])][1]; $hour_2 = $shop->hours[date('w', $days[$day]['date_ts'])][2]; if ($hour_1['o'] == 'X' && $hour_1['c'] == 'X' && $hour_2['o'] == 'X' && $hour_2['c'] == 'X') { $days[$day]['status'] = 'closed'; $days[$day]['available'] = false; } else { $time_start = $hour_1['o'] == 'X' ? $hour_2['o'] : $hour_1['o']; $time_end = $hour_2['c'] == 'X' ? $hour_1['c'] : $hour_2['c']; $pause_start = $hour_1['c'] == 'X' ? false : $hour_1['c']; $pause_end = $hour_2['o'] == 'X' ? false : $hour_2['o']; } } else { $days[$day]['status'] = 'closed'; $days[$day]['available'] = false; } if (!empty($shop->rights_hasCalendar)) { if ($days[$day]['status'] == 'closed') { // apertura straordinaria $shop_availability = AphAvailability::getByDate($id_shop, 0, $day, 1); if (!empty($shop_availability)) { $days[$day]['status'] = 'opened'; $days[$day]['available'] = true; } } else { // chiusura straordinaria $shop_availability = AphAvailability::getByDate($id_shop, 0, $day, -1); if (!empty($shop_availability)) { $days[$day]['status'] = 'closed'; $days[$day]['available'] = false; } } if ($days[$day]['status'] == 'closed') { $days[$day]['class'] = 'store_closed'; $days[$day]['available'] = false; continue; } } // almeno un'offerta disponibile nel giorno $shop_offers = AphOffer::getByDate($id_shop, $id_product, $day, 1); if (!empty($shop_offers)) { $days[$day]['status'] = 'promoted'; $days[$day]['class'] = 'store_promoted'; } print_r($days[$day]['available']); if (!empty($days[$day]['available'])) { $times = array(); for ($hours = substr($time_start, 0, 2); $hours < substr($time_end, 0, 2); $hours++) { // the interval for hours is '1' for ($mins = 0; $mins < 60; $mins += $time_slice) { // the interval for mins is 'APH_CALENDAR_TIME_SLICE' $t = str_pad($hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($mins, 2, '0', STR_PAD_LEFT); // se sto valutanto l'orario nella pausa allora lo ignoro if (!empty($pause_start) && !empty($pause_end) && $t >= $pause_start && $t < $pause_end) { continue; } $times[$t] = array('time' => $t, 'available' => false, 'status' => 'available', 'class' => $days[$day]['class']); $times[$t]['from'] = $t; $time = new DateTime($day . ' ' . $times[$t]['from']); $time->add(new DateInterval('PT' . $service_duration . 'M')); $times[$t]['to'] = $time->format('H:i'); if ($times[$t]['to'] > $time_end) { $times[$t]['available'] = false; $times[$t]['status'] = 'available'; $times[$t]['class'] .= ' time_notavailable'; $days[$day]['times_not_available']++; } $days[$day]['times_tot']++; if (!empty($shop->rights_hasCalendar)) { $completed_orders = AphOrderDetail::getBetweenTimes($id_shop, 0, 0, $days[$day]['date_ts'], $times[$t]['from'], $times[$t]['to']); if ($product_need_tools && !empty($products_use_this_tool)) { $completed_orders_about_tool = AphOrderDetail::getBetweenTimes($id_shop, 0, $products_use_this_tool, $days[$day]['date_ts'], $times[$t]['from'], $times[$t]['to']); $t = AphTool::getByDate($id_tool, $available_date); $tool_is_availability = !empty($t) ? true : false; } else { $completed_orders_about_tool = array(); } $availabilies_of_shop = AphAvailability::getBetweenTimes($id_shop, 0, strtotime($day . ' ' . $times[$t]['from']), strtotime($day . ' ' . $times[$t]['to']), 1); $not_availabilies_of_shop = AphAvailability::getBetweenTimes($id_shop, 0, strtotime($day . ' ' . $times[$t]['from']), strtotime($day . ' ' . $times[$t]['to']), 0); if ($product_need_employees && empty($not_availabilies_of_shop)) { $working_employees = AphWorkingDay::getByEmployeeAndDateBetweenTimes($id_shop, $employees, $day, $times[$t]['from'], $times[$t]['to'], 1); $availabilies_of_employees = AphAvailability::getBetweenTimes($id_shop, $employees, strtotime($day . ' ' . $times[$t]['from']), strtotime($day . ' ' . $times[$t]['to']), 1); $not_availabilies_of_employees = AphAvailability::getBetweenTimes($id_shop, $employees, strtotime($day . ' ' . $times[$t]['from']), strtotime($day . ' ' . $times[$t]['to']), 0); } else { $working_employees = $availabilies_of_employees = $not_availabilies_of_employees = array(); } } // chiusura straordinaria if (!empty($not_availabilies_of_shop)) { $times[$t]['available'] = false; $times[$t]['status'] = 'closed'; $times[$t]['class'] .= ' time_closed'; $days[$day]['times_not_available']++; continue; } // slot di tempo esaurito per mancanza di dipendenti if ($product_need_employees && !empty($completed_orders) && !empty($working_employees) && sizeof($completed_orders) >= sizeof($working_employees) - sizeof($not_availabilies_of_employees)) { $times[$t]['available'] = false; $times[$t]['status'] = 'full'; $times[$t]['class'] .= ' time_full'; $days[$day]['times_full']++; continue; } // slot di tempo esaurito per mancanza di macchinari if ($product_need_tools && !$tool_is_availability || !empty($necessary_quantity_of_tool) && !empty($completed_orders_about_tool) && sizeof($completed_orders_about_tool) >= $necessary_quantity_of_tool) { $times[$t]['available'] = false; $times[$t]['status'] = 'full'; $times[$t]['class'] .= ' time_full'; $days[$day]['times_full']++; continue; } // apertura straordinaria (i macchinari sono sempre disponibili) if ($product_need_employees && !empty($availabilies_of_employees) && !empty($completed_orders) && !empty($availabilies_of_employees) && sizeof($completed_orders) >= sizeof($availabilies_of_employees) - sizeof($not_availabilies_of_employees)) { $times[$t]['available'] = false; $times[$t]['status'] = 'full'; $times[$t]['class'] .= ' time_full'; $days[$day]['times_full']++; continue; } $times[$t]['available'] = true; $times[$t]['status'] = 'available'; $times[$t]['class'] .= ' time_available'; if (!empty($times[$t]['available'])) { //error_log(var_export(__LINE__,true)); //$price = Product::getPriceStatic($id_product, true, 0, 2, null, false, true, 1, false, null, null, null, $specific_price, true, true, $context, true, $day, $times[$t]['from'], $times[$t]['to']); //$times[$t]['price'] = $price; $times[$t]['price'] = 0; $times[$t]['price_label'] = Tools::displayPrice($times[$t]['price'], $id_currency); if ($times[$t]['price'] < $days[$day]['price_from']) { $days[$day]['price_from'] = $times[$t]['price']; } } } } $days[$day]['times'] = $times; $days[$day]['price_from_label'] = Tools::displayPrice($days[$day]['price_from'], $id_currency); if ($days[$day]['times_full'] == $days[$day]['times']) { $days[$day]['available'] = false; $days[$day]['status'] = 'full'; $days[$day]['class'] = 'store_full'; } elseif ($days[$day]['times_not_available'] == $days[$day]['times']) { $days[$day]['available'] = false; $days[$day]['status'] = 'closed'; $days[$day]['class'] = 'store_closed'; } } } $jsVars = array(); $jsVars['calendar_params'] = array('ajax' => 1, 'ajaxCall' => 'ajaxshowCalendar', 'id_product' => $id_product, 'ids' => $id_shop, 'date' => $dday, 'id_city' => empty($id_city) ? $shop->id_city : $id_city); $this->context->smarty->assign(array('days' => $days, 'dday' => $dday, 'first_day' => $days[$first_day], 'last_day' => $days[$last_day], 'jsVars' => $jsVars)); // $jsVars = array( // 'language' => $this->context->language->iso_code, // 'slotDuration' => '00:'.Configuration::get('APH_CALENDAR_TIME_SLICE').':00' // ); // $this->content .= '<script>var admin_aphcalendar = '.json_encode($jsVars).';</script>'; return $this->display(__FILE__, 'availability_calendar.tpl', $this->getCacheId('blockmobilecart-column')); }
public function initContent() { parent::initContent(); $specific_price = NULL; $date = Tools::getValue('date'); if (empty($date) || empty($date)) { $date = date('Y-m-d', strtotime('+2 day')); } $id_product = Tools::getValue('id_product'); if (!empty($date)) { $product = new Product($id_product, true, Context::getContext()->language->id, Context::getContext()->shop->id); } if (empty($product)) { $this->setTemplate('availability_no_product.tpl'); error_log(__LINE__); return; } $id_shop = (int) Tools::getValue('ids'); $id_city = (int) Tools::getValue('id_city'); if (empty($id_shop) && empty($id_city)) { $this->setTemplate('availability_no_location.tpl'); error_log(__LINE__); return; } if (!empty($id_shop)) { $shop = NULL; $alias_shop = (int) Tools::getValue('ash'); if (!empty($alias_shop)) { $_s = AphStore::getByAlias($alias_shop, Context::getContext()->language->id); $id_shop = $_s['id_shop']; } else { $id_shop = (int) Tools::getValue('ids'); } $shop = new AphStore($id_shop, Context::getContext()->language->id); $cover = NULL; $images = AphStoreImage::getImages(Context::getContext()->language->id, $id_shop); foreach ($images as &$image) { if (!empty($image['cover'])) { $cover = $image; } } if (empty($cover) && !empty($images)) { $cover = $images[0]; } } if (empty($id_shop) && !empty($id_city)) { $city = AphCity::getCity($id_city); if (empty($city) && empty($id_shop)) { $this->setTemplate('availability_no_city.tpl'); return; } $shops = AphStore::getByCity($id_city, Context::getContext()->language->id); if (empty($shops)) { $shops = array(); $id_currency = Validate::isLoadedObject(Context::getContext()->currency) ? (int) Context::getContext()->currency->id : (int) Configuration::get('PS_CURRENCY_DEFAULT'); $_shops = AphStore::getStoresByProduct($id_product, Context::getContext()->language->id, null, 100000); foreach ($_shops as &$shop) { if ($this->distance($shop['latitude'], $shop['longitude'], $city['latitude'], $city['longitude'], 'K') > Configuration::get('APH_MAX_KM_FAR_AWAY')) { continue; } $product = new Product($id_product, false, (int) Context::getContext()->language->id, (int) $shop['id_shop']); $price = Product::getPriceStatic($id_product, true, 0, 2, null, false, true, 1, false, null, null, null, $specific_price, true, true, Context::getContext(), true, $date); if (empty($price)) { $price = $product->price; } else { $price = $price; } $shop['price_from'] = Tools::displayPrice($price, $id_currency); //$url = !empty($ssl) ? 'https://'.$shop['domain_ssl'] : 'http://'.$shop['domain']; //$shop['url'] = $url.$shop['uri']; $shop['url'] = Context::getContext()->link->getModuleLink('blockmobilecart', 'availability', array('id_product' => $id_product, 'date' => $date, 'ids' => $shop['id_shop'])); $shop['img_link_rewrite'] = Tools::link_rewrite($shop['legend']); $shops[] = $shop; } if (empty($shops)) { $this->context->smarty->assign(array('availability_no_closure_shops' => true)); } else { $this->context->smarty->assign(array('shops' => $shops, 'availability_no_shops' => true, 'availability_no_shops_city' => $city['name'], 'availability_no_shops_km' => Configuration::get('APH_MAX_KM_FAR_AWAY'))); } $this->setTemplate('availability_shops.tpl'); return; } } else { $shop = new AphStore($id_shop, Context::getContext()->language->id); if (empty($shop) && empty($id_city)) { $this->setTemplate('availability_no_shop.tpl'); error_log(__LINE__); return; } // il negozio non offre il servizio $product = new Product($id_product, true, Context::getContext()->language->id, $id_shop); $employees = AphEmployeeProduct::getEmployeesByShopAndProduct($id_shop, $id_product); if (empty($employees)) { $products = AphStore::getProductsByStore($id_shop, Context::getContext()->language->id, 0, 20, 'name', 'asc', $product->id_category_default, true); foreach ($products as &$product) { $p = new Product($product['id_product'], false, (int) Context::getContext()->language->id, $id_shop); $price = Product::getPriceStatic($product['id_product'], true, 0, 2, null, false, true, 1, false, null, null, null, $specific_price, true, true, Context::getContext(), true, $date); if (empty($price)) { $price = $p->price; } else { $price = $price; } $product['price_from'] = Tools::displayPrice($price, $id_currency); } $this->context->smarty->assign(array('shop' => $shop, 'cover' => $cover, 'images' => $images, 'products' => $products, 'token' => Tools::getToken(false))); $this->setTemplate('availability_no_product_for_shop.tpl'); return; } } if (empty($product)) { $this->setTemplate('availability_no_product.tpl'); return; } $this->context->smarty->tpl_vars['meta_title']->value = 'Acquista ' . $product->name . ' presso ' . $shop->name; $this->context->smarty->tpl_vars['meta_description']->value = $shop->meta_description; // $products_by_category = array(); // $categories = Category::getAllCategoriesName(NULL, Context::getContext()->language->id, true); // if (!empty($categories)){ // foreach ($categories as &$category) { // $products = AphStore::getProductsByStore($id_shop, Context::getContext()->language->id, NULL, NULL, 'name', 'desc', $category['id_category'], true); // foreach ($products as &$product) { // if (empty($products_by_category[ $category['id_category'] ])){ // $products_by_category[ $category['id_category'] ] = array( // 'id_category' => $category['id_category'], // 'name' => $category['name'], // 'class' => Tools::link_rewrite($category['name']), // 'products' => array() // ); // } // $products_by_category[ $category['id_category'] ]['products'][ $product['id_product'] ] = array( // 'id_product' => $product['id_product'], // 'name' => $product['name'], // 'link' => Context::getContext()->link->getProductLink((int)$product['id_product'], $product['link_rewrite'], $category['id_category'], $product['ean13']) // ); // } // } // } $jsVars = array(); $jsVars['calendar_params'] = array('ajax' => 1, 'ajaxCall' => 'ajaxshowCalendar', 'id_product' => $id_product, 'ids' => $id_shop, 'date' => $date, 'id_city' => empty($id_city) ? $shop->id_city : $id_city); $this->context->smarty->assign(array('shop' => $shop, 'cover' => $cover, 'images' => $images, 'product' => $product, 'token' => Tools::getToken(false), 'jsVars' => $jsVars)); $this->setTemplate('availability_full.tpl'); }