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

include("../../opendb.php");
include("../../test.php");
$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error($db));
$r=mysqli_fetch_row($us);
$user_level=$r[0];

$dt=$y.'-'.$m.'-'.$d;
if($user_level<=4)
{
$sq="select to_days(curdate())-to_days('$dt')";
$dtre=mysqli_query($db,$sq)or die(mysqli_error($db));
$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($cr)<=0)
{
 print "<script> alert('Invalid Entry - Credit'); history.back(1); </script>";
 return;
}

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

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

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


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

$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);


$sql="insert into journal2 values('$dt','$jvno','Dr','Cash','$totcramt','0','$crnarr[0].(CR:$crno)','$brcode','CR','$cr_again[0]','$cr_agval[0]','$crno')";
mysqli_query($db,$sql) or die(mysqli_error($db));

/*
$sql="insert into cshrec values('$dt','$crno','Dr','Cash','$totcramt','0','$crnarr[0].(JV:$jvno)','$brcode','CR','$cr_again[0]','$cr_agval[0]')";
mysqli_query($db,$sql) or die(mysqli_error($db));
*/

/*$sql="insert into journal2 values('$dt','$jvno','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j].(BP:)','$brcode','BP','$cr_again[$j]','$cr_agval[$j]')";
print $sql;
//mysqli_query($db,$sql) or die(mysqli_error($db));
*/

for($j=0;$j<$i;$j++)
{
$chre=mysqli_query($db,"select * from ledger where led='$cr[$j]' and brcode='$brcode'");
if(mysqli_num_rows($chre)<=0)
{
$code=substr($cr[$j],0,3).date(ymds);
$code=str_replace(' ','-',$code);

$sql_1="insert into ledger values('$cr[$j]','Sundry Debtors','0','Dr','$code','$brcode')";
mysqli_query($db,$sql_1);
}
 $sql="insert into journal2 values('$dt','$jvno','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j].(CR:$crno)','$brcode','CR','$cr_again[$j]','$cr_agval[$j]','$crno')";
mysqli_query($db,$sql) or die(mysqli_error($db));

 $sql="insert into cshrec values('$dt','$crno','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j].(JV:$jvno)','$brcode','$jvno','$cr_again[$j]','$cr_agval[$j]')";
mysqli_query($db,$sql) or die(mysqli_error($db));
}
print "<script> window.open('prncshrec.php?no=$crno'); </script>";
print "<font color=green>Data has been posted</font><br>";
log_entry($brcode,$user,$REMOTE_ADDR,"Cash Receipt JVNo: $jvno, CR No:$crno");
include("cshrec.php");
?>
