<?php 
include_once("../connect.php");
global $con;
include_once("../session.php");

if(isset($_REQUEST['id'])){
	$id = $_REQUEST['id'];
}else{
	header('Location: index.php');
	exit();
}
$query = "SELECT * FROM COMMISSION_HISTORY_HEAD WHERE ID = $id";
$result = mysqli_query($con,$query);
$row = mysqli_fetch_array($result);
$batchNotes = $row['Notes'];
$batchPeriod = $row['Period'];
$batchUser = $row['User'];
$batchDate = $row['TransactionDate'];


 ?>
 
 <!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="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
     <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/fileinput.js"></script>
       	 <script src="../js/bootstrap-combobox.js"></script>
				 <script src="../js/bootstrap-waitingfor.min.js"></script>
				 
				 <script src="../js/functions.js"></script>
           <script type="text/javascript">
             $(document).ready(function(){
				//				$('#salesDataTable').DataTable();
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="../css/styles.css"/>
	<link rel="stylesheet" href="../css/fileinput.min.css"/>
	
	<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs-3.3.6/dt-1.10.12/af-2.1.2/fh-3.1.2/r-2.1.0/sc-1.4.2/se-1.2.0/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs-3.3.6/dt-1.10.12/af-2.1.2/fh-3.1.2/r-2.1.0/sc-1.4.2/se-1.2.0/datatables.min.js"></script>

   </head>
   <body>
<?php 
	include("navbar.php");

?>
        
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
	<div class="container-fluid">
		<div class="row">
			<div class="col-xs-8">
				<h3 class="page-header">Salesman Commission Result</h3>
			</div>
			<div class="col-xs-4">
				<div class="panel panel-default">
					<div class="panel-heading">
						<h3 class="panel-title">Batch ID: <?= $id ?> (<?= date("d M Y h:i", strtotime($batchDate)) ?>)</h3>
					</div>
					<div class="panel-body" style="">
						<b>Period: </b><?= date("M Y", strtotime($batchPeriod)) ?><br />
						<b>User: </b><?= $batchUser ?><br />
						<b>Notes: </b><?= $batchNotes ?>
					</div>
				</div>
			</div>
		</div>
		
		<div class="row">
			<div class="col-xs-12">
				<h4 class="page-header">Sales Commission Summary</h4>
			</div>
		</div>
		
		<div class="row">
			<div class="col-xs-8">
				<table class="table" id="salesDataTable">
					<thead>
						<tr>
							<th>Location</th>
							<th>Ruta</th>
							<th>Ventas</th>
							<th>Comisiones</th>
							<th>GP</th>
							<th>Download</th>
						</tr>
					</thead>
					<tbody>
					<?php
						$totalVentas = 0;
						$totalComision = 0;
						$totalGP = 0;
						$query = "SELECT Branch, Salesman, SUM(Price) as Ventas, SUM(Commission) as Comisiones, SUM(`GrossMargin`) as GP FROM COMMISSION_HISTORY_SALES_RPT WHERE RunID = $id GROUP BY Branch, Salesman ORDER BY Branch, Salesman, AccountNumber";
						$result = mysqli_query($con, $query);
						while($row = mysqli_fetch_array($result)){
							$totalVentas += $row['Ventas'];
							$totalComision += $row['Comisiones'];
							$totalGP += $row['GP'];
							
					?>
						<tr>
							<td><?= $row['Branch'] ?></td>
							<td><?= $row['Salesman'] ?></td>
							<td><?= moneyFormat($row['Ventas']) ?></td>
							<td><?= moneyFormat($row['Comisiones']) ?></td>
							<td><?= moneyFormat($row['GP']) ?></td>
							<td><a class="btn btn-success" href="mod.php?p=commission&a=functions&f=downloadSalesReport&id=<?= $id ?>&branch=<?= $row['Branch'] ?>&salesman=<?= $row['Salesman'] ?>">Download Detail</a></td>					
						</tr>
					<?php	
						}
					?>
						<tr>
							<td></td>
							<td></td>
							<td><b><?= moneyFormat($totalVentas) ?></b></td>
							<td><b><?= moneyFormat($totalComision) ?></b></td>
							<td><b><?= moneyFormat($totalGP) ?></b></td>
						</tr>
					</tbody>
				</table>
			</div>
			<div class="col-xs-4">
				<div class="panel panel-default">
					<div class="panel-heading">
						<h3 class="panel-title">Other Sales Report Downloads</h3>
					</div>
					<div class="panel-body">
						<table class="table">
							<thead>
								<tr>
									<th>Report Type</th>
									<th></th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td>Summary Report</td>
									<td><a class="btn btn-success" href="mod.php?p=commission&a=functions&f=downloadSalesReport&id=<?= $id ?>&branch=ALL&salesman=ALL&type=summary">Download</a></td>
								</tr>
								<tr>
									<td>Complete Report</td>
									<td><a class="btn btn-success" href="mod.php?p=commission&a=functions&f=downloadSalesReport&id=<?= $id ?>&branch=ALL&salesman=ALL">Download</a></td>
								</tr>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
		<h4 class="page-header">Payment Commission Summary</h4>
		<div class="container-fluid">
			<div class="row">
				<div class="col-xs-8">
					<table class="table" id="paymentsDataTable">
						<thead>
							<tr>
								<th>Location</th>
								<th>Ruta</th>
								<th>Cobros Netos</th>
								<th>Descuento</th>
								<th>IVU</th>
								<th>Comision</th>
								<th>Download</th>
							</tr>
						</thead>
					<tbody>
						<?php
							$totalCobros = 0;
							$totalTax = 0;
							$totalDiscount = 0;
							$totalComision = 0;
							$query = "SELECT Branch, Salesman, SUM(PaymentAmt) as Cobros, SUM(Discount) as Descuento, SUM(Tax) as Tax, SUM(Commission) as Comisiones FROM COMMISSION_HISTORY_PAID_INVOICE_RPT WHERE RunID = $id GROUP BY Branch, Salesman ORDER BY Branch, Salesman, AccountNumber";
							$result = mysqli_query($con, $query);
							while($row = mysqli_fetch_array($result)){
								$totalCobros += $row['Cobros'];
								$totalComision += $row['Comisiones'];
								$totalTax += $row['Tax'];
								$totalDiscount += $row['Descuento'];
								
						?>
								<tr>
									<td><?= $row['Branch'] ?></td>
									<td><?= $row['Salesman'] ?></td>
									<td><?= moneyFormat($row['Cobros']) ?></td>
									<td><?= moneyFormat($row['Descuento']) ?></td>
									<td><?= moneyFormat($row['Tax']) ?></td>
									<td><?= moneyFormat($row['Comisiones']) ?></td>
									<td><a class="btn btn-success" href="mod.php?p=commission&a=functions&f=downloadPaymentsReport&id=<?= $id ?>&branch=<?= $row['Branch'] ?>&salesman=<?= $row['Salesman'] ?>">Download Detail</a></td>					
								</tr>
						<?php	
							}
						?>
							<tr>
								<td></td>
								<td></td>
								<td><b><?= moneyFormat($totalCobros) ?></b></td>
								<td><b><?= moneyFormat($totalDiscount) ?></b></td>
								<td><b><?= moneyFormat($totalTax) ?></b></td>
								<td><b><?= moneyFormat($totalComision) ?></b></td>
							</tr>
						</tbody>
					</table>
				</div>
				<div class="col-xs-4">
					<div class="panel panel-default">
						<div class="panel-heading">
							<h3 class="panel-title">Other Payment Report Downloads</h3>
						</div>
						<div class="panel-body">
							<table class="table">
								<thead>
									<tr>
										<th>Report Type</th>
										<th></th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td>Summary Report</td>
										<td><a class="btn btn-success" href="mod.php?p=commission&a=functions&f=downloadPaymentsReport&id=<?= $id ?>&branch=ALL&salesman=ALL&type=summary">Download</a></td>
									</tr>
									<tr>
										<td>Complete Report</td>
										<td><a class="btn btn-success" href="mod.php?p=commission&a=functions&f=downloadPaymentsReport&id=<?= $id ?>&branch=ALL&salesman=ALL">Download</a></td>
									</tr>
								</tbody>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

     <script type="text/javascript">
			function openIframeModal(identifier){
				if(identifier == "payment"){
					$('#iFrameModalUrl').attr('src', "mod.php?p=commission&a=import&t=payments");
				}
				if(identifier == "sales"){
					$('#iFrameModalUrl').attr('src', "mod.php?p=commission&a=import&t=sales");
				}
				if(identifier == "specials"){
					$('#iFrameModalUrl').attr('src', "mod.php?p=commission&a=import&t=specials");
				}
				$('#iFrameModal').modal();
			}
			function updateSalesStatus(){
				$.get("mod.php?p=commission&a=functions&f=getWorkingTableStatus&table=sales", function(data){
					data = JSON.parse(data);
					$('#salesTableStatusRows').text(data.Count);
					$('#salesTableStatusRevenue').text(moneyFormat(data.TotalRevenue));
					$('#salesTableCommissionTotal').text(moneyFormat(data.TotalCommission));
					$('#salesTableAverageCommissionRate').text(Math.round((data.AverageCommissionRate*10000))/100 + "%");
				});
			}
			function getSalesCount(){
				$.get("mod.php?p=commission&a=functions&f=getWorkingTableStatus&table=salesCount", function(data){
					data = JSON.parse(data);
					alert(data.Count);
				});
			}
			function saveAndViewResults(){
				var period = $('#commissionPeriod').val();
				if(period === ""){
					alert("Please select a period (month and year");
					$('#commissionPeriod').focus();
				}else{
					data = {};
					data.period = period;
					data.notes = $('#commissionNotes').val();
					$.post('mod.php?p=commission&a=functions&f=saveWorkingTable', data, function(result){
						alert(result);
						
						
						});
				}
			}
			function updatePaymentsStatus(){
				$.get("mod.php?p=commission&a=functions&f=getWorkingTableStatus&table=payments", function(data){
					data = JSON.parse(data);
					$('#paymentsTableStatusRows').text(data.Count);
					$('#paymentsTableStatusTotalRevenue').text(moneyFormat(data.TotalPayments));
					$('#paymentsTableStatusTotalDiscount').text(moneyFormat(data.TotalDiscount));
					$('#paymentsTableStatusTotalTax').text(moneyFormat(data.TotalTax));
					$('#paymentsTableStatusTotal').text(moneyFormat(parseFloat(data.TotalPayments)+parseFloat(data.TotalTax)));
					
					$('#paymentsTableCommissionTotal').text(moneyFormat(data.TotalCommission));
				});
			}
			function updateSpecialsStatus(){
				$.get("mod.php?p=commission&a=functions&f=getWorkingTableStatus&table=specials", function(data){
					data = JSON.parse(data);
					$('#specialsTableStatusRows').text(data.Count);
				});
			}
			function refreshAll(){
					updateSalesStatus();
					updatePaymentsStatus();
					updateSpecialsStatus();
			}
			refreshAll();
			
			function processCommissions(what){
				var word = '';
				if(what == "sales"){
						word = "Sales";
				}else if(what == "payments"){
						word = "Payments";
				}				
				waitingDialog.show('Processing ' + word + ' Data');
				$.get("mod.php?p=commission&a=functions&f=processCommissions&table=" + what, function(data){
						//alert(word + " Data has been processed");
						waitingDialog.hide();
						refreshAll();
				});
			}
     </script>
		 	<style>
				#iFrameModalUrl{
					width: 100%;
					height: 500px;
					border: none;
				}
				
			</style>
			<!-- Modal -->
			<div class="modal fade" id="iFrameModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
				<div class="modal-dialog modal-lg" role="document">
					<div class="modal-content">
						<div class="modal-body">
							<iframe id="iFrameModalUrl" src=""></iframe>
						</div>
						<div class="modal-footer">
							<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
							<button type="button" class="btn btn-primary" onclick="refreshAll();" data-dismiss="modal">Continue</button>
						</div>
					</div>
				</div>
			</div>
		 
   </body>
 </html>