<?php

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


include("config.inc.php");
include("Functions_Purchase.php");


$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");


	
//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_Pack,0) As Module_Pack  ".
			 " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

//Group List
$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);
if ($groupexist == 0)
	$querygroup="select group_code, group_name from grouping order by group_code";
	
$resultgroup=mysql_query($querygroup,$connection) or die(mysql_error());


//Warehouse List
$querywarehouse=" 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']."') ".
				" order by warehouse.warehouse_code";
$resultwh = mysql_query($querywarehouse,$connection) or die(mysql_error());
$whexist   = mysql_num_rows($resultwh);
if ($whexist == 0)
	$querywarehouse="select warehouse_code,warehouse_name from warehouse order by warehouse_code";
	
$resultwarehouse=mysql_query($querywarehouse,$connection) or die(mysql_error());


$querydflwh="select default_warehouse_code as defaultwarehouse from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."' and default_warehouse_code != '' ";
$resultdflwh=mysql_query($querydflwh,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflwh)){
  extract($rows);
}

$querycurrency="select Currency_code from currency order by currency_code";
$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error());

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<script type="text/javascript" language="javascript">


function SubmitRecord() {
	x = document.productiondt;
	
	var result = CheckFields();
	var vPrdId    = x.PPrdId.value;
	var vPrdDtId  = x.txtPrdDtId.value;
	var vItemType = x.PItemType.value;
	var vMainMode = x.mainaction.value;
	var vMode= x.actionmode.value;
	
	if (vPrdDtId == '')
		vMode = 'submitrecord';	
	else
		vMode = 'editdt';	
	   
	if (result == 1) {
		x.action="Form_ProductionTemplate_Actions.php?SItemType="+vItemType+"&mode="+vMode+"&mainmode="+vMainMode+"&SPrdId="+vPrdId+"&SPrdDtId="+vPrdDtId;
		x.submit();
		
	}	
}

function ChangeLocation() {
	x = document.productiondt;
	var vMode = x.actionmode.value;
	
	if (vMode == 'submitrecord') {
		
		opener.document.production.actionmode.value = x.actionmode.value;
		opener.document.production.mainaction.value = x.mainaction.value;
		//opener.document.production.message.value = x.message.value;
		opener.document.production.txtPrdId.value = x.PPrdId.value;
		opener.document.production.submit();
		
	} 
	else if (vMode == 'submitrecord' || vMode == 'editdt') {
		opener.document.production.actionmode.value = x.actionmode.value;
		opener.document.production.mainaction.value = x.mainaction.value;
		opener.document.production.txtPrdId.value = x.PPrdId.value;
		opener.document.production.submit();
		
		if (x.message.value == "")
			self.close();
	
	}
	
	//x.txtItemCode.focus();
}

function CheckFields(){
  
    x = document.productiondt;
	
	var PrdDtItem = x.txtItemCode.value;
	var PrdDtWarehouse = x.lstWarehouse.value;
	var PrdDtQty = x.txtPrdDtQty.value;
	
	PrdDtQty = parseFloat(PrdDtQty);
	
	var vmode = x.actionmode.value;
	
		
	if (PrdDtWarehouse=='')
	{
		alert("Warehouse Column is mandatory!");
		x.lstWarehouse.focus();
		return 0;
	}
	else if (PrdDtItem=='' )
	{
		alert("Item Column is mandatory!");
		x.txtItemCode.focus();
		return 0;
	}
	else if ((PrdDtQty == '' || PrdDtQty*1 != PrdDtQty/1 || PrdDtQty < 0)) {
		alert('Quantity should be a NUMERIC and POSITIVE value ');
		x.txtPrdDtQty.focus();
		return 0;
	}
		
	
	else
	return 1 ;	
}



function showlistItems(str){
  var vdate, vid;
  x = document.productiondt;
  
  vdate = x.PPrdDate.value;
  vid   = x.PPrdId.value;
  vItemType  = x.lstItemType.value;
  vItemGroup = x.lstGroup.value;
  vItemSort  = 'ItemCode';
  vItemWh    = x.lstWarehouse.value;
  vItemCode  = x.txtItemCode.value;
  
  var vType = x.PItemType.value;
  if (vType == 'FG') vItemWh = '';
  
  vItemGroup = vItemGroup.replace('&','%26');
  vItemWh = vItemWh.replace('&','%26');
  
  
  window.open('List_SorItems.php?frm=4&PItem_Code='+vItemCode+'&ItemType='+vItemType+'&ItemSort='+vItemSort+'&ItemGroup='+vItemGroup+'&ItemWh='+vItemWh+'&TransDate='+vdate,'mywindow','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


function kd(e)
{
	x=document.productiondt;
    var intKey = (window.Event) ? e.which : e.keyCode;
    if (intKey == 13) { //enter key
        //x.btnSubmitRecord.click();
		return false;
    }
    return true;
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Production Template Detail - Add Items</title>


<script type="text/javascript" src="dropdown.js"></script>
<link rel="stylesheet" href="default.css" type="text/css" />
<link rel="stylesheet" href="short.css" type="text/css" />
<link rel="stylesheet" href="dropdown.css" type="text/css" />
<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 10px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>

</head>
<body onLoad='ChangeLocation();' onkeydown='return kd(event)'>
<form name='productiondt' method='post'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='message' value='$message'>
<input type='hidden' name='PItemType' value='$PItemType'>
<input type='hidden' name='PPrdDate' value='$PPrdDate'>
<input type='hidden' name='PPrdId' value='$PPrdId'>
<input type='hidden' name='PPrdActive' value='$PPrdActive'>

HERE;

echo "<input type='hidden' name='PPrdRemark' value='".htmlspecialchars($PPrdRemark, ENT_QUOTES)."' > ";
	
		                  
						  

$lstWarehouse = $_POST["lstWarehouse"]; if ($lstWarehouse == ""){$lstWarehouse = $_GET["lstWarehouse"];}   
$lstItemType = $_POST['lstItemType']; if ($lstItemType == ""){$lstItemType = $_GET["lstItemType"];} if ($lstItemType == ""){$lstItemType = "All";}

if (empty($lstWarehouse)) {
	if(!empty($defaultwarehouse))
		$lstWarehouse = $defaultwarehouse;
	else	
		$lstWarehouse = $SetupDftWareHouse;
}	

if (empty($txtCurr)) $txtCurr = $base1;

//Edit Production Template Detail Mode
if($PPrdDtId != ''){

	if ($PItemType == 'RM') {
		$querydt = " Select ProdRecipeRM_ID, ProdRecipeRM.Item_CodePacked, ProdRecipeRM.Item_Code, Item_Description, ".
				   " ProdRecipeRM.Warehouse_Code, ProdRecipeRM_Remark, ProdRecipeRM_QuantityFrom, ProdRecipeRM_Quantity, ProdRecipeRM_DistOption, ".
				   " ProdRecipeRM.Currency_Code, ProdRecipeRM_Cost, ProdRecipeRM_WasteRate, ProdRecipeRM_WastePerBillet ".
				   " From ProdRecipeRM Left Join Items on ProdRecipeRM.Item_Code = Items.Item_Code ".
				   " Where ProdRecipeRM.ProdRecipeRM_ID = ".$PPrdDtId;
	}
	else if ($PItemType == 'FG') {
		$querydt = " Select ProdRecipeFG_ID, ProdRecipeFG.Item_CodePacked, ProdRecipeFG.Item_Code, Item_Description, ".
				   " ProdRecipeFG.Warehouse_Code, ProdRecipeFG_Remark, ProdRecipeFG_QuantityFrom, ProdRecipeFG_Quantity, ProdRecipeFG_DistOption ".
				   " From ProdRecipeFG Left Join Items on ProdRecipeFG.Item_Code = Items.Item_Code ".
				   " Where ProdRecipeFG.ProdRecipeFG_ID = ".$PPrdDtId;
	}
	//echo $querydt;
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
    $rowsdt=mysql_fetch_array($resultdt);
 	
	$txtPrdDtId         = $rowsdt[0];
	$txtItemCode        = $rowsdt[2];
	$txtItemDesc        = $rowsdt[3];
	$lstWarehouse       = $rowsdt[4];
	$txtPrdDtRemark     = $rowsdt[5];
	$txtPrdDtQtyF       = $rowsdt[6];
	$txtPrdDtQty        = $rowsdt[7];
	$txtCostOption      = $rowsdt[8];
	
	if ($PItemType == 'RM') {
		$txtCurr            = $rowsdt[9];
		$txtPrdDtCost       = $rowsdt[10];
		$txtWasteRate       = $rowsdt[11];
		$txtWastePerBillet  = $rowsdt[12];
	}	
} 

$i_unitcoef = 1;

if($txtItemCode != ''){
	$query = " SELECT Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1) ".
			 " FROM items WHERE Item_Code = '".$txtItemCode."'";
		
	//echo "<br><br>".$query;
	$result = selectData($connection, $query);
	$i_unit1  	  = $result[0];
	$i_unit2 	  = $result[1]; 
	$i_unitcoef	  = $result[2];
	
	$txtItemUnit1 = $i_unit1;
	$txtItemUnit2 = $i_unit2;

}

//$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' style='table-layout:fixed' width='580px;'>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px;' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Add Items</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";


 echo"<tr  height='25'><td id='search-content1' style='height:34px; width:31px;'>&nbsp;</td>
				<td  align='left' id='search-content2'><table width='100%' align='left' border='0'>";
		
	echo"	<tr >";
echo"	  <td colspan='2' align='center'>

 <button type='button' id='btnSubmitRecord' name='btnSubmitRecord'  onclick='SubmitRecord();return false;' title='Submit Record'><span><span>Submit Record</span></span></button>";
echo"	  </td>";
echo"	</tr>";			
		echo"</table></td><td id='search-content3' style='width:31px;'>&nbsp;</td>
		      </tr>";
	  
echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";  	  
echo"   
        <table border='0' style='table-layout:fixed' align='center' valign='top'>
          <col width=100>
		  <col width=120>
		  <col width=100>
		  <col width=90>
		  <col width=80>
		  <col width=60> ";
		  
echo"	  
		   <tr bgcolor='#F7F7F7' height='25'>	
			 <td colspan='6' align='center'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td></tr>			 
			 
		   <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Warehouse&nbsp;<span class='style26'>*</span></span></td>";
		     
		if ($PPrdDtId == '') {	 	 
			echo" <td colspan='5'><select name='lstWarehouse' id='lstWarehouse' style='font-size:11px; width:200px; height:18px;' >";
			echo"<option></option>";
			while($wrhf=mysql_fetch_row($resultwarehouse)){
			echo"<option ";
				if (($lstWarehouse==$wrhf[0]) || ($DftWareHouse==$wrhf[0]))
					echo " selected ";
			echo" value='$wrhf[0]'>$wrhf[0] / $wrhf[1]</option>";
			}
			echo"</select></td>";
			
		} else 	{
			echo"<td colspan='6'><b>$lstWarehouse</b><input type='hidden' name='lstWarehouse' id='lstWarehouse' value='$lstWarehouse'></td>";
		
		}
			
	echo"</tr>	 
		  <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Item Type</span></td>
		     <td colspan='5'>
			 <input type='hidden' name='txtPrdDtId' value='$txtPrdDtId' >
			 <select name='lstItemType' style='font-size:11px; width:200px; height:18px;'>";
			
			if ($lstItemType == "All"){
				echo "<option selected value='All'>All Items</option>";
			}else {
				echo "<option value='All'>All Items</option>";
			}
			if ($lstItemType == "Packed" && $Module_Pack == 1){
				echo "<option selected value='Packed'>Packed</option>";
			}else if ($Module_Pack == 1){
				echo "<option value='Packed'>Packed</option>";
			}
			if ($lstItemType == "PromoPacked" && $Module_Pack == 1){
				echo "<option selected value='PromoPacked'>Promo Packed</option>";
			}else{
				echo "<option value='PromoPacked'>Promo Packed</option>";
			}
			if ($lstItemType == "Trading"){
				echo "<option selected value='Trading'>Trading</option>";
			}else{
				echo "<option value='Trading'>Trading</option>";
			}
			if ($lstItemType == "PromoTrading"){
				echo "<option selected value='PromoTrading'>Promo Trading</option>";
			}else{
				echo "<option value='PromoTrading'>Promo Trading</option>";
			}
			if ($lstItemType == "Production"){
				echo "<option selected value='Production'>Production</option>";
			}else{
				echo "<option value='Production'>Production</option>";
			}
			if ($lstItemType == "NonStock"){
				echo "<option selected value='NonStock'>Non-Stock</option>";
			}else{
				echo "<option value='NonStock'>Non-Stock</option>";
			}
			echo "</select></td>";
			 
			
echo"     </tr>	 
		  <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Group</span></td>
		     <td colspan='5'><select name='lstGroup' id='lstGroup' style='font-size:11px; width:383px; height:18px;' >";
			echo"<option></option>";
			while($groupf=mysql_fetch_row($resultgroup)){
			echo"<option ";
				if ($lstGroup==$groupf[0] )
					echo " selected ";
			echo" value='$groupf[0]'>$groupf[0] - $groupf[1]</option>";
			}
			
echo"        </select></td>			 
		  </tr>	 
		  <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Item&nbsp;<span class='style26'>*</span></span></td>";
			 
			 if ($PPrdDtId == '') {
				echo " <td colspan='5'><input type='text' name='txtItemCode' value='$txtItemCode'  maxlength='20' style='font-size:12px; width:110px; height:20px;'>
					   <input type='text' name='txtItemDesc' id='txtItemDesc' value='$txtItemDesc' style='font-size:12px; width:270px; height:20px;' />
					   <label><span class='style12'><img src='img/btnopen.jpg' title=' Select Item '  width='15' height='17' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistItems(1); ' /></span></label></td>";
			 } else {
				echo " <td colspan='6'><input type='hidden' name='txtItemCode' value='$txtItemCode' /><input type='hidden' name='txtItemDesc' id='txtItemDesc' value='$txtItemDesc' />
					   <b>$txtItemCode &nbsp;&nbsp; $txtItemDesc</b></td>";
			 }	
			 
echo"     </tr>	 
		  <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Remark</span></td>
			 <td  colspan='5'><input type='text' name='txtPrdDtRemark' value='".htmlspecialchars($txtPrdDtRemark, ENT_QUOTES)."'  maxlength='51' style='font-size:12px; width:385px; height:20px;'></td>
		  </tr> ";
		  
echo"	  <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Cost Option</span></td>
			 <td colspan='5'><span class='style12'>";
echo"  		    <input type='radio' name='txtCostOption' id='txtCostOption' value='1' "; if($txtCostOption=='1' || $txtCostOption=='') echo "checked />"; else echo " />";  echo " Percentage";
echo"	 	    <input type='radio' name='txtCostOption' id='txtCostOption' value='2' "; if($txtCostOption=='2') echo "checked />"; else echo " />";  echo " Quantity";
echo"	 	    <input type='radio' name='txtCostOption' id='txtCostOption' value='3' "; if($txtCostOption=='3') echo "checked />"; else echo " />";  echo " Fixed";
echo"		 </span></td>
		  </tr>	";
		  
if ($PItemType == 'RM') {
	echo"	  <tr bgcolor='#F7F7F7' height='25'>";
	echo"		 <td valign='center' align='left' ><span class='style12'>Cost (NonStock)</span></td> ";
	echo"	     <td align='left'><input type='text' name='txtPrdDtCost' value='$txtPrdDtCost' maxlength='20'  style='font-size:12px; width:100px; height:20px;' </td>";
	echo"		 <td valign='center' align='right' ><span class='style12'>Currency</span></td> ";
	echo"    	 <td colspan='3'><span class='style11'><select name='txtCurr' id='txtCurr' style='width:80px;'>";
				echo"<option></option>";
				while($curf=mysql_fetch_row($resultcurrency)){
				echo"<option ";
					if ($txtCurr==$curf[0])
						echo " selected ";
					echo" value='$curf[0]'>$curf[0] </option>";
				}
				echo"</select></span></td>";
	echo"	  </tr>";
	echo"	  <tr bgcolor='#F7F7F7' height='25'>";
	echo"		 <td valign='center' align='left' ><span class='style12'>Waste Rate(%)</span></td> ";
	echo"	     <td align='left'><input type='text' name='txtWasteRate' value='$txtWasteRate' maxlength='20'  style='font-size:12px; width:100px; height:20px;' </td>";
	echo"		 <td valign='center' align='right' ><span class='style12'>Waste Per Billet(%)</span></td> ";
	echo"	     <td colspan='3' align='left'><input type='text' name='txtWastePerBillet' value='$txtWastePerBillet' maxlength='20'  style='font-size:12px; width:100px; height:20px;' ></td>";
	echo"	  </tr>";
}
		  
echo"	  <tr bgcolor='#F7F7F7' height='25'>";
echo"		 <td valign='center' align='left' ><span class='style12'>Quantity From&nbsp;<span class='style26'>*</span></span></td> ";
echo"	     <td align='left'><input type='text' name='txtPrdDtQtyF' value='$txtPrdDtQtyF' maxlength='20'  style='font-size:12px; width:100px; height:20px;' </td>";
echo"		 <td valign='center' align='right' ><span class='style12'>Quantity To&nbsp;<span class='style26'>*</span></span></td> ";
echo"	     <td colspan='3' align='left'><input type='text' name='txtPrdDtQty' value='$txtPrdDtQty' maxlength='20'  style='font-size:12px; width:100px; height:20px;' >&nbsp;&nbsp;<b>$txtItemUnit1</b></td>";
echo"	  </tr>";
	

 echo "</table></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";			 
	echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:560px;' valign='top'></td>
		<td id='search-bottom3' style='height:34px; width:31px;' valign='top'></td>";
echo" </tr>";	  
   echo"</table>";
        
	

print <<<HERE
</form>
</body>

HERE;

?>