<?php
//ini_set("display_errors", 1);
//error_reporting(E_ALL);
//include_once("connect.php");
//include_once("session.php");
	//die("e");
	$customer = new Customer($_REQUEST['an']);
	//substr(md5($this->Customer->AccountNumber . $config['SaltKey']),0,10);
	$key = substr(md5($customer->AccountNumber . $config['SaltKey']),0,10);
	if(isset($_REQUEST['master'])){
		die($key);
	}
	if($_REQUEST['key'] != $key && !$isAdmin){
		header("Location: index.php");
	}
	
	//var_dump($_SESSION);
//die("d");

?>
<!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'] ?> -  Account Statement</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/functions.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>
		.tooltip-inner {
			white-space:pre-wrap;
		}
		.rightAlign{
			text-align: right;
		}
		@media print {
			
			a[href]:after {
			  content: none;
			}
			
		}
		@media screen{
			body{
				background-color: #666666;
			}
			.printpage, div.container {
				margin-left:auto;
				margin-right:auto;
				margin-top:5px;
				background-color:white;
				width:8.5in !important;
			}
			.printpage{
				min-height: 11in;
			}
		}
		
	</style>
   </head>
   <body >
	<div class="printpage">
       <?php
		//include("navbar.php");
		//$customer = new Customer($_REQUEST['an']);
	   ?>     
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
     <div class="container " style="margin-top: -10px;">
		<img src="/img/<?= $config['Logo'] ?>"><br>
		<?= $config['PostalAddress1'] ?><br>
		<?= $config['PostalAddress2']?($config['PostalAddress2'] . "<br>"):"" ?>
		<?= $config['PostalCity'] . ", " . $config['PostalState'] . " " . $config['PostalZip']  ?>
		<a class="btn btn-primary hidden-print pull-right" onclick="window.print();" href="#">Print</a>
	</div>
     <div class="container page-header">
		
		
		<div class="row">
			<div class="col-xs-6">
				<b><?= $customer->Name ?></b><br>
				<?= $customer->Address1 ?>
				<?= $customer->Address2?("<br>" . $customer->Address2):"" ?>
				<br><?= $customer->City . ", " .  $customer->State . " " . $customer->ZipCode ?>
			</div>
			<div class="col-xs-6 rightAlign">
				<b>Account Number: </b>	<?= $customer->AccountNumber ?><br>
				<b>Statement Date: </b>	<?= date("m/d/Y"); ?><br>
				<b>Salesperson: </b> <?= $customer->Salesman->FullName ?>
			</div>
		
		</div>
		<div class="row">
			<center>
				<h3 class="">ACCOUNT STATEMENT</h3>
			</center>
		</div>
    </div>
 
     <?php
	 
     $transactions = $customer->getStatement();
     
     
	 if(isset($_REQUEST['prueba'])){
		
		var_dump($customer);
	 }
     ?>
    <div class="container row">
        <div class="col-md-12">
            <span><h4>Transactions Detail</h4></span>

    <table class="table table-condensed dataTable" id="statementTable">

   <thead id="">
     <tr>
       <th style="width: 17%">Trx ID</th>
       <th style="width: 20%;">Trx Date</th>
	   <th style="width: 10%;">Days</th>
       <th style="width: 10%;">Type</th>
	   <th style="width: 18%;">Reference</th>
	   
	   <th style="width: 12%;">Charges</th>
	   <th style="width: 12%;">Credits</th>
	   <th style="width: 12%;">Balance</th>
       
	  
     </tr>
   </thead>

	<tbody id="statementTableBody">
	<?php 
	$lineCount = 0;
	$totalBalance = 0;
    foreach($transactions as $transaction){
        $debitAmount = 0;
		$creditAmount = 0;
		$days = "";
		$totalBalance += $transaction['Balance'];
		if($transaction['Type'] == "Invoice"){
			$invoice = new Invoice($transaction['TransactionID']);
			$debitAmount = $invoice->InvoiceTotal;
			$days = $transaction['Days'];
		}
		if($transaction['Type'] == "Credit"){
			$invoice = new Invoice($transaction['TransactionID']);
			$creditAmount = $invoice->InvoiceTotal*-1;
			$days = $transaction['Days'];
		}
		if($transaction['Type'] == "Payment"){
			//$payment = new Payment($transaction['TransactionID']);
			$creditAmount = $transaction['PaymentAmount'];
		}
		
    ?>
         <tr data-line="<?= $lineCount ?>" class="paymentItem" id="row-<?= $lineCount ?>">
			<td>
				<span title="" data-remarks="" data-line="<?= $lineCount ?>" class="" id="TransactionID-<?= $lineCount ?>"><?= $transaction['TransactionID'] ?></a></span>
			</td>
			<td>
				<span data-line="<?= $lineCount ?>" class="" id="TransactionDate-<?= $lineCount ?>"><?= date("m/d/Y", strtotime($transaction['TransactionDate'])) ?></span>
			</td>
			<td>
				<span><?= $days ?></span>
			</td>
			<td>
				<span data-line="<?= $lineCount ?>" class="" id="TransactionType-<?= $lineCount ?>"><?= $transaction['Type'] ?></span>
			</td>
			
			<td> 				
				<span data-line="<?= $lineCount ?>" class="" id="TransactionReference-<?= $lineCount ?>"><?= $transaction['Reference'] ?></span>
			</td>
			<td> 				
				<span data-line="<?= $lineCount ?>" class="" id="TransactionDebit-<?= $lineCount ?>"><?= $debitAmount?moneyFormat($debitAmount):"" ?></span>
			</td>
            
			<td> 				
				<span data-line="<?= $lineCount ?>" class="" id="TransactionCredit-<?= $lineCount ?>"><?= $creditAmount?moneyFormat($creditAmount):"" ?></span>
			</td>
			<td class="rightAlign"> 				
				<span data-line="<?= $lineCount ?>" class="" id="TransactionCredit-<?= $lineCount ?>"><?= !is_null($transaction['Balance'])?moneyFormat($transaction['Balance']):"" ?></span>
			</td>
			
			
		</tr>
    <?php  
		$lineCount++;
	}
	 ?>
	 <tr style="font-weight: bold;">
		<td colspan="5"></td>
		<td  class="rightAlign" colspan="2">Total Due</td>
		<td  class="rightAlign"><?= moneyFormat($totalBalance) ?> </td>
	 </tr>
	</tbody>
	</table>
	<style>
		#summaryTable {
			border: solid 1px;
		}
		#summaryTable, #summaryTable > thead > tr >  th{
			text-align: center;
		}
	</style>
	<?php
	 $query = "SELECT SUM(0To30) 0To30, SUM(31To60) 31To60, SUM(61To90) 61To90,SUM(Over90) Over90 FROM ACCOUNTS_RECEIVABLE_AGING WHERE Balance != 0 AND AccountNumber LIKE '{$customer->AccountNumber}' GROUP BY AccountNumber";
	 $result = mysqli_query($con, $query);
	 $row = mysqli_fetch_array($result);
	?>
	<table id="summaryTable" style="text-align: center;" class="table table-condensed">
		<thead>
			<tr>
				<th>0-30 days</th>
				<th>31-60 days</th>
				<th>61-90 days</th>
				<th>Over 90 days</th>
				<th>Total</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td><?= moneyFormat($row['0To30']) ?></td>
				<td><?= moneyFormat($row['31To60']) ?></td>
				<td><?= moneyFormat($row['61To90']) ?></td>
				<td><?= moneyFormat($row['Over90']) ?></td>
				<td><?= moneyFormat($totalBalance) ?></td>
			</tr>
		</tbody>
	</table>
    <!--
	<input type="button" class="btn btn-primary" id="addNewInvoiceButton" value="Add New Invoice"/>
	-->
	<?php
	
	if(($row['61To90'] + $row['Over90']) > 0 && $totalBalance > 0){
		
	?>
    <h4>*THIS ACCOUNT HAS INVOICES WITH MORE THAN 60 DAYS, PLEASE MAKE A PAYMENT AS SOON AS POSIBLE TO PLACE YOUR ACCOUNT BACK ON THE AGREED TERMS.</h4>
     <?php
	 }
	 ?>
    </div>
    <hr />
 <script type="text/javascript">
 	
 
    
    $(document).ready(function () {
		
    });
	
 </script>
</div>
   </body>
 </html>
 <?php
 
 ?>