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

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

	if($_GET['action']=="GetAllowance")
	{
		$data=array();
			//	$DesignationName=isset($_GET['DesignationName'])?$_GET['DesignationName']:'';
		$Employee=isset($_GET['Employee'])?$_GET['Employee']:'';
		$Allowance=isset($_GET['Allowance'])?$_GET['Allowance']:'';
		$AllowanceType=isset($_GET['AllowanceType'])?$_GET['AllowanceType']:'';
		if($Employee!="")
		{
						$Employee="SalesManName like '%".$Employee."%' and ";

		}
		else
		{
			$Employee="";
		}
		if($Allowance!="")
		{
			$Allowance="MasterValue like '%".$Allowance."%' and ";
		}
		else
		{
			$Allowance="";
		}
		if($AllowanceType!="")
		{
			$AllowanceType="AllowanceType like '%".$AllowanceType."%' and ";
		}
		else
		{
			$AllowanceType="";
		}
		$searchbox=$Employee."".$Allowance."".$AllowanceType;

		// $sql="Select ROW_NUMBER() OVER (
      	// PARTITION BY [Keyfield]
      	// ORDER BY [Keyfield]
		//    ) row_num,DesignationName,DesignationID,LastModificationTimeStamp,Description,Keyfield,'' as col_Action 
		//    from pr_DesignationMaster where ".$searchbox." active=1";

		$sql="SELECT EmployeeAllowanceID as row_num, EmployeeAllowanceID,ea.SalesManID , SalesManName as Employee, 
					 ea.MasterValueID , MasterValue as Allowance, AllowanceType, AllowanceValue,ea.Description ,'' as col_Action
		FROM EmployeeAllowance as ea 
		inner join SalesManInformation as smi on ea.SalesManID=smi.SalesManID
		inner join master_values as mv on ea.MasterValueID=mv.MasterValueID
		where ".$searchbox." ea.active=1";   
		   
		$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{
			$row['Employee']=htmlspecialchars($row['Employee']);
			$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;	
		}
		echo json_encode($data);
	}

	if($_GET['action']=="searchGetAllowance")
	{
		// $DesignationName=$_GET['DesignationName'];
		// $Description=isset($_GET['Description'])?$_GET['Description']:'';
		// $DesignationName=isset($_GET['DesignationName'])?$_GET['DesignationName']:'';
		$Employee=isset($_GET['Employee'])?$_GET['Employee']:'';
		$Allowance=isset($_GET['Allowance'])?$_GET['Allowance']:'';
		$AllowanceType=isset($_GET['AllowanceType'])?$_GET['AllowanceType']:'';

		if($Employee!="")
		{
			$Employee="SalesManName like '%".$Employee."%' and ";
		}
		else
		{
			$Employee="";
		}
		if($Allowance!="")
		{
						$Allowance="MasterValue like '%".$Allowance."%' and ";

		}
		else
		{
			$Allowance="";
		}
		$searchbox=$Employee."".$Allowance;
		$data=array();
		echo $sql="SELECT EmployeeAllowanceID as row_num, ea.SalesManID , SalesManName as Employee, 
		ea.MasterValueID , MasterValue as Allowance, AllowanceType,'' as col_Action
			FROM EmployeeAllowance as ea 
			inner join SalesManInformation as smi on ea.SalesManID=smi.SalesManID
			inner join master_values as mv on ea.MasterValueID=mv.MasterValueID where ".$searchbox." active=1";
		$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{
			//$row['SalesManName']=htmlspecialchars($row['SalesManName']);
			$row['Employee']=htmlspecialchars($row['Employee']);
			$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;	
		}
		echo json_encode($data);
	
	}

	if($_GET['action']=="InsertAllowance")
	{
		$data=array_keys($_POST);
		$data=json_decode($data[0]);
		
		$EmployeeAllowanceID=isset($data->EmployeeAllowanceID)?$data->EmployeeAllowanceID:0;
		$SalesManID=isset($data->SalesManID)?$data->SalesManID:0;
		$MasterValueID=isset($data->MasterValueID)?$data->MasterValueID:0;
		$AllowanceType=isset($data->AllowanceType)?$data->AllowanceType:'';
		$AllowanceValue=isset($data->AllowanceValue)?$data->AllowanceValue:0;
		$Description=isset($data->Description)?$data->Description:'';
		if($EmployeeAllowanceID>0 && $EmployeeAllowanceID!="")
		{
			$updatesql="update EmployeeAllowance set SalesManID='".$SalesManID."', MasterValueID='".$MasterValueID."', AllowanceType='".$AllowanceType."', AllowanceValue='".$AllowanceValue."', Description='".$Description."' where EmployeeAllowanceID=".$EmployeeAllowanceID;	
		}

		else  {
				# code...
			//$DesignationID=getMaxId("pr_DesignationMaster","DesignationID",$connection)+1;
			$updatesql="insert into EmployeeAllowance (SalesManID,MasterValueID,AllowanceType,AllowanceValue,Description,Active) values(".$SalesManID.",".$MasterValueID.",'".$AllowanceType."',".$AllowanceValue.",'".$Description."',1)";
			}	
			odbc_exec($connection,$updatesql);
			$res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
			echo json_encode($res);


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

?>