<?php
$msg = '';
include("include/settings.php");
include("checksession.php");

function getCount($tablename, $status, $where_condition = null)
{
    global $mysqli;
    if ($where_condition != null) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where " . $status . " = 1 and " . $where_condition);
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where " . $status . " = 1");
    }

    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

function getCountLeads($tablename, $type, $e_id = 0)
{
    global $mysqli;

    if ($e_id == 0) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "'");
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and lead_allocated_to=" . $e_id);
    }
    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

function getCountLeadsByLeadStatus($tablename, $type, $status, $e_id = 0)
{
    global $mysqli;

    if ($e_id == 0) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and disposition = '" . $status . "'");
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and disposition = '" . $status . "' and lead_allocated_to=" . $e_id);
    }
    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}


function getCountMonthlyLeads($tablename, $type, $e_id = 0)
{
    global $mysqli;

    if ($e_id == 0) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and MONTH(lead_created_on)=MONTH(now()) and YEAR(lead_created_on)=YEAR(now())");
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and MONTH(lead_created_on)=MONTH(now()) and YEAR(lead_created_on)=YEAR(now()) and lead_allocated_to=" . $e_id);
    }
    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

function getCountMonthlyLeadsStatus($tablename, $type, $status, $e_id = 0)
{
    global $mysqli;

    if ($e_id == 0) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and disposition = '" . $status . "' and MONTH(lead_created_on)=MONTH(now()) and YEAR(lead_created_on)=YEAR(now())");
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and disposition = '" . $status . "' and MONTH(lead_created_on)=MONTH(now()) and YEAR(lead_created_on)=YEAR(now()) and lead_allocated_to=" . $e_id);
    }
    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

function getCountTodaysLeads($tablename, $type, $e_id = 0)
{
    global $mysqli;

    if ($e_id == 0) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and DATE(lead_created_on) = CURDATE() ");
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and DATE(lead_created_on) = CURDATE() and lead_allocated_to=" . $e_id);
    }
    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

function getCountTodaysLeadsStatus($tablename, $type, $status, $e_id = 0)
{
    global $mysqli;

    if ($e_id == 0) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and disposition = '" . $status . "' and DATE(lead_created_on) = CURDATE() ");
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and disposition = '" . $status . "' and DATE(lead_created_on) = CURDATE() and lead_allocated_to=" . $e_id);
    }
    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

function get_percentage($total, $leads)
{
    if ($total == 0) {
        return 0;
    }
    return number_format(($leads / $total) * 100, 2);
}

$ar_leads_data = array();
$ar_leads_data['new call'] = 'New Call';
$ar_leads_data['no match shared yet'] = 'No Match Shared yet';
$ar_leads_data['match shared'] = 'Match Shared';
$ar_leads_data['follow up'] = 'Follow up';
$ar_leads_data['close to conversion'] = 'Close to conversion';
$ar_leads_data['fully paid'] = 'Fully Paid';
$ar_leads_data['lead rejected'] = 'Lead Rejected';
$ar_leads_data['refund'] = 'Refund';
$ar_leads_data['fresh lead'] = 'Fresh Lead';
$ar_leads_data['renewal lead'] = 'Renewal Lead';
$ar_leads_data['ptential crm lead'] = 'Ptential CRM Lead';
$ar_leads_data['old leads'] = 'Old Leads';

$ar_alert_data = array('alert-info', 'alert-warning', 'alert-success', 'alert-danger');
?>
<!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="">
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
	
	<title>Pipeline - Rishta Depot</title>
	<?php include('./include/meta.php'); ?>
	<style>
		.table {
			display: block;
			overflow-x: auto;
			width: 100%;
		}
	</style>


</head>
<?php include('./include/header.php'); ?>


<!-- Main content -->

    <div class="content-wrapper">
        <div class="content-panel">
            <div class='container-fluid'>

                <div class='row'>
                    <div class='col-md-12'>
                        <h3>
                            Sales Executives Reports
                        </h3>
                    </div>
                </div>

                <div class='row'>
                  

                        <div class='col-md-4'>
                            <table class='table table-responsive'>
                                <tr>
                                    <td colspan='2' class='alert alert-myheading'>Total Rishta Leads</td>
                                </tr>
                                <tr class='alert alert-dark'>
                                <td style="color:white;"><strong>Total Leads!</strong></td>
                                <?php
                                $total_leads = getCountLeads('rishta_leadmaster', 'rishta_lead', $_SESSION['userId']);
                                $fullypaid= getCountLeadsByLeadStatus('rishta_leadmaster', 'rishta_lead', 'fully paid', $_SESSION['userId']);
                                ?>
                           <td style="color:white;"><?php echo $total_leads ?> Leads (Converted : <?php 
                           
                           $percentage = (($fullypaid * 100)/$total_leads);
                           
                           echo $percentage.'%';
                           ?>)
                           
                      </td>
                           
                            </tr>
                                
                                
                                
                                <?php
                                if (isset($ar_leads_data) && !empty($ar_leads_data)) {

                                    foreach ($ar_leads_data as $key => $value) {

                                        $alert_class = $ar_alert_data[rand(0, 3)];

                                        ?>
                                        <tr class='alert <?php echo $alert_class; ?>'>
                                            <td><strong><?php echo $value; ?></strong></td>
                                            
                                            <td>
                                                <a href="manage-rishta-leads.php?eid=<?php //echo $_SESSION['userId'] ?>&type=<?php //echo $key; ?>" class="alert-link">
                                                    <div><?php echo getCountLeadsByLeadStatus('rishta_leadmaster', 'rishta_lead', $key, $_SESSION['userId']) ?> Leads</div>
                                                </a>
                                            </td>
                                        </tr>
                                    <?php }
                                }
                                ?>
                            </table>
                        </div>

                        <div class='col-md-4'>
                            <table class='table table-responsive'>
                                <tr>
                                    <td colspan='2' class='alert alert-myheading'>Monthly Rishta Leads</td>
                                </tr>
                                <!--tr class='alert alert-dark'>
                                    <td><strong>This Month Leads!</strong></td>
                                    <!--?php
                                    $total_leads = getCountMonthlyLeads('rishta_leadmaster', 'rishta_lead', $_SESSION['userId']);
                                    ?>
                                    <td><!--?php echo $total_leads ?> Leads (Converted : <!--?php echo get_percentage($total_leads, (getCountMonthlyLeadsStatus('rishta_leadmaster', 'rishta_lead', 'deal', $_SESSION['userId']))) ?>%)</td>
                                </tr-->
                                
                                <tr class='alert alert-dark'>
                                <td style="color:white;"><strong>This Month Leads!</strong></td>
                                <?php
                                $total_leads = getCountMonthlyLeads('rishta_leadmaster', 'rishta_lead', $_SESSION['userId']);
                                // $total_leads = getCountLeads('rishta_leadmaster', 'rishta_lead');
                                $fullypaid= getCountMonthlyLeadsStatus('rishta_leadmaster', 'rishta_lead', 'fully paid', $_SESSION['userId']);
                                ?>
                           <td style="color:white;"><?php echo $total_leads ?> Leads (Converted : <?php 
                           
                           $percentage = (($fullypaid * 100)/$total_leads);
                           
                           echo $percentage.'%';
                           ?>)
                           
                      </td>
                           
                            </tr>
                                
                                <?php
                                if (isset($ar_leads_data) && !empty($ar_leads_data)) {

                                    foreach ($ar_leads_data as $key => $value) {

                                        $alert_class = $ar_alert_data[rand(0, 3)];

                                        ?>
                                        <tr class='alert <?php echo $alert_class; ?>'>
                                            <td><strong><?php echo $value; ?></strong></td>
                                            <td>
                                                <a href="manage-training-leads.php?eid=<?php echo $_SESSION['userId'] ?>&type=<?php echo $key; ?>" class="alert-link">
                                                    <div><?php echo getCountMonthlyLeadsStatus('rishta_leadmaster', 'rishta_lead', $key, $_SESSION['userId']) ?> Leads</div>
                                                </a>
                                            </td>
                                        </tr>
                                    <?php }
                                }
                                ?>
                            </table>
                        </div>

                        <div class='col-md-4'>
                            <table class='table table-responsive'>
                                <tr>
                                    <td colspan='2' class='alert alert-myheading'>Today Rishta Leads</td>
                                </tr>
                                <!--tr class='alert alert-dark'>
                                    <td><strong>Today Leads!</strong></td>
                                    <!--?php
                                    $total_leads = getCountTodaysLeads('rishta_leadmaster', 'rishta_lead', $_SESSION['userId']);
                                    ?>
                                    <td><!--?php echo $total_leads ?> Leads (Converted : <!--?php echo get_percentage($total_leads, (getCountTodaysLeadsStatus('rishta_leadmaster', 'rishta_lead', 'deal', $_SESSION['userId']))) ?>%)</td>
                                </tr-->
                                
                                
                                <tr class='alert alert-dark'>
                                <td style="color:white;"><strong>Today Leads!</strong></td>
                                <?php
                                $total_leads = getCountTodaysLeads('rishta_leadmaster', 'rishta_lead', $_SESSION['userId']);
                                // $total_leads = getCountLeads('rishta_leadmaster', 'rishta_lead');
                                $fullypaid= getCountTodaysLeadsStatus('rishta_leadmaster', 'rishta_lead', 'fully paid', $_SESSION['userId']);
                                ?>
                           <td style="color:white;"><?php echo $total_leads ?> Leads (Converted : <?php 
                           
                           $percentage = (($fullypaid * 100)/$total_leads);
                           
                           echo $percentage.'%';
                           ?>)
                           
                      </td>
                           
                            </tr>
                                
                                
                                <?php
                                if (isset($ar_leads_data) && !empty($ar_leads_data)) {

                                    foreach ($ar_leads_data as $key => $value) {

                                        $alert_class = $ar_alert_data[rand(0, 3)];

                                        ?>
                                        <tr class='alert <?php echo $alert_class; ?>'>
                                            <td><strong><?php echo $value; ?></strong></td>
                                            <td>
                                                <a href="manage-training-leads.php?eid=<?php echo $_SESSION['userId'] ?>&type=<?php echo $key; ?>" class="alert-link">
                                                    <div><?php echo getCountTodaysLeadsStatus('rishta_leadmaster', 'rishta_lead', $key, $_SESSION['userId']) ?> Leads</div>
                                                </a>
                                            </td>
                                        </tr>
                                    <?php }
                                }
                                ?>
                            </table>
                        </div>
                        <!--div class='col-md-6'>
								<table class='table table-responsive'>
									<tr>
										<td colspan='2' class='alert alert-myheading'>Project Leads</td>
									<tr>
									<tr class='alert alert-dark'>
										<td><strong>Total Leads!</strong></td>
										<?php
                        //	$total_leads=getCountLeads('cdi_leadmaster','project',$_SESSION['userId']);
                        ?>
										<td><?php //echo $total_leads ?> Leads (Converted : <?php //echo get_percentage($total_leads,(getCountLeadsByLeadStatus('cdi_leadmaster','project','deal',$_SESSION['userId']))) ?>%)</td>
									<tr>
									<tr class='alert alert-info'>
										<td><strong>Fresh</strong></td>
										<td><a href="manage-project-leads.php?eid=<?php //echo $_SESSION['userId'] ?>&type=fresh" class="alert-link"><div><?php // echo getCountLeadsByLeadStatus('cdi_leadmaster','project','fresh',$_SESSION['userId']) ?> Leads</div></a></td>
									<tr>
									<tr class='alert alert-warning'>
										<td><strong>Processing</strong></td>
										<td><a href="manage-project-leads.php?eid=<?php // echo $_SESSION['userId'] ?>&type=processing" class="alert-link"><div><?php // echo getCountLeadsByLeadStatus('cdi_leadmaster','project','processing',$_SESSION['userId']) ?> Leads</div></a></td>
									<tr>
									<tr class='alert alert-success'>
										<td><strong>Deal</strong></td>
										<td><a href="manage-project-leads.php?eid=<?php // echo $_SESSION['userId'] ?>&type=deal" class="alert-link"><div><?php // echo getCountLeadsByLeadStatus('cdi_leadmaster','project','deal',$_SESSION['userId']) ?> Leads</div></a></td>
									<tr>
									<tr class='alert alert-danger'>
										<td><strong>Lost</strong></td>
										<td><a href="manage-project-leads.php?eid=<?php // echo $_SESSION['userId'] ?>&type=lost" class="alert-link"><div><?php // echo getCountLeadsByLeadStatus('cdi_leadmaster','project','lost',$_SESSION['userId']) ?> Leads</div></a></td>
									<tr>
								</table>
							</div-->
                   
                </div>

            </div>
        </div>
    </div>




						<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
						<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
						<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>



						<!-- /.content-wrapper -->

						<?php include('./include/footer.php'); ?>