<?
session_start();
include("../opendb.php");
include("../test.php");
include("includes/top.php");
$brcode = $_SESSION['brcode'];
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
	th{background-color:lightsteelblue;color:black}
	td{font-size:12}
.right{text-align:right};
</style>
<style>
a:link, a:visited {
  background-color: white;
  color: black;
  padding: 15px 25px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
}

a:hover, a:active {
  background-color: #F3FDB7;
}
</style>
<center>
<h3>LOG REPORT</H3>
<?
if(isset($_POST['show']))
{
$y2=$_REQUEST['y2'];    
$m2=$_REQUEST['m2'];
$d2=$_REQUEST['d2'];
$y1=$_REQUEST['y1'];    
$m1=$_REQUEST['m1'];
$d1=$_REQUEST['d1'];
$dt = $y2."-".$m2."-".$d2." 23:59:00";
$df = $y1."-".$m1."-".$d1;
$re=mysqli_query($db,"select * from log where brcode='$brcode' and date >= '$df' and date <= '$dt' group by date");
print "<h2>LOG Report from $d1-$m1-$y1 to $d2-$m2-$y2</h2>";
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
print "<h3>PSN CONSTRUCTION EQUIPMENT PVT LTD. ".$brrow[1]."</H3>"; 
print "<table border = '1'>";
print "<th>Sl No<th>Account Code<th>JV No</th><th>Date</th><th>Original Date</th><th>Original Entry By</th><th>Modified On<th>Modified By<th>Permanent By<th>Journal Details";
$i=1;
while($row=mysqli_fetch_array($re))
{ 
    $jvno_ad = 0;
    $jvno=strpos(strtoupper($row['action']),"JVNO:");
    if($jvno>0)$jvno_ad = 6;
    if($jvno<=0)
    {
        $jvno=strpos($row['action'],"ournal Entry -");
        if($jvno>0)$jvno_ad = 15;
    }
    if($jvno<=0)
    {
        $jvno=strpos($row['action'],"Journal:");
        if($jvno>0)$jvno_ad = 8;
    }
    if($jvno<=0)
    {
        $jvno=strpos($row['action'],"Journal -");
        if($jvno>0)$jvno_ad = 10;
    }
    
    $jvnoval1= substr($row['action'],$jvno+$jvno_ad,8);
    
    $jvnoval = "";
    $flg=1;
    
    $narration=strpos($row['action'],"dit JV Journal");
    if($narration<=0)
        $narration=strpos($row['action'],"dit BP Journal");
    if($narration<=0)
        $narration=strpos($row['action'],"dit BR Journal");
    if($narration<=0)
        $narration=strpos($row['action'],"dit CR Journal");
    if($narration<=0)
        $narration=strpos($row['action'],"dit CP Journal");        
    if($narration<=0)
        $narration=strpos($row['action'],"el JV Journal");
    if($narration<=0)
        $narration=strpos($row['action'],"el BP Journal");
    if($narration<=0)
        $narration=strpos($row['action'],"el BR Journal");
    if($narration<=0)
        $narration=strpos($row['action'],"el CR Journal");
    if($narration<=0)
        $narration=strpos($row['action'],"el CP Journal");
    
    //echo $narration."teee".$row['action'];
    
    
    
    
   
    
    for($a=0;$a<=strlen($jvnoval1);$a++)
    {
        $char = substr($jvnoval1,$a,1);
        if(strpos("A9876543210",$char)>0 && $flg==1)
        {
            $jvnoval=$jvnoval.$char;  
        }
        else
        {
            $flg=0;
        }
    }
    $entryuser='';
    $entryuserdt='';
    if($narration>='1')
    {
        //SELECT * FROM `log` WHERE `brcode` LIKE 'CHE527' AND `action` LIKE '%8117%' and not( action like "%Edit%" or action like "%Del%");
        //$addaction=
        //echo "select user,date from log where brcode='$brcode' and action LIKE '%$jvnoval%' and not( action like '%Edit%' or action like '%Del%') group by date";
        //echo "<br>";
        $jvupre=mysqli_query($db,"select user,date from log where brcode='$brcode' and action LIKE '%$jvnoval%' and not( action like '%Edit%' or action like '%Del%') group by date");
        $jvuprerow=mysqli_fetch_array($jvupre);
        $entryuser=$jvuprerow[0];
        $entryuserdt=$jvuprerow[1];
    }
    
    $morg_date = implode('-', array_reverse(explode('-', substr($row['date'],0,10))));
    $morg_user = $row['user'];
    if($entryuser>'')
    {
        $morg_user=$entryuser;
        $morg_date=$entryuserdt;
    }
    
    print "\n<tr><td align='right'>".$i++."</td><td></td><td>$jvnoval</td><td>".implode('-', array_reverse(explode('-', substr($row['date'],0,10))))." ".substr($row['date'],-8)."</td>";
    print "<td>".$morg_date."</td><td>$morg_user</td>";
    $modi_date="";
    if($jvnoval>0)
    {
        $modires1=mysqli_query($db,"select entrytime,user from journal2_log where brcode='$brcode' and jvno =$jvnoval ");
        $modirow1=mysqli_fetch_array($modires1);
        if($entryuser>'')
        {
            $modi_date = implode('-', array_reverse(explode('-', substr($row['date'],0,10))))." ".substr($row['date'],-8);
            $modi_user = $row['user'];
        }
        else
        {
            $modi_date = implode('-', array_reverse(explode('-', substr($modirow1['entrytime'],0,10))))." ".substr($modirow1['entrytime'],-8);
            $modi_user = $modirow1['user'];
        }
    }
    if(strlen($modi_user)<=0)
    {
        $per_user = $morg_user;
        print "<td>$modi_date</td><td>".strtoupper($modi_user)."</td><td>".$per_user."</td>";
    }
    else
    {
        $per_user = $modi_user;
        print "<td><a href='../accounts/multientry/viewtransmodi.php?code=$jvnoval' target='_blank'>$modi_date</a></td><td><a href='../accounts/multientry/viewtransmodi.php?code=$jvnoval' target='_blank'>".strtoupper($modi_user)."</a></td><td>".$per_user."</td>";   
    }
    print "<td><a href='../accounts/multientry/viewtranslog.php?code=$jvnoval' target='_blank'>".$row['action']."</a></td>";
}

?>

</table>
<?}?>
<form action='<?Print $PHP_SELF?>' method='post'>
<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 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>



