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


if(isset($_GET['action']))
{
	if($_GET['action']=="DeleteUserAccount")
	{
				      odbc_autocommit($connection, FALSE);
		$Userid=$_GET['UserID'];
		if($Userid!="" && $Userid>0)
		{
		$sql="update UserAccounts set active=0 where Userid=$Userid";
		odbc_exec($connection, $sql);
		$res=array('status'=>"ok",'description'=>'User DeActive Sucessfully');
	
		}
		else
		{
	$res=array('status'=>"error",'description'=>'User not Deleted');
	
		}

	if (!odbc_error())
   {
      odbc_commit($connection);
      $res=array('status'=>"ok",'description'=>'User DeActive Sucessfully');
  }
  else
  {
      odbc_rollback($connection);
	$res=array('status'=>"error",'description'=>'User not Deleted');
  }
//$res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
		echo json_encode($res);

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

		$username=$_SESSION['UserName'];
		//print_r($_POST);
		//print_r($_FILES);
		//print_r($_GET);	
		$documentType=$_POST['documentType'];
		$reference=$_POST['reference'];
		$InvoiceID=$reference;
		$title=$_POST['title'];				
		if(isset($_FILES['file']))
		{
		$ppic=$_FILES['file']['name'];
		}
		else
		$ppic="";	
	if($ppic!="")
		{
			$newfile=rand()*1200;
			$ext=strrchr($ppic,".");
			$newpic=$newfile.$ext;
			save($_FILES['file'],'',"images/");	
			rename("images/".$ppic,"images/".$newpic);
			//$_POST['PictureLocation']=$newpic;
		$pic="PictureLocation='$newpic'";
		//$_SESSION['Picture']=$newpic;
		}
		else
		{
			$pic="";
		}

		$sql="update InvoiceMaster set $pic where InvoiceID='$InvoiceID'";

		odbc_exec($connection,$sql);
			$DocumentName=$title;
      		$Description="";
      		$AttachDate=date("Y-m-d");
      		$AttachmentLocation=$newpic;
      		$SNO=1;
      		$FormFilterID=$reference;
      		$DocumentFilterID=$reference;
      		$KeyField="";;
      		$Active=1;
      		$CreateLog=$_SESSION['UserName'];
		// insert into 
		 $sql="insert into [DocumentAttachments](
      [DocumentName]
      ,[Description]
      ,[AttachDate]
      ,[AttachmentLocation]
      ,[SNO]
      ,[FormFilterID]
      ,[DocumentFilterID]
      ,[KeyField]
      ,[Active]
      ,[CreateLog])
	   values (
'$DocumentName'
      ,'$Description'
      ,'$AttachDate'
      ,'$AttachmentLocation'
      ,'$SNO'
      ,$FormFilterID
      ,$DocumentFilterID
      ,'$KeyField'
      ,$Active
      ,'".$_SESSION['UserName']."'
	)";
	odbc_exec($connection,$sql);
		//$_SESSION['fullname']=$fullname;
		$res=array('status'=>"ok",'description'=>'Record Saved Sucessfully');
		echo json_encode($res);
		//$txtfullname=$_POST['fullname'];
		
	}
	if($_GET['action']=="editprofile")
	{
		$username=$_SESSION['UserName'];
		//print_r($_POST);
		//print_r($_FILES);
		//print_r($_GET);	
		$fullname=$_POST['fullname'];
		$gender=$_POST['gender'];
		$phone=$_POST['phone'];				
		if(isset($_FILES['file']))
		{
		$ppic=$_FILES['file']['name'];
		}
		else
		$ppic="";	
	if($ppic!="")
		{
			$newfile=rand()*1200;
			$ext=strrchr($ppic,".");
			$newpic=$newfile.$ext;
			save($_FILES['file'],'',"images/");	
			rename("images/".$ppic,"images/".$newpic);
			//$_POST['PictureLocation']=$newpic;
		$pic=",picture='$newpic'";
		$_SESSION['Picture']=$newpic;
		}
		else
		{
			$pic="";
		}

		$sql="update UserAccounts set fullname='$fullname',phone='$phone',gender='$gender' $pic where username='$username'";

		odbc_exec($connection,$sql);
		$_SESSION['fullname']=$fullname;
		$res=array('status'=>"ok",'description'=>'Profile Changed Sucessfully');
		echo json_encode($res);
		//$txtfullname=$_POST['fullname'];
	}
	if($_GET['action']=="ChangePassword")
	{
/*		print_r($_GET);
		return;*/
		$password=$_GET['password'];
		$newpassword=$_GET['newpassword'];
		$username=$_SESSION['UserName'];
		//check the old password 
	$sql="select UserName,convert(nvarchar,[Password]) as password,UserID,PassCode from UserAccounts where UserName='$username'";
	$result=odbc_exec($connection,$sql);
	if(odbc_num_rows($result)>0)
	{
		$upassword=odbc_result($result,2);
		$userid=odbc_result($result,3);
		$PassCode=odbc_result($result,4);
	}
	//echo $upassword."-".$password;
	if($password==$upassword)
	{
		
$sql="update useraccounts set password=convert(varbinary,N'$newpassword') where userid='$userid'";
			odbc_exec($connection,$sql);
$res=array('status'=>"ok",'description'=>'Password Changed Sucessfully');
	
	}
	else
	{
$res=array('status'=>"error",'description'=>'Old Password Not Matched or  Some thing Wrong');

	}
	echo json_encode($res);
	}
	
	if($_GET['action']=="ChangePassCode")
	{
		//check the old password
	$PassCode=$_GET['password'];
		$newPassCode=$_GET['newpassword'];
		$username=$_SESSION['UserName'];
		//check the old password 
	$sql="select UserName,convert(nvarchar,[Password]) as password,UserID,PassCode from UserAccounts where UserName='$username'";
	$result=odbc_exec($connection,$sql);
	if(odbc_num_rows($result)>0)
	{
		$upassword=odbc_result($result,2);
		$userid=odbc_result($result,3);
		$uPassCode=odbc_result($result,4);
	}
	//echo $upassword."-".$password;
	if($PassCode==$uPassCode)
	{
			$sql="update useraccounts set passcode='$newPassCode' where userid='$userid'";
			odbc_exec($connection,$sql);
$res=array('status'=>"ok",'description'=>'Password Changed Sucessfully');

	}
		else
	{
$res=array('status'=>"error",'description'=>'Old Password Not Matched or  Some thing Wrong');

	}


	}
	if($_GET['action']=="InsertUserRole")
	{
		      odbc_autocommit($connection, FALSE);
		$data=array();
		//print_r($_POST['data']);
		$data=$_POST['data'];
		$data=json_decode($data);
		//print_r($data);
		$detail=$_POST['detail'];
		$detail=json_decode($detail);

	//	print_r($detail);
	//	return;
 $UserName=$data->UserName;
//$CompanyID=$data->CompanyID;
    $UserID=isset($data->UserID)?$data->UserID:0;
    if($UserID>0 && $UserID!="")
    {
    	//$Pass
     /*$updatesql="update UserAccounts set CompanyID=$CompanyID  where UserID=".$UserID;
     odbc_exec($connection,$updatesql);*/
    }
    else
    {
    $UserID=getMaxId("Roles_Privileges","AccountTypeID",$connection)+1;
    $KeyField=generateCode("Roles_Privileges","*","1=1",$connection);// "";
    $KeyField=str_pad($KeyField,5,"0",STR_PAD_LEFT);
    $KeyField="RUA-".$KeyField;
    //$AccountTypeID=0;
   // $PassCodeValidation=$data->PassCodeValidation;

	}
	
// for user rights
	   $deletesql="delete from Roles_Privileges where AccountTypeID='$UserID'";
	   		odbc_exec($connection,$deletesql);
	  for($a=0;$a<count($detail);$a++)
	  {
	//  if($a==5)break;	
$Execute=(isset($detail[$a]->Execute) && $detail[$a]->Execute!="")?$detail[$a]->Execute:0;
      $Create=(isset($detail[$a]->Create) && $detail[$a]->Create!="")?$detail[$a]->Create:0;
      $Print=(isset($detail[$a]->Print) && $detail[$a]->Print!="")?$detail[$a]->Print:0;
      $Alter=(isset($detail[$a]->Alter) && $detail[$a]->Alter!="")?$detail[$a]->Alter:0;
      $Delete=(isset($detail[$a]->Delete) && $detail[$a]->Delete!="")?$detail[$a]->Delete:0;
$DisplayAll=(isset($detail[$a]->DisplayAll) && $detail[$a]->DisplayAll!="")?$detail[$a]->DisplayAll:0;
$SideBarID=(isset($detail[$a]->SideBarID) && $detail[$a]->SideBarID!="")?$detail[$a]->SideBarID:0;
$Display=(isset($detail[$a]->Display) && $detail[$a]->Display!="")?$detail[$a]->Display:0;
	  $sql="insert into Roles_Privileges( [Execute]
      ,[Create]
      ,[Print]
      ,[Alter]
      ,[Delete]
      ,[DisplayAll]
      ,[SideBarID]
      ,AccountTypeID
      ,TableName
      ,UserID
      ,[Display]
  		) values($Execute
      ,$Create
      ,$Print
      ,$Alter
      ,$Delete
      ,$DisplayAll
      ,$SideBarID
      ,$UserID
      ,'$UserName'
      ,$UserID
      ,$Display)";
      odbc_exec($connection,$sql) or die(odbc_error().$sql);

  		}
  		//get list of user of this role 
			$sqldelete="delete from web_Privileges where AccountTypeID=$UserID";
$result=odbc_exec($connection, $sqldelete) or die(odbc_error().$sqldelete);
  		$sql="select * from UserAccounts where AccountTypeID=$UserID";
		        $result=odbc_exec($connection, $sql) or die(odbc_error().$sql);
		while($row=odbc_fetch_array($result))
		{
			$UID=$row['UserID'];

$sql="insert into web_Privileges( [Execute]
      ,[Create]
      ,[Print]
      ,[Alter]
      ,[Delete]
      ,[DisplayAll]
      ,[SideBarID]
      ,AccountTypeID
      ,UserID
      ,[Display]
  		) select [Execute],[Create]
      ,[Print]
      ,[Alter]
      ,[Delete]
      ,[DisplayAll]
      ,[SideBarID]
      ,AccountTypeID
      ,$UID
      ,[Display] from Roles_Privileges where AccountTypeID=$UserID";
      odbc_exec($connection,$sql) or die(odbc_error().$sql);
		
		}  		
      	// delete all previous rights
      //insert user rights 
      
if (!odbc_error())
   {
      odbc_commit($connection);
      $res=array('status'=>"ok",'description'=>'Record Saved Sucessfully');
  }
  else
  {
      odbc_rollback($connection);
      $res=array('status'=>"Error",'description'=>'Record Saved Sucessfully');

  }
//$res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
		echo json_encode($res);

	
	}
	if($_GET['action']=="InsertUserAccount")
	{
		      odbc_autocommit($connection, FALSE);
		$data=array();
		//print_r($_POST['data']);
		$data=$_POST['data'];
		$data=json_decode($data);
		//print_r($data);
		$detail=$_POST['detail'];
		$detail=json_decode($detail);

	//	print_r($detail);
	//	return;
 $UserName=$data->UserName;
 /*if(checkDuplicate("UserAccounts","UserName",$UserName,$connection)==1)
 {
 	$res=array('status'=>"error",'description'=>'User Name is Already Exists');
		echo json_encode($res);
		return;

 }*/

    $Password=$data->Password;
$AccountTypeID=$data->AccountTypeID;    
    if($Password!="")
    {
    $queryPassword=",Password=convert(varbinary,N'".$Password."')";	
    }
    else
    {
    	    $queryPassword="";
    }	
    $PassCode=$data->PassCode;
    if($PassCode!="")
    {
    $queryPassCode=",PassCode='$PassCode'";	
    }
    else
    {
    $queryPassCode="";	
    }
    $Pass="".$queryPassword.$queryPassCode;
    $CompanyID=($data->CompanyID>0)?$data->CompanyID:0;
    $UserID=$data->UserID;
    if($UserID>0 && $UserID!="")
    {
    	//$Pass
     $updatesql="update UserAccounts set AccountTypeID=$AccountTypeID,CompanyID=$CompanyID $Pass  where UserID=".$UserID;
     odbc_exec($connection,$updatesql);
    }
    else
    {
    $UserID=getMaxId("UserAccounts","UserID",$connection)+1;
    $KeyField=generateCode("UserAccounts","*","1=1",$connection);// "";
    $KeyField=str_pad($KeyField,5,"0",STR_PAD_LEFT);
    $KeyField="UA-".$KeyField;
    //$AccountTypeID=0;
   // $PassCodeValidation=$data->PassCodeValidation;


	 $sql="insert into [UserAccounts]
		 ([UserID]
      ,[UserName]
      ,Password
      ,[AccountTypeID]
      ,[KeyField]
	  ,[CompanyID]
	  ,[PassCode]) 
	  values($UserID
      ,'$UserName'
      ,convert(varbinary,N'".$Password."')
      ,$AccountTypeID
      ,'$KeyField'
	  ,$CompanyID
	  ,'$PassCode')";
		odbc_exec($connection,$sql);
	}
	
// for user rights
	   $deletesql="delete from Web_Privileges where USERID='$UserID'";
	   		odbc_exec($connection,$deletesql);
	  for($a=0;$a<count($detail);$a++)
	  {
	//  if($a==5)break;	
$Execute=(isset($detail[$a]->Execute) && $detail[$a]->Execute!="")?$detail[$a]->Execute:0;
      $Create=(isset($detail[$a]->Create) && $detail[$a]->Create!="")?$detail[$a]->Create:0;
      $Print=(isset($detail[$a]->Print) && $detail[$a]->Print!="")?$detail[$a]->Print:0;
      $Alter=(isset($detail[$a]->Alter) && $detail[$a]->Alter!="")?$detail[$a]->Alter:0;
      $Delete=(isset($detail[$a]->Delete) && $detail[$a]->Delete!="")?$detail[$a]->Delete:0;
$DisplayAll=(isset($detail[$a]->DisplayAll) && $detail[$a]->DisplayAll!="")?$detail[$a]->DisplayAll:0;
$SideBarID=(isset($detail[$a]->SideBarID) && $detail[$a]->SideBarID!="")?$detail[$a]->SideBarID:0;
$Display=(isset($detail[$a]->Display) && $detail[$a]->Display!="")?$detail[$a]->Display:0;
	  $sql="insert into Web_Privileges( [Execute]
      ,[Create]
      ,[Print]
      ,[Alter]
      ,[Delete]
      ,[DisplayAll]
      ,[SideBarID]
      ,UserID
      ,[Display]
      ,AccountTypeID
  		) values($Execute
      ,$Create
      ,$Print
      ,$Alter
      ,$Delete
      ,$DisplayAll
      ,$SideBarID
      ,$UserID
      ,$Display
      ,$AccountTypeID
  		)";
      odbc_exec($connection,$sql) or die(odbc_error().$sql);
  		}
if (!odbc_error())
   {
      odbc_commit($connection);
      $res=array('status'=>"ok",'description'=>'Record Saved Sucessfully');
  }
  else
  {
      odbc_rollback($connection);
      $res=array('status'=>"Error",'description'=>'Record Saved Sucessfully');

  }
echo json_encode($res);
	}
	if($_GET['action']=="getsidebarcategory")
	{
			$data=array();	
		$sql="
SELECT [SideBarID] as SideBarID
      ,[SideBarName] as SideBarName
      ,[SideBarPicture] as SideBarPicture FROM [web_SideBar] where active=1 and under is null";
        $result=odbc_exec($connection, $sql) or die(odbc_error().$sql);
		while($row=odbc_fetch_array($result))
		{


			foreach($row as $key=>$value)
			$row[$key]= utf8_encode($value);	
		$data[]=$row;	
		}
	//	print_r($data);
		echo json_encode($data);

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

		$data=array();
		if(isset($_GET['UserID']) && $_GET['UserID']!="")
		{
			$UserID=" wp.UserID=".$_GET['UserID']." and ";
		}	
		else
		{

		$UserID=" wp.UserID=0 and ";
		}
 		 $sql="select web_SideBar.[SideBarID] as SideBarID
      ,[SideBarName]
      ,[SideBarPicture]
      ,[Group] as Group1
      ,[Under]
	  ,(select top 1 SideBarName from web_SideBar as ws where ws.SideBarID=web_SideBar.[Under]) as Category 
      ,[Degree]
      ,[Hierarchy]
      ,[Active]
      ,web_SideBar.[LastModificationTimeStamp]
      ,web_SideBar.[GUID]
      ,[SortOrder]
      ,[URLLink]
      ,[Separator]
      ,[Flag] 
	  ,[Execute] as Execute1
	  ,[Create] as Create1
      ,[Print] as Print1
      ,[Alter] as Alter1
      ,[Delete] as Delete1
      ,[DisplayAll]
      ,[Display]
	  from web_SideBar 
	   left outer join Roles_Privileges as wp
  on web_SideBar.[SideBarID]=wp.[SideBarID]
  where  $UserID  active=1  
 	  order by under,web_SideBar.SideBarID   
 ";
  $result=odbc_exec($connection, $sql) or die(odbc_error().$sql);
		if(odbc_num_rows($result)>0)
		{
			$sql1="select web_SideBar.[SideBarID] as SideBarID
      ,[SideBarName]
      ,[SideBarPicture]
      ,[Group] as Group1
      ,[Under]
	  ,(select top 1 SideBarName from web_SideBar as ws where ws.SideBarID=web_SideBar.[Under]) as Category 
      ,[Degree]
      ,[Hierarchy]
      ,[Active]
      ,web_SideBar.[LastModificationTimeStamp]
      ,web_SideBar.[GUID]
      ,[SortOrder]
      ,[URLLink]
      ,[Separator]
      ,[Flag] 
	  ,'' as Execute1
	  ,'' as Create1
      ,'' as Print1
      ,'' as Alter1
      ,'' as Delete1
      ,'' as [DisplayAll]
      ,'' as [Display] from web_sidebar where sidebarid not in (select sidebarid from Roles_Privileges as wp where $UserID  1=1)
	  ";
	  $result1=odbc_exec($connection, $sql1) or die(odbc_error().$sql1);
	  				while($row=odbc_fetch_array($result1))
					{
$data[]=$row;
					}
		}
		else
		{
 		$sql="select web_SideBar.[SideBarID] as SideBarID
      ,[SideBarName]
      ,[SideBarPicture]
      ,[Group] as Group1
      ,[Under]
	  ,(select top 1 SideBarName from web_SideBar as ws where ws.SideBarID=web_SideBar.[Under]) as Category 
      ,[Degree]
      ,[Hierarchy]
      ,[Active]
      ,web_SideBar.[LastModificationTimeStamp]
      ,web_SideBar.[GUID]
      ,[SortOrder]
      ,[URLLink]
      ,[Separator]
      ,[Flag] 
	  ,[Execute] as Execute1
	  ,[Create] as Create1
      ,[Print] as Print1
      ,[Alter] as Alter1
      ,wp.[Delete] as Delete1
      ,[DisplayAll]
	  from web_SideBar 
	   left outer join Web_Privileges as wp
  on web_SideBar.[SideBarID]=wp.[SideBarID]
  where  wp.UserID=0 and   active=1 order by under,web_SideBar.SideBarID 
 ";
  $result=odbc_exec($connection, $sql) or die(odbc_error().$sql);			
		}
		while($row=odbc_fetch_array($result))
		{


			foreach($row as $key=>$value)
			$row[$key]= utf8_encode($value);	
		/*$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>';*/
		$data[]=$row;	
		}

	//	print_r($data);
		echo json_encode($data);

	
	}
	if($_GET['action']=="GetPrivileges")
	{
		$data=array();
		if(isset($_GET['UserID']) && $_GET['UserID']!="")
		{
			$UserID=" wp.UserID=".$_GET['UserID']." and ";
		}	
		else
		{

		$UserID=" wp.UserID=0 and ";
		}
 		 $sql="select web_SideBar.[SideBarID]
      ,[SideBarName]
      ,[SideBarPicture]
      ,[Group] as Group1
      ,[Under]
	  ,(select top 1 SideBarName from web_SideBar as ws where ws.SideBarID=web_SideBar.[Under]) as Category 
      ,[Degree]
      ,[Hierarchy]
      ,[Active]
      ,web_SideBar.[LastModificationTimeStamp]
      ,web_SideBar.[GUID]
      ,[SortOrder]
      ,[URLLink]
      ,[Separator]
      ,[Flag] 
	  ,[Execute] as Execute1
	  ,[Create] as Create1
      ,[Print] as Print1
      ,[Alter] as Alter1
      ,[Delete] as Delete1
      ,[DisplayAll]
      ,[Display]
	  from web_SideBar 
	   left outer join Web_Privileges as wp
  on web_SideBar.[SideBarID]=wp.[SideBarID]
  where  $UserID  active=1  
 	  order by under,web_SideBar.SideBarID   
 ";
  $result=odbc_exec($connection, $sql) or die(odbc_error().$sql);
		if(odbc_num_rows($result)>0)
		{
			$sql1="select web_SideBar.[SideBarID]
      ,[SideBarName]
      ,[SideBarPicture]
      ,[Group] as Group1
      ,[Under]
	  ,(select top 1 SideBarName from web_SideBar as ws where ws.SideBarID=web_SideBar.[Under]) as Category 
      ,[Degree]
      ,[Hierarchy]
      ,[Active]
      ,web_SideBar.[LastModificationTimeStamp]
      ,web_SideBar.[GUID]
      ,[SortOrder]
      ,[URLLink]
      ,[Separator]
      ,[Flag] 
	  ,'' as Execute1
	  ,'' as Create1
      ,'' as Print1
      ,'' as Alter1
      ,'' as Delete1
      ,'' as [DisplayAll]
      ,'' as [Display] from web_sidebar where sidebarid not in (select sidebarid from Web_Privileges as wp where $UserID  1=1)
	  ";
	  $result1=odbc_exec($connection, $sql1) or die(odbc_error().$sql1);
	  				while($row=odbc_fetch_array($result1))
					{
$data[]=$row;
					}
		}
		else
		{
 		$sql="select web_SideBar.[SideBarID]
      ,[SideBarName]
      ,[SideBarPicture]
      ,[Group] as Group1
      ,[Under]
	  ,(select top 1 SideBarName from web_SideBar as ws where ws.SideBarID=web_SideBar.[Under]) as Category 
      ,[Degree]
      ,[Hierarchy]
      ,[Active]
      ,web_SideBar.[LastModificationTimeStamp]
      ,web_SideBar.[GUID]
      ,[SortOrder]
      ,[URLLink]
      ,[Separator]
      ,[Flag] 
	  ,[Execute]
	  ,[Create]
      ,[Print]
      ,[Alter]
      ,wp.[Delete] as Delete1
      ,[DisplayAll]
	  from web_SideBar 
	   left outer join Web_Privileges as wp
  on web_SideBar.[SideBarID]=wp.[SideBarID]
  where  wp.UserID=0 and   active=1 order by under,web_SideBar.SideBarID 
 ";
  $result=odbc_exec($connection, $sql) or die(odbc_error().$sql);			
		}
		while($row=odbc_fetch_array($result))
		{


			foreach($row as $key=>$value)
			$row[$key]= utf8_encode($value);	
		/*$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>';*/
		$data[]=$row;	
		}

	//	print_r($data);
		echo json_encode($data);

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

		$data=array();
		if(isset($_GET['CompanyID']) && trim($_GET['CompanyID'])!="")
		{
			$CompanyID="CompanyID=".$_GET['CompanyID']." and ";
		}
		else
		{
			$CompanyID="";
		}
		if(isset($_GET['UserName']) && $_GET['UserName']!="")
		{
			$UserName="UserName like '".$_GET['UserName']."%' and ";
		}	
		else
		{

		$UserName="";
		}
				if(isset($_GET['UserID']) && $_GET['UserID']!="")
		{
			$UserID="UserID=".$_GET['UserID']." and ";
		}	
		else
		{

		$UserID="";
		}
		$searchbox="".$CompanyID."".$UserName."".$UserID;
		$sql="  select Distinct TableName as UserName,AccountTypeID  as row_num,
  '' as PassCode,''  as  CompanyShortForm,'' as KeyField,'' as col_Action,AccountTypeID as UserID,
  '' as LastModificationTimeStamp,'' as CompanyID,'' as Active from Roles_Privileges as u    where $searchbox 1=1";
	$result=odbc_exec($connection, $sql) or die(odbc_error().$sql);
		while($row=odbc_fetch_array($result))
		{

//<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)
			$row[$key]= utf8_encode($value);	
		$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;';
		$data[]=$row;	
		}
	//	print_r($data);
		echo json_encode($data);

	
	}
	if($_GET['action']=="GetUserAccount")
	{
		$data=array();
		if(isset($_GET['CompanyID']) && trim($_GET['CompanyID'])!="")
		{
			$CompanyID="CompanyID=".$_GET['CompanyID']." and ";
		}
		else
		{
			$CompanyID="";
		}
		if(isset($_GET['UserName']) && $_GET['UserName']!="")
		{
			$UserName="UserName like '".$_GET['UserName']."%' and ";
		}	
		else
		{

		$UserName="";
		}
				if(isset($_GET['UserID']) && $_GET['UserID']!="")
		{
			$UserID="UserID=".$_GET['UserID']." and ";
		}	
		else
		{

		$UserID="";
		}
		$searchbox="".$CompanyID."".$UserName."".$UserID;
		$sql="select ROW_NUMBER() OVER (ORDER BY [USERID])  as row_num,UserName,PassCode,g.Name as  CompanyShortForm,u.KeyField as KeyField,'' as col_Action,UserID,u.LastModificationTimeStamp as LastModificationTimeStamp,CompanyID,u.Active as Active,u.AccountTypeID as AccountTypeID  from UserAccounts as u inner join godowns as g  on u.CompanyID=g.GodownID where $searchbox 1=1  ";
	$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{


			foreach($row as $key=>$value)
			$row[$key]= utf8_encode($value);	
		$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>';
		$data[]=$row;	
		}
	//	print_r($data);
		echo json_encode($data);

	}
}
?>