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

$isAdmin = isset($_SESSION['isAdmin'])?$_SESSION['isAdmin']:false;
$payment = new Payment($_REQUEST['id']);
$payment->GetReminderData();
if (!$isAdmin) {
	if(isset($_REQUEST['key'])){
		if($payment->getTokenKey() != $_REQUEST['key']){
			echo $payment->getTokenKey();
		//	header("Location: index.php");
		}
	}else{
		header("Location: index.php");
	}
	
}

//die("ostia");


 ?>
 <!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();
				//window.print();
				/*
				setTimeout(function(){
					if(confirm('Close?')){
						window.close();	
					}else{
						
					}
					
				}, 1000);
				*/
              });
            </script>
 
      <![endif]-->
 	<link rel="stylesheet" href="<?= $root ?>css/styles.css"/>
    </head>
    <body>
    <style>
		@media print {
			body {
				font-size: 12pt;
			}
		}
	</style>
    <!-- Stack the columns on mobile by making one full-width and the other half-width -->
 <div class="container-fluid" style="margin-top: -50px;">
	
	<h2><img style="width: 175px;" src="/img/Logo_250.png"></h2>
	<div style="font-size: 10px; margin-left: 10px;">
	<?= $config["PostalAddess1"] ?> &#x25cf; <?= ($config["PostalAddess2"]!=""?($config["PostalAddess2"] . "&#x25cf;"):"") ?> <?= $config["PostalCity"] ?>,<?= $config["PostalState"] . " " . $config["PostalZip"] ?><br>
		<?= $config["OrderEmail"] ?>
	</div>
 	<div class="row">
 	  <div class="col-xs-5">
 	  	
 	  	<p style="">
			<div style=""><b>Customer</b><br></div>
 	  		<div style=" font-size: 12px;">
				<b><?= $payment->Customer->AccountNumber; ?></b><br>
				<b><?= $payment->Customer->Name; ?></b><br>
				<?= $payment->Customer->Address1?$payment->Customer->Address1 . "":"" ?>
				<?= $payment->Customer->Address2?$payment->Customer->Address2 . "<br>":"<br>" ?>
				<?= $payment->Customer->City . "," . $payment->Customer->State . " " . $payment->Customer->ZipCode ?><br>
				
			</div>
 	  </p>
 	  
 	  </div>
  	  <div class="col-xs-5">
		<p style="">
			
 	  		<div style="">
				
			
				
			</div>
		</p>
		
	  </div>
 	  <div class="col-xs-2" style="text-align: center; float: right; position:  absolute; margin-top: -110px;width: 200px; right: 20px; ">
 		  <h2>
		  <?php
			
				echo "Payment Reminder";
			
		  ?>
		  <br>
		  <b><?= $payment->PaymentID ?></b>
		  </h2>
 		  <p style="font-size: 12px;">
 		  	
 		  <b>Date: </b><?= date("m/d/Y",strtotime($payment->PaymentDate)) ?><br>
		  <b>Time: </b><?= date("g:i a",strtotime($payment->PaymentDate)) ?><br>
		  <b>Salesperson:</b> <?= $payment->Salesman->Name; ?>
 		  </p>
 	  </div>
	  <?php
		$hasInvoices = false;
		foreach($payment->Customer->getInvoices() as $invoice){
			if(ceil((time()-strtotime(($invoice['DueDate'])))/(60*60*24))<1){
				//continue;
			}
		$hasInvoices = true;
		}
		if($hasInvoices){
	  ?>
		  
 	  <table  class="table table-condensed" style="font-size: 12pt;">
 	    <thead>
 	      <tr>
 	        
 	        <th style="width: 10%">InvoiceID</th>
			<th style="width: 10%">Inv. Date</th>
			<th style="width: 10%">Due Date</th>
			<th style="width: 10%">Type</th>
			<th style="width: 5%; text-align: right;" class="">Subtotal</th>
			<th style="width: 5%; text-align: right;" class="">Tax</th>
			<th style="width: 5%; text-align: right;" class="">Total</th>
			<th style="width: 5%; text-align: right;" class="">Balance</th>
			<?php
				if($payment->TotalDiscount){
			?>
			<th style="width: 5%" class="centered">Discount</th>
			<?php
				}
			?>
 	
 	      </tr>
 	    </thead>
 	  	<tbody>
 	  	<?php
 	  		
 	  		
					$totalPastDue = 0;
					$totalSubtotal = 0;
						$totalTax = 0;
					foreach($payment->Customer->getInvoices() as $invoice){
						if(ceil((time()-strtotime(($invoice['DueDate'])))/(60*60*24))<1){
							//continue;
						
						}else{
							$totalPastDue += $invoice['Subtotal']+$invoice['Tax1Total']+$invoice['Tax2Total'];
						}
						
						$invoice = new Invoice($invoice['InvoiceID']);
						
						$totalSubtotal += $invoice->InvoiceSubtotal;
						$totalTax += $invoice->TotalTax;
					
				
			
 	  	?>
 	  		<tr>
 	    			
 	        		<td style="" >
 	        			<span class="itemInfo"><?= $invoice->InvoiceID ?>
						</span>
					</td>
					<td>
 	        			<span class="itemInfo"><?= date("m/d/Y" , strtotime($invoice->InvoiceDate) ) ?>
						
						</span>
 	        			
					</td>
					<td>
						<?php
						if(ceil((time()-strtotime(($invoice->DueDate)))/(60*60*24))<1){
							echo date("m/d/Y", strtotime($invoice->DueDate)) . " (Current)";
						}else{
							echo date("m/d/Y", strtotime($invoice->DueDate)) . " (" . ceil((time()-strtotime(($invoice->DueDate)))/(60*60*24)) . " days)";	
						}
						
						
						?>
					</td>
					<td>
						<?= ($invoice->InvoiceSubtotal < 0)?"Credit":"Invoice" ?>
					</td>
					<td style="text-align: right;" class="">
						<?= moneyFormat($invoice->InvoiceSubtotal) ?>
					</td>
					<td style="text-align: right;" class="">
						<?= moneyFormat($invoice->TotalTax) ?>
					</td>
					<td style="text-align: right;" class="">
						<?= moneyFormat($invoice->InvoiceSubtotal+$invoice->TotalTax) ?>
					</td>
					<td style="text-align: right;" class="">
						<?= moneyFormat($invoice->Subtotal+$invoice->TotalTax) ?>
					</td>
 	      	</tr>
 	      	<?php 
 	      	$index++;
			
 	      	}?>
 	      	<tr>
				<td colspan="2">
 	      		<td colspan="5" style="text-align: right;">
					<span style="font-weight: bold; text-align: right;">
						Subtotal<br>
						
						Total Tax<br>
						Total<br>
						Total Past Due
						
					</span>
				</td>
				<td style="text-align: right;">
					<?= moneyFormat($totalSubtotal); ?><br>
					
					
					<?= moneyFormat($totalTax); ?><br>
					
					<?= moneyFormat($totalSubtotal+$totalTax); ?><br>
					<?= moneyFormat($totalPastDue) ?>
				</td>
 	      	</tr>
 	    </tbody>
 	  </table>
	  <?php
		}else{
			
		}
	  ?>
 	</div>
 	<div class="row" style="font-size: 10pt;">
		<div class="col-xs-8 col-md-8">
            <div class="col-xs-12">
                
            </div>
    
        </div>
	</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: 1.5em }
    
        
 </style>
 </div>
        <div style="border: 1px solid black; font-size: 14px;" class="panel panel-default">
          <div class="panel-body">
            			
						<b>Payment Reminder Reason:</b>
						<?= $payment->Reminder->Reason ?>
			<br />
			<b>Notes:</b>
			<?= ($payment->Reminder->Notes) ?>
			<br />
          </div>
        </div>
		
		
      
    </body>
  </html>