<?php
if(isset($_POST['paramValues'])){
    $paramObj= json_decode($_POST["paramValues"]);
    $clsObj=new deleteAdvanceAmountDetails();
    $clsObj->deleteDetails($paramObj);
}

class deleteAdvanceAmountDetails{
    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 deleteDetails($paramObj){
        $responseArray=array();
        if($paramObj->transactionType == "Adjustment"){
            $stmt=$this->getAdjustmentsQuery($paramObj);
        }elseif($paramObj->transactionType == "Advance"){
            $stmt=$this->getAdvanceQuery($paramObj);
        }
        try{
            $this->db_Pdo->beginTransaction();
                $executionStatus= $stmt->execute();
            $this->db_Pdo->commit();
            if(!$executionStatus){
                $responseArray["error"]=true;
                $responseArray["message"]="Unable to execute delete query";
            }else{
                $responseArray["error"]=false;
                $responseArray["message"]="Successfully Deleted.";
            }
        }catch(PDOException $e){
            $responseArray["error"]=true;
            $responseArray["message"]="Internal error 500!";
        }
        echo json_encode($responseArray);
    }
    function getAdjustmentsQuery($paramObj){
        $stmt=$this->db->prepare("DELETE FROM `fn_adjustments` WHERE `TRANSACTION_ID`=?");
        $stmt->bind_param("s",$paramObj->transactionId);
        return $stmt;
    }
    
    function getAdvanceQuery($paramObj){
        $stmt=$this->db->prepare("DELETE FROM `fn_advances` WHERE `TRANSACTION_ID`=?");
        $stmt->bind_param("s",$paramObj->transactionId);
        return $stmt;
    }
}

?>