<?
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  | Edit Voucher </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">




 
<html>
<head>
<style>
#customers {
  font-family: Times-Roman;
  border-collapse: collapse;
  width: 98%;
}

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

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


#customers th {
  padding-top: 5px;
  padding-bottom: 5px;
  text-align: center;
  background-color:dodgerblue;
  color: white;
}


#customers1 {
  font-family: Times-Roman;
  border-collapse: collapse;
  width: 100%;
}

#customers1 td, #customers1 th {
  border: 0px solid #4444;
  padding: 3px;
}

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


#customers1 th {
  padding-top: 6px;
  padding-bottom: 6px;
  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.stramt.value =="")
                                        {

                                                document.voucher.stramt.focus()
                                        }
                                        }

function openAndPrintPDF() 
{
var pdfWindow = window.open('v_print_direct.php?usr=<?php echo $usr ?>', '_blank');
}
 function formconfirm() {
return confirm("You Clicked the Button. Continue?<? echo "$coursename";?>");
}
                
                function show_cbo2()
                {
                        
                        var val = document.getElementById('item').value;
                        var val2= document.getElementById('item2').value;
                       
                        location.href = "voucher2.php?M_Head="+val+"&Code="+val2;
                }
                

                
function show_cbo1()
                {
                        var val = document.getElementById('item3').value;
                        
                        location.href = "voucher2.php?Name="+val;
                }



 function show_cbo4()
                {
var val = document.getElementById('item').value;
location.href = "voucher2.php?M_Head="+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

                }
                
</script>



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

<table align="center" id="customers">
       
<tr>
    <th colspan="4"><font size="4">EDIT DAILY TRANSACTIONS</th>

  </tr>
  
<td colspan="4" align="right"><b><font size=3 color="black">Voucher#</b>
<?
$result = mysqli_query($link, "select Distinct Vch_Number, L_Type, V_Date from edit_vch where usr='$usr' ");
while($row = mysqli_fetch_object($result))
{
$vch=$row->Vch_Number;
$vtype=$row->L_Type;
$vdate=$row->V_Date;
}
$vdate = strtotime($vdate);
$vdate = date("d-m-Y",$vdate);

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

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


echo "</td>";
?>
<TR>

                        <td><b><font size=3 color=black>Select Main Cost Center:</b></td>
                        <td align="left" >
                                <select class="form-control" name="item" id="item" style=font-size="16" onchange="show_cbo4()">
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['M_Head']))
                                        {
                                                $M_Head = $_GET['M_Head'];
                                                
                                                
                
                                        }
                                else                        
                                        $M_Head = "";
                                        
                                        //include('config.php');
                                        $result = mysqli_query($link, "select distinct Name from mheads order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($M_Head == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>
                                </select>
                                
                    
                        <td><b><font size=3 color=black>Select Head's Name: </font></b></td>
                        <td>
                                <select class="form-control" name="item2" id="item2" style=font-size="16" onchange="show_cbo2()">
                                <option value="">select</option>
                                        <?php
                                                
                                                if(isset($_GET['M_Head']))
                                                        {
                                                        $M_Head = $_GET['M_Head'];
                                                        $Code = $_GET['Code'];
                                                        
                                                        
                                                        }
                                                else                        
                                                        $Code = "";
                                                                
                                                if(isset($_GET['M_Head']))
                                                {
                                                        $M_Head = $_GET['M_Head'];
                                                        $M_Head1 = $_GET['M_Head1'];
$result = mysqli_query($link, "select Distinct Name from acc_heads where M_Head='$M_Head' order by Name" );
                                                        while($row  = mysqli_fetch_object($result))
                                                                {
                                                                if($Code == $row->Name)
                                                                        {
                       echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                                        }        
                                                                        else
                                                                        {
                             echo "<option value=\"$row->Name\">$row->Name</option>";
                                                                        }
                                                                }
                                                        
                                                }
?>
                                </select> 

       
                              <tr align="left">
                                <td><b><font size=3 color=black>Amount:</b></td>
                        <td colspan="3"><input type="text" name="stramt" size="25" onkeypress="return processkey(event)" /></td>
<tr>    
<td><b><font size=3 color=black>Mode of Payment:</b></td>
<td>
<select class="form-control" 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=3 color=black>Cheque No. (If any):</b></td>
<td colspan="1"><input type="text" name="chq" class="input-box" size="25" /></td>
</tr>

<tr>       
<td><b><font size=3 color=black>Remarks (If any):</b></td>
                        <td colspan="3"><input type="text" name="strrem" size="50" onkeydown="return tab(event)" /></td>
                        
               

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

<input type="submit" name="dr" value="Debit" style="background: none; color:#007BFF; border:2px solid #007BFF; padding:2px 20px; border-radius:5px;">
<input type="submit" name="cr" value="Credit" style="background: none; color:#007BFF; border:2px solid #007BFF; padding:2px 20px; border-radius:5px;">

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


</table>
                                                                                              
<?php




if(isset($_POST['dr']))
{  
$item2 = $_POST['item2'];
$strrem = $_POST['strrem'];
$stramt = $_POST['stramt'];
$item = $_POST['item'];
$pmode = $_POST['pmode'];
$chq = $_POST['chq'];

if($stramt==NULL or $item2==NULL)
{
echo "<script language='javascript'>";
echo "alert('Error: Either Amount Field or Sub-Head Field is Empty!')";
echo "</script>";
}
else if ($stramt>0)
{
mysqli_query($link, "insert into tmp_vch values ('','$item','$item2','$strrem','$stramt','0.00','$usr','$pmode','$chq') ");
}
}
                        





if(isset($_POST['cr']))
{  
$item2 = $_POST['item2'];
$strrem = $_POST['strrem'];
$stramt = $_POST['stramt'];
$item = $_POST['item'];
$pmode = $_POST['pmode'];
$chq = $_POST['chq'];              


                                   if($stramt==NULL or $item2==NULL)
                                 {
                                 echo "<script language='javascript'>";
                                echo "alert('Error: Either Amount Field or Sub-Head Field is Empty!')";

                                echo "</script>";
                                 }

                                else if ($stramt>0)
                                {
                                 mysqli_query($link, "insert into tmp_vch values ('','$item','$item2','$strrem','0.00','$stramt','$usr','$pmode','$chq') ");

                                 }
                        }
if($_GET['action']=='delete'){
$lid=intval($_GET['tid']);
//$profilepic=$_GET['profilepic'];
//$ppicpath="teacherspic"."/".$profilepic;
$query=mysqli_query($link,"delete from tmp_vch where Id='$lid' ");

if($query){
//unlink($ppicpath);
//echo "<script>alert('Voucher Deleted Successfully.');</script>";
//echo "<script type='text/javascript'> document.location = 'voucher_search.php'; </script>";


}
}       

if(isset($_POST['save']))
{
date_default_timezone_set ("Asia/Karachi");
$the_time = date('d-m-Y (h:i:s a)', time());
$t_credit = $_POST['t_credit'];
$t_debit = $_POST['t_debit'];
$sum = $_POST['sum'];
$tst1 = $t_credit;
$tst2 = $t_debit;
if ($tst1 == 0 && $tst2 == 0 or $date == '1970-01-01')
{
echo "<script language='javascript'>";
echo "alert('Error! The Voucher is not Completed. Either amount or Date is missing.')";
echo "</script>";
}

else if($sum == 0 && $tst1>0)
{

$result = mysqli_query($link, "select Distinct Vch_Number, L_Type, V_Date from edit_vch where usr='$usr' ");
while($row = mysqli_fetch_object($result))
{
$vch=$row->Vch_Number;
$vtype=$row->L_Type;
$vdate=$row->V_Date;
}

mysqli_query($link, "delete from ledgers where vch_number='$vch' ");
$result13 = mysqli_query($link, "select * from tmp_vch where usr='$usr' order by Id");
while($row = mysqli_fetch_object($result13))
{
if($row->Debit=='0.00')
{
mysqli_query($link, "insert into ledgers (M_Head, vch_number, Code, Details, Credit, D_Date, usr, L_Type, V_Time, Pmode, Chq) values ('$row->M_Head','$vch','$row->Code','$row->Details','$row->Credit','$vdate','$usr','$vtype','$the_time','$row->Pmode','$row->Chq')");

}
                            if($row->Credit=='0.00')
                            {


mysqli_query($link, "insert into ledgers (M_Head, vch_number ,Code, Details, Debit ,D_Date, usr, L_Type, V_Time, Pmode, Chq) values ('$row->M_Head','$vch','$row->Code','$row->Details','$row->Debit','$vdate','$usr','$vtype','$the_time','$row->Pmode','$row->Chq')");

}
                     
                }


mysqli_query($link, "delete from tmp_vch where usr='$usr' ");
echo "<script language='javascript'>";
echo "alert('Successfully Updated!')";
echo "</script>";
$link = "<script>window.open('voucher_search.php','_self')</script>";
echo $link;
}



else
{
echo "<script language='javascript'>";
echo "alert('Debit And Credit Sides are Not Equal!')";
echo "</script>";
}        

        }


?>
<table align="center" id="customers">
<br>       
<tr>
    <th colspan="9"><font size="4">TRANSACTION DETAILS</th>

  </tr>


<?



                $result = mysqli_query($link, "select * from tmp_vch where usr='$usr' order by Id");
                                  
 
    echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>S.No.</b>";
        echo "        </td>";


        
echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Cost Center</b>";
        echo "        </td>";
        

           echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Head's Name</b>";
        echo "        </td>";
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Remarks</b>";
        echo "        </td>";
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Dr. Amount</b>";
        echo "        </td>";
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Cr. Amount</b>";
        echo "        </td>";
      
      echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Payment Mode</b>";
        echo "        </td>";  
     echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Cheque (If any)</b>";
        echo "        </td>";  
  echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Action</b>";

      echo"  </td>";

        echo "</tr>";
                                         $count=0;
                                         $one=1;
                                         
                                        while($row = mysqli_fetch_object($result))
                                        {
                                        $count=$one+$count;
echo "<td align=center><font size=2 color=black>$count</td>";
echo "<td align=center><font size=2 color=black>$row->M_Head</td>";
echo "<td align=center><font size=2 color=black>$row->Code</td>";
echo "<td align=center><font size=2 color=black>$row->Details</td>";
echo "<td align=center><font size=2 color=black>$row->Debit</td>";
echo "<td align=center><font size=2 color=black>$row->Credit</td>";
echo "<td align=center><font size=2 color=black>$row->Pmode</td>";
echo "<td align=center><font size=2 color=black>$row->Chq</td>";
                                                                

                                                        ?>
<td align=center>
 <a href="voucher2.php?action=delete&&tid=<?php echo $row->Id; ?>" style="color:red;" title="Delete Entry" onclick="return confirm('Do you really want to delete record?');"><i class="fa fa-trash" aria-hidden="true"></i> </a>
</td>
<?
                                                echo "</tr>";
                                
                                        }
                
                
                

$t_debit = 0;
        $t_credit = 0;
       
        $result = mysqli_query($link, "select *  from tmp_vch where usr='$usr' ");
        while($row = mysqli_fetch_object($result))
                {
                        $t_debit = $t_debit + $row->Debit;
                        $t_credit = $t_credit + $row->Credit;
                }
                $total = $t_debit - $t_credit;
?>

</table>             
<table align="center" id="customers1">
<br>     
                         
                <td align="center"><b><font size=3 color=black>Total Debits:</b></td>
                <td align="center" colspan="2"><input type="text" name="t_debit"  size="10" style="color: black; font-family: Verdana; background-color: #dddddd; font-size:25" readonly value="<?php echo $t_debit ?>" /></td>
                
                <td><b><font size=3 color=black>Total Credits:</b></td>
                <td align="center" colspan="2"><input type="text"  name="t_credit" size="10" style="color: black; font-family: Verdana; background-color: #dddddd; font-size:25" readonly value="<?php echo $t_credit ?>" /></td>

                <td><b><font size=3 color=black>Difference:</b></td>
                <td align="center" colspan="2"><input type="text" name="sum" size="10" style="color: black; font-family: Verdana; background-color: #dddddd; font-size:25" readonly value="<?php echo $total ?>" /></td>
        </tr>

</table>




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