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

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


$table_name = $_SESSION['db_prefix']."_maintenance_jobs";
$charges_query = "SELECT value, call_out_charge, call_out_charge_final, additional_charge, additional_charge_final, adjustment_text, adjustment_charge, compiled, invoiced, completed FROM $table_name WHERE maintenance_card_no =  '".$maintenance_card_no."' ";


$charges_result = mySQL_query($charges_query);
$charges_num = mysql_num_rows($charges_result);
$value = mysql_result($charges_result, 0, "value");
$call_out_charge = mysql_result($charges_result, 0, "call_out_charge");
$call_out_charge_final = mysql_result($charges_result, 0, "call_out_charge_final");
$additional_charge = mysql_result($charges_result, 0, "additional_charge");
$additional_charge_final = mysql_result($charges_result, 0, "additional_charge_final");
$adjustment_text = mysql_result($charges_result, 0, "adjustment_text");
$adjustment_charge = mysql_result($charges_result, 0, "adjustment_charge");


$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']."_maintenance_job_lines_done";
}
else {
	$table_name = $_SESSION['db_prefix']."_maintenance_job_lines";
}
	
$line_query = "SELECT *  FROM $table_name WHERE  maintenance_card_no =  '".$maintenance_card_no."' ";
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);

$table_name = $_SESSION['db_prefix']."_maintenance_additional_supply_lines";
$job2_lines_query = "SELECT store_item_id, item_name, quantity, price  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  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);

$report_name = 'Maintenance costing';

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

<title>Manase maintenance card <?php echo $maintenance_card_no ?></title>
<link rel="shortcut icon" type="image/x-icon" href="../images/favicon.ico">


<style>
body {
	font-family:Arial, Helvetica, sans-serif;
	font-size:12px;
}
</style>

</head>

<body  onload="window.print(); window.close();" >
<?php include('../include/print_report_header.php'); ?>
<div class="print_main_frame" style="height:auto;"><br>
<br>
<div class="print_standard_frame">

          <table width="650" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="553"><label class="label_page_head">Maintenance costing job card</label></td>
              <td width="97"><label class="label_quote_no"><?php echo $maintenance_card_no;  ?></label>
               </td>
            </tr>
          </table>
</div>
<div class="print_standard_frame"> 
<?php 
include('../include/customer_block.php');
?>
</div>   
<div class="print_standard_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">Project description:</td>
      <td colspan="3" align="left" valign="top"><?php  echo $project_description;  ?></td>
      </tr>
    <tr>
      <td align="left" valign="top"><?php  echo 'Order date:';?></td>
      <td width="203" align="left" valign="top"><?php  echo date("d-m-Y", strtotime($order_date));  ?></td>
      <td width="166" align="left" valign="top"><?php  echo 'Job value'; ?></td>
      <td width="140" 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="print_standard_frame">
Store items
<table width="650" id="table_display" >
          <tr>
            <td height="26" align="left" valign="top">Item</td>
            <td width="144" align="left" valign="top">Group</td>
            <td width="26" align="center" valign="top">Unit</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>
        </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 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 ($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>
        </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");
	  
	  
	  	$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 job_card = '".$maintenance_card_no."'  AND store_item_id = '".$store_item_id."' AND maintenance = 'yes' ";
					$register_result = mySQL_query($register_query);
					$serial = mysql_result($register_result, 0, "serial_number");
			}
				
	  $total2=$delivered*$delivered_price;
	  $a8tot = $a8tot + $total2;
  ?>	  
  <tr>
    <td><?php echo $item_name ?>&nbsp;
      <?php if ($register=='yes') { echo 'serial no: '.$serial ; }?></td>
    <td align="right" valign="top">&nbsp;</td>
            <td align="center" valign="top">&nbsp;</td>
            <td align="center" valign="top"><?php echo $delivered ?></td>
            <td align="right" valign="top"><?php echo number_format($delivered_price, 2, '.', ',') ?></td>
            <td align="right" valign="top"><?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");
	  
	  
	  	$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 job_card = '".$_SESSION['maintenance_card_no']."'  AND store_item_id = '".$store_item_id."' AND maintenance = 'yes' ";
					$register_result = mySQL_query($register_query);
					$serial = mysql_result($register_result, 0, "serial_number");
			}
		
	  $total3=$delivered*$delivered_price;
	  $a8tot = $a8tot + $total3;
				
	  
  ?>	  
  <tr>
    <td><?php echo $item_name ?>&nbsp;
      <?php if ($register=='yes') { echo 'serial no: '.$serial ; }?></td>
    <td align="right" valign="top">&nbsp;</td>
    <td align="center" valign="top">&nbsp;</td>
    <td align="center" valign="top"><?php echo $delivered ?></td>
    <td align="right" valign="top"><?php echo number_format($delivered_price, 2, '.', ',') ?></td>
    <td align="right" valign="top"><?php echo number_format($total3, 2, '.', ',');?></td>
  </tr>
  <?php 
  $i++;
  }
  ?>         
         <tfoot>
          <tr>
            <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="left" valign="top">&nbsp;</td>
            <td align="right" valign="top">&nbsp;</td>
           </tr>
        </tfoot>
      </table>
        <br>
      <br>
</div>      

<div class="print_standard_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 ($call_out_charge!='') {echo number_format($call_out_charge, 2, '.', ',');} ?></td>
            <td align="right" valign="top"><?php if ($call_out_charge_final!='') {echo number_format($call_out_charge_final, 2, '.', ',');} ?></td>
            <td align="right" valign="top"><?php $call_out_dif = $call_out_charge_final - $call_out_charge; if ($call_out_dif>0) {echo '<font color=red>'.number_format($call_out_dif, 2, '.', ',').'</font>'; } else { echo number_format($call_out_dif, 2, '.', ',');} ?>  </td>
        </tr>
          <tr>
            <td valign="top">Additional charge:</td>
            <td align="right" valign="top">&nbsp;</td>
            <td align="right" valign="top"><?php if ($additional_charge!='') {echo number_format($additional_charge, 2, '.', ',');} ?></td>
            <td align="right" valign="top"><?php if ($additional_charge_final!='') {echo number_format($additional_charge_final, 2, '.', ',');} ?></td>
            <td align="right" valign="top"><?php $additional_dif = $additional_charge_final - $additional_charge; if ($additional_dif>0) {echo '<font color=red>'.number_format($additional_dif, 2, '.', ',').'</font>'; } else { echo number_format($additional_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+$call_out_charge+$additional_charge?>
            <?php $invoiced_total = $a8tot+$call_out_charge+$adjustment_charge+$additional_charge_final?> 
            <?php $dif_total = $a10tot+$call_out_dif+$adjustment_charge+$additional_dif?> 
              </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>
</div>
    
   
</div>  
</div>

</body>
</html>