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


if(isset($_GET['action']))
{
  if($_GET['action']=="InsertMemberCard")
  {
      odbc_autocommit($connection, FALSE);
$join1=array();
    $data=array_keys($_POST);
    $Data=json_decode($data[0]);
    //$Data->CurrentTimingSetupID=0;
  //  $Data->CurrentTimingSetupID=1date("Y-m-d");
//print_r($Data);
//return;
  /*  $j=$Data->DateofJoining;
    $join1=explode("-",$j);
    $Data->DateOfjoining=$join1[2]."-".$join1[1]."-"$join1[0];
    
    $birth=explode("-",$Data->DateOfBirth);
    $Data->DateOfBirth=$birth[2]."-".$birth[1]."-"$birth[0];
*/
    $Data->CurrentSalary=str_replace("_",".",$Data->CurrentSalary);
    $Data->EmailID=str_replace("_",".",$Data->EmailID);
    $MemberName=isset($Data->SalesManName)?$Data->SalesManName:'';
    $MemberName=str_replace("_"," ",$MemberName);
    $CardNumber=isset($Data->FathersName)?$Data->FathersName:'';

  $DateOfjoining=isset($Data->DateOfjoining)?$Data->DateOfjoining:Date("Y-m-d");
    $DateOfExpiry=isset($Data->DateOfExpiry)?$Data->DateOfExpiry:Date("Y-m-d");
    $AdditionalInfo5=isset($Data->Code)?$Data->Code:'';
    $AdditionalInfo2=isset($Data->ContactNo)?$Data->ContactNo:'';
    $Balance= (isset($Data->CurrentSalary) && $Data->CurrentSalary>0)?$Data->CurrentSalary:0;
    $AdditionalInfo4=isset($Data->CellPhoneNo)?$Data->CellPhoneNo:'';
$IsCurrentlyActive= isset($Data->IsCurrentlyActive)?$Data->IsCurrentlyActive:0;
    $COAID= isset($Data->COAID)?$Data->COAID:0;
    $AdditionalInfo3=isset($Data->EmailID)?$Data->EmailID:'';

    //print_r($data);
    if(isset($Data->SalesManID) && $Data->SalesManID>0)
    {
      $SalesManID=$Data->SalesManID;
       $sql="update POS_MemberSetup set      
            MemberName='$MemberName',
                    RegistrationDate='$DateOfjoining'  ,
                    ExpiryDate='$DateOfExpiry'  ,
                    AdditionalInfo5='$AdditionalInfo5'  ,
                    AdditionalInfo2='$AdditionalInfo2'  ,
                    OpeningPoints=$Balance  ,
                    AdditionalInfo4='$AdditionalInfo4'  ,
                    IsActive=$IsCurrentlyActive,
                    AdditionalInfo3='$AdditionalInfo3' where MemberID=".$SalesManID;
               odbc_exec($connection,$sql) or die(odbc_error().$sql);

    }
    else
    {
    $MemberID=getMaxId("POS_MemberSetup","MemberID",$connection)+1;
                $KeyField=generateCode("POS_MemberSetup","KeyField","1=1",$connection);
                if($KeyField=="")$KeyField=1;//$KeyField=
            
            $KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
            $KeyField="SYM-".$KeyField2;
            

       $sql="insert into POS_MemberSetup(MemberCardTypeID,
          KeyField,
          MemberID  ,                   
            MemberName,
                    CardNumber,
                    RegistrationDate ,
                    ExpiryDate ,
                    AdditionalInfo5 ,
                    AdditionalInfo2 ,
                    OpeningPoints ,
                    AdditionalInfo4 ,
                    IsActive,
                    AdditionalInfo1,
                    AdditionalInfo3,
                    CreateLog

      )
            values(1,
            '$KeyField',
            $MemberID ,                   
            '$MemberName',
                    '$CardNumber',
                    '$DateOfjoining' ,
                    '$DateOfExpiry' ,
                    '$AdditionalInfo5' ,
                    '$AdditionalInfo2' ,
                    $Balance ,
                    '$AdditionalInfo4' ,
                    $IsCurrentlyActive,
                    '$COAID',
                    '$AdditionalInfo3',
                    '".$_SESSION['UserName']."'
                    )";
            
            odbc_exec($connection,$sql) or die(odbc_error().$sql);
        //return;
            $date=Date("Y-m-d");
      $CardID=getMaxId("CardTransaction","ID",$connection)+1;

            //insert opening balance in card 
$sql="INSERT INTO [dbo].[CardTransaction]
           ([ID]
           ,[CardNumber]
           ,[COAID]
           ,[Debit]
           ,[Credit]
           ,[Narration]
           ,[TDate]
           ,[Reference]
           ,[VoucherID]
           ,[isActive]
           ,[CreateLog]
           ,[InvoiceID]
           ,[LocationID])
     VALUES($CardID,'$CardNumber',$COAID,$Balance,0,'opening $CardNumber','$date','$KeyField',0,1,'".$_SESSION['UserName']."',0,1)";
     odbc_exec($connection,$sql) or die(odbc_error().$sql);
          }//end insert
       //      $res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
       if (!odbc_error())
   {
      odbc_commit($connection);
      $res=array('status'=>"ok",'description'=>'Data Saved Sucessfully');
  }
else
{
      odbc_rollback($connection);
      $res=array('status'=>"Error",'description'=>'Record  Not Updated');

}
    echo json_encode($res);

    
  }
	if($_GET['action']=="InsertSalesMan")
	{
          odbc_autocommit($connection, FALSE);
$join1=array();
		$data=array_keys($_POST);
		$Data=json_decode($data[0]);
		$Data->CurrentTimingSetupID=0;
	//	$Data->CurrentTimingSetupID=1date("Y-m-d");
//print_r($data);
	/*	$j=$Data->DateofJoining;
		$join1=explode("-",$j);
		$Data->DateOfjoining=$join1[2]."-".$join1[1]."-"$join1[0];
		
		$birth=explode("-",$Data->DateOfBirth);
		$Data->DateOfBirth=$birth[2]."-".$birth[1]."-"$birth[0];
*/
		$Data->CurrentSalary=str_replace("_",".",$Data->CurrentSalary);
    $Data->SalesManName=str_replace("_"," ",$Data->SalesManName);
		$Data->EmailID=str_replace("_",".",$Data->EmailID);
		//print_r($data);
		if(isset($Data->SalesManID) && $Data->SalesManID>0)
		{
			$SalesManID=$Data->SalesManID;
			 $sql="update SalesManInformation set 		 
					SalesManName='$Data->SalesManName',
                    FathersName='$Data->FathersName',
                    Address1='$Data->Address1',
                    Address2='$Data->Address2',
                    DateOfjoining='$Data->DateOfjoining' ,
                    Code='$Data->Code' ,
                    ContactNo='$Data->ContactNo' ,
                    Barcode='$Data->Barcode' ,
                   	CurrentSalary= $Data->CurrentSalary ,
                    CellPhoneNo='$Data->CellPhoneNo' ,
                  	 IsCurrentlyActive= $Data->IsCurrentlyActive,
                  	 LocationID= $Data->LocationID,
                  	 DepartmentID= $Data->DepartmentID,
                  	 DesignationID= $Data->DesignationID,
                    Address3='$Data->EmailID' where SalesManID=".$SalesManID;
		}
		else
		{
		$SalesManID=getMaxId("SalesManInformation","SalesManID",$connection)+1;
		    $KeyField=floatval(generateCode("SalesManInformation","KeyField","",$connection))+1;
           	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="Emp-".$KeyField2;
           	

		 	 $sql="insert into SalesManInformation(
					KeyField,
					SalesManID  ,                   
		  			SalesManName,
                    FathersName,
                    Address1,
                    Address2,
                    DateOfjoining ,
                    
                    Code ,
                    ContactNo ,
                    Barcode ,
                    CurrentSalary ,
                    CellPhoneNo ,
             
                    IsCurrentlyActive,
                    LocationID,
                    DepartmentID,
                    DesignationID,
                    Address3
			)
		  			values(
		  			'$KeyField',
		  			$SalesManID ,                   
		  			'$Data->SalesManName',
                    '$Data->FathersName',
                    '$Data->Address1',
                    '$Data->Address2',
                    '$Data->DateOfjoining' ,
                    '$Data->Code' ,
                    '$Data->ContactNo' ,
                    '$Data->Barcode' ,
                    $Data->CurrentSalary ,
                    '$Data->CellPhoneNo' ,
                    1,
                    $Data->LocationID,
                    $Data->DepartmentID,
                    $Data->DesignationID,
                    '$Data->EmailID'
                    )";
            }
            odbc_exec($connection,$sql) or die(odbc_error().$sql);
  //      $res=array('status'=>"ok",'description'=>'Record Save Sucessfully');
		     if (!odbc_error())
   {
      odbc_commit($connection);
      $res=array('status'=>"ok",'description'=>'Data Saved Sucessfully');
  }
else
{
      odbc_rollback($connection);
      $res=array('status'=>"Error",'description'=>'Record  Not Updated');

}
    echo json_encode($res);

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

    $data=array();

    if(isset($_GET['SalesManID']))
    {
      $SalesManID=$_GET['SalesManID'];
     $sql="select ROW_NUMBER() OVER (
    
      ORDER BY emp.[Keyfield])  as row_num,MemberName as SalesManName,dept.[MemberCardTypeName] as Name,'' as DesignationName,AdditionalInfo5 as Code,AdditionalInfo5  as ContactNo,emp.KeyField as KeyField,'' as col_Action,MemberID as SalesManID,ExpiryDate,RegistrationDate,'' as Address1,'' as  as Address2,emp.AdditionalInfo3 as Email,emp.AdditionalInfo4 as CellPhoneNo,CardNumber as CardNumber,OpeningPoints as CurrentSalary,COAID as DepartmentID,'' as DesignationID,'' as Barcode,IsActive,MemberID as SalesManID,DateOfBirth from [POS_MemberSetup] as emp left outer join [POS_MemberCardTypes] as dept on emp.MemberCardTypeID=dept.[MemberCardTypeID]
      where MemberID=".$SalesManID;

      }
      else
      {
        if(isset($_GET['SalesManName']) && $_GET['SalesManName']!="")
        {
          $SalesManName=" MemberName like '%".$_GET['SalesManName']."%' and ";
        }
        else
        {
          $SalesManName="";
        }
        if(isset($_GET['DesignationName']) && $_GET['DesignationName']!="")
        {
          $DesignationName=" DesignationName like '%".$_GET['DesignationName']."%' and ";
        }
        else
        {
          $DesignationName="";
        }
        $searchbox=$SalesManName."".$DesignationName;
 $sql="select ROW_NUMBER() OVER (
      ORDER BY emp.[Keyfield])  as row_num,MemberName as SalesManName,dept.[MemberCardTypeName] as Name,'' as DesignationName,AdditionalInfo5 as Code,AdditionalInfo5  as ContactNo,emp.KeyField as KeyField,'' as col_Action,MemberID as SalesManID,ExpiryDate,RegistrationDate,'' as Address1,'' as Address2,emp.AdditionalInfo3 as Email,emp.AdditionalInfo4 as CellPhoneNo,CardNumber as CardNumber,OpeningPoints as CurrentSalary,AdditionalInfo1 as DepartmentID,'' as DesignationID,'' as Barcode,IsActive,MemberID as SalesManID,DateOfBirth from [POS_MemberSetup] as emp left outer join [POS_MemberCardTypes] as dept on emp.MemberCardTypeID=dept.[MemberCardTypeID]
     where $searchbox 1=1";

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

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

		if(isset($_GET['SalesManID']))
		{
			$SalesManID=$_GET['SalesManID'];
		 $sql="select ROW_NUMBER() OVER (
		
      ORDER BY emp.[Keyfield])  as row_num,SalesManName,dept.Name as Name,dm.DesignationName as DesignationName,Code,emp.ContactNo as ContactNo,emp.KeyField as KeyField,'' as col_Action,SalesManID,DateOfjoining,Address1,emp.Address2 as Address2,emp.Address3 as Email,CellPhoneNo,FathersName,CurrentSalary,DepartmentID,emp.DesignationID as DesignationID,Barcode,'' as Msg_TimeIn,'' as Msg_TimeOut,'' as Msg_Late,'' as Msg_Absent,'' as Msg_EarlyLeave,SalesManID,DateOfBirth from [SalesManInformation] as emp left outer join godowns as dept on emp.DepartmentID=dept.GodownID
			left outer join pr_DesignationMaster as dm on dm.DesignationID=emp.DesignationID
			where SalesManID=".$SalesManID;

  		}
  		else
  		{
        if(isset($_GET['SalesManName']) && $_GET['SalesManName']!="")
        {
          $SalesManName=" SalesManName like '%".$_GET['SalesManName']."%' and ";
        }
        else
        {
          $SalesManName="";
        }
        if(isset($_GET['DesignationName']) && $_GET['DesignationName']!="")
        {
          $DesignationName=" DesignationName like '%".$_GET['DesignationName']."%' and ";
        }
        else
        {
          $DesignationName="";
        }
        $searchbox=$SalesManName."".$DesignationName;
 $sql="select ROW_NUMBER() OVER (
		
      ORDER BY emp.[Keyfield])  as row_num,SalesManName,dept.Name as Name,dm.DesignationName as DesignationName,Code,emp.ContactNo as ContactNo,emp.KeyField as KeyField,'' as col_Action,SalesManID,DateOfjoining,Address1,emp.Address2 as Address2,emp.Address3 as Email,CellPhoneNo,FathersName,CurrentSalary,DepartmentID,emp.DesignationID as DesignationID,Barcode,'' as Msg_TimeIn,'' as Msg_TimeOut,'' as Msg_Late,'' as Msg_Absent,'' as Msg_EarlyLeave,IsCurrentlyActive,SalesManID,'' as DateOfBirth from [SalesManInformation] as emp left outer join godowns as dept on emp.DepartmentID=dept.GodownID
			left outer join pr_DesignationMaster as dm on dm.DesignationID=emp.DesignationID where $searchbox emp.Active=1";

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

	}



}


?>