<?php
$table_name = $_SESSION['db_prefix']."_maintenance_job_lines";
$items_cost_query = "SELECT  SUM(delivered*delivered_price) AS actual_items_cost FROM $table_name WHERE maintenance_card_no = '".$_SESSION['maintenance_card_no']."'  ";
$items_cost_result = mySQL_query($items_cost_query);
$_SESSION['actual_items_cost'] = mysql_result($items_cost_result, 0, "actual_items_cost");


$table_name = $_SESSION['db_prefix']."_maintenance_jobs";
$charges_query = "SELECT maintenance_no, value, call_out_charge, call_out_charge_final,  additional_charge, additional_charge_final, compiled, invoiced FROM $table_name WHERE maintenance_card_no =  '".$_SESSION['maintenance_card_no']."' ";
$charges_result = mySQL_query($charges_query);
$charges_num = mysql_num_rows($charges_result);
$call_out_charge = mysql_result($charges_result, 0, "call_out_charge");
$additional_charge = mysql_result($charges_result, 0, "additional_charge");
$order_value = mysql_result($charges_result, 0, "value");

$table_name = $_SESSION['db_prefix']."_maintenance_job_lines";
$line_query = "SELECT *  FROM $table_name WHERE  maintenance_card_no =  '".$_SESSION['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 = '".$_SESSION['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 = '".$_SESSION['maintenance_card_no']."'  ";
$job3_lines_result = mySQL_query($job3_lines_query);
$job3_lines_num = mysql_num_rows($job3_lines_result);

?>
<script>
function est_total() {
	
	total_value = parseFloat(document.getElementById('parts_total_text').value) +  parseFloat(document.getElementById('call_out_text').value) +  parseFloat(document.getElementById('additional_text').value) + parseFloat(document.getElementById('adjustment_charge').value);
	document.getElementById('actual_total_text').value = parseFloat(total_value).toFixed(2);
	dif_value = parseFloat(document.getElementById('parts_dif_text').value) +  parseFloat(document.getElementById('call_out_dif_text').value) +  parseFloat(document.getElementById('additional_dif_text').value) + parseFloat(document.getElementById('adjustment_dif_text').value);
   document.getElementById('total_dif_text').value = parseFloat(dif_value).toFixed(2);
   act_prof = ((document.getElementById('order_value_text').value / total_value)*100)-100;
   document.getElementById('act_prof_text').value = parseFloat(act_prof).toFixed(2);
}	
</script>	

</head>

<?php include('../include/wait.php') ?>
<link href="../css/wait.css" rel="stylesheet" type="text/css">
<body onLoad="est_total(); MM_showHideLayers('wait','','hide')" onFocus="MM_showHideLayers('wait','','hide');"  onBlur="MM_showHideLayers('wait','','hide');">

<div class="wizard_page_middle_frame">
<form action="" method="get" name="form1" id="form1" >

<div class="page_inside_frame">
Store items used:
  <table width="650" id="table_display" >
          <tr>
            <td width="226" height="26" align="left" valign="top">Item</td>
            <td width="138" align="left" valign="top">Group</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="96" align="center" valign="top">Cost</td>
            <td width="52" align="center" valign="top">&nbsp;</td>
        </tr>
          <?php
		        
				$a8tot=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");
					 
					
					 $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");
					
					 
					 				 
					 $a6 = mysql_result($line_result, $i, "delivered");
					 $a7 = mysql_result($line_result, $i, "delivered_price");
					 $a8 = $a6*$a7;
					 $a8tot = $a8tot + $a8; 
					
					 				 
				 ?>
          <tr>
            <td valign="top"><?php echo $a1a ?></td>
            <td valign="top"><?php echo $a5 ?></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">&nbsp;</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 = '".$_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");
			}
				
	  $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">&nbsp;</td>
    <td align="center"><?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>
            <td align="right" valign="top">&nbsp;</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">&nbsp;</td>
    <td align="center"><?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>
    <td align="right" valign="top">&nbsp;</td>
  </tr>
  <?php 
  $i++;
  }
  ?>         
                 
         <tfoot>
          <tr>
            <td align="left" valign="top">Totals</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"><?php $_SESSION['a8tot']=$a8tot; echo number_format($_SESSION['a8tot'] , 2, '.', ',') ?></td>
            <td align="right" valign="top">&nbsp;</td>
        </tr>
        </tfoot>
      </table>
      <?php
	  $_SESSION['a10tot'] = $_SESSION['a8tot'] - $_SESSION['a8tot'];
	  ?>
        <br>
      <br>
</div>
<div class="page_inside_frame">
Cost over view:
  <table width="650" id="table_display" >
          <tr>
            <td height="26" align="left" valign="middle">Expense</td>
            <td width="116" align="center" valign="middle">Estimated</td>
            <td width="106" align="center" valign="middle">Actual</td>
            <td width="100" align="center" valign="middle">+/-</td>
        </tr>
        
          <tr>
            <td valign="middle">Parts costs:</td>
            <td align="right" valign="middle"><?php echo number_format($_SESSION['a8tot'], 2, '.', ',') ?></td>
            <td align="right" valign="middle"><input type="text" name="parts_total_text" id="parts_total_text" class="text100" value="<?php echo $_SESSION['a8tot'] ?>"  readonly style="text-align:right; border:hidden;" ></td>
            <td align="right" valign="middle"><input type="text" name="parts_dif_text" id="parts_dif_text" class="text100"  value="<?php echo number_format($_SESSION['a10tot'], 2, '.', '') ?>" readonly style="text-align:right; border:hidden;"></td>
          </tr>
          <tr>
            <td valign="middle">Call out charge:</td>
            <td align="right" valign="middle"><input type="hidden" name="call_out_est_text" id="call_out_est_text"   value="<?php echo $call_out_charge?> ">
          <?php if ($call_out_charge!='') {echo number_format($call_out_charge, 2, '.', ',');} ?></td>
            <td align="right" valign="middle"><input type="text" name="call_out_text" id="call_out_text" class="text100" onKeyPress="return isNumberKey(event)" onKeyUp=" this.value = this.value.replace (/(\...).*/, '$1')" style="text-align:right" value="<?php echo $_SESSION['call_out_adjust'] ?>" autocomplete="off" onBlur="if (this.value=='') {this.value=0}; new_dif = document.getElementById('call_out_text').value - document.getElementById('call_out_est_text').value; document.getElementById('call_out_dif_text').value=parseFloat(new_dif).toFixed(2); this.value = parseFloat(this.value).toFixed(2); est_total();" onfocus = "this.select();"></td>
            <td align="right" valign="middle"><?php $call_out_dif = $_SESSION['call_out_adjust'] - $call_out_charge; ?>
              <input type="text" name="call_out_dif_text" id="call_out_dif_text" value="<?php echo number_format($call_out_dif, 2, '.', '')?>" readonly style="text-align:right; border:hidden;" ></td>
          </tr>
         <tfoot>
          <tr>
            <td align="left" valign="middle">Additional charge:</td>
            <td align="right" valign="middle"><input type="hidden" name="additional_est_text" id="additional_est_text"   value="<?php echo $additional_charge?> ">              <?php if ($call_out_charge!='') {echo number_format($additional_charge, 2, '.', ',');} ?></td>
            <td align="right" valign="middle"><input type="text" name="additional_text" id="additional_text" class="text100" onKeyPress="return isNumberKey(event)" onKeyUp=" this.value = this.value.replace (/(\...).*/, '$1')" style="text-align:right" value="<?php echo $_SESSION['additional_adjust'] ?>" autocomplete="off" onBlur="if (this.value=='') {this.value=0}; new_dif = document.getElementById('additional_text').value - document.getElementById('additional_est_text').value; document.getElementById('additional_dif_text').value=parseFloat(new_dif).toFixed(2); this.value = parseFloat(this.value).toFixed(2); est_total();" onfocus = "this.select();"></td>
            <td align="right" valign="middle"><?php $additional_dif = $_SESSION['additional_adjust'] - $additional_charge; ?>              <input type="text" name="additional_dif_text" id="additional_dif_text" value="<?php echo number_format($additional_dif, 2, '.', '')?>" readonly style="text-align:right; border:hidden;" ></td>
          </tr>
          <tr>
            <td colspan="2" align="left" valign="middle">Adjustment:
            <input type="text" name="adjustment_text" id="adjustment_text" class="text300" maxlength="40" value="<?php echo $_SESSION['adjustment_text'] ?>"></td>
            <td align="right" valign="middle"><input type="text" name="adjustment_charge" id="adjustment_charge" class="text100" onKeyPress=" return allowNegativeNumber(event); return isNumberKey(event);" onKeyUp=" this.value = this.value.replace (/(\...).*/, '$1')" style="text-align:right" value="<?php echo number_format($_SESSION['adjustment'], 2, '.', '') ?>" autocomplete="off" onBlur="if (this.value=='') {this.value=0};
            adjustment_dif = document.getElementById('adjustment_charge').value; document.getElementById('adjustment_dif_text').value=parseFloat(adjustment_dif).toFixed(2); this.value = parseFloat(this.value).toFixed(2); est_total();" onfocus = "this.select();" ></td>
            <td align="right" valign="middle"><input type="text" name="adjustment_dif_text" id="adjustment_dif_text"  value="<?php  echo $_SESSION['adjustment_dif']?>" class="text100" readonly style="text-align:right; border:hidden; " ></td>
          </tr>
          <tr>
            <td align="left" valign="middle">
              <?php $_SESSION['estimated_total'] = $_SESSION['a8tot']+$call_out_charge+$additional_charge?>
              <?php $_SESSION['invoiced_total'] = $_SESSION['a8tot']+$_SESSION['call_out_adjust']+$_SESSION['additional_adjust']?> 
              <?php $dif_total = $_SESSION['a10tot']+$call_out_dif?> 
            </td>
            <td align="left" valign="middle">&nbsp;</td>
            <td align="left" valign="middle">&nbsp;</td>
            <td align="right" valign="middle">&nbsp;</td>
        </tr>
          <tr>
            <td align="left" valign="middle">Totals:</td>
            <td align="right" valign="middle"><?php echo number_format($_SESSION['estimated_total'], 2, '.', ',') ?></td>
            <td align="right" valign="middle"><input type="text" name="actual_total_text" id="actual_total_text" value="<?php echo number_format($_SESSION['invoiced_total'], 2, '.', '') ?>" style="text-align:right; border:hidden;" class="text100" readonly></td>
            <td align="right" valign="middle"><input type="text" name="total_dif_text" id="total_dif_text" class="text100"  value="<?php echo number_format($dif_total, 2, '.', '') ?>" readonly style="text-align:right; border:hidden;"></td>
          </tr>
          <tr>
            <td align="left" valign="middle">&nbsp;</td>
            <td align="right" valign="middle">&nbsp;</td>
            <td align="right" valign="middle">&nbsp;</td>
            <td align="right" valign="middle">&nbsp;</td>
          </tr>
          <tr>
            <td align="left" valign="middle">Order value:
            <input type="hidden" name="order_value_text" id="order_value_text" value="<?php echo $order_value ?>">              <?php echo number_format($order_value, 2, '.', ',') ?></td>
            <td align="right" valign="middle"><?php $est_prof = (($order_value / $_SESSION['estimated_total']) *100)-100; echo number_format($est_prof, 2, '.', ',').'%'; ?> </td>
            <td align="right" valign="middle"><?php $act_prof = (($order_value / $_SESSION['invoiced_total']) *100)-100; ?><input type="text" name="act_prof_text" id="act_prof_text" value="<?php echo number_format($act_prof, 2, '.', ',')?> " readonly style="text-align:right; border:hidden;" class="text100"></td>
            <td align="left" valign="middle">%</td>
          </tr>
        </tfoot>
      </table>
        <br>
<br>
</div>
        
        
        <div class="wizard_frame">
      <table width="590" height="50" align="center" id="table_button_bar">
        <tr>
          <td width="117"><input type="button" name="lastprev_button" class="imagebutton" value="Prev"  onclick="needToConfirm = false;MM_showHideLayers('wait','','show');MM_goToURL('parent','maintenance_completeJobWizbase.php?s=&amp;p=1');return document.MM_returnValue" /></td>
          <td width="115">&nbsp;</td>
          <td width="99"><input name="p" type="hidden" id="p" value="2" />
            <input name="s" type="hidden" id="s" value="2" />
            </span></td>
          <td width="125" align="right"><input name="cancelbutton" type="button" class="imagebutton" id="cancelbutton"  onClick="needToConfirm = false; cancel_confirmation(1, '../maintenance/maintenance_main.php');" value="Cancel" /></td>
          <td width="110" align="right"><input type="submit" name="next_button" id="next_button" class="imagebutton" value="Next"  onClick="needToConfirm = false; MM_showHideLayers('wait','','show'); " /></td>
        </tr>
      </table>
      </div>
</form>
</div>

</body>      