<?
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");
include("../test.php");
?>
<html>
    <head>
<link rel="stylesheet" href="../css/form.css" type="text/css" />
<link type="text/css" href="../jquery/jquery-ui.min.css" rel="stylesheet" />	
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../cal/jquery-ui1.js"></script>
<script type="text/javascript" src="../jquery/jquery-1.7.min.js"></script>
<script type="text/javascript" src="../jquery/jquery-ui.min.js"></script>
<script type="text/javascript" src="../jquery/jquery.jqEasyCharCounter.min.js"></script>
<script type="text/javascript">

$(function() {

    $("#fr_date").datepicker({
	dateFormat: "dd-mm-yy"
   });
 $("#to_date").datepicker({
	dateFormat: "dd-mm-yy"
   });
 });
  </script>

</head>
<body>
<form method='post'><center><table border=1 class=report style="width: 40px;">
<tr><td align=center colspan=2><h3 > Budget And Expense Report</h3></td></tr>
<tr><td>FROM</td><td>To</td></tr>
<tr><td align='center'><input type='text'  readonly name='frmdt' value='01-04-2019' id='fr_date'></td><td align=center><input type=text readonly name=todt id='to_date' value=<? echo date('d-m-Y')?>></td></tr>
<tr><td colspan=2 align=center ><input type=submit name=btn value=Show></td></tr>

</table>
</form>
<?
$btn=$_REQUEST['btn'];
$frmdt=$_REQUEST['frmdt'];
$todt=$_REQUEST['todt'];
//$frmdt = implode('-', array_reverse(explode('-', $frmdt)));
   
$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');
//print $cdate;
//print $diff;
//print $year1;



if($btn)
{
?>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
th{background-color:c1d799}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
</style>

<center>
    
<table bgcolor=silver cellspacing=1 cellpadding=1 border=1  width= 60%>
<tr><hr><td align=center colspan=8 width= 670px><h3 > Budget And Expense Report</h3>

<?
print " from ".$frmdt.' to '.$todt;

?>
</td></hr>

</tr>
<tr><hr><td>No:</td><td>Exp Head</td><td >Budget</td><td>Exp Amt</td><td>Diff.</td><td>YTD</td></hr></tr>
<?
$i=1;
$query=mysqli_query($db,"select head,target from brtagetexp where brcode='$brcode' order by head ");
//echo "select distinct invno,invdate,invtype,disc,clamt from warspareinvoice where brcode='$brcode'";
$budtot=0;
$exptot=0;
$difftot=0;
//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='$brcode' 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='$brcode' 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='$brcode' and partic='$row1[0]'  and date<='$cdate'");
    $row12c=mysqli_fetch_array($queryc);
    
    $budtot+=$row1[1]*$diff;
    $budmon=$row1[1]*$diff;
    
    if($budmon>0 || $row12[0]<>0 )
    {
    print "<tr><td >$i</td><td  >$row1[0]</td><td>$budmon</td>";
    if($row12[0]!=0)
    {
         print"<td>$row12[0]</td>";
$exptot+=$row12[0];
     $buddiff= $budmon- $row12[0];
     $difftot+=$buddiff;
    }
    else
    {
         print "<td>0</td>";
         $buddiff= $budmon;
          $difftot+=$buddiff;
       }
     print "<td>$buddiff</td>";
     if($row12[0]!=0)
     {
     print"<td>$row12c[0]</td>";
     }
     else
     {
         print"<td>0</td>";
     }
     
    print"</tr>";
    	$i++;
    }
}
print"<tr bgcolor='lightsteelblue'><td></td><b><td align=right><b> Total</td><td><b>$budtot</td><td><b>$exptot</td><td><b>$difftot</td></tr>";
//print "</table>";
}
//print "</table>";
?>
</table>
</body>
</html>