
<?php


if(isset($_REQUEST['date'])){
	$reportDate = $_REQUEST['date'];	
}else{
	$reportDate = date('Y-m-d');
}
if(false){
	
	class Pickup {
		public $PickupID, $PickupTicket, $Customer, $PickupDate, $Items, $PickupNotes, $Packages, $Salesman, $AccountNumber, $RequestedBy, $Void, $VoidID, $VoidDate, $VoidReason, $VoidUser,$Loaded, $Via;
		public function __construct($PickupID) {
			$query = "SELECT * FROM PICKUP_HEAD WHERE PickupID = $PickupID";
			global $con;
			$result = mysqli_query($con, $query);
			$row = mysqli_fetch_array($result);
			$this->PickupID = $row['PickupID'];
			$this->PickupTicket = $row['PickupTicket'];
			$this->AccountNumber = $row['AccountNumber'];
			$this->Customer = new Customer($row['AccountNumber']);
			$this->PickupDate = $row['PickupDate'];
			$this->PickupNotes = $row['PickupNotes'];
			$this->Packages = $row['Packages'];
			$this->Salesman = new Salesman($row['Salesman']);
			$this->RequestedBy	= $row['RequestedBy'];
			$this->Via	= $row['Via'];
			$this->Loaded = false;
			$this->Void = $row['Void'];
			if($row['Void']){
				$query = "SELECT * FROM PICKUP_VOID WHERE PickupID = " . $row['PickupID'];
				$result = mysqli_query($con, $query);
				$row2 = mysqli_fetch_array($result);
				$this->VoidID = $row2['ID'];
				$this->VoidReason = $row2['Reason'];
				$this->VoidDate = $row2['VoidDate'];
				$this->VoidUser = $row2['User'];
			}
			$this->Items = array();
			$query = "SELECT id FROM PICKUP_DETAIL WHERE PickupID = $PickupID";
			$result = mysqli_query($con, $query);
			while($row = mysqli_fetch_array($result)){
				$item = new PickupItem($row['id']);
				array_push($this->Items, $item);
				unset($item);
			}
		
		}
		public function Void($reason){
			global $con;
			$reason = mysqli_real_escape_string($con,$reason);
			if(trim($reason) == ""){
				die("Invalid Reason");
			}
			/*
			$query = "UPDATE PAYMENT_HEAD SET CashAmount = 0, CheckAmount = 0, ProcessDate = NOW(), Processed = 1, Void = TRUE WHERE id = " . $this->PaymentID . ";";
			$query .= "UPDATE PAYMENT_DETAIL SET InvoiceNumber = CONCAT(InvoiceNumber,'V'), PartialPayment = 0, SubtotalAmount = 0, DiscountAmount = 0, TaxAmount = 0, PaymentAmount = 0, Reference = '' WHERE PaymentID = " . $this->PaymentID . ";";
			$query .= "UPDATE PAYMENT_CREDIT SET CreditNumber = CONCAT(CreditNumber,'V'), Subtotal = 0, Discount = 0, Tax = 0, Total = 0, Reference = '' WHERE PaymentID = " . $this->PaymentID . ";";
			$query .= "INSERT PAYMENT_VOID (PaymentID, Reason, User, VoidDate) VALUES (" . $this->PaymentID . ", '$reason', '" . $_SESSION['Username'] . "', NOW());";
			*/
			$reasonText = "THIS PICKUP REQUEST WAS VOIDED BECAUSE OF THE FOLLOWING REASON:" . $reason;
			$query = "UPDATE PICKUP_HEAD SET ToPickup = NOW(), ToPickupUser = '" . $_SESSION['Username'] . "', AtWarehouse = NOW(), AtWarehouseUser = '" . $_SESSION['Username'] . "', PickupStatus = 'VOID', PickupStatusUser = '" . $_SESSION['Username'] . "', PickupStatusNotes = '$reasonText', Void = TRUE WHERE PickupID = " . $this->PickupID . ";";
			$query .= "INSERT PICKUP_VOID (PickupID, Reason, User, VoidDate) VALUES (" . $this->PickupID . ", '$reason', '" . $_SESSION['Username'] . "', NOW());";
			
			
			if(!mysqli_multi_query($con, $query)){
				$data['result'] = false;
				$data['error'] = mysqli_error($con);
				$data['sql'] = $query;
				echo json_encode($data);
			}else{
				$data['result'] = "true";
				echo json_encode($data);
			}
			//echo $query;
			event_log($_SESSION['Username'],"PICKUP VOID", "Pickup Void \n\Pickup ID: " . $this->PickupID . " \nCustomer: " . $this->Customer->AccountNumber . " - " . $this->Customer->Name . "\nUser: " . $_SESSION['User'] . "\nReason: " . $reason);
		}
		public function GetVoidDetails(){
			global $con;
			$data['Pickup'] = $this->PickupID;
			$data['VoidDate'] = $this->VoidDate;
			$data['VoidReason'] = $this->VoidReason;
			$data['VoidUser'] = $this->VoidUser;
			//var_dump($this);
			echo json_encode($data);
		}
	}
	
	class PickupItem {
		public $Line, $SKU, $Description, $InvoiceNumber, $OrderID, $Reason, $Qty, $ReasonText;
		public function __construct($id) {
			$query = "SELECT PICKUP_DETAIL.*, CONCAT(CONCAT(INVENTORY.Description, ' '),INVENTORY.Notes) AS Description FROM PICKUP_DETAIL, INVENTORY WHERE `id` = $id AND PICKUP_DETAIL.SKU = INVENTORY.SKU AND PICKUP_DETAIL.LINE = INVENTORY.LINE";
			global $con;
			$result = mysqli_query($con, $query);
			$row = mysqli_fetch_array($result);
			$this->Line = $row['Line'];
			$this->SKU = $row['SKU'];
			$this->Description = $row['Description'];
			$this->OrderID = $row['OrderID'];
			$this->InvoiceNumber = $row['InvoiceNumber'];
			$this->Reason = $row['Reason'];
			$this->Qty = $row['Qty'];
			$this->ReasonText = getReasonText($row['Reason']);
			
		}	
	}
}

include_once("connect.php");
include_once("session.php");

?>
<!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(){
              // $('.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"></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 ReceiveDate BETWEEN '$reportDate' AND  '$reportDate 23:59:59' 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"></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 ReceiveDate BETWEEN '$reportDate' AND  '$reportDate 23:59:59' 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 ReceiveDate BETWEEN '$reportDate' AND  '$reportDate 23:59:59' 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 file_get_contents("http://orders.eurojaponautosupply.com/pickupRequest.php?pickup=$pickupID&key=$key");
				echo "<div class=\"breakBefore\"></div>";
				
			}
		}
		
	?>
	
   </body>
   
 </html>
 
 <!--
			
		-->