<?php
if(isset($_POST['paramValues'])){
    $paramValueObj=json_decode($_POST['paramValues']);
    $obj=new getAdvanceAmountStatement();
    $obj->getAdvanceDetails($paramValueObj);
}else{
    echo "No data found";
}

class getAdvanceAmountStatement{
    function __construct(){
            require_once dirname(__DIR__).'../../php/DB_Connect.php';
            $DBClassObject = new DB_conncect();
            $this->db=$DBClassObject->connectToDB();
            $this->db_Pdo =$DBClassObject->connectToDBVia_PDO();
            
    }
    function getAdvanceDetails($paramObj){
        $responseArray=array();
        $tempArray=array();
        // $stmt=$this->db->prepare("SELECT * FROM `fn_advances` WHERE `EMP_ID`=? AND `FORTNIGHT`=? UNION SELECT * FROM `fn_adjustments` where `EMP_ID`=? AND `FORTNIGHT`=?");
         $stmt=$this->db->prepare("SELECT  `fn_adjustments`.`ID`,`fn_adjustments`.`EMP_ID` AS EmpId, `fn_adjustments`.`FORTNIGHT` AS FortNight, `fn_adjustments`.`TRANSACTION_ID` AS TransactionId,
                                            `fn_adjustments`.`ADJUSTMENT_AMOUNT` AS TransactionAmount, `fn_adjustments`.`ADJUSTMENT_RELEASED_DATE` AS TransactionDate,
                                            `fn_adjustments`.`REMARKS` AS Remarks, `fn_adjustments`.`CREATED_DATE` AS CreatedDate, `fn_adjustments`.`CREATED_BY` AS CreatedBy,
                                            `fn_adjustments`.`UPDATED_DATE` AS UpdatedDate,'Adjustment' AS TransactionType
                                            FROM `fn_adjustments` WHERE `fn_adjustments`.EMP_ID=? AND `fn_adjustments`.`FORTNIGHT`=?
                                    UNION 
                                    SELECT `fn_advances`.`ID`,`fn_advances`.`EMP_ID` AS EmpId, `fn_advances`.`FORTNIGHT` AS FortNight, `fn_advances`.`TRANSACTION_ID` As TransactionId,
                                            `fn_advances`.`ADVANCE` AS TransactionAmount, `fn_advances`.`ADVANCE_RELEASE_DATE` AS TransactionDate,
                                            `fn_advances`.`REMARKS` AS Remarks, `fn_advances`.`CREATED_DATE` AS CreatedDate, `fn_advances`.`CREATED_BY` AS  CreatedBy,
                                            `fn_advances`.`UPDATED_DATE`  AS UpdatedDate, 'Advance' AS TransactionType
                                            FROM `fn_advances` WHERE `fn_advances`.`EMP_ID`=? AND `fn_advances`.`FORTNIGHT`=?");
         $stmt->bind_param('ssss',$paramObj->empId,$paramObj->fortNight,$paramObj->empId, $paramObj->fortNight);
        try{
            $this->db_Pdo->beginTransaction();
                $executionStatus=$stmt->execute();
            $this->db_Pdo->commit();
            if(!$executionStatus){
                $responseArray["message"]="FAILURE";
            }else{
                $result=$stmt->get_result();
                while($data=$result->fetch_assoc()){
                    array_push($tempArray,$data);
                }
                $responseArray["message"]="SUCCESS";
                $responseArray["responseDataArray"]=$tempArray;
                
            }
            
        }catch(PDOException $e){
            $responseArray['message']="FAILURE";
        }
        
         echo json_encode($responseArray);
    }
    
}


?>