<?php

include("connect.php");
include("functions.php");

$isAdmin = $_SESSION['isAdmin'];
//error_reporting(0);
$invoice = new Invoice($_REQUEST['id']);

if(!$invoice->Status){
	//header("Location: index.php");
	//die();
}
if(isset($_REQUEST['key'])){
	if($invoice->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();
				
              });
            </script>
 
      <![endif]-->
 	<link rel="stylesheet" href="<?= $root ?>css/styles.css"/>
    </head>
    <body>
 <div class="container-fluid" style="margin-top: -50px;">
	
	<h2><img src="/img/Logo_250.png"></h2>
	<div style="margin-left: 10px;">
		<?= $config['PostalAddress1'] . " " . $config['PostalCity'] . ", " . $config['PostalState'] . " " . $config['PostalZip'] ?><br>
		<?= $config['OrderEmail'] ?>
	</div>
 	<div class="row">
 	  <div class="col-xs-5">
 	  	
 	  	<p style="font-size: 17px;">
			<div style="font-size: 15px;"><b>Bill To: </b><br></div>
 	  		<div style="font-size: 15px;  padding-left: 5px;">
				<?= $invoice->BillToName; ?><br>
				<?= $invoice->BillToAddress1?$invoice->BillToAddress1 . "<br>":"" ?>
				<?= $invoice->BillToAddress2?$invoice->BillToAddress2 . "<br>":"" ?>
				<?= $invoice->BillToCity . "," . $invoice->BillToState . " " . $invoice->BillToZip ?><br>
				
			</div>
 	  </p>
 	  
 	  </div>
  	  <div class="col-xs-5">
		<p>
			<div style="font-size: 17px;"><b>Ship To: </b><br></div>
 	  		<div style="font-size: 15px;  padding-left: 5px;">
				<?= $invoice->ShipToName; ?><br>
				<?= $invoice->ShipToAddress1?$invoice->ShipToAddress1 . "<br>":"" ?>
				<?= $invoice->ShipToAddress2?$invoice->ShipToAddress2 . "<br>":"" ?>
				<?= $invoice->ShipToCity . "," . $invoice->ShipToState . " " . $invoice->ShipToZip ?><br>
				
			</div>
		</p>
		
	  </div>
 	  <div class="col-xs-2" style="float: right; position:  absolute; margin-top: -100px;width: 200px; right: 20px; ">
 		  <h2>
		  <?php
			if($invoice->Status){
				if($invoice->InvoiceTotal < 0){
					echo "Credit";
				}else{
					echo "Invoice";	
				}
				
			}else{
				echo "Invoice In Progress";
			}
		  ?></h2>
 		  <p>
 		  	<b>Invoice ID: </b><span style="font-style: italic;"><?= $invoice->InvoiceID ?></span><br>
 		  <b>Date: </b><?= date("m/d/Y",strtotime($invoice->InvoiceDate)) ?><br>
		  <b>Time: </b><?= date("g:i a",strtotime($invoice->InvoiceDate)) ?><br>
		  <b>Due Date: </b><?= $invoice->DueDate?date("m/d/Y", strtotime($invoice->DueDate)):"" ?>
 		  </p>
 	  </div>
	  <table class="table table-condensed centered">
		<thead>
			<tr class="centered">
				<th class="centered">Customer #</th>
				<th class="centered">Order ID</th>
				<th class="centered">Purchase Order</th>
				<th class="centered">Salesperson</th>
				<th class="centered">Terms</th>
				<th class="centered">Picker</th>
				<th class="centered">Delivery Method</th>
				<th class="centered">Package Count</th>
				
			</tr>
		</thead>
		<tbody>
			<tr class="centered">
				<td><?= $invoice->AccountNumber ?></td>
				<td><?= $invoice->OrderID ?></td>
				<td><?= $invoice->PONumber ?></td>
				<td><?= $invoice->Salesman->Name ?></td>
				<td><?= $invoice->Terms == "0"?"COD":$invoice->Terms ?></td>
				<td><?= $invoice->PackingUser ?></td>
				<td><?= $invoice->DeliveryMethod ?></td>
				<td><?= $invoice->PackageCount ?></td>
				
			</tr>
		</tbody>
	  </table>
	  
 	  <table class="table table-condensed">
 	    <thead>
 	      <tr>
 	        
 	        <th style="width: 10%">Item</th>
			<th style="width: 50%">Description</th>
			<th style="width: 5%" class="centered">Ordered</th>
			<th style="width: 5%" class="centered">Shipped</th>
 	        <th style="width: 5%; text-align: right;">Price</th>
 	        <th style="width: 5%; text-align: right;">Extension</th>
 	      </tr>
 	    </thead>
 	  	<tbody>
 	  	<?php
 	  		
 	  		$index = 0;
			$totalItems = 0;
			$invoiceTotal = 0;
			foreach($invoice->Items as $key => $item){
				$currItem = new Item($item->Group, $item->Line, $item->SKU);
			
 	  		
 	  	?>
 	  		<tr>
 	    			
 	        		<td style="text-overflow: ellipsis;" >
 	        			<span class="itemInfo"><b><?= $item->SKU ?></b></span>
					</td>
					<td>
 	        			<span class="itemInfo"><?= $item->Description . " " . $item->Notes ?>
						</span>
 	        			
					</td>
					<td class="centered">
						<?= $item->OrderQty ?>
					</td>
					<td class="centered">
 	        			<?= $item->ShippedQty; ?>
 	        		</td>
 	        		<td style="text-align: right;">
 	        			<?php
						echo moneyFormat($item->Price);
						?>
 	        		</td>
 	        		
 	        		<td style="text-align: right;">
						<?php
							echo moneyFormat($item->Price*$item->ShippedQty);
							$invoiceTotal+= ($item->Price*$item->ShippedQty);
						?>
 	        		</td>
 	      	</tr>
 	      	<?php 
 	      	$index++;	 
 	      	}?>
 	      	<tr>
 	      		<td colspan="5" style="text-align: right;">
					<span style="font-weight: bold; text-align: right;">
						Subtotal<br>
						<?= getSetting("Tax1Name")['value'] ?><br>
						<?= (getSetting("Tax2Name")['value']) ?><br>
						Total
						
					</span>
				</td>
				<td style="text-align: right;">
					<?= moneyFormat($invoice->InvoiceSubtotal); ?><br>
					<?= moneyFormat($invoice->Tax1Total); ?><br>
					<?= moneyFormat($invoice->Tax2Total); ?><br>
					<?= moneyFormat($invoice->InvoiceTotal); ?>
				</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; background-image: none; font-size: 1em }
    
        
 </style>
 </div>
        <div class="panel panel-default">
          <div class="panel-body">
            <span><b>Invoice Notes:</b></span><br />
            <?= $invoice->Notes ?>
			<br>
			
			
          </div>
        </div>
		
		<div class="panel panel-default">
          <div class="row">
			<div class="col-xs-6 col-xs-offset-3">
				<br>
				<br>
				<b>Received By:</b>________________________________________________________
			</div>
			<div class="col-xs-3">
				<br>
				<br>
				<b>Date:</b>______________________________
			</div>
			<br>
		  </div>
		  
        </div>
		<div class="row">
			<center>
				<br>
				<span></span>
			</center>
			
		  </div>
    </body>
  </html>