<?
session_start();
include('includes/config.php');
include('defs.php');
if(strlen($_SESSION['aid'])==0)
  { header('location:index.php');
}

$usr=$_SESSION['uname'];


echo "                <form name='sub_head' action='' method=POST onSubmit='return  validateForm();'>";




?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  <title>GLAKB  | Sub Head</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='Add-Head' ");
$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>

 

       
        .input-box {
            width: 100%;
            padding: 4px;
            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: 8px;
}

#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>   


<script language="javascript" type="text/javascript">

window.onload= function show()
                        {
                                if(document.sub_head.strid.value =="")
                                        {

                                                document.sub_head.strid.focus()
                                        }
                                        }

function GetDetail(str) {
            if (str.length == 0) {
                document.getElementById("uby1").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("uby1").value = myObj[0];
                          
                                            }
                };
  
                // xhttp.open("GET", "filename", true);
                xmlhttp.open("GET", "dupi.php?strnoc=" + str, true);
                  
                // Sends the request to the server
                xmlhttp.send();
            }
        } 
  function show_cbo1()
                {
                        var val = document.getElementById('strmhead').value;
                        
                        location.href = "acchead.php?D_Name="+val;
                }

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 validateForm()
{

if((document.sub_head.strmhead.value) == '')
		
	{
			alert("Please Select Accounts Cost Center!");
document.sub_head.strmhead.focus();

			return false;
		}
        if((document.sub_head.strid.value) == '')
{
alert("Please Enter Accounts Code!");
document.sub_head.strid.focus();
return false;
}
if((document.sub_head.name.value) == '')
{
alert("Please Enter Accounts Head Name!");
document.sub_head.name.focus();
return false;
}

if((document.sub_head.details.value) == '')
        
    {
            alert("Please Enter Accounts Head Description!");
document.sub_head.details.focus();

            return false;
        }

return true;
}

  </script>
        <table align="center" id="customers">
       
<tr>
    <th colspan="4"><font size="5">ADD ACCOUNTS HEAD</th>

  </tr>


 <td><b><font size=3 color=black>Select Main Category :</b></td>
                        <td colspan="">
                                <select class="form-control" name="strmhead" id="strmhead" style=font-size="20" onchange="show_cbo1()">
                                <option value="">Select</option>
                                <?php
                                if(isset($_GET['D_Name']))
                                        {
                                                $D_Name = $_GET['D_Name'];

                                                   }
                                else
                                        $D_Name = "";

                                        include('config.php');
                                        $result = mysqli_query($link, "select Name from mheads where Name!='Customer' order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($D_Name== $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }


$result = mysqli_query($link, "SELECT MAX(SNO) as fno FROM acc_heads WHERE M_Head='$D_Name' ");
while($row = mysqli_fetch_object($result))
{
$fno = $row->fno;
}
$fno1 = $fno+1;


$result420 = mysqli_query($link, "select M_Id from mheads where Name='$D_Name'");
$row420 =  mysqli_fetch_object($result420);
$cde1 = $row420->M_Id;


$ac_code = str_pad($fno1, 4, "0", STR_PAD_LEFT);


$fcode=$cde1."-".$ac_code;
 ?>

<td colspan="4">
<b><font size=3 color="black">Accounts-ID:</b>
<input type='text' size='6' name="strid" id='strid' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:20px;' value="<?php echo $fcode ?>" readonly >

 

                                       
  <tr>       
<td><b><font size=3 color=black>Account's Head Name:</b></td>
<td>
      <input type="text" name="name" id="name" class="input-box" autofocus>


  
<td><b><font size=3 color=black>Account's Head Description:</b></td>
<td colspan="3"> <input type="text" name="details" class="input-box">
</td>

<tr>
<td><b><font size=3 color=black>CNIC:</b></td>
<td> <input type="text" name="cnic" class="input-box">
</td>

<td><b><font size=3 color=black>NTN:</b></td>
<td> <input type="text" name="ntn1" class="input-box">
</td>
    


<tr>
<td><b><font size=3 color=black>Contact Person (If any):</b></td>
<td> <input type="text" name="cname" class="input-box">
</td>

<td><b><font size=3 color=black>Contact Number:</b></td>
<td> <input type="text" name="cont" class="input-box">
</td>

<tr>
<td><b><font size=3 color=black>Address:</b></td>
<td colspan="3"> <input type="text" name="adr" class="input-box">
</td>

          
<tr>                                      


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

  </td>        
 <?
            if(isset($_POST['del']))
        {
                //#################################################################
                //DELETE DATA FROM THE TEMPORARY TABLE BASED ON DELETE BUTTON VALUE
               include('config.php');
                $rb    = trim($_POST['radiobutton']);   // Delete Key



$strQuery = "select Company, Name from acc_heads where Id='$rb'";

                $nResult = mysqli_query ($link, $strQuery);
                $rstRow = mysqli_fetch_array($nResult);
                $comp= $rstRow["Company"];
                $name= $rstRow["Name"];
        

$strQuery1 = "select count(*) as cnt from ledgers where Code='$name' and Company='$comp'";

                $nResult1 = mysqli_query ($link, $strQuery1);
                $rstRow1 = mysqli_fetch_array($nResult1);
                $counter= $rstRow1["cnt"];


        if($counter>0)
                        {
echo "<script language='javascript'>";
 echo "alert('Sorry: The Accounts Head can not be Deleted as it has Ledger Entries.')";
                                                                echo "</script>";
                                                                
                                                            }
    elseif ($counter==0)
    {

$result = mysqli_query($link, "delete from acc_heads where Id = '$rb' ");
echo "<script language='javascript'>";
echo "alert('Accounts Head Deleted Successfully ')";
echo "</script>";

          }

}

if(isset($_POST['save']))
{
date_default_timezone_set ("Asia/Karachi");
$the_time = date('d-m-Y (h:i:s a)', time());
$strmhead=$_POST['strmhead'];
$name = rtrim($_POST['name']);
$cname=$_POST['cname'];
$det=$_POST['details'];
$ntn1=$_POST['ntn1'];
$cnic=$_POST['cnic'];
$cont=$_POST['cont'];
$adr=$_POST['adr'];
$aid=$_POST['strid'];
$strQuery = "select count(Name) as cnt from acc_heads where Name='$name' ";
$nResult = mysqli_query ($link, $strQuery);
$rstRow = mysqli_fetch_array($nResult);
$counter= $rstRow["cnt"];
if($counter>0)
{
echo "<script language='javascript'>";
echo "alert('Sorry: The Accounts Head Already Exists!')";
echo "</script>";
goto donot;
}

mysqli_query($link, "insert into acc_heads (Name, M_Head, Contact_No, C_Person, Adress, addby, addon, Active, A_Id, CNIC1, Details, NTN1, SNO, M_Id) values ('$name','$strmhead','$cont','$cname','$adr','$usr','$the_time','Y','$aid','$cnic','$det','$ntn1','$fno1','$cde1')");



echo "<script language='javascript'>";
echo "alert('The Accounts Head added Successfully!')";
echo "</script>";
$link1 = "<script>window.open('acchead.php','_self')</script>";
echo $link1; 
donot:



}





  a:      
  include_once('includes/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  ?>