<?php 
if(isset($_REQUEST['Save']))
{
 $maxsql=DB_query("select * from qc_predispatch 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='Pre Dispatch Inspection' ",$db));
    if(DB_num_rows($maxsql)<1)
      $cntr=1;
    else
      $cntr=$maxid['id']+1;
    
    // $brnch=substr($_REQUEST['sitecode'],0,3);
     $finalstr=$prefix['prefix_code']."/".$cntr;   

$data['qc_no']=$finalstr;
$data['sitecode']=$_REQUEST['sitecode'];
$qc_date=str_replace('/','-',$_REQUEST['qcdate']);
$data['date']= date('Y-m-d', strtotime($qc_date));
$data['dipatch_id']=$_REQUEST['dispatch'];
$data['mst_itemid']=$_REQUEST['mst_itemid'];
$data['process_id']=$_REQUEST['process_id'];
$data['sample_no']=$_REQUEST['sample_no'];
$smp_date=str_replace('/','-',$_REQUEST['sample_date']);
$data['sample_date']=date('Y-m-d', strtotime($smp_date));
$data['sample_by']=$_REQUEST['sample_by'];
$data['analysed_by']=$_REQUEST['analysed_by'];
$data['method_no_id']=$_REQUEST['method_no_id'];
$data['accepted_qty']=$_REQUEST['accepted_qty'];
$data['rejected_qty']=$_REQUEST['rejected_qty'];
$data['CompanyId']=$_SESSION['ACGCompanyId'];
$data['created_by']=$_SESSION['UserID'];
$data['created_date']=date('y-m-d');
$data['remarks']=$_REQUEST['remarks'];
$data['prepared']=$_REQUEST['prepared'];
$data['batch_no']=$_REQUEST['batch_no'];
$s->RecordInsert('qc_predispatch', $data);  
	$masterId=DB_Last_Insert_ID($db,'qc_predispatch','id');

for($i=0;$i<count($_REQUEST['paraid']);$i++){
if($_REQUEST['paraid'][$i]!='')
	{
	$data_det['master_id']=$masterId;
	$data_det['parameter_id']=$_REQUEST['paraid'][$i];
	//$data_det['low']=$_REQUEST['result'][$i];
	//$data_det['high']=$_REQUEST['remarks1'][$i];
        $data_det['result']=$_REQUEST['result'][$i];
        $data_det['remarks']=$_REQUEST['remarks1'][$i];
	$s->RecordInsert('qc_predispatch_details', $data_det);
	}
}
echo "<script>location.href='index.php?route=QC/transactions/predispatch_inspection_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";

}
?>

<?php 
$disdet=DB_fetch_assoc(Db_query("select * from dispatch where id='".$_REQUEST['dispatch']."' ",$db));
//echo "<pre>";print_r($disdet);
?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="tab_div">
<center><h2>Pre Dispatch Inspection</h2></center>
<div id="container">
<form method="post" action="">
<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>
		<input type="hidden" name="sitecode" id="sitecode" value="<?php echo $disdet['site'];  ?>" />
  <tr class="row1">
    <td width="13%"><strong>Qc No.</strong></td>
    <td width="20%"><input name="qcno" id="" value="Auto Generate" readonly type="text" class="" style="width:100%;" />   </td>
    <td width="13%"><strong>Qc Date</strong></td>
    <td width="20%"><input name="qcdate" id="" type="text" class="datepicker" style="width:100%;" />    </td>
    <td><strong>Dispatch No.</strong></td>
    <td width="20%"><input name="" id="" type="text" class="" style="width:100%;"  readonly="true" value="<?php echo $disdet['adviceNo'] ?>"/></td>
  </tr>
  <tr class="row1">
    <td><strong>Dispatch Date </strong></td>
    <td><input name="dispatchdate" id="" type="text" class=""  readonly="true" value="<?php echo ConvertSQLDate($disdet['dispatchDate']) ?>" style="width:100%;" /></td>
    <td><strong>Part No.</strong></td>
    <td><select name="mst_itemid" id="part_no_id" style="width:100%" onchange="nameByCode(this.value)">
			<option value="">Select Part No.</option>
		<?php 
		$arr=DB_query("select * from `dispatch_details` WHERE parentId='".$_REQUEST['dispatch']."' ",$db); 
		while($arres=DB_fetch_assoc($arr))
		{?>
			<option value="<?php echo $arres['item'];?>"><?php echo $s->getField('item_master','id',$arres['item'],'StockID');?></option>
		<?php 
		}?>
		</select>
	</td>
    <td><strong>Part Name </strong></td>
    <td>
	<select name="" id="part_name_id" style="width:100%" onchange="codeByName(this.value)">
			<option value="">Select Part Name</option >
		<?php 
		$arr=DB_query("select * from `dispatch_details` WHERE parentId='".$_REQUEST['dispatch']."' ",$db); 
		while($arres=DB_fetch_assoc($arr))
		{?>
			<option value="<?php echo $arres['item'];?>"><?php echo $s->getField('item_master','id',$arres['item'],'item_name');?></option>
		<?php 
		}?>
		</select></td>
  </tr>
  <tr class="row1">
    <td><strong>Unit</strong></td>
    <td><select id="unit_id" style="width:100%;">
	<option></option>
	</select></td>
    <td><strong>Batch No.</strong></td>
    <td><input name="batch_no" id="" type="text" class="" style="width:100%;" />    </td>
    <td><strong>Dispatch Qty.</strong></td>
    <td><input name="" id="dispatchqty" type="text" class="" style="width:100%;" /></td>
  </tr>
  <tr class="row1">
    <td><strong>Process </strong></td>
    <td><select style="width:100%;" name="process_id" id="process_id" onchange="ajax_getmethod(this.value)">
	
	</select></td>
    <td><strong> Party Name</strong></td>
    <td><input name="" id="client" type="text" readonly="true" class="" style="width:100%;" />    </td>
    <td><strong>Sample No. </strong></td>
    <td><input name="sample_no" id="" type="text" class="" style="width:100%;" /></td>
  </tr>
  <tr class="row1">
    <td><strong>Sample Date </strong></td>
    <td><input name="sample_date" id="" type="text" class="datepicker" style="width:100%;" /></td>
    <td><strong>Sample By</strong></td>
    <td><input name="sample_by" id="" type="text" class="" style="width:100%;" />    </td>
    <td><strong>Analysed Name</strong></td>
    <td><input name="analysed_by" id="" type="text" class="" style="width:100%;" /></td>
  </tr>
  <tr class="row1">
    <td><strong>Method No. </strong></td>
    <td><select name="method_no_id" id="method" class="" style="width:100%;"  onchange="ajax_getmethod_details(this.value);">
	</select></td>
    <td><strong>Accepted Qty. </strong></td>
    <td><input name="accepted_qty" id="" type="text" class="" style="width:100%;" />    </td>
    <td><strong>Rejected Qty.</strong></td>
    <td><input name="rejected_qty" id="" type="text" class="" style="width:100%;" /></td>
  </tr>
  <tr class="row1">
	<td colspan="6"><center><h3>Product Information</h3></center></td>
	</tr>
	<tr class="row1">
	<td colspan="6" style="padding:0px;">
	<table width="100%" border="1" cellpadding="3" cellspacing="0" id="methoddetails">
  	</table></td>
	</tr>
	<tr class="row1">
    <td colspan="6">&nbsp;</td>
    </tr>
	<tr class="row1">
    <td colspan="6"><table width="100%" align="center" border="0" cellpadding="3" cellspacing="0">
  <tr>
    <td width="15%"><strong>Prepared By:</strong></td>
    <td width="20%"><input name="prepared" id="" type="text" class="" style="width:100%;" /></td>
    <td width="15%"><strong>Remarks</strong></td>
    <td width="35%"><textarea cols="40" rows="5" name="remarks"></textarea></td>
  </tr>
  
</table></td>
 </tr>
    </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>

</div>
</div>

<script language="javascript" type="text/javascript">

function nameByCode(str){
//alert(str);
	var sitecode=document.getElementById("sitecode").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("dispatchqty").value=data_brk[2];
			document.getElementById("client").value=data_brk[3];
			document.getElementById("process_id").innerHTML=data_brk[4];
			//document.getElementById("method_id").innerHTML=data_brk[5];
	}
}
xmlhttp.open("GET","ajax_item_code_name_predis.php?id="+str+"&type=code&disp="+<?php echo $_REQUEST['dispatch']?>+"&branch="+sitecode,true);
xmlhttp.send();
}

function codeByName(str){
	var sitecode=document.getElementById("sitecode").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_no_id").innerHTML=data_brk[0];
			document.getElementById("unit_id").innerHTML=data_brk[1];
			document.getElementById("dispatchqty").value=data_brk[2];
			document.getElementById("client").value=data_brk[3];
			document.getElementById("process_id").innerHTML=data_brk[4];
		//	document.getElementById("method_id").innerHTML=data_brk[5];
		}
	}
xmlhttp.open("GET","ajax_item_code_name_predis.php?id="+str+"&type=name&disp="+<?php echo $_REQUEST['dispatch']?>+"&branch="+sitecode,true);
xmlhttp.send();
}


function ajax_getmethod(str)
{
var itm_id=document.getElementById("part_no_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);
			document.getElementById("method").innerHTML=data;
			}
	}
xmlhttp.open("POST","ajax_get_method.php?pid="+str+"&itm="+itm_id,true);
xmlhttp.send();
}

function ajax_getmethod_details(str)
{

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);
			document.getElementById("methoddetails").innerHTML=data;
			}
	}
xmlhttp.open("POST","ajax_getmethod_details.php?mid="+str,true);
xmlhttp.send();
}
</script>