<?
//Optid,Sector,VesselId,ShipGearId,FishingZone,District,LandingPort,DepartureDate, ArrivalDate,FishingTime,BottomDepth,FishingGearDepth,HualPerTrip,TotalCatch,Recorder
function Insertopt($optid,$vessel,$port,$ddate,$adate,$fishingtime,$bdepth,$gdepth,$hual,$fdulation,$recorder,$fuel,$ice,$food,$repair,$maintemance,$miscel,$zone)
{
	include "connect.inc.php";
	$sqlchk = "select * from  fishoperation where VesselId = '$vessel' and  DepartureDate='$ddate'";
	//echo $sqlchk;
	$qrychk = mysql_query($sqlchk);
	$numchk = mysql_num_rows($qrychk);
	if($numchk < 1)
	{
	$duration = DateDiff($ddate,$adate)+1;
	$sqlinsert = "insert into fishoperation(Optid,VesselId,LandingPort,DepartureDate, ArrivalDate,Duration,FishingTime,BottomDepth,FishingGearDepth,HualPerTrip,fishingdulation,Recorder,fuel,ice,food,repair,maintenance,miscellaneous_expence,zoneid)";
	$sqlinsert  =$sqlinsert." values($optid,'$vessel',$port,'$ddate','$adate',$duration,'$fishingtime',$bdepth,$gdepth,$hual,$fdulation,'$recorder',$fuel,$ice,$food,$repair,$maintemance,$miscel,'$zone')";
	//echo $sqlinsert;
	mysql_query($sqlinsert);
	}
	else
	{
	require_once("usr_function.php");
	ErrMsg("catch data is duplicate!");
	//$sec = "1";
	//header("Refresh: $sec; url='catchdata.php'");
	}
}

function InsertCatch($optid,$sortno,$spid,$weight,$tray,$price)
{
	include "connect.inc.php";
	require_once("sp_function.inc.php");
	$sqlchk = "select * from catch where Optid=$optid and SortNo=$sortno and Spid=$spid";
	$qrychk = mysql_query($sqlchk);
	$numchk = mysql_num_rows($qrychk);
	if($numchk == 0)
	{
	$sqlInsertCatch = "insert into catch(Optid,SortNo,Spid,Weight,Tray,Price) values($optid,$sortno,$spid,$weight,$tray,$price)";
	//echo $sqlInsertCatch;
	$qryInsertCatch = mysql_query($sqlInsertCatch);
	}
	else
	{
		if($weight != 0)
		{
		$updatecatch = "update catch set weight=$weight where Optid=$optid and SortNo=$sortno and Spid=$spid";
		//echo $updatecatch;
		mysql_query($updatecatch);
		}
		else
		{
		$deletecatch = "delete from catch where Optid=$optid and SortNo=$sortno and Spid=$spid";
		//echo $deletecatch;
		mysql_query($deletecatch);
		}
	}
}

function DeleteCatch($optid,$sortno,$spid)
{
		include "connect.inc.php";
		$deletecatch = "delete from catch where Optid=$optid and SortNo=$sortno and Spid=$spid";
		//echo $deletecatch;
		mysql_query($deletecatch);
}
function EditCatch($optid,$sortno,$spid,$weight,$tray,$price)
{
	include "connect.inc.php";
	require_once("sp_function.inc.php");
	$sqlchk = "select * from catch where Optid=$optid and SortNo=$sortno and Spid=$spid";
	//echo $sqlchk;
	$qrychk = mysql_query($sqlchk);
	$numchk = mysql_num_rows($qrychk);
	if($numchk == 0)
	{
	$sqlInsertCatch = "insert into catch(Optid,SortNo,Spid,Weight,Tray,Price) values($optid,$sortno,$spid,$weight,$tray,$price)";
	//echo $sqlInsertCatch;
	$qryInsertCatch = mysql_query($sqlInsertCatch);
	}
	else
	{
		if($weight != 0)
		{
		$updatecatch = "update catch set weight=$weight,Tray=$tray,Price=$price where Optid=$optid and SortNo=$sortno and Spid=$spid";
		//echo $updatecatch;
		mysql_query($updatecatch) or die(mysql_error());
		
		}
		else
		{
		$deletecatch = "delete from catch where Optid=$optid and SortNo=$sortno and Spid=$spid";
		//echo $deletecatch;
		mysql_query($deletecatch);
		}
	}
}

function EditOpt($optid,$vessel,$port,$ddate,$adate,$fishingtime,$bdepth,$gdepth,$hual,$fdulation,$recorder,$fuel,$ice,$food,$repair,$maintemance,$miscel,$zone)
{
	include "connect.inc.php";
	$sqlchk = "select * from  fishoperation where VesselId = '$vessel' and  DepartureDate='$ddate'  and Optid != $optid";
	//echo $sqlchk;
	$qrychk = mysql_query($sqlchk);
	$numchk = mysql_num_rows($qrychk);
	if($numchk < 1)
	{
	$duration = DateDiff($ddate,$adate)+1;
	$sqledit = "update  fishoperation set  VesselId='$vessel',LandingPort=$port,";
	$sqledit = $sqledit."DepartureDate='$ddate',ArrivalDate='$adate',Duration=$duration,FishingTime='$fishingtime',";
	$sqledit = $sqledit."BottomDepth=$bdepth,FishingGearDepth=$gdepth,HualPerTrip=$hual,fishingdulation=$fdulation,Recorder='$recorder',fuel=$fuel,ice=$ice,food=$food,repair=$repair,maintenance=$maintemance,miscellaneous_expence=$miscel,zoneid='$zone' where Optid=$optid";
	//echo $sqledit;
	mysql_query($sqledit) or die(mysql_error());
		$sqlgetbycatch = "select sum(weight) as sw from catch where SortNo=1 and Optid = $optid group by Optid";
	 // echo  $sqlgetbycatch;
	  $qrygetbycatch = mysql_query($sqlgetbycatch);
	  $resgetbycatch = mysql_fetch_array($qrygetbycatch);
	  $totalprimary = $resgetbycatch["sw"];
	  if($totalprimary != 0)
	  {
	  $totalbycatch = $dbtotalcatch-$totalprimary;
	  updatetotalcatch($optid, $totalprimary,$totalbycatch);
	  }
	  $sqlgetsambycatch = "select sum(weight) as swb from catch where SortNo=2 and Optid = $optid group by Optid";
	 //echo $sqlgetsambycatch;
	 // echo  $sqlgetbyca
	   $qrygetsambycatch = mysql_query($sqlgetsambycatch);
	  $resgetsambycatch = mysql_fetch_array($qrygetsambycatch);
	  $totalsecondary = $resgetsambycatch["swb"];
	  if($totalsecondary != 0)
	  {
	  $totalbycatch =  $dbtotalcatch-$totalprimary;
	  $rais = $totalbycatch/$totalsecondary;
	  updateraisingfact($optid,$totalsecondary,$rais);
	  }
	}
	else
	{
	require_once("usr_function.php");
	ErrMsg("catch data is duplicate!");
	$sec = "1";
	header("Refresh: $sec; url='catchdata.php'");
	}
}
function DeleteOpt($optid)
{
	include "connect.inc.php";
	$sqldelete = "delete from fishoperation where Optid=$optid";
	//echo $sqldelete;
	mysql_query($sqldelete) or die(mysql_error());
	$sqldeletecatch = "delete from catch where Optid=$optid";
	//echo $sqldeletecatch;
	mysql_query($sqldeletecatch) or die(mysql_error());
	$sqldeletebiology = "delete from biology where Optid=$optid";
	mysql_query($sqldeletebiology) or die(mysql_error());
}
function getNumYearSector($dyear,$type)
{
	include "connect.inc.php";
	$sqlgetyear = "select count(Optid) as nyear from fishoperation  where  year(DepartureDate) = '$dyear' and Sector=$type";
	 //echo $sqlgetyear;
	  $qrygetyear = mysql_query($sqlgetyear);
	  $numgetyear = mysql_num_rows($qrygetyear);
	  $resgetyear = mysql_fetch_array($qrygetyear);
	  $ysn = $resgetyear["nyear"];
	  return $ysn;
}
function getNumYear($dyear)
{
	include "connect.inc.php";
	$sqlgetyear = "select count(Optid) as nyear from fishoperation  where  year(DepartureDate) = '$dyear'";
	 //echo $sqlgetyear;
	  $qrygetyear = mysql_query($sqlgetyear);
	  $numgetyear = mysql_num_rows($qrygetyear);
	  $resgetyear = mysql_fetch_array($qrygetyear);
	  $yn = $resgetyear["nyear"];
	  return $yn;
}
function updatetotalcatch($optid,$ttprimary,$ttbycatch)
{
	include "connect.inc.php";
	$sqlupdate = "update fishoperation set catchprimary=$ttprimary,totalbycatch=$ttbycatch where Optid=$optid";
	mysql_query($sqlupdate);
}
function updateraisingfact($optid,$ttsecond,$rfact)
{
	include "connect.inc.php";
	$sqlupdatefact = "update fishoperation set catchsecond=$ttsecond,raisingfact=$rfact where Optid=$optid";
	//echo $sqlupdatefact;
	mysql_query($sqlupdatefact);
}
function getprimarytotalspid($optid,$spid)
{
	 include "connect.inc.php";
	 $sqlgetprimarytotalspid = "select sum(weight) as sw from catch where SortNo=1 and Optid = $optid and spid =$spid";
	 //echo $sqlgetyear;
	  $qrygetprimarytotalspid = mysql_query($sqlgetprimarytotalspid);
	  $numgetprimarytotalspid = mysql_num_rows($qrygetprimarytotalspid);
	  $resgetprimarytotalspid = mysql_fetch_array($qrygetprimarytotalspid);
	  $dbprimarytotalspid = $resgetprimarytotalspid["sw"];
	  if($dbprimarytotalspid != 0)
	  {
	  return  $dbprimarytotalspid;
	  }
	  else
	  {
	  	return 0;
	  }
}

//Source:http://www.thaicreate.com/php/php-date-diff.html     
function DateDiff($strDate1,$strDate2)    
{              
return (strtotime($strDate2) - strtotime($strDate1))/(60*60*24);  // 1 day = 60*60*24     
}     
function TimeDiff($strTime1,$strTime2)    
{                
return (strtotime($strTime2) - strtotime($strTime1))/(60*60); // 1 Hour =  60*60     
}      
function DateTimeDiff($strDateTime1,$strDateTime2)      
{                 
return (strtotime($strDateTime2) - strtotime($strDateTime1))/(60*60); // 1 Hour =  60*60     
} 
?>