static function getOffersCalendar($offers, $initialNrDays, $adults, $children, $month, $year, $bookings, $temporaryReservedRooms, $hotelAvailability)
 {
     $offersCalendar = array();
     $endDay = date('t', mktime(0, 0, 0, $month, 1, $year));
     //	dmp("D: ".$endDay);
     if ($adults == " " || $adults == "") {
         $adults = 2;
     }
     if (count($offers)) {
         foreach ($offers as $offer) {
             $roomsInfo = array();
             //dmp($room->daily);
             $index = 1;
             $daily = array();
             $nrDays = $initialNrDays;
             if ($nrDays < $offer->offer_min_nights) {
                 $nrDays = $offer->offer_min_nights;
             }
             //dmp($offer->offer_name);
             //dmp("days: ".$nrDays);
             $offerRateDetails = $offer->offerRateDetails;
             $roomRateDetails = $offer->roomRateDetails;
             //dmp($offerRateDetails);
             $firstDayPrice = 0;
             foreach ($offer->daily as &$daily) {
                 $available = true;
                 $totalPrice = 0;
                 $offer_max_nights = $offer->offer_max_nights;
                 $extraNightPrice = 0;
                 if ($index <= $endDay) {
                     $startDate = date('Y-m-d', mktime(0, 0, 0, $month, $index, $year));
                     $endDate = date('Y-m-d', mktime(0, 0, 0, $month, $index + $nrDays, $year));
                     $d = strtotime($startDate);
                     $nr_d = 'offer_day_' . date("N", $d);
                     if ($offer->{$nr_d} == 0) {
                         $available = false;
                     }
                     //check if arrival date is disabled
                     if (count($roomRateDetails)) {
                         foreach ($roomRateDetails as $roomRateDetail) {
                             if ($roomRateDetail->date == $startDate && $roomRateDetail->lock_arrival == 1) {
                                 $available = false;
                             }
                         }
                     }
                     //dmp($startDate);
                     //dmp($endDate);
                     $offer->nrRoomsAvailable = $offer->availability;
                     $offer->bookings = 0;
                     for ($d = strtotime($startDate); $d < strtotime($endDate);) {
                         $dayString = date('Y-m-d', $d);
                         //set default price from rate
                         $weekDay = date("N", $d);
                         $string_price = "price_" . $weekDay;
                         $dayPrice = $offer->{$string_price};
                         $childPrice = $offer->child_price;
                         $extraPersonPrice = $offer->extra_pers_price;
                         //check if there is a custom price set
                         if (count($offerRateDetails)) {
                             foreach ($offerRateDetails as $offerRateDetail) {
                                 if ($offerRateDetail->date == $dayString) {
                                     $dayPrice = $offerRateDetail->price;
                                     $extraPersonPrice = $offerRateDetail->extra_pers_price;
                                     $childPrice = $offerRateDetail->child_price;
                                 }
                             }
                         }
                         $isExtraNight = false;
                         //check if we have an extra night
                         if ($offer_max_nights <= 0) {
                             $dayPrice = $offer->extra_night_price;
                             $isExtraNight = true;
                         }
                         if ($offer->price_type == 1) {
                             $totalAdults = $adults <= $offer->base_adults ? $adults : $offer->base_adults;
                             $dayPrice = $dayPrice * $totalAdults + $childPrice * $children;
                         }
                         //add extra person cost - if it is the case
                         if ($adults > $offer->base_adults) {
                             $dayPrice += ($adults - $offer->base_adults) * $extraPersonPrice;
                         }
                         //for single use
                         //if the price is per person apply single supplement , if is for room apply discount
                         if ($adults == 1 && $children == 0) {
                             if (!$isExtraNight) {
                                 if ($offer->price_type == 1) {
                                     $dayPrice = $dayPrice + $offer->single_balancing;
                                     //dmp("add balancing: ".$offer->single_balancing." -> ".$dayPrice);
                                 } else {
                                     $dayPrice = $dayPrice - $offer->single_balancing;
                                 }
                             } else {
                                 if ($offer->price_type_day == 1) {
                                     if ($offer->price_type == 1) {
                                         $dayPrice = $dayPrice + $offer->single_balancing / $offer->offer_min_nights;
                                     } else {
                                         $dayPrice = $dayPrice - $offer->single_balancing / $offer->offer_min_nights;
                                     }
                                 }
                             }
                         }
                         //check if offer is available on stay period
                         if (!(strtotime($offer->offer_datas) <= $d && $d <= strtotime($offer->offer_datae))) {
                             $available = false;
                         }
                         //get the minimum availability in the selected period
                         if (count($roomRateDetails)) {
                             foreach ($roomRateDetails as $roomRateDetail) {
                                 //get room availability - if rate details are set default settings are ignored
                                 if ($roomRateDetail->date == $dayString) {
                                     $offer->nrRoomsAvailable = $roomRateDetail->availability;
                                 }
                             }
                         }
                         $totalNumberRoomsReserved = 0;
                         if (isset($bookings[$offer->room_id][$dayString])) {
                             $totalNumberRoomsReserved = $bookings[$offer->room_id][$dayString];
                         }
                         if (isset($temporaryReservedRooms[$offer->room_id]) && (strtotime($temporaryReservedRooms["datas"]) <= $d && $d < strtotime($temporaryReservedRooms["datae"]))) {
                             $totalNumberRoomsReserved += $temporaryReservedRooms[$offer->room_id];
                         }
                         //calculate maximum number of bookings per stay interval
                         if ($offer->nrRoomsAvailable <= $totalNumberRoomsReserved) {
                             $available = false;
                         }
                         if ($offer_max_nights > 0) {
                             if (count($offerRateDetails)) {
                                 foreach ($offerRateDetails as $offerRateDetail) {
                                     //set single use price
                                     if ($offerRateDetail->date == $dayString && $adults == 1 && $children == 0) {
                                         $dayPrice = $offerRateDetail->single_use_price;
                                     }
                                 }
                             }
                         }
                         //check if hotel is available
                         if (!$hotelAvailability[$dayString]) {
                             $available = false;
                         }
                         if (strtotime($startDate) == $d) {
                             $firstDayPrice = $dayPrice;
                         }
                         if ($isExtraNight) {
                             $extraNightPrice += $dayPrice;
                         }
                         //dmp("DP: ".$dayPrice);
                         $totalPrice += $dayPrice;
                         $offer_max_nights--;
                         $d = strtotime(date('Y-m-d', $d) . ' + 1 day ');
                     }
                 }
                 //dmp("T: ".$totalPrice);
                 //dmp($offer->nrRoomsAvailable);
                 //dmp($offer->bookings);
                 //average price per offer
                 $offer->offer_average_price = round($totalPrice / $nrDays, 2);
                 $offer->pers_total_price = round($totalPrice / ($adults + $children), 2);
                 $price = $offer->offer_average_price;
                 if (JRequest::getVar('show_price_per_person') == 1) {
                     $price = $offer->pers_total_price;
                 }
                 if ($offer->price_type_day == 1) {
                     $price = $firstDayPrice / ($adults + $children);
                     $price += $extraNightPrice / ($adults + $children);
                 }
                 $roomsInfo[] = array("price" => JHotelUtil::fmt($price, 2), "isAvailable" => $available);
                 $index++;
             }
             //dmp($roomsInfo);
             $id = $offer->offer_id . $offer->room_id;
             $hotelId = $offer->hotel_id;
             $offersCalendar[$id] = JHotelUtil::getAvailabilityCalendar($hotelId, $month, $year, $roomsInfo, $nrDays, $id);
         }
     }
     //dmp($offersCalendar);
     return $offersCalendar;
 }
 static function getExcursionCalendar($excursions, $nrOfDays, $adults, $children, $month, $year, $bookings, $temporaryReservedExcursions, $hotelAvailability)
 {
     $excursionsCalendar = array();
     $endDay = date('t', mktime(0, 0, 0, $month, 1, $year));
     //	dmp("D: ".$endDay);
     foreach ($excursions as $excursion) {
         $excursionsInfo = array();
         $index = 1;
         $excursionRateDetails = $excursion->excursionRateDetails;
         $ignoredDates = explode(",", $excursion->excursionIgnoredDates);
         $nrDays = 0;
         foreach ($excursion->daily as &$daily) {
             $price = $daily["price"];
             $available = true;
             $totalPrice = 0;
             $nrDays = $nrOfDays;
             if ($index <= $endDay) {
                 $startDate = date('Y-m-d', mktime(0, 0, 0, $month, $index, $year));
                 $nrDays = $nrDays < $excursion->min_days ? $excursion->min_days : $nrDays;
                 $endDate = date('Y-m-d', mktime(0, 0, 0, $month, $index + $nrDays, $year));
                 $excursion->nrExcursionsAvailable = $excursion->availability;
                 $excursion->bookings = 0;
                 if (array_search($startDate, $ignoredDates)) {
                     $available = false;
                 }
                 $d = strtotime($startDate);
                 $nr_d = 'excursion_day_' . date("N", $d);
                 if ($excursion->{$nr_d} == 0) {
                     $available = false;
                 }
                 for ($i = $index; $i < $index + $nrDays; $i++) {
                     $day = date('Y-m-d', mktime(0, 0, 0, $month, $i, $year));
                     //check if hotel is available
                     if ($hotelAvailability != null && !$hotelAvailability[$day]) {
                         $available = false;
                     }
                     foreach ($excursionRateDetails as $excursionRateDetail) {
                         if ($excursionRateDetail->date == $day) {
                             $excursion->nrExcursionsAvailable = $excursionRateDetail->availability;
                         }
                     }
                     $totalNumberExcursionsReserved = 0;
                     if (isset($bookings[$excursion->excursion_id][$day])) {
                         $totalNumberExcursionsReserved = $bookings[$excursion->excursion_id][$day];
                     }
                     if (isset($temporaryReservedExcursions[$excursion->excursion_id]) && (strtotime($temporaryReservedExcursions["datas"]) <= strtotime($day) && strtotime($day) < strtotime($temporaryReservedExcursions["datae"]))) {
                         $totalNumberExcursionsReserved += $temporaryReservedExcursions[$excursion->excursion_id];
                     }
                     //calculate maximum number of bookings per stay interval
                     if ($excursion->nrExcursionsAvailable <= $totalNumberExcursionsReserved) {
                         $available = false;
                     }
                     if (isset($excursion->daily[$i - 1])) {
                         $price = $excursion->daily[$i - 1]['price'];
                     }
                     //echo $p.' <br/>';
                     $totalPrice += $price;
                 }
             }
             //average price per excursion
             $excursion->excursion_average_price = round($totalPrice / $nrDays, 2);
             $excursion->pers_total_price = round($totalPrice / $nrDays, 2);
             if (JRequest::getVar('show_price_per_person') == 1) {
                 $price = $excursion->pers_total_price;
             } else {
                 $price = $excursion->excursion_average_price;
             }
             $excursionsInfo[] = array("price" => JHotelUtil::fmt($price, 2), "isAvailable" => $available);
             $id = $excursion->excursion_id + 1000;
             $index++;
         }
         $hotelId = $excursion->hotel_id;
         $excursionsCalendar[$id] = JHotelUtil::getAvailabilityCalendar($hotelId, $month, $year, $excursionsInfo, $nrDays, $id);
     }
     return $excursionsCalendar;
 }