<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
    print "<script> alert('Session timed out'); parent.document.location='../index.php'; </script>";
    return;
}
include("opendb.php");
$invno=$_REQUEST['invno']; 
$customer=$_REQUEST['sup'];
$d1=$_REQUEST['d1'];
$d2=$_REQUEST['d2'];
$d3=$_REQUEST['d3'];
$entrydt=$d3.'-'.$d2.'-'.$d1;
$RadioGroup1=$_REQUEST['RadioGroup1'];
$outstanding=$_REQUEST['outstanding'];
$brnchtp=$_REQUEST['brnchtp'];
$downamt=$_REQUEST['down_amt'];
//echo $RadioGroup1;

if($downamt>=0)
{
    $tbl="advamt".$brcode.$user;
    $querry="select sum(advamt) from $tbl";
    $queress=mysqli_query($db,$querry)or die(mysqli_error($db));
    $querrrrow=mysqli_fetch_array($queress);
    $downamt=$querrrrow[0];
    $rs=mysqli_query($db,"select max(cpno) from cshpay where brcode='$brcode'")or die(mysqli_error($db));
    $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'")or die(mysqli_error($db));
    $row=mysqli_fetch_row($res);
    if($row[0]<=0)
        $jvno=1;
    else
        $jvno=($row[0]+1);
	$rs=mysqli_query($db,"select max(bpno) from bnkpay where brcode='$brcode'")or die(mysqli_error($db));
    $cr_row=mysqli_fetch_row($rs);
    if($cr_row[0]<=0)
        $brno=1;
    else   
        $brno=$cr_row[0]+1;
    if($RadioGroup1==1)
    {
    	$cshnart=$_REQUEST['cshnart'];
    	$sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydt','$jvno','Cr','CASH IN HAND','0','$downamt','$cshnart.(CP:$crno)','$brcode','CP','','','$crno')";
	//echo $sql;
	//echo "<br>";
	    mysqli_query($db,$sql) or die(mysqli_error($db));
	    $sql1="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydt','$jvno','Dr','$customer','$downamt','0','$cshnart.(CP:$crno)','$brcode','CP','','','$crno')";
	    mysqli_query($db,$sql1) or die(mysqli_error($db));
	    //echo $sql1;
	    //echo "<br>";	
 	    $sql2="insert into cshpay (date,cpno,type,partic,dramt,cramt,narr,brcode,j_type,against,no) values('$entrydt','$crno','Dr','$customer','$downamt','0','$cshnart.(JV:$jvno)','$brcode','$jvno','','')";
	    mysqli_query($db,$sql2) or die(mysqli_error($db));
       // echo $sql2;
       // echo "<br>";
    }
    if($RadioGroup1==2)
    {
    	$bnkled=$_REQUEST['bnkled'];
    	$bnknart=$_REQUEST['bnknart'];
    	$bnkchqno=$_REQUEST['bnkchqno'];
        $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydt','$jvno','Cr','$bnkled','0','$downamt','$bnknart.(BP:$brno)','$brcode','BP','$bnkchqno','','$brno')";
        mysqli_query($db,$sql)or die(mysqli_error($db));
        //echo $sql;
        //echo "<br>";
        $sql="insert into  bnkpay(date,bpno,type,partic,dramt,cramt,narr,brcode,j_type,against,no) values('$entrydt','$brno','Cr','$bnkled','0','$downamt','$bnknart.(JV:$jvno)','$brcode','$jvno','$bnkchqno','$bnkchqno')";
        mysqli_query($db,$sql)or die(mysqli_error($db));
        //echo $sql;
        //echo "<br>";
        $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydt','$jvno','Dr','$customer','$downamt','0','$bnknart.(BP:$brno)','$brcode','BP','$bnkchqno','','$brno')";
        mysqli_query($db,$sql) or die(mysqli_error($db));
        //echo $sql;
        //echo "<br>";
        $sql="insert into bnkpay (date,bpno,type,partic,dramt,cramt,narr,brcode,j_type,against,no) values('$entrydt','$brno','Dr','$customer','$downamt','0','$bnknart.(JV:$jvno)','$brcode','$jvno','$bnkchqno','')";
        mysqli_query($db,$sql) or die(mysqli_error($db));
        //echo $sql;
        //echo "<br>";
    }
    if($RadioGroup1==3)
    {
        $jourled=$_REQUEST['jourled'];
	    $jrnart=$_REQUEST['jrnart'];
	    $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydt','$jvno','Cr','$jourled','0','$downamt','$jrnart.(CR:$jvno)','$brcode','JV','','','$jvno')";
	    //echo $sql;
	    //echo "<br>";
	    mysqli_query($db,$sql) or die(mysqli_error($db));
	    $sql1="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydt','$jvno','Dr','$customer','$downamt','0','$jrnart.(CR:$jvno)','$brcode','JV','','','$jvno')";
	    mysqli_query($db,$sql1) or die(mysqli_error($db));
	    //echo $sql1;
	    //echo "<br>";	
 	    //$sql2="insert into cshrec values('$entrydt','$crno','Cr','$customer','0','$downamt','$jrnart.(JV:$jvno)','$brcode','$jvno','','')";
	    //mysqli_query($db,$sql2) or die(mysqli_error($db));
        //echo $sql2;
        //echo "<br>";
    }
    $tmpqry=mysqli_query($db,"select * from $tbl");
    while($tmprw=mysqli_fetch_array($tmpqry))
    {
        //if($brnchtp=='bo')
        //{
	    $sqdt=mysqli_query($db,"select entrydate from sparepurchase where pbillno='$tmprw[invno]' and suplier='$customer' and brcode='$brcode'");
	    $sqldtrw=mysqli_fetch_array($sqdt);
	    $invoicedat=$sqldtrw[0];
            //}
        /*
        if($brnchtp=='ac')
        {
	        $sqdt=mysqli_query($db,"select invdate from spareinvoice_oth where invno='$tmprw[invno]' and customer='$customer' and brcode='$brcode'");
	        $sqldtrw=mysqli_fetch_array($sqdt);
	        $invoicedat=$sqldtrw[0];
        }
        */
        if($tmprw['invno']=='select')
            $minvno = "Advance";
        else
            $minvno = $tmprw['invno'];
            if($tmprw['advamt']>0)
            {
        $dwnqry="insert into downpayment_sup(invno,customer,amt,date,brcode,jvno,typ,remark,regno,trno) values('$minvno','$customer','$tmprw[advamt]','$entrydt','$brcode','$jvno','$brnchtp','$invoicedat','$tmprw[pono]','$tmprw[sono]')";
  		echo $dwnqry;
		//echo "<br>";
	    mysqli_query($db,$dwnqry) or die("Err 1".mysqli_error($db));
            }
            
    }
    mysqli_query($db,"drop table $tbl"); 
    if($RadioGroup1==1)
    {
   //     print "<script> window.open('accounts/payrec/prncshrec.php?no=$crno'); </script>";
    }
    if($RadioGroup1==2)
    {
    //    print "<script> window.open('accounts/payrec/prnbnkrec.php?no=$brno'); </script>";
    }
    print "<font color=black>Data has been posted</font><br>";
    echo "<script> document.location='acposting_sup.php';</script>";
}
else
{
    echo "<script>alert('Invalid amount');document.location='acposting_sup.php';</script>";
}
?>