<?PHP
	include'session.php';
	include'connection.php';
	include'../Retailers/functions.php';
	
	$quGetTxnId="SELECT * FROM wallet WHERE Userid='$userid' AND TxnType='Generate_Amount' ORDER BY Id DESC";
	$reGetTxnId=mysqli_query($con,$quGetTxnId);
	$dataGetTxnId=mysqli_fetch_array($reGetTxnId);
	$OldTxnId=$dataGetTxnId['TxnId'];
	$NewTxnId=$OldTxnId+1;
	
?>
<html>
<head>
	<title>Generate Amount</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, .div3{
			background:#f7f7f7;
			width:50%;
			height:auto;
			float:left;
			text-align:center;
			padding:10px;
		}
		.div1 table,.div3 table{
			width:94%;
			text-align:center;
			font-size:13px;
			border-collapse:collapse;
			margin:10px;
			padding:5px;
		}
		.div1 hr, div3 hr{
			margin:5px 10px;
			padding:0 10px;
		}
		table th{
			background:#6b5127;
			color:white;
			height:40px;
			border:solid white 1px;
			padding:4px 8px;
		}
		table td{
			background:white;
			height:40px;
			border:solid black 1px;
			padding:4px 8px;
		}
		.div1 hr, .div3 hr{width:94%;}
		.div1 input,select, textarea{
			width:90%;
			height:38px;
			margin:4px 10px;
			padding:1px 10px;
			border-radius:5px;
			border:1px solid #6b5127;
			font-size:15px;
		}
		
		
		textarea{
			height:100px;
			padding:5px 10px;
		}
		input[type=submit]{
			background:#6b5127;
			color:white;
			font-size:20px;
			transition:1s;
			width:150px;
			float:right;
			margin-right:5%;
		}
		input[type=submit]:hover{
			background:white;
			color:#6b5127;
			border:1px solid #6b5127;
			cursor:pointer;
			transition:1s;
		}
		.div3{width:98%; text-align:left;}
		.div3 input, .div3 select{
			width:120px;
			height:35px;
			margin:10px 5px;
			padding:2px 5px;
			border-radius:5px;
			border:1px solid #6b5127;
			font-size:13px;
		}
		#word{color:green; font-size:12px; padding:6px 3px; font-weight;bold}
		@media(max-width:768px)
		{
			.div1{
				width:100%;
				height:auto;
			}
			.nav,.container{
				height:auto;
			}
		}
	</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>Generate Amount :-</p></div>
				<div class="msg"><marquee><?php include'msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
			<?php
			    if(isset($_POST['Generate']))
			    {
			        $addbal=$_POST['addbal'];
				    $PayTo=$_POST['PayTo'];
				    $TxnService=$_POST['TxnService'];
				    $rmark=$_POST['rmark'];
				    $refNo=$_POST['refNo'];
				    $BankId=$_POST['BankName'];
				    $dup=$_POST['dup'];
				    
				    $quDup="SELECT * FROM wallet WHERE Txn='$addbal' AND Paymentfrom='$PayTo' AND Date='$date'";
				    $reDup=mysqli_query($con,$quDup);
				    $countDup=mysqli_num_rows($reDup);
				    
				    if($PayTo=="--Select Seller--" || $TxnService=="--Select Txn Service--" || $BankId=="--Select Bank--")
				    {
				        $msg= "Invalid Input !";
				        echo "<h3 align='center' style='color:red; font-weight:bold;'>".$msg."</h3>";
				        exit();
				    }
				    elseif(empty($userid) || empty($addbal) || empty($date))
				    {
				        $msg= "Something Empty !";
				        echo "<h3 align='center' style='color:red; font-weight:bold;'>".$msg."</h3>";
				        exit();
				    }
				    elseif($countDup>0 && $dup!="Dup")
				    {   
				        $msg= "Duplicate Txn Not Allowed";
				        echo "<h3 align='center' style='color:red; font-weight:bold;'>".$msg."</h3>";
				    }
				    else
				    {
				        $quBank="SELECT Bankname FROM bank WHERE Id='$BankId'";
	    	            $reBank=mysqli_query($con,$quBank);
		                $dataBank=mysqli_fetch_array($reBank);
			            $BankName=$dataBank['Bankname'];
				        
				        $Admin_Op_Bal=getBal($userid);
				        $Admin_Cl_Bal=$Admin_Op_Bal+$addbal;
				        $query="INSERT INTO wallet(Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,TxnBy,TxnFor,Remarks,Balance,Date,Time)
				        VALUES ('$userid','$Admin_Op_Bal','$addbal','$Admin_Cl_Bal','Generate_Amount','$PayTo','$NewTxnId','$BankId','$TxnService','$rmark','$refNo','$date','$time')";
				
    				    if(mysqli_query($con,$query))
	    			    {
		    		        $AdminEwalletId=mysqli_insert_id($con);
			        			
							$query12="UPDATE admin SET Balance='$Admin_Cl_Bal' WHERE Userid='$userid'";
							mysqli_query($con,$query12);
							if($BankId=="Pay By Cash" || $BankId=="Pay LLP Fund")
							{
							    echo "<h3 align='center' style='color:green; font-weight:bold;'>Generate & Add Amount Succefully !</h3>";
							}
							else
							{
    							
			    	            if($addbal=="-$addbal")
			    	            {
			    	                $addbal2=$addbal;
			    	            }
			    	            else
			    	            {
			    	                $addbal2=-$addbal;
			    	            }
				               $InsertTxn="INSERT INTO Bank_Account_Txn(Date,Time,Bank_Id,Bank_Name,Txn,Txn_Id,BankRefNo,Disc,Txn_For)
				                VALUES ('$date','$time','$BankId','$BankName','$addbal2','$AdminEwalletId','$refNo','Generate_Amount','$PayTo')";
				                if(mysqli_query($con,$InsertTxn))
				                {
				                    $bankSqliId=mysqli_insert_id($con);
				                    if($PayTo=="Ozzy Api")
				                    {
				                        $quApiBal="SELECT * FROM Api_Partner WHERE Api='Ozzy_Api' ORDER BY Id DESC";
                                        $reApiBal=mysqli_query($con,$quApiBal);
                                        $dataApiBal=mysqli_fetch_array($reApiBal);
                                        $clBal=$dataApiBal['ClBal'];
                                        $NetApiBal=$clBal+$addbal;
                                        $insertApiPartner="INSERT INTO Api_Partner (Date,Time,Api,TxnId,TxnType,Amount,OpBal,Txn,ClBal) 
                                        VALUES ('$date','$time','Ozzy_Api','$bankSqliId','PAYMENT','$amount','$clBal','$addbal','$NetApiBal')";
                                        mysqli_query($con,$insertApiPartner);
                                                        	  
				                    }
								    echo "<h3 align='center' style='color:green; font-weight:bold;'>Generate & Add Amount Succefully !</h3>";
				                }
							}
	    			    }
				    }
			    	
				   echo "<META HTTP-EQUIV ='Refresh' Content ='5'>";
				}
			?>
				<form method="post">
					<div class="div1">
				        <script>
				            function firmName()
				            {
				                var SellerId = document.getElementById("Seller").value;
				            
				                var xmlhttpSponser = new XMLHttpRequest();
							    xmlhttpSponser.open("GET","ajax_firmBySeller.php?SellerId="+SellerId, false)
							    xmlhttpSponser.send(null);
				                document.getElementById("firmPlace").innerHTML = xmlhttpSponser.responseText;
				            }
				        </script>
					    <input type="number" name="addbal" onkeyup="word.innerHTML=convertNumberToWords(this.value)" placeholder="Enter Amount..."></td><p id="word"></p>
				        <select name="PayTo" onchange="firmName()" id="Seller">
				            <option>--Select Seller--</option>
				            <?php
				                $quGetSeller="SELECT * FROM Seller WHERE Status='Active' ORDER BY Name ASC";
				                $reGetSeller=mysqli_query($con,$quGetSeller);
				                while($dataGetSeller=mysqli_fetch_array($reGetSeller))
				                {
				                    echo "<option>".$dataGetSeller['Name']."</option>";
				                }
				            ?>
				            <option>Other</option>
				        </select>
				        <select name="TxnService">
				        <option>--Select Txn Service--</option>
				        <option>Manage Differences</option>
				        <option>Rech_API</option>
				        <option>AIRTEL</option>
				        <option>IDEA</option>
				        <option>JIO</option>
				        <option>VODAFONE</option>
				        <option>Quick_API</option>
				        <option>Kwik_API</option>
				        <option>Unique_API</option>
				        <option>DiziPay_API</option>
				        <option>Other</option>
				    </select>
				    <select name="BankName">
			            <option>--Select Bank--</option>
			            <?php
			                $GetBank="SELECT * FROM bank WHERE Admin='SHOW'";
    			            $reGetbank=mysqli_query($con,$GetBank);
	    		            while($dataGetbank=mysqli_fetch_array($reGetbank))
		    	            {
			                   echo "<option value='".$dataGetbank['Id']."'>".$dataGetbank['Bankname']." - ".$dataGetbank['Accname']."</option>"; 
			                }
			            ?>
    			    </select>
				    <div id="firmPlace"><input type="text" name="rmark" Placeholder="Firm Name..."></div>
						<input type="text" name="refNo" placeholder="Bank Ref. No...">
						<input type="submit" name="Generate" value="Submit"  onclick='showLoader()'>
					</div>
				</form>
				<div class="div1">
					<center><h1>Bank Details</h1></center><hr>
					<table cellpadding="20" border="2">
						<tr>
							<th>Bank Name</th>
							<th><i class="fa fa-inr"></i></th>
						</tr>
						<?php
						    $quBank="SELECT * FROM bank WHERE Admin='SHOW'";
		                    $reBank=mysqli_query($con,$quBank);
		                    while($dataBank=mysqli_fetch_array($reBank))
		                    {
		                        $TotalBankBal=0;
		                        $AllBank=$dataBank['Bankname'];
		                        $BankId=$dataBank['Id'];
		            
		                        $quGetBankBal="SELECT * FROM Bank_Account_Txn WHERE Bank_Id='$BankId'";
		                        $reGetBankBal=mysqli_query($con,$quGetBankBal);
		                        while($dataGetBankBal=mysqli_fetch_array($reGetBankBal))
		                        {
		                            $Txn=$dataGetBankBal['Txn'];
		                            $TotalBankBal=$TotalBankBal+$Txn;
		                        }
		                
		                        echo "<tr>";
		                            echo "<td><a href='Bank_Account_Txn.php?BankId=".$BankId."' target='_blank'>".$AllBank." - ".$dataBank['Accname']."</a></td>";
		                            echo "<td>".floor($TotalBankBal)."</td>";
		                        echo "</tr>";
		                    }
						?>
					</table>
				</div>
				
				<div class="div3">
					<center><h1>Generated Transaction</h1></center><hr>
					<form method="post" action="GenerateAmountExport.php">
	                    <input type="date" name="fdate" value="<?php echo $date; ?>" id="inp2">
	                    <input type="date" name="ldate" value="<?php echo $date; ?>" id="inp2">
	                    <input type="submit" name="export" value="Export To Excel" id="inp2" float="left">
				    </form>
				    <form method="post">
	                    <input type="date" name="fdate" value="<?php echo $date; ?>" id="inp2">
	                    <input type="date" name="ldate" value="<?php echo $date; ?>" id="inp2">
						<select name="firm" style="width:200px; font-size:12px;">
						    <option>ALL</option>
						    <?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 id="inp2" name="seller">
	                        <option>ALL</option>
			                <?php
				                $quGetSeller2="SELECT * FROM Seller WHERE Status='Active' ORDER BY Name ASC";
				                $reGetSeller2=mysqli_query($con,$quGetSeller2);
			                    while($dataGetSeller2=mysqli_fetch_array($reGetSeller2))
			                    {
				                    echo "<option>".$dataGetSeller2['Name']."</option>";
			                    }
			                ?>
			                <option>Other</option>
	                   </select>
	                    <input type="submit" name="submit" value="Search" id="inp2">
				    </form>
					<table cellpadding="20" border="2">
						<tr>
							<th>Sl.</th>
							<th>Gen.Id</th>
							<th>Wallet.Id</th>
							<th>Date</th>
							<th>Name</th>
							<th><i class="fa fa-inr"></i></th>
							<th>Bank Name</th>
							<th>Firm</th>
							<th>Txn For</th>
							<th>RN</th>
							<th>Remarks</th>
							<th>#</th>
						</tr>
						<?php
						    $sl=0;
						    $totalAmount=0;
						    if(isset($_POST['submit']))
						    {
						        $fdate=$_POST['fdate'];     $ldate=$_POST['ldate'];     $seller=$_POST['seller'];   $bank=$_POST['bank'];   $sort=$_POST['sort'];   $firm_id = $_POST['firm'];
								if($firm_id=="ALL")
								{
								    $firmQuery="";
								}
								else
								{
								    $firmQuery="AND b.firm_id=".$firm_id;
								}
			                    
    		                    if($seller=="ALL")
    		                    {   
                                    $quPart2=" WHERE Userid='$userid' AND Date>='$fdate' AND Date<='$ldate' AND TxnType='Generate_Amount' $firmQuery";
    		                    }
    		                    else
    		                    {
    		                        $quPart2=" WHERE Userid='$userid' AND TxnWith='$seller' AND Date>='$fdate' AND Date<='$ldate' AND TxnType='Generate_Amount' $firmQuery";
    		                    }
						    }
						    else
						    {
						        $quPart2=" WHERE Userid='$userid' AND TxnType='Generate_Amount' AND Date='$date'";
						    }
						    
	$quPart1="SELECT w.TxnId, w.Id, w.Date, w.Time, w.TxnWith, w.Txn, b.Bankname, b.Accname, w.TxnFor, w.Balance, w.Remarks FROM wallet w INNER JOIN bank b ON w.TxnBy = b.Id";
						    $quGetTxn=$quPart1.$quPart2;
						    $reGetTxn=mysqli_query($con,$quGetTxn);
						    $countTxn=mysqli_num_rows($reGetTxn);
						    if($countTxn>0)
						    {   
							    while($dataGetTxn=mysqli_fetch_array($reGetTxn))
							    {
								    $sl=$sl+1;
								    echo "<tr>";
								    	echo "<td>".$sl."</td>";
									    echo "<td>".$dataGetTxn['TxnId']."</td>";
									    echo "<td>".$dataGetTxn['Id']."</td>";
									    echo "<td>".$dataGetTxn['Date']." ".$dataGetTxn['Time']."</td>";
									    echo "<td>".$dataGetTxn['TxnWith']."</td>";
									    echo "<td>".$dataGetTxn['Txn']."</td>";
									    echo "<td>".$dataGetTxn['Bankname']."</td>";
									    echo "<td>".$dataGetTxn['Accname']."</td>";
									    echo "<td>".$dataGetTxn['TxnFor']."</td>";
									    echo "<td>".$dataGetTxn['Balance']."</td>";
									    echo "<td>".$dataGetTxn['Remarks']."</td>";
									    echo "<td><a href='Edit_Generate_Amount.php?id=".$dataGetTxn['Id']."' target='_blank'>Edit</a></td>";
								    echo "</tr>";
								    $totalAmount=$totalAmount+$dataGetTxn['Txn'];
							    }
							    
								    echo "<tr>";
								    	echo "<th>".$sl."</th>";
									    echo "<th></th>";
									    echo "<th></th>";
									    echo "<th></th>";
									    echo "<th>Total Amount</th>";
									    echo "<th>".$totalAmount."</th>";
									    echo "<th></th>";
									    echo "<th></th>";
									    echo "<th></th>";
									    echo "<th></th>";
									    echo "<th></th>";
									    echo "<th></th>";
								    echo "</tr>";
						    }
						    else
						    {
						        echo "<tr>";
								    echo "<td colspan='12'><h3 style='color:red'>Data Not Found !</h3></td>";
								echo "</tr>";
						    }
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>
<script>
        function convertNumberToWords(amount) {
    var words = new Array();
    words[0] = '';
    words[1] = 'One';
    words[2] = 'Two';
    words[3] = 'Three';
    words[4] = 'Four';
    words[5] = 'Five';
    words[6] = 'Six';
    words[7] = 'Seven';
    words[8] = 'Eight';
    words[9] = 'Nine';
    words[10] = 'Ten';
    words[11] = 'Eleven';
    words[12] = 'Twelve';
    words[13] = 'Thirteen';
    words[14] = 'Fourteen';
    words[15] = 'Fifteen';
    words[16] = 'Sixteen';
    words[17] = 'Seventeen';
    words[18] = 'Eighteen';
    words[19] = 'Nineteen';
    words[20] = 'Twenty';
    words[30] = 'Thirty';
    words[40] = 'Forty';
    words[50] = 'Fifty';
    words[60] = 'Sixty';
    words[70] = 'Seventy';
    words[80] = 'Eighty';
    words[90] = 'Ninety';
    amount = amount.toString();
    var atemp = amount.split(".");
    var number = atemp[0].split(",").join("");
    var n_length = number.length;
    var words_string = "";
    if (n_length <= 9) {
        var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
        var received_n_array = new Array();
        for (var i = 0; i < n_length; i++) {
            received_n_array[i] = number.substr(i, 1);
        }
        for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
            n_array[i] = received_n_array[j];
        }
        for (var i = 0, j = 1; i < 9; i++, j++) {
            if (i == 0 || i == 2 || i == 4 || i == 7) {
                if (n_array[i] == 1) {
                    n_array[j] = 10 + parseInt(n_array[j]);
                    n_array[i] = 0;
                }
            }
        }
        value = "";
        for (var i = 0; i < 9; i++) {
            if (i == 0 || i == 2 || i == 4 || i == 7) {
                value = n_array[i] * 10;
            } else {
                value = n_array[i];
            }
            if (value != 0) {
                words_string += words[value] + " ";
            }
            if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
                words_string += "Crores ";
            }
            if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
                words_string += "Lakhs ";
            }
            if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
                words_string += "Thousand ";
            }
            if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
                words_string += "Hundred and ";
            } else if (i == 6 && value != 0) {
                words_string += "Hundred ";
            }
        }
        words_string = words_string.split("  ").join(" ");
    }
	var words_string_Upper_Case = words_string.toUpperCase();
    return words_string_Upper_Case;
}
</script>
