예제 #1
0
 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'));
 }