<?php
/* $Revision: 1.42 $ */

$PageSecurity = 11;

/* Session started in header.inc for password checking and authorisation level check */
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');

$title = _('GRN Number');
include('includes/header.inc');
//$ModifyPage = $rootpath . "/QualityControl.php?" ;
echo "<a href='" . $rootpath . '/QualityControl.php?' . SID . "'>" . _('Back') . '</a><br>';
?>
<?Php

if(isset($_POST['submit'])){
 $GRNNumber=$_REQUEST['GRNNumber'];
 $itemcode=$_REQUEST['itemcode'];
 $qtyrecev=$_REQUEST['qtyrecev'];
 $quantityaccept=$_REQUEST['accepted'];
 $quantityrej=$_REQUEST['rejected'];
 
/* $sql='insert into quantitycontrol set ponumber="'.$ponumber.'",itemcode="'.$itemcode.'",quantity="'.$qtyrecev.'",
 quantityaccept="'.$quantityaccept.'",quantityrej="'.$quantityrej.'"';*/
 
/* $quantityreject=$qtyrecev-$quantityaccept;
 
 
 $sql='update grns set qtyaccept="'.$quantityaccept.'",qtyreject="'.$quantityreject.'"  where grnno="'.$GRNNumber.'"';

 $result = DB_query($sql,$db);
 
 */
    $grnno=$_REQUEST['grnno'];
 	$GRNNumber=$_REQUEST['GRNNumber'];
	$accepted=$_REQUEST['accepted'];
	$rejected=$_REQUEST['rejected'];
	
	
	
	$sql="select * from quantitycontrol where grnbatch='$GRNNumber'";
	$oh_rs = DB_query($sql,$db,$ErrMsg);
	if(!$oh_rs)
	{
		$sql="INSERT into quantitycontrol (grnbatch,grnno,quantityaccept,quantityrej) values";
		$i=0;
		foreach($grnno as $v)
		{
			$sql.="('$GRNNumber','$v','$accepted[$i]','$rejected[$i]'),";
			$i++;
		}
		$sql=substr($sql,0,-1); 
		$result = DB_query($sql,$db,$ErrMsg);
	}
	else
	{
		
		$result = DB_query("delete from quantitycontrol where grnbatch='$GRNNumber'",$db,$ErrMsg);
		
		$sql="INSERT into quantitycontrol (grnbatch,grnno,quantityaccept,quantityrej) values";
		$i=0;
		foreach($grnno as $v)
		{
			$sql.="('$GRNNumber','$v','$accepted[$i]','$rejected[$i]'),";
			$i++;
		}
		$sql=substr($sql,0,-1); 
		$result = DB_query($sql,$db,$ErrMsg);
	}
 
 echo "<meta http-equiv='Refresh' content='0; url=" . $rootpath ."/QualityControl.php?" . SID . "'>";

}
?>

<?php
if (isset($_GET['GRNNumber'])){

$GRNNumber=$_GET['GRNNumber'];


	 echo '<br><div class=centre><font color=BLUE size=3><b>'.$GRNNumber.'</b> - ' . _('GRN Number') . '</font><div></br>';
	 
	  $sql = 'SELECT * FROM grns left JOIN suppliers  ON (grns.supplierid = suppliers.supplierid)   where grnbatch="'.$GRNNumber.'" '; 

// join purchorderdetails on (grns.podetailitem=purchorders.orderno)join stockmaster on(grns.itemcode=stockmaster.stockid)";
	$result = DB_query($sql,$db);
	$myrow=DB_Fetch_array($result);

	echo "<br><table border=1 >\n";
		printf("<tr><td class='label'>Vendor Code</td><td>".$myrow['supplierid']."</td></tr>
					<tr><td class='label'>Vendor Name</td><td>".$myrow['suppname']."</td>
            		</tr>");
					
echo "<form action='" . $_SERVER['PHP_SELF'] . "?". SID ."' method=post>";
echo"<input type='hidden' name='GRNNumber' value='".$GRNNumber."' />";			
echo "<table border=1 style='margin-top:40px;'>\n";
	echo '<tr><th>' . _('Item Code') . '</th>
	        <th>' . _('Item Description') . '</th>
			<th>' . _('Qty Received') . '</th>
			<th>' . _('Qty Accepted') . '</th>
			<th>' . _('Qty Rejected') . '</th>
            
           </tr>';

	$k=0; //row colour counter
	
$sqlselect = 'SELECT * FROM grns where grns. grnbatch="'.$GRNNumber.'"'; 
$resultselect = DB_query($sqlselect,$db);


	while ($myrowresult=DB_Fetch_array($resultselect)) {
	$sql= 'SELECT * FROM quantitycontrol where grnno="'.$myrowresult['grnno'].'"'; 
    $result = DB_query($sql,$db);
	$row=DB_Fetch_array($result);
		if ($k==1){
			echo '<tr class="EvenTableRows">';
			$k=0;
		} else {
			echo '<tr class="OddTableRows">';
			$k=1;
		}
		if($myrowresult['qtyrecd']>0){
		printf("<td> ".$myrowresult['itemcode']."</td>
            		<td>".$myrowresult['itemdescription']."</td>
					<td>".$myrowresult['qtyrecd']."</td>
					<td><input type='text' name='accepted[]' id='accepted'  value='".$row['quantityaccept']."'/><input type='hidden' name='grnno[]' value='".$myrowresult['grnno']."'></td>
					<td><input type='text' name='rejected[]' id='rejected'  value='".$row['quantityrej']."'/></td>
					
            		</tr>");
  
 }           		 
} 
echo'<tr><td colspan="5"><div align="right"><input type="submit" name="submit" value="Submit" /></div></td></tr>
</table>';
}
include('includes/footer.inc');
?>