<?php
session_start();
if(isset($_POST['paramValues'])){
    $paramObj=json_decode($_POST['paramValues']);
    $paramObj->userId=$_SESSION['userId'];
    $paramObj->userDept=$_SESSION['teamLeads'];
    $obj=new updateInvoiceReports();
    $obj->updateInvoiceReportsDetails($paramObj);
    
}else{
    echo "No Data found";
}
class updateInvoiceReports{
    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 updateInvoiceReportsDetails($paramObj){
        if($paramObj->updateInAllocationDB){
            $this->multiQueriesExecution($paramObj);
        }else{
            $this->singleQueryExecution($paramObj);
        }
    }
    
    function multiQueriesExecution($paramObj){
        $returnResponseArray=array();
        $stmt1=$this->update_siteAllocationDB_DetailsQuery($paramObj);
        $stmt2=$this->insertIn_InvoiceDB_DetailsQuery($paramObj);
        try{
            $this->db_Pdo->beginTransaction();
                $stmt1ExecutionStatus=$stmt1->execute();
                $stmt2ExecutionStatus=$stmt2->execute();
            $this->db_Pdo->commit();
            if($stmt1ExecutionStatus && $stmt2ExecutionStatus){
                $returnResponseArray['message']='SUCCESS';
            }else{
                $returnResponseArray['message']='FAILURE';
            }
            
        }catch(PDOExecution $e){
            $this->db_Pdo->rollback();
            $returnResponseArray['message']='FAILURE';
        }
         echo json_encode($returnResponseArray); 
    }
    
    function singleQueryExecution($paramObj){
        $returnResponseArray=array();
        $stmt=$this->insertIn_InvoiceDB_DetailsQuery($paramObj);
        try{
            $this->db_Pdo->beginTransaction();
                $queryExecutionStatus=$stmt->execute();
            $this->db_Pdo->commit();
            if(!$queryExecutionStatus){
                $returnResponseArray['message']='FAILURE';
            }else{
                $returnResponseArray['message']='SUCCESS';
            }
        }catch(PDOException $e){
            $this->db_Pdo->rollback();  
            $returnResponseArray['message']='FAILURE';
        }
        echo json_encode($returnResponseArray);
    }
    function insertIn_InvoiceDB_DetailsQuery($paramObj){
        $stmt=$this->db->prepare("INSERT INTO `invoices` (`REPORT_SUBMITED_TO_CUSTOMER_ON`,`REPORT_REMARK`,`INVOICE_RISED_ON`,`INVOICE_NUMBER`,`INVOICE_VALUE`,`PAYMENT_RECEIVED_ON`,`PAYMENT_RECEIVED_VALUE`,`REMAINING_BALANCE_IF_ANY`,`PAYMENT_REMARKS`) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?) 
                                     ON DUPLICATE KEY UPDATE `REPORT_SUBMITED_TO_CUSTOMER_ON`=?,`REPORT_REMARK`=?,`INVOICE_RISED_ON`=?,`INVOICE_VALUE`=?,`PAYMENT_RECEIVED_ON`=?,`PAYMENT_RECEIVED_VALUE`=?,`REMAINING_BALANCE_IF_ANY`=?,`PAYMENT_REMARKS`=? ");
        $stmt->bind_param('ssssdsddssssdsdds', $paramObj->reportDate,$paramObj->reportRemarks,$paramObj->invoiceRaiseOn, $paramObj->invoiceNumber, $paramObj->invoiceValue, $paramObj->paymentReceivedOn, $paramObj->paymentReceivedValue, $paramObj->remainingBalance, $paramObj->paymentRemarks,
                                              $paramObj->reportDate,$paramObj->reportRemarks,$paramObj->invoiceRaiseOn, $paramObj->invoiceValue, $paramObj->paymentReceivedOn, $paramObj->paymentReceivedValue, $paramObj->remainingBalance, $paramObj->paymentRemarks);
        return $stmt;
    }
    
    function update_siteAllocationDB_DetailsQuery($paramObj){
        $stmt=$this->db->prepare("UPDATE `site_allocations` SET  `PO_NUMBER`=?, `INVOICE_NUMBER`=? WHERE `SITE_ID`=? AND `SITE_ASSIGNED_DATE`=? AND `SITE_ASSIGNED_TO_EMPID`=?");
        $stmt->bind_param('sssss',$paramObj->poNumber, $paramObj->invoiceNumber, $paramObj->siteId, $paramObj->siteVisitDate, $paramObj->empId);
        return $stmt;
    }
}



?>