<?php
include("../../include/db.php");
/*********************************Check item for edit********************************************************/
function check_party_edit($branch_name)
{
	$sql_query="select * from branch_creation where branch_name='$branch_name' and delete_status='0'";
$rs=mysql_query($sql_query);
 $rscount1=mysql_num_rows($rs);
	return $rscount1;
 }
 
 /*********************************Check item for edit********************************************************/
function check_places_add($booking_place,$branch_no)
{
  $sql_query="select * from branch_sublist where branch_places='$booking_place' and branch_no='$branch_no'";
  $rs=mysql_query($sql_query);
  $rscount1=mysql_num_rows($rs);
	return $rscount1;
 }
/***************************** Check Item for Edit****************************************/
function check_party_edit123($branch_name,$edit_id)
{
 $sql_query="select * from branch_creation where branch_name='$branch_name'  and delete_status='0'";
 $rs=mysql_query($sql_query);
 $rscount1=mysql_num_rows($rs);
 if($rscount1!=0)
 {
 while($rsdata=mysql_fetch_object($rs))
 {
 $branch_id=$rsdata->branch_id;
 } 
	return $branch_id;
 }
 }
 
 /********************************** ADD  QUERY ***************************************/
if(($_POST['action']=="ADD")) 
{
$branch_no=$_POST['branch_no'];
$branch_name=ucfirst($_POST['branch_name']);
$branch_address=$_POST['branch_address'];
$head_off=$_POST['head_off'];
$branch_off=$_POST['branch_off'];
$branch_ph=$_POST['branch_ph'];
$delivery_ph=$_POST['delivery_ph'];
$delivery_off=$_POST['delivery_off'];

$create_date=date('Y-m-d');
$check_item=check_party_edit($branch_name);
if($check_item==0)
{
$Insql=mysql_query("insert into branch_creation(branch_no,branch_name,branch_address,head_off,branch_off,branch_ph,delivery_ph,delivery_off,created_date)values('$branch_no','$branch_name','$branch_address','$head_off','$branch_off','$branch_ph','$delivery_ph','$delivery_off','$create_date')");
echo "Added Successfully!!";
}
else
{
echo "Already Exist!!";
}
}
/********************************** UPDATE QUERY ***************************************/
if(($_POST['action']=="EDIT"))
{
$edit_id = $_POST['edit_id']; 
$branch_no=$_POST['branch_no'];
$branch_name=ucfirst($_POST['branch_name']);
$branch_address=$_POST['branch_address'];
$head_off=$_POST['head_off'];
$branch_off=$_POST['branch_off'];
$branch_ph=$_POST['branch_ph'];
$delivery_ph=$_POST['delivery_ph'];
$delivery_off=$_POST['delivery_off'];

$create_date=date('Y-m-d');
$check_item_edit=check_party_edit123($branch_name,$edit_id);

if(($check_item_edit==$edit_id)||($check_item_edit==''))
{
$Insql=mysql_query("update branch_creation set branch_name='$branch_name',branch_address='$branch_address',head_off='$head_off',branch_off='$branch_off',branch_ph='$branch_ph',delivery_ph='$delivery_ph',delivery_off='$delivery_off' where  branch_id='$_POST[edit_id]'");
echo "Updated Successfully!!";
}
else
{
	echo "Already Exist";
	}
}

/********************************** ADD  Sublist ***************************************/
if(($_POST['action']=="ADD_place")) 
{
$branch_no=$_POST['branch_no'];
$branch_name=ucfirst($_POST['branch_name']);
$branch_address=$_POST['branch_address'];
$booking_place=$_POST['booking_place'];
$create_date=date('Y-m-d');
$check_places_add=check_places_add($booking_place,$branch_no);
if($check_places_add==0)
{
$Insql=mysql_query("insert into branch_sublist(branch_no,branch_name,branch_address,	branch_places,cr_date)values('$branch_no','$branch_name','$branch_address','$booking_place','$create_date')");

include("branch_sublist.php");
}
else
{
echo "<script>alert('Already Exist')</script>";
include("branch_sublist.php");
}
}
/********************************************DELETE*****************************************************************/
if($_POST['action']=="delete_branch")
{ 		 
		   $delete2="update branch_creation set delete_status='1' where branch_id='$_POST[id]'";
			mysql_query($delete2);
			$delete3="delete from branch_sublist  where branch_no='$_POST[branch_no]'";
			mysql_query($delete3);
			echo "<script>alert('Deleted Succesfully')</script>";
			include("branch_entry_list.php");
}
/********************************************DELETE*****************************************************************/
if($_POST['action']=="delete_branch_sub")
{ 		 
			$delete5="delete from branch_sublist  where id='$_POST[id]' and branch_no='$_POST[branch_no]'";
			mysql_query($delete5);
			echo "<script>alert('Deleted Succesfully')</script>";
			include("branch_sublist.php");
}

?>