<?
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;
}
include("../opendb.php");
include("../test.php");
$jvno=$_REQUEST['jvno'];
$br=$_REQUEST['br'];
$div=$_REQUEST['div'];

        
$brres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
$mjvno = $brrow[0]+1;
$bnkres=mysqli_query($db,"select max(brno) from bnkrec where brcode='$brcode'");
$bnkrow=mysqli_fetch_array($bnkres);
$mbrno = $bnkrow[0]+1;
if($div=='PSNEICHER')
{
    $sqlbr="SELECT brname FROM psnindia_gen4psnauto_psn_psneicherdb.branch where brcode = '$br' ";
    $resultbr = mysqli_query($db,$sqlbr);
    $rowbr=mysqli_fetch_array($resultbr);
    $sql="SELECT * FROM psnindia_gen4psnauto_psn_psneicherdb.journal2 where jvno = $jvno and brcode = '$br' and partic = 'PSN COCHIN HO' ";
    $result = mysqli_query($db,$sql);
    while($row=mysqli_fetch_array($result))
    {
        if($row['against']=='SBI')
            $mpartic='SBI A/C NO -30728155988';
        else if($row['against']=='FBL')
            $mpartic='FEDERAL BANK O/D-13805600014689';
        else if($row['against']=='SBI A/C NO -30728155988')
            $mpartic='SBI A/C NO -30728155988';
        else if($row['against']=='SBI CA A/C NO.30392808664')
            $mpartic='SBI CA A/C NO.30392808664';
        else if($row['against']=='FEDERAL BANK O/D-13805600014689')
            $mpartic='FEDERAL BANK O/D-13805600014689';
            //$proddesc=preg_replace('/[^A-Za-z0-9\-]/', '', $proddesc);
        $mnarr = $row['narr'];
        $magainst = preg_replace('/[^\da-z ]/i', '',$row['against']);
        $mno = $row['no'];
        $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,tr_no,no) values('".$row['date']."','".$mjvno."','".$row['type']."','".$mpartic."','".$row['dramt']."','".$row['cramt']."','".$mnarr."','".$brcode."','BR','".$magainst."','".$mbrno."','".$mno."')";
    	//print "<br>".$sql;
    	mysqli_query($db,$sql) or die(mysqli_error());
    	$sql1="insert into bnkrec  (date,brno,type,partic,dramt,cramt,narr,brcode,j_type,against,no) values('".$row['date']."','".$mbrno."','".$row['type']."','".$mpartic."','".$row['dramt']."','".$row['cramt']."','".$mnarr."','".$brcode."','".$mjvno."','".$magainst."','".$mno."')";
    	//print "<br>".$sql1;
    	mysqli_query($db,$sql1) or die(mysqli_error());
    	$sql2="update psnindia_gen4psnauto_psn_psneicherdb.journal2 set prftcntr='$mjvno' where brcode='$br' and jvno='$jvno'";
    	//print $sql2;
    	mysqli_query($db,$sql2) or die(mysqli_error());
    	
    	
        $mpartic=$rowbr[0];
        if($mpartic=='PSN ALAPPUZHA') $mpartic = 'PSN ALAPPUZHA' ;
        if($mpartic=='PSN CHERANELLOOR') $mpartic = 'PSN CHERANELLOOR' ;
        if($mpartic=='PSN KALADY') $mpartic = 'PSN KALADY' ;
        if($mpartic=='PSN PALAKKAD') $mpartic = 'PSN PALAKKAD' ;
        if($mpartic=='PSN THRISSUR') $mpartic = 'PSN THRISSUR' ;
        if($mpartic=='PSN AUTOMOBILES PVT LTD, CALICUT') $mpartic = 'PSN BOSCH CALICUT' ;
        if($mpartic=='PSN AUTOMOBILES PVT LTD THRISSUR') $mpartic = 'PSN BOSCH E FOCUS' ;
//        if($mpartic=='PSN AUTOMOBILES PVT LTD MANACAUD') $mpartic = 'PSN CUMMINS MANACAUD' ;
        if($mpartic=='PSN AUTOMOBILES PVT LTD, AC DELCO EKM') $mpartic = 'PSN AC DELCO EKM' ;
        if($mpartic=='PSN AUTOMOBILES PVT LTD(DANILO) CLT') $mpartic = 'PSN DANILO CALICUT' ;
        if($mpartic=='PSN AUTOMOBILES PVT LTD(DANILO) UNICHIRA') $mpartic = 'PSN DANILO UNICHIRA' ;
        if($mpartic=='PSN AUTOMOBILES PVT LTD(FLEETGUARD)') $mpartic = 'PSN FLEET GUARD UNICHIRA' ;
        if($mpartic=='PSN AUTOMOBILES PVT LTD CALICUT') $mpartic = 'PSN ZF CALICUT' ;
        if($mpartic=='PSN AUTOMOBILES PVT LTD COCHIN') $mpartic = 'PSN ZF COCHIN' ;
        if($mpartic=='PSN AUTOMOBILES PVT LTD MANACAUD') $mpartic = 'PSN ZF TRIVANDRUM' ;
        if($mpartic=='PSN AUTOMOBILES PVT LTD, BANGALORE') $mpartic = 'PSN BOSCH BANGALORE' ;
        if($mpartic=='PSN AUTOMOBILES PVT LTD(FLEETGUARD) CLT') $mpartic = 'PSN FLEET GUARD CALICUT' ;
        if($mpartic=='PSN AUTOMOBILES PVT LTD, BANGALORE') $mpartic = 'PSN ZF BANGALORE' ;
        if($mpartic=='PSN AUTOMOBILES SHARE DIVISION') $mpartic = 'PSN-SHARE DIVISION' ;
        
        $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,tr_no,no) values('".$row['date']."','".$mjvno."','Cr','".$mpartic."','0','".$row['dramt']."','".$mnarr."','".$brcode."','BR','".$magainst."','".$mbrno."','".$mno."')";
    	//print "<br>".$sql;
    	mysqli_query($db,$sql) or die(mysqli_error());
    	$sql1="insert into bnkrec  (date,brno,type,partic,dramt,cramt,narr,brcode,j_type,against,no) values('".$row['date']."','".$mbrno."','".$row['type']."','".$mpartic."','".$row['dramt']."','".$row['cramt']."','".$mnarr."','".$brcode."','".$mjvno."','".$magainst."','".$mno."')";
    	//print "<br>".$sql1;
    	mysqli_query($db,$sql1) or die(mysqli_error());
        
        
    }
    $ip_details="$jvno|BR|Add";
    log_entry($brcode,$user,$ip_details,"Bank Receipt JVNo: $mjvno, from Other $jvno",$db);
    //echo $sql2;
    //mysqli_query($db,$sql2) or die(mysqli_error());
    $mdate1=implode('-', array_reverse(explode('-', $mdate)));
    print "<script> alert(' Bank Receipt Updated Successfully .....');</script>";
    print "<font color=green>Data has been posted</font><br><script>document.location='../br_othdiv.php'</script>";
}
else if($div=='ZF')
{
    $sqlbr="SELECT brname FROM psnindia_gen4psnauto_psn_zfdb.branch where brcode = '$br' ";
    $resultbr = mysqli_query($db,$sqlbr);
    $rowbr=mysqli_fetch_array($resultbr);
    $sql="SELECT * FROM psnindia_gen4psnauto_psn_zfdb.journal2 where jvno = $jvno and brcode = '$br' ";
    $result = mysqli_query($db,$sql);
    while($row=mysqli_fetch_array($result))
    {
        if($row['type']=='Dr')
        {
            if($row['against']=='SBI')
                $mpartic='SBI A/C NO -30728155988';
            else if($row['against']=='FBL')
                $mpartic='FEDERAL BANK O/D-13805600014689';
            else if($row['against']=='SBI A/C NO -30728155988')
                $mpartic='SBI A/C NO -30728155988';
            else if($row['against']=='SBI CA A/C NO.30392808664')
                $mpartic='SBI CA A/C NO.30392808664';
            else if($row['against']=='FEDERAL BANK O/D-13805600014689')
                $mpartic='FEDERAL BANK O/D-13805600014689';
        }
        else
        {
            $mpartic=$rowbr[0];
            if($mpartic=='PSN AUTOMOBILES PVT LTD, CALICUT') $mpartic = 'PSN BOSCH CALICUT' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD THRISSUR') $mpartic = 'PSN BOSCH E FOCUS' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD MANACAUD') $mpartic = 'PSN CUMMINS MANACAUD' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD, AC DELCO EKM') $mpartic = 'PSN AC DELCO EKM' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD(DANILO) CLT') $mpartic = 'PSN DANILO CALICUT' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD(DANILO) UNICHIRA') $mpartic = 'PSN DANILO UNICHIRA' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD(FLEETGUARD)') $mpartic = 'PSN FLEET GUARD UNICHIRA' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD CALICUT') $mpartic = 'PSN ZF CALICUT' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD COCHIN') $mpartic = 'PSN ZF COCHIN' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD MANACAUD') $mpartic = 'PSN ZF TRIVANDRUM' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD, BANGALORE') $mpartic = 'PSN BOSCH BANGALORE' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD(FLEETGUARD) CLT') $mpartic = 'PSN FLEET GUARD CALICUT' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD, BANGALORE') $mpartic = 'PSN ZF BANGALORE' ;
            if($mpartic=='PSN AUTOMOBILES SHARE DIVISION') $mpartic = 'PSN-SHARE DIVISION' ;
        }
        $mnarr = $row['narr'];
        $magainst = $row['against'];
        $mno = $row['no'];
         

        $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,tr_no,no) values('".$row['date']."','".$mjvno."','".$row['type']."','".$mpartic."','".$row['dramt']."','".$row['cramt']."','".$mnarr."','".$brcode."','BR','".$magainst."','".$mbrno."','".$mno."')";
    	//print "<br>".$sql;
    	mysqli_query($db,$sql) or die(mysqli_error());
    	$sql1="insert into bnkrec  (date,brno,type,partic,dramt,cramt,narr,brcode,j_type,against,no) values('".$row['date']."','".$mbrno."','".$row['type']."','".$mpartic."','".$row['dramt']."','".$row['cramt']."','".$mnarr."','".$brcode."','".$mjvno."','".$magainst."','".$mno."')";
    	//print "<br>".$sql1;
    	mysqli_query($db,$sql1) or die(mysqli_error());
    	$sql2="update psnindia_gen4psnauto_psn_zfdb.journal2 set against='".$row['against']."|$mjvno' where brcode='$br' and jvno='$jvno'";
    	//print $sql2;
    	mysqli_query($db,$sql2) or die(mysqli_error());
    }
    $ip_details="$jvno|BR|Add";
    log_entry($brcode,$user,$ip_details,"Bank Receipt JVNo: $mjvno, from Other $jvno",$db);
    //echo $sql2;
    //mysqli_query($db,$sql2) or die(mysqli_error());
    $mdate1=implode('-', array_reverse(explode('-', $mdate)));
    print "<script> alert(' Bank Receipt Updated Successfully .....');</script>";
    print "<font color=green>Data has been posted</font><br><script>document.location='../br_othdiv.php'</script>";
}
else if($div=='BOSCH')
{
    $sqlbr="SELECT brname FROM psnindia_gen4psnauto_psn_accessoriesdb2526.branch where brcode = '$br' ";
    $resultbr = mysqli_query($db,$sqlbr);
    $rowbr=mysqli_fetch_array($resultbr);
    $sql="SELECT * FROM psnindia_gen4psnauto_psn_accessoriesdb2526.journal2 where jvno = $jvno and brcode = '$br' ";
    $result = mysqli_query($db,$sql);
    while($row=mysqli_fetch_array($result))
    {
        if($row['type']=='Dr')
        {
            if($row['against']=='SBI')
                $mpartic='SBI A/C NO -30728155988';
            else if($row['against']=='FBL')
                $mpartic='FEDERAL BANK O/D-13805600014689';
            else if($row['against']=='SBI A/C NO -30728155988')
                $mpartic='SBI A/C NO -30728155988';
            else if($row['against']=='SBI CA A/C NO.30392808664')
                $mpartic='SBI CA A/C NO.30392808664';
            else if($row['against']=='FEDERAL BANK O/D-13805600014689')
                $mpartic='FEDERAL BANK O/D-13805600014689';
        }
        else
        {
            $mpartic=$rowbr[0];
            if($mpartic=='PSN AUTOMOBILES PVT LTD, CALICUT') $mpartic = 'PSN BOSCH CALICUT' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD THRISSUR') $mpartic = 'PSN BOSCH E FOCUS' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD MANACAUD') $mpartic = 'PSN CUMMINS MANACAUD' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD, AC DELCO EKM') $mpartic = 'PSN AC DELCO EKM' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD(DANILO) CLT') $mpartic = 'PSN DANILO CALICUT' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD(DANILO) UNICHIRA') $mpartic = 'PSN DANILO UNICHIRA' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD(FLEETGUARD)') $mpartic = 'PSN FLEET GUARD UNICHIRA' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD CALICUT') $mpartic = 'PSN ZF CALICUT' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD COCHIN') $mpartic = 'PSN ZF COCHIN' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD MANACAUD') $mpartic = 'PSN ZF TRIVANDRUM' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD, BANGALORE') $mpartic = 'PSN BOSCH BANGALORE' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD(FLEETGUARD) CLT') $mpartic = 'PSN FLEET GUARD CALICUT' ;
            if($mpartic=='PSN AUTOMOBILES PVT LTD, BANGALORE') $mpartic = 'PSN ZF BANGALORE' ;
            if($mpartic=='PSN AUTOMOBILES SHARE DIVISION') $mpartic = 'PSN-SHARE DIVISION' ;
        }
        $mnarr = $row['narr'];
        $magainst = $row['against'];
        $mno = $row['no'];
        $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,tr_no) values('".$row['date']."','".$mjvno."','".$row['type']."','".$mpartic."','".$row['dramt']."','".$row['cramt']."','".$mnarr."','".$brcode."','BR','".$magainst."','".$mbrno."')";
    	//print "<br>".$sql;
    	mysqli_query($db,$sql) or die(mysqli_error());
    	$sql1="insert into bnkrec  (date,brno,type,partic,dramt,cramt,narr,brcode,j_type,against,no) values('".$row['date']."','".$mbrno."','".$row['type']."','".$mpartic."','".$row['dramt']."','".$row['cramt']."','".$mnarr."','".$brcode."','".$mjvno."','".$magainst."','".$mno."')";
    	//print "<br>".$sql1;
    	mysqli_query($db,$sql1) or die(mysqli_error());
    	$sql2="update psnindia_gen4psnauto_psn_accessoriesdb2526.journal2 set against='".$row['against']."|$mjvno' where brcode='$br' and jvno='$jvno'";
    	//print $sql2;
    	mysqli_query($db,$sql2) or die(mysqli_error());
    }
    $ip_details="$jvno|BR|Add";
    log_entry($brcode,$user,$ip_details,"Bank Receipt JVNo: $mjvno, from Other $jvno",$db);
    //echo $sql2;
    //mysqli_query($db,$sql2) or die(mysqli_error());
    $mdate1=implode('-', array_reverse(explode('-', $mdate)));
    print "<script> alert(' Bank Receipt Updated Successfully .....');</script>";
    print "<font color=green>Data has been posted</font><br><script>document.location='../br_othdiv.php'</script>";
}
?>
