<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
$_SESSION['current_page']  = "sales";
include ("../navigation/header.php");
$page_name = "Sales-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/sales_sidebar.php");

require_once("../include/tc_calendar.php");
	    
	$date = mysql_real_escape_string($_GET['date']);
		
	if ($date==1) {
	$start = mysql_real_escape_string($_GET['date1']);
    $end = mysql_real_escape_string($_GET['date2']);
	}
	else {
	
	$de=31;
	if (date("m")==2) {$de=28;}
	if ( (date("m")==4) OR (date("m")==6) OR (date("m")==9) OR (date("m")==11) ) {$de=30;}
	
	$start = date("Y-m-01");
	$end = date("Y-m-".$de);
	}
			
	if ($date!=1) {
	   $ds = "1"; }
	   else {
		$ds = date("d", strtotime($start) );
	   }
	$ms = date("m", strtotime($start) );
	$ys = date("Y", strtotime($start) );
	
	
	$de = date("d", strtotime($end) );
	$me = date("m", strtotime($end) );
	$ye = date("Y", strtotime($end) );	

	
$table_name2 = $_SESSION['db_prefix']."_quotations";
$quotation_query = "SELECT quote_no, status, cust_id, type, quotation_date, required_date, company_contact, contact_phone, quote_type, project_no, seller, designer, value FROM $table_name2 WHERE quotation_date >= '".$start."' AND quotation_date <= '".$end."' ORDER BY quotation_date DESC ";

$quotation_result = mySQL_query($quotation_query);
$quotation_num = mysql_num_rows($quotation_result);	

?>
<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />

<!--jscript-->
<script language="javascript" src="../jscript/calendar.js"></script>
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> 
<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": "500px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 2, "desc" ]],
					"aoColumns": [
								{"bSortable": false},
                             	{"bSortable": false},
								{"bSortable": true},
								{"iDataSort": 9},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"iDataSort": 10},
								{"bSortable": true},
								{"bVisible": false},
								{"bVisible": false}
								 ]
				} );
			} );
		</script>

<body>


<div class="main_frame">
  <table id="tableMain" > <!--begin of tableMain-->
    <tr>
      <td width="830" height="74" valign="top" ><div class="page_head_frame">
        
        <table width="809" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="385"><label class="label_page_head">Quotations</label></td>
            <td width="253">&nbsp;</td>
            <td width="171" align="right">&nbsp;</td>
          </tr>
        </table>
      </div><br />
<br /><br />
<br />

<form action="quotations_list.php" method="get" name="dateform" class="today" id="dateform">
   <div class="page_middle_frame">
    <table width="680"  id='table_calendar'>
      <tr>
        <td height="20" colspan="3" align="left" valign="middle"><label class="label_std">Select time period to display</label></td>
        <td valign="middle">&nbsp;</td>
        <td colspan="2" valign="middle"><input name="date" type="hidden" id="date" value="1" /></td>
        </tr>
      <tr>
        <td width="12" height="25" valign="middle">&nbsp;&nbsp; </td>
        <td width="62" valign="middle"><label class="label_std">  from:</label> </td>
        <td width="222" valign="middle"><?php
		$myCalendar = new tc_calendar("date1", true);
		$myCalendar->setIcon("../images/iconCalendar.gif");
		$myCalendar->setDate($ds, $ms, $ys);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2010, 2030);
	 	$myCalendar->writeScript();?></td>
        <td width="20" valign="middle"><label class="label_std">to:</label></td>
        <td width="257" valign="middle"><?php
		$myCalendar = new tc_calendar("date2", true);
		$myCalendar->setIcon("../images/iconCalendar.gif");
		$myCalendar->setDate($de, $me, $ye);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2010, 2030);
		$myCalendar->writeScript();?></td>
        <td width="79" valign="middle"><input name="button5" type="submit" class="linkbutton"  id="button5" value="Go" /></td>
        </tr>
    </table>
    </div>
  </form>
      
      
        </td> <!--center of tableMain-->
</tr>
<tr>
<td> <!--center of tableMain-->
<div class="page_middle_frame">
<div align="left">
  <label class="label_end"><img src="../images/round_green.png" width="8" height="8" />: succes&nbsp;&nbsp;<img src="../images/round_orange.png" width="8" height="8" />: pending&nbsp;&nbsp;<img src="../images/round_red.png" width="8" height="8" />:unsuccesful</label>
</div>
<table width="689" id="scroll_table" class="data_display"  >
  <thead>
  <tr>
    <th align="center" >&nbsp;</th>
    <th style="border-right-style:hidden;">&nbsp;</th>
    <th >Number</th>
    <th >Date</th>
    <th >Project no</th>
    <th >Name</th>
    <th >Contact</th>
    <th >Value</th>
    <th >Sales rep</th>
    <th >&nbsp;</th>
    <th >&nbsp;</th>
  </tr>
  </thead>
<?php 
$i=0;
$a7tot=0;
while ($i<$quotation_num) {
	$a1 = mysql_result($quotation_result, $i, "quote_no");
	
	
	     $a2 = mysql_result($quotation_result, $i, "cust_id");
	     $table_name3 = $_SESSION['db_prefix']."_customer";
         $customer_query = "SELECT comp_name, status, bag_box, box_no, postal_place, phone, contact_1 FROM $table_name3 WHERE cust_id = '".$a2."' ";
         $customer_result = mySQL_query($customer_query);
		 $customer_num = mysql_num_rows($customer_result);
		 if ($customer_num!=0) {
         $a2a = mysql_result($customer_result, 0, "comp_name");
		 $a2b = mysql_result($customer_result, 0, "bag_box");
		 $a2c = mysql_result($customer_result, 0, "box_no");
		 $a2d = mysql_result($customer_result, 0, "postal_place");
		 $a2e = mysql_result($customer_result, 0, "phone");
		 $a2f = mysql_result($customer_result, 0, "contact_1");
		 $a3 = mysql_result($customer_result, 0, "status");
		 }
		 
	
	
	$a5 = mysql_result($quotation_result, $i, "quotation_date");
	$a6 = mysql_result($quotation_result, $i, "required_date");
	$a7 = mysql_result($quotation_result, $i, "value");
	$a8 = mysql_result($quotation_result, $i, "seller");
	$a9 = mysql_result($quotation_result, $i, "company_contact");
	$a10 = mysql_result($quotation_result, $i, "contact_phone");
	$a11 = mysql_result($quotation_result, $i, "status");
	$a12 = mysql_result($quotation_result, $i, "project_no");
	$a14 = mysql_result($quotation_result, $i, "quote_type");
	
	$table_name = $_SESSION['db_prefix']."_quotation_lines";
	$line_query = "SELECT  quote_item_text, quote_item_price   FROM $table_name WHERE quote_number = '".$a1."' ";
	$line_result = mySQL_query($line_query);
	$line_num = mysql_num_rows($line_result);
	
	$l=0;
	//$a7=0;
	$line_text="";
	//$products=mysql_result($line_result, 0, "quote_item_text");
		
	while($l<$line_num) {
	   $price=mysql_result($line_result, $l, "quote_item_price");
	  //$a7=$price+$a7;	
	  
	   $line = mysql_result($line_result, $l, "quote_item_text");
	   $line_text = $line_text."&nbsp;-&nbsp;".$line." @ P ".number_format($price, 2, '.', ',')."<br>";
	   $l++;
	   
	}
	
	$a7tot = $a7tot + $a7;
	
	$table_name = $_SESSION['db_prefix']."_quotation_notes";
	$notes_query = "SELECT  note_date, note_text, seller   FROM $table_name WHERE quote_number = '".$a1."' ORDER BY note_date DESC";
	$notes_result = mySQL_query($notes_query);
	$notes_num = mysql_num_rows($notes_result);
	$notes_text='';
	$m=0;
	while ($m<$notes_num) {
		$notes1 = date("d-m-Y", strtotime(mysql_result($notes_result, $m, "note_date")))." ".mysql_result($notes_result, $m, "seller").": ";
		$notes2 = mysql_result($notes_result, $m, "note_text");
		
		$notes_text=$notes_text.'<tr><td valign=top>'.$notes1.'</td><td valign=top>'.$notes2.'</td><tr>';
		$m++;
	}
	
	
	$quoteTT ="<table id=table_tool_tip width=450 ><tr><td  width=75>Quote no:</td><td width=175>".$a1."</td></tr></tr><tr><td valign=top>Items quoted:</td><td valign=top>".$line_text."</td></tr><tr><td valign=top>Total quote value:</td><td valign=top>P".number_format($a7, 2, '.', ',')."</td></tr><tr><td>Notes:</td><td>".$notes_text."</td></tr></table><br><b>Click for details</b>";
	//echo '<br>'.$quoteTT;
	$productsTT ="<table id=table_tool_tip width=250 ><tr><td  width=75>Products quoted:</td><td width=175>".$line_text."</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>".$a2a."</td></tr><tr><td>Address:</td><td>".$a2b."&nbsp;".$a2c."<br>".$a2d."</td></tr><tr><td>Phone:</td><td>".$a2e."</td></tr><tr><td>Contact:</td><td>".$a2f."</td></tr></table><br><b>Click for details</b>";
	?>
  
  <tr>
    <td width="23" align="center"><?php if ($a11=="quoted") { ?>
      <img src="../images/round_orange.png" width="8" height="8"  />
      <?php } ?>
      <?php if ($a11=="accepted") { ?>
      <img src="../images/round_green.png" width="8" height="8"  />
      <?php } ?>
      <?php if ($a11=="unsuccessful") { ?>
      <img src="../images/round_red.png" width="8" height="8"   />
      <?php } ?></td>
    <td width="23" align="right" style="border-right-style:hidden; padding-right:0px;"><?php if ($a14=='Cash sale') {echo 'C';}?>Q</td>
    <td width="66" style="padding-left:0px;" align="left"><a href="quotation.php?quote_no=<?php echo $a1 ?> " onMouseover="ddrivetip('<?php echo $quoteTT ?>', 450)"; onMouseout="hideddrivetip()"><?php echo $a1 ?></a></td>
    <td width="66" align="left"><?php echo date("d-m-Y", strtotime($a5))?></td>
    <td width="97" align="left"><?php echo $a12 ?>&nbsp;</td>
    <td width="115" align="left"><a href="customer_details.php?cust_id=<?php echo $a2 ?>" onmouseover="ddrivetip('<?php echo $customerTT?>', 250)"; onmouseout="hideddrivetip()"><?php echo $a2a ?></a></td>
    <td width="92" align="left"><?php echo $a9."<br>".$a10 ?>&nbsp;</td>
    <td width="72" align="right"><?php echo number_format($a7, 2, '.', ',') ?></td>
    <td width="68" align="left"><?php echo $a8 ?></td>
    <td width="23" align="left"><?php echo $a5 ?></td>
    <td width="23" align="left"><?php echo $a7 ?></td>
  </tr>
   <?php
  $i++;
}
?>
  <tfoot>
  <tr style="border-top-style:double;">
    <td colspan="7" align="right">Total:</td>
    <td align="right"><?php echo number_format($a7tot, 2, '.', ',') ?></td>
    <td colspan="3" align="right">&nbsp;</td>
    </tr>
  </tfoot>
 
</table>
<div align="left"><label class="label_end">end of list</label></div>
</div>



</td>
</tr>
</table>  <!--end of tableMain-->
  
</div>

</body>
<?php } else {
$_SESSION['denied_return'] = $_SESSION['location'];	
include ("../navigation/sales_sidebar.php");?>
	<body onload="accessdenied(); return false">
	</body>
<?php }
?>
<?php include ('../navigation/side_bar.php'); ?>
</html>