<?php

	include'session.php';
	include'connection.php';
	include'../Retailers/functions.php';
	                          
?>
<html>
<head>
	<title>All Seller Txn</title>
	<!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
	<link rel="icon" href="images/logo.png" type="image/gif" sizes="16x16">
	
	<!-- Admin CSS File Link -->
	<link rel="stylesheet" href="css/style.css">
	
	<!-- User JAVA SCRIPT File Link -->
	<script type="text/javascript" src="js/script.js"></script>

	
	<!-- Font Awesome Icone -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
	
	<style>
		.div1{
			background:;
			width:96%;
			padding:10px;
			margin:2%;
		}
		.div1 input, .div1 select{
			height:30px;
			width:180px;
			font-size:16px;
			padding:5px 8px;
			border-radius:5px;
			border:solid #6b5127 1px;
			overflow:hidden;
			margin:10px 10px 0 5px;
		}
		input[type=submit]{
			background:#6b5127;
			color:white;
			transition:1s;
			cursor:pointer;
		}
		input[type=submit]:hover{
			background:white;
			color:#6b5127;
			transition:1s;
		}
		.div2{
			background:#f7f7f7;
			width:100%;
			float:left;
			text-align:center;
			padding:0 10px 10px 10px;
			overflow:auto;
		}
		.div2 table{
			width:96%;
			text-align:center;
			font-size:12px;
			border-collapse:collapse;
			padding:2px 4px;
			margin:1%;
		}
		table th{
			background:#6b5127;
			color:white;
			height:40px;
			border:solid white 1px;
			padding:2px 4px;
		}
		table td{
			height:40px;
			border:solid black 1px;
			padding:2px 4px;
		}
		table tr:hover{
			background:#D7D4D4;
		}
		.itcInput{display:none; width:110px; padding:2px 5px;}
		.itcPera{width:110px; padding:2px 5px;}
		@media(max-width:768px)
		{
			.div1{
				background:;
				width:100%;
				height:auto;
				margin:10px 0 0 0;
				padding:0;
				text-align:center;
			}.div1 input{
				width:90%;
				margin:10px;
			}
			.div2{
				width:100%;
				margin:10px 0 0 0;
				padding:0px;
				text-align:center;
			}
		}
	</style>
	<link rel="stylesheet" href="css/mediaStyle.css">
</head>
<body>
	<!--Header Divison-->
	<?php
		require'header.php';
	?>
	
	<!--Container Division-->
	<div class="container">
		<?php
			include'menu.php';
		?>
		<div class="content">
			<div class="head">
				<div class="title"><p>All Seller Txn :-</p></div>
				<div class="msg"><marquee><?php include'msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
				<form method="post">
					<div class="div1">
					    <?php
					        if(isset($_POST['submit']))
					        {
					            $date1Value=$_POST['fdate'];
					            $date2Value=$_POST['ldate'];
					        }
					        else
					        {
					            $date1Value=$date;
					            $date2Value=$date;
					        }
					    
					    ?>
						<input type="date" name="fdate" value="<?php echo $date1Value; ?>">
						<input type="date" name="ldate" value="<?php echo $date2Value; ?>">
						<select name="firm" style="width:200px; font-size:12px;">
						    <?php
						        $quFirm="SELECT Id, FirmName, OwnerName, GstNo FROM firm WHERE Status='ACTIVE'";
						        $reFirm=mysqli_query($con,$quFirm);
						        while($dataFirm=mysqli_Fetch_array($reFirm))
						        {
						            echo "<option value='".$dataFirm[0]."'>".$dataFirm[1]." - ".$dataFirm[2]." - ".$dataFirm[3]."</option>";
						        }
						        
						    ?>
						</select>
						<input type="submit" name="submit" value="Submit"  onclick='showLoader()'>
					</div>
				</form>
				<div class="div2">
					<table cellpadding="20" border="2"> 
						<tr>
							<th>Month</th>
							<th>Year</th>
							<th>Seller</th>
							<th>Id</th>
							<th>Firm</th>
							<th>GST</th>
							<th>Pre txn</th>
							<th>Txn</th>
							<th>Total Txn</th>
							<th>ITC</th>
							<th>Rest Txn</th>
							<th>Settle</th>
							<th>Bill REC</th>
						</tr>
						<?php
							if(isset($_POST['submit']))
							{
								$fdate = $_POST['fdate'];
								$ldate = $_POST['ldate'];
								$firm_id = $_POST['firm'];
								
                                $year = date('Y', strtotime($fdate));
                                $month = date('F', strtotime($fdate));

								$totalPreTxn=0;
								$totalTxn=0;
								$GrandTotalTxn=0;
								$quGetGenrateTxn="SELECT w.TxnWith, SUM(w.Txn), b.firm_id FROM wallet w INNER JOIN bank b ON w.TxnBy = b.Id  WHERE b.firm_id='$firm_id' AND w.TxnType='Generate_Amount' AND w.Date>='$fdate' AND w.Date<='$ldate' GROUP BY w.TxnWith";
                                $reGetGenrateTxn=mysqli_query($con,$quGetGenrateTxn);
                                while($dataGenrateTxn=mysqli_fetch_array($reGetGenrateTxn))
                                {   
                                    $sellerName=$dataGenrateTxn[0];
                                    $quFirm="SELECT Id,Firm, GstNo, City FROM Seller WHERE Name='$sellerName'";
                                    $reFirm=mysqli_query($con,$quFirm);
                                    $dataFirm=mysqli_fetch_array($reFirm);
                                    $SellerId=$dataFirm[0];
                                    $txn=$dataGenrateTxn[1];
                                    $txn=$dataGenrateTxn[1];
                                    
                                    $qu_chk_dup = "SELECT Id FROM SellerTxn WHERE Month='$month' AND Year='$year' AND Seller='$SellerId' AND firm_id='$firm_id'";
                                    $re_chk_dup = mysqli_query($con,$qu_chk_dup);
                                    $count_chk_dup = mysqli_num_rows($re_chk_dup);
                                    if($count_chk_dup==0)
                                    {
                                        $qu_pretxn = "SELECT RestTxn, Seller FROM SellerTxn WHERE Seller='$SellerId' AND RestTxn>0 AND Settle='NO' AND firm_id='$firm_id' ORDER By Id DESC";
                                        $re_pretxn = mysqli_query($con,$qu_pretxn);
                                        $data_pretxn = mysqli_fetch_array($re_pretxn);
                                        $preTxn = $data_pretxn[0];
                                        $preTxnSeller = $data_pretxn[1];
                                        $totalTxn=$preTxn+$txn;
                                        
                                        $updatePreTxnSeller="UPDATE SellerTxn SET Settle='YES' WHERE Seller='$preTxnSeller'";
                                        mysqli_query($con,$updatePreTxnSeller);
                                        
                                        $insertTxn="INSERT INTO SellerTxn (Month, Year, Seller, LastTxn, Txn, TotalTxn, firm_id) 
                                        VALUES ('$month', '$year', '$SellerId', '$preTxn', '$txn', '$totalTxn', '$firm_id')";
                                        mysqli_query($con,$insertTxn);
                                    }
			                    }
			                    
			                    $quSellerTxn = "SELECT t.Id, Month, Year, Name, Firm, s.GstNo as GN, LastTxn, Txn, TotalTxn, ITC, RestTxn, Settle, Bill, f.FirmName, f.GstNo FROM SellerTxn t INNER JOIN Seller s ON t.Seller = s.Id INNER JOIN firm f ON t.firm_id = f.Id WHERE Month='$month' AND Year='$year' AND firm_id='$firm_id' OR RestTxn>0 AND Settle='NO' AND firm_id='$firm_id'  ORDER By s.Name ASC";
			                    $reSellerTxn = mysqli_query($con,$quSellerTxn);
			                    while($dataSellerTxn=mysqli_fetch_array($reSellerTxn))
			                    {
			                        $billBGColor="";
			                        $settleBGColor="";
			                        if($dataSellerTxn['Bill']=="YES")
			                        {
			                            $billBGColor="#D7FCD8";
			                        }
			                        if($dataSellerTxn['Settle']=="YES")
			                        {
			                            $settleBGColor="#D7FCD8";
			                        }
			                        
	    		                    echo "<tr >";
    	    				                echo "<td>".$dataSellerTxn['Month']."</td>";
    	    				                echo "<td>".$dataSellerTxn['Year']."</td>";
    	    				                echo "<td>".$dataSellerTxn['Name']."</td>";
    	    				                echo "<td>".$dataSellerTxn['Id']."</td>";
    	    				                echo "<td>".$dataSellerTxn['Firm']."</td>";
    	    				                echo "<td>".$dataSellerTxn['GN']."</td>";
    	    				                echo "<td>".$dataSellerTxn['LastTxn']."</td>";
    	    				                echo "<td>".$dataSellerTxn['Txn']."</td>";
    	    				                if($dataSellerTxn['Settle']=="YES" OR empty($dataSellerTxn['GstNo']))
    	    				                {
    	    				                    $MSG=$dataSellerTxn['TotalTxn'];
    	    				                }
    	    				                else
    	    				                {
    	    				                    $MSG="Please Send Invoice For Txn = ".$dataSellerTxn['TotalTxn'].", Firm = ".$dataSellerTxn[13].", GST NO. = ".$dataSellerTxn[14];
    	    				                }
    	    				                echo "<td><input style='display:none;' id='TotTxn".$dataSellerTxn[0]."' value='".$dataSellerTxn['TotalTxn']."'>".$MSG."</td>";
    	    				                echo "<td ondblclick='showInput(".$dataSellerTxn[0].")' title='ITC AVAIL'><p id='itc".$dataSellerTxn[0]."' class='itcPera'>".$dataSellerTxn['ITC']."</p> <input onfocusout='updateItc(".$dataSellerTxn[0].")' class='itcInput' type='number' id='itcInp".$dataSellerTxn[0]."' value='".$dataSellerTxn['ITC']."'></td>";
    	    				                echo "<td><p id='RestTxn".$dataSellerTxn[0]."'>".$dataSellerTxn['RestTxn']."</p></td>";
    	    				                echo "<td style='background:".$settleBGColor."'>".$dataSellerTxn['Settle']."</td>";
    	    				                echo "<td style='background:".$billBGColor."' ondblclick='showBillInput(".$dataSellerTxn[0].")' ><p id='bill".$dataSellerTxn[0]."' class='billPera'>".$dataSellerTxn['Bill']."</p> <input onfocusout='updateBill(".$dataSellerTxn[0].")' class='itcInput' type='text' id='billInp".$dataSellerTxn[0]."' value='".$dataSellerTxn['Bill']."'></td>";
				                     echo "</tr>";
				                     $totalPreTxn=$totalPreTxn+$dataSellerTxn['LastTxn'];
				                     $totalTxn=$totalTxn+$dataSellerTxn['Txn'];
				                     $GrandTotalTxn = $totalPreTxn+$totalTxn;
			                    }
			                    
			                    echo "<tr>";
		        	                echo"<th></th>";
		        	                echo"<th></th>";
		        	                echo"<th></th>";
		        	                echo"<th></th>";
		        	                echo"<th>All Seller</th>";
		        	                echo"<th>".$totalPreTxn."</th>";
		        	                echo"<th>".$totalTxn."</th>";
		        	                echo"<th>".$GrandTotalTxn."</th>";
		        	                echo"<th></th>";
		        	                echo"<th></th>";
		        	                echo"<th></th>";
		        	                echo"<th></th>";
	        	                echo "</tr>";
							}
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<script>
	    function showInput(Arg)
	    {
	        var TxnId=Arg;
	        var itcPeraId="itc"+Arg;
	        var itcInputId="itcInp"+Arg;
	        document.getElementById(itcPeraId).style.display = "none";
	        document.getElementById(itcInputId).style.display = "inline";
	    }
	   
	    function updateItc(Arg)
	    {
	        var SellerTxnId=Arg;
	        var itcPeraId="itc"+Arg;
	        var itcInputId="itcInp"+Arg;
	        var TotTxnId="TotTxn"+Arg;
	        
	        var RestTxnId="RestTxn"+Arg;
	        var itcInputValue=document.getElementById(itcInputId).value;
	        var TotalTxn=document.getElementById(TotTxnId).value;
	        
	        var xmlhttpState = new XMLHttpRequest();
			xmlhttpState.open("GET","Ajax.php?itcInputValue="+itcInputValue+"&SellerTxnId="+SellerTxnId+"&TotalTxn="+TotalTxn, false)
			xmlhttpState.send(null);
			if(xmlhttpState.responseText=="UPDATED")
			{
			    document.getElementById(itcPeraId).style.display = "inline";
	            document.getElementById(itcInputId).style.display = "none";
	            location.reload();
		    }
	    }
	    
	     function showBillInput(Arg)
	    {
	        var TxnId=Arg;
	        var billPeraId="bill"+Arg;
	        var billInputId="billInp"+Arg;
	        document.getElementById(billPeraId).style.display = "none";
	        document.getElementById(billInputId).style.display = "inline";
	    }
	    
	    function updateBill(Arg)
	    {
	        var SellerTxnId=Arg;
	        var billPeraId="bill"+Arg;
	        var billInputId="billInp"+Arg;
	        
	        var billInputValue=document.getElementById(billInputId).value;
	        var xmlhttpState = new XMLHttpRequest();
			xmlhttpState.open("GET","Ajax.php?billInputValue="+billInputValue+"&SellerTxnId="+SellerTxnId, false)
			xmlhttpState.send(null);
			if(xmlhttpState.responseText=="UPDATED")
			{
			    document.getElementById(billPeraId).style.display = "inline";
	            document.getElementById(billInputId).style.display = "none";
	            location.reload();
		    }
	    }
	</script>
	<?php
		require'footer.php';
	?>
</body>
</html>