<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
include("opendb.php");
include("test.php");
  $fromdt=$_REQUEST['fromdt'];
  $todt=$_REQUEST['todt'];
if(strlen($user)<=0)
{
    print "<script> alert('Invalid User'); document.location='inde.php' </script>";
    return;
}
?>
<link href="css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >

<title>Quantitywise Sale 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 type="text/javascript">

function showsubgrp(str)
{
//var str="hello";
//alert (str);
if (str=="")
  {
  document.getElementById("to_new_registerTD").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("response").innerHTML=xmlhttp.responseText;

    }
  }
xmlhttp.open("GET","selectsubgrp2.php?grp="+str,true);
xmlhttp.send();

}




</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:DXImageTransform.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?>' name='f1' method='post'  >
<br>
<table class="container"><tr><td>
<br>
<center>
<table class="form" style="width:auto;" align='center' >
<tr><th style="background:grey;">QTYWISE SALES REPORT</th></tr>
<?
if ($fromdt>'0000-00-00')
{
  $mfr_dt = $fromdt;
  $mto_dt = $todt;
}
else
{
  $mfr_dt = date('d-m-Y');
  $mto_dt = date('d-m-Y');
}
?>
<tr><td><input type=hidden name='f_yr' id='f_yr' value='<? echo $fin_year ?>'>
Show Report From <input type="text" id="ref_date" value="<?print $mfr_dt;?>" name="fromdt" readonly  >
 To <input type="text" id="ref_date1" name="todt" value="<?echo $mto_dt;?>" readonly > </td></tr>
<tr><td align='center'>Report by:<select name='type' ><option>All</option>
<?
/*$custqry=mysqli_query($db,"select cname from customer where brcode='$brcode'");

while($custrw=mysqli_fetch_array($custqry))
{
?>
  <option><? echo $custrw[0] ?></option>

<?
}
  */
?>
<tr><td align='center'>Item Group:<select name='grp' id='grp' class='grp' onchange='showsubgrp(this.value)'><option>All</option>
<?
$custqry=mysqli_query($db,"select gname from groupmaster order by gname");
while($custrw=mysqli_fetch_array($custqry))
{
?>
  <option><? echo $custrw[0] ?></option>
<?
}?>
</select>
</td></tr>
<tr><td id="response" align='center'>Item Sub Group:<select name='subgrp' ><option>All</option>
</select>
</td>
</tr>
<tr>
<td align=center>
<input type='submit' name='show' value='Show' >
<input type=button value="Print To Excel" onclick=excelfn()>
</td></tr>
</form>
</table>
<br><br>
<?
if(isset($_POST['show']))
{
   $type=$_REQUEST['type'];
   $grp=$_REQUEST['grp'];
   $subgrp=$_REQUEST['subgrp'];
 if($type=='All')
    $msup = "";
  else
    $msup = " and customer = '$type' ";
  if($grp=='All')
    $mgrp = "";
  else
    $mgrp = " and prod_invoice.gname = '$grp' ";
  $fromdt=$_REQUEST['fromdt'];
  $todt=$_REQUEST['todt'];
  $mfrom_date = $fromdt;
  $mto_date = $todt;
    $fromdt = implode('-', array_reverse(explode('-', $fromdt)));
  $todt = implode('-', array_reverse(explode('-', $todt)));
  if($subgrp=='All')
    $qry=mysqli_query($db,"select item_code,item,sum(qty),sum(total) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' ".$msup.$mgrp." group by item_code");
  else
  {
    $qry=mysqli_query($db,"select item_code,item,sum(qty),sum(total) from prod_invoice,partmaster where invdate between '$fromdt' and '$todt' and prod_invoice.brcode='$brcode' ".$msup.$mgrp." and prod_invoice.item_code=partmaster.partcode and partmaster.subgroup = '$subgrp'  group by item_code");
  }
//$sql="select item_code,item,sum(qty),sum(total) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' ".$msup.$mgrp." group by item_code";

  //$qry=mysqli_query($db,"select item_code,item,sum(qty),sum(total) from prodinvoicewholesale where invdate between '$fromdt' and '$todt' and brcode='$brcode' ".$msup.$mgrp." group by item_code");
  print "<table class='report' border='1'>";

  print "<tr><th COLSPAN=5>Quantity wise Sales Report of [".$subgrp." under ".$grp."]for the period from <$mfrom_date> to <$mto_date></th>";
  print "<tr><th>Item Code</th><th>Item </th><th>Qty</th><th>Rate</th><th>Value</th>";
  $i=1;
  $mtot_total = 0;
    while($row=mysqli_fetch_array($qry))
    {
    print "<tr><td>".$row[0]."</td><td>$row[1]</td><td align='right'>$row[2]</td><td align='right'>".round(($row[3]/$row[2]),2)."</td><td align='right'>$row[3]</td></tr>";
    $mgr_total += $row[3];
    $i++;
  }
  print "<tr><td colspan=4><b>Grand Total</td><td align='right'><b>".$mgr_total."</td></tr>";
  print "</table>";
}
?>
<script>
function excelfn()
{
  var dfrom=document.f1.fromdt.value;
  var dto=document.f1.todt.value;
  var type=document.f1.type.value;
  var grp=document.f1.grp.value;
  var subgrp=document.f1.subgrp.value;
  document.location="qtysale_excel.php?dfrom="+dfrom+"&&dto="+dto+"&&type="+type+"&&grp="+grp+"&&subgrp="+subgrp,true;
}
</script>