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



$title = "Payment Details Search";

if($_SESSION['Username'] == "jcorrea" || $_SESSION['Username'] == "elisamuel40"){
	$showVoid = true;
}else{
	$showVoid = false;
}

 ?>

 <!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/excellentexport.js"></script>
           <script type="text/javascript">
             $(document).ready(function(){
       //        $('.grpline').css("width", "100px");
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="../css/styles.css"/>
   </head>
   <body>

       <?php 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 form-inline">
	  <a class="btn btn-success" role="button" style="float: right;" href="#" download="PaymentDetail<?= $title . "(" . date("m.d.Y",strtotime($fromDate)) . " - " . date("m.d.Y",strtotime($toDate)) . ")" ?>.xls" href="#" onclick="return ExcellentExport.excel(this, 'deposithistory', 'Deposit History');" data-toggle="tooltip" data-placement="left" title="Does not work with Internet Explorer">Export To Excel</a>

	  	<div style="margin-right: 10px; float: right;">
	  			<div class="input-group">
	  			      <div id="clearButton" style="display: none;" class="input-group-addon" onclick="clearFilter()">
	  			      	<span class="glyphicon glyphicon-remove" aria-hidden="true"></span></div>
	  			      <input type="text" class="form-control" name="filter" placeholder="" value="" />
	  			      <div class="input-group-addon" onclick="filter()">
	  			      	<span class="glyphicon glyphicon-search" aria-hidden="true"></span></div>
	  			    </div>
	  	</div>
	  		    <h3 class="panel-title">Payment Search: <?= $_REQUEST['pid'] ?></h3><br />

	  </div>
	  <div class="panel-body">
	  	<table class="table table-hover table-condensed" id="paymenthistory">
	  		<thead>
	  			<tr>
	  				<th>Payment</th>
					<th>Customer</th>
	  				<th>Salesperson</th>
					<th>Reconciliation</th>
					<th>Deposit</th>
					<th>CheckNumber</th>
	  				<th style="text-align: center;">Amount</th>
	  			</tr>

	  		</thead>
	    <?php

	    $query = "SELECT 	PAYMENT_HEAD.ID  PaymentID, PaymentDate,ReconcileID, ReconcileDate, AccountNumber, PAYMENT_HEAD.SalesmanID, PAYMENT_HEAD.Void ,ROUND(CashAmount+CheckAmount+TransferAmount,2) PaymentAmount, GROUP_CONCAT(CheckNumber) CheckNumber, DepositID, DepositDate, PAYMENT_DEPOSIT_HEAD.Notes
FROM PAYMENT_HEAD LEFT JOIN PAYMENT_CHECK ON(PAYMENT_HEAD.ID = PAYMENT_CHECK.`PaymentID`) LEFT JOIN  (PAYMENT_SALESMAN_RECONCILE_DETAIL JOIN PAYMENT_SALESMAN_RECONCILE_HEAD USING(ReconcileID)) ON(PAYMENT_HEAD.ID = PAYMENT_SALESMAN_RECONCILE_DETAIL.PaymentID) LEFT JOIN (PAYMENT_DEPOSIT_DETAIL JOIN PAYMENT_DEPOSIT_HEAD USING(DepositID)) ON(PAYMENT_HEAD.ID = PAYMENT_DEPOSIT_DETAIL.PaymentID) WHERE ROUND(CashAmount+CheckAmount+TransferAmount,2) != 0 AND PAYMENT_HEAD.ID = " . mysqli_real_escape_string($con, $_REQUEST['pid']) . " GROUP BY PaymentID";
//echo $query;
		$result = mysqli_query($con, $query);

	   	while ($row = mysqli_fetch_array($result)) {

			$customer = new Customer($row['AccountNumber']);
			$salesman = new Salesman($row['SalesmanID']);
	   	?>
	   		<tr>
	   			<td><a class="" href="./mod.php?p=payment&a=admin.viewpayment&id=<?= $row['PaymentID'] ?>" target="_blank"><?= $row['PaymentID'] ?></a> on <?= date("m/d/Y", strtotime( $row['PaymentDate'])) ?></td>
				<td><?= $customer->Name ?></td>
	   			<td><?= $salesman->Name ?></td>
				<td><a class="" href="./mod.php?p=payment&a=admin.viewreconciliation&id=<?= $row['ReconcileID'] ?>" target="_blank"><?= $row['ReconcileID'] ?></a> on <?= date("m/d/Y", strtotime( $row['ReconcileDate'])) ?></td>
				<td><a class="" href="./mod.php?p=payment&a=viewdeposit&id=<?= $row['DepositID'] ?>" target="_blank"><?= $row['DepositID'] ?></a> on <?= date("m/d/Y", strtotime( $row['DepositDate'])) ?>
						<br>
						<i><small><?= $row['Notes'] ?></small></i>
				</td>
				<td><?= $row['CheckNumber'] ?></td>
				<td style="text-align: right;" id="<?= $row['PaymentID'] ?>-Amount"><?= moneyFormat($row['PaymentAmount']) ?></td>
	   		</tr>
	   	<?php
	   	}
	     ?>
	     </table>
	  </div>
	  <div class="panel-footer" >

	  </div>
	</div>

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

$('[name=filter]').on('keypress', function(e) {
	if (e.which == 13) {
		filter();
	}

});
function clearFilter() {
	$('[name=filter]').val("");
	$('#clearButton').css('display', 'none');
	$('tr').css('display', '');

}
function filter() {
	var	name = $('[name=filter]').val();
	filterByName(name);
}
function filterByName(name) {
	$('#clearButton').css('display', '');
	var cols = $('#paymenthistory th').length;
	for (var i = 0; i < $('#paymenthistory td').length; i=i+cols) {

		var text = $($('td')[i]).text();

		var result = text.toUpperCase().indexOf(name.toUpperCase());
		if (text != name) {
			$($('tr')[i/cols+1]).css('display', 'none');
		}else {
			$($('tr')[i/cols+1]).css('display', '');
		}

	}
}

function promptVoid(paymentID){
	//clearVoidPaymentModal();
	$('#VoidPaymentID').val(paymentID);
	$('#VoidPaymentName').val($('#' + paymentID + '-Customer').text());
	$('#VoidPaymentAmount').val($('#' + paymentID + '-Amount').text());
	$('#VoidPaymentSalesman').val($('#' + paymentID + '-Salesman').text());
	$('#VoidPaymentModal').modal('show');
}
function clearVoidPaymentModal() {
	$('#VoidPaymentID').val('');
	$('#VoidPaymentName').val('');
	$('#VoidPaymentAmount').val('');
	$('#VoidPaymentSalesman').val('');
	$('#VoidPaymentReason').val('');
}
function voidPayment() {
	var data = new Object();
	data.pid = $('#VoidPaymentID').val();
	data.Reason = $('#VoidPaymentReason').val();

	$.post("./mod.php?p=payment&a=functions&f=voidPayment", data, function(returnData) {
		clearVoidPaymentModal();
		console.log(returnData);
		returnData = JSON.parse(returnData);
		$('#VoidPaymentModal').modal('hide');
		if(returnData.result == "true"){
			location.reload();
		}
	});
}
function voidDetails(id){
	$.get('./mod.php?p=payment&a=functions&f=voidPaymentDetails&pid=' + id, function(data){
			clearVoidDetails();
			data = JSON.parse(data);
			$('#VoidByUser').val(data.VoidUser);
			$('#VoidDate').val(data.VoidDate);
			$('#VoidReason').val(data.VoidReason);
			$('#VoidDetailsModal').modal('show');
	});
	//alert('Not Implemented');

}
function clearVoidDetails(){
	$('#VoidByUser').val('');
	$('#VoidDate').val('');
	$('#VoidReason').val('');
}
</script>

	<div class="modal fade" id="VoidPaymentModal">
	  <div class="modal-dialog">
	    <div class="modal-content">
	      <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="VoidPaymentTitle">Void Payment</h4>
	      </div>
	      <div class="modal-body">
			<div class="form">
				<input type="hidden" name="VoidPaymentID" id="VoidPaymentID" value="" />
				<div class="form-group">
					<label for="VoidPaymentName">Customer</label>
					<input type="text" class="form-control" readonly name="VoidPaymentName" id="VoidPaymentName" value="" />
				</div>
				<div class="form-group">
					<label for="VoidPaymentAmount">Payment Total</label>
					<input type="text" class="form-control" readonly name="VoidPaymentAmount" id="VoidPaymentAmount" value="" />
				</div>
				<div class="form-group">
					<label for="VoidPaymentSalesman">Salesman</label>
					<input type="text" class="form-control" readonly name="VoidPaymentSalesman" id="VoidPaymentSalesman" value="" />
				</div>
				<div class="form-group">
					<label for="VoidPaymentReason">Reason</label>
					<textarea class="form-control" rows="5" id="VoidPaymentReason"></textarea>
				</div>
			</div>

	      </div>
	      <div class="modal-footer">
			<small>WARNING: THIS CANNOT BE UNDONE!</small>
	        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
	        <button type="button" class="btn btn-success" onclick="voidPayment()">Submit Void</button>
	      </div>
	    </div><!-- /.modal-content -->
	  </div><!-- /.modal-dialog -->
	</div><!-- /.modal -->

	<!-- -->

	<div class="modal fade" id="VoidDetailsModal">
	  <div class="modal-dialog">
	    <div class="modal-content">
	      <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="VoidPaymentTitle">Void Details</h4>
	      </div>
	      <div class="modal-body">
			<div class="form">

				<div class="form-group">
					<label for="VoidByUser">Username</label>
					<input type="text" class="form-control" readonly name="VoidByUser" id="VoidByUser" value="" />
				</div>
				<div class="form-group">
					<label for="VoidDate">Date</label>
					<input type="text" class="form-control" readonly name="VoidDate" id="VoidDate" value="" />
				</div>
				<div class="form-group">
					<label for="VoidReason">Reason</label>
					<textarea class="form-control" readonly rows="5" id="VoidReason"></textarea>
				</div>
			</div>

	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

	      </div>
	    </div><!-- /.modal-content -->
	  </div><!-- /.modal-dialog -->
	</div><!-- /.modal -->

	<!-- -->

	<div class="modal fade" id="EditOrderModal">
	  <div class="modal-dialog">
	    <div class="modal-content">
	      <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="EditOrderTitle">Edit Order</h4>
	      </div>
	      <div class="modal-body">
	      	<input type="hidden" name="EditModalOrderID" id="EditModalOrderID" value="" />
			<table class="table table-hover table-condensed" id="orderhistory">
				<thead>
					<tr>
						<th>Group<br />Line</th>
						<th>SKU</th>
						<th>Description</th>

						<th>Qty</th>
						<th>Price</th>
						<th style="text-align: center;">Extension</th>
					</tr>

				</thead>
				<tbody id="EditOrderModalBody">
				</tbody>
				</table>

	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
	        <button type="button" class="btn btn-primary" id="saveButton" onclick="saveOrderInfo()">Save</button>
	      </div>
	    </div><!-- /.modal-content -->
	  </div><!-- /.modal-dialog -->
	</div><!-- /.modal -->


   </body>

 </html>
