<?php
if(isset($_POST['paramValues'])){
    $paramValuesObj=json_decode($_POST['paramValues']);
    $classObj=new verifyTID_getFortNightAdvDetails();
    $classObj->pdoOperations($paramValuesObj);
}else{
    echo "No Result Found";
}
class verifyTID_getFortNightAdvDetails{
    function __construct(){
        require_once dirname(__DIR__).'../../php/DB_Connect.php';
        $dbClassObj=new DB_conncect();
        $this->db=$dbClassObj->connectToDB();
        $this->db_Pdo=$dbClassObj->connectToDBVia_PDO();
    }
    function pdoOperations($paramValuesObj){
        $returnResponseArray=array();
        try{
            $transactionIdQuery=$this->verifyTransactionId($paramValuesObj->transactionIdsArray);
            $fnAdvAmtQuery=$this->getFortNightAdvanceAmountDetails($paramValuesObj->fortNight);
           
            $this->db_Pdo->beginTransaction();
                $stmt=$this->db_Pdo->query($transactionIdQuery.$fnAdvAmtQuery);
                $returnResponseArray['message']="SUCCESS"; // if any error occur catch function handles, so directly sucess.
                $returnResponseArray['TransactionID_responseDataArray']=$stmt->fetchAll(PDO::FETCH_OBJ);
                $stmt->nextRowset();
                $returnResponseArray['fnAdvanceAmountDetails_responseDataArray']=$stmt->fetchAll(PDO::FETCH_OBJ);
                $stmt ->closeCursor(); //frees up the connection to the server so that other SQL statements may be issued, but leaves the statement in a state that enables it to be executed again.
            $this->db_Pdo->commit();  
        }catch(PDOException $e){
            $this->db_Pdo->rollback();//  if any of 2 select query fail rollback. (i.e):-Recognize mistake and roll back changes */
            $returnResponseArray['message']=$e;
        }
        
        echo json_encode($returnResponseArray);
    }
    
    function verifyTransactionId($transactionIdsArray){
        $queryStarting="SELECT * FROM `fn_advances` WHERE `TRANSACTION_ID` IN ";
        $queryEnd="(";
        $lastLoopIndex=count($transactionIdsArray)-1;   
        for($loopIndex = 0; $loopIndex < count($transactionIdsArray); $loopIndex++){
            if($loopIndex!=$lastLoopIndex){
                $queryEnd.="'".$transactionIdsArray[$loopIndex]."',";
            }else{
                $queryEnd.="'".$transactionIdsArray[$loopIndex]."'); ";
            }   
        }
        $transactionIdQuery=$queryStarting.$queryEnd;
        return $transactionIdQuery;
    }
    
    function getFortNightAdvanceAmountDetails($fortNight){
        $fnAdvAmtQuery=" 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`='".$fortNight."' 
                        LEFT JOIN `fn_advances` ON `fn_advances`.`EMP_ID`=`empCircle`.`EMP_ID` AND `fort_night_claims`.`FORTNIGHT`='".$fortNight."' ;";
        return $fnAdvAmtQuery;
    }
    
}

?>