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


$fromDate = ($_REQUEST['from']);
$toDate = ($_REQUEST['to']);

 ?>
 
 <!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 type="text/javascript">
             $(document).ready(function(){
            
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="css/styles.css"/>
   </head>
   <body>
   
       <?= include("navbar.php"); ?>     

   
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
<div class="container " style="padding-top: 10px;">
	<div class="panel panel-default">
	  <div class="panel-heading">
	    <h3 class="panel-title">Payment History From: <?= date("m/d/Y", strtotime($fromDate)) ?> To: <?= date("m/d/Y", strtotime($toDate)) ?> - <?= $_SESSION['Name'] ?></h3>
	  </div>
	  <div class="panel-body">
	  	<table class="table table-hover">
	  		<thead>
	  			<tr>
	  				<th>Payment ID</th>
	  				<?= ($_SESSION['isCustomer'] != true) ? "<th>Customer</th>" : "" ?>
	  				<th>Payment Date/Time</th>
					<th>CPD Date</th>
					<?= ($_SESSION['isCustomer'] == true) ? "<th>Salesman</th>" : "" ?>
	  				<th>Total</th>
	  			</tr>
	  			
	  		</thead>
	    <?php 
	    if ($_SESSION['isCustomer']) {
			
			//$query = "SELECT * FROM `PAYMENT_HEAD` WHERE AccountNumber LIKE '" . $_SESSION['AccountNumber'] . "' AND (PaymentDate BETWEEN '$fromDate' AND DATE_ADD('$toDate',INTERVAL 1 DAY)) ORDER BY ID DESC";
			$query = "SELECT * FROM `PAYMENT_HEAD` WHERE AccountNumber LIKE '" . $_SESSION['AccountNumber'] . "' AND ((PaymentDate BETWEEN '$fromDate' AND DATE_ADD('$toDate',INTERVAL 1 DAY) AND CPD = false) OR (CPDDate BETWEEN '$fromDate' AND DATE_ADD('$toDate',INTERVAL 1 DAY))) ORDER BY ID DESC";
	    }else {
			$query = "SELECT * FROM `PAYMENT_HEAD` WHERE SalesmanID LIKE '" . $_SESSION['SalesmanID'] . "' AND (PaymentDate BETWEEN '$fromDate' AND DATE_ADD('$toDate',INTERVAL 1 DAY)) ORDER BY ID DESC";
	    	//$query = "SELECT * FROM `PAYMENT_HEAD` WHERE SalesmanID LIKE '" . $_SESSION['SalesmanID'] . "' AND ((PaymentDate BETWEEN '$fromDate' AND DATE_ADD('$toDate',INTERVAL 1 DAY) AND CPD = false) OR (CPDDate BETWEEN '$fromDate' AND DATE_ADD('$toDate',INTERVAL 1 DAY))) ORDER BY ID DESC";
	    }
		
	   	$result = mysqli_query($con, $query);
		$total = 0;
		$totalCash = 0;
		$totalCheck = 0;
		$totalCPD = 0;
	   	while ($row = mysqli_fetch_array($result)) {
	   		$payment = new Payment($row['ID']);
			
			if($row['CPD']){
			  if(strtotime($payment->CPDDate) > time()){
				$totalCPD += $row['CheckAmount'];
			  }else{
				$total += $row['CheckAmount'];
				$totalCheck += $row['CheckAmount'];
			  }
			}else{
				$total += $row['CashAmount']+$row['CheckAmount'];
				$totalCash += $row['CashAmount'];
				$totalCheck += $row['CheckAmount'];
			}
	   	?>
	   		<tr onclick="window.open('mod.php?p=payment&a=receipt&id=<?= $row['ID'] ?>','_blank','width=400,height=1000,toolbar=no')">
	   			<td><?= $row['ID'] ?></td>
	   			<?= ($_SESSION['isCustomer'] != true) ? "<td>" . trim($payment->Customer->Name) . "</td>" : "" ?>
	   			<td>
				  <?= date("m/d/Y g:i a",strtotime($payment->PaymentDate)) ?>
				</td>
				<td>
				  <?php
					if($payment->CPD == true){
				  ?>
				  
					<img src="img/payment-methods-calendar-icon.png" style="width: 30px;"><i><?=  date("m/d/Y",strtotime($payment->CPDDate)) ?></i>
				  <?php
					}
				  ?>
				</td>
				<?= ($_SESSION['isCustomer'] == true) ? "<td>" . trim($payment->Salesman->Name) . "</td>" : "" ?>
	   			<td><?= moneyFormat($payment->CashAmount+$payment->CheckAmount) ?></td>
	   		</tr>
	   	<?php
	   	}
	     ?>
	     </table>
	  </div>
	  
	  <div class="panel-footer">
		<span style="text-align: left; float: left">
	  		<strong>Cash Total: </strong><i><?= moneyFormat($totalCash) ?></i><br />
	  		<strong>Checks Total: </strong><i><?= moneyFormat($totalCheck) ?></i><br />
			<strong>CPD Total: </strong><i><?= moneyFormat($totalCPD) ?></i><br />
	  	</span>	
	  	<span  style="float: right; text-align: right;">
	  		<strong>Total: </strong><i><?= moneyFormat($total) ?></i>
	  	</span>	
		<br /><br /><br />
		
	  </div>

</div>
       
     
   </body>
 </html>