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

class AdditionalAdjustment{
    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 getPendingAdditionalAdjustment2($paramObj){
        $resultArray=array();
        $tempArray=array();
        $subArray=array();
        $tableTitle=["fortNightTableData", "fn_adjustmentsTableData"];
        $fortNightTableQuery="SELECT `EMP_ID` AS `EmpId`,`NAME`  AS `EmpName`, `FORTNIGHT` AS `FortNight`,`FINANCE_FN_FINAL_AMOUNT` AS `Finance Approved`,`TOTAL_ADVANCE_GIVEN` AS `Total Advance`, 
                                `PREVIOUS_FN_ADJUSTMENT` AS `Prev Adjustment`, `FINAL_PAYABLE_VALUE` AS `Final Payable`, `FTA_ADDITIONAL_ADJUSTMENT` AS `Total Addition Adjustments` 
                        FROM `fort_night_claims` WHERE YEAR(`FROM_DATE`)='".$paramObj->year."' AND MONTHNAME(`FROM_DATE`)='".$paramObj->month."' AND `FINAL_PAYABLE_VALUE`>0 ; ";
        $additionalAdjustmentTableQuery="SELECT * FROM `fn_adjustments` WHERE `FORTNIGHT` LIKE '".$paramObj->year."-".$paramObj->month."-%'";
        try{
            $stmt=$this->db_Pdo->query($fortNightTableQuery.$additionalAdjustmentTableQuery);
            $loopIndex=0;
            do{
                $tempArray= array();
                while($data=$stmt->fetch(PDO::FETCH_ASSOC)){
                    array_push($tempArray, $data);
                }
                $subArray[$tableTitle[$loopIndex]]=$tempArray;
                unset($tempArray);
                $loopIndex++;
            }while($stmt->nextRowset());
            $resultArray["error"]=false;
            $resultArray["response"]=$subArray;
        }catch(PDOException $e){
            $resultArray["error"]=true;
            $reultArray["message"]="Internal Error 500!";
        }
        echo json_encode($resultArray);
    }
    function getPendingAdditionalAdjustment($paramObj){
        $resultArray=array();
        $subArray=array();
        $onlyPendingFn_adjustments_fnClaimsTable=array();
        $onlyTransactionDetails=array();
        $fnClaimsKeys=["EmpId","EmpName", "FortNight", "Finance Approved", "Total Advance", "Prev Adjustment", "Final Payable", "Total Addition Adjustments"];
        $adjustmentsKeys=["EmpId","FortNight","TRANSACTION_ID","ADJUSTMENT_AMOUNT","ADJUSTMENT_RELEASED_DATE","REMARKS","CREATED_DATE","CREATED_BY","UPDATED_DATE"];
        if($paramObj->department == FINANCE_DEPARTMENT){
            $stmt=$this->getFinanceQuery_stmt($paramObj);
        }elseif($paramObj->department == TL_DEPARTMENT){
            $stmt=$this->getTLQuery_stmt($paramObj);
        }
        try{
            $this->db_Pdo->beginTransaction();
                if($stmt->execute()){
                    $resultArray["error"]=false;
                    while($data=$stmt->fetch(PDO::FETCH_ASSOC)){
                        $currentLoop_fnClaims_temp_array=array();
                        foreach($fnClaimsKeys as $index=>$fnClaimKey){
                            $currentLoop_fnClaims_temp_array[$fnClaimKey] = $data[$fnClaimKey];
                        }
                        
                        if(!in_array($currentLoop_fnClaims_temp_array, $onlyPendingFn_adjustments_fnClaimsTable)){ // remove duplicate fn_claims 
                            array_push($onlyPendingFn_adjustments_fnClaimsTable, $currentLoop_fnClaims_temp_array); // if not exist then push
                        }
                        if(isset($data["TRANSACTION_ID"])){ //if currentLoop consist TransactionId only push to all Fn Pending Transaction Array
                            $currentLoop_fnAdjustments_temp_array=array();
                            foreach($adjustmentsKeys as $index=>$adjustmentKey){
                                $currentLoop_fnAdjustments_temp_array[$adjustmentKey] = $data[$adjustmentKey];
                            }
                            array_push($onlyTransactionDetails, $currentLoop_fnAdjustments_temp_array);
                        }
                    }
                    $subArray["fortNightTableData"]=$onlyPendingFn_adjustments_fnClaimsTable;
                    $subArray["fn_adjustmentsTableData"]=$onlyTransactionDetails;
                    $resultArray["response"]=$subArray;
                }else{
                    $resultArray["error"]=true;
                    $resultArray["message"]="Unable To Execute";
                }
            $this->db_Pdo->commit();
            
        }catch(PDOException $e){
            $resultArray["error"]=true;
            $resultArray["message"]="Internal Error 500!";
        }
        echo json_encode($resultArray);
    }
    
 function getFinanceQuery_stmt($paramObj){
        $query="SELECT `fort_night_claims`.`EMP_ID` AS `EmpId`,`NAME`  AS `EmpName`, `fort_night_claims`.`FORTNIGHT` AS `FortNight`,`FINANCE_FN_FINAL_AMOUNT` AS `Finance Approved`,`TOTAL_ADVANCE_GIVEN` AS `Total Advance`, 
                                `PREVIOUS_FN_ADJUSTMENT` AS `Prev Adjustment`, `FINAL_PAYABLE_VALUE` AS `Final Payable`, `FTA_ADDITIONAL_ADJUSTMENT` AS `Total Addition Adjustments`,
                                `fn_adjustments`.`TRANSACTION_ID`, `fn_adjustments`.`ADJUSTMENT_AMOUNT`,`fn_adjustments`.`ADJUSTMENT_RELEASED_DATE`,`fn_adjustments`.`REMARKS`, `fn_adjustments`.`CREATED_DATE`, `fn_adjustments`.`CREATED_BY`, `fn_adjustments`.`UPDATED_DATE`
                        FROM `fort_night_claims`
                        LEFT JOIN `fn_adjustments` ON `fort_night_claims`.`EMP_ID` = `fn_adjustments`.`EMP_ID`  AND `fort_night_claims`.`FORTNIGHT` = `fn_adjustments`.`FORTNIGHT`
                        WHERE YEAR(`fort_night_claims`.`FROM_DATE`)='".$paramObj->year."' AND MONTHNAME(`fort_night_claims`.`FROM_DATE`)='".$paramObj->month."' AND `FINAL_PAYABLE_VALUE`>0";
        $stmt=$this->db_Pdo->prepare($query);
        return $stmt;
    }
    function getTLQuery_stmt($paramObj){
        $query="SELECT `fort_night_claims`.`EMP_ID` AS `EmpId`,`NAME`  AS `EmpName`, `fort_night_claims`.`FORTNIGHT` AS `FortNight`,`FINANCE_FN_FINAL_AMOUNT` AS `Finance Approved`,`TOTAL_ADVANCE_GIVEN` AS `Total Advance`, 
                                `PREVIOUS_FN_ADJUSTMENT` AS `Prev Adjustment`, `FINAL_PAYABLE_VALUE` AS `Final Payable`, `FTA_ADDITIONAL_ADJUSTMENT` AS `Total Addition Adjustments`,
                                `fn_adjustments`.`TRANSACTION_ID`, `fn_adjustments`.`ADJUSTMENT_AMOUNT`,`fn_adjustments`.`ADJUSTMENT_RELEASED_DATE`,`fn_adjustments`.`REMARKS`, `fn_adjustments`.`CREATED_DATE`, `fn_adjustments`.`CREATED_BY`, `fn_adjustments`.`UPDATED_DATE`
                        FROM `fort_night_claims`
                        LEFT JOIN `fn_adjustments` ON `fort_night_claims`.`EMP_ID` = `fn_adjustments`.`EMP_ID`  AND `fort_night_claims`.`FORTNIGHT` = `fn_adjustments`.`FORTNIGHT`
                        WHERE YEAR(`fort_night_claims`.`FROM_DATE`)='".$paramObj->year."' AND MONTHNAME(`fort_night_claims`.`FROM_DATE`)='".$paramObj->month."'  AND `fort_night_claims`.`TL_ID`='".$paramObj->userId."' AND `FINAL_PAYABLE_VALUE`>0";
        $stmt=$this->db_Pdo->prepare($query);
        return $stmt;
    }
}

?>