<?php 
include("../connect.php");
include("../session.php");

$isAdmin = $_SESSION['isAdmin'];
$invoice = new Invoice($_REQUEST['id']);
if (!$isAdmin) {
	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>
            
				<?php
				if(isset($_REQUEST['print']) && $_REQUEST['print'] == "true"){
				?>
				<script type="text/javascript">
					$(document).ready(function(){
               
					window.print();
					setTimeout(function(){
						if(confirm('Close?')){
							window.close();	
						}else{
						
						}
					
					}, 1000);
					});
				</script>	
				<?php	
				}
				?>
				
              
 
      <![endif]-->
 	<link rel="stylesheet" href="<?= $root ?>css/styles.css"/>
	<style>
		
		.invoiceItemsTable thead tr, .invoiceItemsTable thead tr th{
			border: solid;
			border-color: black;
			border-width: 1px;
			
			
		}
		.invoiceItemsTable tbody tr, .invoiceItemsTable tbody tr td, .invoiceItemsTable{
			border: none;
			border-color: white;
			border-width: 0px;
			
			
		}
		.invoiceItemsTable{
			width: 100%;
			
		}
		.doubleHorizontalLine {
			display: block;
			margin-top: 0.5em;
			margin-bottom: 0.5em;
			margin-left: auto;
			margin-right: auto;
			border-top: medium double #333;
			border-width: 3px 0px 0px;
		}
		
	</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="row container-fluid" style="margin-top: -50px;">
	
	<!---INSERT HEADER 12345-->
	<?= insertHeader() ?>
	
 	  
 	  	<?php
 	  		insertTableHeader();
 	  		$index = 0;
			$totalItems = 0;
			$invoiceTotal = 0;
			if(count($invoice->Items) > 17){
				$itemsPerPage = 18;
			}elseif(count($invoice->Items) > 10 ){
				$itemsPerPage = count($invoice->Items);
			}else{
				$itemsPerPage = 12;
			}
			//$itemsPerPage = count($invoice->Items)>17?18:14;
			$page = 1;
			$totalPages = ceil(count($invoice->Items)/(count($invoice->Items) > 10 && count($invoice->Items) < 17?$itemsPerPage-1:$itemsPerPage));
			foreach($invoice->Items as $key => $item){
				$currItem = new Item($item->Group, $item->Line, $item->SKU);
			
 	  		
 	  	?>
 	  		<tr style="">
 	    			
 	        		<td style="text-overflow: ellipsis;" >
 	        			<span class="itemInfo"><b><?= $item->SKU ?></b></span>
					</td>
					<td>
 	        			<span class="itemInfo"><?= $item->Description . " " . $item->Notes ?>
						<?php
						if($item->ReferenceType == "OriginalInvoice"){
							echo "<br>" . "<b>Originally from invoice: " . $item->Reference . "</b>";
						}
						?>
						</span>
 	        			
					</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++;
					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="4" style="text-align: right; border-bottom:  hidden; border-right: hidden; border-left: hidden;">
					<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; font-weight: bold; border-bottom:  hidden; border-right: hidden;">
					<?= 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;  font-size: 1em }
    
        
 </style>
 </div>
        <div style="border: 1px solid black;" class="panel panel-default">
          <div class="panel-body">
            <span><b>Invoice Notes:</b></span><br />
            <?= $invoice->Notes ?>
			<br>
			
			
          </div>
        </div>
		
		<div style="border: none;" class="panel panel-default">
          <div class="row">
			<div class="col-xs-6">
				<br>
				<br>
				<b>Received By:</b>________________________________________________________
			</div>
			<div class="col-xs-3">
				<br>
				<br>
				<b>Date:</b>______________________________
			</div>
			<br>
		  </div>
		  
        </div>
		<?php
		
		
		?>
		<div class="row">
				
				
				<!--
				<div class="col-xs-12" id="crateContainer">
					<span><b>Delivery Crates:</b></span><br />
					<?php
						foreach($invoice->getCrates() as $value){
							
						
					?>
					<div class="col-xs-2 shipcontainer" data-container="<?= $value['CrateNumber'] ?>">
						<img src="/img/shipcrate.png"  style="width: 100px;"/>
						<div class="shipcrate_number">#<?= $value['CrateNumber'] ?>
						</div>
					</div>
					<?php
						}
					?>
					
					
				</div>
				-->
				<style>
					.shipcontainer {
						position: relative;
						text-align: center;
						color: red;
					}
					.shipcrate_number{
						
						font-size: 22px;
						
					}
				</style>
				
			</div>
		<div class="row">
			<center>
				<br>
				<span><big><b><?= $config['InvoiceSlogan']?></b></big></span><br>
				<span><big><b><?= $config['InvoiceDisclaimer']?></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;
	?>
	
	<table style="" id="invoiceItemsTable" class="table table-condensed invoiceItemsTable">
 	    <thead style="border: solid; border-width: 1px; border-color: black;">
 	      <tr style="">
 	        
 	        <th style="width: 10%">Item</th>
			<th style="width: 55%">Description</th>
			
			<th style="width: 10%" class="centered">Quantity</th>
 	        <th style="width: 10%; text-align: right;">Unit Price</th>
 	        <th style="width: 10%; text-align: right;">Net Amount</th>
 	      </tr>
 	    </thead>
 	  	<tbody>
	
	<?php
  }
  function insertHeader($page = 1){
	global $invoice, $config;
	?>
	<div class="row">
		<div class="col-xs-2">
			<img style="width: 150px;" src="/img/<?= $config['InvoiceLogo'] ?>">
		</div>
		<div class="col-xs-6">
			<div style="font-size: 1.3em; margin-left: 10px; margin-top: 5px; margin-bottom: 10px">
				<b><?= $config['CompanyName'] ?></b><br>
				<?= $config['PostalAddress1'] . "<br>" . $config['PostalCity'] . ", " . $config['PostalState'] . " " . $config['PostalZip'] ?><br>
				<?= $config['PostalCountry'] ?><br>
				<?= $config['ConsumerURL'] ?>
				
				
			</div>
		</div>
		<div class="col-xs-4"  style="">
			<div style="width: 100%; color: gray; font-size: 2.5em; font-weight: bolder;" class="row">
				<?php
					if($invoice->Status){
						if($invoice->InvoiceTotal < 0){
							$docType = "Credit";
							$initials = "CR";
							echo "CREDIT";
						}else{
							$initials = "INV";
							$docType = "Invoice";
							echo "SALES INVOICE";	
						}
						
					}else{
						$initials = "IIP";
						$docType = "Invoice";
						echo "Invoice In Progress";
					}
				?>
			</div>
			<div class="row" style=" width: 100%;">
				<table style=" width: 100%;">
					<tbody>
						<tr>
							<td><b>Date</b></td>
							<td><?= date("m/d/Y",strtotime($invoice->InvoiceDate)) ?></td>
						</tr>
						<tr>
							<td><b>Total</b></td>
							<td><?= moneyFormat($invoice->InvoiceTotal) ?></td>
						</tr>
						<tr>
							<td><b>Sales <?= $docType ?> Number</b></td>
							<td><?= $invoice->InvoiceID ?></td>
						</tr>
					</tbody>
				</table>
			</div>
		  
 		  </p>
		</div>
	</div>
	
 	<div class="row">
		<hr class="doubleHorizontalLine">
		<?php
		if($page == 1){
		?>
 	  <div class="col-xs-5">
 	  	
 	  	
			<div class="pull-left" style="margin-right: 10px;font-size: 15px; height: 90px;"><b>Bill To: </b></div>
 	  		<div style="height: 75px; border: none; font-size: 12px; padding-left: 5px;">
				<b><?= $invoice->BillToName; ?></b><br>
				<?= $invoice->BillToAddress1?$invoice->BillToAddress1 . "<br>":"" ?>
				<?= $invoice->BillToAddress2?$invoice->BillToAddress2 . "<br>":"" ?>
				<?= $invoice->BillToCity . "," . $invoice->BillToState . " " . $invoice->BillToZip ?><br>
				
			</div>
 	  
 	  
 	  </div>
  	  <div class="col-xs-5">
		
			<div class="pull-left" style="margin-right: 10px; font-size: 15px; height: 90px;"><b>Ship To: </b><br></div>
 	  		<div style="height: 75px; border: none; font-size: 12px;  padding-left: 5px;">
				<b><?= $invoice->ShipToName; ?></b><br>
				<?= $invoice->ShipToAddress1?$invoice->ShipToAddress1 . "<br>":"" ?>
				<?= $invoice->ShipToAddress2?$invoice->ShipToAddress2 . "<br>":"" ?>
				<?= $invoice->ShipToCity . "," . $invoice->ShipToState . " " . $invoice->ShipToZip ?><br>
				<?= $invoice->Customer->Phone!=""?$invoice->Customer->Phone:""; ?>
				
			</div>
		
		
	  </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">
 		  
 	  </div>
	  </div>
	<div class="row">
	  <hr class="doubleHorizontalLine">
	  <table style="width: 100%;" class="centered">
		<thead>			
			<tr style="" class="centered">
				<th style="" class="centered">Customer #</th>
				<th style="" class="centered">Order ID</th>
				<th class="centered">Purchase Order</th>
				<th class="centered">Salesperson</th>
				<th class="centered">Terms</th>
				<th class="centered">Due Date</th>
				<!--<th class="centered">Delivery Method</th>-->
				<th class="centered">Package Count</th>
				
			</tr>
		</thead>
		<tbody>
			<tr style="" class="centered">
				<td><?= $invoice->AccountNumber ?></td>
				<td><?= $invoice->OrderID ?></td>
				<td><?= $invoice->PONumber ?></td>
				<td><?= $invoice->Salesman->Name ?></td>
				<td>
				<?php
					if($invoice->InvoiceTotal < 0){
							echo "CREDIT";
					}else if($invoice->Terms == "0"){
						echo "COD";
					}else if($invoice->Terms=="-1"){
						echo "Cash";
					}else {
						echo $invoice->Terms . " days";
					}
				?>
				
				<td><?= date("m/d/Y", strtotime($invoice->DueDate)) ?></td>
				<!--<td><?= $invoice->DeliveryMethod ?></td>-->
				<td><?= $invoice->PackageCount ?></td>
				
			</tr>
		</tbody>
	  </table>
	</div>
	<br>
	
	<?php
  }
  
  ?>
  