<?php
	include("../session.php");
	include("../connect.php");
	
	//---------------------id increment-----------------------//

	$str = "SELECT Max(id) FROM licenseReport";
	
		$result2 = mysql_query ($str,$login);
		list($result2)=mysql_fetch_array($result2);
		$number = ++$result2;
			
		if($number == "1")
		{
			$number = "L0000001";
		}
	
	date_default_timezone_set ("Singapore"); 
		$ldate=date("d-m-Y", time());
		$ltime=date("h:i:sa", time());

//--------------sub date to be day, month, year-----------//
$day=substr($ldate,0,2);
$month=substr($ldate,3,2);
$years=substr($ldate,6,4);
		
$user=$_SESSION['admin'];
$id=$_POST["id"];
$expire=$_POST["expire"];
$num=$_POST["year"];
$pType=$_POST["pType"];

//-----------------Take the rate out---------------------------//
$pQuery=mysql_query("select * from price order by id desc");
	$displayP=mysql_fetch_array($pQuery);
	$price=$displayP['price'];
	$agent=$display['agentID'];

	$payment = $num * $price;

	$query=mysql_query("select * from company where id='$id'");
	$display=mysql_fetch_array($query);
	$old=$display['expireDate'];
	
//----------------------------------Validate the update year---------------------------------------//
list($day, $month, $year) = split('[-]', $expire);//expired date

date_default_timezone_set ("Singapore");
$todayDate = date("d-m-Y", time());
list($todays, $tomonth, $toyear) = split('[-]', $todayDate);//today date 

if($toyear > $year ){
	// if expired already
	$truly=updateYear($todayDate,$num);
	
}else if ($toyear < $year){
	//if haven expired
	$truly=updateYear($expire,$num);
	
}else if ($toyear == $year){
	//In this year
	
	if($tomonth > $month){
		//if expired already
		$truly=updateYear($todayDate,$num);
		
	}else if ($tomonth < $month){
		//if haven expire
		$truly=updateYear($expire,$num);
		
	}else if ($tomonth == $month){
		//In this month
		
		if($todays > $day){
			//if expired already
			$truly=updateYear($todayDate,$num);
			
		}else if($todays < $day){
			//if haven expired
			$truly=updateYear($expire,$num);
			
		}else if ($todays == $day){
			//today last day
			$truly=updateYear($expire,$num);
			
		}//end day compare
	}//end month compare
}//end year compare

//--------------------------------------End validate----------------------------------------//	
	

mysql_query("UPDATE company SET expireDate = '$truly'
WHERE id = '$id'");

mysql_query("INSERT INTO licenseReport (id,companyID,day,month,year,updateDate,updateTime,oldExpire,newExpire,updateYear,payment,paymentType,agent)
	VALUES('$number','$id','$day','$month','$years','$ldate','$ltime','$old','$truly','$num','$payment','$pType','$agent')"); 

echo "<script language=javascript>alert('Update successful!!');
		window.location='successUpdate.php?id=$id&number=$number'</script>";

//----------------------------------Function the update year---------------------------------------//
function updateYear($renew,$numYear){

	if($numYear=="1"){
		$date = strtotime(date("d-m-Y", strtotime($renew)) . " +365 days");
		$true=date("d-m-Y", $date);
		return $true;
		
	}// update one year
	if($numYear=="2"){
		$date = strtotime(date("d-m-Y", strtotime($renew)) . " +730 days");
		$true=date("d-m-Y", $date);
		return $true;
		
	}// update two year
	if($numYear=="3"){
		$date = strtotime(date("d-m-Y", strtotime($renew)) . " +1095 days");
		$true=date("d-m-Y", $date);
		return $true;
		
	}//update three year
}//end function

?>