<?php
include("../../include/db.php");
/********************************** CHECK ITEM FOR STORE CREATION***************************************/
function check_store_creat($store_name,$branch_name)
  {   
  $sql_select = "select * from store_creation where (store_name='$store_name' and branch_name='$branch_name')";
       $rs=mysql_query($sql_select);
       $rs_count=mysql_num_rows($rs);
       return $rs_count; 
}

function get_store_creat_edit($store_name,$branch_name)
{
  $sql_select = "select * from store_creation where store_name='$store_name' and branch_name='$branch_name'";
  $rs=mysql_query($sql_select);
  $rscount1=mysql_num_rows($rs);
    if($rscount1!=0)
    {
      while($rsdata=mysql_fetch_object($rs))
     {
      $id=$rsdata->id;
     } 
	return $id;
    }
   }


function get_store_branch_id($branch_id)
{
	$sql_user="select * from  branch_creation where branch_id='$branch_id' and delete_status!='1'";
	$rs_user=mysql_query($sql_user);
	while($rsdata_user=mysql_fetch_object($rs_user))
	{ 
	$branch_name=$rsdata_user->branch_name;
	}
	return $branch_name;
}
 /********************************** ADD  QUERY ***************************************/
$store_name=ucfirst($_POST['store_name']);
$branch_id=$_POST['branch_id'];
$branch_name=get_store_branch_id($branch_id);
$description=$_POST['description'];

if(($_POST['action']=="ADD")) 
{
$create_date=date('Y-m-d');
$acadamic_yr=explode("-",$create_date);
$acdmc_yr_res=$acadamic_yr[0];
$check_party=check_store_creat($store_name,$branch_name);
if(($check_party=='0'))
{
$Insql=mysql_query("insert into store_creation(store_name,branch_name,branch_id,description,acad_yr,cr_date)values('$store_name','$branch_name','$branch_id','$description','$acdmc_yr_res','$create_date')");
echo "Added Successfully!!";
}
else
{
	echo "Already Exist";
}
}
/********************************** UPDATE QUERY ***************************************/
if(($_POST['action']=="EDIT"))
{
$edit_id = $_POST['edit_id']; 
$store_name=ucfirst($_POST['store_name']);
$branch_id=$_POST['branch_id'];
$branch_name=get_store_branch_id($branch_id);
$description=$_POST['description'];

$create_date=date('Y-m-d');
$acadamic_yr=explode("-",$create_date);
$acdmc_yr_res=$acadamic_yr[0];
$check_par_edit=get_store_creat_edit($store_name,$branch_name);
if(($check_par_edit==$_POST['edit_id'])||($check_par_edit==''))
{
$Insql=mysql_query("update store_creation set store_name='$store_name',branch_name='$branch_name',branch_id='$branch_id',description='$description',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_store")
{ 		 
		   $delete2="delete from store_creation where id='$_POST[id]'";
			mysql_query($delete2);
			echo "<script>alert('Deleted Succesfully')</script>";
			include("store_creation_list.php");
}


?>