<?
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'")or die(mysqli_error());
$r=mysqli_fetch_row($us);
$user_level=$r[0];
$y=$_REQUEST['y'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$cr=$_REQUEST['cr'];
$savecount=$_REQUEST['savecount'];
if($savecount==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());
$dtrow=mysqli_fetch_row($dtre);

if($dtrow[0]>2 || $dtrow[0]<0)
{
 print "Invalid Date, Entry possible for last 2 days only";
 
}
}
$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++;
}

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





$rsdn=mysqli_query($db,"select max(crno) from psnindia_psndbdn.cshrec where brcode='$brcode'");
$cr_rowdn=mysqli_fetch_row($rsdn);
if($cr_rowdn[0]<=0)
{
   $crnodn=1;
}
 else  
 {
   $crnodn=$cr_rowdn[0]+1;
 }
$resdn=mysqli_query($db,"select max(jvno) from psnindia_psndbdn.journal2 where brcode='$brcode'");
 $rowdn=mysqli_fetch_row($resdn);
 if($rowdn[0]<=0)
 {
    $jvnodn=1;
 }
 else
 {
    $jvnodn=($rowdn[0]+1);
 }

$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvno','Dr','Cash','$totcramt','0','$cr_narr[0].(CR:$crno)','$brcode','CR','$cragain[0]','$cragval[0]','$crno')";
mysqli_query($db,$sql) or die(mysqli_error());

$sqldn="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Dr','Cash','$totcramt','0','$cr_narr[0].(CR:$crnodn)','$brcode','CR','$cragain[0]','$cragval[0]','$crnodn')";
mysqli_query($db,$sqldn) or die(mysqli_error());


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) values('$dt','$jvno','Cr','$cr[$j]','0','$cr_amt[$j]','$cr_narr[$j].(CR:$crno)','$brcode','CR','$cragain[$j]','$cragval[$j]','$crno')";
mysqli_query($db,$sql) or die(mysqli_error());


$sqldn1="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Cr','$cr[$j]','0','$cr_amt[$j]','$cr_narr[$j].(CR:$crnodn)','$brcode','CR','$cragain[$j]','$cragval[$j]','$crnodn')";
mysqli_query($db,$sqldn1) or die(mysqli_error());

 $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]','$cragval[$j]')";
mysqli_query($db,$sql) or die(mysqli_error());

$sql="insert into psnindia_psndbdn.cshrec(date,crno,type,partic,dramt,cramt,narr,brcode,j_type,against,no) values('$dt','$crnodn','Cr','$cr[$j]','0','$cr_amt[$j]','$cr_narr[$j].(JV:$jvnodn)','$brcode','$jvnodn','$cragain[$j]','$cragval[$j]')";
mysqli_query($db,$sql) or die(mysqli_error());
}
print "<script> window.open('prncshrec.php?no=$crno'); </script>";
print "<script>alert('Data has been posted');</script>";
 echo "<script> document.getElementById('savecount').value=1;document.location='cshrec.php';</script>";

$ip_details="$jvno|CR|Add";
log_entry($brcode,$user,$ip_details,"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>";
}

?>
