<?php
session_start();
if(isset($_POST['paramValues'])){
    $paramValuesObj=json_decode($_POST['paramValues']);
    $paramValuesObj->currentDate = date('Y-m-d'); 
    $clsObj = new UpdateAdvanceAmountDetails();
    $clsObj->updateDetails($paramValuesObj);
}else{
    echo "result not found5";
}

class UpdateAdvanceAmountDetails{
    function __construct(){
        require_once dirname(__DIR__).'../../php/DB_Connect.php';
        $dbClsObj=new DB_conncect();
        $this->db=$dbClsObj->connectToDB();
        $this->db_Pdo=$dbClsObj->connectToDBVia_PDO();
    }
    function updateDetails($paramObj){
        $responseArray=array();
        if($paramObj->transactionType=="Adjustment"){
            $stmt=$this->getAdjustmentsQueryStmt($paramObj);
        }elseif($paramObj->transactionType=="Advance"){
            $stmt=$this->getAdvanceQueryStmt($paramObj);
        }
        // $stmt=$this->db->prepare("UPDATE `fn_advances` SET `ADVANCE`= ?, `REMARKS`=?, `UPDATED_DATE`=?  WHERE `ID`=?");
        // $stmt->bind_param('ssss', $paramObj->advanceAmount,  $paramObj->remarks, $paramObj->currentDate, $paramObj->db_tableIndex);
        try{
            $this->db_Pdo->beginTransaction();
                $executionStatus=$stmt->execute();
            $this->db_Pdo->commit();
            if(!$executionStatus){
                $responseArray["error"]=true;
                $responseArray["message"]="Unable to execute";
            }else{
                $stmt->store_result();
                if(($stmt->affected_rows)>0){
                    $responseArray["error"]=false;
                    $responseArray["message"]="Succesfully Updated";
                }else{
                    $responseArray["error"]=true;
                    $responseArray["message"]="No changes done.";
                }
            }
        }catch(PDOException $e){
            $responseArray["error"]=true;
            $responseArray["message"]="Internal error 500!";
        }
        echo json_encode($responseArray);
    }
    function getAdjustmentsQueryStmt($paramObj){
        $stmt=$this->db->prepare("UPDATE `fn_adjustments` SET `ADJUSTMENT_AMOUNT`=?, `REMARKS`=?, `UPDATED_DATE`=? WHERE `TRANSACTION_ID`=?");
        $stmt->bind_param('ssss',$paramObj->transactionAmount, $paramObj->remarks, $paramObj->currentDate, $paramObj->transactionId);
        return $stmt;
    }
    function getAdvanceQueryStmt($paramObj){
        $stmt=$this->db->prepare("UPDATE `fn_advances` SET `ADVANCE`=?, `REMARKS`=?, `UPDATED_DATE`=? WHERE `TRANSACTION_ID`=?");
        $stmt->bind_param('ssss',$paramObj->transactionAmount, $paramObj->remarks, $paramObj->currentDate, $paramObj->transactionId);
        return $stmt;
    }
}


?>