<?php
//DB Data
include '../../ASEngine/AS.php';
if(!$login->isLoggedIn())
    header("Location: ../../login.php");
$user = new ASUser(ASSession::get("user_id"));
$userInfo = $user->getInfo();

//basic include files
require_once("../../../db.php");
function newdate($date)
{
    list($m, $d, $a) = explode("/", $date);
    return $a . "-" . $m . "-" . $d;
}
function daysDifference($endDate, $beginDate)
{
    $date_parts1 = explode("/", $beginDate);
    $date_parts2 = explode("/", $endDate);
    $start_date  = gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
    $end_date    = gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
    return $end_date - $start_date;
}
//variables
$room_id = $_POST['room_id'];
$room_name = $_POST['room_name'];
$hotel_id = $_POST['hotel_id'];
$hotel_name = $_POST['hotel_name'];
$price_id = $_POST['priceid'];
$room_person = $_POST['roomperson'];
$tabback = $_POST['tab'];
$avafordate = $_POST['avafordate'];
$avatodate = $_POST['avatodate'];
$seasonal_price = $_POST['seasonalprice'];
//Updating the availability of each room
$fiava = strtotime($avafordate);
$diava = date('d', $fiava);
$miava = date('m', $fiava);
$yiava = date('Y', $fiava);
$foava = strtotime($avatodate);
$doava = date('d', $foava);
$moava = date('m', $foava);
$yoava = date('Y', $foava);
$intday_ava = daysDifference($avatodate, $avafordate);
$fechaavainicial = newdate($avafordate);

for ( $interva = 1 ; $interva <= $intday_ava ; $interva ++)      
    {
     $undia = $interva;
     $fechaavasiguiente = mktime(0,0,0, $miava,$diava,$yiava) + $undia * 24 * 60 * 60;
     $fechaavasalida=date("Y-m-d",$fechaavasiguiente);
	 //verificacion diaria
	 $veriday = mysqli_query($conn, "SELECT * FROM hotel_seasonal_price WHERE seasonal_from = '$fechaavainicial' AND seasonal_to = '$fechaavasalida' AND room_type_id = '$room_id' AND room_person = '$room_person' AND seasonal_price_deleted = '0' ");
     $totalveriday= mysqli_num_rows($veriday);
	 $veridayData = mysqli_fetch_array($veriday);
	 $availability_id  = $veridayData['check_availability_id'];		 
	 
	 if ($totalveriday > 0){
        mysqli_query($conn, "UPDATE hotel_seasonal_price SET seasonal_price = '$seasonal_price' WHERE seasonal_price_id = '$availability_id' ");
     }else {		
	    mysqli_query($conn, "INSERT INTO hotel_seasonal_price (room_type_id,room_person,seasonal_price,seasonal_from,seasonal_to) VALUES('$room_id','$room_person','$seasonal_price','$fechaavainicial','$fechaavasalida')");	 
	 }
	 $fechaavainicial = $fechaavasalida;
  }

mysqli_close($conn);

header("Location: ../../seasonal-price-room.php?roomid=".$room_id."&roomname=".$room_name."&hotelid=".$hotel_id."&hotelname=".$hotel_name."&priceid=".$price_id."&roomperson=".$room_person."&tab=".$tabback."&hotelmsn=addedseasonal");
?>