<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--reference to main.css removed-->
<?php
include('../include/session.php');
?>

<?php
$job_card_no=$_GET['job_card_no'];
$table_name = $_SESSION['db_prefix']."_compiled_jobs";
$job_query = "SELECT * FROM $table_name WHERE job_card_no = '".$job_card_no."' ";
$job_result = mySQL_query($job_query);
//$job_card_no = mysql_result($job_result, 0, "job_card_no");
$cust_id = mysql_result($job_result, 0, "cust_id");
$quote_note = mysql_result($job_result, 0, "quote_note");

$order_date = mysql_result($job_result, 0, "date");
$del_date = mysql_result($job_result, 0, "del_date");
$project_description = mysql_result($job_result, 0, "project_description");

$project_no = mysql_result($job_result, 0, "project_no");
$project_place = mysql_result($job_result, 0, "project_place");
$project_area = mysql_result($job_result, 0, "project_area");
$deliver_date = mysql_result($job_result, 0, "del_date");


$table_name = $_SESSION['db_prefix']."_mworks_jobs";
$charges_query = "SELECT value, labour_charge, labour_charge_final, mworks_charge, mworks_charge_final, adjustment_text, adjustment_charge, captured, compiled, invoiced, completed FROM $table_name WHERE job_card_no =  '".$job_card_no."' ";

$charges_result = mySQL_query($charges_query);
$charges_num = mysql_num_rows($charges_result);
$value = mysql_result($charges_result, 0, "value");
$labour_charge = mysql_result($charges_result, 0, "labour_charge");
$labour_charge_final = mysql_result($charges_result, 0, "labour_charge_final");
$mworks_charge = mysql_result($charges_result, 0, "mworks_charge");
$mworks_charge_final = mysql_result($charges_result, 0, "mworks_charge_final");
$adjustment_text = mysql_result($charges_result, 0, "adjustment_text");
$adjustment_charge = mysql_result($charges_result, 0, "adjustment_charge");

$captured = mysql_result($charges_result, 0, "captured");
$compiled = mysql_result($charges_result, 0, "compiled");
$invoiced = mysql_result($charges_result, 0, "invoiced");
$completed = mysql_result($charges_result, 0, "completed");

if ($completed=='yes') {
	$table_name = $_SESSION['db_prefix']."_mworks_order_job_lines_done";
}
else {
	$table_name = $_SESSION['db_prefix']."_mworks_order_job_lines";
}
	
$line_query = "SELECT *  FROM $table_name WHERE  job_card_no =  '".$job_card_no."' ";
$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>

</head>

<body>
<?php
include ("../navigation/header.php");
$page_name = "Orders-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/production_sidebar.php");
?>
<div class="main_frame">
  <div class="page_head_frame">
          <table width="650" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="553"><label class="label_page_head">M-works costing job card</label></td>
              <td width="97"><label class="label_quote_no"><?php echo $job_card_no;  ?></label>
               <?php if (($captured=='yes') AND ($compiled=='') AND ($completed=='') ) { ?> <a href=" " onMouseover="ddrivetip('Job not started yet.', 100)"; onMouseout="hideddrivetip()"> <img src="../images/round_red.png" width="12" height="12">  </a><?php } ?>
              <?php if (($captured=='yes') AND ($compiled=='yes') AND  ($completed=='') ) { ?> <a href=" " onMouseover="ddrivetip('Job in progress.', 100)"; onMouseout="hideddrivetip()"> <img src="../images/round_orange.png" width="12" height="12">  </a><?php } ?>
              <?php if (($captured=='yes') AND ($completed=='yes') ) { ?> <a href=" " onMouseover="ddrivetip('Job completed.', 100)"; onMouseout="hideddrivetip()"> <img src="../images/round_green.png" width="12" height="12">  </a> <?php } ?></td>
            </tr>
          </table>
          </div>
  <div class="page_middle_frame">
<div class="page_inside_frame">
<?php 
include('../include/customer_block.php');
?>
</div>    
<div class="page_inside_frame">
<table width="650" id="table_details_display">
    <tr>
      <td align="left" valign="top">Project:</td>
      <td align="left" valign="top"><?php  echo $project_description;  ?></td>
      <td align="left" valign="top"><?php echo 'Job card no:'; ?></td>
      <td align="left" valign="top"><?php echo $job_card_no;  ?></td>
    </tr>
    <tr>
      <td align="left" valign="top"><?php  echo 'Order date:';?></td>
      <td width="131" align="left" valign="top"><?php  echo date("d-m-Y", strtotime($order_date));  ?></td>
      <td width="101" align="left" valign="top"><?php  echo 'Job value'; ?></td>
      <td width="277" align="left" valign="top"><?php   echo number_format($value, 2, '.', ','); ?></td>
    </tr>
    <tr>
      <td align="left" valign="top">Delivery date:</td>
      <td colspan="3" align="left" valign="top"><?php echo date("d-m-Y", strtotime($deliver_date));  ?></td>
      </tr>
    <tr>
      <td width="121" align="left" valign="top">Project place:</td>
      <td colspan="3" align="left" valign="top"><?php echo $project_area?>&nbsp;&nbsp;<?php echo $project_area?>&nbsp;&nbsp;<?php echo $project_place?></td>
      </tr>
    <tr>
      <td align="left" valign="top">Notes:</td>
      <td colspan="3" align="left" valign="top"><?php echo $quote_note ?></td>
    </tr>
  </table>
</div> 
<div class="page_inside_frame">
Store items
<table width="650" id="table_display" >
          <tr>
            <td height="26" colspan="2" align="left" valign="top">Item</td>
            <td width="66" align="left" valign="top">Group</td>
            <td width="26" align="center" valign="top">Unit</td>
            <td width="46" align="center" valign="top">Qty<br>
            est</td>
            <td width="46" align="center" valign="top">Unit<br>
              cost</td>
            <td width="48" align="center" valign="top">Cost</td>
            <td width="51" align="center" valign="top">Qty<br>
              supp</td>
            <td width="59" align="center" valign="top">Unit<br>
              cost</td>
            <td width="54" align="center" valign="top">Cost</td>
            <td width="52" align="center" valign="top">+/-</td>
        </tr>
          <?php
		        $a4tot=0;
				$a8tot=0;
				$a10=0;
				$a10tot=0;
				$i=0;
				$sub_total_2=0;
				while ($i<$line_num){
					 $a0 = mysql_result($line_result, $i, "order_line_id");
					 $a1 = mysql_result($line_result, $i, "store_item_id");
					 
					 if ( ($a1>0) AND ($a1<100000)) {
					 $table_name = $_SESSION['db_prefix']."_store_items";
						$item_query = "SELECT item_name, unit, subCategory FROM $table_name WHERE store_item_id = '".$a1."' ";
						$item_result = mySQL_query($item_query);
						$a1a = mysql_result($item_result, 0, "item_name");
						$a9 = mysql_result($item_result, 0, "unit");
						$a5 = mysql_result($item_result, 0, "subCategory");
					 }
					 
					 if ($a1>100000) {
					 $table_name = $_SESSION['db_prefix']."_store_items_special";
						$item_query = "SELECT item_name FROM $table_name WHERE special_store_item_id = '".$a1."' ";
						$item_result = mySQL_query($item_query);
						$a1a = mysql_result($item_result, 0, "item_name");
						$a5='Special order';
						$a9='';
					 }
					 
					 $a2 = mysql_result($line_result, $i, "quantity");
					 $a3 = mysql_result($line_result, $i, "price");
					 $a4 = $a2*$a3;
					 $a4tot = $a4tot + $a4;
					 
					 $a6 = mysql_result($line_result, $i, "delivered");
					 $a7 = mysql_result($line_result, $i, "delivered_price");
					 $a8 = $a6*$a7;
					 $a8tot = $a8tot + $a8; 
					 $a10 = $a8-$a4;
					 $a10tot = $a8tot - $a4tot;				 
				 ?>
          <tr>
            <td colspan="2" valign="top"><?php echo $a1a ?></td>
            <td valign="top"><?php echo $a5 ?></td>
            <td align="center" valign="top"><?php echo $a9 ?></td>
            <td align="center" valign="top"><?php if ($a2!='') {echo $a2;} ?></td>
            <td align="right" valign="top"><?php if ($a3!='') {echo number_format($a3, 2, '.', ',');} ?></td>
            <td align="right" valign="top"><?php if ($a4!='') {echo number_format($a4, 2, '.', ',');} ?></td>
            <td align="center" valign="top"><?php if ($a6!='') {echo $a6;} ?></td>
            <td align="right" valign="top"><?php if ($a7!='') {echo number_format($a7, 2, '.', ',');} ?></td>
            <td align="right" valign="top"><?php if ($a8!='') {echo number_format($a8, 2, '.', ',');} ?></td>
            <td align="right" valign="top"><?php if ($a10!='') { if ($a10>0) { echo '<font color=red>'.number_format($a10, 2, '.', ',').'</font>'; } else { echo number_format($a10, 2, '.', ',');} } ?></td>
        </tr>
          <?php 
			  $i++;
				 }
				 ?>
         <tfoot>
          <tr>
            <td colspan="2" align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="right" valign="top">&nbsp;</td>
        </tr>
        </tfoot>
      </table>
        <br>
      <br>
</div>      

<div class="page_inside_frame">
Cost over view
  <table width="650" id="table_display" >
          <tr>
            <td height="26" align="left" valign="top">Expense</td>
            <td width="86" align="center" valign="top">Accepted</td>
            <td width="116" align="center" valign="top">Estimated</td>
            <td width="106" align="center" valign="top">Actual</td>
            <td width="100" align="center" valign="top">+/-</td>
        </tr>
        
          <tr>
            <td valign="top">Parts costs:</td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="right" valign="top"><?php echo number_format($a4tot, 2, '.', ',') ?></td>
            <td align="right" valign="top"><?php echo number_format($a8tot, 2, '.', ',') ?></td>
            <td align="right" valign="top"><?php if ($a10>0) {echo '<font color=red>'.number_format($a10tot, 2, '.', ',').'</font>'; } else { echo number_format($a10tot, 2, '.', ',');} ?></td>
          </tr>
          <tr>
            <td valign="top">Labour charge:</td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="right" valign="top"><?php if ($labour_charge!='') {echo number_format($labour_charge, 2, '.', ',');} ?></td>
            <td align="right" valign="top"><?php if ($labour_charge_final!='') {echo number_format($labour_charge_final, 2, '.', ',');} ?></td>
            <td align="right" valign="top"><?php $labour_dif = $labour_charge_final - $labour_charge; if ($labour_dif>0) {echo '<font color=red>'.number_format($labour_dif, 2, '.', ',').'</font>'; } else { echo number_format($labour_dif, 2, '.', ',');} ?>  </td>
        </tr>
          <tr>
            <td valign="top">Adjustment: <?php echo $adjustment_text ?></td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="right" valign="top"><?php if ($adjustment_charge!='') {echo number_format($adjustment_charge, 2, '.', ',');} ?></td>
            <td align="right" valign="top"><?php if ($adjustment_charge!='') {if ($adjustment_charge>0) { echo '<font color=red>'.number_format($adjustment_charge, 2, '.', ',').'</font>';} else  { echo number_format($adjustment_charge, 2, '.', ',');}} ?></td>
          </tr>
         <tfoot>
          <tr>
            <td align="left" valign="top">
			<?php $estimated_total = $a4tot+$labour_charge?>
            <?php $invoiced_total = $a8tot+$labour_charge_final+$adjustment_charge?> 
            <?php $dif_total = $a10tot+$labour_dif+$adjustment_charge?> 
              </td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="right" valign="top">&nbsp;</td>
        </tr>
          <tr>
            <td align="left" valign="top">Totals:</td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="right" valign="top"><?php echo number_format($estimated_total, 2, '.', ',') ?></td>
            <td align="right" valign="top"><?php echo number_format($invoiced_total, 2, '.', ',') ?></td>
            <td align="right" valign="top"><?php if ($dif_total>0) {echo '<font color=red>'.number_format($dif_total, 2, '.', ',').'</font>'; } else { echo number_format($dif_total, 2, '.', ',');} ?></td>
          </tr>
          <tr>
            <td align="left" valign="top">&nbsp;</td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="right" valign="top">&nbsp;</td>
          </tr>
          <tr>
            <td align="left" valign="top"><?php $dif2_total = $value-$invoiced_total?></td>
            <td align="right" valign="top"><?php echo number_format($value, 2, '.', ',') ?></td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="right" valign="top"><?php echo number_format($invoiced_total, 2, '.', ',') ?></td>
            <td align="right" valign="top"><?php if ($dif2_total>0) {echo '<font color=red>'.number_format($dif2_total, 2, '.', ',').'</font>'; } else { echo number_format($dif2_total, 2, '.', ',');} ?></td>
          </tr>
        </tfoot>
      </table>
        <br>
<br>
</div>     




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


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