<?php
	include'session.php';
	include'connection.php';
	include'../Retailers/functions.php';
?>
<html>
<head>
	<title>All Client 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:auto;
			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-x:auto;
		}
		.div2 table{
			width:96%;
			text-align:center;
			font-size:14px;
			border-collapse:collapse;
			padding:2px 10px;
			margin:2%;
		}
		table th{
			background:#6b5127;
			color:white;
			height:40px;
			border:solid white 1px;
			padding:2px 5px;
		}
		table td{
			background:white;
			height:40px;
			border:solid black 1px;
			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 Client 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'];
					            $usrType = $_POST['usrType'];
					        }
					    
					    ?>
						<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>
						<select name="usrType">
						    <option>API</option>
						    <option>RETAILER</option>
						    <option>DISTRIBUTER</option>
						    <option>ALL</option>
						</select>
						<select name="txnType">
						    <option>PAYMENT</option>
						    <option>DEBITAMOUNT</option>
						</select>
						<select name="DateType">
						    <option value='SettleDate'>Settle Date</option>
						    <option value='Date'>Request Date</option>
						</select>
						<input type="submit" name="submit" value="Submit"  onclick='showLoader()'>
					</div>
				</form>
				<div class="div2">
					<table cellpadding="20" border="2"> 
					
				        <?php
				            if(isset($_POST['submit']))
							{
							    $fdate = $_POST['fdate'];
								$ldate = $_POST['ldate'];
								$usrType = $_POST['usrType'];
								$txnType = $_POST['txnType'];
								$DateType = $_POST['DateType'];
								$firm_id = $_POST['firm'];
								
							    echo "<tr>
							            <th colspan='15'>$fdate To $ldate - $txnType</th>
						            </tr>";
							}
				        ?>
						<tr>
							<th>User Id</th>
							<th>Firm Name</th>
							<th>User Name</th>
							<th>GST No.</th>
							<th>User Type</th>
							<th><?php echo $txnType; ?></th>
							<th>Taxable V.</th>
							<th>IGST</th>
							<th>CGST</th>
							<th>SGST</th>
							<th>Total Tax</th>
						</tr>
						<?php
							if(isset($_POST['submit']))
							{
								$fdate = $_POST['fdate'];
								$ldate = $_POST['ldate'];
								$usrType = $_POST['usrType'];
								$txnType = $_POST['txnType'];
								$DateType = $_POST['DateType'];
								$firm_id = $_POST['firm'];
								$totalTxn=0;
								if($txnType=="DEBITAMOUNT")
								{
								    $quTxn="SELECT SUM(Debitamount), Userid AS USER FROM `recharge` WHERE Status='SUCCESS' AND Rechargedate>='$fdate' AND Rechargedate<='$ldate' GROUP BY Userid ORDER BY Userid";
								}
								else
								{
								    $quTxn="SELECT SUM(p.Amount), p.Userid AS USER, b.Accname FROM `paymentreq` p INNER JOIN bank b ON p.BankId=b.Id WHERE b.firm_id='$firm_id' AND p.Status='APPROVED' AND p.SettleStatus='SETTELED' AND p.$DateType>='$fdate' AND p.$DateType<='$ldate' GROUP BY p.Userid ORDER BY p.Userid";
								}
								$reTxn=mysqli_query($con,$quTxn);
                                while($dataTxn=mysqli_fetch_array($reTxn))
                                {   
                                    $User_Id=$dataTxn['USER'];
                                    $User_Id=$dataTxn['USER'];
                                    $FLAG=0;    $invoiceAmount=0;    $taxableValue=0;    $igst=0;    $cgst=0;    $sgst=0;    $totalTax=0;
                                    if(50==substr($User_Id, 0, 2))
                                    {
                                        $quUserName="SELECT Name, ApiActive FROM retailer WHERE Userid='$User_Id'";
                                        $reUserName=mysqli_query($con,$quUserName);
                                        if( $dataUserName=mysqli_fetch_array($reUserName))
                                        {
                                            if($dataUserName['ApiActive']==1 && $usrType=="API" || $dataUserName['ApiActive']==1 && $usrType=="ALL")
                                            {
                                                $invoiceAmount=floor($dataTxn[0]);  $userType="API";    $FLAG=1;
                                            }
                                            if($dataUserName['ApiActive']==0 && $usrType=="RETAILER" || $dataUserName['ApiActive']==0 && $usrType=="ALL")
                                            {
                                                $invoiceAmount=floor($dataTxn[0]);    $userType="RETAILER";    $FLAG=1;
                                            }   
                                        }
                                    }
                                        
                                    if(20==substr($User_Id, 0, 2) && $usrType=="DISTRIBUTER" || 20==substr($User_Id, 0, 2) && $usrType=="ALL")
                                    {
                                        $invoiceAmount=floor($dataTxn[0]);
                                        $quUserName="SELECT Name FROM distributer WHERE Userid='$User_Id'";
                                        $reUserName=mysqli_query($con,$quUserName);    $dataUserName=mysqli_fetch_array($reUserName);
                                        $userType="DISTRIBUTER";    $FLAG=1;
                                    }
                                    
                                    if($FLAG===1)
                                    {
			                            $quGst="SELECT Gst fROM gst WHERE Userid='$User_Id'";    $reGst=mysqli_query($con,$quGst);
                                        $dataGst=mysqli_fetch_array($reGst);    $GstNo=$dataGst[0];    $GstNoType=substr($GstNo,0,2);
                                        $taxableValue = $invoiceAmount*100/118;    $taxableValue=getRA($taxableValue);
                                        if($GstNoType=="09")
                                        {   
                                            $cgst = $taxableValue*9/100;    $cgst=getRA($cgst);    $sgst = $cgst;
                                        }
                                        elseif(!empty($GstNoType))
                                        {   
                                            $igst = $taxableValue*18/100;    $igst=getRA($igst);
                                        }
                                        $totalTax=$igst+$cgst+$sgst;
                                        echo "<tr>";
    	    			                    echo "<td>".$User_Id."</td>";
    	    			                    echo "<td>".$dataTxn[2]."</td>";
    	    				                echo "<td>".$dataUserName['Name']."</td>";
    	    				                echo "<td>".$GstNo."</td>";
    	    				                echo "<td>".$userType."</td>";
    	    				                echo "<td><a href='Generate_GST_invoice2.php?invoiceAmount=".$invoiceAmount."&firm_id=".$firm_id."&invoiceDate=".$ldate."&uid=".$User_Id."' target='_blank'>".$invoiceAmount."</a></td>";
				                            echo "<td>".$taxableValue."</td>";
				                            echo "<td>".$igst."</td>";
				                            echo "<td>".$cgst."</td>";
				                            echo "<td>".$sgst."</td>";
				                            echo "<td>".$totalTax."</td>";
				                        echo "</tr>";
				                        $totalTxn=$totalTxn+$dataTxn[0];
				                        $totalTaxable=$totalTaxable+$taxableValue;
				                        $totalIGST=$totalIGST+$igst;
				                        $totalCGST=$totalCGST+$cgst;
				                        $totalSGST=$totalSGST+$sgst;
                                    }
			                    }
			                    
			                    echo "<tr>";
		        	                echo"<th>All Client</th>";
		        	                echo"<th></th>";
		        	                echo"<th></th>";
		        	                echo"<th></th>";
		        	                echo"<th></th>";
		        	                echo"<th>".$totalTxn."</th>";
		        	                echo"<th>".getRA($totalTaxable)."</th>";
		        	                echo"<th>".getRA($totalIGST)."</th>";
		        	                echo"<th>".getRA($totalCGST)."</th>";
		        	                echo"<th>".getRA($totalSGST)."</th>";
		        	                echo"<th>".getRA($totalIGST+$totalCGST+$totalSGST)."</th>";
	        	                echo "</tr>";
							}
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>