<?php
session_start();
if (!isset($_SESSION['reusername'])) { echo 'Sorry You Dont Have permissions to View this Page!'; exit();}
require_once 'includes/database.php';
require_once 'includes/display.php';
require_once 'includes/funcs.php';
$user = $_SESSION['reusername'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>QPL | Sales of Trackers </title>
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<link rel="stylesheet" type="text/css" href="css/allcss.css">

</head>

<body>
<p><a href="#" id="printnow" class="printnow" rel="reportsdiv" onclick="myprint();"  style="float:left;">Print</a>
<a href="#" id="closenow" class="closenow" onclick="window.close();">Close</a></p>
<div id="printblk">
 <p><center><span style="font-size:22px;font-weight:bold;font-family:'Trebuchet MS'"> </span><center> <br/>
 <span style="font-size:16px;font-weight:bold;font-family:'Trebuchet MS';"><center> TRACKER SALES&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; From: &nbsp; <?php echo $_REQUEST['s']; ?> &nbsp; To: &nbsp; <?php echo $_REQUEST['e']; ?> </center></span></p>
<table id="treport" style="width:90%;margin-left:20px;font-weight:normal;">

	
<?php	
$db=new MySQLDatabase();
$db->open_connection();
$sd = substr($_REQUEST['s'],6,4)."-".substr($_REQUEST['s'],3,2)."-".substr($_REQUEST['s'],0,2);
$ed = substr($_REQUEST['e'],6,4)."-".substr($_REQUEST['e'],3,2)."-".substr($_REQUEST['e'],0,2);   
$td = $sd;	
$nextmon = date("M", strtotime($sd));		$endmon = date("M",strtotime($ed)); 	$nextm = intval(date("m", strtotime($sd)));
$stm = intval(date("m", strtotime($sd)));
echo '<thead> <th>Customer</th> <th>'.$nextmon.'</th>';
$qry = "SELECT trackinvoices.cname, SUM(IF(MONTH(trackinvoices.idate)=".$nextm.",1,0)) as qty".$nextm;
while ($nextmon !== $endmon){   $tdd = strtotime("+1 month", strtotime($td)); 
								$nextmon = date("M", $tdd);
								$td = date("Y-m-d",$tdd);      
								echo '<th>'.$nextmon.'</th>';
								$nextm = intval(date("m", $tdd));
								$qry = $qry.", SUM(IF(MONTH(trackinvoices.idate)=".$nextm.",1,0)) as qty".$nextm;	}
echo ' <th>Total Qty</th>  <th>Total Value</th> </thead>';
for($i=0; $i<=12; $i++){ $tarr[$i]=0; }
$gtot=0;
$qry = $qry.", SUM(1) as tqty, SUM(jcards.am) as ival FROM trackinvoices, jcards WHERE jcards.invno = trackinvoices.invno AND trackinvoices.idate >='$sd' AND trackinvoices.idate <='$ed' GROUP BY trackinvoices.cname";
$result = mysqli_query($db->connection, $qry) or die(mysqli_error($db->connection) );
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){  
		$nextmon = date("M", strtotime($sd));				$nextm = intval(date("m", strtotime($sd)));		$td = $sd;	
		echo '<tr><td>'.$row['cname'].'</td> <td>'.$row['qty'.$nextm].'</td>';
		$tarr[$nextm] = $tarr[$nextm] + intval($row['qty'.$nextm]);
		while ($nextmon !== $endmon){   $tdd = strtotime("+1 month", strtotime($td)); 
										$nextmon = date("M", $tdd);
										$td = date("Y-m-d",$tdd);
										$nextm = intval(date("m", $tdd));
										$tarr[$nextm] = $tarr[$nextm] + intval($row['qty'.$nextm]);
										echo '<td>'.$row['qty'.$nextm].'</td>';
		 }
		 echo '<td align="right">'.$row['tqty'].'</td> <td align="right">'.number_format($row['ival'],0).'</td></tr>';
		 $tarr[0] = $tarr[0] + intval($row['tqty']);
		 $gtot = $gtot + intval($row['ival']);
    }
$nextmon = date("M", strtotime($sd));	$nextm = intval(date("m", strtotime($sd)));		$td = $sd; 
echo '<tr><td colspan="'.($nextm-$stm+5).'"><hr></td></tr>';
echo '<tr><td>Totals</td> <td>'.$tarr[$nextm].'</td>';
while ($nextmon !== $endmon){   $tdd = strtotime("+1 month", strtotime($td)); 
										$nextmon = date("M", $tdd);
										$td = date("Y-m-d",$tdd);
										$nextm = intval(date("m", $tdd));
										echo '<td>'.$tarr[$nextm].'</td>';
		 }
echo '<td align="right">'.$tarr[0].'</td> <td align="right">'.number_format($gtot,0).'</td></tr>';	
 
?>	
</table>

<span align="left" style="font-size:x-small">Printed by: <?php echo $user; ?> &nbsp;&nbsp; <?php echo date("d-m-Y H:i:s"); ?> </span> <br />
<div style="page-break-after:always"> </div>
</div>
</body></html>

<script type="text/javascript">
function myprint(){
	var w = window.open('', '', 'width=800,height=600,resizeable,scrollbars');
	 w.document.write($("#printblk").html());
	 w.document.close(); 										// needed for chrome and safari
	 javascript:w.print();
	 w.close();
};	


</script>