<?php 
//error_reporting(E_ALL);

$fromDate = $_REQUEST['fromDate'];
$toDate = $_REQUEST['toDate'] . " 23:59:59";


$salesman = $_REQUEST['sm'];




 ?>
 
 <!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">
	 <title><?= $config['CompanyName'] ?> - Analysis Report - <?= $salesman->Name ?></title>
 
	 <!-- Bootstrap -->
	 <link href="../css/bootstrap.css" rel="stylesheet" media="all">
	 <link rel="stylesheet" href="../css/bootstrap-combobox.css" />
 
	 <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
	 <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
	 <!--[if lt IE 9]>
	   <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
	   <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
	   <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
		   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
		   <!-- Include all compiled plugins (below), or include individual files as needed -->
		   <script src="../js/bootstrap.min.js"></script>
		 <script src="../js/bootstrap-combobox.js"></script>
		 <script src="../js/excellentexport.js"></script>
				 <script src="../js/functions.js"></script>
				 <script src="../js/jquery.flot.js"></script>
		   <link rel="stylesheet" href="../css/bootstrap-print.css" media="print"/>
	<script type="text/javascript">
		var month = ['','Jan', 'Feb' , 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep','Oct', 'Nov', 'Dec'];
		
	</script>
	 <![endif]-->
	<link rel="stylesheet" href="../css/styles.css"/>
	<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css"/>
	<style>
		.listPanel {
			height: 500px;
			background-color: #eee;
			
		}
		@media print {
			.breakBefore {page-break-before: always;}
		}
		a[href]:after{
		content:"";
		}
		body {
			padding-top: 50px;
			-webkit-print-color-adjust: exact;
			
		}
@import url("//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css");
body{color:#000}
.widget {
	margin: 0 0 25px 0;
	display: block;
	-webkit-border-radius: 2px;
	-moz-border-radius: 2px;
	border-radius: 2px;
}
.widget .widget-heading {
	padding: 7px 15px;
	-webkit-border-radius: 5px 5px 0 0;
	-moz-border-radius: 5px 5px 0 0;
	border-radius: 5px 5px 0 0;
	text-transform: uppercase;
	text-align: center;
	background: #888;
	color: white;
}
.widget .widget-body {
	padding: 10px 15px;
	font-size: 16px;
	font-weight: 300;
	background: #EEE;
}
.salesWidget{
	height: 16em; 
}
.lostSaleWidget{
	height: 7em; 
}
	</style>
   </head>
   <body>
   
	   <?php //include("navbar.php"); ?>     

<?php
if($salesman == "ALL"){
	$q = "SELECT ID FROM SALESMAN WHERE ID != 'FJ' AND ID != 'AR' AND ID != '91' AND ID != '96' AND ID != '97' AND ID != '0' AND ID != '98' AND ID != '99' AND ID != '30' AND ID != 'PXX'";
	
	$rslt = mysqli_query($con, $q);
	while($rw = mysqli_fetch_array($rslt)){
		displaySAR_EJAS($rw['ID']);
	
		
		echo '<div class="breakBefore"></div>';
	}
}else{
	
	displaySAR_EJAS($salesman);
	
	
	echo '<div class="breakBefore"></div>';
}



function displaySAR_EJAS($sm){
		global $con;
		global $fromDate;
		global $toDate;
		
$salesman = new Salesman($sm);

?>
   

<div class="container">
  
  <div class="text-center">
	<h1>Salesman Analisys Report</h1>
	<p class="lead"><?= $salesman->ID . " - " . $salesman->Name ?></p>
  </div>



  
  <div class="row">
					
			<?php
						
						$year = 0;
						$begun = false;
						$totalSales = 0;
						$totalQuota = 0;
						$query = "SELECT Salesman, `Month`, `Year`, MTD, Goal FROM SALESMAN_QUOTA WHERE Salesman = '" . $salesman->ID . "' AND
						(
							`Year` BETWEEN YEAR('$fromDate') AND YEAR('$toDate') AND `Month` BETWEEN MONTH('$fromDate') AND MONTH('$toDate') 
						OR 
							`Year` BETWEEN YEAR(DATE_ADD('$fromDate', INTERVAL -1 YEAR)) AND YEAR(DATE_ADD('$toDate', INTERVAL -1 YEAR)) AND `Month` BETWEEN MONTH('$fromDate') AND MONTH('$toDate') 	
						) GROUP BY Salesman, `Month`, `Year`	ORDER BY `Year`, `Month` ASC";
						//echo $query;
						$result = mysqli_query($con, $query);
						echo '<script type="text/javascript">';
						echo "var dataQuotaEJAS_" . $salesman->ID . " = [];\n";
						echo "var yearData = [];\n";
						while($row = mysqli_fetch_array($result)){
							if($row['Year'] == date("Y")){
								$totalSales += $row['MTD'];
								$totalQuota += $row['Goal'];
								
							}elseif($row['Year'] == date("Y")-1){
								$totalLastYearSales += $row['MTD'];
								$totalLastYearQuota += $row['Goal'];
							}
							//var_dump($row['OrderYear'] != $year);
							if($row['Year'] != $year){
								$year = $row['Year'];
								if($begun){
									echo "var obj = {};\n";
									//
									echo "obj.label = " . $year . "-1;";
									echo "obj.data = yearData;";
									echo "dataQuotaEJAS_" . $salesman->ID . ".push(obj);\n";
									echo "yearData = [];\n";
								}
								$begun = true;
							}
						?>
							var item = [<?= $row['Month']  ?>, <?= $row['MTD'] ?>];
							yearData.push(item);
							
						<?php	
						}
						echo "var obj = {};\n";
						
						echo "obj.label = " . $year . ";";
						echo "obj.data = yearData;";
						echo "dataQuotaEJAS_" . $salesman->ID . ".push(obj);\n";
						echo "yearData = [];\n";
						
						echo "</script>";
						if($totalSales/$totalQuota > 1){
								$arrowColor = "blue";
								$arrow = "up";
						}else{
								$arrowColor = "red";
								$arrow = "down";
						}
						$TYSalesArrowTag = "<span style=\"color: $arrowColor;\" class=\"glyphicon glyphicon-arrow-" . ($arrow) . "\" aria-hidden=\"true\"></span>";
						if($totalLastYearSales/$totalLastYearQuota > 1){
								$arrowColor = "blue";
								$arrow = "up";
						}else{
								$arrowColor = "red";
								$arrow = "down";
						}
						$LYSalesArrowTag = "<span style=\"color: $arrowColor;\" class=\"glyphicon glyphicon-arrow-" . ($arrow) . "\" aria-hidden=\"true\"></span>";
						?>
						
						<div class="col-lg-3 col-md-3 col-sm-3">
			  <div class="widget">
				<div class="widget-heading clearfix">
				  <div class="pull-left">Sales - Quota</div>
				  <div class="pull-right"></div>
				</div>
				<div class="widget-body clearfix salesWidget" id="salesWidget">
				 
				  <div class="pull-left">TY Sales:</div>
				  <div class="pull-right"><?= moneyFormat($totalSales) . $TYSalesArrowTag ?></div><br>
									<div class="pull-left">LY Sales:</div>
				  <div class="pull-right"><?= moneyFormat($totalLastYearSales) . $LYSalesArrowTag ?></div><br>
									<br>
				  <div class="pull-left">TY Quota:</div>
				  <div class="pull-right"><?= moneyFormat($totalQuota) . $TYSalesArrowTag  ?></div><br>
									<div class="pull-left">LY Quota:</div>
				  <div class="pull-right"><?= moneyFormat($totalLastYearQuota) . $LYSalesArrowTag  ?></div><br>
									<br>
									<div class="pull-left">TY Difference:</div>
				  <div class="pull-right"><?= moneyFormat($totalSales-$totalQuota) . $TYSalesArrowTag  ?></div><br>
									<div class="pull-left">LY Difference:</div>
				  <div class="pull-right"><?= moneyFormat($totalLastYearSales-$totalLastYearQuota) . $LYSalesArrowTag  ?></div><br>
				  
				</div>

			  </div>
			</div>
						
						
			<div class="col-lg-9 col-md-9 col-sm-9">
			  <div class="widget">
				<div class="widget-heading clearfix">
				  <div class="pull-left">Sales Chart - Quota</div>
				  <div class="pull-right"><i class="fa fa-angle-up"></i></div>
				</div>
				<div class="widget-body clearfix salesWidget">
									<div id="quotaChartDiv-<?= $salesman->ID ?>" style="width:100%;height:100%"></div>
				</div>
				
			  </div>
			</div>
						
						<?php
						$query = "SELECT
							MONTH(InvoiceDate) OrderMonth,
							YEAR(InvoiceDate) OrderYear,
							Salesman,
							SUM(ShippedQty * Price)
							TotalSales,
							SUM(IF(SalesmanOrder,(ShippedQty * Price),0)) TotalBySalesman,
							COUNT(DISTINCT(IF(SalesmanOrder,(InvoiceID),null))) CountBySalesman,
							SUM(IF(NOT SalesmanOrder,(ShippedQty * Price),0)) TotalByCustomer,
							COUNT(DISTINCT(IF(NOT SalesmanOrder,(OrderID),null))) CountByCustomer
						FROM `INVOICE_HEAD`
							JOIN `INVOICE_DETAIL` USING(InvoiceID)
						WHERE
							(
								InvoiceDate Between '$fromDate' AND '$toDate'
							OR
								InvoiceDate Between DATE_ADD('$fromDate', INTERVAL -1 YEAR) AND DATE_ADD('$toDate', INTERVAL -1 YEAR)
							)
							AND Salesman = '" . $salesman->ID . "' AND INVOICE_HEAD.Void = 0
						GROUP BY
							Salesman, MONTH(InvoiceDate), YEAR(InvoiceDate)
						ORDER BY
							YEAR(InvoiceDate),
							MONTH(InvoiceDate);";
						//echo $query;
						$result = mysqli_query($con, $query);
						
						$salesByCustomer = 0;
						$salesBySalesman = 0;
						$countByCustomer = 0;
						$countBySalesman = 0;
						//echo "dataQuotaEJAS_" . $salesman->ID . ".push(obj);\n";
						echo '<script type="text/javascript">';
						echo "var data_" . $salesman->ID . " = [];\n";
						echo "var yearData = [];\n";
						$year = 0;
						$begun = false;
						$largestOrderEJAS = 0;
						
						while($row = mysqli_fetch_array($result)){
							$largestOrderEJAS = max($largestOrderEJAS,$row['TotalSales']);
							
							if($row['OrderYear'] == date("Y")){
								
								$salesByCustomer += $row['TotalByCustomer'];
								$salesBySalesman += $row['TotalBySalesman'];
								$countByCustomer += $row['CountByCustomer'];
								$countBySalesman += $row['CountBySalesman'];
							}
							//var_dump($row['OrderYear'] != $year);
							if($row['OrderYear'] != $year){
								$year = $row['OrderYear'];
								if($begun){
									echo "var obj = {};\n";
									echo "obj.label = " . $year . "-1;";
									echo "obj.data = yearData;";
									echo "data_" . $salesman->ID . ".push(obj);\n";
									echo "yearData = [];\n";
								}
								$begun = true;
							}
							
						?>
							var item = [<?= $row['OrderMonth']  ?>, <?= $row['TotalSales'] ?>];
							yearData.push(item);
							
						<?php	
						}
						echo "var obj = {};\n";
						echo "obj.label = '" . $year . "';";
						echo "obj.data = yearData;";
						echo "data_" . $salesman->ID . ".push(obj);\n";
						echo '</script>';
						?>
	
			<div class="col-lg-3 col-md-3 col-sm-3">
			  <div class="widget">
				<div class="widget-heading clearfix">
				  <div class="pull-left">Sales - Web</div>
				  <div class="pull-right"></div>
				</div>
				<div class="widget-body clearfix salesWidget" id="salesWidget">
				  <div class="pull-left">By Customer:</div>
				  <div class="pull-right"><?= moneyFormat($salesByCustomer) ?></div><br>
				  <div class="pull-left">&nbsp;&nbsp;&nbsp;&nbsp;Count:</div>
				  <div class="pull-right"><?= $countByCustomer ?></div><br>
				  <div class="pull-left">&nbsp;&nbsp;&nbsp;&nbsp;Average:</div>
				  <div class="pull-right"><?= moneyFormat(@($salesByCustomer/$countByCustomer)) ?></div><br>
				  <br>
				  <div class="pull-left">By Salesman:</div>
				  <div class="pull-right"><?= moneyFormat($salesBySalesman) ?></div><br>
				  <div class="pull-left">&nbsp;&nbsp;&nbsp;&nbsp;Count:</div>
				  <div class="pull-right"><?= $countBySalesman ?></div><br>
				  <div class="pull-left">&nbsp;&nbsp;&nbsp;&nbsp;Average:</div>
				  <div class="pull-right"><?= moneyFormat(@($salesBySalesman/$countBySalesman)) ?></div><br>
				  <br>
				  <b>
				  <div class="pull-left">Total:</div>
				  <div class="pull-right"><?= moneyFormat($salesBySalesman + $salesByCustomer) ?></div><br>
					<div class="pull-left">&nbsp;&nbsp;&nbsp;&nbsp;Count:</div>
				  <div class="pull-right"><?= $countBySalesman+$countByCustomer ?></div><br>
				  <div class="pull-left">&nbsp;&nbsp;&nbsp;&nbsp;Average:</div>
				  <div class="pull-right"><?= moneyFormat(@(($salesBySalesman+$salesByCustomer)/($countBySalesman+$countByCustomer))) ?></div><br>
				  </b>
				</div>

				
			   
			  </div>
			</div>
						
						<div class="col-lg-9 col-md-9 col-sm-9">
			  <div class="widget">
				<div class="widget-heading clearfix">
				  <div class="pull-left">Sales Chart - Web</div>
				  <div class="pull-right"><i class="fa fa-angle-up"></i></div>
				</div>
				<div class="widget-body clearfix salesWidget">
								<div id="salesChartDiv-<?= $salesman->ID ?>" style="width:100%;height:100%"></div>
				</div>
				
			  </div>
			</div>
						<?php
						
						
						$largestOrderEV = 0;
						$salesByCustomer = 0;
						$salesBySalesman = 0;
						$countByCustomer = 0;
						$countBySalesman = 0;
						
						?>
	
			
						
			<?php
			$query = "SELECT YEAR(PaymentDate) `Year`,ROUND(SUM(PaymentAmount),2) TotalPayment, count(distinct(IF(PaymentAmount = 0, null, PaymentID)) ) Payments ,count(distinct(IF(PaymentAmount = 0, PaymentID, null)) ) PaymentReminders FROM `PAYMENT_HEAD` JOIN PAYMENT_DETAIL ON(PAYMENT_HEAD.ID = PAYMENT_DETAIL.PaymentID) WHERE (PaymentDate Between '$fromDate' AND '$toDate' OR PaymentDate Between DATE_ADD('$fromDate', INTERVAL -1 YEAR) AND DATE_ADD('$toDate', INTERVAL -1 YEAR)) AND SalesmanID = '" . $salesman->ID . "'  AND Void = 0 GROUP BY SalesmanID, YEAR(PaymentDate);";
			if(isset($_REQUEST['test'])){
				echo $query;
			}
			
			$result = mysqli_query($con, $query);
			while($row = mysqli_fetch_array($result)){
				if($row['Year'] == date('Y')){
					$tyTotalPayments = $row['TotalPayment'];
					$tyPaymentCount = $row['Payments'];
					$tyPaymentReminder = $row['PaymentReminders'];
				}else{
					$lyTotalPayments = $row['TotalPayment'];
					$lyPaymentCount = $row['Payments'];
					$lyPaymentReminder = $row['PaymentReminders'];
				}
			}
			?>
		<div class="col-lg-3 col-md-3 col-sm-3">
				<div class="widget">
					<div class="widget-heading clearfix">
						<div class="pull-left">PAYMENTS</div>
			<div class="pull-right"><i class="fa fa-angle-up"></i></div>
		  </div>
					<div class="widget-body clearfix salesWidget">
					  <div class="pull-left">TY Payments</div>
						<div class="pull-right"><?= moneyFormat($tyTotalPayments) ?></div><br>
						<div class="pull-left">LY Payments</div>
						<div class="pull-right"><?= moneyFormat($lyTotalPayments) ?></div><br>
						
			<br>
						<div class="pull-left">TY Count:</div>
			<div class="pull-right"><?= $tyPaymentCount ?></div><br>
						<div class="pull-left">LY Count:</div>
			<div class="pull-right"><?= $lyPaymentCount ?></div><br>
						<br>
						<div class="pull-left">TY Payment Reminders</div>
						<div class="pull-right"><?= $tyPaymentReminder ?></div><br>
						<div class="pull-left">LY Payment Reminders</div>
						<div class="pull-right"><?= $lyPaymentReminder ?></div><br>
		  </div>
		</div>
	  </div>
		<?php
		$query = "SELECT YEAR(PaymentDate) `Year`, MONTH(`PaymentDate`) `Month`,ROUND(SUM(PaymentAmount),2) TotalPayment, count(distinct(IF(PaymentAmount = 0, null, PaymentID)) ) Payments ,count(distinct(IF(PaymentAmount = 0, PaymentID, null)) ) PaymentReminders FROM `PAYMENT_HEAD` JOIN PAYMENT_DETAIL ON(PAYMENT_HEAD.ID = PAYMENT_DETAIL.PaymentID) WHERE (PaymentDate Between '$fromDate' AND '$toDate' OR PaymentDate Between DATE_ADD('$fromDate', INTERVAL -1 YEAR) AND DATE_ADD('$toDate', INTERVAL -1 YEAR)) AND SalesmanID = '" . $salesman->ID . "' AND Void = 0 GROUP BY SalesmanID, YEAR(PaymentDate), MONTH(`PaymentDate`);";
		$result = mysqli_query($con, $query);
		$year = "";
		$chartArray = array();
		$series = array();
		while($row = mysqli_fetch_array($result)){
			if($year != $row['Year']){
				$year = $row['Year'];
				if(sizeof($series)){
					array_push($chartArray, $series);
				}
				$series = array();
				$series['label'] = $row['Year'];
				$series['data'] = array();
			}
			$data = array(intval($row['Month']), floatval($row['TotalPayment']));
			array_push($series['data'], $data);
		}
		array_push($chartArray, $series);
			echo "<script type=\"text/javascript\">\n";
			echo "paymentDataEJAS = JSON.parse('" . json_encode($chartArray) . "');";
			echo "</script>\n";
		?>
	  <div class="col-lg-9 col-md-9 col-sm-9">
				<div class="widget">
					<div class="widget-heading clearfix">
						<div class="pull-left">Payments Chart</div>
						<div class="pull-right"><i class="fa fa-angle-up"></i></div>
					</div>
					<div class="widget-body clearfix salesWidget">				
						<div id="paymentChartDiv-<?= $salesman->ID ?>" style="width:100%;height:100%"></div>
					</div>
				</div>
			</div>
			
			
		
			<?php
			
			$query = "SELECT count(DISTINCT(IF(DATEDIFF(NOW(), LastLogin)<=15,AccountNumber,null))) Active, count(DISTINCT(IF(DATEDIFF(NOW(), LastLogin)<=15,null,AccountNumber))) Inactive, count(LOGIN_LOG.ID) Traffic FROM CUSTOMER LEFT JOIN LOGIN_LOG ON CUSTOMER.AccountNumber = LOGIN_LOG.Username WHERE (LoginDate BETWEEN '$fromDate' AND '$toDate' or LoginDate is Null) AND Salesman = '" . $salesman->ID . "'";
			$result = mysqli_query($con, $query);
			$row = mysqli_fetch_assoc($result);
			
			
			?>
			<div class="col-lg-3 col-md-3 col-sm-3">
				<div class="widget">
					<div class="widget-heading clearfix">
						<div class="pull-left">Customers</div>
			<div class="pull-right"><i class="fa fa-angle-up"></i></div>
		  </div>
		  <div class="widget-body clearfix lostSaleWidget">
						<div class="pull-left">Active (< 15 days):</div>
							<div class="pull-right"><?= $row['Active'] ?></div><br>
							<div class="pull-left">Inactive (> 15 days):</div>
							<div class="pull-right"><?= $row['Inactive'] ?></div><br>
							<div class="pull-left">Traffic:</div>
							<div class="pull-right"><?= $row['Traffic'] ?></div><br>
						</div>
		  </div>
		</div>
			
			
		
			<div class="col-lg-3 col-md-3 col-sm-3">
				<div class="widget">
					<div class="widget-heading clearfix">
						<div class="pull-left">Lost Sales</div>
			<div class="pull-right"><i class="fa fa-angle-up"></i></div>
		  </div>
		  <div class="widget-body clearfix lostSaleWidget">
				<?php
				
				$query = "SELECT Reason, Count(id) LostSaleCount FROM LOST_SALE WHERE AccountNumber = '" . $salesman->ID . "' AND LostSaleDate BETWEEN '$fromDate' AND '$toDate' GROUP BY Reason";
				
				$result = mysqli_query($con, $query);
				
				while($row = mysqli_fetch_array($result)){
				?>
							<div class="pull-left"><?= $row['Reason'] ?></div>
							<div class="pull-right"><?= $row['LostSaleCount'] ?></div><br>
				<?php
				}
				
				?>
				</div>
		</div>
	  </div>
			
			
			
			<div class="col-lg-3 col-md-3 col-sm-3">
				<div class="widget">
					<div class="widget-heading clearfix">
						<div class="pull-left">Pickups</div>
			<div class="pull-right"><i class="fa fa-angle-up"></i></div>
		  </div>
		  <div class="widget-body clearfix">
					<?php
						$query = "SELECT Count(PickupID) PickupsCount, count(IF(Reason = 'GAR', (PICKUP_DETAIL.id), null)) Warranties, COUNT(IF(Approved = 0,(PICKUP_DETAIL.id), null)) ApprovedWarranties, COUNT(IF(Approved = 0,(PICKUP_DETAIL.id), null)) DeniedWarranties, COUNT(IF(ISNULL(Approved),(PICKUP_DETAIL.id), null)) PendingWarranties FROM PICKUP_HEAD JOIN PICKUP_DETAIL using(PickupID) LEFT JOIN `PICKUP_REASON` ON `Reason` = `Key` WHERE Salesman = '" . $salesman->ID . "' AND PickupDate BETWEEN '$fromDate' AND '$toDate'";
						$result = mysqli_query($con, $query);
						while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
					?>
						<div class="pull-left">Returns</div>
						<div class="pull-right"><?= $row['PickupsCount'] ?></div><br>
						<div class="pull-left">Warranties</div>
						<div class="pull-right"><?= $row['Warranties'] ?></div><br>
						<div class="pull-left">Approved Warranties</div>
						<div class="pull-right"><?= $row['ApprovedWarranties'] ?></div><br>
						<div class="pull-left">Denied Warranties</div>
						<div class="pull-right"><?= $row['DeniedWarranties'] ?></div><br>
						<div class="pull-left">Pending Returns</div>
						<div class="pull-right"><?= $row['PendingWarranties'] ?></div><br>
					<?php
						}
					?>
						
					</div>
		</div>
	  </div>
			
			
			
			
			<div class="col-lg-3 col-md-3 col-sm-3">
				<div class="widget">
					<div class="widget-heading clearfix">
						<div class="pull-left">Last Login</div>
			<div class="pull-right"><i class="fa fa-angle-up"></i> </div>
		  </div>
		  <div class="widget-body clearfix">
						<br>
						<br>
						<br>
						<br>
						<br>
					</div>
		</div>
	  </div>
			
			
			
			<div class="col-lg-6 col-md-6 col-sm-6">
				<div class="widget">
					<div class="widget-heading clearfix">
						<div class="pull-left">Top 10 Accounts</div>
		  </div>
		  <div class="widget-body clearfix" style="height: 30em;">
						<table class='table'>
							<thead>
								<tr>
			
									<td>Account</td>
									<td>Name</td>
									<td>YTD</td>
								</tr>
							</thead>
							<body>
			<?php
				//$query = "SELECT AccountNumber, COUNT(OrderID) OrderCount, SUM(OrderQty * Price) TotalAmount, OrderDate FROM ORDER_HEAD JOIN ORDER_DETAIL USING(OrderID) WHERE YEAR(OrderDate) = YEAR(NOW()) AND Salesman = '" . $salesman->ID . "' GROUP BY AccountNumber ORDER BY SUM(OrderQty * Price) DESC LIMIT 10";
				$query = "SELECT AccountNumber, `Name`,COUNT(InvoiceID) OrderCount, SUM(ShippedQty * Price) TotalAmount, InvoiceDate FROM INVOICE_HEAD JOIN INVOICE_DETAIL USING(InvoiceID) JOIN CUSTOMER USING(AccountNumber) WHERE YEAR(InvoiceDate) = YEAR(NOW()) AND INVOICE_HEAD.Salesman = '" . $salesman->ID . "' GROUP BY AccountNumber ORDER BY SUM(ShippedQty * Price) DESC LIMIT 10";
				$result = mysqli_query($con, $query);
				//echo $query;
				while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
					
				
			?>
								<tr>
									<td><?= $row['AccountNumber'] ?></td>
									<td><?= substr($row['Name'],0,30) ?></td>
									<td><?= moneyFormat($row['TotalAmount']) ?></td>
								</tr>
								
			<?php
				}
			?>
							</body>
						</table>
					</div>
		</div>
	  </div>
			
			
			
			<div class="col-lg-6 col-md-6 col-sm-6">
				<div class="widget">
					<div class="widget-heading clearfix">
						<div class="pull-left">Top 10 Overdue</div>
		  </div>
		  <div class="widget-body clearfix"  style="height: 32em;">
						<table class='table'>
							<thead>
								<tr>
									
									<td>Customer</td>
									<td>Invoice Number</td>
									<td>Days Overdue</td>
									<td>Subtotal</td>
								</tr>
							</thead>
							<tbody>
								<?php
								
							//$query = "SELECT AccountNumber, DATEDIFF(NOW(),MIN(InvoiceDate)) DaysDue, SUM(`Subtotal`) Subtotal FROM INVOICE JOIN CUSTOMER USING(AccountNumber) WHERE Salesman = '" . $salesman->ID . "' GROUP BY AccountNumber ORDER BY InvoiceDate ASC LIMIT 10";
							$query = "SELECT AccountNumber, InvoiceID, `Name`, DATEDIFF(NOW(),MIN(InvoiceDate)) DaysDue, SUM(`Subtotal`) Subtotal FROM INVOICE_HEAD LEFT JOIN CUSTOMER USING(AccountNumber) WHERE CUSTOMER.Salesman = '" . $salesman->ID . "' AND (`Subtotal`) > 0 AND `Status` = 1 AND Void = 0 AND InvoiceID not IN (SELECT InvoiceID FROM PAYMENT_DETAIL WHERE PartialPayment = 0) GROUP BY AccountNumber ORDER BY InvoiceDate ASC LIMIT 10";
							//$query = "SELECT AccountNumber, InvoiceNumber, `Name`, DATEDIFF(NOW(),MIN(InvoiceDate)) DaysDue, SUM(`Subtotal`) Subtotal FROM INVOICE JOIN CUSTOMER USING(AccountNumber) WHERE Salesman = '" . $salesman->ID . "' GROUP BY AccountNumber ORDER BY InvoiceDate ASC LIMIT 10";
							$result = mysqli_query($con, $query);
							$totalOverdueEJAS = 0;
							while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
							$totalOverdueEJAS += 	$row['Subtotal'];
								
							
						?>
											<tr>
												
												<td style="white-space: nowrap; overflow:hidden !important; text-overflow: ellipsis;"><?=  substr($row['AccountNumber'] . "-" . $row['Name'],0,28) ?></td>
												<td><?= $row['InvoiceID'] ?></td>
												<td><?= ($row['DaysDue']) ?></td>
												<td ><?= moneyFormat($row['Subtotal']) ?></td>
											</tr>
											
						<?php
							}
						?>
									<tr>
											<td><b>Total</b></td>
											<td></td>
											<td></td>
											<td><b><?= moneyFormat($totalOverdueEJAS) ?></b></td>
										</tr>
							</tbody>
						</table>
					</div>
		</div>
	  </div>
			
			
		</div>
	</div><!-- /.container -->
	<script type="text/javascript">
			$(document).ready(function(){
//        $('.grpline').css("width", "100px");
				 var quotaOptions_<?= $salesman->ID ?> = {
												 series: {
													 lines : { show: true },
													 //bars : { show: true }
													 points: { show: true },
												 },
												 grid: {
													 hoverable: true,
													 clickable: true,
												 },
												 xaxis: {
																	ticks: [ [1,'Jan'], [2,'Feb'], [3,'Mar'], [4,'Apr'], [5,'May'], [6,'Jun'], [7,'Jul'], [8,'Aug'], [8,'Sep'], [10,'Oct'], [11,'Nov'], [12,'Dec'] ],
																	tickSize: 1
																}
											 };
				 var salesOptionsEJAS_<?= $salesman->ID ?> = {
												 series: {
													 lines : { show: true },
													 //bars : { show: true }
													 points: { show: true }
												 },
												 grid: {
													 hoverable: true,
													 clickable: true,
												 },
												 yaxis: { max: <?= $largestOrderEJAS ?> },
												 xaxis: {
																	ticks: [ [1,'Jan'], [2,'Feb'], [3,'Mar'], [4,'Apr'], [5,'May'], [6,'Jun'], [7,'Jul'], [8,'Aug'], [8,'Sep'], [10,'Oct'], [11,'Nov'], [12,'Dec'] ],
																	tickSize: 1
																}
											 };
				 var salesOptionsEV_<?= $sid ?> = {
												 series: {
													 lines : { show: true },
													 //bars : { show: true }
													 points: { show: true },
												 },
												 grid: {
													 hoverable: true,
													 clickable: true,
												 },
												 yaxis: { max: <?= $largestOrderEV ?> },
												 xaxis: {
																	ticks: [ [1,'Jan'], [2,'Feb'], [3,'Mar'], [4,'Apr'], [5,'May'], [6,'Jun'], [7,'Jul'], [8,'Aug'], [8,'Sep'], [10,'Oct'], [11,'Nov'], [12,'Dec'] ],
																	tickSize: 1
																}
											 };
				 var paymentOptionsEJAS_<?= $salesman->ID ?> = {
												 series: {
													 lines : { show: true },
													 //bars : { show: true }
													 points: { show: true }
												 },
												 grid: {
													 hoverable: true,
													 clickable: true,
												 },
												 //yaxis: { max: <?= $largestOrderEJAS ?> },
												 xaxis: {
																	ticks: [ [1,'Jan'], [2,'Feb'], [3,'Mar'], [4,'Apr'], [5,'May'], [6,'Jun'], [7,'Jul'], [8,'Aug'], [8,'Sep'], [10,'Oct'], [11,'Nov'], [12,'Dec'] ],
																	tickSize: 1
																}
											 };
				 var paymentOptionsEV_<?= $sid ?> = {
												 series: {
													 lines : { show: true },
													 //bars : { show: true }
													 points: { show: true },
												 },
												 grid: {
													 hoverable: true,
													 clickable: true,
												 },
												 //yaxis: { max: <?= $largestOrderEV ?> },
												 xaxis: {
																	ticks: [ [1,'Jan'], [2,'Feb'], [3,'Mar'], [4,'Apr'], [5,'May'], [6,'Jun'], [7,'Jul'], [8,'Aug'], [8,'Sep'], [10,'Oct'], [11,'Nov'], [12,'Dec'] ],
																	tickSize: 1
																}
											 };
				 $.plot($("#salesChartDiv-<?= $salesman->ID ?>"),data_<?= $salesman->ID ?>, salesOptionsEJAS_<?= $salesman->ID ?>);
				 
				 $.plot($("#quotaChartDiv-<?= $salesman->ID ?>"),dataQuotaEJAS_<?= $salesman->ID ?>, quotaOptions_<?= $salesman->ID ?>);
				 $.plot($("#paymentChartDiv-<?= $salesman->ID ?>"),paymentDataEJAS, paymentOptionsEJAS_<?= $salesman->ID ?>);
				 
				 
				 $("<div id='tooltip'></div>").css({
						position: "absolute",
						display: "none",
						border: "1px solid #fdd",
						padding: "2px",
						"background-color": "#fee",
						opacity: 0.80
					}).appendTo("body");
					
					$("#quotaChartDiv-<?= $salesman->ID ?>").bind("plothover", function (event, pos, item) {
					
						if ($("#enablePosition:checked").length > 0) {
							var str = "(" + pos.x.toFixed(2) + ", " + pos.y.toFixed(2) + ")";
							$("#hoverdata").text(str);
						}
					
						if ($("#enableTooltip:checked").length > 0) {
							if (item) {
								var x = item.datapoint[0].toFixed(2),
									y = item.datapoint[1].toFixed(2);
					
								$("#tooltip").html(item.series.label + " of " + x + " = " + y)
									.css({top: item.pageY+5, left: item.pageX+5})
									.fadeIn(200);
							} else {
								$("#tooltip").hide();
							}
						}
					});
					
					$("#quotaChartDiv-<?= $salesman->ID ?>").bind("plotclick", function (event, pos, item) {
						if (item) {
							$("#clickdata").text(" - click point " + item.dataIndex + " in " + item.series.label);
							plot.highlight(item.series, item.datapoint);
						}
					});
			});
		</script>
		<hr />
		<!--   START EMPRESAS VELAZQUEZ SECTION   -->
		
		<?php
		
}
function displaySAR_EV($sid){
	//global $con;
		global $fromDate;
		global $toDate;
		$salesman = new Salesman($sid);
		if($sid == 35 || $sid == 37 || $sid == 38){
		//if salesman is 35 ,37, or 38, substract 20 to get 15, 17 or 18 respectively (Salesmen with 2 numbers)
			$sid -= 20;
		}
		$salesman->ID = $sid;
		
		?>
		
<?php
}
?>
	
   </body>
   
 </html>