<?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"];}   
$actionmode  	= $_POST["actionmode"]; 	if ($actionmode == ""){		$actionmode 	= $_GET["actionmode"];}   
$itemcode  	 	= $_POST["itemcode"]; 		if ($itemcode == ""){		$itemcode 		= $_GET["itemcode"];}   
$fgqty  	 	= $_POST["fgqty"]; 			if ($fgqty == ""){			$fgqty 			= $_GET["fgqty"];}   
$fromdate  	 	= $_POST["fromdate"]; 		if ($fromdate == ""){		$fromdate 		= $_GET["fromdate"];}   
$itemcur  	 	= $_POST["itemcur"]; 		if ($itemcur == ""){			$itemcur 	= $_GET["itemcur"];}   

$Calculation_id = $_POST["Calculation_id"]; 		if ($Calculation_id == ""){$Calculation_id = $_GET["Calculation_id"];}   
$Calculationdt_id = $_POST["Calculationdt_id"]; 		if ($Calculationdt_id == ""){$Calculationdt_id = $_GET["Calculationdt_id"];}   


if($mainaction == 'insert'){
	$maxid = "select ifnull(max(Calculation_id),0) + 1 from RM_Calculation";
	$res = mysql_query($maxid,$connection);
	$row = mysql_fetch_row($res);
	$Calculation_id = $row[0];
	
	$insert = "insert into RM_Calculation (Calculation_id,item_codefg, qty, user_code) values ($Calculation_id, '$itemcode',$fgqty, '".$_SESSION['useraccount']."')";
	$res  = mysql_query($insert,$connection);
	
	$maxid = "select ifnull(max(Calculationdt_id),0) + 1 from RM_Calculationdt";
	$res = mysql_query($maxid,$connection);
	$row = mysql_fetch_row($res);
	$Calculationdt_id = $row[0];
	
	$query  = " select packdt.item_codepackdt,ifnull(packdt_quantity,0)*$fgqty from packdt where packdt.item_code = '$itemcode' ";
	$result = mysql_query($query,$connection);
	while($rw = mysql_fetch_row($result)){
		$insert2 = " insert into RM_Calculationdt (Calculationdt_id, Calculation_id, item_coderm, qty) values ".
				   " ($Calculationdt_id, $Calculation_id, '$rw[0]', $rw[1] ) ;";
		$res  = mysql_query($insert2,$connection);
		$Calculationdt_id = $Calculationdt_id + 1;
	}
}

if($actionmode == 'removeItemRM'){
	
	$delete = "delete from RM_Calculationdt where Calculationdt_id= $Calculationdt_id;";
	$res = mysql_query($delete,$connection);
	getDetail($fromdate,$itemcur,$base1,$base2,$connection);
	exit;
}
if($actionmode == 'removeFG'){
	
	$delete = "delete from RM_Calculationdt where Calculation_id= $Calculation_id;";
	$res = mysql_query($delete,$connection);
	$delete1 = "delete from RM_Calculation where Calculation_id= $Calculation_id;";
	$res = mysql_query($delete1,$connection);
	getDetail($fromdate,$itemcur,$base1,$base2,$connection);
	exit;
}
if($actionmode == 'updateQty'){
	
	$update = "update RM_Calculation set qty = $fgqty where Calculation_id = $Calculation_id;";
	$res = mysql_query($update,$connection);
	$delete = "update RM_Calculationdt,packdt, RM_Calculation  set RM_Calculationdt.qty = ifnull(packdt_quantity,0)*$fgqty 
				where RM_Calculation.Calculation_id= $Calculation_id and RM_Calculationdt.Calculation_id=RM_Calculation.Calculation_id and packdt.item_code = item_codefg  and packdt.item_codepackdt = item_coderm;";
	$res = mysql_query($delete,$connection);
	getDetail($fromdate,$itemcur,$base1,$base2,$connection);
	exit;
}
if($actionmode == 'reloadgrid'){
	
	getDetail($fromdate,$itemcur,$base1,$base2,$connection);
	exit;
}

if($mainaction == 'insert'){
echo "<form name='rmcalculateaction' method='post' action='Form_RM_Calculation.php'>";
echo "<input type='hidden' name='mainaction' value='$mainaction'>";
echo "<input type='hidden' name='newTable' value='1'>";
echo "<input type='hidden' name='itemcur' value='$itemcur'>";
echo "<input type='hidden' name='fromdate' value='$fromdate'>";
echo " <script>";
echo " document.rmcalculateaction.submit(); ";
echo " </script>";
echo "</form>";
}


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>";
	
}

?>