<?php 
//$isAdmin = stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true;
if($isAdmin){
	include("../connect.php");
	include("../session.php");
}else{
	include("connect.php");
	include("functions.php");
}
//error_reporting(E_ALL);
//ini_set("display_errors", "1"); 

	$po = new PurchaseOrder($_REQUEST['id']);	


//die('test');
if (!$isAdmin) {
	if(isset($_REQUEST['key'])){
		if($po->getTokenKey() != $_REQUEST['key']){
			header("Location: index.php");
		}
	}else{
		header("Location: index.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="<?= $root ?>css/bootstrap.min.css" rel="stylesheet">
      <link rel="stylesheet" href="<?= $root ?>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="<?= $root ?>js/bootstrap.min.js"></script>
        	 <script src="<?= $root ?>js/bootstrap-combobox.js"></script>
            <script type="text/javascript">
              $(document).ready(function(){
                //$('.combobox').bscombobox();
				<?php
				if(isset($_REQUEST['print']) && $_REQUEST['print'] == "true"){
				?>
				
				window.print();
				setTimeout(function(){
					if(confirm('Close?')){
						window.close();	
					}else{
						
					}
					
				}, 1000);
				
				<?php	
				}
				?>
				
              });
            </script>
 
      <![endif]-->
 	<link rel="stylesheet" href="<?= $root ?>css/styles.css"/>
	<style>
		#purchaseOrderItemsTable , #purchaseOrderItemsTable tr, #purchaseOrderItemsTable tr th, #purchaseOrderItemsTable tr td {
			border: solid;
			border-width: thin;
			border-color: black;
		}
		.purchaseOrderItemsTable , .purchaseOrderItemsTable tr, .purchaseOrderItemsTable tr th, .purchaseOrderItemsTable tr td {
			border: solid;
			border-width: thin;
			border-color: black;
		}
		.purchaseOrderItemsTable{
			width: 100%;
			
		}
	</style>
    </head>
    <body>
    <style>
		.table-bordered > tbody > tr > td, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > td, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > thead > tr > th {
    border: thin solid black !important;
}

	</style>
    <!-- Stack the columns on mobile by making one full-width and the other half-width -->
 <div class="container-fluid" style="margin-top: -50px;">
	
	<!---INSERT HEADER 12345-->
	<?= insertHeader() ?>
	
 	  
 	  	<?php
 	  		insertTableHeader();
 	  		$index = 0;
			$totalItems = 0;
			$invoiceTotal = 0;
			$itemsPerPage = 18;
			$page = 1;
			$totalPages = ceil(count($po->Items)/$itemsPerPage);
			
			foreach($po->Items as $key => $item){
				//var_dump($item);
				$currItem = new Item(null, $item->Line, $item->SKU);
			
 	  		
 	  	?>
 	  		<tr style="border: solid; border-width: thin; border-color: black;">
 	    			
 	        		<td style="text-overflow: ellipsis;" >
 	        			<span class="itemInfo"><b><?= $item->SKU ?></b></span>
					</td>
					<td>
 	        			<span class="itemInfo"><?= $item->Description . " " . $item->Notes ?>
						</span>
 	        			
					</td>
					<?php
					if(!$po->Confirmed){
					?>
					<td class="centered">
						<?= $item->OrderQty ?>
					</td>
					<?php
					}
					
					if($po->Confirmed && !$po->Completed){
			
					?>
					<td class="centered">
						<?= $item->ReceivedQty ?>
					</td>
					<?php
					}
					if($po->Confirmed && $po->Completed){
			
					?>
					<td class="centered">
						<?= $item->ConfirmedQty ?>
					</td>
					<?php
					}
					?>
					
					
 	        		<td style="text-align: right;">
 	        			<?php
						echo moneyFormat($item->Cost);
						?>
 	        		</td>
 	        		<?php
					if(!$po->Confirmed){
					?>
 	        		<td style="text-align: right;">
						<?php
							echo moneyFormat($item->Cost*$item->OrderQty);
							$poTotal+= ($item->Cost*$item->OrderQty);
						?>
 	        		</td>
					<?php }else{ ?>
					<td style="text-align: right;">
						<?php
							echo moneyFormat($item->Cost*$item->ConfirmedQty);
							$poConfirmedTotal+= ($item->Cost*$item->ConfirmedQty);
						?>
 	        		</td>
					<?php
					}
					?>
 	      	</tr>
 	      	<?php 
 	      	$index++;
					if($index%$itemsPerPage == 0){
						
						echo "</tbody></table>";
						
					?>
						<div class="col-xs-12">
							<br>
						<center style="font-size: 30px;"><b>Continued...</b></center>
							<br>
						</div>
			<?php
					
					
						echo "<span class='pull-right' style='position: relative; bottom: 5px;'><b>Page " . ($page) . " of " . $totalPages . "</b></span>";
						echo "<p style=\"page-break-before: always; margin-top: 0px;\"><br> </p>";
						$page++;
						insertHeader($page);
						insertTableHeader();
	
					}
 	      	}?>
			
				
			<tr style="border-bottom:  hidden;">
				<td colspan="<?= $po->Confirmed&&$po->Completed?"5":"4" ?>" style="text-align: right; border-bottom:  hidden; border-right: hidden; border-left: hidden;">
					<span style="font-weight: bold; text-align: right;">
						Freight
					</span>
				</td>
				<td style="text-align: right; font-weight: bold; border-bottom:  hidden; border-right: hidden;">
				<?php
				$poConfirmedTotal+=$po->Freight;
				$poTotal+=$po->Freight;
				echo moneyFormat($po->Freight); ?>
				
					
				</td>
 	      	</tr>
			<tr style="border-bottom:  hidden;">
				<td colspan="<?= $po->Confirmed&&$po->Completed?"5":"4" ?>" style="text-align: right; border-bottom:  hidden; border-right: hidden; border-left: hidden;">
					<span style="font-weight: bold; text-align: right;">
						Misc. Charges
						
					</span>
				</td>
				<td style="text-align: right; font-weight: bold; border-bottom:  hidden; border-right: hidden;">
				<?php
				$poConfirmedTotal+=$po->MiscCharges;
				$poTotal+=$po->MiscCharges;
				echo moneyFormat($po->MiscCharges);
				
				?>
					
					
				</td>
 	      	</tr>
 	      	<tr style="border-bottom:  hidden;">
				<td colspan="<?= $po->Confirmed&&$po->Completed?"5":"4" ?>" style="text-align: right; border-bottom:  hidden; border-right: hidden; border-left: hidden;">
					<span style="font-weight: bold; text-align: right;">
						Total
						
					</span>
				</td>
				<td style="text-align: right; font-weight: bold; border-bottom:  hidden; border-right: hidden;">
				<?php
				if($po->Confirmed){
				?>
				<?= moneyFormat($poConfirmedTotal); ?><br>
				
				<?php
				}else{
				?>
				<?= moneyFormat($poTotal); ?><br>
				<?php
				}
				?>
					
					
				</td>
 	      	</tr>
 	    </tbody>
 	  </table>
 	</div>
 	
 <style>
	.borderless {
		border: hidden;
		border-top: hidden;
		border-left-style: none;
		border-right-style: none;
		border-bottom-style: none;
		line-height: 1;
	}
	.borderless>td {
		line-height: 1;
	}
	
	.borderless td span {
		line-height: 1;
	}
 </style>
 <style type="text/css" media="print">
	body { margin: 0;  font-size: 1em }
    
        
 </style>
 </div>
        <div style="border: 1px solid black;" class="panel panel-default">
          <div class="panel-body">
            <span><b>Purchase Order Notes:</b></span><br />
            <?= $po->Notes ?>
			<br>
			
			
          </div>
        </div>
		
		<div style="border: 1px solid black;" class="panel panel-default">
          <div class="row">
			
		  </div>
		  
        </div>
		<?php
		
		
		?>
		<div class="row">
				
				
				
				
				
			</div>
		<div class="row">
			<center>
				<br>
				<span><big><b><?= $config["CompanySlogan"] ?></b></big></span>
			</center>
			
		  </div>
        
    <?php echo "<span class='pull-right' style='position: relative; bottom: 5px;'><b>Page " . ($page) . " of " . $totalPages . "</b></span>"; ?>  
    </body>
	
  </html>
  
  <?php
  
  function insertTableHeader(){
	global $invoice, $config;
	?>
	
	<table style="border: solid; border-width: thin; border-color: black;" id="purchaseOrderItemsTable" class="table table-condensed purchaseOrderItemsTable">
 	    <thead>
 	      <tr style="border: solid; border-width: thin; border-color: black;">
 	        
 	        <th style="width: 10%">Item</th>
			<th style="width: 50%">Description</th>
			<th style="width: 5%" class="centered">Ordered Qty</th>
			<?php
			if($po->Confirmed && !$po->Completed){
			
			?>
			<th class="centered">
				Shipped Qty
			</th>
			<?php
			}
			if($po->Confirmed && $po->Completed){
	
			?>
			<th class="centered">
			Received Qty
			</th>
			<?php
			}
			?>
					
	
			
 	        <th style="width: 5%; text-align: right;">Price</th>
 	        <th style="width: 5%; text-align: right;">Extension</th>
 	      </tr>
 	    </thead>
 	  	<tbody>
	
	<?php
  }
  function insertHeader($page = 1){
	global $po, $config;
	?>
	
	<h2><img src="/img/Logo_250.png"></h2>
	<div style="margin-left: 10px; margin-top: -10px; margin-bottom: 10px">
		<?= $config['PostalAddress1'] ?> &#x25cf; <?= $config['PostalCity'] ?>, <?= $config['PostalState'] ?> <?= $config['PostalZip'] ?><br>
		<?= $config['OrderEmail'] ?>
		
	</div>
 	<div class="row">
		<?php
		if($page == 1){
		?>
 	  <div class="col-xs-5">
 	  	
 	  	<p style="">
			<div style="font-size: 15px;"><b>Vendor: </b><br></div>
 	  		<div style="height: 100px; border: none; border-width: thin; font-size: 12px; padding-left: 5px;">
				<b><?= $po->Vendor->Name; ?></b><br>
				<?= $po->Vendor->PostalAddress1?$po->Vendor->PostalAddress1 . "<br>":"" ?>
				<?= $po->Vendor->PostalAddress2?$po->Vendor->PostalAddress2 . "<br>":"" ?>
				<?= $po->Vendor->PostalAddress3?$po->Vendor->PostalAddress3 . "<br>":"" ?>
				<?= $po->Vendor->PostalCity . "," . $po->Vendor->PostalState . " " . $po->Vendor->PostalZip ?><br>
				<?= $po->Vendor->PostalCountry ?><br>
				
			</div>
 	  </p>
 	  
 	  </div>
  	  <div class="col-xs-5">
		<p style="">
			<div style="font-size: 15px;"><b>Ship To: </b><br></div>
 	  		<div style="height: 100px; border: none; border-width: thin; font-size: 12px;  padding-left: 5px;">
				<b><?= $po->ShipToName; ?></b><br>
				<?= $po->ShipToAddress1?$po->ShipToAddress1 . "<br>":"" ?>
				<?= $po->ShipToAddress2?$po->ShipToAddress2 . "<br>":"" ?>
				<?= $po->ShipToCity . "," . $po->ShipToState . " " . $po->ShipToZip ?><br>
				<?= $config['CompanyPhone'] ?>
				
			</div>
		</p>
		
	  </div>
		<?php
		}else{
		?>
			<div class="col-xs-12">
				<br>
			<center style="font-size: 30px;"><b>Continued...</b></center>
				<br>
			</div>
		<?php
		}
		?>
 	  <div class="col-xs-2" style="text-align: center; float: right; position:  absolute; margin-top: -110px;width: 200px; right: 20px; ">
 		  <h2>
		  Purchase Order
		  <br>
		  <b><?= $po->PurchaseOrderID ?></b>
		  </h2>
 		  <p>
 		  	
 		  <b>Date: </b><?= date("m/d/Y",strtotime($po->PODate)) ?><br>
		  <?php
		  $createdBy = new Administrator($po->Username);
		  $createdBy = $createdBy->Name;
		  ?>
		  <b>Created By: </b><?= $createdBy ?><br>
 		  </p>
 	  </div>
	  <table style="border: solid; border-width: thin; border-color: black;" class="table table-condensed table-bordered centered">
		<thead>			
			<tr style="border: solid; border-width: thin; border-color: black;" class="centered">
				<th style="border: solid; border-width: thin; border-color: black;" class="centered">Our Account #</th>
				<th style="border: solid; border-width: thin; border-color: black;" class="centered">Shipping Terms</th>
				<th class="centered">Payment Terms</th>
				<th class="centered">Packages</th>
				<th class="centered">Delivery Date</th>
				
			</tr>
		</thead>
		<tbody>
			<tr style="border: solid; border-width: thin;" class="centered">
				<td><?= $po->Vendor->OurAccountNumber ?></td>
				<td><?= $po->ShippingTerms ?></td>
				<td><?= $po->PaymentTerms ?></td>
				<td><?= $po->Packages ?></td>
				<td><?= is_null($po->ExpectedDate)?"":date("m/d/Y", strtotime($po->ExpectedDate)) ?>
				</td>
				
			</tr>
		</tbody>
	  </table>
	</div>
	
	
	<?php
  }
  
  ?>
  