<?php
if(isset($_POST["paramValues"])){
    $paramObj=json_decode($_POST["paramValues"]);
    $clsObj= new invoiceUpdate();
    $clsObj->updateInvoiceDetails($paramObj);
    
}else{
    echo "Result Not found";
}
class invoiceUpdate{
    function __construct(){
        require_once dirname(__DIR__).'../../../../php/DB_Connect.php';
        $obj=new DB_conncect();
        $this->db=$obj->connectToDB();
        $this->db_Pdo=$obj->connectToDBVia_PDO();
    }
    function updateInvoiceDetails($paramObj){
        $mainArray=array();
        $stmt=$this->db->prepare("UPDATE `invoices` SET `INVOICE_RISED_ON`=?, `INVOICE_VALUE`=?, `PAYMENT_RECEIVED_ON`=?,  `PAYMENT_RECEIVED_VALUE`=?,
                                                        `PAYMENT_REMARKS`=?,  `REMAINING_BALANCE_IF_ANY`=? WHERE `INVOICE_NUMBER`=? ");
        $stmt->bind_param('sssssss',$paramObj->invoiceRaiseOn, $paramObj->invoiceValue, $paramObj->paymentReceivedOn, $paramObj->paymentReceivedValue,
                                     $paramObj->paymentRemarks, $paramObj->remainingBalance, $paramObj->invoiceNumber);
        try{
            $this->db_Pdo->beginTransaction();
                $stmtExecution=$stmt->execute();
                if(!$stmtExecution){
                    $mainArray["message"]="FAILURE";
                    $mainArray["reason"]="Query Execution Failure.";
                }else{
                    $stmt->store_result();
                    if(($stmt->affected_rows)>0){
                       $mainArray["message"]="UPDATED"; 
                       $mainArray["reason"]="Succesfully Updated!";
                    }else{
                        $mainArray["message"]="FAILURE";
                        $mainArray["reason"]="No changes done.";
                    }
                }
            $this->db_Pdo->commit();
            
        }catch(PDOException $e){
            $mainArray['message']="FAILURE";
            $mainArray['reason']="Internal Error 500!";
        }
        echo json_encode($mainArray);
    }
}

?>