<?php session_name('CAKEPHP'); session_start();

?>
<script language="javascript" type="text/javascript">
function CalrequiredQty(val,i){
 

var a=	document.getElementById("stock_qty_"+i).value;
document.getElementById("required_qty_"+i).value=val-a;
 
}
function open_items()
{
	window.open("requisition_items.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>","item_popup_window","height=500,width=500,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
function generate_reqno(str)
{
	
	if (str.length==0)
  {
  document.getElementById("reqno").innerHTML="";
  return;
  }
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("reqno").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Purchase/get_reqnopurchase.php?site="+str,true);
xmlhttp.send();
}
function check_approveqty(str,j)
{
	var req_qty=document.getElementById("req_qty_"+j).value;
	if(str>req_qty)
	{
		alert("Quantity to approve cannot be greater than requsition quantity");
		document.getElementById("app_qty_"+j).value='';
		document.getElementById("app_qty_"+j).focus();
	}
}
</script>
<!--<a href="MR_SelectOSMaterialRequi.php">Back>></a>-->
<?php if($_REQUEST['RequisitionNo']=='')
{ ?>
<h2 align="center"><strong>New Requisition</strong></h2>
<h3 align="center"><font color="#FF0000">Please enter the requisition details first</font></h3>
<form action="" method="post">
<div align="center">
<fieldset style="width:650px"><legend><h3 align="center"><strong>Requisition Details</strong></h3></legend>
<table width="100%" border="0" cellspacing="1" cellpadding="5" align="center">
  <tr>
    <th><strong>REQUISITION DATE</strong></th>
    <td><input type="text" name="date" id="date" class="datepicker" readonly="true" value="<?php if(isset($_SESSION['date']))echo $_SESSION['date']; else echo date('d/m/Y');?>"></td>
    <th rowspan="3"><strong>REMARKS</strong></th>
    <td rowspan="3"><textarea name="remarks" style="width:100%" rows="3"><?php echo $_SESSION['remarks'];?></textarea></td>
  </tr>
  <tr>
    <th><strong>SITE</strong></th>
    <td>
	<?php 
	if($_SESSION['AssignedSites']!='0')
	  {
	  	 $ss="Select loccode,locationname from locations where loccode in (".$_SESSION['AssignedSites'].")";
	  	 $site_sql=DB_query($ss,$db);
	  }
	  else
	  {
		$ss="Select loccode,locationname from locations";
		$site_sql=DB_query($ss,$db);
	  }
	?>
	<select name="site" id="site" style="width:120px" onchange="generate_reqno(this.value)">
      <option value="000">---Select---</option>
      <?php
	while($site_row=DB_fetch_array($site_sql)){
	?>
      <option value="<?php echo $site_row['loccode'];?>"<?php echo ($_SESSION['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
      <?php } ?>
    </select></td>
  </tr>
  <tr>
    <th><strong>REQUISITION NUMBER </strong></th>
    <td><div id="reqno">
        <input name="requisition_no" type="text" id="requisition_no" value="<?php echo $_SESSION['requisition_no'];?>" size="30" readonly="true" />
      </div></td>
  </tr>
</table>
</fieldset>
</div>
<p align="left">
<input type="hidden" name="date1" value="<?php echo $_SESSION['date'];?>" />
<input type="hidden" name="site1" value="<?php echo $_SESSION['site'];?>" />
<input type="hidden" name="requisition_no1" value="<?php echo $_SESSION['requisition_no'];?>" />
<input type="hidden" name="remarks1" value="<?php echo $_SESSION['remarks'];?>" />
<input type="submit" name="item_popup" value="Add Items" id="item_popup"  onclick="open_items();" /></p> 
<div align="center">
<fieldset><legend><h3 align="center"><strong>Item Details</strong></h3></legend>
<div style="overflow:auto; width:auto; height:200px">
<table width="100%" border="0" align="center" cellpadding="1" cellspacing="2">
  <tr>
    <th width="6%" align="center">ITEM CODE </th>
    <th width="8%" align="center">ITEM NAME </th>
    <th width="13%" align="center">MAKE</th>
    <th width="8%" align="center">GRADE</th>
    <th width="8%" align="center">COLOR</th>
	<th width="8%" align="center">SIZE</th>
	<th width="6%" align="center">UNIT</th>
    <th width="10%" align="center">REQUISITION QUANTITY </th>
    <th width="30%" align="center">REMARKS</th>
    </tr>
<?php 
if(isset($_SESSION['manvi']))
{
$itemlist=array();
//print_r($_SESSION['manvi']);
$itemlist=explode('#',$_SESSION['manvi']);
//print_r($itemlist);
?>
	<?php 
	for($i=0;$i<count($itemlist);$i++)
	{
	 $itemquery=DB_query("select * from item_master where StockID='".$itemlist[$i]."'" , $db);
	 //echo "select StockID,item_name,unit, make ,grade, color from item_master where StockID='".$itemlist[$i]."'" ;
	while($item_row=DB_fetch_array($itemquery))
	{
		/*if($i%2==0)
		{$bg="#CCCCCC";}
		else
		$bg="#C8BECB";*/
	
	?>
	<tr bgcolor="#FFFFFF">	
	<td><?php echo $itemlist[$i];?><input type="hidden" name="item_code[<?php echo $i;?>]" id="item_code_<?php echo $i;?>" value="<?php echo $itemlist[$i];?>">	</td>
	<td><?php echo $item_row['item_name'];?><input type="hidden" name="item_name[<?php echo $i;?>]" id="item_name[<?php echo $i;?>]" value="<?php echo $item_row['item_name'];?>"/>	</td>
	
	<td><select name="Make[<?php echo $i;?>]" id="Make" style="width:100%">
      <?php
		 $querysp=DB_query("SELECT * FROM product_manufacture" ,$db);
		while($que_row=DB_fetch_array($querysp))
		{
				
		 ?>
      <option value="<?php echo $que_row['id'];?>"><?php echo $que_row['manufacturer'];?></option>
      <?php  }?>
	  </select></td>
	  
	<td><select name="Grade[<?php echo $i;?>]" style="width:100%">
		<?php
		 $querysp1=DB_query("select * from product_grade" ,$db);
			while($que_row=DB_fetch_array($querysp1))
		{
		?>
		<option value="<?php echo $que_row['id'];?>"><?php echo $que_row['grade'];?></option>
		<?php } ?></select></td>
		
		
	<td><select name="Color[<?php echo $i;?>]" style="width:100%">
		<?php
		 $querysp2=DB_query("select * from product_color" ,$db);
		 while($que_row=DB_fetch_array($querysp2))
			{
			
		 ?>
		<option value="<?php echo $que_row['id'];?>"><?php echo $que_row['color'];?></option>
		<?php } ?></select></td>
		
		<td>
		  <?php 
		$sizesql="Select * from size_master";
		$size_result=DB_query($sizesql,$db);?>
		 <select name="size[<?php echo $i;?>]" id="size_<?php echo $i;?>" style="width:100%">
		 <?php 
			while($size_row=DB_fetch_array($size_result))
			{
			
		?>
		    <option value="<?php echo $size_row['id'];?>"><?php echo $size_row['name'];?></option>
		    <?php } ?>
		    </select></td>
		    
		<td><?php
		$grade_query=DB_query("select * from unitsofmeasure where unitid='".$item_row['unit']."'", $db);
		while($grade_row=DB_fetch_array($grade_query))
		 {
		 echo $grade_row['unitname'];?>
		  <?php } ?><input type="hidden" name="Unit[<?php echo $i;?>]" id="Unit[<?php echo $i;?>]" value="<?php $grade_row['id'];?>"/></td>
	<td align="center">  <input name="requisition_qty[<?php echo $i;?>]" type="text" id="requisition_qty_<?php echo $i;?>" style="width:100%" size="14" onblur="CalrequiredQty(this.value,<?php echo $i;?>)" /></td>
     <td align="center"><input name="remark[<?php echo $i;?>]" type="text" id="remark_<?php echo $i;?>" style="width:100%"/></td>
	</tr><?php }}?> 
	 
  <?php } ?>
  <input type="hidden" name="ctr" value="<?php echo $i;?>" />
</table>
</div>
</fieldset>
</div>
<p align="center"><input type="submit" name="Save" value="Save"><input type="submit" name="cancel" value="Cancel"></p>
</form>
<?php }

if($_REQUEST['RequisitionNo']!='' && $_REQUEST['Approve']=='1')
{
if(isset($_REQUEST['approve']))
{
	for($CTR=0;$CTR<$_REQUEST['count_req'];$CTR++)
	{
	  $update_sql="update requisitionorderdetails set 
							App_quantity='".$_REQUEST['app_qty'][$CTR]."',
							approved_date='".FormatDateForSQL(date('d/m/Y'))."',
							status='1'
							where requsitionno='".$_REQUEST['reqid']."'	and product_id='".$_REQUEST['item_code'][$CTR]."'
							and make='".$_REQUEST['make'][$CTR]."' and color='".$_REQUEST['color'][$CTR]."' 
							and grade='".$_REQUEST['grade'][$CTR]."' ";
	//echo "<pre>";
	//print_r($update_sql);  
	$update_result=DB_query($update_sql,$db);
	}
echo "<script>location.href='index.php?route=MR_SelectOSMaterialRequi&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}

$req_sql=DB_query("Select Reqno from materialrequisitions where requisitionno='".$_REQUEST['RequisitionNo']."'",$db);
$req=DB_fetch_array($req_sql);
?>

<form action="" method="post">
<br>
<br>
<br>
<h2 align="center"><strong>Approve Requisition - <?php echo $req['Reqno'];?></strong></h2>
<br>
<br>
<br>
<br>
<br>
<input type="hidden" name="reqid" value="<?php echo $_REQUEST['RequisitionNo'];?>" />
<table width="80%" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <th><strong>ITEM CODE </strong></th>
    <th><strong>ITEM NAME </strong></th>
    <th><strong>MAKE</strong></th>
    <th><strong>GRADE</strong></th>
    <th><strong>COLOR</strong></th>
    <th><strong>UNIT</strong></th>
    <th><strong>TOTAL STOCK </strong></th>
    <th><strong>REQUISITION QUANTITY </strong></th>
	<th><strong>QUANTITY TO APPROVE</strong></th>
  </tr>
		<?php 
	echo	$get_sql="Select * from requisitionorderdetails where requsitionno='".$_REQUEST['RequisitionNo']."'";
		$get_result=DB_query($get_sql,$db);
		$i=0;
		while($get_row=DB_fetch_array($get_result))
		{
		if($i%2==0)
		{$bg="#CCCCCC";}
		else
		$bg="#C8BECB";
		
		?>
	  <tr bgcolor="<?php echo $bg;?>">
  	  <td>
	  <?php echo $get_row['product_id'];?><input type="hidden" name="item_code[<?php echo $i;?>]" id="item_code_<?php echo $i;?>" value="<?php echo $get_row['product_id'];?>"></td>      
    <td><?php echo $get_row['product_description'];?><input type="hidden" name="item_name[<?php echo $i;?>]" id="item_name_<?php echo $i;?>" value="<?php echo $get_row['product_description'];?>"></td>
    <td><?php 
	$makename=$s->getField('product_manufacture','id',$get_row['make'],'manufacturer');
	echo $makename;?>
      <input type="hidden" name="make[]" id="make_" value="<?php echo $get_row['make'];?>" /></td>
    <td><?php 
	$gradename=$s->getField('product_grade','id',$get_row['grade'],'grade');
	echo $gradename;?><input type="hidden" name="grade[]" id="grade_" value="<?php echo $get_row['grade'];?>" /></td>
    <td><?php 
	$colorname=$s->getField('product_color','id',$get_row['color'],'color');
	echo $colorname;?><input type="hidden" name="color[]" id="color_" value="<?php echo $get_row['color'] ;?>" /></td>
    <td><?php 
	$unit=$s->getField('item_master','StockID',$get_row['product_id'],'unit');
	$unitname=$s->getField('unitsofmeasure','unitid',$unit,'unitname');
	echo $unitname;?></td>
    <td><input type="text" name="stock_qty[<?php echo $i;?>]" value="<?php echo $get_row['stock_qty'];?>" id="stock_qty_<?php echo $i;?>"></td>
    <td><input type="text" name="req_qty[<?php echo $i;?>]" id="req_qty_<?php echo $i;?>" value="<?php echo $get_row['requisition_qty'];?>" size="10" readonly="true"></td>
	<td><input type="text" name="app_qty[<?php echo $i;?>]" id="app_qty_<?php echo $i;?>" value="" size="10" onblur="check_approveqty(this.value,<?php echo $i;?>)"></td>
  </tr>
  <?php $i++; }?>
  <input type="hidden" name="count_req" value="<?php echo $i;?>" id="count_req"/>
</table>
<br>
<p align="center"><input type="submit" name="approve" value="Approve" ></p>
<!--onclick="return validate()"-->
</form>
<?php 	
}
?>
<?php 
include('includes/footer.inc');
?>
<script language="javascript" type="text/javascript">
function validate()
{
	var counter=document.getElementById("count_req").value;
	for(var x=0;x<counter;x++)
	{
		var approve=document.getElementById("app_qty_"+x).value;
		if(approve=='')
		{
			alert("Please enter quantity to be approved for all items");
			return false;
		}
	}
}
</script>