<?php
if(isset($_POST['paramValues'])){
    $paramObj=json_decode($_POST['paramValues']);
    $clsObj=new duplicateInvoices();
    $clsObj->getInvoiceAlreadyExistInserver($paramObj);
}
class duplicateInvoices{
    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 getInvoiceAlreadyExistInserver($paramObj){
        $mainArray=array();
        $tempArray=array();
        $stmt=$this->getSelectStatement($paramObj->invoices);
        try{
            $this->db_Pdo->beginTransaction();
                $executionStatus=$stmt->execute();
            $this->db_Pdo->commit();
            if(!$executionStatus){
                $mainArray["message"]="FAILURE";
            }else{
                $mainArray["message"]="SUCCESS";
                $result=$stmt->get_result();
                while($data=$result->fetch_assoc()){
                    array_push($tempArray,$data);
                }
                $mainArray["responseData"]=$tempArray;
            }
        }
        catch(PDOException $e){
            $this->db_Pdo->rollback();
            $mainArray["message"]="FAILURE";
        }
        echo json_encode($mainArray);
    }
    function getSelectStatement($invoicesArray){
        $query="SELECT * FROM `invoices` WHERE `INVOICE_NUMBER` IN(";
        $lastIndex=count($invoicesArray)-1;
        forEach($invoicesArray as $index=>$val){
            if($index!=$lastIndex){
                $query.="'$val',";
            }else{
                $query.="'$val')";
            }
        }
        $stmt=$this->db->prepare($query);
        return $stmt;
    }
}
?>