<?php
include_once('database_connection.php');

if(isset($_GET['action']))
{
	if($_GET['action']=='GetLocation')
	{

				$data=array();
		$GodownID=isset($_GET['GodownID'])?$_GET['GodownID']:'';
		$UnderName=isset($_GET['UnderName'])?$_GET['UnderName']:'';
		$Department=isset($_GET['Department'])?$_GET['Department']:'';
		$Company=isset($_GET['Company'])?$_GET['Company']:'';			
		$Keyfield=isset($_GET['KeyField'])?$_GET['KeyField']:'';

		if($Department!="")
		{
			$Department="a.Name like '%".$Department."%' and ";
		}
		else
		{
			$Department="";
		}
		if($Company!="")
		{
			$Company="a.Name like '%".$Company."%' and ";
		}
		else
		{
			$Company="";
		}
		if($UnderName!="")
		{
						$UnderName="b.Name like '%".$UnderName."%' and ";

		}
		else
		{
			$UnderName="";
		}
		if($Keyfield!="")
		{
						$Keyfield="a.Keyfield like '%".$Keyfield."%' and ";

		}
		else
		{
			$Keyfield="";
		}
		if($GodownID!="")
		{
						$GodownID="a.GodownID=".$GodownID." and ";

		}
		else
		{ 
			$GodownID="";
		}
		$searchbox=$Department."".$UnderName."".$Keyfield."".$GodownID."".$Company;

		$sql="Select  ROW_NUMBER() OVER (
      ORDER BY a.degree
   )  as row_num,a.Name as Name,a.EmailAddress1 as EmailAddress1,a.EmailAddress2 as EmailAddress2,a.Address as Address,a.Address2 as Address2,a.ContactNo as ContactNo,a.TRNNo as TRNNo,a.Website as Website,a.FAX as FAX,b.Name as UnderName,a.Keyfield as Keyfield,'' as col_Action,a.GodownID as GodownID,a.AdditionalInfo1 as AdditionalInfo1,a.AdditionalInfo2 as AdditionalInfo2,a.AdditionalInfo3 as AdditionalInfo3,a.AdditionalInfo4 as AdditionalInfo4,a.AdditionalInfo5 as AdditionalInfo5,a.LastModificationTimeStamp as LastModificationTimeStamp
   ,a.Degree as degree1 from godowns as a left outer join godowns as b 
   on a.Under=b.GodownID where ".$searchbox." a.degree=1 and a.Active=1";
	$result=odbc_exec($connection, $sql) or die(odbc_error().$sql);
	 while ($row = odbc_fetch_array($result)) {
	$row['Name']=htmlspecialchars($row['Name']); 	
  	$row['col_Action']='<i class="fa fa-pencil fa-lg" style="text-align: left;color: blue;cursor:pointer;" onclick="javascript:editRow(this);"></i> &nbsp;&nbsp;&nbsp;<i class="fa fa-trash-o fa-lg" style="text-align: right;color: red;cursor:pointer;" onclick="javascript:deleteRow(this);"></i>';
    $data []=$row;
  		}//end while
  		//print_r($data);
  		echo json_encode($data); 


	
	}

	if($_GET['action']=="GetDepartment")
	{
				$data=array();
		$GodownID=isset($_GET['GodownID'])?$_GET['GodownID']:'';
		$UnderName=isset($_GET['UnderName'])?$_GET['UnderName']:'';
		$Department=isset($_GET['Department'])?$_GET['Department']:'';
		$Keyfield=isset($_GET['Keyfield'])?$_GET['Keyfield']:'';
		if($Department!="")
		{
			$Department="a.Name like '%".$Department."%' and ";
		}
		else
		{
			$Department="";
		}
		if($UnderName!="")
		{
						$UnderName="b.Name like '%".$UnderName."%' and ";

		}
		else
		{
			$UnderName="";
		}
		if($Keyfield!="")
		{
						$Keyfield="a.Keyfield like '%".$Keyfield."%' and ";

		}
		else
		{
			$Keyfield="";
		}
		if($GodownID!="")
		{
						$GodownID="a.GodownID=".$GodownID." and ";

		}
		else
		{
			$GodownID="";
		}
		$searchbox=$Department."".$UnderName."".$Keyfield."".$GodownID;

		$sql="Select  ROW_NUMBER() OVER (
      ORDER BY a.degree
   )  as row_num,a.Name as Name,b.Name as UnderName,a.Keyfield as Keyfield,'' as col_Action,a.GodownID as GodownID,a.LastModificationTimeStamp as LastModificationTimeStamp
   ,a.Degree as degree1 from godowns as a left outer join godowns as b 
   on a.Under=b.GodownID where ".$searchbox." a.degree=2 and a.Active=1";
	$result=odbc_exec($connection, $sql) or die(odbc_error().$sql);
	 while ($row = odbc_fetch_array($result)) {
	$row['Name']=htmlspecialchars($row['Name']); 	
  	$row['col_Action']='<i class="fa fa-pencil fa-lg" style="text-align: left;color: blue;cursor:pointer;" onclick="javascript:editMaster(this);"></i> &nbsp;&nbsp;&nbsp;<i class="fa fa-trash-o fa-lg" style="text-align: right;color: red;cursor:pointer;" onclick="javascript:deleteMaster(this);"></i>';
    	foreach ($row as $key => $value) {
    		# code...
    		$row[$key]=utf8_encode($value);
    	}
    $data []=$row;
  		}//end while
  		//print_r($data);
  		echo json_encode($data); 


	}
	if($_GET['action']=="InsertDepartment")
	{
	//	print_r($_POST);
		$data=array_keys($_POST);
		$data=json_decode($data[0]);
		$GodownID=isset($data->GodownID)?$data->GodownID:0;
		$Name=isset($data->Name)?$data->Name:'';
		$Under=isset($data->Under)?$data->Under:0;

		if($GodownID>0 && $GodownID!="")
		{
		$updatesql="update godowns set Name='".$Name."',Under=".$Under." where GodownID=".$GodownID;	
		}

		else  {
				# code...
			$where="";
			$where=" Degree=2 and active=1 ";
			$KeyField=generateCode("godowns","KeyField",$where,$connection)+1;
           	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="DEPT-".$KeyField2;
           	

			$ID=getMaxId("godowns","GodownID",$connection)+1;
			$updatesql="insert into godowns (GodownID,Name,Under,Keyfield,Active,Degree) values(".$ID.",'".$Name."',".$Under.",'".$KeyField."',1,2)";
			}	
			odbc_exec($connection,$updatesql);
			$res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
		echo json_encode($res);

	}
	if($_GET['action']=="InsertGodowns")
	{

			//$_POST=utf8_decode($_POST);
				$data=array_keys($_POST);
			//	$data=utf8_decode($data[0]);
		//
				//print_r($data);
		foreach ($data as $key => $value) {
			# code...
			$data[$key]=urldecode($value);
		}
		$data=json_decode($data[0]);

		$GodownID=isset($data->GodownID)?$data->GodownID:0;
		 $Name=isset($data->Name)?urldecode($data->Name):'';
		 $Name=str_replace("_"," ",$Name);
		//return;
		$Under=isset($data->Under)?$data->Under:0;
		$AdditionalInfo1=isset($data->AdditionalInfo1)?str_replace("_"," ",$data->AdditionalInfo1):'';
		$AdditionalInfo2=isset($data->AdditionalInfo2)?str_replace("_"," ",$data->AdditionalInfo2):'';
		$AdditionalInfo3=isset($data->AdditionalInfo3)?str_replace("_"," ",$data->AdditionalInfo3):'';
		$AdditionalInfo4=isset($data->AdditionalInfo4)?str_replace("_"," ",$data->AdditionalInfo4):'';
		$AdditionalInfo5=isset($data->AdditionalInfo5)?str_replace("_"," ",$data->AdditionalInfo5):'';
		$Address=isset($data->Address)?str_replace("_"," ",$data->Address):'';
		$ContactNo=isset($data->ContactNo)?str_replace("_"," ",$data->ContactNo):'';
		$EmailAddress1=isset($data->EmailAddress1)?str_replace("_",".",$data->EmailAddress1):'';
		$EmailAddress2=isset($data->EmailAddress2)?str_replace("_",".",$data->EmailAddress2):'';
		$FAX=isset($data->FAX)?str_replace("_",".",$data->FAX):'';
		$TRNNo=isset($data->TRNNo)?$data->TRNNo:'';
		$Website=isset($data->Website)?str_replace("_",".",$data->Website):'';
		$Address2=isset($data->Address2)?str_replace("_"," ",$data->Address2):'';
		
		if($GodownID>0 && $GodownID!="")
		{
		$sql="update godowns set Name='".$Name."',Under=".$Under.",AdditionalInfo1='$AdditionalInfo1',AdditionalInfo2='$AdditionalInfo2',AdditionalInfo3='$AdditionalInfo3',AdditionalInfo4='$AdditionalInfo4',AdditionalInfo5='$AdditionalInfo5',Address='$Address',Address2='$Address2',ContactNo='$ContactNo',EmailAddress1='$EmailAddress1',EmailAddress2='$EmailAddress2',FAX='$FAX',TRNNo='$TRNNo',Website='$Website' where GodownID=".$GodownID;	
		}
		else
		{
			$where=" Degree=1 and active=1 ";
		$KeyField=generateCode("godowns","KeyField",$where,$connection)+1;
        $KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
        $KeyField="G-".$KeyField2;
        $ID=getMaxId("godowns","GodownID",$connection)+1;
  		  $sql="insert into godowns (GodownID,Name,Under,Keyfield,Active,Degree,AdditionalInfo1,AdditionalInfo2,AdditionalInfo3,AdditionalInfo4,AdditionalInfo5,Address,Address2,ContactNo,EmailAddress1,EmailAddress2,FAX,TRNNo,Website,GodownType,[group]) values(".$ID.",'".$Name."',".$Under.",'".$KeyField."',1,1,'$AdditionalInfo1','$AdditionalInfo2','$AdditionalInfo3','$AdditionalInfo4','$AdditionalInfo5','$Address','$Address2','$ContactNo','$EmailAddress1','$EmailAddress2','$FAX','$TRNNo','$Website',1,0)";
  		}
  		  odbc_exec($connection,$sql);
  		    		  $res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
		echo json_encode($res);

	}

	if($_GET['action']=="DeleteDepartment")
	{
		$GodownID=$_GET['GodownID'];
		$sql="update godowns set Active=0 where GodownID=".$GodownID;
		odbc_exec($connection,$sql);
$res=array('status'=>"ok",'description'=>'Record Deleted Sucessfully');
		echo json_encode($res);
	}
	if($_GET['action']=="DeleteLocation")
	{
		$GodownID=$_GET['GodownID'];
		$sql="update godowns set Active=0 where GodownID=".$GodownID;
		odbc_exec($connection,$sql);
$res=array('status'=>"ok",'description'=>'Record Deleted Sucessfully');
		echo json_encode($res);
	}
}


?>