<?php 
if($_REQUEST['mrnid']!=''){
	$sql=DB_fetch_array(DB_query("Select * from addmrn where id=".$_REQUEST['mrnid'],$db));
	extract($sql);
	
	$sql3=DB_query("Select mst_itemid from qc_incoming_inspection where mrn_id=".$_REQUEST['mrnid'],$db);
	while($row3=DB_fetch_array($sql3)){
		$arr2[]=$row3['mst_itemid'];
	}
	if(!empty($arr2)){
		$string2=implode(',',$arr2);
		$sql2=DB_query("Select mst_itemid from addmrnqty where issue_id=".$_REQUEST['mrnid']." and mst_itemid not in(".$string2.")",$db);	
	}else{
		$sql2=DB_query("Select mst_itemid from addmrnqty where issue_id=".$_REQUEST['mrnid'],$db);	
	}
	while($row2=DB_fetch_array($sql2)){
		$arr[]=$row2['mst_itemid'];
	}
}
if($_REQUEST['Save']){
    //echo $_REQUEST['qc_incoming_inspection']['sitecode'];
    $maxsql=DB_query("select * from qc_incoming_inspection order by id DESC",$db);
    $maxid=DB_fetch_assoc($maxsql);
   // echo DB_num_rows($maxsql);
    $prefix=DB_fetch_assoc(DB_query("select prefix_code from prefix_tbl where prefix_name='Incoming Material Inspection' ",$db));
    if(DB_num_rows($maxsql)<1)
      $cntr=1;
    else
      $cntr=$maxid['id']+1;
    
     $brnch=substr($_REQUEST['qc_incoming_inspection']['sitecode'],0,3);
     $finalstr=$prefix['prefix_code']."/".$brnch."/".$cntr;

    $_REQUEST['qc_incoming_inspection']['qc_no']=$finalstr;
	$_REQUEST['qc_incoming_inspection']['date']=FormatDateForSQL($_REQUEST['qc_incoming_inspection']['date']);
	$_REQUEST['qc_incoming_inspection']['sample_date']=FormatDateForSQL($_REQUEST['qc_incoming_inspection']['sample_date']);
	$sql1=$s->insertRecord('qc_incoming_inspection', $_REQUEST['qc_incoming_inspection']);
	$result1=DB_query($sql1,$db);
	$last_id=DB_Last_Insert_ID($db,'qc_incoming_inspection','id');
	if($last_id!=''){
		foreach($_REQUEST['qc_incoming_inspection_details'] as $key=>$val){
			$val['master_id']=$last_id;
			$sql2=$s->insertRecord('qc_incoming_inspection_details', $val);
			$result2=DB_query($sql2,$db);
		}
	}
	echo "<script>location.href='index.php?route=QC/transactions/goods_incoming_inspection_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="tab_div">
<center> <h2>Incoming Material Inspection</h2></center>
<div id="container">
<form action="" method="post">
<table width="100%" align="center" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td colspan="6"><center><h3>General Information</h3></center></td>
	</tr>
	<tr class="row1">
    <td width="13%"><strong>Branch</strong></td>
    <td width="20%" align="left" valign="top"><?php $SQLSite=DB_query("Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in ('".$_SESSION['AssignedBranch']."')",$db); ?>
     <select name="qc_incoming_inspection[sitecode]" id="sitecode" style="width:100%;">
      <option value="000">Select Branch</option>
      <?php while($site_row=DB_fetch_array($SQLSite)){?>
      <option value="<?php echo $site_row['loccode'];?>"<?php echo ($sitecode==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
      <?php } ?>
    </select></td>
    <td width="13%">&nbsp;</td>
    <td width="20%">&nbsp;</td>
    <td>&nbsp;</td>
    <td width="20%">&nbsp;</td>
	</tr>
  <tr class="row1">
    <td width="13%"><strong>Qc No.</strong></td>
    <td width="20%"><input name="qc_incoming_inspection[qc_no]" id="" readonly type="text" value="Auto Generate" class="" style="width:100%;" />    </td>
    <td width="13%"><strong>Qc Date</strong></td>
    <td width="20%"><input name="qc_incoming_inspection[date]" id="" type="text" style="width:100%;" class="datepicker" value="<?php echo date('d/m/Y');?>" />    </td>
    <td><strong>Mrn No.</strong></td>
    <td width="20%"><input name="" id="" type="text"  style="width:100%;" value="<?php echo $MRNNo;?>" readonly/>
	<input name="qc_incoming_inspection[mrn_id]" id="" type="hidden"  style="width:100%;" value="<?php echo $_REQUEST['mrnid'];?>" readonly/></td>
  </tr>
  <tr class="row1">
    <td><strong>Mrn Date </strong></td>
    <td><input name="" id="" type="text"  style="width:100%;" value="<?php echo ConvertSQLDate($MRNDate);?>" readonly/></td>
    <td><strong>Part No.</strong></td>
    <td><select name="qc_incoming_inspection[mst_itemid]" id="part_no_id" style="width:100%" onchange="nameByCode(this.value)">
			<option value="">Select Part No.</option>
		<?php foreach($arr as $key=>$val){?>
			<option value="<?php echo $val;?>"><?php echo $s->getField('item_master','id',$val,'StockID');?></option>
		<?php 
		}?>
		</select>
	</td>
    <td><strong>Part Name </strong></td>
    <td><select name="qc_incoming_inspection[mst_itemid]" id="part_name_id" style="width:100%" onchange="codeByName(this.value)">
			<option value="">Select Part Name</option>
		<?php foreach($arr as $key=>$val){?>
			<option value="<?php echo $val;?>"><?php echo $s->getField('item_master','id',$val,'item_name');?></option>
		<?php 
		}?>
		</select></td>
  </tr>
  <tr class="row1">
    <td><strong>Unit</strong></td>
    <td><select name="" id="unit_id" style="width:100%"></select></td>
    <td><strong>Batch No.</strong></td>
    <td><input name="" id="batch_id" type="text" class="" style="width:100%;" readonly/>    </td>
    <td><strong>Recieved Qty.</strong></td>
    <td><input name="" id="recieve_id" type="text" style="width:100%;" readonly/></td>
  </tr>
  <tr class="row1">
    <td><strong>Process </strong></td>
    <td><select name="qc_incoming_inspection[process_id]" style="width:100%" id="process_id">
		<?php $sql1=DB_query("Select * from production_process_master where manuf_process_name like '%incoming%' and CompanyId=".$_SESSION['ACGCompanyId'],$db);
				while($row1=DB_fetch_array($sql1)){
					extract($row1);
				?>
					<option value="<?php echo $id;?>"><?php echo $manuf_process_name;?></option>
				<?php 
				}
		?>
		</select>
	</td>
    <td><strong> Party Name </strong></td>
    <td><select name="" id="party_id" style="width:100%"></select></td>
    <td><strong>Sample No. </strong></td>
    <td><input name="qc_incoming_inspection[sample_no]" id="" type="text" style="width:100%;" /></td>
  </tr>
  <tr class="row1">
    <td><strong>Sample Date </strong></td>
    <td><input name="qc_incoming_inspection[sample_date]" id="" type="text" style="width:100%;" class="datepicker" /></td>
    <td><strong>Sample By</strong></td>
    <td><input name="qc_incoming_inspection[sample_by]" id="" type="text" class="" style="width:100%;" />    </td>
    <td><strong>Analysed By </strong></td>
    <td><input name="qc_incoming_inspection[analysed_by]" id="" type="text" class="" style="width:100%;" /></td>
  </tr>
  <tr class="row1">
    <td><strong>Method No. </strong></td>
    <td><select name="qc_incoming_inspection[method_no_id]" id="method_id" style="width:100%" onchange="methodParameters()"></select></td>
    <td><strong>Accepted Qty. </strong></td>
    <td><input name="qc_incoming_inspection[accepted_qty]" id="" type="text" class="" style="width:100%;" />    </td>
    <td><strong>Rejected Qty.</strong></td>
    <td><input name="qc_incoming_inspection[rejected_qty]" id="" type="text" class="" style="width:100%;" /></td>
  </tr>
  <tr class="row1">
	<td colspan="6"><center><h3>Parameters And Results</h3></center></td>
	</tr>
	<tr class="row1">
	<td colspan="6" style="padding:0px;" id="details_id"></td>
	</tr>
  <tr class="row1">
    <td colspan="6">&nbsp;</td>
    </tr>
	<tr class="row1">
    <td colspan="6" style="text-align:left"><table width="30%" align="left" border="0" cellpadding="3" cellspacing="0">
  <tr>
    <td width="15%"><strong>Prepared By:</strong></td>
    <td width="35%"><?php echo $_SESSION['UsersRealName'];?>
	<input name="qc_incoming_inspection[CompanyId]" id="" type="hidden" class="" style="width:100%;" readonly="true" value="<?php echo $_SESSION['ACGCompanyId'];?>" />
	<input name="qc_incoming_inspection[created_by]" id="" type="hidden" class="" style="width:100%;" readonly="true" value="<?php echo $_SESSION['UserID'];?>" />
	<input name="qc_incoming_inspection[created_date]" id="" type="hidden" class="" style="width:100%;" readonly="true" value="<?php echo date('Y-m-d');?>" />			
	</td>
    </tr>
  
</table></td>
    </tr>
	<tr class="row1">
    <td colspan="6" style="text-align:center;"> <input type="submit" name="Save" value="Save" class="search" />&nbsp;<input type="submit" name="Cancel" value="Cancel" class="search" /></td>
    </tr>
</table>
</form>
</div>
</div>
<script language="javascript" type="text/javascript">
function nameByCode(str){
	var process_id=document.getElementById("process_id").value;
	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){
			var data=xmlhttp.responseText;
			//alert(data);
			var data_brk=data.split('__');
			document.getElementById("part_name_id").innerHTML=data_brk[0];
			document.getElementById("unit_id").innerHTML=data_brk[1];
			document.getElementById("batch_id").value=data_brk[2];
			document.getElementById("recieve_id").value=data_brk[3];
			document.getElementById("party_id").innerHTML=data_brk[4];
			document.getElementById("method_id").innerHTML=data_brk[5];
		}
	}
xmlhttp.open("GET","ajax_item_code_name.php?id="+str+"&type=code&mrn="+<?php echo $_REQUEST['mrnid']?>+"&pid="+process_id,true);
xmlhttp.send();
}
function codeByName(str){
	var process_id=document.getElementById("process_id").value;
	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){
			var data=xmlhttp.responseText;
			var data_brk=data.split('__');
			document.getElementById("part_no_id").innerHTML=data_brk[0];
			document.getElementById("unit_id").innerHTML=data_brk[1];
			document.getElementById("batch_id").value=data_brk[2];
			document.getElementById("recieve_id").value=data_brk[3];
			document.getElementById("party_id").innerHTML=data_brk[4];
			document.getElementById("method_id").innerHTML=data_brk[5];
		}
	}
xmlhttp.open("GET","ajax_item_code_name.php?id="+str+"&type=name&mrn="+<?php echo $_REQUEST['mrnid']?>+"&pid="+process_id,true);
xmlhttp.send();
}
function methodParameters(){
	var method_id=document.getElementById("method_id").value;
	if(method_id==''){
		document.getElementById("details_id").innerHTML='';
	}else{
	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){
			var data=xmlhttp.responseText;
			document.getElementById("details_id").innerHTML=data;
		}
	}
xmlhttp.open("GET","ajax_method_prmtrs.php?id="+method_id,true);
xmlhttp.send();}
}
</script>