<?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>
				<h4 class="">Overdue Invoices</h4>
			</center>
		</div>
    </div>

     <?php




	 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%;">Amount</th>
	   <th style="width: 12%;">Payment</th>
	   <th style="width: 12%;">Balance</th>


     </tr>
   </thead>

	<tbody id="statementTableBody">
	<?php
	$lineCount = 0;
	$totalBalance = 0;
	$query = "SELECT `ACCOUNTS_RECEIVABLE_AGING_DELIVERY_CACHE`.*, DueDate, InvoiceDate, DATEDIFF(NOW(), DueDate)-1 DueDays, IF(Balance>0,\"Invoice\", \"Credit\") `Type`, PONumber Reference  FROM ACCOUNTS_RECEIVABLE_AGING_DELIVERY_CACHE JOIN INVOICE_HEAD USING(InvoiceID, AccountNumber) WHERE ACCOUNTS_RECEIVABLE_AGING_DELIVERY_CACHE.AccountNumber = '{$customer->AccountNumber}' AND Balance != 0 AND NOW() > DueDate";
	$result = mysqli_query($con, $query);
	$transactions = mysqli_fetch_all($result,MYSQLI_ASSOC);
    foreach($transactions as $transaction){
        $debitAmount = 0;
		$creditAmount = 0;
		$days = "";
		$totalBalance += $transaction['Balance'];
		$invoice = new Invoice($transaction['InvoiceID']);
		$days = $transaction['ElapsedDays'];
		if($transaction['Type'] == "Invoice"){

			$debitAmount = $invoice->InvoiceTotal;

		}
		if($transaction['Type'] == "Credit"){

			$debitAmount = $invoice->InvoiceTotal*-1;

		}

		$creditAmount = $transaction['Payment'];



    ?>
         <tr data-line="<?= $lineCount ?>" class="paymentItem" id="row-<?= $lineCount ?>">
			<td>
				<span title="" data-remarks="" data-line="<?= $lineCount ?>" class="" id="TransactionID-<?= $lineCount ?>"><?= $transaction['InvoiceID'] ?></a></span>
			</td>
			<td>
				<span data-line="<?= $lineCount ?>" class="" id="TransactionDate-<?= $lineCount ?>"><?= date("m/d/Y", strtotime($transaction['InvoiceDate'])) ?></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 Overdue</td>
		<td  class="rightAlign"><?= moneyFormat($totalBalance) ?> </td>
	 </tr>
	</tbody>
	</table>
	<style>
		#summaryTable {
			border: solid 1px;
		}
		#summaryTable, #summaryTable > thead > tr >  th{
			text-align: center;
		}
	</style>

    <!--
	<input type="button" class="btn btn-primary" id="addNewInvoiceButton" value="Add New Invoice"/>
	-->

    </div>
    <hr />
 <script type="text/javascript">



    $(document).ready(function () {

    });

 </script>
</div>
   </body>
 </html>
 <?php

 ?>
