<?php 
ob_start();
/* $Revision: 1.31 $ */

/*
*      AddQualityposting.php
*
*      webERP Linux-Apache-MySQL-PHP based accounting system
*      Copyright Logic Works Ltd - 2007 - Contact: info@acgil.com
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
*  MA 02110-1301, USA.
*
*
*  modifications and add-ons started from:
*   Revision: 1.18
*  by Thomas Pulina
*  <hollerland@nord-com.net>|<thomas.pulina@wagner-bremen.de>
*  2008
*
*/

$PageSecurity = 4;
$PageName ='Quality Control';	
include('includes/DefinePOClass.php');
include('includes/session.inc');
if (isset($_GET['ModifyOrderNumber']) and $_SESSION[UserStockLocation]== "H001")  {
	$title = _('Modify Purchase Order') . ' ' . $_GET['ModifyOrderNumber'];
} else {
	//$title = _('Add Issue Slip');
}
    
 if(isset($_GET['issueid'])  && isset($_GET['issueid']) !='')
 {
  $title = _('Update Quality Posting');
 }
 else
 {
  $title = _('Add Quality Posting');
 }

include('includes/header.inc');
include('includes/SQL_CommonFunctions.inc');

/*If the page is called is called without an identifier being set then
 * it must be either a new order, or the start of a modification of an
 * order, and so we must create a new identifier.
 * 
 * The identifier only needs to be unique for this php session, so a 
 * unix timestamp will be sufficient.
 */
 
/*$date1=date('M-Y');
$date2=date('Y-m-d');*/



 
		if (empty($_GET['identifier'])) {
			 $identifier=date('U');
		} else {
			 $identifier=$_GET['identifier'];
		}
		

	
    $Prefix_SQL = "Select prefix_id,prefix_code,prefix_name from prefix_tbl Where prefix_name ='".$PageName."'";
	$Prefix_Result = DB_query($Prefix_SQL,$db);
	$Prefix_Row = DB_fetch_array($Prefix_Result);
	$Prefix_code = $Prefix_Row['prefix_code'];
	$Prefix_date = date("Y");
	$Prefix_date++;
	$CurrentFinancialYear = date("Y")."-".$Prefix_date; 
?>
<link rel="stylesheet" type="text/css" href="script/tabcontent.css" />
<script type="text/javascript" src="script/tabcontent.js"></script>
<script type="text/javascript" src="script/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/dynamicRow.js"></script>
<script type="text/javascript">
function showbalance(str)
{
    var StockBalance=document.getElementById("stockbal").value;	
	var Issuequantity=document.getElementById("issueqty").value;	
	var RemaningBalance =StockBalance-Issuequantity;
	document.getElementById('remaningbalance').value =RemaningBalance;
}

</script>

<link rel="stylesheet" href="css/jquery.ui.all.css">
<script src="js/jquery-1.5.1.js"></script>
<script src="ui/jquery.ui.core.js"></script>
<script src="ui/jquery.ui.widget.js"></script>
<script src="ui/jquery.ui.datepicker.js"></script>

<script>
$(function() {
	$( ".datepicker" ).datepicker({
		showOtherMonths: true,
		selectOtherMonths: true
	});
});
</script>
<script type="text/JavaScript">  
function show(id)  
{  
	 if (document.getElementById(id).style.display == 'none')  
     {  
          document.getElementById(id).style.display = '';  
     }  
}  
 
function hide(id)  
{  
  document.getElementById(id).style.display = 'none';  
}  
</script> 

<script>
function Material_Receipt_Note()
{
var mrn=document.getElementById("mrn").value;
location.replace("Qualityposting.php?MRNID="+mrn);
}
</script>

<script>
function Record_Search()
{
	if(document.getElementById('datepicker').value ==''){
		 alert ('Please Select Start Date');
		 document.getElementById('datepicker').focus()
		 return false;
	  }
  	if(document.getElementById('datepicker1').value ==''){
		 alert ('Please Select End Date');
	 	document.getElementById('datepicker1').focus()
	 return false;
  	}
}
</script>
<?php 
		/*Page is called with NewOrder=Yes when a new order is to be entered
		 * the session variable that holds all the PO data $_SESSION['MR'][$identifier] 
		 * is unset to allow all new details to be created */
		///////////////////////////////////////////// Add IUssue Slip  Form  By Amit Kumar////////////////////////////////////////
	 $date = date($_SESSION['DefaultDateFormat']);
	if(isset($_REQUEST['MRNID']) && $_REQUEST['MRNID']!="")
	{
	 $MRNID = $_REQUEST['MRNID'];
     $mrn_sql = "SELECT * FROM addmrn WHERE id='".$MRNID."'";
	 $MRN_Result =DB_query($mrn_sql,$db);
	 $MRN_Row = DB_fetch_array($MRN_Result);
	 $MRNNo = $MRN_Row['MRNNo'];
	 $mrnid = $MRN_Row['id'];
	 $supplierno = $MRN_Row['supplierno']; 
	 $Suppliername  = $MRN_Row['suppliername']; 

	if(isset($_POST['Submit']))
	{
	    $_POST['PRDate']=Date($_SESSION['DefaultDateFormat']);
		$_POST['DatePrinted']=Date($_SESSION['DefaultDateFormat']);
		ConvertSQLDate($_SESSION['MR'.$identifier]->Orig_OrderDate);
		 $sql='select id, QCno from qualityposting order by id desc limit 0,1';
		 $result = DB_query($sql,$db);
		  $numrows = DB_num_rows($result);
		 if($numrows>0)
		 {
				 $i=0;
				 $strmax = "";
				 $myrow=DB_Fetch_array($result);
				 
				 if($myrow['QCno'] !='')
				 {
						 $val= substr($myrow['QCno'],19);
						   $strChar = substr($myrow['QCno'],0,19);
						
					
						 if($val!="")
						 {
							 $arr[$i] = $val;
							 $i++;
						}
				
				if(is_array($arr))
				{ 
					$maxval =  max($arr);
				}
				else
				{
					$maxval=0;
				}
			}
			else
			{
				$strChar = $Prefix_code."/".$_SESSION[UserStockLocation]."/".$CurrentFinancialYear."/";
			}
		  }
		$strFinal = $maxval+1;
		$strBothFinal = $strChar.str_pad($strFinal,4,"0",STR_PAD_LEFT);
	    $qty=$_REQUEST['NumofRow'];
		$mrnno= $_REQUEST['mrnno'];
		$mrnid= $_REQUEST['mrnid'];
		  for($i=1;$i<$qty;$i++)
			 {
        $sql = "INSERT INTO qualityposting (`id`,
										`ItemCode`, 
										`desc`, 
										`Unit`, 
										`ChallanQty`, 
										`Shortage`, 
										`Usable`,
										`RejectedQty`, 
										`PassedQty`, 
										`Remarks`, 
										`createdby`,
										`MRN_No`,
										`MRNID`,
										`status`,
										`date`,
										`QCno`,
										`storecode`,
										`storename`,
										`site_puraches`)
										VALUES('Null',
										'".$_POST['itemcode'][$i]."', 
										'".$_POST['desc'][$i]."', 
										'".$_POST['MRNUnit'][$i]."',
										'".$_POST['MRNchallanQty'][$i]."',
										'".$_POST['MRNstorage'][$i]."', 
										'".$_POST['MRNusable'][$i]."', 
										'".$_POST['MRNrejectqty'][$i]."', 
										'".$_POST['MRNpassqty'][$i]."', 
										'".$_POST['MRNremarks'][$i]."', 
										'".$_SESSION[UserID]."', 
										'".$MRNNo."',
										'".$_REQUEST['MRNID']."',
										'".Pending."',
										'".FormatDateForSQL($date)."',
										'".$strBothFinal."',
										'".$_POST['storecode']."',
										'".$Suppliername."',
										'".$_SESSION[UserStockLocation]."')";
							$ErrMsg = _('An error occurred Record Note Save') . '. ' . _('The error says');
							$insertResult = DB_query($sql,$db,$ErrMsg);
					}
				  if($insertResult == true)
					{
					?>
					 <br/>
						<div align="center"> <a href="Qualityposting.php">Back To Quality Posting </a></div>
						<br/>
						<br/>
					
					<?
					 prnMsg( _(''.$MRNNo.' has been Waiting success!'), 'success');
						 exit;
					}
			   
				else
				{
				 $errormsg = "Record Not Save Successfully";
				}		
	 }
  }else
  {
  
  if(isset($_POST['Search']))
	{
		$fromexp = explode("/", $_POST['fromedate']);
		$fromdate = $fromexp[2]."-".$fromexp[0]."-".$fromexp[1];
		$toexp = explode("/", $_POST['todate']);
		$todate = $toexp[2]."-".$toexp[0]."-".$toexp[1];
		if(isset($_POST['radiobutton']) =='MRN Waiting For QC'){
			$sql="Select * from qualityposting Where status='Pending' AND date between '".$fromdate."'and '".$todate."'";
		}elseif (isset($_POST['radiobutton']) =='MRN Under QC'){
			$sql="Select * from qualityposting Where status='Pending' AND date between '".$fromdate."'and '".$todate."'";
		}elseif( isset($_POST['radiobutton']) =='QC Posted MRN'){
			$sql="Select * from qualityposting Where status='Approve' AND date between '".$fromdate."'and '".$todate."'";
		}
		$selectresult = DB_query($sql,$db);
		$numrows = DB_num_rows($selectresult);
    }
  } 

	if(isset($_REQUEST['id'])!='' && isset($_REQUEST['mod'])=='Approve' && isset($_REQUEST['status'])==2)
	{
	 
				   $id          = $_REQUEST['id'];
				   $status      = $_REQUEST['mod'];
				   $mrnid       = $_REQUEST['mrnid'];
				   $itemcode    = $_REQUEST['itemcode'];
				   $itemquty    = $_REQUEST['itemquty'];
				   $rejquantity = $_REQUEST['rejquantity'];
				   $QtyReceived = $_REQUEST['receiveqty'];
				  
					  $QC_SQL    				= "Select * from qualityposting Where `status`='". $status ."' And `id`='". $id ."'";
					  $QC_Result 				= DB_query($QC_SQL,$db,$ErrMsg);
					  $QC_row    				= DB_Fetch_array($QC_Result);
					  $QC_passQuantity 			= $QC_row['PassedQty'];
					  $QC_RejQuantity 			= $QC_row['RejectedQty'];
					  $QC_ShortageQuantity 		= $QC_row['Shortage'];
					  $QC_ChallanQtyQuantity 	= $QC_row['ChallanQty']; 
					  $QC_Defaltrecive 			= $QC_row['date'];
					  $QC_Sitecode 				= $QC_row['site_puraches'];
					  $QC_Suppliercode 			= $QC_row['storecode'];
					  $QC_UsableQuantity 		= $QC_row['Usable'];

				   $update_query = "UPDATE qualityposting SET `status`='".$status."' Where `id`='".$id."'";
				   $Aproveresult = DB_query($update_query,$db,$ErrMsg);
					if($Aproveresult==true)
					{
					
					    $Stock_SQL="Select stockid, description from stockmaster Where stockid='".$itemcode."'";	
						$Stock_Result=DB_query($Stock_SQL,$db,$ErrMsg);
						$Stock_row=DB_Fetch_array($Stock_Result);
						$ItemDescription = $Stock_row['description'];
						$sql="Select * from qualityposting Where MRNID='".$mrnid."' AND `status`='Pending'";				
						$result=DB_query($sql,$db,$ErrMsg);
						$numrows = DB_num_rows($result);
						$selsql="Select addmrn.id, addmrn.MRNNo, addmrn.order_no from addmrn Where id='".$mrnid."'";	
						$selresult=DB_query($selsql,$db,$ErrMsg);
						$setrow=DB_Fetch_array($selresult);
						$mrnno = $setrow['MRNNo'];
						$SupplierId = $setrow['supplierno'];
						$OrderNo = $setrow['order_no'];
						$PODetail_SQL = "UPDATE purchorderdetails SET quantityrecd=purchorderdetails.quantityrecd +'".$rejquantity."' Where orderno='".$OrderNo."' AND itemcode='".$itemcode."'";
						$POresult=DB_query($PODetail_SQL,$db,$ErrMsg);
                    	//$SQL = "UPDATE locstock SET quantity = locstock.quantity + " . $itemquty . " WHERE locstock.stockid = '" . $itemcode . "'	AND loccode = '" . $_SESSION['UserStockLocation'] . "'";
						$Excuted_Query =DB_query($SQL,$db,$ErrMsg);
						
						if ($itemcode !='') { /*Its a stock item line */
				/*Need to get the current standard cost as it is now so we can process GL jorunals later*/
			 	$SQL = "SELECT materialcost + labourcost + overheadcost as stdcost
						FROM stockmaster
						WHERE stockid='" . $itemcode . "'";
				$ErrMsg =  _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The standard cost of the item being received cannot be retrieved because');
				$DbgMsg = _('The following SQL to retrieve the standard cost was used');
				$Result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
				$myrow = DB_fetch_row($Result);
               
				if ($QtyReceived=0){ //its the first receipt against this line
					$StandardCost = $myrow[0];
				}
				$CurrentStandardCost = $myrow[0];

				/*Set the purchase order line stdcostunit = weighted average standard cost used for all receipts of this line
				 This assures that the quantity received against the purchase order line multiplied by the weighted average of standard
				 costs received = the total of standard cost posted to GRN suspense*/
				echo  $StandardCost = (($CurrentStandardCost * $ReceiveQty) + ($QtyReceived)) / ($ReceiveQty + $QtyReceived);

			}
			echo 	$SQL = "INSERT INTO grns (grnbatch,
						podetailitem,
						itemcode,
						itemdescription,
						deliverydate,
						qtyrecd,
						supplierid,
						stdcostunit,
						qtyaccept,
						qtyreject)
				VALUES ('" . $mrnno . "',
					'" . $itemquty . "',
					'" . $itemcode . "',
					'" . $ItemDescription . "',
					'" . $QC_Defaltrecive . "',
					" . $QtyReceived . ",
					'" . $SupplierId . "',
					'" . $CurrentStandardCost ."',
					'" . $QC_passQuantity."',
					'" . $rejquantity ."')";

			$ErrMsg =  _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('A GRN record could not be inserted') . '. ' . _('This receipt of goods has not been processed because');
			$DbgMsg =  _('The following SQL to insert the MRN record was used');
			$Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
			
			
						if ($itemcode!=''){ /* if the order line is in fact a stock item */

/* Update location stock records - NB  a PO cannot be entered for a dummy/assembly/kit parts */

/* Need to get the current location quantity will need it later for the stock movement */
			echo 	$SQL="SELECT locstock.quantity
					FROM locstock
					WHERE locstock.stockid='" . $itemcode . "'
					AND loccode= '" .$QC_Sitecode . "'";

				$Result = DB_query($SQL, $db);
				if (DB_num_rows($Result)==1){
					$LocQtyRow = DB_fetch_row($Result);
					echo $QtyOnHandPrior = $LocQtyRow[0];
				} else {
					/*There must actually be some error this should never happen */
					$QtyOnHandPrior = 0;
				}

				$sql='SELECT conversionfactor
					FROM purchdata
					WHERE supplierno="'. $QC_Suppliercode .'"
					AND stockid="'.$itemcode.'"';
				$result=DB_query($sql, $db);
				if (DB_num_rows($result)>0) {
					$myrow=DB_fetch_array($result);
					$conversionfactor=$myrow['conversionfactor'];
				} else {
					$conversionfactor=1;
				}
				$OrderLine->ReceiveQty=$OrderLine->ReceiveQty*$conversionfactor;

			echo 	$SQL = "UPDATE locstock
					SET quantity = locstock.quantity + " . $OrderLine->ReceiveQty . "
					WHERE locstock.stockid = '" . $OrderLine->StockID . "'
					AND loccode = '" . $_SESSION['PO']->Location . "'";

				$ErrMsg =  _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The location stock record could not be updated because');
				$DbgMsg =  _('The following SQL to update the location stock record was used');
				$Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);


	/* If its a stock item still .... Insert stock movements - with unit cost */

			echo 	$SQL = "INSERT INTO stockmoves (stockid,
								type,
								transno,
								loccode,
								trandate,
								price,
								prd,
								reference,
								qty,
								standardcost,
								newqoh)
					VALUES ('" . $itemcode . "',
						25,
						" . $id . ", '" . $QC_Suppliercode . "',
						'" . $QC_Defaltrecive . "',
						'" . $LocalCurrencyPrice . "',
						'" . $PeriodNo . "',
						'" . $QC_Suppliercode . "',
						 '" . $QC_UsableQuantity . "',
						'" . $StandardCost . "',
						'" . ($QtyOnHandPrior + $QC_RejQuantity) . "')";

				$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('stock movement records could not be inserted because');
				$DbgMsg =  _('The following SQL to insert the stock movement records was used');
				$Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);

				/*Get the ID of the StockMove... */
				$StkMoveNo = DB_Last_Insert_ID($db,'stockmoves','stkmoveno');
				/* Do the Controlled Item INSERTS HERE */

				/*if ($OrderLine->Controlled ==1){
					foreach($OrderLine->SerialItems as $Item){*/
						/* we know that StockItems return an array of SerialItem (s)
						 We need to add the StockSerialItem record and
						 The StockSerialMoves as well */
						//need to test if the controlled item exists first already
							echo $SQL = "SELECT COUNT(*) FROM stockserialitems
									WHERE stockid='" .$itemcode . "'
									AND loccode = '" . $QC_Sitecode . "'
									AND serialno = '" . $mrnid . "'";
							$ErrMsg =  _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Could not check if a batch or lot stock item already exists because');
							$DbgMsg =  _('The following SQL to test for an already existing controlled but not serialised stock item was used');
							$Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
							$AlreadyExistsRow = DB_fetch_row($Result);
							if (trim($mrnid) != ""){
								if ($AlreadyExistsRow[0]>0){
									if ($OrderLine->Serialised == 1) {
										$SQL = 'UPDATE stockserialitems SET quantity = ' . $QC_UsableQuantity . ' ';
									} else {
										$SQL = 'UPDATE stockserialitems SET quantity = quantity + ' . $QC_UsableQuantity . ' ';
									}
									$SQL .= "WHERE stockid='" . $itemcode . "'
											 AND loccode = '" .$QC_Sitecode . "'
											 AND serialno = '" . $Item->BundleRef . "'";
								} else {
								echo 	$SQL = "INSERT INTO stockserialitems (stockid,
												loccode,
												serialno,
												qualitytext,
												quantity)
											VALUES ('" . $itemcode . "',
												'" . $QC_Sitecode . "',
												'',
												'',
												'" . $QC_UsableQuantity . "')";
								}
								$ErrMsg =  _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The serial stock item record could not be inserted because');
								$DbgMsg =  _('The following SQL to insert the serial stock item records was used');
								$Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
//assetmanager								
							/** end of handle stockserialitems records */

							/** now insert the serial stock movement **/
						echo 	$SQL = "INSERT INTO stockserialmoves (stockmoveno,
											stockid,
											serialno,
											moveqty)
									VALUES (" . $StkMoveNo . ",
										'" . $itemcode . "',
										'',
										'" .$QC_UsableQuantity."')";
							$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The serial stock movement record could not be inserted because');
							$DbgMsg = _('The following SQL to insert the serial stock movement records was used');
							$Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
						}//non blank BundleRef
					//} //end foreach
				//}
			} 
			if($numrows ==0)
			{
			 $sql = "UPDATE addmrn SET `mrnstatus`='". $status ."' Where `id`='".$mrnid."'";
			 $Setresult=DB_query($sql,$db,$ErrMsg);
			 if($Setresult ==true)
			 {
			 ?>
			 <br/>
			<div align="center"> <a href="Qualityposting.php">Back To Quality Posting </a></div>
			<br/>
			<br/>
			  <?
				 prnMsg( _('MRN has been Approve Successfully !'), 'success');
				 exit;
			  }
			} 
			?>
		 <br/>
			<div align="center"> <a href="Qualityposting.php">Back To Quality Posting </a></div>
			<br/>
			<br/>
			<?
		   prnMsg( _('MRN has been Approve Successfully !'), 'success');
		}			   
				exit;
			}
 ?>
 
 
<form name="form1"  action="" method="post">
<table width="100%" border="2" align="center" cellpadding="0" cellspacing="0">

  <tr>
    <td width="57%" height="40" align="center" bgcolor="#999999"> <div align="center"><b><font color="#FFFFFF"><?php echo $title; ?></font></b></div> </td>
  </tr>
  <tr>
    <td height="20" align="center" bgcolor="#999999"> <div align="Left"><b><font color="#FFFFFF">General Details</font></b></div> </td>
  </tr>
  
  
  <tr>
    <td><table width="100%" border="1" cellspacing="0" cellpadding="0">
      <tr>
        <td width="13%">&nbsp;&nbsp;Status</td>
       <td width="2%">
	   <?php if(isset($_POST['radiobutton'])=='MRN Waiting For QC')
	   {
	   	$ck = "checked";
	   }else{
	   	$ck = '';
	   } ?>
		<input name="radiobutton" type="radio" value="MRN Waiting For QC"  onfocus="hide('tblB');hide('tblC');show('tblA');" <?php echo $ck; ?> checked="checked"/></td>
		<td width="16%">
		MRN Waiting For QC</td>
		<td width="3%">
		<?php if(isset($_POST['radiobutton'])=='MRN Under QC')
	   {
	   	$ck = "checked";
	   }else{
	   	$ck = '';
	   } ?>
		<input name="radiobutton" type="radio" value="MRN Under QC" onfocus="hide('tblA');hide('tblC');show('tblB');return true;" <?php echo $ck; ?> /></td>
		<td width="15%">
		MRN Under QC</td>
		<td width="3%">
		<?php if(isset($_POST['radiobutton'])=='QC Posted MRN')
	   {
	   	$ck = "checked";
	   }else{
	   	$ck = '';
	   } ?>
		<input name="radiobutton" type="radio" value="QC Posted MRN" onfocus="hide('tblA');hide('tblB');show('tblC');return true;" <?php echo $ck; ?> /></td>
		<td width="48%">
		QC Posted MRN</td>
		</tr>
    </table>
	</td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="1%">&nbsp;</td>
        <td width="12%">From Date </td>
        <td width="37%"><input type="text" name="fromedate" class="datepicker"  id="datepicker"/></td>
        <td width="6%">To Date </td>
        <td width="27%"><input type="text" name="todate"  id="datepicker1" class="datepicker"  /></td>
        <td width="17%"><input name="Search" type="submit" id="Search" value="Search" onclick="return Record_Search();" /></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="12%">&nbsp;&nbsp;MRN No. Wise </td>
        <td width="19%">
		<?php 
		 $sql_query = "select addmrn.id, addmrn.MRNNo, addmrn.mrnstatus from addmrn Where mrnstatus ='Pending'";
		 $MRNresult = DB_query($sql_query,$db);
		?>
		<select name="mrn" id="mrn" onChange="Material_Receipt_Note()">
		<option value="">Select </option>
		<?php  while($mrnrow=DB_fetch_array($MRNresult)) { ?>
		<option value="<?php echo $mrnrow['id']; ?>" <?php if($mrnrow['id']==$mrnid) { echo "Selected";  }   ?>><?php echo $mrnrow['MRNNo'];  ?> </option>
		<?php } ?>
		</select>		</td>
        <td width="19%">&nbsp;</td>
        <td width="6%">Store</td>
        <td width="16%"><input type="text" name="storecode"  id="mrnno" value="<?php echo $supplierno; ?>"/></td>
        <td width="11%">QC Doc No. </td>
        <td width="17%"><input type="text" name="qcdocno"  readonly id="storecode"/></td>
      </tr>
    </table></td>
  </tr>
  
  
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td bgcolor="#999999"><b><font color="#FFFFFF">Item Details</font></b> </td>
  </tr>
 
  <tr>
    <td align="center" colspan="2">
	
	<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tblA" style="DISPLAY:block;">
  <tr>
    <td align="center"> 
	<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0"  >
  <tr>
  <td width="5%" >S No. </td>
    <td width="11%">Item Code (I)</td>
    <td width="11%">Description/Item Specification(II)</td>
    <td width="5%">Unit (III)</td>
    <td width="10%">Challan Qty (IV) </td>
    <td width="15%">Shortage/Unserviceable (V)</td>
    <td width="10%">Recd.Usable(VI)</td>
    <td width="10%">Rejected Qty (VII)</td>
    <td width="13%">Passed  Qty (VIII =VI - VII) </td>
    <td width="10%">Remarks (IX)</td>
	</tr>
   <tr>

     <td colspan="10">&nbsp;</td>
     </tr>
    <tr>
 <td colspan="10" valign="top" >

 
 <table width="100%" border="1" cellspacing="0" cellpadding="0" id="txtid">
  <?php 
 $sql="Select * from addmrnqty Where issue_id ='".$_REQUEST['MRNID']."'";
 $result = DB_query($sql,$db);
 $numrows = DB_num_rows($result);
 ?>
<?php
 	for($i=1; $i<=$numrows; $i++)
	{ 
		$myrow=DB_Fetch_array($result);
		$mrnno= $exp[1];
	?>
   <tr>
     <td width="16%"><?php echo $i;?>
	 <input type="hidden" name="mrnid" id="mrnid" value="<?php echo $exp[0];?>" />
	 <input type="hidden" name="mrnno" id="mrnno" value="<?php echo $exp[1];?>" />
	 <input type="text"  readonly=""  name="itemcode[<?php echo $i;?>]"size="15" value="<?php echo $myrow['itemcode'];?>" /></td>
     <td width="14%">
	 <input type="text" name="desc[<?php echo $i;?>]"  id="<?php echo $i;?>"  size="20" value="<?php echo $myrow['desc'];?>"/>
	 </td>
     <td width="9%">
	 <input name="MRNUnit[<?php echo $i;?>]" type="text" id="<?php echo $i;?>" size="10" value="<?php echo $myrow['UOM']; ?>" />
	 </td>
     <td width="9%">
	 <input name="MRNchallanQty[<?php echo $i;?>]" type="text" id="<?php echo $i;?>" size="10" value="<?php echo $myrow['reciveqty']; ?>" />
	 </td>
     <td width="9%">
	 <input name="MRNstorage[<?php echo $i;?>]" type="text" id="<?php echo $i;?>"  size="10" value="" />
	 </td>
     <td width="8%">
	 <input name="MRNusable[<?php echo $i;?>]" type="text"  id="MRNusable_<?php echo $i;?>" size="10"  value=""/>
	 </td>
     <td width="11%">
	 <input type="text" name="MRNrejectqty[<?php echo $i;?>]"  id="MRNrejectqty_<?php echo $i;?>" size="13" value="" onblur="Fiend_Passed_Qty(this.value,'<?php echo $i; ?>')"  />
	 </td>
     <td width="8%">
	 <input type="text" name="MRNpassqty[<?php echo $i;?>]" id="MRNpassqty_<?php echo $i;?>"  size="13" value=""  />
	 </td>
     <td width="16%">
	 <input type="text" name="MRNremarks[<?php echo $i;?>]" id="<?php echo $i;?>"  size="30"   />	
	 </td>
     </tr>
	 <tr>
	 <td>&nbsp;</td>
	 <td>&nbsp;</td>
	 <td>&nbsp;</td>
	 <td>&nbsp;</td>
	 <td>&nbsp;</td>
	 <td>&nbsp;</td>
	 <td>&nbsp;</td>
	 </tr>
  <?php } ?>
  <input type="hidden" name="NumofRow" value="<?php echo $i;?>" />
 </table>
 
 </td>
  </tr>
     <tr><td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td><input type="submit" name="Submit" value="Submit"/></td>
     <td><input type="submit" name="Submit" value="Cancel" onclick=""/></td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     </tr>
  </table>
  
  </td>
  </tr>
</table>

</td>
  </tr>
  <tr>
    <td>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tblB" style="display:none;">
  <tr>
    <td><table width="100%" border="1" cellspacing="0" cellpadding="0" >
  <tr>
  <td width="2%">&nbsp;</td>
    <td width="14%">Item Code (I)</td>
    <td width="11%">Description/Item Specification(II)</td>
    <td width="5%">Unit (III)</td>
    <td width="10%">Challan Qty (IV) </td>
    <td width="17%">Shortage/Unserviceable (V)</td>
    <td width="11%">Recd.Usable(VI)</td>
    <td width="10%">Rejected Qty (VII)</td>
    <td width="8%">Passed  Qty (VIII =VI - VII) </td>
    <td width="12%">Remarks (IX)</td>
	</tr>
   <tr>

     <td colspan="10">&nbsp;</td>
     </tr>
    <tr>
 <td colspan="10">
 <?php 
  $sql="Select * from qualityposting Where MRNID='".$_REQUEST['MRNID']."' AND `status`='Pending'";
  $result = DB_query($sql,$db);
  $numrows = DB_num_rows($result);
?>
 <table width="100%" border="2" cellspacing="0" cellpadding="0" >
<?php
if($numrows>0)
{
 	for($i=1; $i<=$numrows; $i++)
	{ 
	     $myrow=DB_Fetch_array($result);
?>
   <tr>
     <td width="11%">
	 
	 <input type="hidden" name="mrnid" id="mrnid" value="<?php echo $q;?>" />
	
	 <input type="text"  readonly=""  name="itemcode[<?php echo $i;?>]"size="15" value="<?php echo $myrow['ItemCode'];?>" /></td>
     <td width="12%">
	 <input type="text" name="desc1[<?php echo $i;?>]"   size="20" value="<?php echo $myrow['desc'];?>"/>	 </td>
     <td width="5%">
	 <input name="MRNUnit1[<?php echo $i;?>]" type="text"  size="10" value="<?php echo $myrow['Unit']; ?>" />	 </td>
     <td width="6%">
	 <input name="MRNchallanQty1[<?php echo $i;?>]" type="text"  size="10" value="<?php echo $myrow['ChallanQty']; ?>" />	 </td>
     <td width="6%">
	 <input name="MRNstorage1[<?php echo $i;?>]" type="text"  size="10" value="<?php echo $myrow['Shortage']; ?>" />	 </td>
     <td width="6%">
	 <input name="MRNusable1[<?php echo $i;?>]" type="text"  size="10"  value="<?php echo $myrow['Usable']; ?>"/>	 </td>
     <td width="8%">
	 <input type="text" name="MRNrejectqty1[<?php echo $i;?>]"  size="13" value="<?php echo $myrow['RejectedQty']; ?>"  />	 </td>
     <td width="8%">
	 <input type="text" name="MRNpassqty1[<?php echo $i;?>]"  size="13" value="<?php echo $myrow['PassedQty']; ?>"  />	 </td>
     <td width="27%">
	 <input type="text" name="MRNremarks1[<?php echo $i;?>]"  size="20"  value="<?php echo $myrow['Remarks']; ?>"  />	 </td>
     <td width="4%"><a href="Qualitypostingedit.php?mrnid=<?php echo $myrow[12];?>&id=<?php echo $myrow['id'];?>&mod=edit&status=1">Edit</a>|</td>
     <td width="7%">
	 <a href="Qualityposting.php?mrnid=<?php echo $myrow[12];?>&id=<?php echo $myrow['id'];?>&itemquty=<?php echo $myrow['Usable']; ?>&rejquantity=<?php echo $myrow['RejectedQty'];?>&itemcode=<?php echo $myrow['ItemCode'];?>&receiveqty=<?php echo $myrow['PassedQty'];?>&usable=<?php echo $myrow['Usable'];?>&challqty=<?php echo $myrow['ChallanQty'];?>&mod=Approve&status=2">approved</a></td>
  </tr>
  
  <?php } } else { ?>
  
   <tr>
     <td colspan="11"><div align="center"><font color="#FF0000"> <b>Sorry ! Record Not Found !</b>	</font></div> 			 	 	 		 	 	 			 		 	 	 		</td>
  </tr>
  <?php }  ?>
  
  <input type="hidden" name="NumofRow" value="<?php echo $i;?>" />
  <input type="hidden" name="update" value="UPDATE" id="update" />
  </table>
 </td>
  </tr>
  
  
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td><input type="submit" name="Update" value="Update"/></td>
     <td><input type="submit" name="Cancel" value="Cancel"/></td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
	  </tr>
  </table></td>
  </tr>
</table>
</td>
  </tr>
  <tr>
    <td>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tblC" style="DISPLAY: none">
  <tr>
    <td><table width="100%" border="1" cellspacing="0" cellpadding="0" id="txtdetail">
  <tr>
 
    <td width="14%">Item Code (I)</td>
    <td width="11%">Description/Item Specification(II)</td>
    <td width="5%">Unit (III)</td>
    <td width="10%">Challan Qty (IV) </td>
    <td width="17%">Shortage/Unserviceable (V)</td>
    <td width="11%">Recd.Usable(VI)</td>
    <td width="10%">Rejected Qty (VII)</td>
    <td width="8%">Passed  Qty (VIII =VI - VII) </td>
    <td width="12%">Remarks (IX)</td>
	</tr>
  
	 <?php   
	    $select_sql="Select * from qualityposting  Where `status`='Approve' AND MRNID='".$_GET['MRNID']."'";
 	    $selectresult = DB_query($select_sql,$db);
	    $numrows = DB_num_rows($selectresult);
		   if($numrows >0)
		   { 
	        while($row =DB_Fetch_array($selectresult)) { 
	 ?>
	 <tr>
 <td colspan="10">
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
 
   <tr>
     <td width="15%"><font color="#000000"><?php echo $row['ItemCode']; ?></font></td>
     <td width="10%"><?php echo $row['desc']; ?></td>
     <td width="10%"><?php echo $row['Unit']; ?></td>
     <td width="10%"><?php echo $row['ChallanQty']; ?></td>
     <td width="10%"><?php echo $row['Shortage']; ?></td>
     <td width="10%"><?php echo $row['Usable']; ?></td>
     <td width="10%"><?php echo $row['RejectedQty']; ?></td>
     <td width="10%"><?php echo $row['PassedQty']; ?></td>
     <td width="15%"><?php echo $row['Remarks']; ?></td>
     </tr>
	</table>
 </td>
 </tr>
 <?php } } else {?>
 <tr>
     <td><div align="center"><font color="#FF0000"> <b>Sorry ! Record Not Found !</b>	</font></div></td>
     </tr>
	  <?php  } ?>
  </table></td>
  </tr>
</table>
</td>
    </tr>
</table>
<?php 
///echo "<pre>"; print_r( $_SESSION);


?>

</form>

<?
include('includes/footer.inc');
?>


<script type="text/javascript">

var countries=new ddtabcontent("countrytabs")
countries.setpersist(true)
countries.setselectedClassTarget("link") //"link" or "linkparent"
countries.init()

</script>




<script>
function Fiend_Passed_Qty(str,i)
{
    var MRNRejectQuantity = document.getElementById("MRNrejectqty_"+i).value;
	var MRNUsableQuantity = document.getElementById("MRNusable_"+i).value;
	var Passed_quantity = (MRNUsableQuantity - MRNRejectQuantity);
	document.getElementById("MRNpassqty_"+i).value=Passed_quantity;
}
</script>