<?php
session_start();
include('includes/config.php');
include('defs.php');

if(strlen($_SESSION['aid'])==0)
  { header('location:index.php');
}

$usr=$_SESSION['uname'];



 ?>

 <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>GLAKB  | Amount Receiving</title>

  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../plugins/fontawesome-free/css/all.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../dist/css/adminlte.min.css">
  <!--Function Email Availabilty---->

</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
  <!-- Navbar -->
<?php include_once("includes/navbar.php");?>
  <!-- /.navbar -->

  <!-- Main Sidebar Container -->
 <?php include_once("includes/sidebar.php");?>

 
  <div class="content-wrapper">
<?
$result12 = mysqli_query($link, "select Role from user_details where Name='$usr' ");
$row12=  mysqli_fetch_object($result12);
$role = $row12->Role;
if($role=='Operator')
{
$result = mysqli_query($link, "select Role from user_priv where Name='$usr' and Form='Cash-Rec' ");
$row=  mysqli_fetch_object($result);
$role = $row->Role;
if($role=='0')
{
{
echo"<center>";
echo"<br><br><br><br><br>";
echo"<img src='users.png' width='160' height='160'><br>";
echo "<font size=6 color=red><b>ACCESS DENIED!</b><br>";
echo "<font size=4 color=darkblue><b>You Do Not Have Permission to View This Page.</b><br>";
goto a;
}
}
}
?> 

<style>
#suggestions1 {
            border: 2px solid #ccc;
            display: none;
            position: absolute;
            background-color: white;
            width: 300px;
            max-height: 250px;
            overflow-y: auto;
            z-index: 1000;
        }
        #suggestions1 div {
            padding: 3px;
            cursor: pointer;
        }
        #suggestions1 div:hover,
        #suggestions1 .selected {
            background-color: #f0f0f0;
        }

        .input-box {
            width: 100%;
            padding: 2px;
            margin-top: 2px;
            margin-bottom: 2px;
            border: 2px solid gainsboro;
            border-radius: 4px;
            font-size: 16px;

        }
#customers {
  font-family: Times-Roman;
  border-collapse: collapse;
  width: 80%;
}

#customers td, #customers th {
  border: 1px solid #4444;
  padding: 4px;
}

#customers tr:nth-child(even){background-color:gainsboro ;}


#customers th {
  padding-top: 8px;
  padding-bottom: 8px;
  text-align: center;
background-color:dodgerblue;
    color: white;
}
</style>   
<link rel="stylesheet" href="css/style.css">
<script language="javascript">

 window.onload= function show()
                        {
                                if(document.voucher.ext.value =="")
                                        {

                                                document.voucher.ext.focus()
                                        }
                                        }

function GetDetail1(str) {
            if (str.length == 0) {
                document.getElementById("ext").value = "";
                return;
            }
            else {
  
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function () {
                      if (this.readyState == 4 && 
                            this.status == 200) {
                          
                        var myObj = JSON.parse(this.responseText);
                          
                        document.getElementById
                            ("ext").value = myObj[0];
                          
                                            }
                };
  
                // xhttp.open("GET", "filename", true);
                xmlhttp.open("GET", "dupi1.php?strnoc=" + str, true);
                  
                // Sends the request to the server
                xmlhttp.send();
            }
        } 
function GetDetail2(str) {
            if (str.length == 0) {
                document.getElementById("searchBox1").value = "";
                return;
            }
            else {
  
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function () {
                      if (this.readyState == 4 && 
                            this.status == 200) {
                          
                        var myObj = JSON.parse(this.responseText);
                          
                        document.getElementById
                            ("searchBox1").value = myObj[0];
                          
                                            }
                };
  
                // xhttp.open("GET", "filename", true);
                xmlhttp.open("GET", "dupi2.php?strnoc=" + str, true);
                  
                // Sends the request to the server
                xmlhttp.send();
            }
        } 
function setValue()
{
        var val = document.getElementById("dt").value;
        document.getElementById("date").value=val;
}
 function formconfirm() {
return confirm("You Clicked the Button. Continue?<? echo "$coursename";?>");
}

function show_cbo11()
                {
                        var val2 = document.getElementById('cash_type').value;
                       
                        location.href = "cash_rec.php?M_Headc="+val2;
                        
                }


                
            
                
                  function processkey(e)
                {
                        var kc = (window.Event?e.which:e.keyCode)
                        if (kc >=48 && kc<= 57 || kc ==8 || kc == 46)
                                return true
                        else
                                return false

     
           }

function openAndPrintPDF() 
{
var pdfWindow = window.open('rep_rec.php?usr=<?php echo $usr ?>', '_blank');
}
///////////////////////////////////////////


function validateForm()
	{
		




if((document.voucher.cash_type.value) == '')
		
	{
			alert("Please Select Deposited in First!");
document.voucher.cash_type.focus();

			return false;
		}

if((document.voucher.ext.value) == '')
                
        {
alert("Please Enter Accounts Code!");
document.voucher.ext.focus();

                        return false;
                }


	if((document.voucher.searchBox1.value) == '')
		
	{
			alert("Please Enter Accounts Head Name!");
document.voucher.searchBox1.focus();

			return false;
		}



if((document.voucher.gamt.value) == '' || (document.voucher.gamt.value) == 0 )
{
alert("Please Enter Gross Amount.");
document.voucher.gamt.focus();
return false;
}

return true;
}




//////////////////////////////////////////////////////











                
</script>





<form name="voucher" action="" method="POST" onSubmit = "return  validateForm();">



        <table align="center" id="customers">
       
<tr>
    <th colspan="4"><font size="5">AMOUNT RECEIVING VOUCHER</th>

  </tr>
 
<?
$result12 = mysqli_query($link, "select MAX(vch_number) as inv from ledgers" );
while($row12  = mysqli_fetch_object($result12))
{
$inv=$row12->inv+1;
}
?>
<td colspan="4" align="right"><b><font size=4 color="black">Voucher#</b>
<?

echo "<input type='text' id='strinv' name='strinv' size='5' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:25px;' value='$inv' readonly   />";



$strvoucher= Date("Y-m-d");
if(isset($_POST['strDate']) && !isset($_POST['reset']) )
{
$strvoucher=$_POST['strDate'];
AddDate1("<b><font size=4 color=black>Voucher Date: </b> &nbsp;", "strDate", $strvoucher, 10, 10, 0);         
}
else
{
AddDate1("<b><font size=4 color=black>Voucher Date: </b> &nbsp;", "strDate", $strvoucher, 10, 10, 0);         
}
echo "</td>";
?>


  
<tr>
                        <td align=left><b><font size=4 color=black>Amount Deposited In:</b></td>
                        <td align=left colspan="2">
                                <select class="form-control" name="cash_type" id="cash_type" style=font-size="16" onchange="show_cbo11()">
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['M_Headc']))
                                        {
                                                $M_Headc = $_GET['M_Headc'];
                                                
                                                
                
                                        }
                                else                        
                                        $M_Headc = "";
                                        
                                        
                                        $result = mysqli_query($link, "select Name from acc_heads where M_Head='Assets' order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($M_Headc == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>
                                </select>

<?
$rst = MySQLi_Query($link, "Select Details from acc_heads where Name='$M_Headc'");

while($rt = mysqli_fetch_array($rst))
{
$det=$rt['Details'];
}
echo "<td align=right colspan=1><b><input type='text' class='input-box' name='desc' size='10' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='desc' value='$det' >";

?>

<tr>

<td width="200"><b><font size=4 color="black">Received From (Code):</b></td>

    
<?

echo "<td align=right><b><input type='text' class='input-box' name='ext' size='10' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='ext' value='' onkeyup='GetDetail2(this.value)' required >";

?>
    


<td width="200"><b><font size=4 color="black">Received From (Name):</b></td>
<td colspan="1">


    <input type="text" id="searchBox1" name="searchBox1" class='input-box' placeholder="Click to search..." autocomplete="off" onkeyup='GetDetail1(this.value)' onkeydown="return event.key !== 'Enter';">
    <div id="suggestions1"></div>

    <script>
        const searchBox1 = document.getElementById("searchBox1");
        const suggestions1 = document.getElementById("suggestions1");
        let selectedIndex1 = -1;

        searchBox1.addEventListener("click", fetchSuggestions1);
        searchBox1.addEventListener("keyup", function(e) {
            const items1 = suggestions1.querySelectorAll("div");

            if (e.key === "ArrowDown") {
                selectedIndex1 = (selectedIndex1 + 1) % items1.length;
                updateSelection1(items1);
            } else if (e.key === "ArrowUp") {
                selectedIndex1 = (selectedIndex1 - 1 + items1.length) % items1.length;
                updateSelection1(items);
            } else if (e.key === "Enter" && selectedIndex1 > -1) {
                searchBox1.value = items1[selectedIndex1].innerText;
                suggestions1.style.display = "none";
            } else {
                fetchSuggestions1();
            }
        });

        function updateSelection1(items) {
            items.forEach((item, i) => {
                item.classList.toggle("selected", i === selectedIndex1);
            });
        }

        function fetchSuggestions1() {
            const query = searchBox1.value;
            fetch(`search2.php?q=${encodeURIComponent(query)}`)
                .then(response => response.json())
                .then(data => {
                    suggestions1.innerHTML = "";
                    data.forEach(name => {
                        const div = document.createElement("div");
                        div.textContent = name;
                        div.onclick = () => {
                            searchBox1.value = name;
                            suggestions1.style.display = "none";
                        };
                        suggestions1.appendChild(div);
                    });
                    suggestions1.style.display = data.length ? "block" : "none";
                    selectedIndex1 = -1;
                });
        }
    </script>

<tr align="left">
<td><b><font size=4 color=black>Amount Received:</b></td>
        <?
echo "<td colspan=3><b><input type='text' name='gamt' class='input-box' size='16' style='color: black; font-family: Times-Roman; font-size:2' id='gamt' value='0' onkeypress='return processkey(event)' onkeyup='myFunction()'>";
?>
<tr>
<td><b><font size=4 color=black>Mode of Payment:</b></td>
<td>
<select name=pmode class='input-box'>
<option value='NIL'>NIL</option>
<option value='By Cheque'>By Cheque</option>
<option value='By Cash'>By Cash</option>
<option value='Online'>Online</option>
<option value='Others'>Others</option>

</select><td><b><font size=4 color=black>Cheque No. (If any):</b></td>
<td colspan="1"><input type="text" name="chq" class="input-box" size="25" /></td>
<tr>
<td align="left"><b><font size=4 color=black>Remarks (If any):</b></td>
                        <td align="left" colspan="3"><input type="text" name="strrem" class="input-box"  size="87" onkeydown="return tab(event)" /></td>
                        
                </tr>

                
                


      <td colspan="4" align="center">

<input type="submit" name="save" value="Save Voucher" style="background: none; color:#007BFF; border:2px solid #007BFF; padding:2px 20px; border-radius:5px;">

<tr>
<td colspan="5" align="right">
<span class="icon text-white-50">
<i class="fas fa-book"></i>
</span>
<button class="btn btn-info btn-icon-split" onclick="openAndPrintPDF()"><span class="text">VIEW VOUCHER</span></button>



  <?
                                                 


if(isset($_POST['save']))
        {


date_default_timezone_set ("Asia/Karachi");
$the_time = date('d-m-Y (h:i:s a)', time());

$date = strtotime($_POST['strDate']);
$date = date("Y-m-d",$date);
$name = $_POST['searchBox1'];
$idd = $_POST['ext'];
$fhead = $_POST['cash_type'];

$gamt = $_POST['gamt'];

$rems = $_POST['strrem'];
$chq = $_POST['chq'];
$pmode = $_POST['pmode'];

$result = mysqli_query($link, "select max(vch_number) as v_no2 from ledgers ");
$row =  mysqli_fetch_object($result);
$v_no2 = $row->v_no2 + 1;

$cpVch = str_pad($v_no2, 6, "0", STR_PAD_LEFT);
$cpVch = "CR".$cpVch;



                        
$result13 = mysqli_query($link, "select A_Id from acc_heads where Name='$fhead'");
$row13 =  mysqli_fetch_object($result13);
$aid = $row13->A_Id;

                        
  mysqli_query($link, "insert into ledgers (M_Head, vch_number, vch, Code, Details ,Debit ,D_Date, usr, L_Type, V_Time, Chq, Pmode, A_Id) values ('Assets','$v_no2','$cpVch','$fhead','$rems','$gamt','$date','$usr','CR','$the_time','$chq','$pmode','$aid')");


$result12 = mysqli_query($link, "select M_Head, Salesman from acc_heads where A_Id='$idd' and Name='$name'");
$row12 =  mysqli_fetch_object($result12);
$mhead = $row12->M_Head;
$sman = $row12->Salesman;
                        
 mysqli_query($link, "insert into ledgers (M_Head, vch_number, vch, Code, Details ,Credit ,D_Date, usr, L_Type, V_Time, Chq, Pmode, A_Id, Salesman) values ('$mhead','$v_no2','$cpVch','$name','$rems','$gamt','$date','$usr','CR','$the_time','$chq','$pmode','$idd','$sman')");

echo "<script language='javascript'>";
echo "alert('Successfully Received!')";
echo "</script>";

$link = "<script>window.open('cash_rec.php','_self')</script>";
echo $link;

}
a:
        include "footer.php";
?>
</div>
<!-- ./wrapper -->

<!-- jQuery -->
<script src="../plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- bs-custom-file-input -->
<script src="../plugins/bs-custom-file-input/bs-custom-file-input.min.js"></script>
<!-- AdminLTE App -->
<script src="../dist/js/adminlte.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../dist/js/demo.js"></script>
<!-- Page specific script -->
<script>
$(function () {
  bsCustomFileInput.init();
});
</script>
</body>
</html>
<?php  ?>