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

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

	if($_GET['action']=="GetAllowanceDeduction")
	{
		$data=array();
			//	$DesignationName=isset($_GET['DesignationName'])?$_GET['DesignationName']:'';
		$Description=isset($_GET['Description'])?$_GET['Description']:'';
		$COAName=isset($_GET['COAName'])?$_GET['COAName']:'';
		// $AllowanceType=isset($_GET['AllowanceType'])?$_GET['AllowanceType']:'';
		if($Description!="")
		{
						$Description="Description like '%".$Description."%' and ";
		}
		else
		{
			$Description="";
		}
		if($COAName!="")
		{
			$COAName="COAName like '%".$COAName."%' and ";
		}
		else
		{
			$COAName="";
		}
	
		$searchbox=$Description."".$COAName;

		// $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 ROW_NUMBER() OVER (
			PARTITION BY adm.KeyField
			ORDER BY adm.KeyField
			 ) row_num, AllowanceDeductionID, adm.Description, adm.COAID as COAID, a.COAName, IsCarryForwardable , CanBeEncashed,adm.KeyField,'' as col_Action
				FROM pr_AllowanceDeductionMaster as adm 
				inner join Accounts as a on a.COAID=adm.COAID		
				where ".$searchbox." adm.active=1";   
		   
		$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{
			$row['Description']=htmlspecialchars($row['Description']);
			$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']=="InsertAllowanceDeduction")
	{
		$data=array_keys($_POST);
		$data=json_decode($data[0]);
		
		$AllowanceDeductionID=isset($data->AllowanceDeductionID)?$data->AllowanceDeductionID:0;
		$Description=isset($data->Description)?$data->Description:'';
		$COAID=isset($data->COAID)?$data->COAID:0;
		//$IsCarryForwardable=isset($data->IsCarryForwardable)?$data->IsCarryForwardable:0;		
		$IsCarryForwardable=isset($data->IsCarryForwardable)?$data->IsCarryForwardable:false;		
		//$CanBeEncashed=isset($data->CanBeEncashed)?$data->CanBeEncashed:0;		
		$CanBeEncashed=isset($data->CanBeEncashed)?$data->CanBeEncashed:false;		
		if($AllowanceDeductionID>0 && $AllowanceDeductionID!="")
		{
			$updatesql="update pr_AllowanceDeductionMaster set Description='".$Description."', COAID=".$COAID.", IsCarryForwardable='".$IsCarryForwardable."', CanBeEncashed='".$CanBeEncashed."' where AllowanceDeductionID=".$AllowanceDeductionID;	
		}

		else  {
			# code...
			$AllowanceDeductionID=getMaxId("pr_AllowanceDeductionMaster","AllowanceDeductionID",$connection)+1;
			$updatesql="insert into pr_AllowanceDeductionMaster (AllowanceDeductionID, TypeID,        Description,     COAID,      IsCarryForwardable,     CanBeEncashed,  KeyField,Active) 
														  values(".$AllowanceDeductionID.",0,'".$Description."',".$COAID.",'".$IsCarryForwardable."','".$CanBeEncashed."','',1)";
			}	
			odbc_exec($connection,$updatesql);
			$res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
			echo json_encode($res);
	}
	if($_GET['action']=="DeleteAllowanceDeduction")
	{
		$AllowanceDeductionID=$_GET['AllowanceDeductionID'];
		$sql="update pr_AllowanceDeductionMaster set Active=0 where AllowanceDeductionID=".$AllowanceDeductionID;
		odbc_exec($connection,$sql);
		$res=array('status'=>"ok",'description'=>'Record Deleted Sucessfully');
		echo json_encode($res);
	}

	if($_GET['action']=="GetCOAName")
	{
		$data=array();

		if(isset($_GET['COAID']))
		{
		$COAID=$_GET['COAID'];
		$sql="select COAID  as 
      		row_num, COAID, COAName,Degree,Active ,'' as col_Action
        	from [Accounts] as a
        	where Degree=5 and COAID=".$COAID;

  		}
  		else
  		{
        if(isset($_GET['COAName']) && $_GET['COAName']!="")
        {
          $COAName=" COAName like '%".$_GET['COAName']."%' and ";
        }
        else
        {
          $COAName="";
        }
        if(isset($_GET['Degree']) && $_GET['Degree']!="")
        {
          $Degree=" Degree like '%".$_GET['Degree']."%' and ";
        }
        else
        {
          $Degree="";
        }
        $searchbox=$COAName."".$Degree;
        $sql="select COAID  as 
        	row_num, COAID, COAName, Degree, Active, '' as col_Action
          from [accounts] as a  where $searchbox Active=1 and degree=5";

  		}
		$result=odbc_exec($connection, $sql) or die(odbc_error().$sql);
		while($row=odbc_fetch_array($result))
		{
			$row['COAName']=utf8_encode($row['COAName']);
			$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;	
		}
	  //	print_r($data);
		echo json_encode($data);
  }

}

?>