<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php 
include ("../navigation/header.php");

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

if ($permission_num==1) {

include ("../navigation/sales_sidebar.php");
	
		
?>
<!--style sheets-->
<link href="../css/main.css" rel="stylesheet" type="text/css">

<!--jscript-->
<script language="javascript" src="../jscript/calendar.js"></script>
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />

<?php
include('../include/period_select_date.php');
$sales_rep= $_GET['sales_rep'];

$table_name1 = $_SESSION['db_prefix']."_quotations";
$quote_query = "SELECT quote_no, quotation_date, quote_type, cust_id, quote_value, branch, quote_discount, transport, quote_vat FROM $table_name1 WHERE seller = '".$sales_rep."' AND quotation_date BETWEEN '".$start."' AND '".date('Y-m-d', strtotime(date("Y-m-d", strtotime($end))." +1 day"))."'  ";
$quote_result = mySQL_query($quote_query);
$quote_num = mysql_num_rows($quote_result);	


?>

 
       
</head>

<body>

<div class="main_frame">
 
<div class="page_head_frame">
    <label class="label_page_head">Customer sales reports</label>
</div>

<div class="page_middle_frame">

<script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table').dataTable( {
					"sScrollY": "420px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "desc" ]],
					"aoColumns": [
								{"iDataSort": 1},
								{"bVisible": false},
								{"bSortable": true},
								{"bSortable": true},
								{"iDataSort": 5},
								{"bVisible": false},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"iDataSort": 10},
								{"bVisible": false},
								{"bSortable": true},
								{"bSortable": true}
								 ]
				} );
			} );
		</script>
       
        
 
         <?php
$page='sales_rep_sales_list.php';
$reference_name='';
$reference='';
$reference_name_2='sales_rep';
$reference_2= $sales_rep;
include('../include/period_select_block_2.php');
?>       </form>
       
        
        
    <div style="margin-top:120px;">

          <label class="label_std"><font size="+2">Sales by <?php echo $sales_rep ?></font></label> 
          </div>
          <table width="445" id="scroll_table" class="data_display" >
            <thead>
            <tr>
              <th >Quote no</th>
              <th >&nbsp;</th>
              <th >Type</th>
              <th >Branch</th>
              <th >Quote date</th>
              <th >&nbsp;</th>
              <th >Customer</th>
              <th >Quote<br>value</th>
              <th >Order<br>success</th>
              <th >Order<br>date</th>
              <th >&nbsp;</th>
              <th >Order<br>
              value</th>
              
              <th>Sales rep order</th>
              </tr>
            </thead>
            <?php 
$i=0;

$a9_total=0;
$a5_total=0;
$price=0;

while ($i<$quote_num) {
	$a1 = mysql_result($quote_result, $i, "quote_no");
	$a2 = mysql_result($quote_result, $i, "branch");
	$a3 = mysql_result($quote_result, $i, "quotation_date");
	$a4 = mysql_result($quote_result, $i, "cust_id");
	$a5 = mysql_result($quote_result, $i, "quote_value");
	$a6 = mysql_result($quote_result, $i, "quote_type");
	$quote_discount = mysql_result($quote_result, $i, "quote_discount");
	$transport = mysql_result($quote_result, $i, "transport");
	$quote_vat = mysql_result($quote_result, $i, "quote_vat");
	
	if ($a6=='tiles') {$a6a='Tiles & Acc.';}
	if ($a6=='trusses') {$a6a='Installation';}
	
			$table_name = $_SESSION['db_prefix']."_customer";
			$customer_query = "SELECT status, account_type, comp_name, name_2, title, bag_box, box_no, postal_place, phone, cell_1  FROM $table_name WHERE cust_id = '".$a4."' ";
			$customer_result = mySQL_query($customer_query);
			$customer_num = mysql_num_rows($customer_result);
			if ($customer_num!=0) {
			$type = mysql_result($customer_result, 0, "account_type");
			if ($type=="corporate") { 
				$a4a = mysql_result($customer_result, 0, "comp_name"); 
			} 
			else {
				$a4a = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 			}	
			$a4b = mysql_result($customer_result, 0, "bag_box");
			$a4c = mysql_result($customer_result, 0, "box_no");
			$a4d = mysql_result($customer_result, 0, "postal_place");
			$a4e = mysql_result($customer_result, 0, "phone");
			$a4f = mysql_result($customer_result, 0, "cell_1");
			$a4g = mysql_result($customer_result, 0, "status");
			$a4h="";
			}
			else {
			$a4a='';
			}
		

if ($a6=='tiles') {
		$table_name = $_SESSION['db_prefix']."_tile_orders";
		$tile_order_query = "SELECT seller, order_date, value FROM $table_name WHERE order_no = '".$a1."'  ";
		$tile_order_result = mySQL_query($tile_order_query);
		$tile_order_num = mysql_num_rows($tile_order_result);
		if ($tile_order_num>0) {
		$a7 = 'yes';
		$a8 = mysql_result($tile_order_result, 0, "order_date");
	    $a9 = mysql_result($tile_order_result, 0, "value");
		$a10 = mysql_result($tile_order_result, 0, "seller");
		}
		else {
		$a7 = '';
		$a8 = '';
	    $a9 = '';
		$a10 = '';
		}
}

if ($a6=='trusses') {
		$table_name = $_SESSION['db_prefix']."_install_orders";
		$install_order_query = "SELECT seller, order_date, order_value FROM $table_name WHERE order_no = '".$a1."'  ";
		$install_order_result = mySQL_query($install_order_query);
		$install_order_num = mysql_num_rows($install_order_result);
		if ($install_order_num>0) {
		$a7 = 'yes';
		$a8 = mysql_result($install_order_result, 0, "order_date");
	    $a9 = mysql_result($install_order_result, 0, "order_value");
		$a10 = mysql_result($install_order_result, 0, "seller");
		}
		else {
		$a7 = '';
		$a8 = '';
	    $a9 = '';
		$a10 = '';
		}
}

	if ($a6=='tiles') {
	$table_name = $_SESSION['db_prefix']."_quotation_lines";
	$line_query = "SELECT  store_item_id, quantity, price   FROM $table_name WHERE quote_no = '".$a1."' ";
	$line_result = mySQL_query($line_query);
	$line_num = mysql_num_rows($line_result);
	
	
	$l=0;
	$total=0;
	$line_text="";
	$products=mysql_result($line_result, 0, "store_item_id");
		
	while($l<$line_num) {
		$store_item_id = mysql_result($line_result, $l, "store_item_id");
		$quantity = mysql_result($line_result, $l, "quantity");
		$price = mysql_result($line_result, $l, "price");
		
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		$store_item_name = mysql_result($name_result, 0, "item_name");
		$sub_total =  $quantity * $price;
		$total = $total + $sub_total;
			  
	   	$line_text = $line_text."<tr><td>".$store_item_name."</td><td>".$quantity."</td><td align=right>".number_format($price, 2, '.', ',')."</td><td align=right>".number_format($sub_total, 2, '.', ',')."</td></tr>";
	   $l++;
	}
	$discount = $total * ($quote_discount/100);
	$sub_total = $total - $discount;
	$sub_total3 = $sub_total + $transport;
	$vat = $sub_total3 * ($quote_vat/100); 
	$all_total = $sub_total3 + $vat;
	$quoteTT ="<table id=table_tool_tip width=450 ><tr><td>Item</td><td>Quantity</td><td align=center>Price</td><td align=center>Total</td></tr>".$line_text."<tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>Sub total:</td><td align=right>".number_format($total, 2, '.', ',')."</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>Discount: ".$quote_discount."%</td><td align=right>".number_format($discount, 2, '.', ',')."</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>Sub total:</td><td align=right>".number_format($sub_total, 2, '.', ',')."</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>Transport: </td><td align=right>".number_format($transport, 2, '.', ',')."</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>Sub total: </td><td align=right>".number_format($sub_total3, 2, '.', ',')."</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>VAT: ".$quote_vat."%</td><td align=right>".number_format($vat, 2, '.', ',')."</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>Total:</td><td align=right>".number_format($all_total, 2, '.', ',')."</td></tr></table><br><b>Click for details</b>";
	}

	
if ($a6=='trusses') {
	$table_name = $_SESSION['db_prefix']."_quotation_lines";
	$line_query = "SELECT  truss_item, quantity, price FROM $table_name WHERE quote_no = '".$a1."' ";
	$line_result = mySQL_query($line_query);
	$line_num = mysql_num_rows($line_result);
	
	$l=0;
	$total=0;
	$line_text="";
	
		
	while($l<$line_num) {
	   $truss_item=mysql_result($line_result, $l, "truss_item");	
	   $quantity=mysql_result($line_result, $l, "quantity");
	   $price=mysql_result($line_result, $l, "price");
	   
	  	   
	   $sub_total =  $quantity * $price;
	   $total = $total + $sub_total;
	     
	   
	   $line_text = $line_text."<tr><td>".$truss_item."</td><td align=right>".$quantity."</td><td align=right>".number_format($price, 2, '.', ',')."</td><td align=right>".number_format($sub_total, 2, '.', ',')."</td></tr>";
	   $l++;
	}
	$discount = $total * ($quote_discount/100);
	$sub_total = $total - $discount;
	$sub_total2 = $sub_total + $transport;
	$vat = $sub_total2 * ($quote_vat/100); 
	$all_total = $sub_total2 + $vat;
	$quoteTT ="<table id=table_tool_tip width=450 ><tr><td>Item</td><td>Quantity</td><td align=center>Price</td><td align=center>Total</td></tr>".$line_text."<tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>Sub total:</td><td align=right>".number_format($total, 2, '.', ',')."</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>Discount: ".$quote_discount."%</td><td align=right>".number_format($discount, 2, '.', ',')."</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>Sub total:</td><td align=right>".number_format($sub_total, 2, '.', ',')."</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>Transport:</td><td align=right>".number_format($transport, 2, '.', ',')."</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>Sub total:</td><td align=right>".number_format($sub_total2, 2, '.', ',')."</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>VAT: ".$quote_vat."%</td><td align=right>".number_format($vat, 2, '.', ',')."</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td align=right>Total:</td><td align=right>".number_format($all_total, 2, '.', ',')."</td></tr></table><br><b>Click for details</b>";
	}
	
$customerTT ="<table id=table_tool_tip width=250 ><tr><td  width=75>Company:</td><td width=175>".$a4a."</td></tr><tr><td>Address:</td><td>".$a4b."&nbsp;".$a4c."<br>".$a4d."</td></tr><tr><td>Phone:</td><td>".$a4e."</td></tr><tr><td>Contact:</td><td>".$a4f."</td></tr></table><br><b>Click for details</b>";	
	?>
            <tr>
              <td align="left"  style="padding-left:0px;"><a href="tile_order_display.php?order_no=<?php echo $a1 ?>" onMouseOver="ddrivetip('<?php echo $quoteTT ?>', 450)"; onMouseOut="hideddrivetip()"><?php echo $a1 ?></a></td>
              <td align="left"  style="padding-left:0px;"><?php echo $a1 ?></td>
              <td align="left"><?php echo $a6a ?></td>
              <td align="left"><?php echo $a2 ?></td>
              <td align="left"><?php echo date("d-m-Y", strtotime($a3)) ?></td>
              <td align="left"><?php echo $a3 ?></td>
              <td align="left"><a href="../store/store_item_display.php?store_item_id=<?php echo $a4 ?>" onMouseOver="ddrivetip('<?php echo $customerTT ?>', 200)"; onMouseOut="hideddrivetip()"><?php echo $a4a ?></a></td>
              <td align="right"><?php echo $a5 ?></td>
              <td align="center"><?php echo $a7 ?></td>
              <td align="left"><?php if($a8!='') { echo date("d-m-Y", strtotime($a8)); } ?></td>
              <td align="left"><?php echo $a8 ?></td>
              <td width="80" align="right"><?php echo $a9 ?></td>
              
              <td align="left"><?php echo $a10 ?></td>
            </tr>
            <?php
			$a5_total = $a5_total + $a5;
			$a9_total = $a9_total + $a9;
  $i++;
}
?>
            <tfoot>
              <tr>
                <td colspan="7" align="right" valign="bottom">Total:</td>
                <td align="right" valign="bottom"><?php echo number_format($a5_total, 2, '.', ',') ?></td>
                <td align="right" valign="bottom">&nbsp;</td>
                <td align="right" valign="bottom">&nbsp;</td>
                <td align="right" valign="bottom">&nbsp;</td>
                <td align="right" valign="bottom"><?php echo number_format($a9_total, 2, '.', ',') ?></td>
                <td >&nbsp;</td>
              </tr>
            </tfoot>
          </table>

</div>
</div> 

<?php include ('../navigation/side_bar.php'); ?>
</body>
<?php } else { ?>
<script> needToConfirm = false; </script>
<?php 
    $_SESSION['denied_return'] = "sales/sales_main.php?date=&branch=";	?>
<body onLoad="accessdenied(); return false">
</body>
<?php }
?>

