<?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->department==TL_DEPARTMENT){
            $this->TLDepart_queriesExecution($paramObj);
        }elseif($paramObj->department==FINANCE_DEPARTMENT){
            $this->FinanceDepart_queriesExecution($paramObj);
        }

    }
    
    function FinanceDepart_queriesExecution($paramObj){
        $returnResponseArray=array();
        $stmt1=$this->financeDept_updateSiteAllocationsDetails_statement($paramObj);
        $stmt2=$this->financeDept_insertIn_InvoiceDB_Details_statement($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 TLDepart_queriesExecution($paramObj){
        $returnResponseArray=array();
        $stmt=$this->tlDept_updateSiteAllocationsDetails_statement($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 financeDept_insertIn_InvoiceDB_Details_statement($paramObj){
        $stmt=$this->db->prepare("INSERT INTO `invoices` (`INVOICE_RISED_ON`,`INVOICE_NUMBER`,`INVOICE_VALUE`,`PAYMENT_RECEIVED_ON`,`PAYMENT_RECEIVED_VALUE`,`REMAINING_BALANCE_IF_ANY`,`PAYMENT_REMARKS`) VALUES(?, ?, ?, ?, ?, ?, ?) 
                                     ON DUPLICATE KEY UPDATE `INVOICE_RISED_ON`=?,`INVOICE_VALUE`=?,`PAYMENT_RECEIVED_ON`=?,`PAYMENT_RECEIVED_VALUE`=?,`REMAINING_BALANCE_IF_ANY`=?,`PAYMENT_REMARKS`=? ");
        $stmt->bind_param('ssdsddssdsdds', $paramObj->invoiceRaiseOn, $paramObj->invoiceNumber, $paramObj->invoiceValue, $paramObj->paymentReceivedOn, $paramObj->paymentReceivedValue, $paramObj->remainingBalance, $paramObj->paymentRemarks,
                                             $paramObj->invoiceRaiseOn, $paramObj->invoiceValue, $paramObj->paymentReceivedOn, $paramObj->paymentReceivedValue, $paramObj->remainingBalance, $paramObj->paymentRemarks);
        return $stmt;
    }
    
    function tlDept_updateSiteAllocationsDetails_statement($paramObj){
        // $stmt=$this->db->prepare("UPDATE `site_allocations` SET  `PO_NUMBER`=?, `REPORT_SUBMITED_TO_CUSTOMER_ON`=?, `REPORT_REMARK`=?  WHERE `SITE_ID`=? AND `SITE_ASSIGNED_DATE`=? AND `SITE_ASSIGNED_TO_EMPID`=?");
        $stmt=$this->db->prepare("UPDATE `tesf` SET  `PO_NUMBER`=?, `REPORT_SUBMITED_TO_CUSTOMER_ON`=?, `REPORT_REMARK`=?  WHERE `SITE_ID`=? AND `SITE_VISIT_DATE`=? AND `EMP_ID`=?");
        $stmt->bind_param('ssssss',$paramObj->poNumber, $paramObj->reportDate, $paramObj->reportRemarks, $paramObj->siteId, $paramObj->siteVisitDate, $paramObj->empId);
        return $stmt;
    }
    function financeDept_updateSiteAllocationsDetails_statement($paramObj){
        // $stmt=$this->db->prepare("UPDATE `site_allocations` SET  `INVOICE_NUMBER`=?, `INVOICE_REMARK`=? WHERE `SITE_ID`=? AND `SITE_ASSIGNED_DATE`=? AND `SITE_ASSIGNED_TO_EMPID`=?");
        $stmt=$this->db->prepare("UPDATE `tesf` SET  `INVOICE_NUMBER`=?, `INVOICE_REMARK`=? WHERE `SITE_ID`=? AND `SITE_VISIT_DATE`=? AND `EMP_ID`=?");
        $stmt->bind_param('sssss', $paramObj->invoiceNumber, $paramObj->invoiceRemarks, $paramObj->siteId, $paramObj->siteVisitDate, $paramObj->empId);
        return $stmt;   
    }
}



?>