<?php
session_start();
if(isset($_POST['paramValues'])){
    $paramObj=json_decode($_POST['paramValues']);
    $paramObj->managerId=$_SESSION['userId'];
    $paramObj->department=$_SESSION['teamLeads'];
    
    $clsObj=new PendingReportSubmitions();
    $clsObj->getPendingReportSubmitionDetails($paramObj);
    
}else{
    echo "Result Not Found";
}
class PendingReportSubmitions{
    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 getPendingReportSubmitionDetails($paramObj){
        $mainArray=array();
        $tempArray=array();
        switch($paramObj->searchBy){
            case 'date':
                $stmt=$this->getPendingReportSubmitionsBySiteVisitDate($paramObj);
                break;
            case 'siteId':
                $stmt=$this->getPendingReportSubmitionsBySiteID($paramObj);
                break;
            case 'PONo':
                $stmt=$this->getPendingReportSubmitionsByPONo($paramObj);
                break;
        }
        
        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["responseDataArray"]=$tempArray;
        }
        catch(PDOException $e){
            $this->db_Pdo->rollback();
            $mainArray["message"]="FAILURE";
        }
        echo json_encode($mainArray);
    }
    function getPendingReportSubmitionsBySiteVisitDate($paramObj){
        if($paramObj->department==FINANCE_DEPARTMENT){
            $stmt=$this->getFinanceDepartment_SiteVisitDateQueryStmt($paramObj->monthName, $paramObj->year);
        }elseif($paramObj->department==TL_DEPARTMENT){
            $stmt=$this->getTLDepertment_SiteVisitDateQueryStmt($paramObj->managerId, $paramObj->monthName, $paramObj->year);
        }
        return $stmt;
    }
    function getPendingReportSubmitionsBySiteID($paramObj){
        if($paramObj->department==FINANCE_DEPARTMENT){
            $stmt=$this->getFinanceDepartment_SiteIdQueryStmt($paramObj->siteId);
        }elseif($paramObj->department==TL_DEPARTMENT){
            $stmt=$this->getTLDepartment_SiteIdQueryStmt($paramObj->managerId, $paramObj->siteId);
        }
        return $stmt;
    }
    function getPendingReportSubmitionsByPONo($paramObj){
        if($paramObj->department==FINANCE_DEPARTMENT){
            $stmt=$this->getFinanceDepartment_PONoQueryStmt($paramObj->PONo);
        }elseif($paramObj->department==TL_DEPARTMENT){
            $stmt=$this->getTLDepartment_PONoQueryStmt($paramObj->managerId, $paramObj->PONo);
        }
        return $stmt;
    }
    // -----------------------------------------------------------------------------------------------------------------
    function getFinanceDepartment_SiteVisitDateQueryStmt($monthName, $year){
        if($monthName !="All"){
            $stmt=$this->db->prepare("SELECT * FROM `site_allocations` WHERE MONTHNAME(`SITE_ASSIGNED_DATE`)=? AND YEAR(`SITE_ASSIGNED_DATE`)=? AND `SITE_VISIT_STATUS`='".SITE_VISITED."' AND (`REPORT_SUBMITED_TO_CUSTOMER_ON` IS NULL || `REPORT_SUBMITED_TO_CUSTOMER_ON`='0000-00-00')");
            $stmt->bind_param('ss',$monthName, $year);
        }else{
            $stmt=$this->db->prepare("SELECT * FROM `site_allocations` WHERE YEAR(`SITE_ASSIGNED_DATE`)=? AND `SITE_VISIT_STATUS`='".SITE_VISITED."' AND (`REPORT_SUBMITED_TO_CUSTOMER_ON` IS NULL || `REPORT_SUBMITED_TO_CUSTOMER_ON`='0000-00-00')");
            $stmt->bind_param('s',$year);
        }
        return $stmt;
    }
    function getTLDepertment_SiteVisitDateQueryStmt($managerId, $monthName, $year){
        if($monthName !="All"){
            $stmt=$this->db->prepare("SELECT * FROM `site_allocations` WHERE `MANAGER_ID`=?  AND MONTHNAME(`SITE_ASSIGNED_DATE`)=? AND YEAR(`SITE_ASSIGNED_DATE`)=? AND `SITE_VISIT_STATUS`='".SITE_VISITED."' AND (`REPORT_SUBMITED_TO_CUSTOMER_ON` IS NULL || `REPORT_SUBMITED_TO_CUSTOMER_ON`='0000-00-00')");
            $stmt->bind_param('sss',$managerId, $monthName, $year);
        }else{
            $stmt=$this->db->prepare("SELECT * FROM `site_allocations` WHERE `MANAGER_ID`=?  AND YEAR(`SITE_ASSIGNED_DATE`)=? AND `SITE_VISIT_STATUS`='".SITE_VISITED."'  AND (`REPORT_SUBMITED_TO_CUSTOMER_ON` IS NULL || `REPORT_SUBMITED_TO_CUSTOMER_ON`='0000-00-00')");
            $stmt->bind_param('ss',$managerId, $year);
        }
        return $stmt;  
    }
    function getFinanceDepartment_SiteIdQueryStmt($siteId){
        $stmt=$this->db->prepare("SELECT * FROM `site_allocations` WHERE `SITE_ID`=? AND `SITE_VISIT_STATUS`='".SITE_VISITED."' AND (`REPORT_SUBMITED_TO_CUSTOMER_ON` IS NULL || `REPORT_SUBMITED_TO_CUSTOMER_ON`='0000-00-00')");
        $stmt->bind_param('s',$siteId);
        return $stmt;
    }
    function getTLDepartment_SiteIdQueryStmt($managerId, $siteId){
        $stmt=$this->db->prepare("SELECT * FROM `site_allocations` WHERE `MANAGER_ID`=? AND `SITE_ID`=? AND `SITE_VISIT_STATUS`='".SITE_VISITED."' AND (`REPORT_SUBMITED_TO_CUSTOMER_ON` IS NULL || `REPORT_SUBMITED_TO_CUSTOMER_ON`='0000-00-00')");
        $stmt->bind_param('ss',$managerId, $siteId);
        return $stmt;        
    }
    function getFinanceDepartment_PONoQueryStmt($poNo){
        $stmt=$this->db->prepare("SELECT * FROM `site_allocations` WHERE `PO_NUMBER`=? AND `SITE_VISIT_STATUS`='".SITE_VISITED."' AND (`REPORT_SUBMITED_TO_CUSTOMER_ON` IS NULL || `REPORT_SUBMITED_TO_CUSTOMER_ON`='0000-00-00')");
        $stmt->bind_param('s',$poNo);
        return $stmt;
    }
    function getTLDepartment_PONoQueryStmt($managerId, $poNo){
        $stmt=$this->db->prepare("SELECT * FROM `site_allocations` WHERE `MANAGER_ID`=? AND `PO_NUMBER`=? AND `SITE_VISIT_STATUS`='".SITE_VISITED."' AND (`REPORT_SUBMITED_TO_CUSTOMER_ON` IS NULL || `REPORT_SUBMITED_TO_CUSTOMER_ON`='0000-00-00')");
        $stmt->bind_param('ss',$managerId, $poNo);
        return $stmt;        
    }
    
}
?>