<?php
session_start();
    if(isset($_POST["paramValues"])){
        $paramObj=json_decode($_POST["paramValues"]);
        $paramObj->userId=$_SESSION["userId"];
        $paramObj->department=$_SESSION["teamLeads"];
        $clsObj=new PendingAdditionalAdjustments();
        $clsObj->getYearWisePendingAdditionalAdjustments($paramObj);
    }else{
        echo "result not found";
    }

class PendingAdditionalAdjustments{
    function __construct(){
        require_once dirname(__DIR__).'../../../php/DB_Connect.php';
        $clsObj = new DB_conncect();
        $this->db=$clsObj->connectToDB();
        $this->db_Pdo=$clsObj->connectToDBVia_PDO();
    }
    function getYearWisePendingAdditionalAdjustments($paramObj){
        $mainArray=array();
        $tempArray= array();
        $stmt=$this->getQueryStatement($paramObj);
        try{
            $this->db_Pdo->beginTransaction();
                if($stmt->execute()){
                    if($stmt->rowCount()>0){
                        while($data=$stmt->fetch(PDO::FETCH_ASSOC)){
                            array_push($tempArray, $data);
                        }
                    }
                    $mainArray["error"]=false;
                    $mainArray["responseArray"]=$tempArray;
                }else{
                    $mainArray["error"]=true;
                    $mainArray["message"]="Unable to execute query";
                }
            $this->db_Pdo->commit();
        }
        catch(PDOException $e){
            $mainArray["error"]=true;
            $mainArray["message"]="Internal Error 500!";
        }
        echo json_encode($mainArray);
    }
    private function getQueryStatement($paramObj){
        $query="SELECT  fnTable.*, fn_advances.FORTNIGHT, fn_advances.TRANSACTION_ID, fn_advances.ADVANCE AS TRANSACTION_AMOUNT, fn_advances.ADVANCE_RELEASE_DATE, fn_advances.REMARKS, fn_advances.CREATED_BY,
                                fn_advances.CREATED_DATE, fn_advances.UPDATED_BY, fn_advances.UPDATED_DATE 
                                FROM (SELECT `fort_night_claims`.`TL_ID`,`fort_night_claims`.EMP_ID,`fort_night_claims`.NAME,(SUM(`fort_night_claims`.FINANCE_FN_FINAL_AMOUNT)-(SUM(`fort_night_claims`.TOTAL_ADVANCE_GIVEN)+SUM(`fort_night_claims`.FTA_ADDITIONAL_ADJUSTMENT))) AS REMAINIG_AMOUNT,
                                              CONCAT(FinancialYearFromDate(`fort_night_claims`.FORTNIGHT),'_',FinancialYearToDate(`fort_night_claims`.FORTNIGHT))AS FINANCIAL_YEAR
                                      FROM `fort_night_claims` GROUP BY `fort_night_claims`.EMP_ID, FINANCIAL_YEAR) AS fnTable
                        LEFT JOIN fn_advances ON  fn_advances.TRANSACTION_ID=CONCAT_WS('_',fnTable.EMP_ID,fnTable.FINANCIAL_YEAR, 'yearAdjustment')";
        $whereCondition="WHERE fnTable.TL_ID='".$paramObj->userId."'";
        if($paramObj->department == FINANCE_DEPARTMENT){
            $stmt=$this->db_Pdo->prepare($query);
        }elseif($paramObj->department == TL_DEPARTMENT){
            $stmt=$this->db_Pdo->prepare($query." ".$whereCondition);
        }
        return $stmt;
    }
}
?>