<?php
	require('../config/dbcon.php');
	$supplier_code  = trim($_REQUEST["supplier_code"]);
	$delete = "DELETE FROM ledgercr_mst";
	if (mysqli_query($connect, $delete)) {
	}
	$query = mysqli_query($connect, "select inv_number,inv_date,total_inv_val,supplier_code,supplier_name,supplier_place,supplier_email,supplier_mobile,supplier_gstno,rec_amount,balance_amount,ch_amount from pinvtotal_mst where supplier_code = '" .$supplier_code. "'") or die(mysqli_error());
	while($fetch = mysqli_fetch_array($query)){
	    $inv_number = $fetch["inv_number"];
	    $inv_date = $fetch["inv_date"];
	    $total_inv_val = $fetch["total_inv_val"];
	    $supplier_code = $fetch["supplier_code"];
	    $supplier_name = $fetch["supplier_name"];
	    $supp_place = $fetch["supplier_place"];
	    $supp_email = $fetch["supplier_email"];
	    $supp_mobile = $fetch["supplier_mobile"];
	    $supp_gstno = $fetch["supplier_gstno"];
	    $rec_amount = 0;
	    $balance_amount = $fetch["balance_amount"];
	    $ch_amount = $fetch["ch_amount"];
	    $ch_number = "-";
	    $ch_date = "-";
	    $payment_number = "-";
	    $payment_date = "-";
	    $sorting_date = $fetch["inv_date"];
	    $query1 = "insert into ledgercr_mst(inv_number,inv_date,total_inv_val,supplier_code,supplier_name,supp_place,supp_email,supp_mobile,supp_gstno,rec_amount,balance_amount,ch_amount,ch_number,ch_date,payment_number,payment_date,sorting_date) VALUES ('" . $inv_number . "','" . $inv_date . "','" . $total_inv_val . "','" . $supplier_code . "','" . $supplier_name . "','" . $supp_place . "','" . $supp_email . "','" . $supp_mobile . "','" . $supp_gstno . "','" . $rec_amount . "','" . $balance_amount . "','" . $ch_amount . "','" . $ch_number . "','" . $ch_date . "','" . $payment_number . "','" . $payment_date . "','" . $sorting_date . "')";
		mysqli_query($connect, $query1) or die(mysqli_last_error());
	}
	$query2 = mysqli_query($connect, "select inv_number,inv_date,total_inv_val,supplier_code,supplier_name,supplier_place,rec_amount,balance_amount,ch_amount,ch_number,ch_date,payment_number,payment_date from payment_mst where supplier_code = '" .$supplier_code. "'") or die(mysqli_error());
	while($fetch1 = mysqli_fetch_array($query2)){
	    $inv_number = $fetch1["inv_number"];
	    $inv_date = $fetch1["inv_date"];
	    $total_inv_val = $fetch1["total_inv_val"];
	    $supplier_code = $fetch1["supplier_code"];
	    $supplier_name = $fetch1["supplier_name"];
	    $supp_place = $fetch1["supplier_place"];
	    $supp_email = "-";
	    $supp_mobile = "-";
	    $supp_gstno = "-";
	    $rec_amount = $fetch1["rec_amount"];
	    $balance_amount = $fetch1["balance_amount"];
	    $ch_amount = $fetch1["ch_amount"];
	    $ch_number = $fetch1["ch_number"];
	    $ch_date = $fetch1["ch_date"];
	    $payment_number = $fetch1["payment_number"];
	    $payment_date = $fetch1["payment_date"];
	    $sorting_date = $fetch1["payment_date"];
	    $query3 = "insert into ledgercr_mst(inv_number,inv_date,total_inv_val,supplier_code,supplier_name,supp_place,supp_email,supp_mobile,supp_gstno,rec_amount,balance_amount,ch_amount,ch_number,ch_date,payment_number,payment_date,sorting_date) VALUES ('" . $inv_number . "','" . $inv_date . "','" . $total_inv_val . "','" . $supplier_code . "','" . $supplier_name . "','" . $supp_place . "','" . $supp_email . "','" . $supp_mobile . "','" . $supp_gstno . "','" . $rec_amount . "','" . $balance_amount . "','" . $ch_amount . "','" . $ch_number . "','" . $ch_date . "','" . $payment_number . "','" . $payment_date . "','" . $sorting_date . "')";
		mysqli_query($connect, $query3) or die(mysqli_last_error());
	}
?>
<?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>Supplier Ledger</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="min-width: <?php echo $width; ?>; max-width:  auto;">
        <div style="min-width: <?php echo $width; ?>; max-width:  auto;">
        <form method="POST" id = "form" action="">
            <nav class="navbar navbar-expand-lg navbar-dark bg-primary" style="min-width: <?php echo $width; ?>; max-width:  auto; margin-top: -20px; height: 62px; 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">
                <input style = "background-color: #007bff; font-size:24px; width:200px; height : 55px; margin-top : px;" type="button" onclick="exportToExcel('tblexportData', 'Export-data')" value="Export to Excel">
                <h1 style="color:white; font-size:24px; margin-top: -40px;" class="text-primary" align="center">Supplier Ledger</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="min-width: <?php echo $width; ?>; max-width:  auto; margin-left: 0px; margin-top: -00px;">
            	<br>
            	<center>
            	<div class="table-responsive" style="min-width: <?php echo $width; ?>; max-width:  auto; margin-left : 0px; overflow:auto; height: 80%;">
            	<table id="tblexportData" style="font-size:18px; width : 80%;">
            		<?php
            			$query = mysqli_query($connect, "select distinct(supplier_code),supplier_name,supplier_place,supplier_mobile,supplier_email,supplier_gstno from supplier_mst where supplier_code = '" . $supplier_code . "'") or die(mysqli_error());
						while($fetch = mysqli_fetch_array($query)){
						    $supcode = $fetch['supplier_code'];
						    $supname = $fetch['supplier_name'];
						    $supplace = $fetch['supplier_place'];
						    $supmobile = $fetch['supplier_mobile'];
						    $supemail = $fetch['supplier_email'];
					    ?>
					    <tr style="height : 30px">
    					    <td ColSpan=7 style="text-align: center"><?php echo $supcode ?> - <?php echo $supname ?> - <?php echo $supplace ?> - <?php echo $supmobile ?> - <?php echo $supemail ?></td>
    				    </tr>
        				<tr>
        				    <th style = "width:14.2857%; text-align: left">inv_number</th>
            				<th style = "width:14.2857%; text-align: left">inv_date</th>
            				<th style = "width:14.2857%; text-align: left">payment_number</th>
            				<th style = "width:14.2857%; text-align: left">payment_date</th>
            				<th style = "width:14.2857%; text-align: right">credit</th>
            				<th style = "width:14.2857%; text-align: right">debit</th>
            				<th style = "width:14.2857%; text-align: right">closing</th>
            			</tr>
        				<?php
						}
					?>
            		<tbody>
            			<?php
            			$sum = 0;
            			$sum1 = 0;
            			$query = mysqli_query($connect, "select inv_number,inv_date,total_inv_val,supplier_code,supplier_name,supp_place,supp_email,supp_mobile,supp_gstno,rec_amount,balance_amount,ch_amount,ch_number,ch_date,payment_number,payment_date,sorting_date from ledgercr_mst where supplier_code = '" . $supplier_code . "' order by sorting_date") or die(mysqli_error());
						while($fetch = mysqli_fetch_array($query)){
                            $originalinvDate = $fetch['inv_date'];
                            $newInvDate = date("d-m-Y", strtotime($originalinvDate));
                            if($fetch['payment_date'] == '-'){
                                $newrecDate = '-';
                            }
                            else{
                                $originalrecDate = $fetch['payment_date'];
                                $newrecDate = date("d-m-Y", strtotime($originalrecDate));
                            }
                            $invamt = 0.00;
                            if($fetch['payment_number'] == '-'){
                                $invamt = $fetch['total_inv_val'];
                            }
                            $sum += $invamt;
                            $sum1 += $fetch['rec_amount'];
                            $balamt = $sum - $sum1;
					    ?>
					<tr>
					    <td style="text-align: left"><?php echo $fetch['inv_number']?></td>
        			    <td style="text-align: left"><?php echo $newInvDate?></td>
        			    <td style="text-align: left"><?php echo $fetch['payment_number']?></td>
        			    <td style="text-align: left"><?php echo $newrecDate?></td>
        			    <td style="text-align: right"><?php echo number_format($invamt, 2, '.', '')?></td>
        			    <td style="text-align: right"><?php echo number_format($fetch['rec_amount'], 2, '.', '')?></td>
        			    <td style="text-align: right"><?php echo number_format($balamt, 2, '.', '')?></td>
					</tr>
					<?php
						}
					?>
					<?php
            			$query1 = mysqli_query($connect, "select sum(total_inv_val),sum(rec_amount),sum(balance_amount) from pinvtotal_mst  where supplier_code = '" . $supplier_code . "'") or die(mysqli_error());
        				while($fetch1 = mysqli_fetch_array($query1)){
					?>
					<tr>
					    <td style="text-align: left"><?php echo "TOTAL"?></td>
            		    <td style="text-align: left"><?php echo ""?></td>
            		    <td style="text-align: right"></td>
            		    <td style="text-align: right"></td>
            		    <td style="text-align: right"><?php echo number_format($fetch1[0], 2, '.', '')?></td>
            		    <td style="text-align: right"><?php echo number_format($fetch1[1], 2, '.', '')?></td>
            		    <td style="text-align: right"><?php echo number_format($fetch1[2], 2, '.', '')?></td>
            		    
					</tr>
					<?php
						}
					?>
            		</tbody>
            	</table>
            	</div>
            	</center>
        	</div>
        </form>
        </div>
    </body>
</html>
<?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>
<script type="text/javascript">
	function exportToExcel(tableID, filename = '') {
		var downloadurl;
		var dataFileType = 'application/vnd.ms-excel';
		var tableSelect = document.getElementById(tableID);
		var tableHTMLData = tableSelect.outerHTML.replace(/ /g, '%20');
		// Specify file name
		filename = filename ? filename + '.xls' : 'export_excel_data.xls';
		// Create download link element
		downloadurl = document.createElement("a");
		document.body.appendChild(downloadurl);
		if (navigator.msSaveOrOpenBlob) {
			var blob = new Blob(['\ufeff', tableHTMLData], {
				type: dataFileType
			});
			navigator.msSaveOrOpenBlob(blob, filename);
		} else {
			// Create a link to the file
			downloadurl.href = 'data:' + dataFileType + ', ' + tableHTMLData;
			// Setting the file name
			downloadurl.download = filename;
			//triggering the function
			downloadurl.click();
		}
	}
</script>