<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php

session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}


print <<< HERE
<HTML>
<HEAD>
<TITLE> Variance Analysis Report </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<style type="text/css">
table.params td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }

table.master th { font-size: 13px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
table.master td { font-size: 13px; 
                  font-family:Times-New-Roman;
                 }
				 

table.detail th { font-size: 12px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                 }				 			   
table.detail td { font-size: 11px; 
				  font-weight: normal;
                  font-family:Times-New-Roman;
                }			   
				 
.totals { font-size: 12px; 
          font-weight: bold;
          font-family:Times-New-Roman;
        }					  
				 
h4 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
   }
h5 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }   
   
</style>




</HEAD>

HERE;



include("config.inc.php");

//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

$querylog="SELECT print as printaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')	";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}


if ($printaccess==0 or $printaccess=='')
echo'<BODY oncontextmenu="return false;">';
else
echo'<BODY>';


$querysetup="select grid_color1,grid_color2, DecimalBase, DecimalBase1, DecimalBase2, Base1, Base2, Company_Name As CompanyName, ifNull(ListOfProductionMaskQty,0) As ListOfProductionMaskQty from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultsetup)){
  extract($rows);
}


$querylog=" select ifNull(FI_RepRestrictProject,0) As FI_RepRestrictProject, ifNull(FI_RepRestrictCostcenter,0) As FI_RepRestrictCostcenter, ".
		  " ifNull(FI_RepRestrictWarehouse,0) As FI_RepRestrictWarehouse, ifNull(FI_ShowCostPrice,0) As FI_ShowCostPrice  ".
          " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."') ";
$resultlog=mysql_query($querylog) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%') ".
			   " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		

$querywh=" select distinct warehouse.warehouse_code, warehouse_name ".
         " from warehouse where warehouse_code in ( select warehouse_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
		 "                                          where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
		 "                                          and (loginstk.warehouse_code is not null and loginstk.warehouse_code != '') ) ".
		 " order by warehouse.warehouse_code";
$resultwh = mysql_query($querywh,$connection) or die(mysql_error());
$whexist  = mysql_num_rows($resultwh);


$querygroup=" select distinct group_code,group_name ".
			" from grouping where group_code in (select group_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."') ".
			" order by group_code";
$resultgroup = mysql_query($querygroup,$connection) or die(mysql_error());
$groupexist  = mysql_num_rows($resultgroup);

$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);

$ReportTitle = 'Variance Analysis Report'; 

print <<< HERE
<table width="100%" border="0" align="center">
<tr><td>
<table width='100%' border='0' align='left' style='table-layout:fixed'>
<tr>
  <td width='25%'><b>$CompanyName</b></td>
  <td width='50%' align='center'><font size=4><b>$ReportTitle</b></font></td>
  <td width='10%'><font size=1><b> Printed Date: </b> <br><b>Time: </b>
  <td width='15%'><font size=1>
HERE;

echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
//echo DATE("d"."/"."m"."/"."Y");
echo "<br>".$dt[1]." ".$dt[2];

if (empty($curr))  $curr = $Base1;


$SelectedOptions = "Filter by: <br>";
if ($fromdate != '')    $SelectedOptions .= "-> Date From: ". $fromdate . "  To: ". $todate ."<br>";	
if ($curr != '')        $SelectedOptions .= "-> Currency: ". $curr ."<br>";	
if ($fromitemRM != '')  $SelectedOptions .= "-> Item From: ". $fromitemRM . "  To: ". $toitemRM ."<br>";


if (!empty($fromdate))  $fromdate = explodeword($fromdate);
if (!empty($todate))    $todate = explodeword($todate);  

$fromitemRM    = GetSQLValueString($fromitemRM, 'text');
$toitemRM      = GetSQLValueString($toitemRM, 'text');
$fromdate  	   = GetSQLValueString($fromdate, 'date');
$todate        = GetSQLValueString($todate, 'date');     
$fromproject   = GetSQLValueString($fromproject, 'text');
$toproject     = GetSQLValueString($toproject, 'text');
$fromcostcent  = GetSQLValueString($fromcostcent, 'text');
$tocostcent    = GetSQLValueString($tocostcent, 'text');
$fromwarehouse = GetSQLValueString($fromwarehouse, 'text');
   

if (empty($showdetail)) $showdetail = 0;
if (empty($showcost))   $showcost = 0;

$masterclass = 'master';   

print <<<HERE
</font></td>
</tr>
<tr height='5'><td colspan=4><h5></td></tr>
</table>
</td></tr>

<tr><td>
<table class="params" width="100%" border="0" align="center" >
<tr>
  <td width="100%" valign='top'><b>$SelectedOptions</b></td>
</tr>
<tr>
   <td width='100%'> <hr width='100%' color='black' size='1'> </td>
</tr>
</table>
</td></tr>

<tr><td>
HERE;



echo "<table width='860' border='0' align='left' class=$masterclass style='table-layout:fixed'>
	  <col width=150>
	  <col width=120>
	  <col width=120>
	  <col width=120>
	  <col width=120>
	  <col width=140>
	  <col width=120>
	  <col width=120>
	  <col width=100>";

		
		 


$query = '';

$QueryRestrictPrj = " Inner join ( select distinct project_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
					" 			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
					"              and (project_code is not null and project_code != '') ".
					"			 ) Q4  ";
					
$QueryRestrictCst = " Inner join ( select distinct costcent_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
					"			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
					"              and (costcent_code is not null and costcent_code != '') ".
					"			 ) Q5  ";
					
					


$query = " Select ProdRecipeRM.Item_Code as ItemCodeRM, ItemsRM.Item_Description as ItemNameRM, ProdRecipeRM.ProdRecipeRM_Quantity/ProdRecipeFG.ProdRecipeFG_Quantity as ProdRecipeRM_Quantity, ItemsRM.Item_UnitSalesDesc as ItemUnitRM, ".
		 " ProdRecipeFG.Item_Code as ItemCodeFG, ItemsFG.Item_Description as ItemNameFG, ProdRecipeFG.ProdRecipeFG_Quantity, ItemsFG.Item_UnitSalesDesc as ItemUnitFG, ".
		 " ifNull(QItemQtyFG.QuantityFG,0) as QuantityFG, ifNull(QItemQtyRM.QuantityRM,0) as QuantityRM, ".
		 " (ifNull(QItemQtyFG.QuantityFG,0)*ProdRecipeRM.ProdRecipeRM_Quantity/ProdRecipeFG.ProdRecipeFG_Quantity) as ItemStdUsgRM ".
         " From ProdRecipeRM ".
		 " Inner Join ProdRecipeFG on ProdRecipeFG.ProdRecipe_Id = ProdRecipeRM.ProdRecipe_Id ".
		 " Inner Join Items as ItemsRM on ItemsRM.Item_Code = ProdRecipeRM.Item_Code ".
		 " Inner Join Items as ItemsFG on ItemsFG.Item_Code = ProdRecipeFG.Item_Code ".
		 " Left Join (   ".
		 " 		Select StkTransaction.Item_code As ItemCodeFG, Sum(ifNull(QuantityProduceIn,0) + ifNull(QuantityEntered,0)) As QuantityFG ".
		 "   	From StkTransaction ".
		 "		Inner Join Items on Items.Item_Code = StkTransaction.Item_Code ";
					  
		if ($projectexist >	0 && $FI_RepRestrictProject == 1)
			$query = $query.$QueryRestrictPrj." on StkTransaction.project_code like concat(Q4.project_code,'%') ";
									 
		if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
			$query = $query.$QueryRestrictCst." on StkTransaction.Costcent_Code like concat(Q5.costcent_code,'%') ";		 
			 

$query = $query." Where ifNull(StkTransaction.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(StkTransaction.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(StkTransaction.Project_Code,'-1')) ".
				" And ifNull(StkTransaction.Costcent_Code,'-1') between ifNull(".$fromcostcent.",ifNull(StkTransaction.Costcent_Code,'-1')) and ifNull(". $tocostcent.",ifNull(StkTransaction.Costcent_Code,'-1')) ".
				" And ifNull(StkTransaction.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(StkTransaction.Warehouse_Code,'-1')) ".
				" And Date_format(StkTransaction.Transaction_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(StkTransaction.Transaction_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(StkTransaction.Transaction_Date,'%Y-%m-%d'))  ".
				" And Upper(Transaction_Type) In ('AI','PD') ";
						  
		if ($whexist > 0 && $FI_RepRestrictWarehouse == 1)
			$query = $query." And StkTransaction.warehouse_code In (select distinct warehouse_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."')  ";  
		if ($groupexist > 0)
			$query = $query." And Items.group_code In (select distinct group_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."')  ";  

$query = $query." Group By ItemCodeFG  ";
$query = $query." ) as QItemQtyFG on QItemQtyFG.ItemCodeFG = ProdRecipeFG.Item_Code ".
				" Left Join (   ".
				" 		Select StkTransaction.Item_code As ItemCodeRM, Sum(ifNull(QuantityProduceOut,0) + ifNull(QuantityDelivered,0)) As QuantityRM ".
				"   	From StkTransaction ".
				"		Inner Join Items on Items.Item_Code = StkTransaction.Item_Code ";
					  
		if ($projectexist >	0 && $FI_RepRestrictProject == 1)
			$query = $query.$QueryRestrictPrj." on StkTransaction.project_code like concat(Q4.project_code,'%') ";
									 
		if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
			$query = $query.$QueryRestrictCst." on StkTransaction.Costcent_Code like concat(Q5.costcent_code,'%') ";		 
			 

$query = $query." Where StkTransaction.Item_Code Between ifNull(".$fromitemRM.", StkTransaction.Item_Code) and ifNull(". $toitemRM.", StkTransaction.Item_Code) ".
				" And ifNull(StkTransaction.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(StkTransaction.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(StkTransaction.Project_Code,'-1')) ".
				" And ifNull(StkTransaction.Costcent_Code,'-1') between ifNull(".$fromcostcent.",ifNull(StkTransaction.Costcent_Code,'-1')) and ifNull(". $tocostcent.",ifNull(StkTransaction.Costcent_Code,'-1')) ".
				" And ifNull(StkTransaction.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(StkTransaction.Warehouse_Code,'-1')) ".
				" And Date_format(StkTransaction.Transaction_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(StkTransaction.Transaction_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(StkTransaction.Transaction_Date,'%Y-%m-%d'))  ".
				" And Upper(Transaction_Type) In ('AR','PDT','PDO') ";
						  
		if ($whexist > 0 && $FI_RepRestrictWarehouse == 1)
			$query = $query." And StkTransaction.warehouse_code In (select distinct warehouse_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."')  ";  
		if ($groupexist > 0)
			$query = $query." And Items.group_code In (select distinct group_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."')  ";  

$query = $query." Group By ItemCodeRM  ";
$query = $query." ) as QItemQtyRM on QItemQtyRM.ItemCodeRM = ProdRecipeRM.Item_Code ";
$query = $query." Where ProdRecipeRM.Item_Code Between ifNull(".$fromitemRM.", ProdRecipeRM.Item_Code) and ifNull(". $toitemRM.", ProdRecipeRM.Item_Code) ";
$query = $query." Order By ItemCodeRM, ItemCodeFG ";

				

//echo $query;
//exit;



$ArrayTotalByMaster = Array();
$ArrayItemRM = Array();
$ArrayData   = Array();

$MasterItemCode = '';
$TotalItemUsgStdRM = 0;

$result=mysql_query($query,$connection) or die(mysql_error());
while($rows=mysql_fetch_row($result)){

	
	$ItemRMFound = 0;
	$CountRM = count ($ArrayItemRM['Code']); 
	for ($j=0; $j<$CountRM; $j++){
		if ($ArrayItemRM['Code'][$j] == $rows[0])
			$ItemRMFound = 1;
	}
							
	if ($ItemRMFound == 0) { 
		$ArrayItemRM['Code'][$CountRM] = $rows[0];
		$ArrayItemRM['Name'][$CountRM] = $rows[1];
		$ArrayItemRM['Unit'][$CountRM] = $rows[3];
	
		$TotalItemUsgStdRM = 0;
	}
	
	$ItemQtyStdRM   = $rows[2];
	$ItemQtyActFG   = $rows[8];
	$ItemQtyActRM   = $rows[9];
	$ItemUsgStdRM   = $rows[10];
	
	$TotalItemUsgStdRM = $TotalItemUsgStdRM + $ItemUsgStdRM;
	
	
	
	$CountFG = count ($ArrayData[$rows[0]]['ItemCodeFG']); 
	
	$ArrayData[$rows[0]]['ItemCodeFG'][$CountFG]   = $rows[5];
	$ArrayData[$rows[0]]['ItemQtyStdRM'][$CountFG] = $ItemQtyStdRM;
	$ArrayData[$rows[0]]['ItemQtyActFG'][$CountFG] = $ItemQtyActFG;
	$ArrayData[$rows[0]]['ItemQtyActRM'][$CountFG] = $ItemQtyActRM;
	$ArrayData[$rows[0]]['ItemUsgStdRM'][$CountFG] = $ItemUsgStdRM;
	$ArrayData[$rows[0]]['TotalItemUsgStdRM'][$CountFG] = $TotalItemUsgStdRM;
	

}

array_multisort($ArrayItemRM);

$CountItemRM = count($ArrayItemRM['Code']);
for ($i=0; $i<$CountItemRM; $i++){

	unset($ArrayTotalByMaster);
	CreateTableHeader($ArrayItemRM['Name'][$i],$ArrayItemRM['Unit'][$i]);

	$CountData = count($ArrayData[$ArrayItemRM['Code'][$i]]['ItemCodeFG']);
	for ($j=0; $j<$CountData; $j++){
	
		$ItemQtyStdRM 	   = $ArrayData[$ArrayItemRM['Code'][$i]]['ItemQtyStdRM'][$j];
		$ItemQtyActFG 	   = $ArrayData[$ArrayItemRM['Code'][$i]]['ItemQtyActFG'][$j];
		$ItemUsgStdRM 	   = $ArrayData[$ArrayItemRM['Code'][$i]]['ItemUsgStdRM'][$j];
		$ItemQtyActRM 	   = $ArrayData[$ArrayItemRM['Code'][$i]]['ItemQtyActRM'][$j];
		$TotalItemUsgStdRM = $ArrayData[$ArrayItemRM['Code'][$i]]['TotalItemUsgStdRM'][$CountData-1];
		$ItemQtyStdFG	   = ($ItemQtyActRM/$TotalItemUsgStdRM)*$ItemQtyActFG;
		$ItemUsgActRM	   = $ItemQtyStdRM*$ItemQtyStdFG;
		$ItemVarianceFG	   = $ItemQtyActFG - $ItemQtyStdFG;
		$ItemVarianceRM    = ($ItemVarianceFG*$ItemQtyStdRM)/1000;
		$ItemEfficiency	   = $ItemUsgStdRM - $ItemUsgActRM;
	
		echo "<tr>"; 
		echo "	<td align='left' valign='top' >". $ArrayData[$ArrayItemRM['Code'][$i]]['ItemCodeFG'][$j] ."</td>";
		echo "	<td align='right' valign='top'>".format_currency($ItemQtyStdRM, $ListOfProductionMaskQty)."</td>";
		echo "	<td align='right' valign='top'>".format_currency($ItemQtyActFG, $ListOfProductionMaskQty)."</td>";
		echo "	<td align='right' valign='top'>".format_currency($ItemUsgStdRM, $ListOfProductionMaskQty)."</td>";
		echo "	<td align='right' valign='top'>".format_currency($ItemQtyStdFG, $ListOfProductionMaskQty)."</td>";
		echo "	<td align='right' valign='top'>".format_currency($ItemUsgActRM, $ListOfProductionMaskQty)."</td>";
		echo "	<td align='right' valign='top'>".format_currency($ItemVarianceFG, $ListOfProductionMaskQty)."</td>";
		echo "	<td align='right' valign='top'>".format_currency($ItemVarianceRM, $ListOfProductionMaskQty)."</td>";
		echo "	<td align='right' valign='top'>".format_currency($ItemEfficiency, $ListOfProductionMaskQty)."</td>";
		echo "</tr>";
		
		$ArrayTotalByMaster['TotalUsgStdRM']   = $ArrayTotalByMaster['TotalUsgStdRM'] + $ItemUsgStdRM;
		$ArrayTotalByMaster['TotalQtyStdFG']   = $ArrayTotalByMaster['TotalQtyStdFG'] + $ItemQtyStdFG;
		$ArrayTotalByMaster['TotalUsgActRM']   = $ArrayTotalByMaster['TotalUsgActRM'] + $ItemUsgActRM;
		$ArrayTotalByMaster['TotalVarianceFG'] = $ArrayTotalByMaster['TotalVarianceFG'] + $ItemVarianceFG;
		$ArrayTotalByMaster['TotalVarianceRM'] = $ArrayTotalByMaster['TotalVarianceRM'] + $ItemVarianceRM;
	}
	
	echo "<tr class='totals'>"; 
	echo "	<td align='left' valign='top' >TOTAL</td>";
	echo "	<td align='right' valign='top' colspan='2'>&nbsp;</td>";
	echo "	<td align='right' valign='top'>".format_currency($ArrayTotalByMaster['TotalUsgStdRM'], $ListOfProductionMaskQty)."</td>";
	echo "	<td align='right' valign='top'>".format_currency($ArrayTotalByMaster['TotalQtyStdFG'], $ListOfProductionMaskQty)."</td>";
	echo "	<td align='right' valign='top'>".format_currency($ArrayTotalByMaster['TotalUsgActRM'], $ListOfProductionMaskQty)."</td>";
	echo "	<td align='right' valign='top'>".format_currency($ArrayTotalByMaster['TotalVarianceFG'], $ListOfProductionMaskQty)."</td>";
	echo "	<td align='right' valign='top'>".format_currency($ArrayTotalByMaster['TotalVarianceRM'], $ListOfProductionMaskQty)."</td>";
	echo "	<td align='right' valign='top'>&nbsp;</td>";
	echo "</tr>";
	
	echo "<tr height='10'>"; 
	echo "	<td align='left' valign='top' colspan='9'>&nbsp;</td>";
	echo "</tr>";
	
	echo "<tr class='totals'>"; 
	echo "	<td align='left' valign='top' >ACTUAL USAGE OF ".$ArrayItemRM['Name'][$i]."</td>";
	echo "	<td align='right' valign='top' colspan='2'>&nbsp;</td>";
	echo "	<td align='right' valign='top'>".format_currency($ItemQtyActRM, $ListOfProductionMaskQty)."</td>";
	echo "	<td align='right' valign='top' colspan='5'>&nbsp;</td>";
	echo "</tr>";
	
	echo "<tr height='50'>"; 
	echo "	<td align='left' valign='top' colspan='9'>&nbsp;</td>";
	echo "</tr>";
}


			
echo "</table></td></tr>";
echo "</table>";


function format_currency($amount, $DecimalBase) { 

    if (Round(abs($amount), $DecimalBase) == 0)
		return number_format(abs($amount), $DecimalBase); 
	else if($amount < 0) 
        return "(".number_format(abs($amount),$DecimalBase).")"; 
    else 
		return number_format($amount, $DecimalBase); 
} 

function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}

function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}

function GetSQLValueString($theValue, $theType) 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "Null";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "Null";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;
  }
  return $theValue;
}

function CreateTableHeader($PItemCodeRM,$PItemUnitRM) {
	
	echo"  <tr>";
	echo"	<th align='left'>$PItemCodeRM</th>";
	echo"	<th align='center'>Std $PItemCodeRM Content Per Unit [$PItemUnitRM]</th>";
	echo"	<th align='center'>Actual Output [Units]</th>";
	echo"	<th align='center'>Std $PItemCodeRM Usg [$PItemUnitRM]</th>";
	echo"	<th align='center'>Std Output [Units]</th>";
	echo"	<th align='center'>Actual $PItemCodeRM usg based on Units of Prod [$PItemUnitRM]</th>";
	echo"	<th align='center'>Production Variance [Units]</th>";
	echo"	<th align='center'>$PItemCodeRM Usg Variance [$PItemUnitRM]</th>";
	echo"	<th align='center'>Efficiency</th>";
	echo"  </tr>";
		  
	echo"<tr height='5'><td colspan=9><h4></td></tr>";

}

if ($printaccess==1)
print <<< HERE

<script language=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
}
</script>
</BODY>
</HTML>
HERE;

?>