<?
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
$fromdt=$_REQUEST['fromdt'];
$todt=$_REQUEST['todt'];
$type=$_REQUEST['type'];
$redtype=$_REQUEST['redtype'];
?>

<link href="css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >

<title>Rebate Report</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="scripts/jquery-latest.js"></script>
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form.css" rel="stylesheet" />
<link rel="stylesheet" href="cal/jquery-ui.css" />
<script src="cal/jquery-1.9.1.js"></script>
<script src="cal/jquery-ui1.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script type="text/javascript">
$(function() {
$( "#ref_date" ).datepicker("destroy");
     var fyr=document.getElementById('f_yr').value;
     var splv=fyr.explode("-");
     var syr=splv[0];
     var eyr='20'.concat(splv[1]);
     var syr1='01-04-';
     var eyr1='31-03-';
     var stfnyr=syr1.concat(syr);
     var efnyr=eyr1.concat(eyr);
     $("#ref_date").datepicker({ dateFormat: 'dd-mm-yy', minDate: stfnyr, maxDate: efnyr});

$( "#ref_date1" ).datepicker("destroy");
     var fyr=document.getElementById('f_yr').value;
     var splv=fyr.explode("-");
     var syr=splv[0];
     var eyr='20'.concat(splv[1]);
     var syr1='01-04-';
     var eyr1='31-03-';
     var stfnyr=syr1.concat(syr);
     var efnyr=eyr1.concat(eyr);
     $("#ref_date1").datepicker({ dateFormat: 'dd-mm-yy', minDate: stfnyr, maxDate: efnyr});
});
</script>
<script>
function valid()
{
}
</script>
<style>
#txt{
font-family: Arial, Helvetica;
background-color: #F2F2F2;
}
</style>
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<script type="text/javascript" src="jsDatePick.min.1.3.js"></script>
<script type="text/javascript">
window.onload = function(){
new JsDatePick({
  useMode:2,
  target:"inputField",
  dateFormat:"%Y-%m-%d"
});
new JsDatePick({
  useMode:2,
  target:"inputField1",
  dateFormat:"%Y-%m-%d"
});

};
</script>

<SCRIPT LANGUAGE="JavaScript" SRC="util/PopupWindow.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="util/AnchorPosition.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="util/CalendarPopup.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="util/date.js"></SCRIPT>
<script>

var cal1 = new CalendarPopup();
cal1.showYearNavigation();

var cal2 = new CalendarPopup();
cal2.showYearNavigation();

</script>
<script>
function printstfn()
 {
   var dfrom=document.f1.fromdt.value;
   var dto=document.f1.todt.value;
   var type=document.f1.type.value;
  var redtype=document.f1.redtype.value;

   var url="rebate_printinv_prn.php?dfrom="+dfrom+"&&dto="+dto+"&&type="+type+"&&redtype="+redtype;

   document.location="rebate_printinv_prn.php?dfrom="+dfrom+"&&dto="+dto+"&&type="+type+"&&redtype="+redtype,true;

 }
 function printcerfn()
 {
  var dfrom=document.f1.fromdt.value;
   var dto=document.f1.todt.value;
   var type=document.f1.type.value;
  var redtype=document.f1.redtype.value;

   //var url="reb_printcertprn.php?dfrom="+dfrom+"&&dto="+dto+"&&type="+type+"&&redtype="+redtype;
   //alert(url);
 document.location="reb_printcertprn.php?dfrom="+dfrom+"&&dto="+dto+"&&type="+type+"&&redtype="+redtype,true;

 }
function printstpdffn()
 {
   	var dfrom=document.f1.fromdt.value;
   	var dto=document.f1.todt.value;
   	var type=document.f1.type.value;
  	var redtype=document.f1.redtype.value;
	window.open("paypdf/rebtstmntpdf.php?dfrom="+dfrom+"&&dto="+dto+"&&type="+type+"&&redtype="+redtype,true);
 }
 function printcerpdffn()
 {
  	var dfrom=document.f1.fromdt.value;
   	var dto=document.f1.todt.value;
   	var type=document.f1.type.value;
  	var redtype=document.f1.redtype.value;
 	window.open("paypdf/rebtcertpdf.php?dfrom="+dfrom+"&&dto="+dto+"&&type="+type+"&&redtype="+redtype,true);
 }
</script>
<style>
th{
    // background-color: grey;
    font-weight: bold;
    color: white;
    border:1px solid #D7D7D1;
    border-collapse:collapse;
background: rgb(181,189,200); /* Old browsers */
background: -moz-linear-gradient(top, rgba(181,189,200,1) 0%, rgba(130,140,149,1) 36%, rgba(40,52,59,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(181,189,200,1)), color-stop(36%,rgba(130,140,149,1)), color-stop(100%,rgba(40,52,59,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* IE10+ */
background: linear-gradient(to bottom, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#b5bdc8', endColorstr='#28343b',GradientType=0 ); /* IE6-9 */
}
.ro_settled td
{
background: #6bba70; /* Old browsers */
background: -moz-linear-gradient(top, #6bba70 0%, #6bba70 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6bba70), color-stop(100%,#6bba70)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* IE10+ */
background: linear-gradient(to bottom, #6bba70 0%,#6bba70 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6bba70', endColorstr='#6bba70',GradientType=0 ); /* IE6-9 */
color:white;
}
.ro_settl td
{
background: #6bba70; /* Old browsers */
background: -moz-linear-gradient(top, #6bba70 0%, #6bba70 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6bba70), color-stop(100%,#6bba70)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* IE10+ */
background: linear-gradient(to bottom, #6bba70 0%,#6bba70 100%); /* W3C */
filter: progid:DXImageTran.Microsoft.gradient( startColorstr='#6bba70', endColorstr='#6bba70',GradientType=0 ); /* IE6-9 */
color:red;
}
</style>
<body background="back_ground.jpg">
<?php //include('logo2.php'); ?>
<?php //include('includes/top.php'); ?>
<form action='<?print $PHP_SELF?>' method='post' name='f1'>
<br>
<table class="container"><tr><td>
<br>
<center>
<table class="form" style="width:auto;" align='center' >
<tr><th style="background:grey;">REBATE REPORT</th><input type=hidden name='f_yr' id='f_yr' value='<? echo $fin_year ?>'></tr>
<?
if($fromdt > 0)
{ ?>
   <tr><td>Show Report From <input type="text" id="ref_date" value="<?echo $fromdt;?>" name="fromdt" readonly  >
    To <input type="text" id="ref_date1" name="todt" value="<?echo $todt;?>" readonly > </td></tr>
<?}
else
{ ?>
    <tr><td>Show Report From <input type="text" id="ref_date" value="<?echo date('d-m-Y');?>" name="fromdt" readonly  >
    To <input type="text" id="ref_date1" name="todt" value="<?echo date('d-m-Y');?>" readonly > </td></tr>
<?}?>
<tr><td align='center'>Item Group:
<?
$sql="select gname from groupmaster order by gname asc";
$rs=mysqli_query($db,$sql);
if($type!='')
{
    ?>
<select name='type'>
<option><?echo $type?></option>
<?}
else
{ ?>
<select name='type' >
<?}?>
<?
while($row=mysqli_fetch_array($rs))
{
?>
    <option><? echo $row[0] ?></option>
<?
}
?>
</select>
Report by:
<?
if($redtype!='')
{
?>
<select name='redtype'>
<?
if($redtype=='mda')
$redtype='MDA';
if($redtype=='strbtnml')
$redtype='State Rebate Normal';
if($redtype=='strbtspl')
$redtype='State Rebate Special';
?>
<option><?echo $redtype?></option>
<?
}
else
{
?>

<select name='redtype' >
<?
}
?>
<option value='mda'>MDA</option>
<option value='strbtnml'>State Rebate Normal</option>
<option value='strbtspl'>State Rebate Special</option>
</select>

<input type='submit' name='show' value='Show' ></td></tr>
<tr><td align="center"><input type='button'  value='Print Rebcert' onclick=printcerfn()>
<input type='button'  value='Print Rebstmt' onclick=printstfn()>
<input type='button'  value='PDF Rebstmt' onclick=printstpdffn()>
<input type='button'  value='PDF Rebcert' onclick=printcerpdffn()>
</td></tr>
</table>
<br><br>

<?
if(isset($_POST['show']))
{
$fromdt=$_REQUEST['fromdt'];
$todt=$_REQUEST['todt'];
$type=$_REQUEST['type'];
$redtype=$_REQUEST['redtype'];
$mfrom_date = $fromdt;
  $mto_date = $todt;
  $fromdt = implode('-', array_reverse(explode('-', $fromdt)));
  $todt = implode('-', array_reverse(explode('-', $todt)));
$qry=mysqli_query($db,"select gname,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),min(invno),max(invno) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and gname='$type' group by gname,invdate");

print "<table class='report' border='1'>";



print "<tr><th COLSPAN=9>Rebate Report for the period from <$mfrom_date> to <$mto_date></th>";
if(($redtype=='mda')or ($redtype=='MDA'))
{
print "<tr><th>Date</th>
<th>From Bill</th><th>To Bill</th>
<th>Amount</th>
<th>MDA</th><th>Total</th><th>Remarks</th>";
}
if(($redtype=='strbtnml')or ($redtype=='State Rebate Normal'))
{
print "<tr><th>Date</th>
<th>From Bill</th><th>To Bill</th>
<th>Amount</th>
<th>State Rebate Normal</th><th>Total</th><th>Remarks</th>";
}
if(($redtype=='strbtspl')or ($redtype=='State Rebate Special'))
{
print "<tr><th>Date</th>
<th>From Bill</th><th>To Bill</th>
<th>Amount</th>
<th>State Rebate Spl</th><th>Total</th><th>Remarks</th>";
}



  
        $colspan=8;
  $mstr[]='';
  $i=1;
  $mgname = '.';
  $mtot_total = 0;
  $mtot_mba= 0;
  $mtot_strbtnml = 0;
  $mtot_strbtspl = 0;
  $mtot_redct = 0;
  $mtot_befrtx = 0;
  $mtot_taxamt = 0;
  $mtot_nettot = 0;

  $mgr_total = 0;
  $mgr_mba= 0;
  $mgr_strbtnml = 0;
  $mgr_strbtspl = 0;
  $mgr_redct = 0;
  $mgr_befrtx = 0;
  $mgr_taxamt = 0;
  $mgr_nettot = 0;
  $ctr=0;
    while($row=mysqli_fetch_array($qry))
    {
    if($mgname <> $row[0])
    {
      if($mgname <> '.')
      {


print "<tr><td><b>$mgname Total</td><td align='right'>".$mgr_total."</td><td align='right'>".$mgr_mba."</td><td align='right'>".$mgr_strbtnml."</td><td align='right'>".$mgr_strbtspl."</td><td align='right'>".$mgr_redct."</td><td align='right'>".$mgr_befrtx."</td><td align='right'>".$mgr_taxamt."</td><td align='right'>".$mgr_nettot."</td></tr>";
                      $mgr_total = 0;
        $mgr_mba= 0;
        $mgr_strbtnml = 0;
        $mgr_strbtspl = 0;
        $mgr_redct = 0;
        $mgr_befrtx = 0;
        $mgr_taxamt = 0;
        $mgr_nettot = 0;
      }
      print "<tr><td><b>".$row[0]."</td><td></td><td></td><td></td><td></td><td></td><td></td></tr>";

$mgname = $row[0];
    }

$date = $row[1];
if(($redtype=='mda')or ($redtype=='MDA'))
{
print "<tr><td>".$date."</td><td align='right'>$row[10]</td><td align='right'>$row[11]</td><td align='right'>$row[2]</td><td align='right'>$row[3]</td><td align='right'>$row[3]</td><td align='right'></td></tr>";

}

if(($redtype=='strbtnml')or ($redtype=='State Rebate Normal'))
{
print "<tr><td>".$date."</td><td align='right'>$row[10]</td><td align='right'>$row[11]</td><td align='right'>$row[2]</td><td align='right'>$row[4]</td><td align='right'>$row[4]</td><td align='right'></td></tr>";

}
if(($redtype=='strbtspl')or ($redtype=='State Rebate Special'))
{
print "<tr><td>".$date."</td><td align='right'>$row[10]</td><td align='right'>$row[11]</td><td align='right'>$row[2]</td><td align='right'>$row[5]</td><td align='right'>$row[5]</td><td align='right'></td></tr>";

}
    $mgr_total += $row[2];
    $mgr_mba += $row[3];
    $mgr_strbtnml += $row[4];
    $mgr_strbtspl += $row[5];
    $mgr_redct += $row[6];
    $mgr_befrtx += $row[7];
    $mgr_taxamt += $row[8];
    $mgr_nettot += $row[9];

    $mtot_total += $row[2];
    $mtot_mba += $row[3];
    $mtot_strbtnml += $row[4];
    $mtot_strbtspl += $row[5];
    $mtot_redct += $row[6];
    $mtot_befrtx += $row[7];
    $mtot_taxamt += $row[8];
    $mtot_nettot += $row[9];
    $i++;
  }


if(($redtype=='mda')or ($redtype=='MDA'))
{

print "<tr><td><b>Grand Total</td><td></td><td></td><td align='right'><b>".$mtot_total."</td><td align='right'><b>".$mtot_mba."</td><td align='right'><b>".$mtot_mba."</td></tr>";

}

if(($redtype=='strbtnml')or ($redtype=='State Rebate Normal'))
{
print "<tr><td><b>Grand Total</td><td></td><td></td><td align='right'><b>".$mtot_total."</td><td align='right'><b>".$mtot_strbtnml."</td><td align='right'><b>".$mtot_strbtnml."</td></tr>";

}
if(($redtype=='strbtspl')or ($redtype=='State Rebate Special'))
{
print "<tr><td><b>Grand Total</td><td></td><td></td><td align='right'><b>".$mtot_total."</td><td align='right'><b>".$mtot_strbtspl."</td><td align='right'><b>".$mtot_strbtspl."</td></tr>";
}
print "<script> window.open('rebate_printinv.php?fromdt=$fromdt&&todt=$todt&&type=$type&&redtype=$redtype'); </script>";
print "<script> window.open('rebate_printcert.php?fromdt=$fromdt&&todt=$todt&&type=$type&&redtype=$redtype'); </script>";

  print "</table></form>";
  ?>
    <?
}
?>
