<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}

// start 
?>
<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>AMPL</title>
<script>
function valid()
{
var cus=document.f1.cus.selectedIndex;
var pptype=document.f1.ac.selectedIndex;
var vtype=document.f1.ptype.selectedIndex;

submitOK="true"

if (cus<=0)
 {
 //submitOK="false"

 }
if (ptype<=0)
 {
 //submitOK="false"

 }
if (ptype<=0)
 {
 submitOK="false"
alert('Invalid Voucher Type1');
 }

if (submitOK=="false")
 {
 return false
 }
}
</script>

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="jquery/jquery-1.7.min.js"></script>
<script type="text/javascript" src="jquery/jquery-ui.min.js"></script>
<link type="text/css" href="jquery/jquery-ui.min.css" rel="stylesheet" />	
<link rel="stylesheet" href="css/form.css" type="text/css" />
<script>
  $(function() {
    $( "#leddt" ).datepicker({ dateFormat: 'yy-mm-dd' });

  });
  
  $(function() {
    $( "#supdt" ).datepicker({ dateFormat: 'yy-mm-dd' });

  });
</script>

<script type="text/javascript">
$(function(){
  $(".ac").live("keydown.autocomplete",function(){
  $led = $(this);
  $row = $led.parent().parent();
  $(this).autocomplete({
    source: function( request, response ) {
    $.ajax({
      url: "searchledger.php",
      dataType: "json",
      data: "key="+$led.val(),
      success: function( data ) {
        response( $.map( data, function( item ) {
          return {
            label: item.led,
            value: item.led,
            
            }
        }));
      }
    });
  }
  ,
  minLength: 2,
  select: function( event, ui ) {
  $row.find('.led').val(ui.item.value);

  /*log( ui.item ?
  "Selected: " + ui.item.label :
  "Nothing selected, input was " + this.value);
  */
   }

  }).data("ui-autocomplete")._renderItem = function (ul, item) {
         return $("<li></li>")
             .data("item.autocomplete", item)
             .append( item.label )
             .appendTo(ul);
     };
  });
});



</script>


<?
// enc




$fno=$_REQUEST['fno'];
$amt=$_REQUEST['amt'];
$name=$_REQUEST['name'];
$type=$_REQUEST['type'];
$dfrom=$_REQUEST['dfrom'];
$dto=$_REQUEST['dto'];
$narr=$_REQUEST['narr'];
$brc=$_REQUEST['brc'];
include("opendb.php");
include("test.php");
include("includes/top.php");
?>
<link href="css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body>


<?

if(isset($_POST['save']))
{
$y=$_REQUEST['y'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$ptype=$_REQUEST['ptype'];


if ($ptype=='')
 $ptype='blank';


$cus=$_REQUEST['cus'];
$amt=$_REQUEST['amt'];
$narr=$_REQUEST['narr'];
$fno=$_REQUEST['fno'];
$ac=$_REQUEST['ac'];
$dt=$y.'-'.$m.'-'.$d;

$acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error($db));
     $acrow=mysqli_fetch_row($acpd);

      $acf= $acrow[0];
      $act= $acrow[1];
      
      if($dt<$acf || $dt>$act)
             {
                 print "<script> alert('Invalid Date, Entry Possible for Current Financial Year only'); </script>";
                 return;
             }
             
$qry=mysqli_query($db,"select * from ledger where led='$ac' and brcode='$brcode'");
//print $q=("select * from ledger where led='$ac' and brcode='$brcode'");

 		if(mysqli_num_rows($qry)<=0)
 		{

  		  print "<script>alert('Invalid Ledger Head...!!!')</script>";
          return;
 		}
if($ptype=='blank')
{
      print "<script>alert('Invalid Voucher Type...!!!')</script>";
       // return;
}


$rr1=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
 $jv_row=mysqli_fetch_row($rr1);
  if($jv_row<=0)
    $jvno=1;
  else
    $jvno=$jv_row[0]+1;
  

 if($ptype=='CR')
  {

   $sql="select max(crno) from cshrec where brcode='$brcode'";
   $rr=mysqli_query($db,$sql);
   $cr_row=mysqli_fetch_row($rr);
    if($cr_row[0]<=0)
     $csno=1;
    else
     $csno=$cr_row[0]+1;

   $sql="insert into cshrec values('$dt','$csno','Cr','$cus','0','$amt', 'Advance $narr', '$brcode','$jvno','done','$fno','0','2017-09-23')";
   
 mysqli_query($db,$sql)or die('Csh rec err:'.mysqli_error($db));
//log_entry($brcode,$user,$REMOTE_ADDR,"CR - $jvno");
 }

 elseif($ptype=='BR')
  {
   
   $sql="select max(brno) from bnkrec where brcode='$brcode'";
   $rr=mysqli_query($db,$sql)or die('brno err:'.mysqli_error($db));
   $br_row=mysqli_fetch_row($rr);
    if($br_row[0]<=0)
     $brno2=1;
    else
     $brno2=$br_row[0]+1;

  $sql="insert into bnkrec values('$dt','$brno2','Dr','$ac','$amt','0','Advance $narr','$brcode','$jvno','done','0','2017-09-23','0','2017-09-23')";
 mysqli_query($db,$sql) or die('bnkr ins err:'.mysqli_error($db));
 //log_entry($brcode,$user,$REMOTE_ADDR,"BR - $jvno");
 }

 elseif($ptype=='CP')
  {


  
   $sql="select max(cpno) from cshpay where brcode='$brcode'";
   $rr=mysqli_query($db,$sql)or die('brno err:'.mysqli_error($db));
   $br_row=mysqli_fetch_row($rr);
    if($br_row[0]<=0)
     $brno2=1;
    else
     $brno2=$br_row[0]+1;


  $sql="insert into cshpay values('$dt','$brno2','Dr','$ac','$amt','0','$narr','$brcode','$jvno','done','$fno','0','2017-09-23')";


 mysqli_query($db,$sql) or die('cshpay ins err:'.mysqli_error($db));
 //log_entry($brcode,$user,$REMOTE_ADDR,"CP - $jvno");

 }

 elseif($ptype=='BP')
  {
   
   $sql="select max(bpno) from bnkpay where brcode='$brcode'";
   $rr=mysqli_query($db,$sql)or die('brno err:'.mysqli_error($db));
   $br_row=mysqli_fetch_row($rr);
    if($br_row[0]<=0)
     $brno2=1;
    else
     $brno2=$br_row[0]+1;

  $sql="insert into bnkpay values('$dt','$brno2','Dr','$ac','$amt','0','$narr','$brcode','$jvno','done','0','2017-09-23','0','2017-09-23')";
   mysqli_query($db,$sql) or die('bnkp ins err:'.mysqli_error($db));
   //log_entry($brcode,$user,$REMOTE_ADDR,"BP - $jvno");

 }



 //posting  

if($ptype=='CR')
 {
$cr='CR';
 $jour_dr="insert into journal2 values('$dt','$jvno','Dr','$ac','$amt','0','$narr, CRNo.:$csno', '$brcode','$cr', 'done','$fno','$csno','3770','0000-00-00')"; 
$nr="".$narr.", CRNo:$csno";
 mysqli_query($db,$jour_dr) or die('dr err:'.mysqli_error($db));
 $jour_cr="insert into journal2 values('$dt','$jvno','Cr','$cus','0','$amt','$nr','$brcode','$cr', 'done','$fno','','3770','0000-00-00')";
 mysqli_query($db,$jour_cr) or die('cr err:'. mysqli_error($db));
 
 $brledger1=getbrled($brcode,$db);

$sq="update gen4psnauto_psn_amplvehicle.journal2 set against='done',tr_no='$jvno' where brcode='$brc' and jvno='$fno' and partic='$brledger1' ";
//print $sq;
mysqli_query($db,$sq) or die('advcoll err:'.mysqli_error($db));

log_entry($brcode,$user,"$jvno|CR|Add","CR - $jvno",$db);


}
elseif($ptype=='BR')
 {
$cr='BR';
$jour_dr="insert into journal2 values('$dt','$jvno','Dr','$ac','$amt','0','$narr,BR No.:$brno2', '$brcode','$cr', 'done','$fno','$brno2','3770','0000-00-00')"; 
$nr="".$narr.", BRNo:$brno2";
 mysqli_query($db,$jour_dr) or die('dr err:'.mysqli_error($db));
 $jour_cr="insert into journal2 values('$dt','$jvno','Cr','$cus','0','$amt','$nr','$brcode','$cr', 'done','$fno','','3770','0000-00-00')";
 mysqli_query($db,$jour_cr) or die('cr err:'. mysqli_error($db));
 $brledger1=getbrled($brcode,$db);

$sq="update gen4psnauto_psn_amplvehicle.journal2 set against='done',tr_no='$jvno' where brcode='$brc' and jvno='$fno' and partic='$brledger1' ";
//print $sq;
mysqli_query($db,$sq) or die('advcoll err:'.mysqli_error($db));

log_entry($brcode,$user,"$jvno|BR|Add","BR - $jvno",$db);


}

elseif($ptype=='CP')
 {
$cr='CP';
$jour_dr="insert into journal2 values('$dt','$jvno','Dr','$ac','$amt','0','$narr,CP No.:$brno2', '$brcode','$cr', 'done','$fno','$brno2','3770','0000-00-00')"; 
$nr="".$narr.", CPNo:$brno2";
 mysqli_query($db,$jour_dr) or die('dr err:'.mysqli_error($db));
 $jour_cr="insert into journal2 values('$dt','$jvno','Cr','$cus','0','$amt','$nr','$brcode','$cr', 'done','$fno','','3770','0000-00-00')";
 mysqli_query($db,$jour_cr) or die('cr err:'. mysqli_error($db));
$brledger1=getbrled($brcode,$db);

$sq="update gen4psnauto_psn_amplvehicle.journal2 set against='done',tr_no='$jvno' where brcode='$brc' and jvno='$fno' and partic='$brledger1' ";
//print $sq;
mysqli_query($db,$sq) or die('advcoll err:'.mysqli_error($db));

log_entry($brcode,$user,"$jvno|CP|Add","CP - $jvno",$db);

}

elseif($ptype=='BP')
 {
$cr='BP';
$jour_dr="insert into journal2 values('$dt','$jvno','Dr','$ac','$amt','0','$narr,BR No.:$brno2', '$brcode','$cr', 'done','$fno','$brno2','3770','0000-00-00')"; 
$nr="".$narr.", BPNo:$brno2";
 mysqli_query($db,$jour_dr) or die('dr err:'.mysqli_error($db));
 $jour_cr="insert into journal2 values('$dt','$jvno','Cr','$cus','0','$amt','$nr','$brcode','$cr', 'done','$fno','','3770','0000-00-00')";
 mysqli_query($db,$jour_cr) or die('cr err:'. mysqli_error($db));
 $brledger1=getbrled($brcode,$db);

$sq="update gen4psnauto_psn_amplvehicle.journal2 set against='done',tr_no='$jvno' where brcode='$brc' and jvno='$fno' and partic='$brledger1' ";
//print $sq;
mysqli_query($db,$sq) or die('advcoll err:'.mysqli_error($db));

log_entry($brcode,$user,"$jvno|BP|Add","BP - $jvno",$db);


}


elseif($ptype=='JV')
 {
$cr='JV';
$jour_dr="insert into journal2 values('$dt','$jvno','Dr','$ac','$amt','0','$narr', '$brcode','$cr', 'done','$fno','$brno2','3770','0000-00-00')"; 
$nr="".$narr."";
 mysqli_query($db,$jour_dr) or die('dr err:'.mysqli_error($db));
 $jour_cr="insert into journal2 values('$dt','$jvno','Cr','$cus','0','$amt','$nr','$brcode','$cr', 'done','$fno','','3770','0000-00-00')";
 mysqli_query($db,$jour_cr) or die('cr err:'. mysqli_error($db));

$brledger1=getbrled($brcode,$db);

$sq="update gen4psnauto_psn_amplvehicle.journal2 set against='done',tr_no='$jvno' where brcode='$brc' and jvno='$fno' and partic='$brledger1' ";
//print $sq;
mysqli_query($db,$sq) or die('advcoll err:'.mysqli_error($db));


log_entry($brcode,$user,"$jvno|JV|Add","JV - $jvno",$db);

}
/*
$query="select brcode from branch where brname='$ac'";
//echo $query;
//echo "<br>";
$qrrs=mysqli_query($db,$query);
$brc=mysqli_fetch_array($qrrs);
$brc=$brc[0];

*/
 
$dt1 = explode("-", $dfrom);
$dt2 = explode("-", $dto);
?>
<script> 
document.location='clearing2.php?d1=<?print $dt1[2]?>&m1=<?print $dt1[1]?>&y1=<?print $dt1[0]?>&d2=<?print $dt2[2]?>&m2=<?print $dt2[1]?>&y2=<?print $dt2[0]?>&show=show'; 

</script>
<?
} // end save
print "<p style={background-color:c1d799}>&nbsp; Customer :".$name."</p>";
print "<p style={background-color:c1d799}>&nbsp; Address :".$addr."</p>";
?>

<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
th{background-color:c1d799}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
</style>

<body>
<center>
<form name='f1' action='<?print $PHP_SELF?>' method='POST' onSubmit="return valid()">
<input type=hidden name='code' value='<?print $code ?>'>
<input type=hidden name='amt1' value='<?print $amt ?>'>
<input type=hidden name='ptype' value='<?print $ptype ?>'>
<input type=hidden name='brc' value='<?print $brc ?>'>
<input type=hidden name='fno' value='<?print $fno ?>'>
<input type=hidden name='dfrom' value='<?print $dfrom ?>'>
<input type=hidden name='dto' value='<?print $dto ?>'>


<table border=1>
<th colspan=4 align=center>RECEIPT ENTRY</TH>
<?

   print "<tr>";
   print "<td>Date </td><td>dd <input type=text name='d' size='2' value='".date("d",time())."'>";
   print "mm <input type=text name='m' size='2' value='".date("m",time())."'>";
   print "yy <input type=text name='y' size='4' value='".date("Y",time())."'>";
   print "</td></tr>";
   print "<tr><td>Ledger(Cr) </td><td>";
 $name=trim($name);
  
$brled=getbrlednamee($name,$db);




//$len=strlen($name);
//print $len;
if($type=='Dr')
{
    $re=mysqli_query($db,"select led from ledger where led='$name' order by led asc limit 1")or die(mysqli_error($db));

  }

else
{
   $re=mysqli_query($db,"select led from ledger where brcode='$brcode' and grp!='branches' order by led asc")or die(mysqli_error($db));

 }

 
  print "<select name='cus'>";
   while($row=mysqli_fetch_row($re))
   {
     print "\n<option>$row[0]</option>";
   }
   print "</select></td>";
   print "<td>Amount </td><td><input type=text name='amt' size='12' readonly value='$amt' style={text-align:right}></td></tr>";
  print "\n<tr><td>VoucherType</td><td><select name='ptype'><option>$ptype</option><option>JV</option><option>CR</option><option>CP</option><option>BR</option><option>BP</option></td>";
  
  //print "<td><input type=text size=30 name='exphd' id=exphd class=exphd></td>";Customer
 // print "</tr><tr><td>Ledger (Dr)</td><td><input type=text size=80 name='ac' id=ac class=ac></td>";
  print "</tr><tr><td>Ledger (Dr)</td><td><select name='ac' id=ac class=ac>";

echo $narr;

if($type=='Cr')
{
   $re=mysqli_query($db,"select led from ledger where led='$name' order by led asc limit 1")or die(mysqli_error($db));
   
 
}
else
{
   $re=mysqli_query($db,"select led from ledger where brcode='$brcode' and grp!='branches' order by led asc")or die(mysqli_error($db));
}

   while($row=mysqli_fetch_row($re))
   {
     print "\n<option>$row[0]</option>";
   }
   print "</select></td>";
 
   
?>
<tr><td>Narration<td colspan=3><input type=text name='narr' size=70 value='<? echo $narr?>'></td></tr>

 <tr bgcolor=c1d799><td colspan=4 align=center>
     <input type=submit value=' SAVE AND PRINT RECEIPT ' name='save'> 
     <input type=button value=' CANCEL ' onClick=document.location='../clearing1.php'> 
</td></tr>

<script>
  function xx1(str,event,this1)
    {
        var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
        inputField = document.getElementById(str);
        initInputFieldValue = inputField.value;
        dataList = document.getElementById('htmlListTags');
	if(keyCode!='40')
            {
                if(keyCode!='38')
                    {
                        yy1(str,event,this1);
                    }
            }
 
    }
    </script>
</form>

