<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php 
include('../include/session.php');
$today = date("Y-m-d");

$page_name = "Stores-main";
include ("../navigation/access.php");

	
$report_year = $_GET['report_year_select'];
$report_type = $_GET['report_type'];
$line_num = $_GET['line_num_text'];


if ($report_type==1) {
$report_cat = 'Tiles and Accessories';
$table_name = $_SESSION['db_prefix']."_order_lines";
$total_query = "SELECT order_date, SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' GROUP BY EXTRACT(month FROM order_date) ";
$total_result = mySQL_query($total_query);
$total_num = mysql_num_rows($total_result);
}

if ($report_type==2) {
$report_cat = 'Tiles';
$table_name = $_SESSION['db_prefix']."_order_lines";
$total_query = "SELECT order_date, SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND sub_category = 'Tiles'  GROUP BY EXTRACT(month FROM order_date) ";
$total_result = mySQL_query($total_query);
$total_num = mysql_num_rows($total_result);
}

if ($report_type==3) {
$report_cat = 'Accessories';
$table_name = $_SESSION['db_prefix']."_order_lines";
$total_query = "SELECT order_date, SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND sub_category = 'Accessories'  GROUP BY EXTRACT(month FROM order_date) ";
$total_result = mySQL_query($total_query);
$total_num = mysql_num_rows($total_result);
}

$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$double_roman_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Double Roman'  GROUP BY EXTRACT(month FROM order_date) ";
$double_roman_result = mySQL_query($double_roman_query);
$double_roman_num = mysql_num_rows($double_roman_result);


$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$slate_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Slate'  GROUP BY EXTRACT(month FROM order_date) ";
$slate_result = mySQL_query($slate_query);
$slate_num = mysql_num_rows($slate_result);


$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$venetian_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Venetian'  GROUP BY EXTRACT(month FROM order_date) ";
$venetian_result = mySQL_query($venetian_query);
$venetian_num = mysql_num_rows($venetian_result);

$table_name = $_SESSION['db_prefix']."_order_lines";
$accessories_query = "SELECT SUM(quantity) AS total_quantity FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND sub_category = 'Accessories'  GROUP BY EXTRACT(month FROM order_date) ";
$accessories_result = mySQL_query($accessories_query);
$accessories_num = mysql_num_rows($accessories_result);

$report_name = 'Product distribution distributors '.$report_year;  
$report_no = '';
?>
<!--style sheets-->
<link href="../css/print.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" type="image/x-icon" href="../images/favicon.ico">
<title>Product distribution distributors <?php echo $report_year.' ('.$today.')' ?> </title>
       
</head>

<body>

<script>
window.onload = function () {
  window.print();
  setTimeout(function(){window.close();}, 100);
}
</script>

<br>
<!--Page 1-->
<div class="print_standard_frame" style="width:700px;">
<?php 
if ($line_num<=9) {
	  $lines=$line_num;
}
else {
	$lines=9;
}

$t=0;
while ($t<$lines) { 
?>
<div style="min-height:700px;">
<div style="float:left;"><img src="../images/radstone_medium_logo.png" width="144" height="54"></div>
<div style="float:left; margin-left:20px;"><label style="font-size:18px;"><b> <?php echo $report_name ?> </b></label><br>
<br>
Report date: <?php echo date("d-m-Y", strtotime($today)) ?>&nbsp;&nbsp;page 1 </div> 

<div class="print_standard_frame" style="width:700px;">
Reporting on: <?php echo $report_cat ?> &nbsp;&nbsp;&nbsp;Report year: <?php echo $report_year ?><br><br>
<table border="1" cellspacing="3" cellpadding="3" style="border-collapse:collapse;" >
<thead>
  <tr>
    <td align="center" width="50">Month</td>
    <td align="center" width="50">Total<br>Sales</td>
    
    <?php	
	$columns = 0;
	$k=0;
	while ($k<$lines) {
	
	 
	  		$cust_id = $_SESSION['cust_id'.$k];
						
				$table_name2 = $_SESSION['db_prefix']."_customer";
				$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
				$customer_result = mySQL_query($customer_query);
				$customer_num = mysql_num_rows($customer_result);	
				$account_type = mysql_result($customer_result, 0, "account_type");
				if ($account_type=="corporate") { 
					$cust_name = mysql_result($customer_result, 0, "comp_name"); 
				} 
				else {
					$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 				}
			
			${'column_cust_id'.$columns} = $cust_id;
			
			$columns = $columns + 1;
					  	
	?>
    <td align="center" width="60"><?php echo $cust_name ?></td>
    <td align="center" width="16">in %</td>
    <?php
	$k++;
	}
		
	
	
	?>
  </tr>
</thead>
<?php 
$i=0;
while ($i<$total_num) {
	$order_month =  mysql_result($total_result, $i, "order_date");
	$order_month = date("F", strtotime($order_month));
	$all_total = mysql_result($total_result, $i, "total_quantity");
	$month_no = date("m", strtotime($order_month));
?>	  
  <tr>
    <td><?php echo $order_month ?></td>
    <td align="right"><?php echo $all_total ?></td>
    <?php
	
	
	$j=0;
	while ($j<$columns) {
	
	$cust_id = $_SESSION['cust_id'.$j];	
	$month_period = $month_no;
	
	
if ($report_type==1) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND EXTRACT(month FROM order_date) = '".$month_period."' AND cust_id = '".$cust_id."'  GROUP BY EXTRACT(month FROM order_date) ";
	
	$cust_total_result = mySQL_query($cust_total_query);
	$cust_total_num = mysql_num_rows($cust_total_result);
	if ($cust_total_num>0) {
	$cust_total = mysql_result($cust_total_result, 0, "total_quantity");
	}
	else {
    $cust_total = 0;
	}
	$perc = ($cust_total / $all_total) * 100;
}

if ($report_type==2) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND EXTRACT(month FROM order_date) = '".$month_period."' AND cust_id = '".$cust_id."' AND sub_category = 'Tiles'  GROUP BY EXTRACT(month FROM order_date) ";
	$cust_total_result = mySQL_query($cust_total_query);
	$cust_total_num = mysql_num_rows($cust_total_result);
	if ($cust_total_num>0) {
	$cust_total = mysql_result($cust_total_result, 0, "total_quantity");
	}
	else {
    $cust_total = 0;
	}
	$perc = ($cust_total / $all_total) * 100;
}

if ($report_type==3) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND EXTRACT(month FROM order_date) = '".$month_period."' AND cust_id = '".$cust_id."' AND sub_category = 'Accessories'  GROUP BY EXTRACT(month FROM order_date) ";
	$cust_total_result = mySQL_query($cust_total_query);
	$cust_total_num = mysql_num_rows($cust_total_result);
	if ($cust_total_num>0) {
	$cust_total = mysql_result($cust_total_result, 0, "total_quantity");
	}
	else {
    $cust_total = 0;
	}
	$perc = ($cust_total / $all_total) * 100;
}


//===================popup balloon

	$table_name2 = $_SESSION['db_prefix']."_customer";
	$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
	$customer_result = mySQL_query($customer_query);
	$customer_num = mysql_num_rows($customer_result);	
	$account_type = mysql_result($customer_result, 0, "account_type");
		if ($account_type=="corporate") { 
				$cust_name = mysql_result($customer_result, 0, "comp_name"); 
			} 
		else {
				$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 			}
			
	$double_roman_quantity = mysql_result($double_roman_result, $i, "total_quantity");
	$slate_quantity = mysql_result($slate_result, $i, "total_quantity");
	$venetian_quantity = mysql_result($venetian_result, $i, "total_quantity");
	$accessories_quantity = mysql_result($accessories_result, $i, "total_quantity");
	
$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$cust_double_roman_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Double Roman' AND $table_name.cust_id = '".$cust_id."' AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_double_roman_result = mySQL_query($cust_double_roman_query);
$cust_double_roman_num = mysql_num_rows($cust_double_roman_result);
if ($cust_double_roman_num > 0) {
$cust_double_roman = mysql_result($cust_double_roman_result, 0, "total_quantity");
}
else {
$cust_double_roman = 0;
}
$cust_double_roman_perc = number_format(($cust_double_roman / $double_roman_quantity) * 100, 2, '.', ',');

$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$cust_slate_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Slate' AND $table_name.cust_id = '".$cust_id."'  AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_slate_result = mySQL_query($cust_slate_query);
$cust_slate_num = mysql_num_rows($cust_slate_result);
if ($cust_slate_num > 0) {
$cust_slate = mysql_result($cust_slate_result, 0, "total_quantity");
}
else {
$cust_slate = 0;
}
$cust_slate_perc = number_format(($cust_slate / $slate_quantity) * 100, 2, '.', ',');

$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$cust_venetian_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Venetian' AND $table_name.cust_id = '".$cust_id."'  AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_venetian_result = mySQL_query($cust_venetian_query);
$cust_venetian_num = mysql_num_rows($cust_venetian_result);
if ($cust_venetian_num > 0) {
$cust_venetian = mysql_result($cust_venetian_result, 0, "total_quantity");
}
else {
$cust_venetian = 0;
}
$cust_venetian_perc = number_format(($cust_venetian / $venetian_quantity) * 100, 2, '.', ',');

$table_name = $_SESSION['db_prefix']."_order_lines";
$cust_accessories_query = "SELECT SUM(quantity) AS total_quantity FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND sub_category = 'Accessories' AND $table_name.cust_id = '".$cust_id."'  AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_accessories_result = mySQL_query($cust_accessories_query);
$cust_accessories_num = mysql_num_rows($cust_accessories_result);
if ($cust_accessories_num > 0) {
$cust_accessories = mysql_result($cust_accessories_result, 0, "total_quantity");
}
else {
$cust_accessories = 0;
}
$cust_accessories_perc = number_format(($cust_accessories / $accessories_quantity) * 100, 2, '.', ',');
			
$customerTT_line1 = $cust_name.'<br>';	
$customerTT_line2 = '<table><tr><td width=100px; align=center;>&nbsp;</td><td width=50px; align=center;>DR</td><td width=50px; align=center;>SL</td><td width=50px; align=center;>VN</td><td width=50px; align=center;>Acc</td></tr><tr><td>Total Sales</td><td align=right;>'.$double_roman_quantity.'</td><td align=right;>'.$slate_quantity.'</td><td align=right;>'.$venetian_quantity.'</td><td align=right;>'.$accessories_quantity.'</td></tr><tr><td>Customer Sales</td><td align=right;>'.$cust_double_roman.'</td><td align=right;>'.$cust_slate.'</td><td align=right;>'.$cust_venetian.'</td><td align=right;>'.$cust_accessories.'</td></tr><tr><td>Customer %</td><td>'.$cust_double_roman_perc.'</td><td>'.$cust_slate_perc.'</td><td>'.$cust_venetian_perc.'</td><td>'.$cust_accessories_perc.'</td></tr></table>';
$customerTT = $customerTT_line1.$customerTT_line2;


	
	?>
    <td align="right"><?php echo $cust_total ?></td>
    <td align="right"><?php echo number_format($perc, 1, '.', ',') ?></td>
    <?php
	$j++;
	}
	?>
		
  </tr>
<?php
$i++;
}
?>
<tfoot>
<tr>
    <td>&nbsp;</td>
    <?php
	if ($report_type==1) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' ";
	$full_total_result = mySQL_query($full_total_query);
	$full_total = mysql_result($full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==2) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND sub_category = 'Tiles' ";
	$full_total_result = mySQL_query($full_total_query);
	$full_total = mysql_result($full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==3) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND sub_category = 'Accessories' ";
	$full_total_result = mySQL_query($full_total_query);
	$full_total = mysql_result($full_total_result, 0, "total_quantity");
	}
	?>
    <td align="right"><?php echo $full_total ?></td>
    <?php
	$m=0;
	while ($m<$columns) {
		$cust_id = ${'column_cust_id'.$m};
	
	if ($report_type==1) {	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND cust_id = '".$cust_id."' ";
	$cust_full_total_result = mySQL_query($cust_full_total_query);
	$cust_full_total = mysql_result($cust_full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==2) {	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND cust_id = '".$cust_id."'  AND sub_category = 'Tiles' ";
	$cust_full_total_result = mySQL_query($cust_full_total_query);
	$cust_full_total = mysql_result($cust_full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==3) {	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND cust_id = '".$cust_id."'  AND sub_category = 'Accessories' ";
	$cust_full_total_result = mySQL_query($cust_full_total_query);
	$cust_full_total = mysql_result($cust_full_total_result, 0, "total_quantity");
	}
	
	$perc_full_total = ($cust_full_total / $full_total) * 100;
	?>
    <td align="right"><?php echo $cust_full_total ?></td>
    <td align="right"><?php echo number_format($perc_full_total, 1, '.', ',') ?></td>
    <?php
	$m++;
	$t++;
	}
	?>
  </tr>
</tfoot>
</table>
</div>
</div>
<?php
}
?>

</div>

<!--Page 2-->
<div class="print_standard_frame" style="margin-top:-50px; width:700px;">
<?php 
if ($line_num>9) {

if ($line_num<=18) {
	  $lines=$line_num;
}
else {
	$lines=18;
}

	
while ($t<$lines) { 
?>
<div <?php if ($line_num>18) { ?> style="page-break-after:always; min-height:700px;" <?php } ?> >
<div style="float:left;"><img src="../images/radstone_medium_logo.png" width="144" height="54"></div>
<div style="float:left; margin-left:20px;"><label style="font-size:18px;"><b> <?php echo $report_name ?> </b></label><br>
<br>
Report date: <?php echo date("d-m-Y", strtotime($today)) ?>&nbsp;&nbsp;page 2</div> 

<div class="print_standard_frame" style="width:700px;">
Reporting on: <?php echo $report_cat ?> &nbsp;&nbsp;&nbsp;Report year: <?php echo $report_year ?><br><br>
<table  border="1" cellspacing="3" cellpadding="3" style="border-collapse:collapse;" >
<thead>
  <tr>
    <td align="center" width="50">Month</td>
    <td align="center" width="50">Total<br>Sales</td>
    
    <?php	
	//$columns = 0;
	
	while ($k<$lines) {
	
	 
	  		$cust_id = $_SESSION['cust_id'.$k];
						
				$table_name2 = $_SESSION['db_prefix']."_customer";
				$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
				$customer_result = mySQL_query($customer_query);
				$customer_num = mysql_num_rows($customer_result);	
				$account_type = mysql_result($customer_result, 0, "account_type");
				if ($account_type=="corporate") { 
					$cust_name = mysql_result($customer_result, 0, "comp_name"); 
				} 
				else {
					$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 				}
			
			${'column_cust_id'.$columns} = $cust_id;
			
			$columns = $columns + 1;
					  	
	?>
    <td align="center" width="60"><?php echo $cust_name ?></td>
    <td align="center" width="16">in %</td>
    <?php
	$k++;
	}
		
	
	
	?>
  </tr>
</thead>

<?php 
$i=0;
while ($i<$total_num) {
	$order_month =  mysql_result($total_result, $i, "order_date");
	$order_month = date("F", strtotime($order_month));
	$all_total = mysql_result($total_result, $i, "total_quantity");
	$month_no = date("m", strtotime($order_month));
?>	  
  <tr>
    <td><?php echo $order_month ?></td>
    <td align="right"><?php echo $all_total ?></td>
    <?php
	
	
	
	$j=9;
	while ($j<$columns) {
	
	$cust_id = $_SESSION['cust_id'.$j];	
	$month_period = $month_no;
	
	
if ($report_type==1) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND EXTRACT(month FROM order_date) = '".$month_period."' AND cust_id = '".$cust_id."'  GROUP BY EXTRACT(month FROM order_date) ";
	
	
	$cust_total_result = mySQL_query($cust_total_query);
	$cust_total_num = mysql_num_rows($cust_total_result);
	if ($cust_total_num>0) {
	$cust_total = mysql_result($cust_total_result, 0, "total_quantity");
	}
	else {
    $cust_total = 0;
	}
	$perc = ($cust_total / $all_total) * 100;
	
}

if ($report_type==2) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND EXTRACT(month FROM order_date) = '".$month_period."' AND cust_id = '".$cust_id."' AND sub_category = 'Tiles'  GROUP BY EXTRACT(month FROM order_date) ";
	$cust_total_result = mySQL_query($cust_total_query);
	$cust_total_num = mysql_num_rows($cust_total_result);
	if ($cust_total_num>0) {
	$cust_total = mysql_result($cust_total_result, 0, "total_quantity");
	}
	else {
    $cust_total = 0;
	}
	$perc = ($cust_total / $all_total) * 100;
}

if ($report_type==3) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND EXTRACT(month FROM order_date) = '".$month_period."' AND cust_id = '".$cust_id."' AND sub_category = 'Accessories'  GROUP BY EXTRACT(month FROM order_date) ";
	$cust_total_result = mySQL_query($cust_total_query);
	$cust_total_num = mysql_num_rows($cust_total_result);
	if ($cust_total_num>0) {
	$cust_total = mysql_result($cust_total_result, 0, "total_quantity");
	}
	else {
    $cust_total = 0;
	}
	$perc = ($cust_total / $all_total) * 100;
}


	$table_name2 = $_SESSION['db_prefix']."_customer";
	$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
	$customer_result = mySQL_query($customer_query);
	$customer_num = mysql_num_rows($customer_result);	
	$account_type = mysql_result($customer_result, 0, "account_type");
		if ($account_type=="corporate") { 
				$cust_name = mysql_result($customer_result, 0, "comp_name"); 
			} 
		else {
				$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 			}
			
	$double_roman_quantity = mysql_result($double_roman_result, $i, "total_quantity");
	$slate_quantity = mysql_result($slate_result, $i, "total_quantity");
	$venetian_quantity = mysql_result($venetian_result, $i, "total_quantity");
	$accessories_quantity = mysql_result($accessories_result, $i, "total_quantity");
	
$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$cust_double_roman_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Double Roman' AND $table_name.cust_id = '".$cust_id."' AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_double_roman_result = mySQL_query($cust_double_roman_query);
$cust_double_roman_num = mysql_num_rows($cust_double_roman_result);
if ($cust_double_roman_num > 0) {
$cust_double_roman = mysql_result($cust_double_roman_result, 0, "total_quantity");
}
else {
$cust_double_roman = 0;
}
$cust_double_roman_perc = number_format(($cust_double_roman / $double_roman_quantity) * 100, 2, '.', ',');

$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$cust_slate_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Slate' AND $table_name.cust_id = '".$cust_id."'  AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_slate_result = mySQL_query($cust_slate_query);
$cust_slate_num = mysql_num_rows($cust_slate_result);
if ($cust_slate_num > 0) {
$cust_slate = mysql_result($cust_slate_result, 0, "total_quantity");
}
else {
$cust_slate = 0;
}
$cust_slate_perc = number_format(($cust_slate / $slate_quantity) * 100, 2, '.', ',');

$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$cust_venetian_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Venetian' AND $table_name.cust_id = '".$cust_id."'  AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_venetian_result = mySQL_query($cust_venetian_query);
$cust_venetian_num = mysql_num_rows($cust_venetian_result);
if ($cust_venetian_num > 0) {
$cust_venetian = mysql_result($cust_venetian_result, 0, "total_quantity");
}
else {
$cust_venetian = 0;
}
$cust_venetian_perc = number_format(($cust_venetian / $venetian_quantity) * 100, 2, '.', ',');

$table_name = $_SESSION['db_prefix']."_order_lines";
$cust_accessories_query = "SELECT SUM(quantity) AS total_quantity FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND sub_category = 'Accessories' AND $table_name.cust_id = '".$cust_id."'  AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_accessories_result = mySQL_query($cust_accessories_query);
$cust_accessories_num = mysql_num_rows($cust_accessories_result);
if ($cust_accessories_num > 0) {
$cust_accessories = mysql_result($cust_accessories_result, 0, "total_quantity");
}
else {
$cust_accessories = 0;
}
$cust_accessories_perc = number_format(($cust_accessories / $accessories_quantity) * 100, 2, '.', ',');
			
$customerTT_line1 = $cust_name.'<br>';	
$customerTT_line2 = '<table><tr><td width=100px; align=center;>&nbsp;</td><td width=50px; align=center;>DR</td><td width=50px; align=center;>SL</td><td width=50px; align=center;>VN</td><td width=50px; align=center;>Acc</td></tr><tr><td>Total Sales</td><td align=right;>'.$double_roman_quantity.'</td><td align=right;>'.$slate_quantity.'</td><td align=right;>'.$venetian_quantity.'</td><td align=right;>'.$accessories_quantity.'</td></tr><tr><td>Customer Sales</td><td align=right;>'.$cust_double_roman.'</td><td align=right;>'.$cust_slate.'</td><td align=right;>'.$cust_venetian.'</td><td align=right;>'.$cust_accessories.'</td></tr><tr><td>Customer %</td><td>'.$cust_double_roman_perc.'</td><td>'.$cust_slate_perc.'</td><td>'.$cust_venetian_perc.'</td><td>'.$cust_accessories_perc.'</td></tr></table>';
$customerTT = $customerTT_line1.$customerTT_line2;


	
	?>
    <td align="right"><?php echo $cust_total ?></td>
    <td align="right"><?php echo number_format($perc, 1, '.', ',') ?></td>
    <?php
	$j++;
	}
	?>
		
  </tr>
<?php
$i++;
}
?>
<tfoot>
<tr>
    <td>&nbsp;</td>
    <?php
	if ($report_type==1) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' ";
	$full_total_result = mySQL_query($full_total_query);
	$full_total = mysql_result($full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==2) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND sub_category = 'Tiles' ";
	$full_total_result = mySQL_query($full_total_query);
	$full_total = mysql_result($full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==3) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND sub_category = 'Accessories' ";
	$full_total_result = mySQL_query($full_total_query);
	$full_total = mysql_result($full_total_result, 0, "total_quantity");
	}
	?>
    <td align="right"><?php echo $full_total ?></td>
    <?php
	
	while ($m<$columns) {
		$cust_id = ${'column_cust_id'.$m};
	
	if ($report_type==1) {	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND cust_id = '".$cust_id."' ";
	$cust_full_total_result = mySQL_query($cust_full_total_query);
	$cust_full_total = mysql_result($cust_full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==2) {	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND cust_id = '".$cust_id."'  AND sub_category = 'Tiles' ";
	$cust_full_total_result = mySQL_query($cust_full_total_query);
	$cust_full_total = mysql_result($cust_full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==3) {	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND cust_id = '".$cust_id."'  AND sub_category = 'Accessories' ";
	$cust_full_total_result = mySQL_query($cust_full_total_query);
	$cust_full_total = mysql_result($cust_full_total_result, 0, "total_quantity");
	}
	
	$perc_full_total = ($cust_full_total / $full_total) * 100;
	?>
    <td align="right"><?php echo $cust_full_total ?></td>
    <td align="right"><?php echo number_format($perc_full_total, 1, '.', ',') ?></td>
    <?php
	$m++;
	$t++;
	}
	?>
  </tr>
</tfoot>
</table>
</div>
</div>
<?php
}
}
?>
</div>

<!--Page 3-->
<div class="print_standard_frame" style="margin-top:-50px;">
<?php 
if ($line_num>18) {

if ($line_num<=27) {
	  $lines=$line_num;
}
else {
	$lines=27;
}

	
while ($t<$lines) { 
?>
<div <?php if ($line_num>27) { ?> style="page-break-after:always; min-height:700px;" <?php } ?> >
<div style="float:left;"><img src="../images/radstone_medium_logo.png" width="144" height="54"></div>
<div style="float:left; margin-left:20px;"><label style="font-size:18px;"><b> <?php echo $report_name ?> </b></label><br>
<br>
Report date: <?php echo date("d-m-Y", strtotime($today)) ?>&nbsp;&nbsp;page 3</div> 

<div class="print_standard_frame" style="width:700px;">
Reporting on: <?php echo $report_cat ?> &nbsp;&nbsp;&nbsp;Report year: <?php echo $report_year ?><br><br>
<table  border="1" cellspacing="3" cellpadding="3" style="border-collapse:collapse;" >
<thead>
  <tr>
    <td align="center" width="50">Month</td>
    <td align="center" width="50">Total<br>Sales</td>
    
    <?php	
	//$columns = 0;
	
	while ($k<$lines) {
	
	 
	  		$cust_id = $_SESSION['cust_id'.$k];
						
				$table_name2 = $_SESSION['db_prefix']."_customer";
				$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
				$customer_result = mySQL_query($customer_query);
				$customer_num = mysql_num_rows($customer_result);	
				$account_type = mysql_result($customer_result, 0, "account_type");
				if ($account_type=="corporate") { 
					$cust_name = mysql_result($customer_result, 0, "comp_name"); 
				} 
				else {
					$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 				}
			
			${'column_cust_id'.$columns} = $cust_id;
			
			$columns = $columns + 1;
					  	
	?>
    <td align="center" width="60"><?php echo $cust_name ?></td>
    <td align="center" width="16">in %</td>
    <?php
	$k++;
	}
		
	
	
	?>
  </tr>
</thead>

<?php 
$i=0;
while ($i<$total_num) {
	$order_month =  mysql_result($total_result, $i, "order_date");
	$order_month = date("F", strtotime($order_month));
	$all_total = mysql_result($total_result, $i, "total_quantity");
	$month_no = date("m", strtotime($order_month));
?>	  
  <tr>
    <td><?php echo $order_month ?></td>
    <td align="right"><?php echo $all_total ?></td>
    <?php
	
	
	
	$j=18;
	while ($j<$columns) {
	
	$cust_id = $_SESSION['cust_id'.$j];	
	$month_period = $month_no;
	
	
if ($report_type==1) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND EXTRACT(month FROM order_date) = '".$month_period."' AND cust_id = '".$cust_id."'  GROUP BY EXTRACT(month FROM order_date) ";
	
	
	$cust_total_result = mySQL_query($cust_total_query);
	$cust_total_num = mysql_num_rows($cust_total_result);
	if ($cust_total_num>0) {
	$cust_total = mysql_result($cust_total_result, 0, "total_quantity");
	}
	else {
    $cust_total = 0;
	}
	$perc = ($cust_total / $all_total) * 100;
	
}

if ($report_type==2) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND EXTRACT(month FROM order_date) = '".$month_period."' AND cust_id = '".$cust_id."' AND sub_category = 'Tiles'  GROUP BY EXTRACT(month FROM order_date) ";
	$cust_total_result = mySQL_query($cust_total_query);
	$cust_total_num = mysql_num_rows($cust_total_result);
	if ($cust_total_num>0) {
	$cust_total = mysql_result($cust_total_result, 0, "total_quantity");
	}
	else {
    $cust_total = 0;
	}
	$perc = ($cust_total / $all_total) * 100;
}

if ($report_type==3) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND EXTRACT(month FROM order_date) = '".$month_period."' AND cust_id = '".$cust_id."' AND sub_category = 'Accessories'  GROUP BY EXTRACT(month FROM order_date) ";
	$cust_total_result = mySQL_query($cust_total_query);
	$cust_total_num = mysql_num_rows($cust_total_result);
	if ($cust_total_num>0) {
	$cust_total = mysql_result($cust_total_result, 0, "total_quantity");
	}
	else {
    $cust_total = 0;
	}
	$perc = ($cust_total / $all_total) * 100;
}


	$table_name2 = $_SESSION['db_prefix']."_customer";
	$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
	$customer_result = mySQL_query($customer_query);
	$customer_num = mysql_num_rows($customer_result);	
	$account_type = mysql_result($customer_result, 0, "account_type");
		if ($account_type=="corporate") { 
				$cust_name = mysql_result($customer_result, 0, "comp_name"); 
			} 
		else {
				$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 			}
			
	$double_roman_quantity = mysql_result($double_roman_result, $i, "total_quantity");
	$slate_quantity = mysql_result($slate_result, $i, "total_quantity");
	$venetian_quantity = mysql_result($venetian_result, $i, "total_quantity");
	$accessories_quantity = mysql_result($accessories_result, $i, "total_quantity");
	
$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$cust_double_roman_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Double Roman' AND $table_name.cust_id = '".$cust_id."' AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_double_roman_result = mySQL_query($cust_double_roman_query);
$cust_double_roman_num = mysql_num_rows($cust_double_roman_result);
if ($cust_double_roman_num > 0) {
$cust_double_roman = mysql_result($cust_double_roman_result, 0, "total_quantity");
}
else {
$cust_double_roman = 0;
}
$cust_double_roman_perc = number_format(($cust_double_roman / $double_roman_quantity) * 100, 2, '.', ',');

$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$cust_slate_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Slate' AND $table_name.cust_id = '".$cust_id."'  AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_slate_result = mySQL_query($cust_slate_query);
$cust_slate_num = mysql_num_rows($cust_slate_result);
if ($cust_slate_num > 0) {
$cust_slate = mysql_result($cust_slate_result, 0, "total_quantity");
}
else {
$cust_slate = 0;
}
$cust_slate_perc = number_format(($cust_slate / $slate_quantity) * 100, 2, '.', ',');

$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$cust_venetian_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Venetian' AND $table_name.cust_id = '".$cust_id."'  AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_venetian_result = mySQL_query($cust_venetian_query);
$cust_venetian_num = mysql_num_rows($cust_venetian_result);
if ($cust_venetian_num > 0) {
$cust_venetian = mysql_result($cust_venetian_result, 0, "total_quantity");
}
else {
$cust_venetian = 0;
}
$cust_venetian_perc = number_format(($cust_venetian / $venetian_quantity) * 100, 2, '.', ',');

$table_name = $_SESSION['db_prefix']."_order_lines";
$cust_accessories_query = "SELECT SUM(quantity) AS total_quantity FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND sub_category = 'Accessories' AND $table_name.cust_id = '".$cust_id."'  AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_accessories_result = mySQL_query($cust_accessories_query);
$cust_accessories_num = mysql_num_rows($cust_accessories_result);
if ($cust_accessories_num > 0) {
$cust_accessories = mysql_result($cust_accessories_result, 0, "total_quantity");
}
else {
$cust_accessories = 0;
}
$cust_accessories_perc = number_format(($cust_accessories / $accessories_quantity) * 100, 2, '.', ',');
			
$customerTT_line1 = $cust_name.'<br>';	
$customerTT_line2 = '<table><tr><td width=100px; align=center;>&nbsp;</td><td width=50px; align=center;>DR</td><td width=50px; align=center;>SL</td><td width=50px; align=center;>VN</td><td width=50px; align=center;>Acc</td></tr><tr><td>Total Sales</td><td align=right;>'.$double_roman_quantity.'</td><td align=right;>'.$slate_quantity.'</td><td align=right;>'.$venetian_quantity.'</td><td align=right;>'.$accessories_quantity.'</td></tr><tr><td>Customer Sales</td><td align=right;>'.$cust_double_roman.'</td><td align=right;>'.$cust_slate.'</td><td align=right;>'.$cust_venetian.'</td><td align=right;>'.$cust_accessories.'</td></tr><tr><td>Customer %</td><td>'.$cust_double_roman_perc.'</td><td>'.$cust_slate_perc.'</td><td>'.$cust_venetian_perc.'</td><td>'.$cust_accessories_perc.'</td></tr></table>';
$customerTT = $customerTT_line1.$customerTT_line2;


	
	?>
    <td align="right"><?php echo $cust_total ?></td>
    <td align="right"><?php echo number_format($perc, 1, '.', ',') ?></td>
    <?php
	$j++;
	}
	?>
		
  </tr>
<?php
$i++;
}
?>
<tfoot>
<tr>
    <td>&nbsp;</td>
    <?php
	if ($report_type==1) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' ";
	$full_total_result = mySQL_query($full_total_query);
	$full_total = mysql_result($full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==2) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND sub_category = 'Tiles' ";
	$full_total_result = mySQL_query($full_total_query);
	$full_total = mysql_result($full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==3) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND sub_category = 'Accessories' ";
	$full_total_result = mySQL_query($full_total_query);
	$full_total = mysql_result($full_total_result, 0, "total_quantity");
	}
	?>
    <td align="right"><?php echo $full_total ?></td>
    <?php
	
	while ($m<$columns) {
		$cust_id = ${'column_cust_id'.$m};
	
	if ($report_type==1) {	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND cust_id = '".$cust_id."' ";
	$cust_full_total_result = mySQL_query($cust_full_total_query);
	$cust_full_total = mysql_result($cust_full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==2) {	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND cust_id = '".$cust_id."'  AND sub_category = 'Tiles' ";
	$cust_full_total_result = mySQL_query($cust_full_total_query);
	$cust_full_total = mysql_result($cust_full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==3) {	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND cust_id = '".$cust_id."'  AND sub_category = 'Accessories' ";
	$cust_full_total_result = mySQL_query($cust_full_total_query);
	$cust_full_total = mysql_result($cust_full_total_result, 0, "total_quantity");
	}
	
	$perc_full_total = ($cust_full_total / $full_total) * 100;
	?>
    <td align="right"><?php echo $cust_full_total ?></td>
    <td align="right"><?php echo number_format($perc_full_total, 1, '.', ',') ?></td>
    <?php
	$m++;
	$t++;
	}
	?>
  </tr>
</tfoot>
</table>
</div>
</div>
<?php
}
}
?>
</div>

<!--Page 4-->
<div class="print_standard_frame" style="margin-top:-50px;">
<?php 
if ($line_num>27) {

if ($line_num<=36) {
	  $lines=$line_num;
}
else {
	$lines=36;
}

	
while ($t<$lines) { 
?>
<div <?php if ($line_num>36) { ?> style="page-break-after:always; min-height:700px;" <?php } ?> >
<div style="float:left;"><img src="../images/radstone_medium_logo.png" width="144" height="54"></div>
<div style="float:left; margin-left:20px;"><label style="font-size:18px;"><b> <?php echo $report_name ?> </b></label><br>
<br>
Report date: <?php echo date("d-m-Y", strtotime($today)) ?>&nbsp;&nbsp;page 3</div> 

<div class="print_standard_frame" style="width:700px;">
Reporting on: <?php echo $report_cat ?> &nbsp;&nbsp;&nbsp;Report year: <?php echo $report_year ?><br><br>
<table  border="1" cellspacing="3" cellpadding="3" style="border-collapse:collapse;" >
<thead>
  <tr>
    <td align="center" width="50">Month</td>
    <td align="center" width="50">Total<br>Sales</td>
    
    <?php	
	//$columns = 0;
	
	while ($k<$lines) {
	
	 
	  		$cust_id = $_SESSION['cust_id'.$k];
						
				$table_name2 = $_SESSION['db_prefix']."_customer";
				$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
				$customer_result = mySQL_query($customer_query);
				$customer_num = mysql_num_rows($customer_result);	
				$account_type = mysql_result($customer_result, 0, "account_type");
				if ($account_type=="corporate") { 
					$cust_name = mysql_result($customer_result, 0, "comp_name"); 
				} 
				else {
					$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 				}
			
			${'column_cust_id'.$columns} = $cust_id;
			
			$columns = $columns + 1;
					  	
	?>
    <td align="center" width="60"><?php echo $cust_name ?></td>
    <td align="center" width="16">in %</td>
    <?php
	$k++;
	}
		
	
	
	?>
  </tr>
</thead>

<?php 
$i=0;
while ($i<$total_num) {
	$order_month =  mysql_result($total_result, $i, "order_date");
	$order_month = date("F", strtotime($order_month));
	$all_total = mysql_result($total_result, $i, "total_quantity");
	$month_no = date("m", strtotime($order_month));
?>	  
  <tr>
    <td><?php echo $order_month ?></td>
    <td align="right"><?php echo $all_total ?></td>
    <?php
	
	
	
	$j=27;
	while ($j<$columns) {
	
	$cust_id = $_SESSION['cust_id'.$j];	
	$month_period = $month_no;
	
	
if ($report_type==1) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND EXTRACT(month FROM order_date) = '".$month_period."' AND cust_id = '".$cust_id."'  GROUP BY EXTRACT(month FROM order_date) ";
	
	
	$cust_total_result = mySQL_query($cust_total_query);
	$cust_total_num = mysql_num_rows($cust_total_result);
	if ($cust_total_num>0) {
	$cust_total = mysql_result($cust_total_result, 0, "total_quantity");
	}
	else {
    $cust_total = 0;
	}
	$perc = ($cust_total / $all_total) * 100;
	
}

if ($report_type==2) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND EXTRACT(month FROM order_date) = '".$month_period."' AND cust_id = '".$cust_id."' AND sub_category = 'Tiles'  GROUP BY EXTRACT(month FROM order_date) ";
	$cust_total_result = mySQL_query($cust_total_query);
	$cust_total_num = mysql_num_rows($cust_total_result);
	if ($cust_total_num>0) {
	$cust_total = mysql_result($cust_total_result, 0, "total_quantity");
	}
	else {
    $cust_total = 0;
	}
	$perc = ($cust_total / $all_total) * 100;
}

if ($report_type==3) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND EXTRACT(month FROM order_date) = '".$month_period."' AND cust_id = '".$cust_id."' AND sub_category = 'Accessories'  GROUP BY EXTRACT(month FROM order_date) ";
	$cust_total_result = mySQL_query($cust_total_query);
	$cust_total_num = mysql_num_rows($cust_total_result);
	if ($cust_total_num>0) {
	$cust_total = mysql_result($cust_total_result, 0, "total_quantity");
	}
	else {
    $cust_total = 0;
	}
	$perc = ($cust_total / $all_total) * 100;
}


	$table_name2 = $_SESSION['db_prefix']."_customer";
	$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
	$customer_result = mySQL_query($customer_query);
	$customer_num = mysql_num_rows($customer_result);	
	$account_type = mysql_result($customer_result, 0, "account_type");
		if ($account_type=="corporate") { 
				$cust_name = mysql_result($customer_result, 0, "comp_name"); 
			} 
		else {
				$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 			}
			
	$double_roman_quantity = mysql_result($double_roman_result, $i, "total_quantity");
	$slate_quantity = mysql_result($slate_result, $i, "total_quantity");
	$venetian_quantity = mysql_result($venetian_result, $i, "total_quantity");
	$accessories_quantity = mysql_result($accessories_result, $i, "total_quantity");
	
$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$cust_double_roman_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Double Roman' AND $table_name.cust_id = '".$cust_id."' AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_double_roman_result = mySQL_query($cust_double_roman_query);
$cust_double_roman_num = mysql_num_rows($cust_double_roman_result);
if ($cust_double_roman_num > 0) {
$cust_double_roman = mysql_result($cust_double_roman_result, 0, "total_quantity");
}
else {
$cust_double_roman = 0;
}
$cust_double_roman_perc = number_format(($cust_double_roman / $double_roman_quantity) * 100, 2, '.', ',');

$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$cust_slate_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Slate' AND $table_name.cust_id = '".$cust_id."'  AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_slate_result = mySQL_query($cust_slate_query);
$cust_slate_num = mysql_num_rows($cust_slate_result);
if ($cust_slate_num > 0) {
$cust_slate = mysql_result($cust_slate_result, 0, "total_quantity");
}
else {
$cust_slate = 0;
}
$cust_slate_perc = number_format(($cust_slate / $slate_quantity) * 100, 2, '.', ',');

$table_name = $_SESSION['db_prefix']."_order_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$cust_venetian_query = "SELECT SUM($table_name.quantity) AS total_quantity FROM $table_name, $table_name2 WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND $table_name.store_item_id = $table_name2.store_item_id AND $table_name2.item_type = 'Venetian' AND $table_name.cust_id = '".$cust_id."'  AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_venetian_result = mySQL_query($cust_venetian_query);
$cust_venetian_num = mysql_num_rows($cust_venetian_result);
if ($cust_venetian_num > 0) {
$cust_venetian = mysql_result($cust_venetian_result, 0, "total_quantity");
}
else {
$cust_venetian = 0;
}
$cust_venetian_perc = number_format(($cust_venetian / $venetian_quantity) * 100, 2, '.', ',');

$table_name = $_SESSION['db_prefix']."_order_lines";
$cust_accessories_query = "SELECT SUM(quantity) AS total_quantity FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."'  AND sub_category = 'Accessories' AND $table_name.cust_id = '".$cust_id."'  AND EXTRACT(month FROM order_date) = '".$month_no."' ";
$cust_accessories_result = mySQL_query($cust_accessories_query);
$cust_accessories_num = mysql_num_rows($cust_accessories_result);
if ($cust_accessories_num > 0) {
$cust_accessories = mysql_result($cust_accessories_result, 0, "total_quantity");
}
else {
$cust_accessories = 0;
}
$cust_accessories_perc = number_format(($cust_accessories / $accessories_quantity) * 100, 2, '.', ',');
			
$customerTT_line1 = $cust_name.'<br>';	
$customerTT_line2 = '<table><tr><td width=100px; align=center;>&nbsp;</td><td width=50px; align=center;>DR</td><td width=50px; align=center;>SL</td><td width=50px; align=center;>VN</td><td width=50px; align=center;>Acc</td></tr><tr><td>Total Sales</td><td align=right;>'.$double_roman_quantity.'</td><td align=right;>'.$slate_quantity.'</td><td align=right;>'.$venetian_quantity.'</td><td align=right;>'.$accessories_quantity.'</td></tr><tr><td>Customer Sales</td><td align=right;>'.$cust_double_roman.'</td><td align=right;>'.$cust_slate.'</td><td align=right;>'.$cust_venetian.'</td><td align=right;>'.$cust_accessories.'</td></tr><tr><td>Customer %</td><td>'.$cust_double_roman_perc.'</td><td>'.$cust_slate_perc.'</td><td>'.$cust_venetian_perc.'</td><td>'.$cust_accessories_perc.'</td></tr></table>';
$customerTT = $customerTT_line1.$customerTT_line2;


	
	?>
    <td align="right"><?php echo $cust_total ?></td>
    <td align="right"><?php echo number_format($perc, 1, '.', ',') ?></td>
    <?php
	$j++;
	}
	?>
		
  </tr>
<?php
$i++;
}
?>
<tfoot>
<tr>
    <td>&nbsp;</td>
    <?php
	if ($report_type==1) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' ";
	$full_total_result = mySQL_query($full_total_query);
	$full_total = mysql_result($full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==2) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND sub_category = 'Tiles' ";
	$full_total_result = mySQL_query($full_total_query);
	$full_total = mysql_result($full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==3) {
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND sub_category = 'Accessories' ";
	$full_total_result = mySQL_query($full_total_query);
	$full_total = mysql_result($full_total_result, 0, "total_quantity");
	}
	?>
    <td align="right"><?php echo $full_total ?></td>
    <?php
	
	while ($m<$columns) {
		$cust_id = ${'column_cust_id'.$m};
	
	if ($report_type==1) {	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND cust_id = '".$cust_id."' ";
	$cust_full_total_result = mySQL_query($cust_full_total_query);
	$cust_full_total = mysql_result($cust_full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==2) {	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND cust_id = '".$cust_id."'  AND sub_category = 'Tiles' ";
	$cust_full_total_result = mySQL_query($cust_full_total_query);
	$cust_full_total = mysql_result($cust_full_total_result, 0, "total_quantity");
	}
	
	if ($report_type==3) {	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$cust_full_total_query = "SELECT SUM(quantity) AS total_quantity  FROM $table_name WHERE EXTRACT(YEAR FROM order_date) = '".$report_year."' AND cust_id = '".$cust_id."'  AND sub_category = 'Accessories' ";
	$cust_full_total_result = mySQL_query($cust_full_total_query);
	$cust_full_total = mysql_result($cust_full_total_result, 0, "total_quantity");
	}
	
	$perc_full_total = ($cust_full_total / $full_total) * 100;
	?>
    <td align="right"><?php echo $cust_full_total ?></td>
    <td align="right"><?php echo number_format($perc_full_total, 1, '.', ',') ?></td>
    <?php
	$m++;
	$t++;
	}
	?>
  </tr>
</tfoot>
</table>
</div>
</div>
<?php
}
}
?>
</div>

</div>
</div> 
</div>
</body>


