<?
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;
}

?>
<link href="css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >
<title>Purchase 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" />
<script type="text/javascript">
$(function() {
if ($.browser.msie && $.browser.version.substr(0,1)<7)
{
	$('li').has('ul').mouseover(function(){
	$(this).children('ul').show();
	}).mouseout(function(){
	$(this).children('ul').hide();
	})
        }
});        
</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>
<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?>' method='post' >
<br>
<table class="container"><tr><td>
<br>
<center>
<table class="form" style="width:auto;" align='center' >
<tr><th style="background:grey;">PURCHASE REPORT</th></tr>
<tr><td>Purchase Report From <input type="text" id="inputField" value="<?echo date('Y-m-d');?>" name="fromdt" readonly  >
 To <input type="text" id="inputField1" name="todt" value="<?echo date('Y-m-d');?>" readonly > </td></tr>
<tr><td align='center'>Report by:<select name='type' ><option>All</option></select>
<input type='submit' name='show' value='Show' ></td></tr>
</table>
    </form>
<br><br>
<?
if(isset($_POST['show']))
{
	$fromdt=$_REQUEST['fromdt'];
	$todt=$_REQUEST['todt'];
	$mgrp = "";
	$mgrp1[]="";
	$mtotal1[]=0;
	$qry1=mysqli_query($db,"select distinct gname from itempurchase where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by gname");
	$i=0;
  	while($row=mysqli_fetch_array($qry1))
  	{
		$mgrp .= "<th>".$row[0]."</th>";
		$mgrp1[$i++]=$row[0];
	}
	$grp_nos = $i;
	$qry=mysqli_query($db,"select invdate,supinvno,invno,customer,gname,sum(total),mba,comision,discount,taxamt,mrgin,pfchrgs,othr_chrgs from itempurchase where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by invno,gname");
       print "<table class='report' border='1'>";
	$mfrom_date = $fromdt;
	$mto_date = $todt;
	print "<tr><th COLSPAN=22>Purchase Report for the period from <$mfrom_date> to <$mto_date></th>";
	print "<tr><th>Date</th><th>Invoice No</th><th>GRS No.</th><th>Party</th>".$mgrp."<th>MDA</th><th>Commission</th><th>Discount</th><th>Bef.Tax</th><th>Tax</th><th>Margin</th><th>P&F Charges</th><th>Other Charges</th><th>Net</th>";
	$i=1;
	$minvno = "";
	$mtotal = 0;
	$mtax = 0;
	$mnettot = 0;
	$mgr_total = 0;
	$mgr_tax = 0;
	$mgr_nettot = 0;

	$mgr_mda = 0;
	$mgr_comm = 0;
	$mgr_disc = 0;
	$mgr_beftax = 0;
	$mgr_tax = 0;
	$mgr_margin = 0;
	$mgr_pfcharge = 0;
	$mgr_othcharge = 0;
	$mgr_nettot = 0;
	$mgr_tax = 0;
  	while($row=mysqli_fetch_array($qry))
  	{  
		if($minvno <> $row[2])
		{
			if($i<>1)
			{
				for ($k=$mstart;$k<$grp_nos;$k++)
				{
					print "<td align='right'>0.00</td>";
				}
				$mgr_total += $mtotal;
				$mgr_tax += $mtax;
				$mgr_nettot += $mnettot;
				$mmda = $mtotal * $mmda_per/100;
				$mcommission = $mtotal * $mcomm/100;
				$mdiscount = $mtotal * $mdisc_per/100;
				$mbefrtx = ($mtotal - $mmda - $mcommission - $mdiscount);
				$margin = $mtotal * $mmargin/100;
				$mnettot = $mbefrtx + $mtaxamt + $margin + $mpfchrgs + $mothchrgs;
				$mgr_mda += $mmda;
				$mgr_comm += $mcommission;
				$mgr_disc += $mdiscount;
				$mgr_beftax += $mbefrtx;
				$mgr_tax += $mtaxamt;
				$mgr_margin += $margin;
				$mgr_pfcharge += $mpfchrgs;
				$mgr_othcharge += $mothchrgs;
				$mgr_nettot += $mnettot;
				print "<td align='right'>$mmda</td><td align='right'>$mcommission</td><td align='right'>$mdiscount</td>";
				print "<td align='right'>$mbefrtx</td><td align='right'>$mtaxamt</td><td align='right'>$margin</td><td align='right'>$mpfchrgs</td><td align='right'>$mothchrgs</td><td align='right'>$mnettot</td></tr>";
				$mtotal = 0;
				$mtax = 0;
				$mnettot = 0;
			}
			print "<tr><td>".$row[0]."</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td>";
			$mstart = 0;
		}
		$key = array_search($row[4], $mgrp1);
		for ($k=$mstart;$k<$key;$k++)
		{
			print "<td align='right'>0.00</td>";
		}
		$mstart = $k+1;
		print "<td align='right'>$row[5]</td>";
		$mtotal1[$mstart] += $row[5];
		$mtotal += $row[5];
		$mgr_total += $row[3];

		$mmda_per = $row[6];
		$mcomm = $row[7];
		$mdisc_per = $row[8];
		$mtaxamt = $row[9];
		$mmargin = $row[10];
		$mpfchrgs = $row[11];
		$mothchrgs = $row[12];

		$i++;
		$minvno = $row[2];
	}		
	for ($k=$mstart;$k<$grp_nos;$k++)
	{
		print "<td align='right'>0.00</td>";
	}
	$mgr_total += $mtotal;
	$mgr_tax += $mtax;
	$mgr_nettot += $mnettot;
	$mmda = $mtotal * $mmda_per/100;
	$mcommission = $mtotal * $mcomm/100;
	$mdiscount = $mtotal * $mdisc_per/100;
	$mbefrtx = ($mtotal - $mmda - $mcommission - $mdiscount);
	$margin = $mtotal * $mmargin/100;
	$mnettot = $mbefrtx + $mtaxamt + $margin + $mpfchrgs + $mothchrgs;

	$mgr_mda += $mmda;
	$mgr_comm += $mcommission;
	$mgr_disc += $mdiscount;
	$mgr_beftax += $mbefrtx;
	$mgr_tax += $mtaxamt;
	$mgr_margin += $margin;
	$mgr_pfcharge += $mpfchrgs;
	$mgr_othcharge += $mothchrgs;
	$mgr_nettot += $mnettot;
	print "<td align='right'>$mmda</td><td align='right'>$mcommission</td><td align='right'>$mdiscount</td>";
	print "<td align='right'>$mbefrtx</td><td align='right'>$mtaxamt</td><td align='right'>$margin</td><td align='right'>$mpfchrgs</td><td align='right'>$mothchrgs</td><td align='right'>$mnettot</td></tr>";
	print "<tr><td colspan=4><b>Grand Total </td>";
	for ($y=1;$y<$k+1;$y++)
	{
		print "<td align='right'>$mtotal1[$y]</td>";
	}
	print "<td align='right'>$mgr_mda</td><td align='right'>$mgr_comm</td><td align='right'>$mgr_disc</td><td align='right'>$mgr_beftax</td><td align='right'>$mgr_tax</td>";
	print "<td align='right'>$mgr_margin</td><td align='right'>$mgr_pfcharge</td><td align='right'>$mgr_othcharge</td><td align='right'>$mgr_nettot</td>";
	print "</table>";
}
?>
</td>
</tr>
</table>