
<?php


if(isset($_REQUEST['date'])){
	$reportDate = $_REQUEST['date'];	
}else{
	$reportDate = date('Y-m-d');
}
if(isset($_REQUEST['batch'])){
	$batch = $_REQUEST['batch'];
}else{
	$batch = getCurrentBatchNumber();
}
if(isset($_REQUEST['save']) && $_REQUEST['save'] == "true"){
	saveBatch();
}
include_once("../connect.php");
include_once("../session.php");

?>

 <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(){
              // $('.combobox').combobox();
       //        $('.grpline').css("width", "100px");
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="../css/styles.css"/>
	<style>
		.listPanel {
			height: 500px;
			background-color: #eee;
			
		}
		@media print {
			.breakBefore {page-break-before: always;}
		}
	</style>
   </head>
   <body  onload="window.print();">
   
       <?php// include("navbar.php"); ?>     

   <br />
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
	<div class="container " style="margin-top: -50px; width: 100%;">
		<div class="row">
			<center>
			<img src="/img/logo_ej.png" style="height: 75px;" />
			</center>
			
		</div>
		<div class="container">
			<div class="row">
				<center><h3>Carrier Receiving Report</h3></center>
			</div>
			<div class="row">
				<h4>
				<div class="col-md-3">Report Date: <?= date("m/d/Y", strtotime($reportDate)) ?></div>
				<div class="col-md-2">Batch: <?= $batch ?></div>
				<div class="col-md-3">User: <?= $_SESSION['Username'] ?></div>
				<div class="col-md-4">Print Date/Time: <?= date("m/d/Y h:i a", time()+(3*60*60)) ?></div>
				</h4>
			</div>
			<div class="row">
				
				<table class="table table-bordered table-striped table-condensed table-responsive">
					<thead>
						<tr>
							<th>
								Pickup ID
							</th>
							<th>
								Customer
							</th>
							<th>
								Total Packages
							</th>
							<th>
								Received
							</th>
							<th>
								Missing
							</th>
							<th>
								Status
							</th>
						</tr>
					</thead>
					<tbody>
					<?php
						$query = "SELECT PickupID, COUNT(id) AS ReceivedToday FROM PICKUP_RECEIVE_PACKAGE WHERE BatchID = $batch GROUP BY PickupID";
						$result = mysqli_query($con, $query);
						$lastNumber = "";
						$newNumber = false;
						$totalPackages = 0;
						$totalMissing = 0;
						$totalReceived = 0;
						while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
							
							$pickup = new Pickup($row['PickupID']);
							$missingCount  = 0;
							$missing = "";
							$totalPackages += $pickup->Packages;
							$totalReceived += $row['ReceivedToday'];
							for($i = 1; $i <= count($pickup->PackageDetails); $i++){
								if(!$pickup->PackageDetails[$i]->Received){
									$totalMissing++;
									$missingCount++;
									$missing .= $pickup->PickupID . "-" . $i . "<br>";
								}else{
									
								}
							}
							if($row['ReceivedToday'] != $pickup->Packages && $missingCount == 0){
								$missing = "None. Received on multiple dates.";
								//$missing = $row['ReceivedToday'] . "!=" .  $pickup->Packages;
							}
					?>
						<tr>
							<td>
								<?= $pickup->PickupID ?>
							</td>
							<td>
								<?= $pickup->Customer->Name ?>
							</td>
							<td>
								<?= $pickup->Packages ?>
							</td>
							<td>
								<?= ($row['ReceivedToday'])?>
							</td>
							<td>
								<?= $missing == ""?"":$missing ?>
							</td>
							<td style="text-align: center;"><?= $missingCount == 0?"<span style=\"color: green;\" class=\"glyphicon glyphicon-ok\"></span>":"<span style=\"color: red;\" class=\"glyphicon glyphicon-remove\"></span>" ?></td>
						</tr>
						
					<?php
						}
					?>
					<tr>
						<td>
						</td>
						<td>
						</td>
						<td>
							<b>Total Packages:</b> <?= $totalPackages ?>
						</td>
						<td>
							<b>Total Received:</b> <?= $totalReceived ?>
						</td>
						<td>
							<b>Total Missing:</b> <?= $totalMissing ?>
						</td>
					</tr>
					</tbody>
				</table>
				
			</div>
			<div class="row">
				<br>
				<h4>
				<div class="col-xs-6">Receiver Signature: _______________________________</div>
				
				
				<div class="col-xs-6">Carrier Signature: _______________________________</div>
				</h4>
			</div>
		</div>
			
	</div>
	<div class="breakBefore"></div>
	
	
	
	
	<!--   -->
	
	<!-- Stack the columns on mobile by making one full-width and the other half-width -->
	<div class="container " style="margin-top: -50px; width: 100%;">
		<div class="row">
			<center>
			<img src="/img/logo_ej.png" style="height: 75px;" />
			</center>
			
		</div>
		<div class="container">
			<div class="row">
				<center><h3>Carrier Receiving Report</h3></center>
			</div>
			<div class="row">
				<h4>
				<div class="col-md-3">Report Date: <?= date("m/d/Y", strtotime($reportDate)) ?></div>
				<div class="col-md-2">Batch: <?= $batch ?></div>
				<div class="col-md-3">User: <?= $_SESSION['Username'] ?></div>
				<div class="col-md-4">Print Date/Time: <?= date("m/d/Y h:i a", time()+(3*60*60)) ?></div>
				</h4>
			</div>
			<div class="row">
				
				<table class="table table-bordered table-striped table-condensed table-responsive">
					<thead>
						<tr>
							<th>
								Pickup ID
							</th>
							<th>
								Customer
							</th>
							<th>
								Total Packages
							</th>
							<th>
								Received
							</th>
							<th>
								Missing
							</th>
							<th>
								Status
							</th>
						</tr>
					</thead>
					<tbody>
					<?php
						$query = "SELECT PickupID, COUNT(id) AS ReceivedToday FROM PICKUP_RECEIVE_PACKAGE WHERE BatchID = $batch GROUP BY PickupID";
						$result = mysqli_query($con, $query);
						$lastNumber = "";
						$newNumber = false;
						$totalPackages = 0;
						$totalMissing = 0;
						$totalReceived = 0;
						while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
							
							$pickup = new Pickup($row['PickupID']);
							$missingCount  = 0;
							$missing = "";
							$totalPackages += $pickup->Packages;
							$totalReceived += $row['ReceivedToday'];
							for($i = 1; $i <= count($pickup->PackageDetails); $i++){
								if(!$pickup->PackageDetails[$i]->Received){
									$totalMissing++;
									$missingCount++;
									$missing .= $pickup->PickupID . "-" . $i . "<br>";
								}else{
									
								}
							}
							if($row['ReceivedToday'] != $pickup->Packages && $missingCount == 0){
								$missing = "None. Received on multiple dates.";
								//$missing = $row['ReceivedToday'] . "!=" .  $pickup->Packages;
							}
					?>
						<tr>
							<td>
								<?= $pickup->PickupID ?>
							</td>
							<td>
								<?= $pickup->Customer->Name ?>
							</td>
							<td>
								<?= $pickup->Packages ?>
							</td>
							<td>
								<?= ($row['ReceivedToday'])?>
							</td>
							<td>
								<?= $missing == ""?"":$missing ?>
							</td>
							<td style="text-align: center;"><?= $missingCount == 0?"<span style=\"color: green;\" class=\"glyphicon glyphicon-ok\"></span>":"<span style=\"color: red;\" class=\"glyphicon glyphicon-remove\"></span>" ?></td>
						</tr>
						
					<?php
						}
					?>
					<tr>
						<td>
						</td>
						<td>
						</td>
						<td>
							<b>Total Packages:</b> <?= $totalPackages ?>
						</td>
						<td>
							<b>Total Received:</b> <?= $totalReceived ?>
						</td>
						<td>
							<b>Total Missing:</b> <?= $totalMissing ?>
						</td>
					</tr>
					</tbody>
				</table>
				
			</div>
			<div class="row">
				<br>
				<h4>
				<div class="col-xs-6">Receiver Signature: _______________________________</div>
				
				
				<div class="col-xs-6">Carrier Signature: _______________________________</div>
				</h4>
			</div>
		</div>
			
	</div>
	<div class="breakBefore"></div>
	<?php
		
		
		if(isset($_REQUEST['printReceipts']) && $_REQUEST['printReceipts'] == "true"){
			//echo file_get_contents("http://orders.eurojaponautosupply.com/admin/mod.php?p=pickupreceiving&a=carrierreport");
			//echo "<div class=\"breakBefore\"></div>";
			
			$query = "SELECT PickupID, COUNT(id) AS ReceivedToday FROM PICKUP_RECEIVE_PACKAGE WHERE BatchID = $batch GROUP BY PickupID";
			$result = mysqli_query($con, $query);
			while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
				$realKey = md5($row['PickupID'] . "EJAS");
				$realKey = substr($realKey, -16);
				
				$pickupID = $row['PickupID'];
				$key = $realKey;
				echo "<div class=\"pickupReceipt\">";
				echo file_get_contents("http://orders.eurojaponautosupply.com/pickupRequest.php?pickup=$pickupID&key=$key");
				echo "</div>";
				echo "<div class=\"breakBefore\"></div>";
				$query2 = "UPDATE PICKUP_HEAD SET Loaded = 1 WHERE PickupID = " . $row['PickupID'];
				mysqli_query($con, $query2);
				
			}
			
		}
		
	?>
	<script type="text/javascript">
		$(document).ready(function(){
			$('.pickupReceipt').css('font-size', '2vw');
			$('.pickupReceipt table').css('font-size', '2vw');
			$('.pickupReceipt h4').css('font-size', '2vw');
			$('.pickupReceipt h4').css('margin-top', '100px');
			$('.pickupReceipt h4').css('padding-top', '100px');
			
		});
	</script>
   </body>
   
 </html>
 
 <!--
			
		-->