<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}
include_once("test.php");
include_once('opendb.php');
$us=mysqli_query($db,"select level from bruser where username='$user' and brcode='$brcode' ")or die(mysqli_error($db));
$r=mysqli_fetch_row($us);
$user_level=$r[0];
$y=$_REQUEST['y'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$cr=$_REQUEST['cr'];

if($savecount==0)
{
$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;
             }
             
if($user_level<=4 && $user!='kishore' && $user!='harshitha' )
{
$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]>10 || $dtrow[0]<0)
{
 print "Invalid Date, Entry possible for last 10 days only";
  return;
}
}
$dt=$y.'-'.$m.'-'.$d;
$i=0;

if( count($cr)<=0)
{
 print "<script> alert('Invalid Entry - Credit');  </script>";
 return;
}

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


if(count(array_unique($cr))<count($cr))
{
    echo " Name has duplicates";
    exit;
} 
$i=0;
$totcramt=0;
$cr_amt=$_REQUEST['cr_amt'];

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

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

$i=0;
$cq_no=$_REQUEST['cq_no'];

foreach($cq_no as $a)
{
 $cq_no[$i]=$a; 
 $i++;
}
$flg=0;
$flg2=0;


for($j=0;$j<$i;$j++)
{
 $k=$j+1;
 $qry=mysqli_query($db,"select * from ledger where led='$cr[$j]' and brcode='$brcode'");
 if(mysqli_num_rows($qry)<=0)
 {
  
  print "<script>document.getElementById('myTable2').rows[$k].cells[0].style.color='red';</script>";
  $flg++;
 }
  
   if($cr[$j]=='')

    {
    print "<script>document.getElementById('myTable2').rows[$k].cells[0].style.color='red';</script>";
 $flg++;
    }
      if(($cr_amt[$j]=='')||($cr_amt[$j]=='0')||($cr_narr[$j]==''))

    {
    print "<script>document.getElementById('myTable2').rows[$k].cells[0].style.color='red';</script>";
 $flg2++;
    }
   
}

if($flg>0)
 {
  echo "<small>Check the  Cr of Red marked row(s)</br></small>";
 }

elseif($flg2>0)
 {
  echo "<small>Enter Amount/Narration of Red marked row(s)</small>";
 }
else
{


$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 cshrec values('$dt','$crno','Dr','CASH IN HAND','$totcramt','0','$cr_narr[0].(JV:$jvno)','$brcode','CR','$cragain[0]','$cragval[0]')";
mysqli_query($db,$sql) or die(mysqli_error($db));
*/

/*$sql="insert into journal2 values('$dt','$jvno','Cr','$cr[$j]','0','$cr_amt[$j]','$cr_narr[$j].(BP:)','$brcode','BP','$cragain[$j]','$cragval[$j]')";
print $sql;
//mysqli_query($db,$sql) or die(mysqli_error($db));
*/
date_default_timezone_set("Asia/Kolkata");
$logdt=date("Y-m-d");
$logtime=date("H:i:s");


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(led,grp,op,drcr,code,brcode) values('$cr[$j]','Sundry Debtors','0','Dr','$code','$brcode')";
mysqli_query($db,$sql_1);
}
 $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,prftcntr,docdt) values('$dt','$jvno','Cr','$cr[$j]','0','$cr_amt[$j]','$cr_narr[$j].(CR:$crno)','$brcode','CR','$cragain[$j]','$cq_no[$j]','$crno','$logtime $user','$logdt')";
// echo "insert into journal2 values('$dt','$jvno','Cr','$cr[$j]','0','$cr_amt[$j]','$cr_narr[$j].(CR:$crno)','$brcode','CR','$cragain[$j]','$cragval[$j]','$crno')";
//print $sql;

mysqli_query($db,$sql) or die(mysqli_error($db));


$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,prftcntr,docdt) values('$dt','$jvno','Dr','CASH IN HAND','$totcramt','0','$cr_narr[$j].(CR:$crno)','$brcode','CR','$cragain[$j]','$cq_no[$j]','$crno','$logtime $user','$logdt')";

    mysqli_query($db,$sql) or die(mysqli_error($db));

//print $sql;

 $sql="insert into cshrec(date,crno,type,partic,dramt,cramt,narr,brcode,j_type,against,no) values('$dt','$crno','Cr','$cr[$j]','0','$cr_amt[$j]','$cr_narr[$j].(JV:$jvno)','$brcode','$jvno','$cragain[$j]','$cq_no[$j]')";
mysqli_query($db,$sql) or die(mysqli_error($db));
//print $sql;

}
//print "<script> window.open('prncshrec.php?no=$crno'); </script>";
print "<font color=black>Data has been posted</font><br>";
 echo "<script> document.getElementById('savecount').value=1;document.location='cshrec.php';</script>";
$ip_address = "$jvno|CR|Add";
log_entry($brcode,$user,$ip_address,"Cash Receipt JVNo: $jvno, CR No:$crno",$db);
//include("cshrec.php");
}
}
elseif($savecount==1)
{
echo "Data already saved";
  echo "<script> document.location='cshrec.php';</script>";
}

?>
