<?php

$fromDate = $_REQUEST['fromDate'];
$toDate = $_REQUEST['toDate'];

?>
<!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'] ?></title>
 
     <!-- Bootstrap -->
     <link href="../css/bootstrap.min.css" rel="stylesheet">
     <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 type="text/javascript">
             $(document).ready(function(){
       //        $('.grpline').css("width", "100px");
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="../css/styles.css"/>
   </head>
   <body>
   
       <?php include("navbar.php"); ?>     

   
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
   
   <div class="container " style="padding-top: 10px;">
		<img src="../img/<?= $config['SmallLogo'] ?>">
	<div class="panel panel-default">
	  <div class="panel-heading form-inline">
			<div style="margin-right: 10px; float: right;">
	  			
	  	</div>	    
			<center><h5>Order Summary Report<br><?= date("m/d/y", strtotime($fromDate)) ?> to <?= date("m/d/y", strtotime($toDate)) ?></h5></center>
	  	
				
					    
	  </div>
		<style>
					.ReportSalesmanName{
						width: 300px;
					}
					.ReportSalesmanPercent{
						width: 100px;
						text-align: right;
					}
					.ReportSalesmanDetailAmount{
						width: 150px;
						text-align: right;
					}
					.ReportSalesmanTotalAmount{
						width: 150px;
						text-align: center;
						vertical-align: middle !important;
						font-weight: bold;
					}
					.ReportTable{
						border-style: solid;
					}
				</style>
	  <div class="panel-body">
			<div class="row">
			<?php
			$query = "SELECT SALESMAN.ID, FullName, SUM(IF(SalesmanOrder,(OrderQty * Price),0)) SalesmanTotal, SUM(IF(NOT SalesmanOrder,(OrderQty * Price),0)) CustomerTotal FROM SALESMAN  LEFT JOIN (ORDER_HEAD JOIN ORDER_DETAIL USING(OrderID)) ON Salesman = SALESMAN.ID WHERE OrderDate BETWEEN '$fromDate' AND '$toDate 23:59:59' AND DeleteOrder = 0 GROUP BY SALESMAN.ID  ORDER BY SALESMAN.ID";
			$result = mysqli_query($con, $query);
			$fullCustomerTotal = 0;
			$fullSalesmanTotal = 0;
			while($row = mysqli_fetch_array($result)){
				$customerAmount = $row['CustomerTotal'];
				$salesmanAmount = $row['SalesmanTotal'];
				$fullCustomerTotal += $customerAmount;
				$fullSalesmanTotal += $salesmanAmount;
				$total = $customerAmount + $salesmanAmount;
			?>
				
				
					<div class="col-xs-6">
							<table class="table table-hover table-condensed ReportTable" id="">
								<thead></thead>
								<tbody>
									<tr>
										<td class="ReportSalesmanName"><?= $row['ID'] . "-Customer" ?></td>
										<td class="ReportSalesmanPercent"><?= @round($customerAmount/$total*100,0) ?>%</td>
										<td class="ReportSalesmanDetailAmount"><?= moneyFormat($customerAmount) ?></td>
										<td class="ReportSalesmanTotalAmount" rowspan="2"><?= moneyFormat($total) ?></td>
									</tr>
									<tr>
										<td class="ReportSalesmanName"><?= $row['ID'] . "-" . $row['FullName'] ?></td>
										<td class="ReportSalesmanPercent"><?= @round($salesmanAmount/$total*100,0) ?>%</td>
										<td class="ReportSalesmanDetailAmount"><?= moneyFormat($salesmanAmount) ?></td>
									</tr>
								</tbody>
							 </table>
						
					</div>
				
				
			<?php
			}
			?>
			</div>
	  </div>
	  <div class="panel-footer" style="height: 110px; text-align: right;" >
			<div class="col-xs-6" style="float: right;">
				<table class="table table-condensed">
					<tr>
						<td><b>Salesman</b></td>
						<td style="text-align: right;"><b><?= @round(@($fullSalesmanTotal/@($fullSalesmanTotal+$fullCustomerTotal)*100)) ?>%</b></td>
						<td style="text-align: right;"><b><?= @moneyFormat($fullSalesmanTotal) ?></b></td>
					</tr>
					<tr>
						<td><b>Customer</b></td>
						<td style="text-align: right;"><b><?= @round($fullCustomerTotal/($fullSalesmanTotal+$fullCustomerTotal)*100) ?>%</b></td>
						<td style="text-align: right;"><b><?= moneyFormat($fullCustomerTotal) ?></b></td>
					</tr>
					<tr>
						<td></td>
						<td style="text-align: right;"><b>Total</b></td>
						<td style="text-align: right;"><b><?= @moneyFormat(($fullSalesmanTotal+$fullCustomerTotal)) ?></b></td>
					</tr>
				</table>
			</div>
	  	
	  </div>
	</div>

</div>   
   
   
   </body>
 </html>