<?php
session_start();
if(isset($_POST['paramValues'])){
    $paramObj=json_decode($_POST['paramValues']);
    $fortNight=$paramObj->year."-".$paramObj->month."-".$paramObj->FnValue;                    //fortNight formate '2021-November-1st FN'
    $userId=$_SESSION['userId'];
    $userDepartment=$_SESSION['teamLeads'];
    $obj=new employeeAdvanceInfo();
    $obj->getEmployeeAdvanceInfo($fortNight, $userId, $userDepartment);
}else{
    echo "Nodata found";
}


class employeeAdvanceInfo{
    
    function __construct(){
        require_once dirname(__DIR__).'../../php/DB_Connect.php';
        $obj=new DB_conncect();
        $this->db=$obj->connectToDB();
        $this->db_Pdo=$obj->connectToDBVia_PDO();
    }
    function getEmployeeAdvanceInfo($fortNight,$userEmpId,$userDept){
        $returnResponseArray=array();
        $tempArray=array();
            if($userDept==TL_DEPARTMENT){
                $stmt=$this->getTLDepartmentQueryStmt($fortNight,$userEmpId);
            }else if($userDept==FINANCE_DEPARTMENT){
                $stmt=$this->getFinanceDepartmentQueryStmt($fortNight);
            }

        try{ 
            $this->db_Pdo->beginTransaction();
                $queryExecutionStatus=$stmt->execute();
            $this->db_Pdo->commit();
            if(!$queryExecutionStatus){
                $returnResponseArray['message']='FAILURE';
            }else{
                 $result=$stmt->get_result();
                 while($data=$result->fetch_assoc()){
                     array_push($tempArray,$data);
                 }
                 $returnResponseArray['message']='SUCCESS';
                 $returnResponseArray['responseDataArray']=$tempArray;
            }
        }
        catch(PDOException $e){
            $this->db_Pdo->rollback();
            $returnResponseArray['message']='FAILURE';
        }
        
        echo json_encode($returnResponseArray);
    }
    
    function getFinanceDepartmentQueryStmt($fortNight){
        $stmt=$this->db->prepare("SELECT `empCircle`.`EMP_ID`,`empCircle`.`EMP_NAME`,
                                         `fort_night_claims`.`FORTNIGHT`,
                                         `fn_advances`.`TRANSACTION_ID`, `fn_advances`.`ADVANCE`, `fn_advances`.`ADVANCE_RELEASE_DATE`,`fn_advances`.`REMARKS` 
                                  FROM `empCircle` 
                                  LEFT JOIN `fort_night_claims` ON `fort_night_claims`.`EMP_ID`=`empCircle`.`EMP_ID` AND `fort_night_claims`.`FORTNIGHT`=? 
                                  LEFT JOIN `fn_advances` ON `fn_advances`.`EMP_ID`=`empCircle`.`EMP_ID` AND `fn_advances`.`FORTNIGHT`=?");
        $stmt->bind_param("ss",$fortNight,$fortNight);
        return $stmt;
    }
    
    function getTLDepartmentQueryStmt($fortNight, $TLEmpId){
        $stmt=$this->db->prepare("SELECT `empCircle`.`EMP_ID`,`empCircle`.`EMP_NAME`,
                                         `fort_night_claims`.`FORTNIGHT`,
                                         `fn_advances`.`TRANSACTION_ID`, `fn_advances`.`ADVANCE`, `fn_advances`.`ADVANCE_RELEASE_DATE`,`fn_advances`.`REMARKS` 
                                  FROM `empCircle` 
                                  LEFT JOIN `fort_night_claims` ON `fort_night_claims`.`EMP_ID`=`empCircle`.`EMP_ID` AND `fort_night_claims`.`FORTNIGHT`=? 
                                  LEFT JOIN `fn_advances` ON `fn_advances`.`EMP_ID`=`empCircle`.`EMP_ID` AND `fn_advances`.`FORTNIGHT`=?  
                                   WHERE  `empCircle`.`TL_EMP_ID`=?");
        $stmt->bind_param("sss",$fortNight,$fortNight,$TLEmpId);
        return $stmt;
    }
    
    
    
}


?>