<?php
if(isset($_POST["paramValues"])){
    $paramObj=json_decode($_POST["paramValues"]);
    $obj= new updateDetails();
    $obj->updatePendingPaymentDetails($paramObj);
}else{
    echo "No records found";
}
class updateDetails{
    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 updatePendingPaymentDetails($paramObj){
        $returnMainArray=array();
        $totalInsertQueries=count($paramObj);
        $stmt=$this->getStatement($paramObj);
        try{
            $this->db_Pdo->beginTransaction();
            $executionStatus=$stmt->execute();
                if(!$executionStatus){
                    $returnMainArray["message"]="FAILURE";
                    $returnMainArray["Reason"]="Execution Failure";
                }else{
                    $noOfEffectedRows=$stmt->rowCount();
                    // if(($noOfEffectedRows/2)!=$totalInsertQueries){
                    //     $returnMainArray["message"]="FAILURE";
                    //     $returnMainArray["Reason"]="Error On Updating All Records, No Of Total Records=".$totalInsertQueries.", No Of Updated Records=".($noOfEffectedRows/2);// because on using ON Insert on With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row and 2 if an existing row is updated.  
                    // }else{
                    //     $returnMainArray["message"]="SUCCESS"; 
                    //     $returnMainArray["Reason"]="Succesfully Updated!";
                    // }
                    if(($noOfEffectedRows/2)==0){
                        $returnMainArray["message"]="FAILURE"; 
                        $returnMainArray["Reason"]="Details already Exist!";
                    }else{
                        $returnMainArray["message"]="SUCCESS"; 
                        $returnMainArray["Reason"]="Succesfully ".($noOfEffectedRows/2)." Records Updated!";
                    }
                }
            $this->db_Pdo->commit();
        }catch(PDOException $e){
            $returnMainArray["message"]="FAILURE";
            $returnMainArray["Reason"]="Internal Error 500!";
        }
        echo json_encode($returnMainArray);
    }
    
    private function getStatement($paramObj){
        $query="INSERT INTO `invoices`(`INVOICE_NUMBER`,`PAYMENT_RECEIVED_ON`,`PAYMENT_RECEIVED_VALUE`,`PAYMENT_REMARKS`, `REMAINING_BALANCE_IF_ANY`) VALUES";
        $lastLopIndex=count($paramObj)-1;
        forEach($paramObj as  $index=>$currentObj){
            if($index!=$lastLopIndex){
                $query.="('".$currentObj->{"InVoiceNo"}."','".$currentObj->{"Payment Date(yyyy-mm-dd)"}."','".$currentObj->{"Payment Value"}."','".$currentObj->{"Report Remarks"}."','".$currentObj->{"Balance"}."'),";
            }else{// last loop
                $query.="('".$currentObj->{"InVoiceNo"}."','".$currentObj->{"Payment Date(yyyy-mm-dd)"}."','".$currentObj->{"Payment Value"}."','".$currentObj->{"Report Remarks"}."','".$currentObj->{"Balance"}."') 
                         ON  DUPLICATE KEY UPDATE `PAYMENT_RECEIVED_ON`=VALUES(`PAYMENT_RECEIVED_ON`) , `PAYMENT_RECEIVED_VALUE`=VALUES(`PAYMENT_RECEIVED_VALUE`) ,
                        `PAYMENT_REMARKS`=VALUES(`PAYMENT_REMARKS`) , `REMAINING_BALANCE_IF_ANY`=VALUES(`REMAINING_BALANCE_IF_ANY`)";
            }
        }
        $stmt=$this->db_Pdo->prepare($query);
        return $stmt;
    }
}
?>