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

$table_name = $_SESSION['db_prefix']."_compiled_jobs";
$charges_query = "SELECT value, labour_charge, labour_charge_final, travel_expense, travel_expense_final, accomodation_expense, accomodation_expense_final, mworks_charge, mworks_charge_final, captured, compiled, invoiced     FROM $table_name WHERE job_card_no =  '".$_SESSION['job_card_no']."' ";

$charges_result = mySQL_query($charges_query);
$charges_num = mysql_num_rows($charges_result);
$labour_charge = mysql_result($charges_result, 0, "labour_charge");
$travel_expense = mysql_result($charges_result, 0, "travel_expense");
$accomodation_expense = mysql_result($charges_result, 0, "accomodation_expense");
$mworks_charge = mysql_result($charges_result, 0, "mworks_charge");
$order_value = mysql_result($charges_result, 0, "value");

$table_name = $_SESSION['db_prefix']."_order_job_lines";
$line_query = "SELECT *  FROM $table_name WHERE  job_card_no =  '".$_SESSION['job_card_no']."' ";
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);

?>
<script>
function est_total() {
	
	total_value = parseFloat(document.getElementById('parts_total_text').value) +  parseFloat(document.getElementById('labour_text').value) + parseFloat(document.getElementById('travel_text').value) + parseFloat(document.getElementById('accomodation_text').value) + parseFloat(document.getElementById('mworks_charge_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('labour_dif_text').value) + parseFloat(document.getElementById('travel_dif_text').value) + parseFloat(document.getElementById('accomodation_dif_text').value) + parseFloat(document.getElementById('mworks_charge_dif_text').value) + parseFloat(document.getElementById('adjustment_dif_text').value);
   document.getElementById('total_dif_text').value = parseFloat(dif_value).toFixed(2);
   if (total!=0) {
   act_prof = ((document.getElementById('order_value_text').value / total_value)*100)-100;
   }
   else {
   act_prof = 0;
   }
   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 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;
				$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;
					 				 
				 ?>
          <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">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="left" valign="top">&nbsp;</td>
            <td align="right" valign="top"><?php $_SESSION['a4tot']=$a4tot; echo number_format($_SESSION['a4tot'], 2, '.', ',') ?></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, '.', ',') ?>&nbsp;</td>
            <td align="right" valign="top">&nbsp;</td>
        </tr>
        </tfoot>
      </table>
      <?php
	  $_SESSION['a10tot'] = $_SESSION['a8tot'] - $_SESSION['a4tot'];
	  ?>
        <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['a4tot'], 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">Labour charge:</td>
            <td align="right" valign="middle"><input type="hidden" name="labour_est_text" id="labour_est_text"   value="<?php echo $labour_charge?> "><?php if ($labour_charge!='') {echo number_format($labour_charge, 2, '.', ',');} ?></td>
            <td align="right" valign="middle"><input type="text" name="labour_text" id="labour_text" class="text100" onKeyPress="return isNumberKey(event)" onKeyUp=" this.value = this.value.replace (/(\...).*/, '$1')" style="text-align:right" value="<?php echo $_SESSION['labour_adjust'] ?>" autocomplete="off" onBlur="if (this.value=='') {this.value=0}; new_dif = document.getElementById('labour_text').value - document.getElementById('labour_est_text').value; document.getElementById('labour_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 $labour_dif = $_SESSION['labour_adjust'] - $labour_charge; ?>  <input type="text" name="labour_dif_text" id="labour_dif_text" value="<?php echo number_format($labour_dif, 2, '.', '')?>" readonly style="text-align:right; border:hidden;" ></td>
        </tr>
          <tr>
            <td valign="middle">Travel expense:</td>
            <td align="right" valign="middle"><input type="hidden" name="travel_est_text" id="travel_est_text" value="<?php echo $travel_expense?> "> <?php if ($travel_expense!='') {echo number_format($travel_expense, 2, '.', ',');} ?></td>
            <td align="right" valign="middle"><input type="text" name="travel_text" id="travel_text" class="text100" onKeyPress="return isNumberKey(event)" onKeyUp=" this.value = this.value.replace (/(\...).*/, '$1')" style="text-align:right" value="<?php echo $_SESSION['travel_adjust'] ?>" autocomplete="off" onBlur="if (this.value=='') {this.value=0}; new_travel_dif = document.getElementById('travel_text').value - document.getElementById('travel_est_text').value; document.getElementById('travel_dif_text').value=parseFloat(new_travel_dif).toFixed(2); this.value = parseFloat(this.value).toFixed(2); est_total();" onfocus = "this.select();"></td>
            <td align="right" valign="middle"><?php $travel_dif = $_SESSION['travel_adjust'] - $travel_expense; ?>
            <input type="text" name="travel_dif_text" id="travel_dif_text" value="<?php echo number_format($travel_dif, 2, '.', '')?>" readonly style="text-align:right; border:hidden;" ></td>
          </tr>
          <tr>
            <td valign="middle">Accomodation expense:</td>
            <td align="right" valign="middle"><input type="hidden" name="accomodation_est_text" id="accomodation_est_text" value="<?php echo $accomodation_expense?> ">              <?php if ($accomodation_expense!='') {echo number_format($accomodation_expense, 2, '.', ',');} ?></td>
            <td align="right" valign="middle"><input type="text" name="accomodation_text" id="accomodation_text" class="text100" onKeyPress="return isNumberKey(event)" onKeyUp=" this.value = this.value.replace (/(\...).*/, '$1')" style="text-align:right" value="<?php echo $_SESSION['accomodation_adjust'] ?>" autocomplete="off" onBlur="if (this.value=='') {this.value=0}; new_accomodation_dif = document.getElementById('accomodation_text').value - document.getElementById('accomodation_est_text').value; document.getElementById('accomodation_dif_text').value=parseFloat(new_accomodation_dif).toFixed(2); this.value = parseFloat(this.value).toFixed(2); est_total();" onfocus = "this.select();"></td>
            <td align="right" valign="middle"><?php $accomodation_dif = $_SESSION['accomodation_adjust'] - $accomodation_expense; ?>
            <input type="text" name="accomodation_dif_text" id="accomodation_dif_text" value="<?php echo number_format($accomodation_dif, 2, '.', '')?>" readonly style="text-align:right; border:hidden;" ></td>
          </tr>
          <tr>
            <td valign="middle">M-works job:</td>
            <td align="right" valign="middle"><input type="hidden" name="mworks_charge_est_text" id="mworks_charge_est_text" value="<?php echo $mworks_charge?> ">              <?php if ($mworks_charge!='') {echo number_format($mworks_charge, 2, '.', ',');} ?></td>
            <td align="right" valign="middle"><input type="text" name="mworks_charge_text" id="mworks_charge_text" class="text100" onKeyPress="return isNumberKey(event)" onKeyUp=" this.value = this.value.replace (/(\...).*/, '$1')" style="text-align:right" value="<?php echo $_SESSION['mworks_charge_adjust'] ?>" autocomplete="off" onBlur="if (this.value=='') {this.value=0};
            new_mworks_charge_dif = document.getElementById('mworks_charge_text').value - document.getElementById('mworks_charge_est_text').value; document.getElementById('mworks_charge_dif_text').value=parseFloat(new_mworks_charge_dif).toFixed(2); this.value = parseFloat(this.value).toFixed(2); est_total();" onfocus = "this.select();"></td>
            <td align="right" valign="middle"><?php $mworks_charge_dif = $_SESSION['mworks_charge_adjust'] - $mworks_charge; ?>
            <input type="text" name="mworks_charge_dif_text" id="mworks_charge_dif_text" value="<?php echo number_format($mworks_charge, 2, '.', '')?>" readonly style="text-align:right; border:hidden;" ></td>
          </tr>
         <tfoot>
          <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['a4tot']+$labour_charge+$travel_expense+$accomodation_expense+$mworks_charge?>
              <?php $_SESSION['invoiced_total'] = $_SESSION['a8tot']+$_SESSION['labour']+$_SESSION['travel']+$_SESSION['accomodation']+$_SESSION['mworks_charge']?> 
              <?php $dif_total = $_SESSION['a10tot']+$labour_dif+$travel_dif+$accomodation_dif+$mworks_charge_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;" readonly class="text100"></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 if ($_SESSION['estimated_total']>0) {$est_prof = (($order_value / $_SESSION['estimated_total']) *100)-100; echo number_format($est_prof, 2, '.', ',').'%';} else { echo '0.00'; } ?></td>
            <td align="right" valign="middle"><?php if ($_SESSION['invoiced_total']>0) {$act_prof = (($order_value / $_SESSION['invoiced_total']) *100)-100;} else { $act_prof=0; } ?>
              <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','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, '../orders/orders_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>
<!--form validation-->
<script language="JavaScript" type="text/javascript" xml:space="preserve">

  var frmvalidator  = new Validator("form1");
  frmvalidator.EnableMsgsTogether();
  frmvalidator.addValidation("forman_select","req","Please select forman for this project");
 
  
</script>
</body>      