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

<?php
$maintenance_card_no=$_GET['maintenance_card_no'];

$table_name = $_SESSION['db_prefix']."_maintenance_jobs";
$job_query = "SELECT * FROM $table_name WHERE maintenance_card_no = '".$maintenance_card_no."' ";
$job_result = mySQL_query($job_query);
//$maintenance_card_no = mysql_result($job_result, 0, "maintenance_card_no");
$cust_id = mysql_result($job_result, 0, "cust_id");
$quote_note = mysql_result($job_result, 0, "quote_note");
$value = mysql_result($job_result, 0, "value");
$order_date = mysql_result($job_result, 0, "date");
$start_date = mysql_result($job_result, 0, "start_date");
$end_date = mysql_result($job_result, 0, "end_date");
$maintenance_no = mysql_result($job_result, 0, "maintenance_no");
$cycle_no = mysql_result($job_result, 0, "cycle_no");
$project_description = mysql_result($job_result, 0, "project_description");
$project = mysql_result($job_result, 0, "project");

$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, "start_date");
$team = mysql_result($job_result, 0, "team");

$call_out_charge = mysql_result($job_result, 0, "call_out_charge");
$additional_charge = mysql_result($job_result, 0, "additional_charge");



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





$table_name = $_SESSION['db_prefix']."_maintenance_job_lines";
$job_lines_query = "SELECT store_item_id, item_name, delivered, delivered_price, store_reg_id  FROM $table_name WHERE maintenance_card_no = '".$maintenance_card_no."'  ";
$job_lines_result = mySQL_query($job_lines_query);
$job_lines_num = mysql_num_rows($job_lines_result);

$table_name = $_SESSION['db_prefix']."_maintenance_additional_supply_lines";
$job2_lines_query = "SELECT store_item_id, item_name, quantity, price, store_reg_id  FROM $table_name WHERE maintenance_card_no = '".$maintenance_card_no."'  ";
$job2_lines_result = mySQL_query($job2_lines_query);
$job2_lines_num = mysql_num_rows($job2_lines_result);

$table_name = $_SESSION['db_prefix']."_maintenance_return_lines";
$job3_lines_query = "SELECT store_item_id, item_name, quantity, price, store_reg_id  FROM $table_name WHERE maintenance_card_no = '".$maintenance_card_no."'  ";
$job3_lines_result = mySQL_query($job3_lines_query);
$job3_lines_num = mysql_num_rows($job3_lines_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 = "Maintenance-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/maintenance_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">Maintenance card (closed)</label></td>
              <td width="97"><label class="label_quote_no"><?php echo $maintenance_card_no;  ?></label>
              </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;  ?></td>
      <td align="left" valign="top"><?php echo 'Maintenance card no:'; ?></td>
      <td align="left" valign="top"><?php echo $maintenance_card_no;  ?></td>
    </tr>
    <?php if ($project=='Monthly maintenance') { ?>
    <tr>
      <td align="left" valign="top">Cycle number:</td>
      <td align="left" valign="top"><?php echo $cycle_no ?></td>
      <td align="left" valign="top">Maintenance contract no:</td>
      <td align="left" valign="top"><?php echo $maintenance_no ?></td>
    </tr>
    <?php } ?>
    <tr>
      <td align="left" valign="top">Maintenance team:</td>
      <td align="left" valign="top"><?php  echo $team;  ?></td>
      <td align="left" valign="top">&nbsp;</td>
      <td align="left" valign="top">&nbsp;</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="168" align="left" valign="top"><?php  echo 'Job value'; ?></td>
      <td width="210" align="left" valign="top"><?php   echo number_format($value, 2, '.', ','); ?></td>
    </tr>
    <tr>
      <td align="left" valign="top">Project period:</td>
      <td colspan="3" align="left" valign="top"><?php if ($deliver_date!='') {?>from: <?php echo date("d-m-Y", strtotime($deliver_date));  ?> to: <?php echo date("d-m-Y", strtotime($end_date));  ?><?php } ?> </td>
      </tr>
    <tr>
      <td width="121" align="left" valign="top">Project place:</td>
      <td colspan="3" align="left" valign="top"><?php echo $project_no?>&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">
<table width="650" id="table_display">
  <tr>
    <td>Call out charge:</td>
    <td width="171" align="right"><?php echo number_format($call_out_charge, 2, '.', ',') ?>&nbsp;</td>
  </tr>
  <tr>
    <td>Additional charge:</td>
    <td align="right"><?php echo number_format($additional_charge, 2, '.', ',') ?></td>
  </tr>
  <tr>
    <td>Sub total:
      <?php $sub_total_1 = $call_out_charge + $additional_charge ?></td>
    <td align="right"><?php echo number_format($sub_total_1, 2, '.', ',') ?>&nbsp;</td>
  </tr>
</table>
Store items
<table width="650" id="table_display">
    <tr>
      <td width="416">Item</td>
      <td width="66" align="center">Quantity</td>
      <td width="56" align="center">Price</td>
      <td width="92" align="center">Total</td>
    </tr>
    <?php
  $i=0;
  $sub_total_3=0;
  while ($i<$job_lines_num) {
	  $store_item_id = mysql_result($job_lines_result, $i, "store_item_id");
	  $item_name = mysql_result($job_lines_result, $i, "item_name");
	  $delivered = mysql_result($job_lines_result, $i, "delivered");
	  $delivered_price = mysql_result($job_lines_result, $i, "delivered_price");
	  $store_reg_id = mysql_result($job_lines_result, $i, "store_reg_id");
	  
	  $total=$delivered*$delivered_price;
	  $sub_total_3=$sub_total_3+$total;
	  
	  $table_name = $_SESSION['db_prefix']."_store_items";
	  $register_query = "SELECT register FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
	  $register_result = mySQL_query($register_query);
	  $register = mysql_result($register_result, 0, "register");
	  
	  if ($register=='yes') {
				  	$table_name = $_SESSION['db_prefix']."_store_register";
					$register_query = "SELECT serial_number  FROM $table_name WHERE store_register_id = '".$store_reg_id."' ";
					$register_result = mySQL_query($register_query);
					$register_num = mysql_num_rows($register_result);
					$serial = mysql_result($register_result, 0, "serial_number");
			}	
  ?>
    <tr>
      <td><?php echo $item_name ?> <?php if ($register=='yes') { echo 'serial no: '.$serial ; }?></td>
      <td align="center"><?php echo $delivered ?></td>
      <td align="right"><?php echo number_format($delivered_price, 2, '.', ',') ?></td>
      <td align="right"><?php echo number_format($total, 2, '.', ',');?></td>
    </tr>
    <?php 
  $i++;
  }
  ?>
  <?php
  $i=0;
  while ($i<$job2_lines_num) {
	  $store_item_id = mysql_result($job2_lines_result, $i, "store_item_id");
	  //$item_name = mysql_result($job2_lines_result, $i, "item_name");
	  $delivered = mysql_result($job2_lines_result, $i, "quantity");
	  $delivered_price = mysql_result($job2_lines_result, $i, "price");
	  $store_reg_id = mysql_result($job2_lines_result, $i, "store_reg_id");
	  
	  	$table_name = $_SESSION['db_prefix']."_store_items";
		$register_query = "SELECT item_name, register FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
		$register_result = mySQL_query($register_query);
		$item_name = mysql_result($register_result, 0, "item_name");
		$register = mysql_result($register_result, 0, "register");
	  
	  if ($register=='yes') {
				  	$table_name = $_SESSION['db_prefix']."_store_register";
					$register_query = "SELECT serial_number  FROM $table_name WHERE store_register_id = '".$store_reg_id."' ";
					$register_result = mySQL_query($register_query);
					$register_num = mysql_num_rows($register_result);
					$serial = mysql_result($register_result, 0, "serial_number");
			}
				
	  $total2=$delivered*$delivered_price;
	  $sub_total_3=$sub_total_3+$total2;
  ?>	  
  <tr>
    <td><?php echo $item_name ?>&nbsp;
      <?php if ($register=='yes') { echo 'serial no: '.$serial ; }?></td>
    <td align="center"><?php echo $delivered ?></td>
    <td align="right"><?php echo number_format($delivered_price, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($total2, 2, '.', ',');?></td>
  </tr>
  <?php 
  $i++;
  }
  ?>
  <?php
  $i=0;
  while ($i<$job3_lines_num) {
	  $store_item_id = mysql_result($job3_lines_result, $i, "store_item_id");
	  //$item_name = mysql_result($job2_lines_result, $i, "item_name");
	  $delivered = mysql_result($job3_lines_result, $i, "quantity");
	  $delivered_price = mysql_result($job3_lines_result, $i, "price");
	  $store_reg_id = mysql_result($job3_lines_result, $i, "store_reg_id");
	  
	  	$table_name = $_SESSION['db_prefix']."_store_items";
		$register_query = "SELECT item_name, register FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
		$register_result = mySQL_query($register_query);
		$item_name = mysql_result($register_result, 0, "item_name");
		$register = mysql_result($register_result, 0, "register");
	  
	  if ($register=='yes') {
				  	$table_name = $_SESSION['db_prefix']."_store_register";
					$register_query = "SELECT serial_number  FROM $table_name WHERE store_register_id = '".$store_reg_id."' ";
					$register_result = mySQL_query($register_query);
					$register_num = mysql_num_rows($register_result);
					$serial = mysql_result($register_result, 0, "serial_number");
			}
		
	  $total3=$delivered*$delivered_price;
	  $sub_total_3=$sub_total_3+$total3;
				
	  
  ?>	  
  <tr>
    <td><?php echo $item_name ?>&nbsp;
      <?php if ($register=='yes') { echo 'serial no: '.$serial ; }?></td>
    <td align="center"><?php echo $delivered ?></td>
    <td align="right"><?php echo number_format($delivered_price, 2, '.', ',') ?></td>
    <td align="right"><?php echo number_format($total3, 2, '.', ',');?></td>
  </tr>
  <?php 
  $i++;
  }
  ?>
    <tfoot>
      <tr>
        <td colspan="3">Sub total</td>
        <td align="right"><?php echo number_format($sub_total_3, 2, '.', ',') ?></td>
      </tr>
    </tfoot>
  </table><br>
<br>

  <table width="650"  id="table_display" >
    <tr>
      <td width="476">Project total:&nbsp;
        <?php $_SESSION['all_total'] = $sub_total_1 + $sub_total_3 ; ?></td>
      <td width="45" align="right"><?php if ($_SESSION['all_total'] > $_SESSION['value']) {?>
        <a href=" " onMouseover="ddrivetip('The value is higher than the order value.', 200)"; onMouseout="hideddrivetip()"><img src="../images/exclamation_mark_red_small.png" alt="." width="10" height="16"></a>
        <?php } ?>
  </td>
      <td width="113" align="right"><?php echo number_format($_SESSION['all_total'], 2, '.', ',') ?></td>
    </tr>
  </table>
  <br>
<br>
<a href="closed_maintenance_card_print.php?maintenance_card_no=<?php echo $maintenance_card_no ?>" target="_blank"><input name="print_button" type="button" value="Print maintenance card" class="imagebutton"></a></div>      



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


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