<?php 
//error_reporting(E_ALL);

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


$customer = new Customer($_REQUEST['an']);



 ?>
 
 <!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 - <?= $customer->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" media="all"/>
	<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" media="all"/>
	<style media="all">
		.listPanel {
			height: 500px;
			background-color: #eee;
			
		}
		@media print {
			.breakBefore {page-break-before: always;}
			body {
				-webkit-print-color-adjust: exact;
			}
			div.widget{
        page-break-inside: avoid;
			}
		}
		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: 9em; 
}
	</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;
		
global $customer;

?>
   

<div class="container">
  
  <div class="text-center">
	<h1>Customer Analisys Report</h1>
	<p class="lead"><?= $customer->AccountNumber . " - " . $customer->Name ?></p>
  </div>



  
  <div class="row">
						
						<?php
						//$query = "SELECT
						//	MONTH(OrderDate) OrderMonth,
						//	YEAR(OrderDate) OrderYear,
						//	Salesman,
						//	SUM(OrderQty * Price)
						//	TotalSales,
						//	SUM(IF(SalesmanOrder,(OrderQty * Price),0)) TotalBySalesman,
						//	COUNT(DISTINCT(IF(SalesmanOrder,(OrderID),null))) CountBySalesman,
						//	SUM(IF(NOT SalesmanOrder,(OrderQty * Price),0)) TotalByCustomer,
						//	COUNT(DISTINCT(IF(NOT SalesmanOrder,(OrderID),null))) CountByCustomer
						//FROM `ORDER_HEAD`
						//	JOIN `ORDER_DETAIL` USING(OrderID)
						//WHERE
						//	(
						//		OrderDate Between '$fromDate' AND '$toDate'
						//	OR
						//		YEAR(OrderDate) = YEAR(DATE_ADD('$fromDate', INTERVAL -1 YEAR))
						//	)
						//	AND AccountNumber = '" . $customer->AccountNumber . "' AND DeleteOrder = 0
						//GROUP BY
						//	Salesman, MONTH(OrderDate), YEAR(OrderDate)
						//ORDER BY
						//	YEAR(OrderDate),
						//	MONTH(OrderDate);";
						
						$query = "SELECT MONTH(InvoiceDate) OrderMonth, YEAR(InvoiceDate) OrderYear, Salesman,
										SUM(ShippedQty * Price) TotalSales, SUM(IF(SalesmanOrder,(ShippedQty * Price),0)) TotalBySalesman,
										COUNT(DISTINCT(IF(SalesmanOrder,(OrderID),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 DATE('$fromDate') AND DATE('$toDate') OR YEAR(InvoiceDate) = YEAR(DATE_ADD('$fromDate', INTERVAL -1 YEAR)) ) AND
										AccountNumber = '" . $customer->AccountNumber . "' AND 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_" . $customer->AccountNumber . ".push(obj);\n";
						echo '<script type="text/javascript">';
						echo "var data_" . $customer->AccountNumber . " = [];\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_" . $customer->AccountNumber . ".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_" . $customer->AccountNumber . ".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 Page - </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>
				
				<?php
					//$query = "SELECT
					//		MONTH(OrderDate) OrderMonth,
					//		YEAR(OrderDate) OrderYear,
					//		Salesman,
					//		SUM(OrderQty * Price)
					//		TotalSales,
					//		SUM(IF(SalesmanOrder,(OrderQty * Price),0)) TotalBySalesman,
					//		COUNT(DISTINCT(IF(SalesmanOrder,(OrderID),null))) CountBySalesman,
					//		SUM(IF(NOT SalesmanOrder,(OrderQty * Price),0)) TotalByCustomer,
					//		COUNT(DISTINCT(IF(NOT SalesmanOrder,(OrderID),null))) CountByCustomer
					//	FROM `ORDER_HEAD`
					//		JOIN `ORDER_DETAIL` USING(OrderID)
					//	WHERE
					//		(
					//			OrderDate Between '$fromDate' AND '$toDate'
					//		OR
					//			YEAR(OrderDate) = YEAR(DATE_ADD('$fromDate', INTERVAL -1 YEAR))
					//		)
					//		AND AccountNumber = '" . $customer->AccountNumber . "' AND DeleteOrder = 0
					//	GROUP BY
					//		Salesman, MONTH(OrderDate), YEAR(OrderDate)
					//	ORDER BY
					//		YEAR(OrderDate),
					//		MONTH(OrderDate);";
					$query = "SELECT MONTH(InvoiceDate) OrderMonth, YEAR(InvoiceDate) OrderYear, Salesman,
										SUM(ShippedQty * Price) TotalSales, SUM(IF(SalesmanOrder,(ShippedQty * Price),0)) TotalBySalesman,
										COUNT(DISTINCT(IF(SalesmanOrder,(OrderID),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 DATE('$fromDate') AND DATE('$toDate') OR YEAR(InvoiceDate) = YEAR(DATE_ADD('$fromDate', INTERVAL -1 YEAR)) ) AND
										AccountNumber = '" . $customer->AccountNumber . "' AND Void = 0 GROUP BY Salesman, MONTH(InvoiceDate), YEAR(InvoiceDate) ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate)";
						
					$result = mysqli_query($con, $query);
					$year = "";
					//echo $query;
					$chartArray = array();
					$series = array();
					while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
						
						if($year != $row['OrderYear']){
							$year = $row['OrderYear'];
							if(sizeof($series)){
								array_push($chartArray, $series);
							}
							$series = array();
							$series['label'] = $row['OrderYear'];
							$series['data'] = array();
						}
						$data = array(intval($row['OrderMonth']), floatval($row['TotalSales']));
						array_push($series['data'], $data);
					}
					array_push($chartArray, $series);
						echo "<script type=\"text/javascript\">\n";
						echo "salesDataEJASN = 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">Sales Chart - Web Page - </div>
				  <div class="pull-right"><i class="fa fa-angle-up"></i></div>
				</div>
				<div class="widget-body clearfix salesWidget">
								<div id="salesChartDiv-<?= $customer->AccountNumber ?>" style="width:100%;height:100%"></div>
				</div>
				
			  </div>
			</div>
	</div>				
	<div class="row">
			<div class="col-lg-12 col-md-12 col-sm-12">
				<div class="widget">
					<div class="widget-heading clearfix">
						<div class="pull-left">Sales</div>
			<div class="pull-right"><i class="fa fa-angle-up"></i></div>
		  </div>
		  <div class="widget-body clearfix lostSaleWidget">
					<table class="table">
						<thead>
							<tr>
								<th>Year</th>
								<th>Jan</th>
								<th>Feb</th>
								<th>Mar</th>
								<th>Apr</th>
								<th>May</th>
								<th>Jun</th>
								<th>Jul</th>
								<th>Ago</th>
								<th>Sep</th>
								<th>Oct</th>
								<th>Nov</th>
								<th>Dec</th>
								
							</tr>
						</thead>
						<tbody>
							<?php
							//var_dump($chartArray);
								$index = 0;
								foreach($chartArray as $key => $value){
									echo "<tr>";
									echo "<td>" . $value['label'] . "</td>";
									$monthIndex = 1;
									foreach($value['data'] as $month=>$monthTotal){
										//echo $monthTotal[0];
										while($monthIndex < $monthTotal[0]){
											//echo "<td>" . moneyFormat(0) . "</td>";
											echo "<td></td>";
											$monthIndex++;
											//continue;
										}
						?>
							
								<td>
									
									<?= moneyFormat($monthTotal[1]) ?>
								</td>
							
						<?php
									$monthIndex++;
										
									}
									echo "</tr>";
							}
						?>
						
						</tbody>
					</table>
		  </div>
		</div>
						
			<?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 AccountNumber = '" . $customer->AccountNumber . "'  AND Void = 0 AND Processed = 1 GROUP BY AccountNumber, 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
					YEAR(PaymentDate) = YEAR(DATE_ADD('$fromDate', INTERVAL -1 YEAR))
				) AND AccountNumber = '" . $customer->AccountNumber . "' AND Void = 0 AND Processed = 1 GROUP BY AccountNumber, YEAR(PaymentDate), MONTH(`PaymentDate`);";
		//echo $query;
		$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-<?= $customer->AccountNumber ?>" style="width:100%;height:100%"></div>
					</div>
				</div>
			</div>
			
			
			<div class="col-lg-12 col-md-12 col-sm-12">
				<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 lostSaleWidget">
					<table class="table">
						<thead>
							<tr>
								<th>Year</th>
								<th>Jan</th>
								<th>Feb</th>
								<th>Mar</th>
								<th>Apr</th>
								<th>May</th>
								<th>Jun</th>
								<th>Jul</th>
								<th>Ago</th>
								<th>Sep</th>
								<th>Oct</th>
								<th>Nov</th>
								<th>Dec</th>
								
							</tr>
						</thead>
						<tbody>
						<?php
							//var_dump($chartArray);
								foreach($chartArray as $key => $value){
									echo "<tr>";
									echo "<td>" . $value['label'] . "</td>";
									$monthIndex = 1;
									foreach($value['data'] as $month=>$monthTotal){
										//echo $monthTotal[0];
										while($monthIndex < $monthTotal[0]){
											echo "<td></td>";
											$monthIndex++;
											//continue;
										}
						?>
							
								<td>
									
									<?= moneyFormat($monthTotal[1]) ?>
								</td>
							
						<?php
									$monthIndex++;
										
									}
									echo "</tr>";
							}
						?>
						</tbody>
					</table>
		  </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 Invoices Due</div>
		  </div>
		  <div class="widget-body clearfix" style="height: 30em;">
						<table class='table'>
							<thead>
								<tr>
									<td>Date</td>
									<td>Invoice</td>
									<td>Days</td>
									<td>Balance</td>
								</tr>
							</thead>
							<tbody>
			<?php
				//$query = "SELECT INVOICE.*, ROUND(INVOICE.Subtotal-SUM(PaymentAmount),2) AS Balance, PAYMENT_DETAIL.PartialPayment, ROUND(SUM(PAYMENT_DETAIL.PaymentAmount),2) as PaymentAmount, IF(INVOICE.Reference LIKE '%BC%', 1, 2) AS BC  FROM INVOICE LEFT JOIN PAYMENT_DETAIL ON INVOICE.InvoiceNumber = PAYMENT_DETAIL.InvoiceNumber JOIN CUSTOMER USING(AccountNumber) WHERE AccountNumber = '" . $customer->AccountNumber . "' AND (PartialPayment = 1 OR PartialPayment IS NULL) AND INVOICE.InvoiceNumber NOT IN (SELECT InvoiceNumber FROM PAYMENT_DETAIL WHERE PartialPayment = 0) GROUP BY InvoiceNumber  ORDER BY BC, InvoiceDate ASC limit 10";
				//echo $query;
				$query = "SELECT INVOICE_HEAD.*, ROUND(INVOICE_HEAD.Subtotal-SUM(PaymentAmount),2) AS Balance, PAYMENT_DETAIL.PartialPayment, ROUND(SUM(PAYMENT_DETAIL.PaymentAmount),2) as PaymentAmount FROM INVOICE_HEAD LEFT JOIN PAYMENT_DETAIL USING(InvoiceID) JOIN CUSTOMER USING(AccountNumber) WHERE AccountNumber = '" . $customer->AccountNumber . "' AND (PartialPayment = 1 OR PartialPayment IS NULL) AND INVOICE_HEAD.Void = 0 AND INVOICE_HEAD.InvoiceID NOT IN (SELECT InvoiceID FROM PAYMENT_DETAIL WHERE PartialPayment = 0) GROUP BY InvoiceID ORDER BY InvoiceDate ASC limit 10";
				$result = mysqli_query($con, $query);
				while($row = mysqli_fetch_array($result)){
					
				
				
			?>
								<tr>
									<td><?= date("m/d/Y" , strtotime($row['InvoiceDate'])) ?></td>
									<td><?= $row['InvoiceID'] ?></td>
									<td><?= $days = ceil((time() - strtotime($row['InvoiceDate']))/(24*60*60)); ?></td>
									<td><?= moneyFormat($row['Subtotal']-$row['PaymentAmount'] + $row['SalesTax']) ?></td>
								</tr>
								
			<?php
				}
			?>
							</tbody>
						</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">Last 10 Payments</div>
		  </div>
		  <div class="widget-body clearfix"  style="height: 30em;">
						<table class='table'>
							<thead>
								<tr>
									
									<td>Payment ID</td>
									<td>Payment Date</td>
									<td>Total</td>
								</tr>
							</thead>
							<tbody>
								<?php
								
							
							$query = "SELECT ID, PaymentDate, round(CashAmount+CheckAmount,2) Total FROM PAYMENT_HEAD WHERE AccountNumber = '" . $customer->AccountNumber . "' order by PaymentDate DESC LIMIT 10";
							$result = mysqli_query($con, $query);
							$totalOverdueEJAS = 0;
							while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
							$totalOverdueEJAS += 	$row['Subtotal'];
							$moneyTotal = moneyFormat($row['Total']);
						?>
											<tr>
												
												<td style="white-space: nowrap; overflow:hidden !important; text-overflow: ellipsis;"><?=  ($row['ID']) ?></td>
												<td><?= date("m/d/Y h:i a", strtotime($row['PaymentDate'])) ?></td>
												
												<td ><?= $row['Total'] == 0?"<i>Payment Reminder</i>":$moneyTotal ?></td>
											</tr>
											
						<?php
							}
						?>
									
							</tbody>
						</table>
					</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 = '" . $customer->AccountNumber . "'";
			$result = mysqli_query($con, $query);
			$row = mysqli_fetch_assoc($result);
			
			
			?>
			
			<!--
		
			<div class="col-lg-6 col-md-6 col-sm-6">
				<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 = '" . $customer->AccountNumber . "' 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 AccountNumber = '" . $customer->AccountNumber . "' 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-6 col-md-6 col-sm-6">
			<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>
		-->
			<?php
			
			//query para invoices due
			
			
			//SELECT INVOICE.*, ROUND(INVOICE.Subtotal-SUM(PaymentAmount),2) AS Balance, PAYMENT_DETAIL.PartialPayment, ROUND(SUM(PAYMENT_DETAIL.PaymentAmount),2) as PaymentAmount, IF(INVOICE.Reference LIKE '%BC%', 1, 2) AS BC  FROM INVOICE LEFT JOIN PAYMENT_DETAIL ON INVOICE.InvoiceNumber = PAYMENT_DETAIL.InvoiceNumber JOIN CUSTOMER USING(AccountNumber) WHERE Salesman = '34' AND (PartialPayment = 1 OR PartialPayment IS NULL) AND INVOICE.InvoiceNumber NOT IN (SELECT InvoiceNumber FROM PAYMENT_DETAIL WHERE PartialPayment = 0) GROUP BY InvoiceNumber  ORDER BY BC, InvoiceDate ASC
			
			
			?>
			
			
		</div>
	</div><!-- /.container -->
	<script type="text/javascript">
			$(document).ready(function(){
//        $('.grpline').css("width", "100px");
				 
				 var salesOptionsEJAS_<?= $customer->AccountNumber ?> = {
												 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'], [9,'Sep'], [10,'Oct'], [11,'Nov'], [12,'Dec'] ],
																	tickSize: 1
																}
											 };
				
				 var paymentOptionsEJAS_<?= $customer->AccountNumber ?> = {
												 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'], [9,'Sep'], [10,'Oct'], [11,'Nov'], [12,'Dec'] ],
																	tickSize: 1
																}
											 };
				 
				 $.plot($("#salesChartDiv-<?= $customer->AccountNumber ?>"),data_<?= $customer->AccountNumber ?>, salesOptionsEJAS_<?= $customer->AccountNumber ?>);
				 
				 $.plot($("#paymentChartDiv-<?= $customer->AccountNumber ?>"),paymentDataEJAS, paymentOptionsEJAS_<?= $customer->AccountNumber ?>);
				 
				 
				 $("<div id='tooltip'></div>").css({
						position: "absolute",
						display: "none",
						border: "1px solid #fdd",
						padding: "2px",
						"background-color": "#fee",
						opacity: 0.80
					}).appendTo("body");
					
					
			});
		</script>
		<hr />
		<!--   START EMPRESAS VELAZQUEZ SECTION   -->
		
		<?php
		
}
?>
		</div>
   </body>
   
 </html>