<?php
   ob_start();
$PageSecurity = 4;
$PageName = 'View Purchase Requisition';
include('includes/DefinePOClass.php');
include('includes/SQL_CommonFunctions.inc');
include('includes/DefineSuppTransClass.php');
$title = _('View Purchase Requisition');
$req_sql=DB_query("Select * from inv_material_return where id='".$_REQUEST['Id']."'",$db);


$req_row=DB_fetch_array($req_sql);
$mrn_sql=DB_query("Select * from addmrn where id='".$req_row['MRNno']."'",$db);


$mrn_row=DB_fetch_array($mrn_sql);


//print_r($req_row);
?>
<script language="javascript" type="text/javascript">

function cal_short_qty(i)

{
	
	var k=document.getElementById("rate_"+i).value;
	var k1=document.getElementById("returnQty_"+i).value;
	if(k==''){k=0;}
	if(k1==''){k1=0;}
	var shortqty=parseFloat(k)*parseFloat(k1);
	document.getElementById("totalvalue_"+i).value=shortqty;
	
	
	
	
}
</script>
 <?php
  if($_REQUEST['Submit'])
{




  
	 $qty=$_REQUEST['item_code'];
			
	 //$updatemrn_res=DB_query($updatemrn_sql,$db);
	
	
	 
	
	
	 for($cr=0;$cr<($_REQUEST['countval']);$cr++)
		{
		   $text_item_code = htmlspecialchars($_REQUEST['item_code'][$cr], ENT_QUOTES); 
		   $text_item_name = htmlspecialchars($_REQUEST['desc'][$cr], ENT_QUOTES);
		   $updatemrndetails_sql="UPDATE inv_material_return_details SET 
				     
				     QtyAccepted='".$_REQUEST['qtyAccept'][$cr]."',
				     QtyAsPerBill='".$_REQUEST['qty_as_per_bill'][$cr]."',
				     ItemPrice='".$_REQUEST['rate'][$cr]."',
				     TotalValue='".$_REQUEST['totalvalue'][$cr]."',
				     ReturnQty='".$_REQUEST['returnQty'][$cr]."',
				     Remarks='".$_REQUEST['remarks'][$cr]."'
				      WHERE parentId='".$_REQUEST['Id']."'";
	//echo "<pre>";
	//print_r($updatemrndetails_sql);
	}
	$updatemrndetails_res=DB_query($updatemrndetails_sql,$db);
	 
	$_SESSION['success_msg']="Update Successfully";
/*echo "<script>location.href='index.php?route=Inventory/transation/Material_Return_To_Vendor_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";*/
	 
	 
}
  
  
  
  
  ?>
<h2 align="center"><strong>Material Return To Vendor</strong></h2>
<form action="" method="post">
<br />
<table width="95%" border="0" cellspacing="1" cellpadding="1" align="center" style="border: solid 1px #CCC; background-color: #FFFFFF">
  <tr>
    <th nowrap="nowrap"><strong>Branch</strong></th>
     <td>
    	<?php
    	 $site_sql=DB_query("Select loccode,locationname from locations where company='".$_SESSION['ACGCompanyId']."' and loccode='".$req_row['sitecode']."'",$db);
    	?>
    	<select name="site" id="site" style="width:100%">
    	 <?php
    
   
	while($site_row=DB_fetch_array($site_sql))
	{
		$p1='';
		if($req_row['sitecode']==$site_row['loccode'])
		{
			$p1="selected";
		}
    
    ?>
    		   <option value="<?php echo $site_row['locationname'];?>" <?php echo $p1;?>><?php echo $site_row['locationname'];?></option>
    		   
    		    <?php
    		   
	}
    		   ?>
    		   </select>
    		   
    </td>
    <th nowrap="nowrap"><strong>Requisition No</strong></th>
  	
  		<td><input name="requisition_no" type="text" id="requisition_no" value="<?php echo $req_row['Reqno'];?>" style="width:100%" readonly="true" /></td>
<th><strong>Date</strong></th>
  	  <td> <input name="returnDate" type="text" style="width:100%" value="<?php echo $req_row['Reqdate'];?>" readonly="true" /></td>
  	
    
      </tr>
  <tr>
      <th nowrap="nowrap"><strong>Vendor</strong></th>
   <td>
    	<?php
    	 $v_sql=DB_query("Select id,vendor_code,vendor_name from suppliers where id='".$req_row['vendor_code']."'",$db);
    	
    	?>
     <select name="vendor" id="vendor" style="width:100%">
	  <?php 
	 
	  while($v_row=DB_fetch_array($v_sql))
	   {
	  	$p1='';
	  	if($req_row['vendor_code']==$v_row['vendor_code'])
		{
			
			$p1="selected";
			
		}
	  ?>
	  <option value="<?php echo $v_row['vendor_name'];?>" <?php echo $p1;?>><?php echo $v_row['vendor_name'];?></option>
	  <?php } ?>
      </select>   </td>
  
    <th nowrap="nowrap"><strong>Return Against</strong></th>
   <td>
   	<select name="returnagainst" id="returnagainst" style="width:100%">
	  <option value="<?php echo $req_row['returnagainst'];?>"><?php echo $req_row['returnagainst'];?></option>
	  <?php ?>
      </select> 
		</td>
 <th><strong>MRN</strong></th>
    <td>
   <div id='hidmrndiv'> 
	<input name="mrn" id="mrn"  type="text" value="<?php echo $mrn_row['MRNNo'];?>" style="width:100%" readonly>
	  
	  
	  
      </div>
      </td>
  </tr>
   <?php if($req_row['returnagainst']=='MRN'){?>
   <tr>
 <th width="5%" nowrap="nowrap"><strong>MRN Date</strong></th>
 <td width="22%"><input name="mrnDate" id="mrnDate" type="text" style="width: 100%"  value="<?php echo $mrn_row['MRNDate'];?>" readonly/></td>
 <th width="15%" nowrap="nowrap"><strong>PO No</strong></th>
 <?php
 $poSql="SELECT PO_Orderno FROM purchorders WHERE orderno='".$req_row['PONo']."'";
$poResult=DB_query($poSql,$db);
$poRow=DB_fetch_array($poResult);
 
 ?>
 <td><input name="poNo" id="poNo" type="text" style="width: 100%"  value="<?php echo $poRow['PO_Orderno'];?>" readonly/></td>
 <th nowrap="nowrap"><strong>PO Date</strong></th>
 <td><input name="poDate" id="poDate" type="text" style="width: 100%"  value="<?php echo $req_row['PODate'];?>" readonly/>
 </td>
 </tr>
<?php }?>
 </table>


<br />
<br />
<br />
<div align="center">
<div style="overflow:auto; width:800px;height:150px">
<table width="80%" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <th width="6%">Item Code</th>
    <th width="9%">Item Name</th>
    <th width="6%">Make</th>
    <th width="9%">Grade</th>
    <th width="8%">Color</th>
    <th width="10%">UOM</th>
	<th width="10%">Description</th>
	<th width="9%">Width</th>
	<th width="15%">Length</th>
	<th width="9%">Original </th>
	 <?php if($req_row['returnagainst']=='MRN'){?>
	<th width="6%">PO Qty</th>
    <th width="6%">Qty As Per Bill</th>
    <th width="9%">Qty Accepted</th>
    <?php }?>
    <th width="9%">Rate</th>
   <th width="10%">Return Qty</th>
    <th width="8%">Total Value</th>
	<th width="10%">Remarks</th>
  </tr>
<?php 
$i=0;
$get_sql="Select * from inv_material_return_details where parentId='".$req_row['id']."'";
$get_result=DB_query($get_sql,$db);
while($get_row=DB_fetch_array($get_result))
{

if($i%2==0)
{$bg="#C8BECB";}
else
$bg="#CCCCCC";
 

//echo $get_row['id'];

$item_sql="Select * from item_master where StockID='".$get_row['itemCode']."'";
$item_result=DB_query($item_sql,$db);

$item_row=DB_fetch_array($item_result);
//{
	
	
		
	



?>
  <tr bgcolor="<?php echo $bg;?>">
    <td><?php echo $get_row['itemCode'];?><input type="hidden" name="item_code[<?php echo $i;?>]" id="item_code_<?php echo $i;?>" value="<?php echo $get_row['itemCode'];?>"></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 
    $makesql="Select manufacturer  from  product_manufacture where id='".$item_row['make']."'";
	$makeresult=DB_query( $makesql,$db);
	$make_row=DB_fetch_array($makeresult);
	echo $make_row['manufacturer'];?></td>
    <td><?php 
	$grade=$s->getField('product_grade','id',$item_row['grade'],'grade');
	echo $grade;?></td>
    <td><?php 
	$color=$s->getField('product_color','id',$item_row['color'],'color');
	echo $color;?></td>
	
	    
    <td><?php 

	$unitname=$s->getField('unitsofmeasure','unitid',$item_row['unit'],'unitname');
	echo $unitname;?></td>
	<td><?php echo $item_row['item_name'];?></td>   
    <td><?php echo $item_row['width'];?></td>
    <td><?php echo $item_row['length'];?></td>
	<td><?php echo $item_row['orig_local'];?></td>
	
	 <?php if($req_row['returnagainst']=='MRN'){?>
	 <td>
	 <input type="text" name="poQty[<?php echo $i;?>]" id="poQty_<?php echo $i;?>" value="<?php echo $get_row['POQty'];?>">
	 </td>
	<td>
	<input type="text" name="qty_as_per_bill[<?php echo $i;?>]" id="qty_as_per_bill_<?php echo $i;?>" value="<?php  echo $get_row['QtyAsPerBill'];?>">
	</td>
	 <td>
	 <input type="text" name="qtyAccept[<?php echo $i;?>]" id="qtyAccept_<?php echo $i;?>" value="<?php echo $get_row['QtyAccepted'];?>">
	 </td>
	 <?php }?>
	<td>
    <input type="text" name="rate[<?php echo $i;?>]" id="rate_<?php echo $i;?>" value="<?php echo $get_row['ItemPrice'];?>">
	</td>
	<td>
		<input type="text" name="returnQty[<?php echo $i;?>]" id="returnQty_<?php echo $i;?>" value="<?php echo $get_row['ReturnQty'];?>">
	</td>
	 <td>
	 	<input type="text" name="totalvalue[<?php echo $i;?>]" id="totalvalue_<?php echo $i;?>" value="<?php echo $get_row['TotalValue'];?> " onfocus="cal_short_qty(<?php echo $i;?>);">
	 </td>
	<td>
		<input type="text" name="remarks[<?php echo $i;?>]" id="remarks_<?php echo $i;?>" value="<?php echo $get_row['Remarks'];?>">
	</td>
	
	
	
  </tr>
 
 
  <?php
  //}
//}
 ?>
    
  

  <?php   $i++;} ?>
   <input type="hidden" name="countval" value="<?php echo $i;?>" />

 


 
 
</table>
</div></div>
<p align="center"><input type="submit" name="Submit" value="Update" onclick="return check_po()"/>
  <input type="submit" name="cancel" value="Reset">
<a href="index.php?route=Inventory/transation/Material_Return_To_Vendor_index&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&NewReturn=yes"><input type="button" value="Cancel" name="Back" /></a></p>
 
  </form>
<?php 
include('includes/footer.inc');
   
?>