<?PHP
	include'session.php';
	include'../connection.php';
	include'../functions.php';
?>
<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="../main_script.js"></script>
	<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;
			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']))
			    {
			        $TxnTyp   = $_POST['TxnTyp'];
			        $Amount   = $_POST['Amount'];
				    $SellerId = $_POST['Seller'];
				    $Service  = $_POST['Service'];
				    $BankId   = $_POST['BankId'];
				    $FirmId   = $_POST['FirmId'];
				    $RefNo    = $_POST['RefNo'];
				    
				    $IJstNbw = "INNER JOIN bank_wallet ON seller_txn.BankWalletId=bank_wallet.Id";
				    $where1  = "seller_txn.Amount='$Amount'";
				    $where2  = "AND bank_wallet.BankRefNo='$RefNo'";
				    $where3  = "AND seller_txn.Date='$date'";
				    $quDup    = "SELECT seller_txn.Id FROM seller_txn $IJstNbw WHERE $where1 $where2 $where3"; 
				    $reDup    = mysqli_query($con,$quDup);
				    $countDup = mysqli_num_rows($reDup);
				    if($SellerId=="--Select Seller--" || $Service=="--Select Txn Service--" || $FirmId=="--Select Seller Firm--")
				    {
				        $msg= "Invalid Input !";
				        echo "<h3 align='center' style='color:red; font-weight:bold;'>".$msg."</h3>";
				        exit();
				    }
				    elseif($TxnTyp=="BANKING" && $BankId=="--Select Bank--")
				    {
				        $msg= "Select Bank Please !";
				        echo "<h3 align='center' style='color:red; font-weight:bold;'>".$msg."</h3>";
				        exit();
				    }
				    elseif(empty($userid) || empty($Amount))
				    {
				        $msg= "Something Empty !";
				        echo "<h3 align='center' style='color:red; font-weight:bold;'>".$msg."</h3>";
				        exit();
				    }
				    elseif($countDup>0)
				    {
				        $msg= "Duplicate Txn Not Allowed !";
				        echo "<h3 align='center' style='color:red; font-weight:bold;'>".$msg."</h3>";
				        exit();
				    }
				    else
				    {
				        $quSeller = "INSERT INTO seller_txn (Date,Time,SellerId,FirmId,Amount,TxnType) 
				        VALUES ('$date','$time','$SellerId','$FirmId','$Amount','$TxnTyp')";
				        if(mysqli_query($con,$quSeller))
				        {
				            $sellerInsertId = mysqli_insert_id($con);
				            
				            $AdminOpBal=getBal($userid);
				            $AdminClBal=$AdminOpBal+$Amount;
				            $query="INSERT INTO wallet(Date,Time,Userid,OpBal,Txn,ClBal,TxnId,TxnType,TxnWith)
				            VALUES ('$date','$time','$userid','$AdminOpBal','$Amount','$AdminClBal','$sellerInsertId','SellerTxn','$SellerId')";
    				        if(mysqli_query($con,$query))
	    			        {
	    			            $walletId = mysqli_insert_id($con); 
		    		            if($TxnTyp=="BANKING")
			        		    {
							        $BankOpBal = getBankBal($BankId);
							        $BankClBal = $BankOpBal-$Amount;
    							    if($Amount<0)
    							    {
    							        $Amount=abs($Amount);
    							    }
    							    else
    							    {
    							        $Amount=-$Amount;
    							    }
			    	                $InsertTxn="INSERT INTO bank_wallet(BankId,Date,Time,OpBal,Txn,ClBal,TxnId,TxnType,TxnWith,BankRefNo)
				                    VALUES ('$BankId','$date','$time','$BankOpBal','$Amount','$BankClBal',$sellerInsertId,'SellerTxn','$SellerId','$RefNo')";
				                    if(mysqli_query($con,$InsertTxn))
				                    {
				                        $bankSqliId=mysqli_insert_id($con);
				                        $update = "UPDATE seller_txn SET TxnWith='$BankId', TxnFor='$walletId', BankWalletId='$bankSqliId' WHERE Id='$sellerInsertId'";
				                        if(mysqli_query($con,$update))
				                        {
								            echo "<h3 align='center' style='color:green; font-weight:bold;'>Generate & Add Amount Succefully !</h3>";
				                        }
				                    }
				                }
				                else
				                {   
								    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">
				    <input type="number" name="Amount" onkeyup="word.innerHTML=convertNumberToWords(this.value)" placeholder="Enter Amount..."></td><p id="word"></p>
			        <select name="TxnTyp" id="txntype" onchange="showHideSelect()">
				        <option>BANKING</option>
				        <option>EXCHANGE</option>
				        <option>CASHPAID</option>
				    </select>
			        <select name="Seller" onchange="getFirmBySellerId('--Select Firm--')" id="SellerId2">
			            <option>--Select Seller--</option>
				            <?php
				                $quSeller = "SELECT * FROM Seller WHERE Status='Active' ORDER BY Name ASC";
				                $reSeller = mysqli_query($con,$quSeller);
				                while($dataSeller=mysqli_fetch_array($reSeller))
				                {
				                    echo "<option value='".$dataSeller['Id']."'>".$dataSeller['Name']." - ".$dataSeller['City']."</option>";
				                }
				            ?>
			            <option>Other</option>
			        </select>
		    	    <SELECT name="FirmId" id="FirmPlace2">
			            <OPTION>--Select Firm--</OPTION>
			        </SELECT>
			        <select name="Service">
		    	        <option>--Select Service--</option>
			            <option>Manage Differences</option>
			            <option>AIRTEL</option>
			            <option>VI</option>
		   		        <option>JIO</option>
		    	        <option>BSNL</option>
			            <option>API</option>
			            <option>Other</option>
			        </select>
				    <select name="BankId" id="bank">
			            <?php
			                $quBank    = "SELECT * FROM bank WHERE Status='ACTIVE'";
    			            $reBank    = mysqli_query($con,$quBank);
    			            $countBank = mysqli_num_rows($reBank);
    			            if($countBank>1)
    			            {
    			                echo "<option>--Select Bank--</option>";
    			            }
	    		            while($dataBank=mysqli_fetch_array($reBank))
		    	            {
			                   echo "<option value='".$dataBank['Id']."'>".$dataBank['BankName']." - ".$dataBank['AccName']."</option>"; 
			                }
			            ?>
    			    </select>
						<input type="text" name="RefNo" placeholder="Bank Ref. No..." id="txnInp">
						<input type="submit" name="Generate" value="Submit"  onclick='showLoader()' style="float:right">
					</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 Status='ACTIVE'";
		                    $reBank=mysqli_query($con,$quBank);
		                    while($dataBank=mysqli_fetch_array($reBank))
		                    {
		                        $Bankname = $dataBank['BankName'];
		                        $BankId   = $dataBank['Id'];
		            
		                        $quBankBal   = "SELECT SUM(Txn) FROM bank_wallet WHERE BankId='$BankId'";
		                        $reBankBal   = mysqli_query($con,$quBankBal);
		                        $dataBankBal = mysqli_fetch_array($reBankBal);
		                        $bankBal     = $dataBankBal[0];
		                
		                        echo "<tr>";
		                            echo "<td><a href='Bank_Account_Txn.php?BankId=".$BankId."' target='_blank'>".$Bankname." - ".$dataBank['AccName']."</a></td>";
		                            echo "<td>".floor($bankBal)."</td>";
		                        echo "</tr>";
		                    }
						?>
					</table>
				</div>
				<div class="div3">
					<center><h1>Generated Transaction</h1></center><hr>
					<form method="post" action="exportSellerTxn.php">
	                    <input type="date" name="fdate" value="<?php echo $date; ?>" id="inp2">
	                    <input type="date" name="ldate" value="<?php echo $date; ?>" id="inp2">
	                    <select name="groupBy">
	                        <option>ALL</option>
	                        <option value="SellerId">SELLER</option>
	                        <option value="FirmId">FIRM</option>
	                    </select>
	                    <input type="submit" name="export" value="Export To Excel" id="inp2">
				    </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="seller" id="SellerId" onchange="getFirmBySellerId('ALL')">
	                        <option>ALL</option>
			                <?php
				                $quSeller = "SELECT * FROM Seller WHERE Status='Active' ORDER BY Name ASC";
				                $reSeller = mysqli_query($con,$quSeller);
			                    while($dataSeller = mysqli_fetch_array($reSeller))
			                    {
				                    echo "<option value='".$dataSeller['Id']."'>".$dataSeller['Name']."</option>";
			                    }
			                ?>
			                <option>Other</option>
	                   </select>
						<select id="FirmPlace" name="firm" style="width:200px; font-size:12px;">
						    <option>ALL</option>
						</select>
	                    <input type="submit" name="submit" value="Search" id="inp2">
				    </form>
					<table cellpadding="20" border="2">
						<tr>
							<th>Sl.</th>
							<th>ID</th>
							<th>Date<br>Time</th>
							<th>Seller</th>
							<th>Firm</th>
							<th><i class="fa fa-inr"></i></th>
							<th>TxnType</th>
							<th>Bank<br>Firm</th>
							<th>Ref. No.</th>
							<th>Action</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    = $_POST['firm'];
						        
						        $whereQu = "seller_txn.Date>='$fdate' AND seller_txn.Date<='$ldate'";
						        if($seller!="ALL")
						        {   
	    							$whereQu = "seller_txn.SellerId='$seller' AND $whereQu";
						            if($firm!="ALL")
		    						{
			    					    $whereQu = "seller_txn.FirmId='$firm' AND $whereQu";
				    				}
						        }
						    }
						    else
						    {   //$date = "2025-08-01";
						        $whereQu = "seller_txn.Date='$date'";
				            }
				            
				            $FDseller_txn    = "seller_txn.Id, seller_txn.Date, seller_txn.Time, seller_txn.Amount, seller_txn.TxnType, seller_txn.TxnFor,";
				            $FDSeller        = "Seller.Name,";
				            $FDseller_firm   = "seller_firm.FirmName,";
				            $FDbank          = "bank.BankName, bank.AccName,";
				            $FDbank_wallet   = "bank_wallet.BankRefNo, bank_wallet.Id,";
				            $FDorder_req     = "order_req.Userid,";
				            $FDuser          = "user.Name";
				            
						    $fetchData    = $FDseller_txn.$FDSeller.$FDseller_firm.$FDbank.$FDbank_wallet.$FDorder_req.$FDuser;
						    
					        $IJs_tNSeller = "LEFT JOIN Seller ON seller_txn.SellerId=Seller.Id";
					        $IJs_tNb_W    = "LEFT JOIN bank_wallet ON seller_txn.BankWalletId=bank_wallet.Id";
					        $IJs_tNb      = "LEFT JOIN bank ON seller_txn.TxnWith=bank.Id";
					        $IJs_tNFirm   = "LEFT JOIN seller_firm ON seller_txn.FirmId=seller_firm.Id";
					        $IJs_tNOrder  = "LEFT JOIN order_req ON seller_txn.OrderReqId=order_req.Id";
					        $IJs_tNuser   = "LEFT JOIN user ON order_req.Userid=user.Userid";
					        
						    $quGetTxn     = "SELECT $fetchData FROM seller_txn $IJs_tNb_W $IJs_tNb $IJs_tNSeller $IJs_tNFirm $IJs_tNOrder $IJs_tNuser WHERE $whereQu ORDER BY seller_txn.Id";    
						    $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[0]."</td>";
									    echo "<td>".$dataGetTxn[1]."<br>".$dataGetTxn[2]."</td>";
									    echo "<td>".$dataGetTxn[6]."</td>";
									    echo "<td>".$dataGetTxn[7]."</td>";
									    echo "<td>".$dataGetTxn[3]."</td>";
									    echo "<td>".$dataGetTxn[4]."</td>";
									    echo "<td>";
									        if(!empty($dataGetTxn[8] || !empty($dataGetTxn[9])))
									        {
									            echo $dataGetTxn[8]."<br>".$dataGetTxn[9];
									        }
									        if(!empty($dataGetTxn[13] || !empty($dataGetTxn[12])))
									        {
									            echo $dataGetTxn[13]."<br>".$dataGetTxn[12];
									        }
									    echo "</td>";
									    echo "<td>".$dataGetTxn[10]."</td>";
									    echo "<td>";
									    $ActionName = "Action1";
									    $ActionData = "ActionName=".$ActionName."&sellerTxnId=".$dataGetTxn[0]."&bankWalletId=".$dataGetTxn[11]."&WalletId=".$dataGetTxn[5];
									        echo "<a href='edit_generate_amount.php?id=".$dataGetTxn[0]."' target='_blank'><i class='fa fa-pencil'></i></a>";
									        echo "<a href='Action.php?$ActionData' target='_blank' style='margin-left:20px;'><i class='fa fa-trash' aria-hidden='true'></i></a>";
									    echo "</td>";
								    echo "</tr>";
								    $totalAmount=$totalAmount+$dataGetTxn['Amount'];
							    }
							    
								    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 "</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>
