    <?php
    header("Content-Type:application/json");
    include('../include/main.function.php');
    if($_GET['type']=='stats'){
        $start_date = $_GET['start_date'];
        $end_date = $_GET['end_date'];
        $team = $_GET['team'];
        
        if($team=='DS'){
            $_SESSION['payqucount'] = 0;
            $team = 'D';
        }
        
        if($team=='D'){
            $result = $db->qu->query("SELECT  p.name , p.ondate , p.team , p.amount , p.exchange_amount ,p.added_by, w.id  as wid , w.url as url , w.team , w.status , p.payment_type,p.utilized FROM `crm_payments` as p INNER JOIN websites as w on w.id = p.website_id WHERE p.display_id='1' and date(p.ondate) between '$start_date' and '$end_date'");
        }
        else{
            $result = $db->qu->query("SELECT  p.name , p.ondate , p.team , p.amount , p.exchange_amount ,p.added_by, w.id  as wid , w.url  as url , w.team , w.status ,  p.payment_type,p.utilized FROM `crm_payments` as p INNER JOIN websites as w on w.id = p.website_id WHERE p.display_id='1' and date(p.ondate) between '$start_date' and '$end_date' and p.team='$team'");
        }
            
        if(isset($_GET['count'])){
            $check = (isset($_SESSION['payqucount'])) ? $_SESSION['payqucount'] : 0 ; 
            
            $empRecord = mysqli_num_rows($result);
            if($empRecord == $check){
               $_SESSION['payqucount'] = $empRecord;     
               echo json_encode('not');
                die();
            }
            
              
        }
    
        $empData = array();
        while( $empRecord = mysqli_fetch_assoc($result) ) {
        	$empData[] = $empRecord;
    
        }
            $_SESSION['payqujson'] = '';
            $qujson  = json_encode($empData);
            echo $_SESSION['payqujson']  = $qujson;
            die();
            
    }else if($_GET['type']=='invoice'){
        
        $start_date = $_GET['start_date'];
        $end_date = $_GET['end_date'];
        $team = $_GET['team'];
        if($team=='D'){
        $result = $db->query("SELECT IF(p.currency = 'USD' , p.amount , p.amount * p.exchange_rate) as examunt , p.*   FROM `crm_payments`  as p WHERE date(ondate) between '$start_date' and '$end_date'");
        }
        else{
        $result = $db->query("SELECT * FROM `invoice` WHERE delete_id='1' and date(ondate) between '$start_date' and '$end_date' and team='$team' group by invoice_no");
        }
        
        if(isset($_GET['count'])){
            $check = (isset($_SESSION['invqucount'])) ? $_SESSION['invqucount'] : 0 ; 
            
            $empRecord = mysqli_num_rows($result);
            if($empRecord == $check){
               $_SESSION['invqucount'] = $empRecord;     
               echo json_encode('not');
                die();
            }
            
              
        }
        
        
        $empData = array();
        while( $empRecord = mysqli_fetch_assoc($result) ) {
        	$empData[] = $empRecord;
    
        }
            $_SESSION['invqujson'] = '';
            $qujson  = json_encode($empData);
            echo $_SESSION['invqujson']  = $qujson;
            die();
            
    }
    else if($_GET['type']=='spending'){
        $start_date = $_GET['start_date'];
        $end_date = $_GET['end_date'];
        $team = $_GET['team'];
        if($team=='D'){
        $result = $db->query("SELECT * FROM `ppc_spending` WHERE status='1' and date(date_spending) between '$start_date' and '$end_date'");
        }
        else{
        $result = $db->qu->query("SELECT * FROM `ppc_spending` WHERE status='1' and date(date_spending) between '$start_date' and '$end_date' and team='$team'");
        }
        $empData = array();
        while( $empRecord = mysqli_fetch_assoc($result) ) {
        	$empData[] = $empRecord;
        }
        response($empData);
    
    }
    else if($_GET['type']=='refund'){
        $start_date = $_GET['start_date'];
        $end_date = $_GET['end_date'];
        $team = $_GET['team'];
        if($team=='D'){
        $result = $db->qu->query("SELECT * FROM `refund_table` WHERE status='1' and date(ondate) between '$start_date' and '$end_date'");
        }
        else{
        $result = $db->qu->query("SELECT * FROM `refund_table` WHERE status='1' and date(ondate) between '$start_date' and '$end_date' and team='$team'");
        }
        $empData = array();
        while( $empRecord = mysqli_fetch_assoc($result) ) {
        	$empData[] = $empRecord;
        	
        }
        //response($empData);
    }
    else if($_GET['type']=='charge_back'){
        $start_date = $_GET['start_date'];
        $end_date = $_GET['end_date'];
        $team = $_GET['team'];
        if($team=='D'){
        $result = $db->qu->query("SELECT * FROM `charge_back_table` WHERE status='1' and date(ondate) between '$start_date' and '$end_date' and status='1'");
        }
        else{
        $result = $db->qu->query("SELECT * FROM `charge_back_table` WHERE status='1' and date(ondate) between '$start_date' and '$end_date' and team='$team' and status='1'");
        }
        $empData = array();
        while( $empRecord = mysqli_fetch_assoc($result) ) {
        	$empData[] = $empRecord;
        	
        }
        //response($empData);
    }
    else if($_GET['type']=='charge_back_won'){
        $start_date = $_GET['start_date'];
        $end_date = $_GET['end_date'];
        $team = $_GET['team'];
        if($team=='D'){
        $result = $db->qu->query("SELECT * FROM `charge_back_won_table` WHERE status='1' and date(ondate) between '$start_date' and '$end_date' and status='1'");
        }
        else{
        $result = $db->qu->query("SELECT * FROM `charge_back_won_table` WHERE status='1' and date(ondate) between '$start_date' and '$end_date' and team='$team' and status='1'");
        }
        $empData = array();
        while( $empRecord = mysqli_fetch_assoc($result) ) {
        	$empData[] = $empRecord;
        	
        }
        //response($empData);
    }
    else if($_GET['type']=='target'){
        $start_date = $_GET['start_date'];
        $end_date = $_GET['end_date'];
        $team = $_GET['team'];
        if($team=='D'){
        $result = $db->qu->query("SELECT * FROM `sales_target` WHERE month= '$start_date'");
        }
        else{
        $result = $db->qu->query("SELECT * FROM `sales_target` WHERE month= '$start_date' and team='$team'");
        }
        $empData = array();
        while( $empRecord = mysqli_fetch_assoc($result) ) {
        	$empData[] = $empRecord;
        	
        }
        //response($empData);
    }
    else if($_GET['type']=='close_target'){
        $start_date = $_GET['start_date'];
        $end_date = $_GET['end_date'];
        $team = $_GET['team'];
        if($team=='D'){
        $result = $db->qu->query("SELECT * FROM `sales_target` WHERE month between '$start_date' and '$end_date'");
        }
        else{
        $result = $db->qu->query("SELECT * FROM `sales_target` WHERE month between '$start_date' and '$end_date' and team='$team'");
        }
        $empData = array();
        while( $empRecord = mysqli_fetch_assoc($result) ) {
        	$empData[] = $empRecord;
        	
        }
        //response($empData);
    }
    else if($_GET['type']=='associate'){
        $start_date = $_GET['start_date'];
        $end_date = $_GET['end_date'];
        $team = $_GET['team'];
        if($team=='DS'){
            $_SESSION['payqucount'] = 0;
            $team = 'D';
        }
        if($team=='D'){
        $result = $db->qu->query("SELECT * FROM `associate_payments` WHERE display_id='1' and date(ondate) between '$start_date' and '$end_date'");
        }
        else{
        $result = $db->qu->query("SELECT * FROM `associate_payments` WHERE display_id='1' and date(ondate) between '$start_date' and '$end_date' and team='$team'");
        }
        $empData = array();
        while( $empRecord = mysqli_fetch_assoc($result) ) {
        	$empData[] = $empRecord;
        	
        }
        echo $qujson  = json_encode($empData);
        die();
        //response($empData);
    }
    else{
        $empData[] = array(NULL, NULL, 400,"id"=>"Invalid Request");
        //response($empData);
    }
        
    ?>