<!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");

$job_no = $_GET['job_no'];
$quote='no';
$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
$job_query = "SELECT * FROM $table_name WHERE job_no = '".$job_no."' ";
$job_result = mySQL_query($job_query);
$cust_id = mysql_result($job_result, 0, "cust_id");	
$captured = mysql_result($job_result, 0, "captured");	
$authorisation = mysql_result($job_result, 0, "authorisation");	
$delivered = mysql_result($job_result, 0, "delivered");
$quote_note = mysql_result($job_result, 0, "quote_note");

if  ( ($captured=='yes') AND ($authorisation=='yes') AND ($delivered=='yes') ) {
	$completed ='yes';
}
else {
	$completed = 'no';
}


$table_name2 = $_SESSION['db_prefix']."_customer";
		$customer_query = "SELECT * FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);	
		$a1 = mysql_result($customer_result, 0, "account_type");
			if ($a1=="corporate") { 
				$name = mysql_result($customer_result, 0, "comp_name"); 
			} 
			else {
				$name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 			}


$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$line_check_query = "SELECT  store_item_id FROM $table_name WHERE quote_no = '".$job_no."'  ";
$line_check_result = mySQL_query($line_check_query);
$line_check_num = mysql_num_rows($line_check_result);
if ($line_check_num!=0) { 
$table_name = $_SESSION['db_prefix']."_cash_job_lines";
}
else {
$table_name = $_SESSION['db_prefix']."_cash_job_lines_done";
}
$line_query = "SELECT store_item_id, SUM(quantity) AS summed_quantity, price,  SUM(authorised) AS summed_authorised, auth_price FROM $table_name WHERE quote_no = '".$job_no."'  GROUP BY store_item_id";
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);

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


<!--jscript-->
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> 
<script type="text/javascript" src="../jscript/tablesort.js"></script>

</head>

<body >
<div class="main_frame" style="height:auto;">
  <div class="page_head_frame">
          <table width="809" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="452"><label class="label_page_head">Cash sale costing</label>
                &nbsp;&nbsp;&nbsp;<label class="label_page_head_2"><?php echo $name; ?></label>
                </td>
              <td width="228"><label style="font-size:24px; color:#FB6808;"><?php echo "J".$job_no ?></label></td>
            </tr>
          </table>
        </div>
        <br />
        <br />
        
        <div class="page_middle_frame">
     <?php 
		include ('../include/customer_block.php');
		echo '<br>';
		include ('../include/cash_job_block.php')
		?><br />
<br />
        </div>
     <div class="page_middle_frame">
        
          <table width="676" id="table_details_display" border="1">
           <thead>
            <tr>
              <th colspan="2">&nbsp;</th>
              <th colspan="3" align="center">Quoted</th>
              <th colspan="3" align="center">Authorised</th>
              <th align="left">&nbsp;</th>
            </tr>
            <tr>
              <th>Item</th>
              <th align="center"><label class="label_std">Unit</label></th>
              <th align="center">Qty</th>
              <th align="center">Price</th>
              <th align="center">Total</th>
              <th align="center"><label class="label_std">Qty</label></th>
              <th align="center"><label class="label_std">Price</label></th>
              <th align="center"><label class="label_std">Total</label></th>
              <th align="left">&nbsp;</th>
            </tr>
           </thead> 
            <?php 
	$i=0;
	$total = '0.00';
	$quote_total2 = '0.00';
	$total2 ='0.00';
	$sub_total2='0.00';
	while ($i<$line_num) {
		$store_item_id = mysql_result($line_result, $i, "store_item_id");
			$table_name4 = $_SESSION['db_prefix']."_store_items";
			$item_query = "SELECT item_name, unit FROM $table_name4 WHERE store_item_id = '".$store_item_id."' ";
			$item_result = mySQL_query($item_query);
			$item_name = mysql_result($item_result, 0, "item_name");
			$unit = mysql_result($item_result, 0, "unit");
		$quantity = mysql_result($line_result, $i, "summed_quantity");
				
		$price = mysql_result($line_result, $i, "price");
		$auth_price = mysql_result($line_result, $i, "auth_price");
		$auth_quantity = mysql_result($line_result, $i, "summed_authorised");
		$price = mysql_result($line_result, $i, "price");
		$total = $quantity * $price;
		$total2 = $auth_quantity * $auth_price;
		$quote_total2 = $quote_total2 + $total;
		//$total2 = $price * $delivered;
		$sub_total2 = $sub_total2 + $total2;
		$difference2a = $total2 - $total;
	?>
            <tr>
              <td width="177"><?php echo $item_name ?></td>
              <td width="28" align="left"><?php echo $unit ?></td>
              <td width="50" align="center"><?php if ($quantity>0) {echo $quantity;} ?></td>
              <td width="54" align="right"><?php if ($price>0) {echo $price;} ?></td>
              <td width="69" align="right" style="background-color:#FFE9D2"><?php if ($total>0) {echo $total;} ?></td>
              <td width="54" align="center"><?php  if ($auth_quantity>0) {echo $auth_quantity;} ?></td>
              <td width="54" align="right"><?php if ($auth_quantity>0) {echo number_format($auth_price, 2, '.', ',');} ?></td>
              <td width="69" align="right" style="background-color:#FFE9D2"><?php if ($total2>0) {echo number_format($total2, 2, '.', ',');} ?></td>
              <td width="63" align="right"><?php if ($auth_quantity>0) { if ($difference2a>0) { echo '<font color=red>+'.number_format($difference2a, 2, '.', ','); } else { echo number_format($difference2a, 2, '.', ','); }} ?></td>
            </tr>
            <?php
	$i++;
	}
	
	$difference2 = $sub_total2 - $quote_total2;
	?>
            <tr>
              <td>&nbsp;</td>
              <td align="left">&nbsp;</td>
              <td align="left">&nbsp;</td>
              <td align="left">&nbsp;</td>
              <td align="right" style="background-color:#FFBF80"><?php if ($quote_total2>0) {echo $quote_total2 ;} ?></td>
              <td align="left">&nbsp;</td>
              <td align="left">&nbsp;</td>
              <td align="right" style="background-color:#FFBF80"><?php if ($sub_total2>0) {echo number_format($sub_total2, 2, '.', ',');} ?></td>
              <td align="right" style="background-color:#FFBF80"><?php if ($difference2>0) { echo '<font color=red>+'.number_format($difference2, 2, '.', ','); } else { echo number_format($difference2, 2, '.', ','); } ?>
              </td>
            </tr>
          </table>
   </div>
   <div class="page_middle_frame">
          <table width="676" id="table_details_display" border="1">
           <thead>
            <tr>
              <th colspan="2"><label class="label_std">Project  total: </label></th>
              <th colspan="2" align="center">Quoted</th>
              <th colspan="2" align="center">Authorised</th>
              <th width="63" align="left">&nbsp;</th>
            </tr>
            
           </thead> 
            <?php 
				  $all_total = $sub_total2  ;
				  $difference_total = $all_total - $quote_total2;
				 ?>
            <tr>
              <td width="76">&nbsp;</td>
              <td width="125">&nbsp;</td>
              <td width="114" align="right">&nbsp;</td>
              <td width="69" align="right"  style="background-color:#FFBF80"><?php if ($quote_total2>0) {echo number_format($quote_total2, 2, '.', ',');} ?></td>
              <td width="114" align="right">&nbsp;</td>
              <td width="69" align="right" style="background-color:#FFBF80"><?php if ($all_total>0) {echo number_format($all_total, 2, '.', ',');} ?></td>
              <td width="63" align="right" style="background-color:#FFBF80"><?php if ($difference_total>0) { echo '<font color=red>+'.number_format($difference_total, 2, '.', ','); } else { echo number_format($difference_total, 2, '.', ','); } ?>
              </span></td>
            </tr>
          </table>
        </div>

        
        <div class="page_middle_frame">
          <div align="left">
            <label class="label_std">Result: </label>
          </div>
          <table width="676" id="table_details_display">
            <?php 
				  $final_total = $all_total ;
				 ?>
            <tr>
              <td width="191" valign="middle"><?php if ($completed=='yes') { ?>
              <img src="../images/round_green.png" width="16" height="16" />&nbsp;Job completed. <?php } else { ?>
              <img src="../images/round_red.png" width="16" height="16" /> Job not completed yet. <?php } ?> </td>
              <td width="163" valign="middle">Job value:</td>
              <td width="164" valign="middle">Job cost:</td>
              <td width="71" valign="middle" >Result:</td>
              <td width="65" align="right" valign="middle" >&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td align="right"><?php echo number_format($value, 2, '.', ',') ?>&nbsp;</td>
              <td align="right">
                <?php if ($final_total>0) {echo number_format($final_total, 2, '.', ',');} ?>
              </td>
              <td align="right" style="background-color:#FFBF80"><?php echo number_format($value-$final_total, 2, '.', ',') ?></td>
              <td align="right" ><?php $percentage = (($value-$final_total)/$value)*100; echo number_format($percentage, 2, '.', ',') ?> %</td>
            </tr>
          </table>
        </div>
  
</div>
<?php include ('../navigation/side_bar.php'); ?>



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