<?php
include_once("functions.php");


	$customer = new Customer($_REQUEST['an']);


?>
<!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'] ?> Customer Aging</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();
               $('.toggleCheckbox').checkboxpicker();
       //        $('.grpline').css("width", "100px");

             });
           </script>
     <![endif]-->
	<link rel="stylesheet" href="/css/styles.css"/>
	<style>
		.tooltip-inner {
			white-space:pre-wrap;
		}
	</style>
   </head>
   <body>
       <?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="">
     <div class="page-header">

		<img src="../img/<?= $config['Logo'] ?>">
		<center>

		<h3 class=" center">A G I N G</h3>
		As of <?= date("m/d/Y") ?>
		</center>
		<a class="btn btn-primary hidden-print pull-right" onclick="window.print();" href="#">Print</a>
       <h3><?= $customer->AccountNumber . " - " . $customer->Name . " <small>(" . $customer->City . ")</small>	" ?></h3>
     </div>
 </div>
     <?php

     $invoices = $customer->getInvoices();


	 if(isset($_REQUEST['prueba'])){

		var_dump($customer);
	 }
     ?>
    <div class="container row">
        <div class="col-md-12">
            <span><h4>Invoice Detail</h4></span>

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

   <thead id="">
     <tr>
       <th style="width: 10%">Invoice Date</th>
       <th style="width: 10%;">Due Date</th>
       <th style="width: 6%;">Days</th>
	   <th style="width: 15%;">Invoice Number</th>

	   <th style="width: 10%;">Subtotal</th>
	   <th style="width: 10%;">Paid</th>
       <th style="width: 6%">IVU</th>
       <th style="width: 10%">Balance</th>



     </tr>
   </thead>

	<tbody id="paymentTableBody">
	<?php
	$dueDays = 30;
	$lineCount = 0;
	$totalDue = 0;
    foreach($invoices as $invoice){
			$dateDays = $invoice['DeliveryDate'];
			if(!$dateDays){
				$dateDays = $invoice['InvoiceDate'];
			}
			$days = round((time()-14400 - strtotime($dateDays))/(24*60*60),0);

		//$discount = ($days>45?0:round($invoice['Subtotal']*.05,2));
		$discount = 0;
		$invoiceObj = new Invoice($invoice['InvoiceID']);
		$invSubtotal = $invoiceObj->InvoiceSubtotal;
		$invPaid = $invoiceObj->InvoiceSubtotal-$invoice['Subtotal'];
		$invoiceTotal = $invoice['Subtotal']+$invoice['Tax1Total']+$invoice['Tax2Total'];
		//var_dump($invoiceTotal);
		$totalDue+= $invoiceTotal;
		$remarks = strtoupper($invoice['Reference']);
		$lockNet = false;
		$lockBC = false;
		if(stripos($remarks,"KYB") === false && stripos($remarks,"DHI") === false && stripos($remarks,"NIP") === false){
			$noDiscount = false;
		}else{
			$noDiscount = true;
		}
		if(stripos($remarks,"SO/") === false && stripos($remarks,"SO-") === false && stripos($remarks,"NIP") === false){

		}else{
			$lockNet = true;
		}
		if(stripos($remarks,"COD") === false && stripos($remarks,"C O D") === false && stripos($remarks,"C  O  D") === false && $invoice['Terms'] != "COD"){
			$cod = false;
		}else{
			$cod = true;
		}
		if(is_numeric(substr($invoice['InvoiceID'],0,1)) === true){

		}else{
			//BANK CHARGES
			$lockBC = true;
		}

    ?>
        <tr data-line="<?= $lineCount ?>" class="paymentItem" id="row-<?= $lineCount ?>">
			<td>
				<span data-value="<?= $invoice['InvoiceDate'] ?>" data-line="<?= $lineCount ?>" class="" id="invoiceDate-<?= $lineCount ?>"><?= date("m/d/Y", strtotime($invoice['InvoiceDate'])) ?></span>
			</td>
			<td>
				<span data-line="<?= $lineCount ?>" class="" id="dueDate-<?= $lineCount ?>"><?= date("m/d/Y", strtotime($invoice['DueDate'])) ?></span>
			</td>
			<td>
				<span data-line="<?= $lineCount ?>" class="" id="days-<?= $lineCount ?>"><?= $days<0?0:$days ?></span>
			</td>
			<td>
				<span title="<?= $remarks ?>" data-remarks="<?= $remarks ?>" data-line="<?= $lineCount ?>" <?= ($noDiscount)?'style="background-color: #fff200;" ':"" ?> <?= ($lockNet)?'style="background-color: #00e600;" ':"" ?> <?= ($lockBC)?'style="background-color: #F88379;" ':"" ?> class="" id="invoiceNumber-<?= $lineCount ?>"><a target="_blank" href="mod.php?p=invoice&a=view&id=<?= $invoice['InvoiceID'] ?>"><?= $invoice['InvoiceID'] ?></a></span>
				<?php
				if(isset($invoice['Reasons'])){
					?>
				<a href="#" data-toggle="tooltip" title="<?= $invoice['User'] . " applied " . $invoice['Overpayment'] . "\n" . $invoice['Reasons'] ?>"><span class="badge">i</span></a><br>
				<?php }
				if($cod){
				?>
					<img src="img/cod_icon.jpg" style="width: 30px;"/>
				<?php
				} ?>
			</td>
			<td>
				<span data-value="<?= $invSubtotal ?>" data-line="<?= $lineCount ?>" class="" id="invoiceSubtotal-<?= $lineCount ?>"><?= moneyFormat($invSubtotal) ?></span>
			</td>
			<td>
				<span data-value="<?= $invPaid ?>" data-line="<?= $lineCount ?>" class="" id="invoicePaid-<?= $lineCount ?>"><?= moneyFormat($invPaid) ?></span>
			</td>

			<td>
				<span data-value="<?= ($invoice['Tax1Total']+invoice['Tax2Total']) ?>" data-line="<?= $lineCount ?>" class="" id="invoiceTax-<?= $lineCount ?>"><?= moneyFormat($invoice['Tax1Total']+invoice['Tax2Total']) ?></span>
			</td>
			<td>
				<span data-value="<?= $invoiceTotal ?>" data-line="<?= $lineCount ?>" class="" id="invoiceTotal-<?= $lineCount ?>"><?= moneyFormat($invoiceTotal) ?></span>
			</td>

		</tr>
    <?php
		$lineCount++;
	}
	 ?>
	 <tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td><b><?= moneyFormat($totalDue) ?></b></td>
	 </tr>
	</tbody>
	</table>

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



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

 	function recalculate(returnData = false){

    }

    $(document).ready(function () {

    });

 </script>

   </body>
 </html>
