<?php
	require('../config/dbcon.php');
	$customer_code  = trim($_REQUEST["customer_code"]);
?>
<?php
require('../config/dbcon.php');
    $query = "SELECT timeout from timeout_mst";
    $rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
    while ($row = mysqli_fetch_assoc($rs)) {
    	$timeout = $row['timeout'];
    }
    $query = "select screenwidth from width_mst";
    $rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
    while ($row = mysqli_fetch_assoc($rs)) {
    	$width = $row['screenwidth'];
    }
    $width = $width . "px";
    //$width = '2000';
session_start();
if(isset($_SESSION["user"]))
{
 if((time() - $_SESSION['last_time']) > $timeout) // Time in Seconds
 {
 header("location:../login/logout.php");
 }
 else
 {
 $_SESSION['last_time'] = time();
 //echo "<h1 align='center'>Welcome ".$_SESSION["user"]. "</h1>";
 //echo "<h3 align='center'>Automatic Logout after 1 minute of inactive</h3>";
 //echo "<p align='center'><a href='../login/logout.php'>Logout</a></p>";
 //echo "<p align='center'><a href='../main/welcome.php'>Welcome1</a></p>";
 }
}
else
{
 header('Location:../login/index.php');
}
?>
<html>
<head>
	<title>Customer Outstanding</title>
</head>
<script src='../js/jquery-3.1.1.min.js' type='text/javascript'></script>
<script src='../js/jquery-ui.min.js' type='text/javascript'></script>
<link href="../css/style.css" rel="stylesheet" />
<link href="../css/style1.css" rel="stylesheet" />
<link href='../css/jquery-ui.min.css' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="../css/all.css">
<link href="../css/bootstrap.min.css" rel="stylesheet" />
<link href="../css/font-awesome.min.css" rel="stylesheet" />
<link href="../css/styleg.css" rel="stylesheet" />
<script type="text/javascript" src="../js/jquery.placeholder.label.js"></script>
    <body style="width: <?php echo $width; ?>; align="center">
        <div style="width: <?php echo $width; ?>; align="center">
        <form method="POST" id = "form" action="update">
            <nav class="navbar navbar-expand-lg navbar-dark bg-primary" style="width: <?php echo $width; ?>; height: 62px; margin-top: -20px; background-color: #007bff; font-size:24px;">
                <input style = "background-color: #007bff; font-size:24px; width:100px; height : 55px; margin-top : px;" type="button" onclick="location.href='../main/index';" value="Home">
                <input style = "background-color: #007bff; font-size:24px; width:100px; height : 55px; margin-top : px;" type="button" onclick="location.href='index';" value="Back">
                <h1 style="color:white; font-size:24px; margin-top: -40px;" class="text-primary" align="center">Receipt ( Bank )</h1>
                <h1 style="color:white; font-size:24px; margin-top : -40px;margin-right :20px;" class="text-primary" id="demo2" align="right"></h1>
            </nav>
        	<div class="container" style="width: <?php echo $width; ?>; margin-left: 0px; margin-top: -00px;">
        	<center>
			<tr>
			    <?php
			        $curr_month = date('m');
			        if($curr_month >= 4 && $curr_month <= 12){
                		$year = date('y') . '-' . date('y', strtotime("+1 year"));
                	} else{
                		$year = date("y",strtotime("-1 year")) . '-' . date("y");
                	}
    			    $query1 = mysqli_query($connect, "select srno from control_mst  where description = '" ."RECEIPTS". "'") or die(mysqli_error());
    				while($fetch1 = mysqli_fetch_array($query1)){
    				$rec_number = 'RECB- '.$fetch1['srno'].'  '.'('.$year.')';
				    $srno = $fetch1['srno']; 
        			?>
			        <div class="col-xs-2">
    					<label style="font-size:18px;" class=" col-sm-2 control-label">Receipt_Number</label>
    					<input type="text" tabindex="-1" name="receipt_number1" id="receipt_number1" class="roundedCategory" value="<?php echo $rec_number?>"  readonly style="font-size:18px; margin-top: -6px; width:100%; height: 40px; font-weight:bold; color: #000066;" onfocus="GetCustCode(this)" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
    					<input type="hidden" name="receipt_number" id="receipt_number" value="<?php echo $rec_number?>" />
    					<input type="hidden" name="srno" id="srno" value="<?php echo $srno?>" />
    				</div>
    				<div class="col-xs-2">
    					<label style="font-size:18px;" class=" col-sm-2 control-label">Receipt_Date</label>
    					<input type="text" tabindex="-1" name="receipt_date" id="receipt_date" class="roundedCategory" value="<?php date_default_timezone_set('Asia/Kolkata'); echo date( 'd-m-Y H:i:s'); ?>"  style="font-size:18px; margin-top: -6px; width:100%; height: 40px; font-weight:bold; color: #000066;" value="" onfocus="GetCustCode(this)" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
    				</div>
        			<?php
    				}
    			?>
			    <?php
    			    $query1 = mysqli_query($connect, "select sum(total_inv_val),sum(rec_amount),sum(balance_amount),customer_code,customer_name,cust_place from invtotal_mst  where status = '" ."PENDING". "' and customer_code = '" .$customer_code . "'") or die(mysqli_error());
    				while($fetch1 = mysqli_fetch_array($query1)){
        			?>
    				<div class="col-xs-2">
    					<label style="font-size:18px;" class=" col-sm-2 control-label">customer_code</label>
    					<input type="text" tabindex="-1" name="customer_code" id="customer_code" class="roundedCategory" value="<?php echo $fetch1[3]?>"  readonly style="font-size:18px; margin-top: -6px; width:100%; height: 40px; font-weight:bold; color: #000066;" onfocus="GetCustCode(this)" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
    				</div>
    				<div class="col-xs-3">
    					<label style="font-size:18px;" class=" col-sm-2 control-label">customer_name</label>
    					<input type="text" tabindex="-1" name="customer_name" id="customer_name" class="roundedCategory" value="<?php echo $fetch1[4]?>"  readonly style="font-size:18px; margin-top: -6px; width:100%; height: 40px; font-weight:bold; color: #000066;" onfocus="GetCustCode(this)" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
    				</div>
    				<div class="col-xs-3">
    					<label style="font-size:18px;" class=" col-sm-2 control-label">customer_place</label>
    					<input type="text" tabindex="-1" name="cust_place" id="cust_place" class="roundedCategory" value="<?php echo $fetch1[5]?>"  readonly style="font-size:18px; margin-top: -6px; width:100%; height: 40px; font-weight:bold; color: #000066;" onfocus="GetCustCode(this)" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
    				</div>
			<?php
				}
			?>
			</tr>
			<br><br><br><br>
		    </center>
		    <center>
			<tr>
			    <?php
    			    $query1 = mysqli_query($connect, "select sum(balance_amount),customer_code,customer_name,cust_place from invtotal_mst  where status = '" ."PENDING". "' and customer_code = '" .$customer_code . "'") or die(mysqli_error());
    				while($fetch1 = mysqli_fetch_array($query1)){
        			?>
        			<div class="col-xs-2">
        			    <label style="font-size:18px;" class=" col-sm-2 control-label">acc_type</label>
        			    <input type="text" name="acc_type" id="acc_type" class="roundedCategory" style="font-size:18px; margin-top: -6px; width:100%; height: 40px; font-weight:bold; color: #000066;" placeholder="Type and Select Bank Name" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
    				</div>
				    <div class="col-xs-2">
    			        <label style="font-size:18px;" class=" col-sm-2 control-label">Bank_Name</label>
    			        <input type="text" name="bankname" id="bankname" class="roundedCategory" readonly style="font-size:18px; margin-top: -6px; width:100%; height: 40px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
    				</div>
    				<div class="col-xs-2">
    			        <label style="font-size:18px;" class=" col-sm-2 control-label">Bank_Account_No</label>
    			        <input type="text" name="bankaccno" id="bankaccno" class="roundedCategory" readonly style="font-size:18px; margin-top: -6px; width:100%; height: 40px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
    				</div>
        			<div class="col-xs-2">
    					<label style="font-size:18px;" class=" col-sm-2 control-label">Ch_Amount</label>
    					<input type="text" autofocus name="cheque_amount" id="cheque_amount" class="roundedCategory" value="<?php echo number_format($fetch1[0], 2, '.', '')?>" style="font-size:18px; margin-top: -6px; width:100%; height: 40px; font-weight:bold; text-align: right; color: #000066;" onfocus="this.select();" autocomplete="off" required>
    				</div>
    				<div class="col-xs-2">
    					<label style="font-size:18px;" class=" col-sm-2 control-label">Ch_Number</label>
    					<input type="text" autofocus name="ch_number" id="ch_number" class="roundedCategory" value="-" style="font-size:18px; width:100%; margin-top: -6px; height: 40px; font-weight:bold; text-align: left; color: #000066;" onfocus="this.select();" autocomplete="off" required>
    				</div>
    				<div class="col-xs-2">
    					<label style="font-size:18px;" class=" col-sm-2 control-label">Ch_Date</label>
    					<input type="text" autofocus name="ch_date" id="ch_date" class="roundedCategory" value="<?php date_default_timezone_set('Asia/Kolkata'); echo date( 'd-m-Y H:i:s'); ?>" style="font-size:18px; margin-top: -6px; width:100%; height: 40px; font-weight:bold; text-align: left; color: #000066;" onfocus="this.select();" autocomplete="off" required>
    				</div>
			<?php
				}
			?>
			</tr>
			<br><br><br><br>
		    </center>
		    <center>
			    <tr>
			        <div class="col-xs-3">
        			    <label style="font-size:18px;" class=" col-sm-2 control-label">Customer_Bank_Name</label>
        			    <input type="text" name="bank_name" id="bank_name" class="roundedCategory" value="" style="font-size:18px; width:100%; margin-top: -6px; height: 40px; font-weight:bold; color: #000066;" placeholder="Type and Select Bank Name" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
    				</div>
        			<div class="col-xs-4">
        			    <label style="font-size:18px;" class=" col-sm-2 control-label">Remarks</label>
        			    <input type="text" name="remarks" id="remarks" class="roundedCategory" value="-" style="font-size:18px; width:100%; margin-top: -6px; height: 40px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
    				</div>
			    </tr>
			<br><br><br>
		    </center>
            	<br>
            	<center>
            	<table id="myTable1" style="font-size:18px;width: <?php echo $width; ?>;">
            		<tr>
            			<th style = "width:14.28%; font-weight:bold; text-align: left"></th>
						<th style = "width:14.28%; font-weight:bold; text-align: left"></th>
						<th style = "width:14.28%; font-weight:bold; text-align: left">total_inv_val</th>
						<th style = "width:14.28%; font-weight:bold; text-align: left">rec_amount</th>
						<th style = "width:14.28%; font-weight:bold; text-align: left">balance_amount</th>
						<th style = "width:14.28%; font-weight:bold; text-align: left">ch_amount</th>
						<th style = "width:14.28%; font-weight:bold; text-align: left">Balance</th>
            		</tr>
            		<tbody>
            			<?php
            			$query1 = mysqli_query($connect, "select sum(total_inv_val),sum(rec_amount),sum(balance_amount) from invtotal_mst  where status = '" ."PENDING". "' and customer_code = '" .$customer_code . "'") or die(mysqli_error());
        				while($fetch1 = mysqli_fetch_array($query1)){
					?>
					<tr>
					    <td style="text-align: left"> <?php echo "TOTAL"?></td>
            			    <td style = "width:14.28%; text-align: left"> <?php echo ""?></td>
            			    <td style = "width:14.28%; text-align: right"><input style = "width:100%; height: 40px; text-align: right;font-size:20px; font-weight:bold;" readonly type="text" autocomplete="off" tabindex="-1" style="font-size:18px; font-weight:bold; text-align: right;" value="<?php echo number_format($fetch1[0], 2, '.', '')?>"/></td>
                            <td style = "width:14.25%; text-align: left"><input style = "width:100%; height: 40px; text-align: right;font-size:20px; font-weight:bold;" readonly type="text" autocomplete="off" tabindex="-1" style="font-size:18px; font-weight:bold; text-align: right;" value="<?php echo number_format($fetch1[1], 2, '.', '')?>"/></td>
                            <td style = "width:14.28%; text-align: left"><input style = "width:100%; height: 40px; text-align: right;font-size:20px; font-weight:bold;" readonly type="text" autocomplete="off" tabindex="-1" style="font-size:18px; font-weight:bold; text-align: right;" value="<?php echo number_format($fetch1[2], 2, '.', '')?>"/></td>
                            <td style = "width:14.28%; text-align: left"><input style = "width:100%; height: 40px; text-align: right;font-size:20px; font-weight:bold;" readonly type="text" autocomplete="off" tabindex="-1" style="font-size:18px; font-weight:bold; text-align: right;" value="<?php echo ""?>" name="calculation" class='calculation' id="calculation"/></td>
            			    <td style = "width:14.28%; text-align: left"><input style = "width:100%; height: 40px; text-align: right;font-size:20px; font-weight:bold;" readonly type="text" autocomplete="off" tabindex="-1" style="font-size:18px; font-weight:bold; text-align: right;" value="<?php echo ""?>" name="balance" class='balance' id="balance"/></td>
					</tr>
					<?php
						}
					?>
            		</tbody>
            	</table>
            	</center>
            	<br>
            	<center>
            	<div class="table-responsive" style="width: <?php echo $width; ?>; margin-left : 0px; overflow:auto; height: 40%;">
            	<table id="myTable" style="font-size:18px; width: <?php echo $width; ?>;">
            		<tr>
            			<th style = "width:14.28%; font-weight:bold; text-align: left">inv_number</th>
						<th style = "width:14.28%; font-weight:bold; text-align: left">inv_date</th>
						<th style = "width:14.28%; font-weight:bold; text-align: left">total_inv_val</th>
						<th style = "width:14.28%; font-weight:bold; text-align: left">rec_amount</th>
						<th style = "width:14.28%; font-weight:bold; text-align: left">balance_amount</th>
						<th style = "width:14.28%; font-weight:bold; text-align: left">ch_amount</th>
						<th style = "width:14.28%; font-weight:bold; text-align: left">status</th>
            		</tr>
            		<tbody>
            			<?php
            			$query = mysqli_query($connect, "select * from invtotal_mst where status = '" ."PENDING". "' and customer_code = '" .$customer_code . "' order by inv_date,inv_number ASC") or die(mysqli_error());
						while($fetch = mysqli_fetch_array($query)){
					?>
					<tr>
					    <input type="hidden" value="<?php echo $fetch['inv_number']?>" name="mem_id[]">
					    <td style = "width:14.28%; text-align: left"><input style = "width:100%; height: 40px; text-align: left;font-size:20px; font-weight:bold;" readonly onfocus="this.select();" type="text" autocomplete="off" tabindex="-1" style="font-size:20px; font-weight:bold; text-align: left" autofocus value="<?php echo $fetch['inv_number']?>" name="inv_number[]"/></td>
					    <td style = "width:14.28%; text-align: left"><input style = "width:100%; height: 40px; text-align: left;font-size:20px; font-weight:bold;" readonly onfocus="this.select();" type="text" autocomplete="off" tabindex="-1" style="font-size:20px font-weight:bold; text-align: left" value="<?php echo $fetch['inv_date']?>" name="inv_date[]"/></td>
					    <td style = "width:14.28%; text-align: right"><input style = "width:100%; height: 40px; text-align: right;font-size:20px; font-weight:bold;" readonly onfocus="this.select();" type="text" autocomplete="off" tabindex="-1" style="font-size:20px; font-weight:bold; text-align: right;" value="<?php echo number_format($fetch['total_inv_val'], 2, '.', '')?>" name="total_inv_val[]"/></td>
					    <td style = "width:14.28%; text-align: right"><input style = "width:100%; height: 40px; text-align: right;font-size:20px; font-weight:bold;" readonly onfocus="this.select();" type="text" autocomplete="off" tabindex="-1" style="font-size:20px; font-weight:bold; text-align: right;" value="<?php echo number_format($fetch['rec_amount'], 2, '.', '')?>" name="rec_amount[]" name="rec_amount[]" class='ch_amount1'/></td>
					    <td style = "width:14.28%; text-align: right"><input style = "width:100%; height: 40px; text-align: right;font-size:20px; font-weight:bold;" readonly type="text" autocomplete="off" tabindex="-1" style="font-size:20px; font-weight:bold; text-align: right;" value="<?php echo number_format($fetch['balance_amount'], 2, '.', '')?>" name="balance_amount[]" name="balance_amount[]" class='balance_amount'/></td>
					    <td style = "width:14.28%; text-align: right"><input style = "width:100%; height: 40px; text-align: right;font-size:20px; font-weight:bold;" type="number" onfocus="this.select();" autocomplete="off" style="font-size:20px; font-weight:bold; text-align: right;" value="" name="ch_amount[]" class='ch_amount'/></td>
					    <td style = "width:14.28%; text-align: left"><input style = "width:100%; height: 40px; font-size:20px; font-weight:bold;" readonly onfocus="this.select();" type="text" autocomplete="off" tabindex="-1" style="font-size:20px; font-weight:bold; text-align: left" value="<?php echo $fetch['status']?>" name="status[]" name="status[]" class='status'/></td>
					</tr>
					<?php
						}
					?>
            		</tbody>
            	</table>
            	</div>
            	<br>
            	<td><button type="submit" class="btn btn-primary" id="submit" disabled="disabled">Save</button></td>
            	</center>
        	</div>
        </form>
        </div>
    </body>
</html>

<script>
$(document).ready(function() {
    $('#balance').on('input change keyup blur click', function() {
        if($(this).val() == '0.00') {
            $("#form :input").prop("readonly", true);
            $('#submit').prop('disabled', false);
            
        } else {
            //$("#form :input").prop("readonly", false);
            $('#submit').prop('disabled', true);
        }
    });
});
</script>
<script>
$(document).ready(function(){
    $("#myTable").on('input change keyup blur click', '.ch_amount', function () {
        var calculated_total_sum = 0;
        $("#myTable .ch_amount").each(function () {
            var get_textbox_value = $(this).val();
            if ($.isNumeric(get_textbox_value)) {
                calculated_total_sum += parseFloat(get_textbox_value);
                var clickval = $(this).find("td:eq(4) input[type='text']").val();
                $(this).find("td:eq(5) input[type='number']").val(clickval);
            }                  
        });
        $('#calculation').val(calculated_total_sum.toFixed(2));
        
        
        var a = document.getElementById("cheque_amount").value;
        var myNumber = calculated_total_sum;
        $bal = a - myNumber;
        var $tblrow = $(this);
        $('#balance').val($bal.toFixed(2));
        
        if(myNumber == a)
        {
            var nextInputvalue = $(this).next(':input[name="total_inv_val[]"]').val();
            //alert(nextInputvalue);
            $(this).next().val(nextInputvalue);
        }
        if(myNumber > a)
        {
            $(this).val("");
            $this.select();
            var calculated_total_sum = 0;
            $("#myTable .ch_amount").each(function () {
                var get_textbox_value = $(this).val();
                if ($.isNumeric(get_textbox_value)) {
                    calculated_total_sum += parseFloat(get_textbox_value);
                    var a = document.getElementById("cheque_amount").value;
                    var myNumber = calculated_total_sum;
                    $bal = a - myNumber;
                    if(myNumber == a)
                    {
                        $("input[name='status[]']").val("CLEARED");
                    }
                }                  
            });
            $('#calculation').val(calculated_total_sum.toFixed(2));
        };
        
        
    });
});
</script>
<script>
$(document).ready(function() {
    $(function() {
		$("#acc_type").autocomplete({
			source: function(request, response) {
				$.ajax({
					url: "acctype",
					type: 'post',
					dataType: "json",
					data: {
						search: request.term
					},
					success: function(data) {
						response(data);
					}
				});
			},
			select: function(event, ui) {
				$('#acc_type').val(ui.item.value);
				$('#bankname').val(ui.item.label)
				$('#bankaccno').val(ui.item.bankaccno);
				document.getElementById("cheque_amount").focus();
				return false;
			}
		});
	});
	
	$(function() {
		$("#bank_name").autocomplete({
			source: function(request, response) {
				$.ajax({
					url: "bankname",
					type: 'post',
					dataType: "json",
					data: {
						search: request.term
					},
					success: function(data) {
						response(data);
					}
				});
			},
			select: function(event, ui) {
				$('#bank_name').val(ui.item.label);
				document.getElementById("remarks").focus();
				return false;
			}
		});
	});
});
</script>
<script>
$(document).keydown(function(objEvent) {
	if (objEvent.keyCode == 13) {
		objEvent.preventDefault();
	}
})
</script>
<?php
	$date = new DateTime();
	$current_timestamp = $date->getTimestamp();
?>
<script>
    flag_time = true;
	timer = '';
	setInterval(function(){phpJavascriptClock();},1000);
	function phpJavascriptClock()
	{
		if ( flag_time ) {
		timer = <?php echo $current_timestamp;?>*1000;
		}
		var d = new Date(timer);
		months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
		
		month_array = new Array('January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'Augest', 'September', 'October', 'November', 'December');
		
		day_array = new Array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
		
		currentYear = d.getFullYear();
		month = d.getMonth();
		var currentMonth = months[month];
		var currentMonth1 = month_array[month];
		var currentDate = d.getDate();
		currentDate = currentDate < 10 ? '0'+currentDate : currentDate;
		
		var day = d.getDay();
		current_day = day_array[day];
		var hours = d.getHours();
		var minutes = d.getMinutes();
		var seconds = d.getSeconds();
		
		var ampm = hours >= 12 ? 'PM' : 'AM';
		hours = hours % 12;
		hours = hours ? hours : 12; // the hour ’0′ should be ’12′
		minutes = minutes < 10 ? '0'+minutes : minutes;
		seconds = seconds < 10 ? '0'+seconds : seconds;
		var strTime = hours + ':' + minutes+ ':' + seconds + ' ' + ampm;
		timer = timer + 1000;
		document.getElementById("demo2").innerHTML='Date : ' + currentDate+'-' +(month+1)+'-' +currentYear + ' Time : ' + strTime ;
		flag_time = false;
	}
</script>
