<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];


if(strlen($user)<=0)
{
   print "<script> alert('Invalid User'); document.location='inde.php' </script>";
   return;
}
include("opendb.php");
include("test.php");
?>

<? include('includes/top.php'); ?>


<?
if(isset($_POST[sprint]))

{
date_default_timezone_set("Asia/Kolkata");
$y1=$_REQUEST['y1'];
$m1=$_REQUEST['m1'];
$d1=$_REQUEST['d1'];

$y2=$_REQUEST['y2'];
$m2=$_REQUEST['m2'];
$d2=$_REQUEST['d2'];

$frmdt=$y1."-".$m1."-".$d1;
$todt=$y2."-".$m2."-".$d2;
 
$jtype = $_REQUEST['jtype'];
  
$ts1 = strtotime($frmdt);
$ts2 = strtotime($todt);

$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);

$month1 = date('m', $ts1);
$month2 = date('m', $ts2);

$diff = (($year2 - $year1) * 12) + ($month2 - $month1)+1;
$cdate=date('Y/m/d');

$q1=mysqli_query($db,"select brname from branch where brcode='$jtype'");
$ro1=mysqli_fetch_array($q1);

$q=mysqli_query($db,"select acfrom from acperiod");
$ro=mysqli_fetch_array($q);
 $d1 = $ro[0];
 $d2 = $todt;
$xy= (int)abs((strtotime($d2) - strtotime($d1))/(60*60*24*30.75));

 $xy=$xy+1;


   
   $i=1;
$query=mysqli_query($db,"select head,target from brtagetexp where brcode='$jtype' and target>0 order by head ");



$budtot=0;
$exptot=0;
$difftot=0;

$budtot_ytd=0;
$exptot_ytd=0;
$difftot_ytd=0;




?>
<style>
td{font-family:verdana;font-size:14px}
a{text-decoration:none;color:black}
a:hover{text-decoration:underline}
</style>


<table  border=1 cellspacing=1 cellpadding=1 bgcolor=white align=center>
<tr><td colspan=8 align=center><b><?print $ro1[0]?></td></tr>
<tr><td colspan=8 align=center><font size=4>Budget Comparison <?print implode('-', array_reverse(explode('-', $frmdt)))?> To <?print implode('-', array_reverse(explode('-', $todt)))?></font></td></tr>
<tr><td colspan=8 align=center>[Printed by <?print $user?> on <?print date("d-m-Y  h:i:s a")?>]</td></tr>


<?



   print" <tr><td align=center><b>No:</td><td align=center><b>Exp Head</b></td><td align=center ><b>Budget</b></td><td align=center><b>Exp Amt</b></td><td align=center><b>Diff.</b></td><td align=center><b>YTD BUDGET</b></td><td align=center><b>YTD TOTAL</b></td><td align=center><b>YTD Diff.</b></td></tr>";

//print "<table>";
// $frmdt = implode('-', array_reverse(explode('-',  $frmdt)));
 //  $todt = implode('-', array_reverse(explode('-', $todt)));
   
while($row1=mysqli_fetch_array($query))
{
   
     $sqq="select sum(dramt)-sum(cramt) from journal2 where  brcode='$jtype' and partic='$row1[0]' and date>='$frmdt' and date<='$todt'";
   // print $sqq."<br>";
    $query1=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where  brcode='$jtype' and partic='$row1[0]' and date>='$frmdt' and date<='$todt'");
    $row12=mysqli_fetch_array($query1);
    
    $queryc=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where  brcode='$jtype' and partic='$row1[0]'  and date<='$todt'");
    $row12c=mysqli_fetch_array($queryc);
    
    
    
    $budtot+=$row1[1]*$diff;
    $budmon=$row1[1]*$diff;
     $budmon_ytd=$row1[1]*$xy;
     
     $budtot_ytd+=$budmon_ytd;

     
    $buddiff_ytd= $budmon_ytd- $row12c[0];
     $difftot_ytd+=$buddiff_ytd;
     
    if($budmon>0 || $row12[0]<>0 )
    {
    print "<tr><td >$i</td><td><font size=4>$row1[0]</font></td><td align=right><font size=4>$budmon </font></td>";
    if($row12[0]!=0)
    {
         print"<td align=right><font size=4>$row12[0]</font></td>";
$exptot+=$row12[0];
     $buddiff= $budmon- $row12[0];
    
     
    
    
     $difftot+=$buddiff;
    }
    else
    {
         print "<td align=right><font size=4>0</font></td>";
         $buddiff= $budmon;
          $difftot+=$buddiff;
       }
       
       if($buddiff<0)
       {
     print "<td align=right><font color='red' size=4><b>$buddiff</b></font></td>";
       }
       else
       {
        print "<td align=right><font size=4>$buddiff</font></td>";   
       }
       //YTD BUDGET
      print "<td align=right><font size=4>$budmon_ytd</font></td>";
     
     // YTD TOTAL
     if($row12c[0]!=0)
     {
          $exptot_ytd+=$row12c[0];
     print"<td align=right><font size=4>$row12c[0]</font></td>";
     }
     else
     {
         print"<td align=right><font size=4>0</font></td>";
     }
     // YTD DIFF
     
     
       if($buddiff_ytd<0)
       {
     print "<td align=right><font color='red'><font size=4><b>$buddiff_ytd</font></b></font></td>";
       }
       else
       {
        print "<td align=right><font size=4>$buddiff_ytd</font></td>";   
       }
      
      
      
    print"</tr>";
    $buddiff_ytd=0;
    	$i++;
    }
}
print"<tr bgcolor='lightsteelblue' ><td></td><b><td align=right><b><font size=4> Total</td><td align=right><b><font size=4>$budtot</td><td align=right><b><font size=4>$exptot</td><td align=right><font size=4><b>$difftot</td><td align=right><font size=4><b>$budtot_ytd</td><td align=right><font size=4><b>$exptot_ytd</td><td align=right><font size=4><b>$difftot_ytd</td></tr>";
//print "</table>";
}
//print "</table>";
   
   
   
   

print "</table><br>";
 


?>
<body bgcolor='lavender'>
<form name='f1' id='f1' action='<?print $PHP_SELF?>' method='POST' onSubmit="return valid()">
<br>
<CENTER>
<table border=1 cellspacing=1 cellpadding=1 class=report>
<tr bgColor='c1d7bb'><td colspan=7 align=center bgcolor='8A8D89'>
</TD></TR>
<tr><td bgcolor="8A8D89" align='left'><font color='ivory'><b></b></td><td bgcolor="8A8D89" align='left'><font color='ivory'><b></b></td></tr>
<?
if($user=='joy1' || $user=='sunith' || $user=='anoop' 
||$user=='jaimon'|| $user=='vijith')
{
$qry=mysqli_query($db,"select brname,brcode from branch  order by brname ");
}
else if ($user=='kamath' || $user=='joy1')
 {   
$qry=mysqli_query($db,"select brname,brcode from branch where brcode in (3799,3785,3786,3796) order by brname "); 
}

else if($user=='deepuks' ||$user=='anson' || $user=='adarsh')
    {
$qry=mysqli_query($db,"select brname,brcode from branch where brcode in (3794,3790,3789,3795,3788,3783,3798) order by brname "); 
}

else if($user=='deepuks' )
    {
$qry=mysqli_query($db,"select brname,brcode from branch where brcode in (3794,3790,3789,3795,3788,3783,3798,3799,3785,3786,3796) order by brname "); 
}

else if($user=='pradeepr' || $user=='ram' )
    {
$qry=mysqli_query($db,"select brname,brcode from branch where brcode in (3793,3774,3771,3772,3773,3775,3777,3781) order by brname "); 
}
else
{
$qry=mysqli_query($db,"select brname,brcode from branch where brcode='$brcode' order by brname ");         
}
        

    
    

?>
<tr>
<td>Ledger Group</td>
<td><select name='jtype'>
<option value='select'>-select-</option>
<?
while($row=mysqli_fetch_array($qry))
{
?>
       <option value='<? echo $row[1] ?>'><? echo $row[0] ?></option>
<?
}
?>
</select>
</td></tr>
<tr><td> From </td>
    <td>dd<input type=text name=d1 size=1 value='<?print date("d",time())?>'>
  mm<input type=text name=m1 size=1 value='<?print date("m",time())?>'>
  yyyy<input type=text name=y1 size=3 value='<?print date("Y",time())?>'>
    </td>
</tr>

<tr><td> To </td>
    <td>dd<input type=text name=d2 size=1 value='<?print date("d",time())?>'>
  mm<input type=text name=m2 size=1 value='<?print date("m",time())?>'>
  yyyy<input type=text name=y2 size=3 value='<?print date("Y",time())?>'>
    </td>
</tr>

<tr><td colspan=6 align=center bgcolor='8A8D89'>
<input type=submit value='SHOW' name='sprint'>
<input type=reset value='CANCEL'></td></tr>
</td></tr>
<tr><td colspan=6 align=center bgcolor='8A8D89'>
<input type=submit value='SHOW' name='sprint'>
<input type=reset value='CANCEL'></td></tr>
</td></tr>
</table>
</center>