<?PHP
ini_set('max_execution_time', '1200'); //300 seconds = 5 minutes
ob_start();
session_start();
include 'connect.php';
if(isset($_SESSION['adminemail']))
{
$user=$_SESSION['adminemail'];

$gtp=mysqli_query($conn,"select * from admin where username='$user'");
$gtp1=mysqli_fetch_assoc($gtp);

include 'top.php';
?>


<div id="page-wrapper" >
          <div id="page-inner">
<div class="main-content">
				<div class="main-content-inner">
					<div class="breadcrumbs ace-save-state" id="breadcrumbs">
						<ul class="breadcrumb">
							<li>
								<i class="ace-icon fa fa-home home-icon"></i>
								<a href="#">Home</a>
							</li>
							<li class="active">Executive Summary</li>
						</ul><!-- /.breadcrumb -->
					</div>

					<div class="page-content">
			

						<div class="page-header">
                        
<?php

$aca=mysqli_query($conn,"select * from financial where user='$user' order by ID desc");
$aca1=mysqli_fetch_assoc($aca);

$financial=$aca1['financial'];

?>                        
                        
							<table style="width:100%;">
                        
                        <tr><td style="width:50%;">
                        
							<h1>
								Executive Summary
							</h1>
                            
                            </td>
                            
                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">
                            
                            <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>
                            
                            </td></tr></table>
                            
						</div><!-- /.page-header -->
<br />

						<div class="row">
							<div class="col-xs-12">
                            
								<!-- PAGE CONTENT BEGINS -->
                                
<script src="assets/js/jquery.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css">

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
<script>
  $(function() {
  // Initialize the datepicker for the "From Date" input
  $("#from-datepicker").datepicker({
    format: "mm/dd/yyyy",
    autoclose: true,
    todayHighlight: true
  }).on("show", function(e) {
    var inputOffset = $(this).offset();
    var inputHeight = $(this).outerHeight();
    var datepickerDropdown = $(".datepicker-dropdown");

    datepickerDropdown.css({
      top: inputOffset.top + inputHeight + 10,
      left: inputOffset.left
    });
  });

  // Initialize the datepicker for the "To Date" input
  $("#to-datepicker").datepicker({
    format: "mm/dd/yyyy",
    autoclose: true,
    todayHighlight: true
  }).on("show", function(e) {
    var inputOffset = $(this).offset();
    var inputHeight = $(this).outerHeight();
    var datepickerDropdown = $(".datepicker-dropdown");

    datepickerDropdown.css({
      top: inputOffset.top + inputHeight + 10,
      left: inputOffset.left
    });
  });
});
</script>
<script>
	
jQuery(function($){
		   
	// simple jQuery validation script
	$('#work').submit(function(){
		
		var valid = true;
		var errormsg = 'This field is required!';
		var errorcn = 'error';
		
		$('.' + errorcn, this).remove();			
		
		$('#required', this).each(function(){
			var parent = $(this).parent();
			if( $(this).val() == '' ){
				var msg = $(this).attr('title');
				msg = (msg != '') ? msg : errormsg;
				$('<span style="font-size:13px;color:#FF0000; padding-left:3px;" class="'+ errorcn +'">'+ msg +'</span>')
					.appendTo(parent)
					.fadeIn('fast')
					.click(function(){ $(this).remove(); });
				valid = false;
			};
		});
		
		return valid;
	});
	
});	

</script>

<?php 

if(isset($_GET['invoice']))
{
	$invoice=$_GET['invoice'];
	$executive=$_GET['executive'];
	$fromdate=$_GET['fromdate'];
	$todate=$_GET['todate'];
	
	
	$frmdate=date('Y-m-d',strtotime($fromdate));
	$toddate=date('Y-m-d',strtotime($todate));
	
	$emp=mysqli_query($conn,"select * from employee where ID='$executive'");
	$emp1=mysqli_fetch_assoc($emp);
	
	echo"<center><table style='line-height:25px;'>

	<tr><td>Period : <b>".$fromdate." - ".$todate."</b></td></tr>
	<tr><td>Executive : <b>".$emp1['name']."</b></td></tr>

	</table></center>";
	
	
	echo'<table id="dynamic-table" class="table table-striped table-bordered table-hover">';

	echo"<thead><tr><th width='9%'>Invoice No</th><th width='11%'>Date</th>

	<th width='35%'>Customer</th><th width='15%'>Mobile</th><th width='17%'>Amount</th>

	<th style='width:11%;'>Edit</th></tr></thead>

	<tbody>";

	$select="select *,sum(total) as amount from invoice where date between '$frmdate' and '$toddate' and executive='$executive' group by invoiceno";
	$select1=mysqli_query($conn,$select);
	$sno=0;
	$stl=0;
	while($value=mysqli_fetch_assoc($select1))
	{
	$sno=$sno+1;

	$cust=mysqli_query($conn,"select * from customer where ID='$value[customer]'");
	$cust1=mysqli_fetch_assoc($cust);

	echo"<tr><td>".$value['invno'].".</td><td><span style='display:none;'>".$value['date']."</span> ".date('d-m-Y',strtotime($value['date']))."</td>
	<td>".$cust1['name']."</td><td>".$cust1['mobile1']."</td><td style='text-align:right;'>".number_format($value['amount'],2,'.','')."</td><td>

														   <div class='hidden-sm hidden-xs btn-group'>
																<a href='viewinvoice.php?id=".$value['invoiceno']."' title='View'>
																	<i class='ace-icon fa fa-search-plus bigger-100 blue'></i></a> &nbsp;
																	
															<a href='editinvoice.php?id=".$value['invoiceno']."' title='Edit'>
																	<i class='ace-icon fa fa-pencil bigger-100 green'></i></a> &nbsp;
																	
															<a href='del.php?id=".$value['invoiceno']."&desc=invoice' title='Delete'>
															<i class='ace-icon fa fa-trash-o bigger-100 red'></i></a></div>

	</td></tr>";

	$stl=$stl+$value['amount'];

	}

	echo"</tbody></table>";
	
	echo"<center><b>Total Rs : ".number_format($stl,2,'.','')."</b><br /><br />
	
	<a href='javascript:history.back(1);'>Back</a></center><br />";

}

elseif(isset($_GET['order']))
{

	$order=$_GET['order'];
	$executive=$_GET['executive'];
	$fromdate=$_GET['fromdate'];
	$todate=$_GET['todate'];
	
	$frmdate=date('Y-m-d',strtotime($fromdate));
	$toddate=date('Y-m-d',strtotime($todate));
	
	$emp=mysqli_query($conn,"select * from employee where ID='$executive'");
	$emp1=mysqli_fetch_assoc($emp);

	echo"<center><table style='line-height:25px;'>

	<tr><td>Period : <b>".$fromdate." - ".$todate."</b></td></tr>
	<tr><td>Executive : <b>".$emp1['name']."</b></td></tr>

	</table></center>";
	
	
	echo'<table id="dynamic-table" class="table table-striped table-bordered table-hover">';

	echo"<thead><tr><th width='9%'>Order No</th><th width='11%'>Date</th>
	<th width='35%'>Customer</th><th width='15%'>Mobile</th><th width='17%'>Amount</th>
	<th style='width:11%;'>Edit</th></tr></thead>
	<tbody>";
	
	$select="select *,sum(total) as amount from sales_order where date between '$frmdate' and '$toddate' and executive='$executive' group by orderno";
	$select1=mysqli_query($conn,$select);
	$sno=0;
	$stl=0;
	while($value=mysqli_fetch_assoc($select1))
	{
	$sno=$sno+1;

	$cust=mysqli_query($conn,"select * from customer where ID='$value[customer]'");
	$cust1=mysqli_fetch_assoc($cust);

	echo"<tr><td><span style='display:none;'>".$value['orderno']."</span>".$value['ordno'].".</td><td><span style='display:none;'>".$value['date']."</span> ".date('d-m-Y',strtotime($value['date']))."</td>
	<td>".$cust1['name']."</td><td>".$cust1['mobile1']."</td>
	<td style='text-align:right;'>".number_format($value['amount'],2,'.','')."</td>
	
	<td>

														   <div class='hidden-sm hidden-xs btn-group'>
														   
																<a href='viewsalesorder.php?id=".$value['orderno']."' title='View'>
																	<i class='ace-icon fa fa-search-plus bigger-100 blue'></i></a> &nbsp;
																	
																	<a href='editsalesorder.php?id=".$value['orderno']."' title='Edit'>
																	<i class='ace-icon fa fa-pencil bigger-100 green'></i></a> &nbsp;
																	
																<a href='del.php?id=".$value['orderno']."&desc=salesorder' title='Delete'>
																	<i class='ace-icon fa fa-trash-o bigger-100 red'></i></a>

	</td></tr>";

	$stl=$stl+$value['amount'];

	}

	echo"</tbody></table><br />";
	
	
	echo"<center><b>Total Rs. : ".number_format($stl,2,'.','')."</b><br /><br />
	
	<a href='javascript:history.back(1);'>Back</a></center><br />";

}

elseif(isset($_GET['submit']))
{
$fromdate=$_GET['fromdate'];
$todate=$_GET['todate'];
$executive=$_GET['executive'];

$emp=mysqli_query($conn,"select * from employee where ID='$executive'");
$emp1=mysqli_fetch_assoc($emp);

$frmdate=date('Y-m-d',strtotime($fromdate));
$toddate=date('Y-m-d',strtotime($todate));

echo"<center><table style='line-height:25px;'>

<tr><td><b>Period : ".$fromdate." - ".$todate."</b></td></tr>
<tr><td><b>Executive : ".$emp1['name']."</b></td></tr>

</table></center>";

$foll=mysqli_query($conn,"select * from followup where date between '$frmdate' and '$toddate' and executive='$executive'");
$foll1=mysqli_num_rows($foll);

$ufoll=mysqli_query($conn,"select * from followup where date between '$frmdate' and '$toddate' and executive='$executive' and status='Updated'");
$ufoll1=mysqli_num_rows($ufoll);

$cfoll=mysqli_query($conn,"select * from followup where date between '$frmdate' and '$toddate' and executive='$executive' and status='Confirmed'");
$cfoll1=mysqli_num_rows($cfoll);

$cno=0;

$so=mysqli_query($conn,"select * from sales_order where date between '$frmdate' and '$toddate' and executive='$executive' group by orderno");
while($so1=mysqli_fetch_assoc($so))
{
$cno=$cno+1;
}

$vso=mysqli_query($conn,"select sum(total) as total from sales_order where date between '$frmdate' and '$toddate' and executive='$executive'");
$vso1=mysqli_fetch_assoc($vso);

$csno=0;
$sinv=mysqli_query($conn,"select * from invoice where date between '$frmdate' and '$toddate' and executive='$executive' group by invoiceno");
while($sinv1=mysqli_fetch_assoc($sinv))
{
$csno=$csno+1;
}

$vsinv=mysqli_query($conn,"select * from invoice where date between '$frmdate' and '$toddate' and executive='$executive'");
$vsinv1=mysqli_fetch_assoc($vsinv);

echo"<table class='table table-bordered table-condensed'>

<thead><tr><th style='width:15%;'>Total Calls</th><th style='width:16%;'>Updated</th><th style='width:17%;'>Order Confirmed</th><th style='width:17%;'>Order Value (Rs.)</th><th style='width:15%;'>Total Invoices</th><th style='width:20%;'>Total Invoice Value (Rs.)</th></tr></thead>

<tr><td style='text-align:center;'>".$foll1."</td><td style='text-align:center;'>".$ufoll1."</td><td style='text-align:center;'><a href='executivesummary.php?order=order&executive=".$executive."&fromdate=".$fromdate."&todate=".$todate."'>".$cno."</a></td><td style='text-align:right;'><a href='executivesummary.php?order=order&executive=".$executive."&fromdate=".$fromdate."&todate=".$todate."'>".$vso1['total']."</a></td><td style='text-align:center;'><a href='executivesummary.php?invoice=invoice&executive=".$executive."&fromdate=".$fromdate."&todate=".$todate."'>".$csno."</a></td><td style='text-align:right;'><a href='executivesummary.php?invoice=invoice&executive=".$executive."&fromdate=".$fromdate."&todate=".$todate."'>".$vsinv1['total']."</a></td></tr>

</table><br /><br />

<center><a href='executivesummary.php'>Back</a></center><br />";

}

else
{

?>

<center>

<form action="executivesummary.php" method="get" id="work">

<table width="50%" style="line-height:35px;">

<tr><td>From Date <span class="red"></span></td><td></td>
<td> <input class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="fromdate" id="from-datepicker" title="*" class="datepicker" autocomplete="off" /required></td></tr>

<tr><td>To Date <span class="red"></span></td><td></td>
<td> <input class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="todate" id="to-datepicker" title="*" class="popupdatepicker" autocomplete="off" /required></td></tr>

<tr><td>Executive </td><td></td>
<td><select class="form-control" style="width: 30ch; margin-bottom:10px" name="executive" class="chosen" style="width:220px;"><option value="">-- Select --</option>

<?php

$sel=mysqli_query($conn,"select * from admin where username='$user'");
$nmrw=mysqli_num_rows($sel);
$sel1=mysqli_fetch_assoc($sel);

if($nmrw>0)
{
$exec=mysqli_query($conn,"select employee.ID as ID, employee.name as name from employee left join department on employee.department=department.ID where department.crm='1' order by employee.ID asc");
while($exec1=mysqli_fetch_assoc($exec))
{
	echo"<option value='".$exec1['ID']."'>".$exec1['name']."</option>";
}
}
else
{

$empd=mysqli_query($conn,"select * from employee where username='$user'");
$empd1=mysqli_fetch_assoc($empd);

$usr=$empd1['ID'];

echo"<option value='".$usr."'>".$empd1['name']."</option>";

}

?>

</select></td></tr>

</table><br />

<button  style="margin-left: -65px; " type="submit" name="submit" class="btn btn btn-primary" value="submit">Go</button>

</form>

</center>

<?php

}

?>                             

								<!-- PAGE CONTENT ENDS -->
							</div><!-- /.col -->
						</div><!-- /.row -->

					</div><!-- /.page-content -->
				</div>
			</div><!-- /.main-content -->
			</div><!-- /.main-content -->
			</div><!-- /.main-content -->

<?php

include 'bottom.php';

}
else
{

 header("location:login.php");
}

ob_flush();

?>