<?php

include_once("connect.php");
include_once("session.php");

$isCustomer = $_SESSION['isCustomer'];
if ($isCustomer) {
	
	$customer = new Customer($_SESSION['AccountNumber']);
}else {
	header("Location: index.php");
	exit();
}

?>
<!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="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="padding-top: 10px;">
     <div class="page-header">
		
		<center>
			<h2 class="visible-print-block">WESTING PR, INC</h2>
		<h3 class="visible-print-block center">A G I N G</h3>
		</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();
     $credits = $customer->getCredits();
     
	 if(isset($_REQUEST['prueba'])){
		
		var_dump($customer);
	 }
     ?>
    <div class="container row">
        <div class="col-md-12">
            <span><h4>Facturas</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%;">Discount</th>
       <th style="width: 6%">IVU</th>
       <th style="width: 10%">Total</th>
	   
       
	  
     </tr>
   </thead>

	<tbody id="paymentTableBody">
	<?php 
	$dueDays = 30;
	$lineCount = 0;
	$totalDue = 0;
    foreach($invoices as $invoice){
        $days = ceil((time() - strtotime($invoice['InvoiceDate']))/(24*60*60));
		//$discount = ($days>45?0:round($invoice['Subtotal']*.05,2));
		$discount = 0;
		$invoiceTotal = $invoice['Subtotal']-$discount+$invoice['SalesTax'];
		$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['InvoiceDate']) + ($dueDays*24*60*60)) ?></span>
			</td>
			<td>
				<span data-line="<?= $lineCount ?>" class="" id="days-<?= $lineCount ?>"><?= $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 ?>"><?= $invoice['InvoiceID'] ?></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="<?= $invoice['Subtotal'] ?>" data-line="<?= $lineCount ?>" class="" id="invoiceSubtotal-<?= $lineCount ?>"><?= moneyFormat($invoice['Subtotal']) ?></span>
			</td>
            <td>
				<span data-value="<?= $discount ?>" data-line="<?= $lineCount ?>" class="" id="invoiceDiscount-<?= $lineCount ?>"><?= moneyFormat($discount) ?></span>
			</td>
			<td> 				
				<span data-value="<?= $invoice['SalesTax'] ?>" data-line="<?= $lineCount ?>" class="" id="invoiceTax-<?= $lineCount ?>"><?= moneyFormat($invoice['SalesTax']) ?></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"/>
	-->
	
    <!-- Creditos -->
    <span><h4>Cr&eacute;ditos</h4></span>
            <table class="table table-condensed">
   <thead>
     <tr>
       <th style="width: 10%">Credit Date</th>
       <th style="width: 10%;">Credit Number</th>
       <th style="width: 15%; min-width: 92px">Refrerence</th>
	   <th style="width: 10%;">Subtotal</th>
	   <th style="width: 10%;">Discount</th>
	   <th style="width: 10%;">IVU</th>
	   <th style="width: 10%;">Total</th>
     </tr>
   </thead>
	<tbody id="creditsTableBody">
	<?php 
	
	$lineCount = 0;
    foreach($credits as $credit){
		$creditSubtotal = $credit['Subtotal'];
		$creditDiscount = 0;
		$creditTax = $credit['SalesTax'];
		$creditTotal = $creditSubtotal-$creditDiscount+$creditTax;
		$totalDue+=$creditTotal;
		$totalCredit += $creditTotal;
    ?>
        <tr class="creditItem" data-line="<?= $lineCount ?>" id="row-<?= $lineCount ?>">
			<td>
				<span data-line="<?= $lineCount ?>" class="" id="creditDate-<?= $lineCount ?>"><?= date("m/d/Y", strtotime($credit['CreditDate'])) ?></span>
			</td>
			<td>
				<span data-line="<?= $lineCount ?>" class="" id="creditNumber-<?= $lineCount ?>"><?= $credit['CreditNumber'] ?></span>
			</td>
			<td>
				<span data-line="<?= $lineCount ?>" class="" id="creditReference-<?= $lineCount ?>"><?= $credit['Reference'] ?></span>
			</td>
			<td> 	
				<span data-line="<?= $lineCount ?>" data-value="<?= $creditSubtotal ?>" class="" id="creditSubtotal-<?= $lineCount ?>"><?= moneyFormat($creditSubtotal) ?></span>
			</td>
			<td> 	
				<span data-line="<?= $lineCount ?>" data-value="<?= $creditDiscount?>" class="" id="creditDiscount-<?= $lineCount ?>"><?= moneyFormat($creditDiscount) ?></span>
			</td>
			<td> 	
				<span data-line="<?= $lineCount ?>" data-value="<?= $creditTax?>" class="" id="creditTax-<?= $lineCount ?>"><?= moneyFormat($creditTax) ?></span>
			</td>
			<td> 	
				<span data-line="<?= $lineCount ?>" data-value="<?= $creditTotal?>" class="" id="creditTotal-<?= $lineCount ?>"><?= moneyFormat($creditTotal) ?></span>
			</td> 
			
		</tr>
    <?php  
		$lineCount++;
	}

	 ?>
	 <tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td><b><?= moneyFormat($totalCredit) ?></b></td>
	 </tr>
	</tbody>
	</table> 
        </div>
    <hr />
 <script type="text/javascript">
 	
 	function recalculate(returnData = false){
		
    }
    
    $(document).ready(function () {
		
    });
	
 </script>

   </body>
 </html>