<!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
include ("../navigation/header.php");
$page_name = "Sales-costing";
include ("../navigation/access.php");
$today = date("Y-m-d");

if ($permission_num==1) {

	include ("../navigation/sales_sidebar.php");
    include ("../include/date_set.php"); 
	 

$table_name = $_SESSION['db_prefix']."_cash_job_lines_done";
$table_name2 = $_SESSION['db_prefix']."_quotations";
$display_query = "
SELECT 
$table_name.quote_no, 
SUM($table_name.quantity*$table_name.price) AS summed_price,
SUM($table_name.authorised*$table_name.auth_price) AS summed_auth_price,
SUM($table_name.delivered*$table_name.price_2) AS summed_price_2
FROM $table_name, $table_name2
WHERE $table_name2.quotation_date >= '".$start."' AND $table_name2.quotation_date <= '".$end."'
AND $table_name.quote_no=$table_name2.quote_no
GROUP BY quote_no 
";
$display_result = execsql($display_query);
$display_num = mysql_num_rows($display_result);

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

</head>

<body>
<div class="main_frame" style="height:auto;">
  <div class="page_head_frame">
    <label class="label_page_head">Cash sale  costing</label>
  </div>
  <br />
  <div class="page_middle_frame">
  <form action="../sales/costing_cash_sale_report_list.php" method="get" name="dateform" class="today" id="dateform">
    <table width="645" border="0" align="left" cellpadding="0" cellspacing="0" id="table_calendar">
      <tr>
        <td width="39"><label class="label_std">from:</label></td>
        <td width="244"><?php
		$myCalendar = new tc_calendar("date1", true);
		$myCalendar->setIcon("../images/iconCalendar.gif");
		$myCalendar->setDate($ds, $ms, $ys);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2014, 2090);
	 	$myCalendar->writeScript();?></td>
        <td width="30"><label class="label_std">to:</label></td>
        <td width="280"><?php
		$myCalendar = new tc_calendar("date2", true);
		$myCalendar->setIcon("../images/iconCalendar.gif");
		$myCalendar->setDate($de, $me, $ye);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2014, 2090);
		$myCalendar->writeScript();?>
          <input name="date" type="hidden" id="date" value="1" /></td>
        <td width="52"><label>
            <input name="button5" type="submit" class="linkbutton"  id="button5" value="Select" />
          </label></td>
      </tr>
    </table>
    &nbsp;&nbsp;&nbsp;&nbsp;
  </form>
  </div>
  <br />
</head>

<body>

<div class="page_middle_frame" style="width:710px;" >
<?php if ($display_num!=0) { ?>
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />       
<!--jscript-->        
<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" language="javascript" src="../jscript/formatted-numbers.js"></script>



<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table').dataTable( {
					"sScrollY": "500px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "desc" ]],
					       
						
     				
					"aoColumns": [
								{"iDataSort": 1},
                              	{"bVisible": false},
								{"iDataSort": 3},
                              	{"bVisible": false},
								{"bSortable": true},
								{"iDataSort": 6},
                              	{"bVisible": false},
                             	{"iDataSort": 8},
                              	{"bVisible": false},
								{"iDataSort": 11},
                              	{"bVisible": false},
								{"iDataSort": 13},
                              	{"bVisible": false},
								{"bSortable": true},
								{"bSortable": true}
								 ]
								 
							 
						 
				} );
			} );
		</script>
  <table width="710" id="scroll_table" class="data_display">
  <thead>
    <tr>
      <th width="48" align="center">Cash sale no</th>
      <th width="18" align="center">&nbsp;</th>
      <th width="33" align="center">Date</th>
      <th width="28" align="center">&nbsp;</th>
      <th width="134" align="center">Customer</th>
      <th width="50" align="center">Quote value</th>
      <th width="10" align="center">&nbsp;</th>
      <th width="48" align="center">Quote<br />
        cost</th>
      <th width="13" align="center">&nbsp;</th>
      <th width="45" align="center">Sales value</th>
      <th width="9" align="center">&nbsp;</th>
      <th width="51" align="center">Result</th>
      <th width="9" align="center">&nbsp;</th>
      <th width="51" align="center">in %</th>
      <th width="79" align="center">Sales rep</th>
    </tr>
    </thead>
    
<?php

$job_value_total=0;
$cost_value_total=0;
$auth_value_total=0;
$sales_value_total=0;
$result_value_total=0; 

$z=0;
while ($z<$display_num) {

$job_no = mysql_result($display_result, $z, "quote_no");
$cost_value = mysql_result($display_result, $z, "summed_price");
$auth_value = $cost_value;//mysql_result($display_result, $z, "summed_auth_price");
//$sales_value = mysql_result($display_result, $z, "summed_price_2");
$delivery_date = '';	

   
$table_name = $_SESSION['db_prefix']."_quotations";
$quote_query = "SELECT  seller FROM $table_name WHERE quote_no = '".$job_no."' ";
$quote_result = mySQL_query($quote_query);
$seller = mysql_result($quote_result, 0, "seller");

$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
$single_job_query = "SELECT * FROM $table_name WHERE job_no = '".$job_no."' ";
$single_job_result = mySQL_query($single_job_query);
$cust_id = mysql_result($single_job_result, 0, "cust_id");
$job_value = mysql_result($single_job_result, 0, "value");
$sales_value = mysql_result($single_job_result, 0, "value");
$sales_rep = mysql_result($single_job_result, 0, "sales_rep");
$date = mysql_result($single_job_result, 0, "date");

$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 = '".$cust_id."' ";
		 			$customer_result = mySQL_query($customer_query);
         			$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");
										
					$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
					$cust_id_query = "SELECT date, cust_id, project_no, project_place, project_description FROM $table_name WHERE job_no = '".$job_no."'  ";
					$cust_id_result = mySQL_query($cust_id_query);
					
					$b1 = mysql_result($cust_id_result, 0, "date");
					$b9 = mysql_result($cust_id_result, 0, "project_no");
					$b10 = mysql_result($cust_id_result, 0, "project_place");
					$b11 = mysql_result($cust_id_result, 0, "project_description");
					$b12 = '';


$job_value_total = $job_value_total + $job_value;
$cost_value_total = $cost_value_total + $cost_value;
$auth_value_total = $auth_value_total + $auth_value;
$sales_value_total = $sales_value_total + $sales_value;

					
$result_value = $sales_value - $auth_value;
$result_value_total = $result_value_total + $result_value;

//$prognose_perc_average = (($job_value_total-$quoted_value_total)/$job_value_total)*100;
if ($result_value>0) {
$result_perc_average = ($result_value/$sales_value) * 100;
}
else {
	$result_perc_average=0;
}

if ($result_value_total>0) {
$result_total_perc_average = ($result_value_total/$sales_value_total) * 100;
}
else {
	$result_total_perc_average=0;
}
					
										
		$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>";
		
		$jobTT ="<table id=table_tool_tip width=300 ><tr><td  width=100>Job no:</td><td width=175>".$job_no."</td></tr><tr><td valign=top>Project no /place:</td><td>".$b9."&nbsp;".$b10."</td></tr><tr><td>Project description:</td><td>".$b11."</td></tr><tr><td>Designer:</td><td>".$b12."</td></tr></table><br><b>Click for details</b>";		
?>

<tr>
      <td><a href="../sales/costing_cash_sale.php?job_no=<?php echo $job_no ?>&amp;load=0 " onmouseover="ddrivetip('<?php echo$jobTT?>', 300)"; onmouseout="hideddrivetip()"><?php echo 'J'.$job_no?></a></td>
      <td><a href="../sales/costing_cash_sale.php?job_no=<?php echo $job_no ?>&amp;load=0 " onmouseover="ddrivetip('<?php echo$jobTT?>', 300)"; onmouseout="hideddrivetip()"><?php echo $job_no ?></a></td>
      <td><?php echo date("d-m-Y", strtotime($date))?></td>
      <td><?php echo $date ?></td>
      <td><a href="customer_details.php?cust_id=<?php echo $cust_id ?>" onmouseover="ddrivetip('<?php echo $customerTT?>', 300)"; onmouseout="hideddrivetip()"><?php echo $a2a?></a>&nbsp;</td>
      <td align="right"><?php echo number_format($job_value, 2, '.', ',') ?></td>
      <td align="right">&nbsp;</td>
      <td align="right"><?php echo number_format($cost_value, 2, '.', ',') ?></td>
      <td align="right">&nbsp;</td>
      <td align="right"><?php echo number_format($sales_value, 2, '.', ',') ?></td>
      <td align="right">&nbsp;</td>
      <td align="right"><?php echo number_format($result_value, 2, '.', ',') ?></td>
      <td align="right">&nbsp;</td>
      <td align="right"><?php echo number_format($result_perc_average, 2, '.', ',') ?></td>
      <td><?php echo $sales_rep ?></td>
    </tr>
    <?php
	$z++;
}

?>
<tfoot>
    <tr>
      <td align="right">&nbsp;</td>
      <td align="right">&nbsp;</td>
      <td align="right">&nbsp;</td>
      <td align="right">&nbsp;</td>
      <td align="right">&nbsp;</td>
      <td align="right"><?php echo number_format($job_value_total, 2, '.', ',') ?></td>
      <td align="right">&nbsp;</td>
      <td align="right"><?php echo number_format($cost_value_total, 2, '.', ',') ?></td>
      <td align="right">&nbsp;</td>
      <td align="right"><?php echo number_format($sales_value_total, 2, '.', ',') ?></td>
      <td align="right">&nbsp;</td>
      <td align="right"><?php echo number_format($result_value_total, 2, '.', ',') ?></td>
      <td align="right">&nbsp;</td>
      <td align="right"><?php echo number_format($result_total_perc_average, 2, '.', ',') ?></td>
      <td>&nbsp;</td>
    </tr>
    </tfoot>
  </table>
<?php } else { ?>
No data available in this report for the selected time period.
<?php } ?>
  </div>
 
  </div>
  


<?php include ('../navigation/side_bar.php'); ?>




</body>

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

</html>