<?
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;
}
$d1=$_REQUEST['d1'];
$m1=$_REQUEST['m1'];
$y1=$_REQUEST['y1'];
$d2=$_REQUEST['d2'];
$m2=$_REQUEST['m2'];
$y2=$_REQUEST['y2'];
$show=$_REQUEST['show'];
include("opendb.php");
include("test.php");
include("includes/top.php");
?>
<link href="css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >

<?
if($show=='show')
{
$_POST['show']=$show;
}
if(isset($_POST['show']))
{
$d1=$_REQUEST['d1'];
$m1=$_REQUEST['m1'];
$y1=$_REQUEST['y1'];
$d2=$_REQUEST['d2'];
$m2=$_REQUEST['m2'];
$y2=$_REQUEST['y2'];

$dfrom=$y1.'-'.$m1.'-'.$d1;
$dto=$y2.'-'.$m2.'-'.$d2;
$led = $_REQUEST['led'];

$rewst=mysqli_query($db,"select brstate from branch where brcode='$brcode'");

$brst1=mysqli_fetch_row($rewst);
 $brst2=$brst1[0];
 
?>

<center>

<form action='<?print $PHP_SELF?>' method='POST'>
<table border=1 >
<tr><td align=center>Form No. <input type=text name='seafno' size=5> customer Name <input type=text name='seaname'></td><td>
<td>
<input type=submit value='Search' name='sea'>
</td></tr>
</table>
</form>
<font color=green size=4><b><u>Entries passed by branches to your a/c </b></u>
<?

$rewbr=mysqli_query($db,"select brcode,brname,brled from branch where brstate='$brst2' and brcode not in('3784','3792')");

while($rowbr=mysqli_fetch_array($rewbr))
{
//$led=getbrled($brcode);

$sql="select * from journal2  where partic='$rowbr[2]'  and date>='$dfrom' and date<='$dto' and brcode!='$rowbr[0]]' and j_type!='CH' and j_type!='GRN' and against!='done' and j_type!='Spare trans out' and j_type!='Spare trans out agt' and j_type!='Spare trans in' order by brcode";
//echo $sql;
//echo "<br>";

 $result=mysqli_query($db,$sql)or die(mysqli_error($db));
 if(mysqli_num_rows($result)>0)
 {
     

print "<table align='center' border=1> <tr><td>SL. </td><td>Clear</td><td>Date </td><td>Jvno</td> <td> Dr/Cr</td> <td>Branch</td><td>Amount Dr</td><td>Amount Cr</td><td>Trans.Type</td><td>Narration</td><td>Pending In Branch</td></tr>";
$i=1;
while($row=mysqli_fetch_array($result))
{
$led=getbrname($row[7],$db);
$amt=$row[5]+$row[4];
 print "\n<form action='clr_adv1_new.php' method='POST'>
 <input type=hidden name='fno' value='$row[jvno]'>";
 print "<input type=hidden name='amt' value='$amt'>";
 print "<input type=hidden name='name' value='$led'>";
 print "<input type=hidden name='type' value='$row[type]'>";
 print "<input type=hidden name='dfrom' value='$dfrom'>";
 print "<input type=hidden name='dto' value='$dto'>";
 print "<input type=hidden name='narr' value='$row[6]'>";

 
 print "<input type=hidden name='brc' value='$row[brcode]'>";

if($row['j_type']=='JV')
$ptype='JV';
else if($row['j_type']=='CP')
$ptype='CR';
else if($row['j_type']=='CR')
$ptype='CP';
else if($row['j_type']=='BP')
$ptype='BR';
else if($row['j_type']=='BR')
$ptype='BP';
print "<input type=hidden name='ptype' value='$ptype'>";


 print "\n<tr><td>".$i++."</td><td align=center></td><td>".datetohtml($row[0])."</td><td>$row[1]</td>";
 print "<td>$row[2]</td><td>".getbrname($row[7],$db)."</td><td>$row[4]</td><td>$row[5]</td><td>$row[8]</td><td>$row[6]</td><td>$rowbr[1]</td>";
 print "\n</tr></form>";
}
print"</table><br><br>";
}
}
$led=getrpled($brcode,$db);
//$sql1="select * from psn_psneicherdb.journal2  where partic='$led'  and date>='$dfrom' and date<='$dto' and brcode!='$brcode' and j_type!='CH' and j_type!='GRN' and against!='done' and j_type !='Spare trans out' and j_type !='Spare trans in' order by brcode,date";
//echo $sql1;
//echo "<br>";
 //$result1=mysqli_query($db,$sql1)or die(mysqli_error($db));
print "</table>";
print "<table align='center' border=1>";
print "<tr><td colspan=10><b>Between PSN AUTOMOBILE Branches</td></tr><tr><td>SL.</td><td>Clear</td><td>Date</td><td>Jvno</td><td>Dr/Cr</td><td>Branch</td><td>Amount Dr</td><td>Amount Cr</td><td>Trans.Type</td><td>Narration</td></tr>";
$i=1;
while($row1=mysqli_fetch_array($result1))
{
    $led=getrpname($row1[7],$db);
    $amt=$row1[5]+$row1[4];
    $pbillno = $row1[10];
   $jtype= trim($row1[8]);
   
    $rew1=mysqli_query($db,"select sum(tot) from sparepurchase where brcode='$brcode' and pbillno = '$pbillno'");
    

    
    $rerow1=mysqli_fetch_row($rew1);
    
    if($rerow1[0]>0)
    {
       // print $rerow1[0];
        
        if(abs($rerow1[0]-$row1[4])>1)
        {
            print "\n<form action='clr_adv1_new.php' method='POST'>
            <input type=hidden name='fno' value='$row1[jvno]'>";
            print "<input type=hidden name='amt' value='$amt'>";
            print "<input type=hidden name='name' value='$led'>";
            print "<input type=hidden name='type' value='$row[type]'>";
            print "<input type=hidden name='dfrom' value='$dfrom'>";
            print "<input type=hidden name='dto' value='$dto'>";
            print "<input type=hidden name='narr' value='$row[6]'>";
            print "<input type=hidden name='brc' value='$row[brcode]'>";
            
            if($row1['j_type']=='JV')
                $ptype='JV';
            else if($row1['j_type']=='CP')
                $ptype='CR';
            else if($row1['j_type']=='CR')
                $ptype='CP';
            else if($row1['j_type']=='BP')
                $ptype='BR';
            else if($row1['j_type']=='BR')
                $ptype='BP';
                
                if($jtype=='Spare Invoice' || $jtype=='Spare Pur' )
                {
            print "<input type=hidden name='ptype' value='$ptype'>";
            print "\n<tr><td  bgcolor='#FF0000'>".$i++."</td><td></td><td bgcolor='#FF0000'>".datetohtml($row1[0])."</td><td bgcolor='#FF0000'>$row1[1]</td>";
                    
                }
            else
            {
              print "<input type=hidden name='ptype' value='$ptype'>";
            print "\n<tr><td  bgcolor='#FF0000'>".$i++."</td><td  bgcolor='#FF0000' align=center><input type=submit value='Clear'></td><td bgcolor='#FF0000'>".datetohtml($row1[0])."</td><td bgcolor='#FF0000'>$row1[1]</td>";
                     
            }
            print "<td bgcolor='#FF0000'>$row1[2]</td><td bgcolor='#FF0000'>".getrpname($row1[7],$db)."</td><td bgcolor='#FF0000'>$row1[4]</td><td bgcolor='#FF0000'>$row1[5]</td><td bgcolor='#FF0000'>$row1[8]</td><td bgcolor='#FF0000'>$row1[6]</td>";
            print "\n</tr></form>";
        }
    }
    else
    {
        print "\n<form action='clr_adv1_new.php' method='POST'>
        <input type=hidden name='fno' value='$row1[jvno]'>";
        print "<input type=hidden name='amt' value='$amt'>";
        print "<input type=hidden name='name' value='$led'>";
        print "<input type=hidden name='type' value='$row1[type]'>";
        print "<input type=hidden name='dfrom' value='$dfrom'>";
        print "<input type=hidden name='dto' value='$dto'>";
        print "<input type=hidden name='narr' value='$row1[6]'>";
        print "<input type=hidden name='brc' value='$row1[brcode]'>";
        if($row[j_type]=='JV')
            $ptype='JV';
        else if($row[j_type]=='CP')
            $ptype='CR';
        else if($row[j_type]=='CR')
            $ptype='CP';
        else if($row[j_type]=='BP')
            $ptype='BR';
        else if($row[j_type]=='BR')
            $ptype='BP';
        print "<input type=hidden name='ptype' value='$ptype'>";
        
    
 
        if($jtype=='Spare Invoice' || $jtype=='Spare Pur' )
        {
           print "\n<tr><td>".$i++."</td><td></td><td>".datetohtml($row1[0])."</td><td>$row1[1]</td>"; 
               //print $row[8];
        }
else
{
        print "\n<tr><td>".$i++."</td><td></td><td>".datetohtml($row1[0])."</td><td>$row1[1]</td>"; 
     //   print "\n<tr><td>".$i++."</td><td align=center><input type=submit value='Clear'></td><td>".datetohtml($row1[0])."</td><td>$row1[1]</td>";
        
        
}       
        print "<td>$row1[2]</td><td>".getrpname($row1[7])."</td><td>$row1[4]</td><td>$row1[5]</td><td>$jtype</td><td>$row1[6]</td>";
        print "\n</tr></form>";
    }
}
return;
}

?>
</table>
<center>
<form action='<?Print $PHP_SELF?>' method='POST' >
<table  cellspacing=1 cellpadding=1 bgcolor='steelblue'>
<tr><td bgcolor='white' align=center>
<table class=noprint>
    <!--
<tr><td>Date from </td><td>dd <input type=text name='d1' size=2 value='<?print date("d")?>'>
mm <input type=text name='m1' size=2 value='<?print date("m")?>'>
yy <input type=text name='y1' size=4 value='<?print date("Y")?>'></td></tr>
-->

<tr><td>Date from </td><td>dd <input type=text  name='d1' size=2 value='01'>
mm <input type=text  name='m1' size=2 value='04'>
yy <input type=text   name='y1' size=4 value='2021'></td></tr>

<tr><td align=right>to </td><td>dd <input type=text name='d2' size=2 value='<?print date("d")?>'>
mm <input type=text name='m2' size=2 value='<?print date("m")?>'>
yy <input type=text name='y2' size=4 value='<?print date("Y")?>'>
</td></tr>

<tr><td colspan=2 align=center bgcolor='lightsteelblue'><input type=submit value='Show' name='show'></td></tr>
</table>
</td></tr>
</table>


</form>
 
