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

?>
<!--
<a href='../ledge.php' target='m'>Create Ledger</a>

<input type=button value='SHOW TB' onClick=document.location='trialb.php' style={font-weight:bold}> 

<p>&nbsp;</p>
-->
<style>
body,td{font-family:verdana;font-size:12;color:black;}
th{background-color:c1d799}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
</style>
<html>
<head>
<script type="text/javascript">
var tq=0,tq2=0;
var cur=1;
var vv;
var drs=[];
var krs=[];

var cnamear=[];
var dcommar=[];
var modar=[];


function setval(a)
{
 vv=a.name;
}
function insRow()
{
var x=document.getElementById('myTable').insertRow(cur)
if(cur>1)
 var nar=document.getElementById('dd1').value;
var a=x.insertCell(0)
var y=x.insertCell(1)
var z=x.insertCell(2)	
var z1=x.insertCell(3)	
var ag=x.insertCell(4)
var agval=x.insertCell(5)
a.innerHTML=cur
var value="<select name=dr[] onClick=setval(this)>";

for(k=0;k<drs.length;k++)
 value=value+"<option>"+drs[k]+"</option>";

value=value+"</select>";
y.innerHTML=value


z.innerHTML="<input type=text name=dr_amt[] id=dr"+cur+" size=10>"
z1.innerHTML="<input type=text id='dd"+cur+"' name=dr_narr[] size=30 value="+nar+">"

ag.innerHTML="<select name='dragain[]'><option>OFNO</option><option>DA</option><option> ON Acc </option> <option>CHEQ</option></select>";
agval.innerHTML="<input type=text name='dragval[]'>";
//alert(cur);
cur++; 

}
function delRow(x1)
{
 var num=x1.name;
 if(cur>1)
  {
    var x=document.getElementById('myTable').deleteRow(cur-1)
    cur--;
  }
}

//************************************
function calcdr()
{
var tq=0;
for(i=1;i<cur;i++)
{
 var ele='dr'+i; //alert(ele);
 if(document.getElementById(ele).value>0)
  {
   amount=document.getElementById(ele).value;
   tq+=parseFloat(amount);
  }
 }
document.f1.tdamt.value=tq.toFixed(2);
}

function calccr()
{
var tq2=0;
for(i=1;i<cur2;i++)
{
 var ele2='cr'+i; //alert(ele);
 if(document.getElementById(ele2).value>0)
  {
   amount=document.getElementById(ele2).value;
   tq2+=parseFloat(amount);
  }
 }
document.f1.tcamt.value=tq2.toFixed(2);
}

//-------*888888888888888888888888*********************8888
var cur2=1;
function insRow2()
{
var x=document.getElementById('myTable2').insertRow(cur2)
if(cur2>1)
 var nar=document.getElementById('d2d1').value;

var a=x.insertCell(0)
var y=x.insertCell(1)
var z=x.insertCell(2)	
var z1=x.insertCell(3)	

var crag=x.insertCell(4)
var cragval1=x.insertCell(5)

a.innerHTML=cur2
var value="<select name=cr[] onClick=setval(this)>";
for(kk=0;kk<drs.length;kk++)
 value=value+"<option>"+drs[kk]+"</option>";
value=value+"</select>";
y.innerHTML=value


z.innerHTML="<input type=text name=cr_amt[] id=cr"+cur2+" size=10>"
z1.innerHTML="<input type=text id='d2d"+cur2+"' name=cr_narr[] size=30 value='"+nar+"'>"

crag.innerHTML="<select name='cragain[]'><option>OFNO</option><option>DA</option><option> ON Acc </option> <option>CHEQ</option></select>";
cragval1.innerHTML="<input type=text name='cragval[]'>";

cur2++;
}
function delRow2(x1)
{
 var num=x1.name;
 if(cur2>1)
 {
   var x=document.getElementById('myTable2').deleteRow(cur2-1)
   cur2--;
 }
}

function valid()
{
submitOK="true";
 if(document.f1.panno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid PanNo.');
return false;

 }

 if(document.f1.cname.value.length<=0)
 {
   submitOK="false";
   alert('Invalid customer.');
return false;
 }

if(document.f1.amtpaid.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Amount.');
return false;
 }

if(document.f1.tdsamt.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Tdsamt.');
return false;
 }

 if(document.f1.section.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Section.');
return false;
 }

 if(document.f1.branch1.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Branch.');
return false;
 }

 calccr();
 calcdr();

 if(document.f1.tdamt.value!=document.f1.tcamt.value)
{
  {alert('Dr, Cr Amount mis-match'); submitOK="false";}
}

 if(document.f1.tdamt.value<=0)
{
  {alert('Invalid amount'); submitOK="false";}
}


if(submitOK=="true")
   return true;
  else
   return false;

  }

function getcname()
{ 

 document.f1.panno.value=dcommar[document.f1.cname.selectedIndex];
 document.f1.dcode.value=modar[document.f1.cname.selectedIndex];

}


function calc()
{
var taxper=parseFloat(document.f1.rate.value);
 tax=parseFloat(document.f1.amtpaid.value)*parseFloat(document.f1.rate.value)/100;
 document.f1.tdsamt.value=(tax).toFixed(2);
 }
</script>

</head>

<body>

<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()">
Debit <input type="button" onclick="insRow()" value=" + ">
<input type='button' onclick='delRow(this)' value=' - '>
<input type='button' onclick='calcdr()' value=' CALCULATE DEBIT SUM  '>
<p align=center style={background-color:silver}>

Date:dd <input type=text name='d' size='2' value='<?print date("d",time());?>'>
mm <input type=text name='m' size='2' value='<?print date("m",time());?>'>
yy <input type=text name='y' size='4' value='<?print date("Y",time());?>'>
</p>
<table id="myTable" border="1">
<th>No.<th>Dr <th>Amt <th>Narr<th>Against <th>Value
<tr>
<!--
<td><input type=text name=dr[] onClick=setval()></td>
<td><input type=text name=dr_amt[]></td>
-->
</tr>
</table><br>
Total Debit Amount <input type=text readonly name='tdamt'>
<hr>

Credit <input type="button" onclick="insRow2()" value=" + ">
<input type='button' onclick='delRow2(this)' value=' - '><!--
<input type='button' onclick='calccr()' value=' CALCULATE CREDIT SUM  '>

<br>&nbsp;

<table id="myTable2" border="1">
<th>No.<th>Cr <th>Amt <th>Narr<th>Against <th>Value
</table>

<br>
Total Credit Amount <input type=text readonly name='tcamt'>



<?
include("../opendb.php");
include("../test.php");

$sprint=$_REQUEST['sprint'];
if($sprint)
{ 
$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error());
$r=mysqli_fetch_row($us);
$user_level=$r[0];
$y=$_REQUEST['y'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$dt=$y.'-'.$m.'-'.$d;
if($user_level<=3)
{
$sq="select to_days(curdate())-to_days('$dt')";
$dtre=mysqli_query($db,$sq)or die(mysqli_error());
$dtrow=mysqli_fetch_row($dtre);

if($dtrow[0]>2 || $dtrow[0]<0)
{
 print "<script> alert('Invalid Date, Entry possible for last 2 days only'); history.back(1); </script>";
 return;
}
}






//$dt=$y.'-'.$m.'-'.$d;
$i=0;
/*if(count($dr)<=0)
{
 print "<script> alert('Invalid Entry - Debit'); history.back(1); </script>";
 return;
}
if( count($cr)<=0)
{
 print "<script> alert('Invalid Entry - Credit'); history.back(1); </script>";
 return;
}

foreach($dr as $a)
{
 $drs[$i]=$a; 
 $i++;
}


for($x=0;$x<$i;$x++)
{
  for($y=0;$y<$x;$y++)
     { 
         if($drs[$x]==$drs[$y])
           print "<script>alert('Duplicate entry - $drs[$x]'); history.back(1) </script>";
     }
}


$i=0;
foreach($cr as $a)
{
 $crs[$i]=$a; 
 $i++;
}


for($x=0;$x<$i;$x++)
{
  for($y=0;$y<$x;$y++)
     { 
         if($crs[$x]==$crs[$y])
           print "<script>alert('Duplicate entry - $crs[$x]'); history.back(1) </script>";
     }
}


for($x=0;$x<$i;$x++)
{
  for($y=0;$y<$i;$y++)
     { 
        if($drs[$x]==$crs[$y])
          print "<script> alert('Invalid Entry - $drs[$x]'); history.back(1) </script>";
     }
}

foreach($dr_amt as $a)
{ 
 if($a<=0)
 {
           print "<script> alert('Invalid Amount - $a'); history.back(1) </script>";
 }
}

foreach($cr_amt as $a)
{ 
 if($a<=0 )
 {
           print "<script> alert('Invalid Amount - $a'); history.back(1) </script>";
 }
}
*/
//***********************
$i=0;
$dr=$_REQUEST['dr'];
foreach($dr as $a)
{
 $dr[$i]=$a; 
 $i++;
}

$i=0;
$dr_amt=$_REQUEST['dr_amt'];
foreach($dr_amt as $a)
{
 $dramt[$i]=$a; 
 $i++;
}
$i=0;
$dr_narr=$_REQUEST['dr_narr'];
foreach($dr_narr as $a)
{
 $drnarr[$i]=addslashes($a); 
 $i++;
}
$i=0;
$ag=$_REQUEST['ag'];
foreach($ag as $a)
{
 $again[$i]=$a;$i++;
}
$i=0;
$dragain=$_REQUEST['dragain'];
foreach($dragain as $a)
{
 $dr_again[$i]=$a;$i++;
}
$i=0;
$dragval=$_REQUEST['dragval'];
foreach($dragval as $a)
{
 $dr_agval[$i]=$a;$i++;
}

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

for($j=0;$j<$i;$j++)
{
 $sql="insert into journal2 values('$dt','$jvno','Dr','$dr[$j]','$dramt[$j]','0','".addslashes(htmlspecialchars($drnarr[$j]))."','$brcode','TDSJV','$dr_again[$j]','$dr_agval[$j]','')";
 mysqli_query($db,$sql)or die(mysqli_error());
}

//***********************
$i=0;
$cr=$_REQUEST['cr'];
foreach($cr as $a)
{
 $cr[$i]=$a; 
 $i++;
}

$i=0;
$cr_amt=$_REQUEST['cr_amt'];
foreach($cr_amt as $a)
{
 $cramt[$i]=$a; 
 $i++;
}
$i=0;
$cr_narr=$_REQUEST['cr_narr'];
foreach($cr_narr as $a)
{
 $crnarr[$i]=addslashes($a); 
 $i++;
}

$i=0;
$cragain=$_REQUEST['cragain'];
foreach($cragain as $a)
{
 $cr_again[$i]=$a;$i++;
}
$i=0;
$cragval=$_REQUEST['cragval'];
foreach($cragval as $a)
{
 $cr_agval[$i]=$a;$i++;
}

for($j=0;$j<$i;$j++)
{
 $sql="insert into journal2 values('$dt','$jvno','Cr','$cr[$j]','0','$cramt[$j]','".addslashes(htmlspecialchars($crnarr[$j]))."','$brcode','TDSJV','$cr_again[$j]','$cr_agval[$j]','')";
mysqli_query($db,$sql) or die(mysqli_error());
}

 $sql="insert into tdstrans (tdsno,cname,panno,dcode,amtpaid,taxrate,tdsamt,section,tdscoldt,jvno,brcode,branch) values('$tdsno','$cname','$panno','$dcode','$amtpaid','$rate','$tdsamt','$section','$dt','$jvno','$brcode','$branch1')";
mysqli_query($db,$sql) or die(mysqli_error());


log_entry($brcode,$user,$REMOTE_ADDR,"Journal Entry - $jvno");
print "<font color=green>Data has been posted</font><br>";

}

$re=mysqli_query($db,"select * from ledger where brcode='$brcode' and grp!='Cash in hand' and grp!='Bank Accounts' order by led");
$i=0;
while($row=mysqli_fetch_row($re))
{
 print "\n<script>drs[$i]='$row[0]' </script>";
 $i++;
}


$rs=mysqli_query($db,"select max(tdsno) from tdstrans where brcode='$brcode' ")or die(mysqli_error());
 $p_row=mysqli_fetch_row($rs);
 if(mysqli_num_rows($rs)<=0)
  $pno=1;
 else
    $pno=$p_row[0]+1;


?>
<table cellspacing=1 cellpadding=5 bgcolor=lightsteelblue>
<th colspan=8 style={background-color:lightsteelblue}><h3>TDS DETAILS</h3></th>
<tr><td>TDS No </td><td><input type=text readonly name='tdsno' value='<?print $pno?>' size=8 style={background-color:silver}></td><td>Branch</td><td><select name='branch1'>
<option></option>
<?
$empre1=mysqli_query($db,"select brname from br_master where brcode='$brcode'");

 while($emprow1=mysqli_fetch_row($empre1))
 {
  print "<option>$emprow1[0]</option>";
 }
?>
</select>
</td>
</tr>
<tr><td>Customer </td><td><select name='cname' onChange=getcname()> <option></option>
<?
$c=1;
$msres=mysqli_query($db,"select custname,acname,corporate from pan_master order by custname")or die('errr'.mysqli_error());
while($msrow=mysqli_fetch_array($msres))
{
 print "\n<option>$msrow[0]</option>";
 print "<script>\n cnamear[$c]='$msrow[0]'; dcommar[$c]='$msrow[1]';";
 print "modar[$c]='$msrow[2]'; </script>";
 $c++;
}
?>
</select>
</td>

<td>PAN.NO</td><td><input type=text name='panno' readonly size=10></td>
<td>Deductee Code</td><td><input type=text name='dcode' readonly size=12></td></tr>
<tr><td>Principal Amount</td><td><input type=text name='amtpaid' size=12 onChange=calc()></td>
<td>TaxRate </td><td><input type=text name='rate' size=12 value='2' onChange=calc()></td>
<td>TDS Amount </td><td><input type=text name='tdsamt' size=12></td>
</tr><tr><td>Section</td><td><select name='section'>
<option></option>
<?
$empre=mysqli_query($db,"select corpname from section where branch='$brcode' order by corpname");

 while($emprow=mysqli_fetch_row($empre))
 {
  print "<option>$emprow[0]</option>";
 }
?>
</select>
</td>
</tr>




</table>
<Hr><input type=submit value='    SAVE JOURNAL    ' name='sprint'>
</form>

</body>
</html>
