<?php
include("../../include/db.php");

/********************************** CHECK ITEM FOR STORE CREATION***************************************/

function check_vehicle_no($vehicle_no)
  {   
       $sql_select = "select * from vehicle_entry where vehicle_no='$vehicle_no'";
       $rs=mysql_query($sql_select);
       $rs_count=mysql_num_rows($rs);
       return $rs_count; 
}

function get_vehicle_no_edit($vehicle_no)
{
  $sql_select = "select * from vehicle_entry where vehicle_no='$vehicle_no'";
  $rs=mysql_query($sql_select);
  $rscount1=mysql_num_rows($rs);
    if($rscount1!=0)
    {
      while($rsdata=mysql_fetch_object($rs))
     {
      $id=$rsdata->id;
     } 
	return $id;
    }
   }
 /********************************** ADD  QUERY ***************************************/
if(($_POST['action']=="ADD")) 
{
$vehicle_type=ucfirst($_POST['vehicle_type']);
$vehicle_no=$_POST['vehicle_no'];
$own=$_POST['own'];
$address=$_POST['address'];
$create_date=date('Y-m-d');
$acadamic_yr=explode("-",$create_date);
$acdmc_yr_res=$acadamic_yr[0];
$check_party=check_vehicle_no($vehicle_no);
if(($check_party=='0'))
{
$Insql=mysql_query("insert into vehicle_entry(vehicle_type,vehicle_no,own,address,acad_yr,cr_date)values('$vehicle_type','$vehicle_no','$own','$address','$acdmc_yr_res','$create_date')");
echo "Added Successfully!!";
}
else
{
	echo "Already Exist";
}
}

/********************************** UPDATE QUERY ***************************************/
if(($_POST['action']=="EDIT"))
{
$edit_id = $_POST['edit_id']; 
$vehicle_type=ucfirst($_POST['vehicle_type']);
$vehicle_no=$_POST['vehicle_no'];
$own=$_POST['own'];
$address=$_POST['address'];
$create_date=date('Y-m-d');
$acadamic_yr=explode("-",$create_date);
$acdmc_yr_res=$acadamic_yr[0];
$check_par_edit=get_vehicle_no_edit($vehicle_no);
if(($check_par_edit==$_POST['edit_id'])||($check_par_edit==''))
{
$Insql=mysql_query("update vehicle_entry set vehicle_type='$vehicle_type',vehicle_no='$vehicle_no',own='$own',address='$address',acad_yr='$acdmc_yr_res',cr_date='$create_date' where  id='$_POST[edit_id]'");
echo "Updated Successfully!!";
}
else
{
	echo "Already Exist";
}
}

/********************************************DELETE*****************************************************************/
if($_POST['action']=="delete_vehicle")
{ 		 
		   $delete2="delete from vehicle_entry where id='$_POST[id]'";
			mysql_query($delete2);
			echo "<script>alert('Deleted Succesfully')</script>";
			include("vehicle_entry_list.php");
}


?>