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

/*********************************Check item for edit********************************************************/
function check_place_add($place_name,$measre_qty)
{
	$sql_query="select * from place_ratefix where place='$place_name' and measurement='$measre_qty' and delete_status!='1'";
    $rs=mysql_query($sql_query);
    $rscount1=mysql_num_rows($rs);
	return $rscount1;
}
 

function check_place_edit123($place_name,$measre_qty,$edit_id)
{
  $sql_query="select * from place_ratefix where place='$place_name' and measurement='$measre_qty' and delete_status!='1'";
  $rs=mysql_query($sql_query);
  $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")) 
{
$place_name=ucfirst($_POST['place_name']);
$measre_qty=$_POST['measre_qty'];
$rate=$_POST['rate'];
$create_date=date('Y-m-d');
$check_item=check_place_add($place_name,$measre_qty);
if($check_item==0)
{
$Insql=mysql_query("insert into place_ratefix(place,measurement,rate,created_date)values('$place_name','$measre_qty','$rate','$create_date')");
echo "Added Successfully!!";
}
else
{
echo "Already Exists!!";
}
}
/********************************** UPDATE QUERY ***************************************/
if(($_POST['action']=="EDIT"))
{
$edit_id = $_POST['edit_id']; 
$place_name=ucfirst($_POST['place_name']);
$measre_qty=$_POST['measre_qty'];
$rate=$_POST['rate'];
$create_date=date('Y-m-d');
$check_item_edit=check_place_edit123($place_name,$measre_qty,$edit_id);

if(($check_item_edit==$_POST['edit_id'])||($check_item_edit==''))
{
$Insql=mysql_query("update place_ratefix set place='$place_name',measurement='$measre_qty',rate='$rate',created_date='$create_date' where  id='$_POST[edit_id]'");
echo "Updated Successfully!!";
}
else
{
	echo "Already Exists";
	}
}
/********************************************DELETE*****************************************************************/
if($_POST['action']=="delete_rate_fix")
{ 		 
		   $delete2="update place_ratefix set delete_status='1' where id='$_POST[id]'";
			mysql_query($delete2);
			echo "<script>alert('Deleted Succesfully')</script>";
			include("place_ratefix_entry_list.php");
}


?>