<link rel="stylesheet" href="css/new_mxerp.css">
<?php session_name('CAKEPHP'); session_start();
$PageSecurity = 4;
$PageName = 'Purchase Requisition';
include('includes/DefinePOClass.php');
include('includes/SQL_CommonFunctions.inc');
include('includes/DefineSuppTransClass.php');
$title = _('Purchase Requisition');
if($_REQUEST['RequisitionNo']!='' && $_REQUEST['Delete']=='1')
{
	$delete_sql1="Delete from materialrequisitions where requisitionno='".$_REQUEST['RequisitionNo']."'";
	$delete_res1=DB_query($delete_sql1,$db);
	$delete_sql2="Delete from requisitionorderdetails where requsitionno='".$_REQUEST['RequisitionNo']."'";
	$delete_res2=DB_query($delete_sql2,$db);
	echo "<script>location.href='index.php?route=MR_SelectOSMaterialRequi&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
if($_REQUEST['cancel'])
{
	unset($_SESSION['manvi']);
	unset($_SESSION['date']);
	unset($_SESSION['site']);
	unset($_SESSION['requisition_no']);
	unset($_SESSION['remarks']);
}
if($_REQUEST['item_popup'])
{
	$_SESSION['date']=$_REQUEST['date'];
	$_SESSION['site']=$_REQUEST['site'];
	$_SESSION['requisition_no']=$_REQUEST['requisition_no'];
	$_SESSION['remarks']=$_REQUEST['remarks'];
}
if($_REQUEST['Save']!='')
{
$requisition_sql="Insert into materialrequisitions set
									requisitionno='',
									comments='".$_REQUEST['remarks']."',
									prdate='".FormatDateForSQL($_REQUEST['date'])."',
									initiator='".$_SESSION['UserID']."',
									sitecode='".$_REQUEST['site']."',
									Reqno='".$_REQUEST['requisition_no']."',
									CompanyId='".$_SESSION['ACGCompanyId']."'";
									
	//echo "<pre>";
	//print_r($requisition_sql);
	$requisition_res=DB_query($requisition_sql,$db);
	$last_id=DB_Last_Insert_ID($db,'materialrequisitions','id');
	
	if($last_id!='')
	{
		for($cr=0;$cr<$_REQUEST['ctr'];$cr++)
		{
			$material_details_sql="insert into requisitionorderdetails set
											id='',
											requsitionno='".$last_id."',
											product_id='".$_REQUEST['item_code'][$cr]."',
											product_description='".$_REQUEST['item_name'][$cr]."',
											totalreceivetilldate = '".$_REQUEST['stock_qty'][$cr]."', 
											req_quantity='".$_REQUEST['req_qty'][$cr]."'
											"; 
			//echo "<pre>";
			//print_r($material_details_sql);
			$details_res=DB_query($material_details_sql,$db);
		}
	}
//	exit;
unset($_SESSION['manvi']);
unset($_SESSION['date']);
unset($_SESSION['site']);
unset($_SESSION['requisition_no']);
unset($_SESSION['remarks']);
prnMsg( _('Requisition No. - '.$_REQUEST['requisition_no'].' Saved Successfully'),'success');
echo "<script>location.href='index.php?route=MR_SelectOSMaterialRequi&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
?>
<script language="javascript" type="text/javascript">
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=900,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","get_reqno.php?site="+str,true);
xmlhttp.send();
}
function check_approveqty(j)
{
	 var str=document.getElementById("app_qty_"+j).value;
	var req_qty1=parseInt(document.getElementById("req_qty_"+j).value);
	//alert(req_qty1);
	if(str>req_qty1)
	{
		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']=='')
{ ?>
<div class="body_contain">
<div class="tab_div">

<h1>New Requisition</h1>
<p><font color="#FF0000">Please enter the requisition details first</font></p>
<form action="" method="post">

<h3 align="center">Requisition Details</h3>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr class="row1">
    <td class="tabledata1">Branch</td>
    <td>
	<?php 
 
	 	$ss="Select loccode,locationname from  locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode='".$_SESSION['AssignedBranch']."'";
		$site_sql=DB_query($ss,$db);
	  
	?>
	<select name="site" id="site" style="width:150px" 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>
    <td  style="border-left:#b6b6b6 solid 1px; padding-left:5px;" rowspan="3">Remarks</td>
    <td rowspan="3" style="padding-right:5px;"><textarea name="remarks" style="width:100%" rows="3"><?php echo $_SESSION['remarks'];?></textarea></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1">Requisition Number</td>
    <td>	<div id="reqno">
        <input name="requisition_no" type="text" id="requisition_no" value="<?php echo $_SESSION['requisition_no'];?>"  style="width:150px;" readonly="true" />
      </div>
</td>
  </tr>
  <tr class="row1">
    <td class="tabledata1">Requisition Date</td>
    <td>
	<input type="text" name="date" id="date" class="datepicker" style="width:150px;" readonly="true" value="<?php if(isset($_SESSION['date']))echo $_SESSION['date']; else echo date('d/m/Y');?>">
</td>
  </tr>
</table>



<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> 


<h3>Item Details</h3>
<div style="overflow:auto; width:810px; height:200px">
<table width="100%" border="1" cellspacing="0" cellpadding="3">
  <tr>
    <td class="tabledata">ITEM CODE </td>
    <td class="tabledata">ITEM NAME </td>
     <td class="tabledata">ITEM DESCRIPTION</td>
     <td class="tabledata">UNIT</td>
    <td class="tabledata">TOTAL STOCK </td>
    <td class="tabledata">REQUISITION QUANTITY </td>   
    <td class="tabledata">FLEXO/OFFSET</td>
    
   </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="#c1c1c1">
	<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><?php echo $item_row['item_desc'];?><input type="hidden" name="item_desc[<?php echo $i;?>]" id="item_desc[<?php echo $i;?>]" value="<?php echo $item_row['item_desc'];?>"/>	</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><input type="hidden" name="stock_qty[<?php echo $i;?>]" id="stock_qty[<?php echo $i;?>]" /></td>
	
	
	<td><input name="req_qty[<?php echo $i;?>]" type="text" id="req_qty_<?php echo $i;?>" size="15" /></td>
	
	<td><?php echo $item_row['flexo_offset'];?><input type="hidden" name="flexo_offset[<?php echo $i;?>]" id="flexo_offset[<?php echo $i;?>]" value="<?php echo $item_row['flexo_offset'];?>"/>	</td>
	  </tr><?php }}?> 
	 
  <?php } ?>
  <input type="hidden" name="ctr" value="<?php echo $i;?>" />
</table>
</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]."' ";
	//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);
?>
</div>
</div>
<div class="body_contain">
<div class="tab_div">
<form action="" method="post">
<h1>Approve Requisition - <?php echo $req['Reqno'];?></h1>
<input type="hidden" name="reqid" value="<?php echo $_REQUEST['RequisitionNo'];?>" />
<table width="100%" border="1" cellspacing="0" cellpadding="3">
  <tr>
    <td class="tabledata">Item Code </td>
    <td class="tabledata">Item Name </td>
      <td class="tabledata">Unit</td>
    <td class="tabledata">Total Stock </td>
    <td class="tabledata">Requisition Qty </td>
	<td class="tabledata">Qty To Approve</td>
  </tr>
		<?php 
		$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="#c1c1c1">
  	  <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 
	$unit=$s->getField('item_master','StockID',$get_row['product_id'],'unit');
	$unitname=$s->getField('unitsofmeasure','unitid',$unit,'unitname');
	echo $unitname;?></td>
    <td><input type="hidden" name="stock_qty[<?php echo $i;?>]" value="" 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['req_quantity'];?>" size="10" readonly="true"></td>
	<td><input type="text" name="app_qty[<?php echo $i;?>]" id="app_qty_<?php echo $i;?>"  size="10"   onchange="check_approveqty(<?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" onclick="return validate()"></p>
</form>
</div>
</div>

<?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>