<?php

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

include("config.inc.php");

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


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

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");


$querysetup ="select base1, base2, if(DefaultBase=1, base1, base2) as defaultcurr from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
} 
$mainaction  = $_POST["mainaction"]; 	if ($mainaction == ""){$mainaction = $_GET["mainaction"];}   
$newTable = $_POST["newTable"]; 	if ($newTable == ""){$newTable = $_GET["newTable"];}   

if($mainaction == '') $mainaction = 'insert';
if($newTable == '') $newTable = 1;

print <<< HERE
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 

<link rel="stylesheet" type="text/css" media="all" href="js/grid/UI_DataGrid_V1.css">
<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20101112" media="screen"></link>
<script type="text/javascript" src="dhtmlgoodies_calendar.js?random=20100118"></script>
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<script type="text/javascript" language="javascript">
function showlistitem(str,itemtype){
	window.open('select_item.php?item='+str+'&itemtype='+itemtype,'mywindow','width=900,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function AddItem(){
	var x = document.rmcalculate;
	if(x.itemcode.value==''){
		alert("Please Select an Item First!");
		return 0;
	} else {
		x.action = 'Form_RM_Calculation_action.php';
		x.submit();
	}
}

function removeItemRM(itemcode, caldt_id){
	
	if(confirm("Remove Item "+itemcode+" Completely?")){
	
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				document.getElementById('rmdetails').innerHTML=xmlhttp.responseText;		
			}
		  }
		  
		var fromdate = document.rmcalculate.fromdate.value; 
		var cur = document.getElementById("itemcur").value; 
		xmlhttp.open("GET","Form_RM_Calculation_action.php?Calculationdt_id="+caldt_id+"&fromdate="+fromdate+"&itemcur="+cur+"&actionmode=removeItemRM",true);
		xmlhttp.send();
	}
}


function updateQty(cal_id){
	
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('rmdetails').innerHTML=xmlhttp.responseText;		
		}
	  }
	var fromdate = document.rmcalculate.fromdate.value; 
	var cur = document.getElementById("itemcur").value;  
	var fgqty = document.getElementById("fgquantity"+cal_id).value;  
	xmlhttp.open("GET","Form_RM_Calculation_action.php?Calculation_id="+cal_id+"&fgqty="+fgqty+"&fromdate="+fromdate+"&itemcur="+cur+"&actionmode=updateQty",true);
	xmlhttp.send();

}


function removeFG(itemcode, cal_id){
	
	if(confirm("Remove Item "+itemcode+" Completely?")){
	
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				document.getElementById('rmdetails').innerHTML=xmlhttp.responseText;		
			}
		  }
		var fromdate = document.rmcalculate.fromdate.value; 
		var cur = document.getElementById("itemcur").value;
		xmlhttp.open("GET","Form_RM_Calculation_action.php?Calculation_id="+cal_id+"&fromdate="+fromdate+"&itemcur="+cur+"&actionmode=removeFG",true);
		xmlhttp.send();
	}
}

function clearPage(){
	var x = document.rmcalculate;
	if(confirm("Are You Sure You Want To Clear The Page?")){
		x.newTable.value=0;
		x.fromdate.value='';
		x.itemcur.value='';
		x.action = "Form_RM_Calculation.php";
		x.submit();
	}
	
}
function reloadgrid(){
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('rmdetails').innerHTML=xmlhttp.responseText;		
		}
	  }
	var fromdate = document.rmcalculate.fromdate.value; 
	var cur = document.getElementById("itemcur").value;
	xmlhttp.open("GET","Form_RM_Calculation_action.php?fromdate="+fromdate+"&itemcur="+cur+"&actionmode=reloadgrid",true);
	xmlhttp.send();
}
function printSummary(){
	if(document.rmcalculate.numrow.value=''){
		alert("Please Select an Item First!");
		return 0;
	} else {
		var fromdate = document.rmcalculate.fromdate.value;
		var cur = document.getElementById("itemcur").value;
		window.open("Rep_ItemsRM_Summary.php?fromdate="+fromdate+"&cur="+cur,"Rep_ItemsRM_Summary");
		
	}
}
</script>
<title>RM Items Summary</title>
<style type="text/css">
.style88 {
	font-size: 11px;
    font-weight: bold;
}

.style84 {
	font-size: 14px;
    font-family: sans-serif;
}
</style>

</head>
<body>
<form id='rmcalculate' name='rmcalculate' method='post' action=''>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='newTable' value='$newTable'>

HERE;


if($newTable == 0){
	
	$delete = "delete from RM_Calculationdt where Calculation_id in (select Calculation_id from RM_Calculation where user_code = '".$_SESSION['useraccount']."');";
	$res = mysql_query($delete,$connection);
	$delete1 = "delete from RM_Calculation where user_code = '".$_SESSION['useraccount']."' ;";
	$res = mysql_query($delete1,$connection);
	$newTable = 1;
}

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>RM Items Summary</span></td>
				<td valign='middle' style='height:26px;'>&nbsp;</td>
			</tr></table>
		</td>
	  </tr>";

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'>
				<button type='button' id='btnClear' name='btnClear' onclick=\"clearPage();return false;\" title='Clear Page'><span><span>Clear</span></span></button>
				<button type='button' id='btnPrint' name='btnPrint' onclick=\"printSummary();return false;\" title='Print Summary'><span><span>Print Summary</span></span></button>
				</td>";
echo"		</tr>";
echo"</table>";
echo"</td></tr>";

//add Item header
echo"<tr><td id='content-body'>
	<table border='0'  align='center' valign='top' class='tablecontent'>
	<tr bgcolor='#F5F5F5'><td valign='top' align='left'>
		<table border='0' style='table-layout:fixed' width='600px'>
		  <col width=30><col width=380><col width=120><col width=100>";	

if($fromdate =='') $fromdate = date('d/m/Y');		  
echo" <tr height='30'><td></td>";	  
echo"   <td><span class='style88'>Date &nbsp;</span><input type='text' name='fromdate' value='$fromdate' maxlength='10'style='width:80px;' onchange=\"reloadgrid();\">";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.rmcalculate.fromdate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span></td>";
echo "<td colspan='2'><span class='style88' >Currency &nbsp;</span>";
echo "<select name='itemcur' id='itemcur' onchange=\"reloadgrid();\">";
if($itemcur =='') $itemcur = $defaultcurr;
echo "<option value='$base1' ".($base1==$itemcur?'selected':'')." >$base1</option>";
echo "<option value='$base2' ".($base2==$itemcur?'selected':'')." >$base2</option>";
echo "</select>"; 
echo "</td>";
echo" </tr>";
		  
echo" <tr height='30'><td></td>";	  
echo"   <td><span class='style88'>Finished Item Code / Description</span></td><td><span class='style88'>Quantity</span></td><td></td>";
echo" </tr>";

echo" <tr height='30'><td></td>";	  
echo"   <td><span class='style88'>";
echo"   <label>
        <input name='itemcode' type='text' id='itemcode' value='".$itemcode."' style='width:150px;'/>
        <input name='itemdesc' type='text' id='itemdesc' value='".$itemdesc."' style='width:200px;' />
        <input name='btncode1' type='hidden' id='btncode1' value='...' style='width:20px; height:20px; border:thin;' title='Select Item From' />
        </label>
      </span>
      <label><span class='style11'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Item From ' 
             onClick=\"showlistitem(10,'productionFG'); \"><img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label> ";
echo" </td>";
echo" <td><input type=text' name='fgqty' id='fgqty' value='$fgqty' style='width:110px;'></td>";
echo" <td><button type='button' id='btnAdd' name='btnAdd' onclick=\"AddItem();return false;\" title='Add Item'><span><span>Add Item</span></span></button></td>";
echo"</tr>";
echo "<tr height='15px'><td></td></tr>";
$query = "select Calculation_id, item_codefg,item_description, qty
		from RM_Calculation
		inner join items on items.item_code = item_codefg
		order by Calculation_id";
$res = mysql_query($query,$connection);
$numrow = mysql_num_rows($res);
echo "<input type='hidden' name='numrow' value='$numrow'>";
while($rw = mysql_fetch_row($res)){
	
	echo "<tr>";
	echo "<td><img src='img/delete.png' onclick=\"removeFG('$rw[1]','$rw[0]');return false;\" title='Update Quantity'></td>";
	echo "<td><span class='style84'>$rw[1] - $rw[2]</span></td>";
	echo "<td><input type='text' name='fgquantity$rw[0]' id='fgquantity$rw[0]'  value='$rw[3]' style='width:110px;'></td>";
	echo "<td><input type='button' id='imgSubmit[0]' name='imgSubmit[0]' onclick=\"updateQty('$rw[0]');return false;\" title='Update Quantity' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)'></td>";
	echo "</tr>";
}	
echo"   </table></td></tr>"; 
echo "<tr height='25px'><td></td></tr>";

//detail
echo"<tr bgcolor='#F5F5F5'><td valign='top' align='left'>
		<table border='0' style='table-layout:fixed' width='570px'>
		  <col width=30><col width=350><col width=90><col width=100>";	

echo" <tr height='30'><td></td>";	  
echo"   <td><span class='style88'>Raw Material Code / Description</span></td><td><span class='style88'>Quantity</span></td><td><span class='style88'>Cost</span></td>";
echo" </tr>";
echo"   </table></td></tr>"; 

echo"<tr bgcolor='#F5F5F5'><td valign='top' align='left'><div id='rmdetails' style=''>";
getDetail($fromdate,$itemcur,$base1,$base2,$connection);
echo "</div></td></tr>"; 

echo "<tr height='25px'><td></td></tr>";
echo"</table></td></tr>"; 



echo" <tr height='30'><td align='center' id='content-bottom' ></td></tr>";  
echo" </table>  ";//end of last table menu


function getDetail($date,$cur,$base1,$base2,$connection){
	echo "<table border='0' style='table-layout:fixed' width='570px'>
		  <col width=30><col width=350><col width=90><col width=100>";	
	$ndate = explode('/',$date); 	  
	$date = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	$query = "select Calculationdt_id, Calculation_id, item_coderm,items.item_description, qty, 
			if('$cur'='$base1',MeanCostCur(item_coderm,'$date', 1, '$cur','$cur') , MeanCostCur(item_coderm,'$date', 2, '$cur','$cur') ) 
			from RM_Calculationdt
			inner join items on items.item_code = item_coderm
			order by Calculationdt_id, Calculation_id ;";// echo $query;
	$res = mysql_query($query,$connection);

while($rw = mysql_fetch_row($res)){
	
	echo" <tr height='30'><td><input type='checkbox' name='chk$rw[0]' id='chk$rw[0]' value='1' checked onclick=\"removeItemRM('$rw[2]','$rw[0]');\"></td>";	  
	echo" <td><span class='style84'>$rw[2] - $rw[3]</td>";
	echo" <td><span class='style84'>$rw[4]</span></td>";
	echo" <td><span class='style84'>".number_format($rw[5],2)."</span></td>";
	echo"</tr>";
}
echo"   </table>";
	
}
echo"  
</form>
</body>
</html>";


?>