<?php
$msg = '';

include("include/settings.php");
include("checksession.php");

$and = 'and';
$condition = '';

$usermaster = $mysqli->prepare("select * from cdi_usermaster where user_type='sales_exec' order by user_creationdate desc");
$usermaster->execute();
$userresult = $usermaster->get_result();


//$query = "select count(*) from rishta_3cx_calls where calltype='Inbound'  ";
//$query = "SELECT rishta_3cx_calls.*, cdi_usermaster.extension3,cdi_usermaster.user_full_name FROM rishta_3cx_calls LEFT JOIN cdi_usermaster ON rishta_3cx_calls.agentextension=cdi_usermaster.extension3 where calltype='Inbound'  ";
//$userresulttotaldata = "SELECT cdi_usermaster.*, rishta_3cx_calls.agentextension,count(rishta_3cx_calls.calltype) FROM cdi_usermaster LEFT JOIN rishta_3cx_calls ON cdi_usermaster.extension3 = rishta_3cx_calls.agentextension where calltype='Inbound'  ";

$userresulttotaldata = "select count(*) from rishta_3cx_calls JOIN cdi_usermaster ON cdi_usermaster.extension3 = rishta_3cx_calls.agentextension where calltype='Inbound' order by id desc ";

$usermaster = $mysqli->prepare($userresulttotaldata);
$usermaster->execute();
$usermaster->bind_result($ins);
$usermaster->fetch();
$usermaster->close();

$userresulttotaldata = "select count(*) from rishta_3cx_calls JOIN cdi_usermaster ON cdi_usermaster.extension3 = rishta_3cx_calls.agentextension where calltype='Outbound' order by id desc ";

$usermaster = $mysqli->prepare($userresulttotaldata);
$usermaster->execute();
//$userresult = $usermaster->get_result();
$usermaster->bind_result($outs);
$usermaster->fetch();
$usermaster->close();

$duration='07:00';


//$usermaster = $mysqli->prepare("select *,(select count(*) from rishta_target where user_id = cdi_usermaster.user_id) as incount,(select count(*) from rishta_3cx_calls where agentextension = cdi_usermaster.extension3 AND calltype='Outbound') as outcount,(select count(contactnumber) from rishta_3cx_calls where agentextension = cdi_usermaster.extension3 AND calltype='Outbound' OR calltype='Inbound') as freash
//from cdi_usermaster order by user_id desc");
//$usermaster->execute();
//$userresult = $usermaster->get_result();


$salesmaster = $mysqli->prepare("select * from cdi_usermaster where user_type='sales_exec' order by user_creationdate desc");
$salesmaster->execute();
$salesresult = $salesmaster->get_result();

$usermaster = $mysqli->prepare("select *,(select count(*) from rishta_leadmaster where 	lead_allocated_to = rishta_target.user_id and MONTH(lead_created)=01 and YEAR(lead_created)=YEAR(now())) as leadcount
,(select count(*) from rishta_leadmaster where 	lead_allocated_to = rishta_target.user_id and 	disposition='fully paid' and MONTH(lead_created)=01 and YEAR(lead_created)=YEAR(now())) as paidcount
,(select count(*) from rishta_leadmaster where 	lead_allocated_to = rishta_target.user_id and 	disposition!='fully paid' and MONTH(lead_created)=01 and YEAR(lead_created)=YEAR(now())) as pencount

from rishta_target where month =08 and YEAR=YEAR(now())");
$usermaster->execute();
$userresult = $usermaster->get_result();



?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Agents August Report - Rishta Depot</title>
  
	
<?php include('./include/meta.php'); ?>

</head>
	
<?php include('./include/header.php'); ?>
  
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
	  <div class="container-full">
		<!-- Content Header (Page header) -->
		<div class="content-header">
			<div class="d-flex align-items-center">
				<div class="me-auto">
					<h4 class="page-title">Reports</h4>
					<div class="d-inline-block align-items-center">
						<nav>
							<ol class="breadcrumb">
								<li class="breadcrumb-item"><a href="#"><i class="mdi mdi-home-outline"></i></a></li>
								<li class="breadcrumb-item" aria-current="page">Current Year August Report</li>
								<li class="breadcrumb-item active" aria-current="page">Reports</li>
							</ol>
						</nav>
					</div>
				</div>
				
			</div>
		</div>

		<!-- Main content -->
		<section class="content">
		  <div class="row">
			<div class="col-12">

			 <div class="box">
				<div class="box-header with-border">
				  <h3 class="box-title">Current Year August Report</h3>
				</div>
				<form class="form">
					<div class="box-body">
					<div class="box-footer" style="border-top: 0px;padding-top: 0px;margin-top: -10px;margin-left: -20px;">
							<button type="button" class="btn btn-warning me-1"><a href="all_agent_report.php">Back</a></button>
						</div>
					</div>
				</form>
				<!-- /.box-header -->
				<div class="box-body" id="ajaxcoursedata" style="margin-top: -40px;">
					<div class="table-responsive">
					    <?php
										if(mysqli_num_rows($userresult)>0)
										{
											?>
					  <table id="example1" class="table table-bordered table-striped">
						<thead>
							<tr>
								<th>Sr.No</th>
                                <th>Name</th>
                                <th>Target</th>
                                <th>Total Leads</th>
                                <th>Achievements</th>
                                <th>Pending</th>
                                <th>Percentage</th>
							</tr>
						</thead>
						<tbody>
											        <?php
													$i=0;
													while($userrow=mysqli_fetch_object($userresult))
													{
														$i++;
														?>
													
															<tr class="text-center">
																<td><?php echo $i; ?></td>
															
																	    <td><a href="dialers-report.php?userid=<?php echo $userrow->user_id ?>"><?php
																	    
																	    $user= $userrow->user_id;
																	    
																	    $salesmaster = $mysqli->prepare("select * from cdi_usermaster where user_id=' $user'");
                                                                        $salesmaster->execute();
                                                                        $salesresult = $salesmaster->get_result();
                                                                        
                                                                        $salerow=mysqli_fetch_object($salesresult);
																	    
																	   
																	  echo  $salerow->	user_full_name;
																	    
																	    ?></a></td>
												<td class="text-left"><?php echo $userrow-> target ?></td>
															
															
																	<td class="text-left"><?php echo  $userrow-> leadcount ?></td>
																		<td class="text-left"><?php echo  $userrow-> paidcount ?></td>
																			<td class="text-left"><?php echo  $userrow-> pencount ?></td>
																				<td class="text-left"><?php 
																				$total_leads=  $userrow-> leadcount;
																				$total_achievements=  $userrow-> paidcount;
																			  $percentage = 0;
                                        if(!empty($total_leads)) {

                                            if(!empty($total_achievements)) {

                                                $percentage = (($total_achievements * 100)/$total_leads);
                                            }
                                        }
                                        $percentage = number_format($percentage, 2, '.', '');
                                        echo $percentage ; 
                                        ?>%
																				
																				
																				</td>
																
																
																
															</tr>
																<?php
														}
													?>
												</tbody>
					
					  </table>
					  		<?php
										}
									?>
					</div>
				</div>
				<!-- /.box-body -->
			  </div>
			  <!-- /.box -->


			  <!-- /.box -->          
			</div>
			<!-- /.col -->
		  </div>
		  <!-- /.row -->
		</section>
		<!-- /.content -->
	  
	  </div>
  </div>
  <!-- /.content-wrapper -->
  
<script>
    function getLeads() {

        var txtFromDate = $('#txtFromDate').val();
        var txtToDate = $('#txtToDate').val();
        var txtSearch = $('#txtSearch').val();

        var ddlUserId = $('#ddlUserId').val();

    


        $('#loading').css('display', '');
        $.ajax({
            url: 'all_agents_ajax_reports.php',
            type: 'post',
            data: {
                ddlUserId: ddlUserId,

            
                txtFromDate: txtFromDate,
                txtToDate: txtToDate,
                txtSearch: txtSearch,
      
            },
            datatype: 'html',
            success: function (rsp) {
                $('#ajaxcoursedata').html(rsp);
                $('#loading').css('display', 'none');
            }
        });
    }
    
    
 function getLeadsPagination(page_skip) {
        var txtFromDate = $('#txtFromDate').val();
        var txtToDate = $('#txtToDate').val();
        var txtSearch = $('#txtSearch').val(); 
       
        var ddlUserId = $('#ddlUserId').val();

        $('#loading').css('display', '');
        $.ajax({
            url: 'all_agents_ajax_reports.php',
            type: 'post',
            data: {"ddlUserId": ddlUserId, "txtFromDate": txtFromDate, "txtToDate": txtToDate, "txtSearch": txtSearch, "page_skip": page_skip},
            datatype: 'html',
            success: function (rsp) {
                $('#ajaxcoursedata').html(rsp);
                $('#loading').css('display', 'none');
            }
        });
    }


    function changeLeadAllocation(executive_id, lead_id) {
        $('#loading').css('display', '');
        $.ajax({
            url: 'allocate_lead.php',
            type: 'post',
            data: {"executive_id": executive_id, "lead_id": lead_id},
            datatype: 'html',
            success: function (rsp) {
                $('#loading').css('display', 'none');
            }
        });
    }

  

    $(document).ready(function () {
        $("#txtFromDate,#txtToDate").datepicker({
            dateFormat: 'yy-mm-dd',
		//	dateFormat: 'mm-dd-yy'
            // minDate: 0, // 0 days offset = today
        });
    });
</script>	
<?php include('./include/footer.php'); ?>