<?php
if($_REQUEST['action1']='Addnew')
{	
}

$url_cancel= "index.php?route=ToolPlants/Transaction/fixed_assets_transfer_receipt_entry_index".$rurl;
$url_add= "index.php?route=ToolPlants/Transaction/fixed_assets_transfer_receipt_entry".$rurl;
if($_REQUEST['save'])
{
	$sql="select * from tpl_asset_transfer_receipt where site='".$_REQUEST['site']."'";
	$qyprefix=DB_query("Select prefix from locations Where loccode ='".$_REQUEST['site']."'",$db);
	$row_prefix=DB_fetch_array($qyprefix);
	$projectprefix=$row_prefix['prefix'];
	$result = DB_query($sql,$db);
	$numrows = DB_num_rows($result);
	if($numrows>0)
	{
	$IsuuNo=DB_fetch_array($result);
	$Explo=explode("/",$IsuuNo['transfer_receiptno']);
	$maxval=$Explo[2];
	$strFinal = $maxval+1;
	$strBothFinal = "ISSUERECNO".'/'.$projectprefix."/".str_pad($strFinal,4,"0",STR_PAD_LEFT);
	}
	else
	{
	$strBothFinal= "ISSUERECNO".'/'.$projectprefix."/".'0001';
	}
	$data['site']=$_REQUEST['site'];
	$data['receipt_site']=$_REQUEST['receipt_project'];
	$data['transfer_issueno']=$_REQUEST['transfer_issueno'];
	$data['transfer_receiptno']=$strBothFinal;
	$data['transfer_receipt_date']=FormatDateForSQL($_REQUEST['transfer_receipt_date']);
	$data['created']=date('Y-m-d');
	$data['created_by']=$_SESSION['UserID'];
	$data['CompanyId']=$_SESSION['ACGCompanyId'];
	$s->RecordInsert('tpl_asset_transfer_receipt',$data);
	$last_id=DB_Last_Insert_Id($db,"tpl_asset_transfer_receipt","id");
	for($i=0;$i<count($_REQUEST['machine_id']);$i++)
	{
		$data1['tpl_asset_transfer_receipt_id']=$last_id;
		$data1['machine_id']=$_REQUEST['machine_id'][$i];
		$data1['avail_qty']=$_REQUEST['avail_qty'][$i];
		$data1['issue_qty']=$_REQUEST['issue_qty'][$i];
		$data1['receive_qty']=$_REQUEST['receive_qty'][$i];
		$s->RecordInsert('tpl_asset_transfer_receipt_details',$data1);
	
	if($_REQUEST['issue_qty'][$i]==$_REQUEST['receive_qty'][$i])
	{
		$cond="id='".$_REQUEST['issueid_details'][$i]."' and machine_id='".$_REQUEST['machine_id'][$i]."'";
		$dataArray['status']='FINISH';
		$s->RecordEdit('tpl_asset_transfer_issue_details', $dataArray, $cond);
	}
	else
	{
		$BalReqQty=$_REQUEST['issue_qty'][$i]-$_REQUEST['receive_qty'][$i];
		$dataArray2['issue_qty']=$BalReqQty;
		$cond2="id='".$_REQUEST['issueid_details'][$i]."' and machine_id='".$_REQUEST['machine_id'][$i]."'";
		$s->RecordEdit('tpl_asset_transfer_issue_details', $dataArray2, $cond2);
	}
	}
		$SQLUP=DB_query("select * from tpl_asset_transfer_issue_details 
		where tpl_asset_transfer_issue_id='".$_REQUEST['issueid']."'",$db);
	$numRows=DB_num_rows($SQLUP);
	$count=0;
	while($Row1=DB_fetch_array($SQLUP))
	{
	if($Row1['status']=='FINISH')
	{
	$count=$count+1;
	}
	}
	if($count==$numRows)
	{
	$data4['status']='FINISH';
	$Cond23="id='".$_REQUEST['issueid']."'";
	$s->RecordEdit('tpl_asset_transfer_issue', $data4, $Cond23);
	}
	$_SESSION['success_msg']="Saved Successfully";
	echo "<script>location.href='$url_cancel'</script>";	
}
if(isset($_REQUEST['action']) && isset($_REQUEST['reid']))
{
	if($_REQUEST['action']=='edit' && !empty($_REQUEST['reid']))
	{	
		$recEdit=DB_fetch_array($s->GetRecordById('tpl_asset_transfer_receipt','id='.$_REQUEST['reid']));
		extract($recEdit);		
	}
}
if(isset($_REQUEST['update']))
{   $num5=0;
	$col=0;
	//print_r($_REQUEST); exit;
	$sql=DB_query("select transfer_issueno from tpl_asset_transfer_receipt where id='".$_REQUEST['reid']."'",$db);
	$rowdata=DB_fetch_array($sql);
	$requestid=$rowdata['transfer_issueno'];			
	$project=$_REQUEST['tpl_asset_transfer_receipt']['receipt_project'];
	$autoNo=$autoID->GenerateId('tpl_asset_transfer_receipt','transfer_receiptno','receipt_project='."'$project'",'/TP/FATR');
	$date=FormatDateForSQL($_REQUEST['tpl_asset_transfer_receipt']['transfer_receipt_date']);
	$_REQUEST['tpl_asset_transfer_receipt']['transfer_receiptno']=$autoNo;
	$_REQUEST['tpl_asset_transfer_receipt']['transfer_receipt_date']=$date;
	$_REQUEST['tpl_asset_transfer_receipt']['transfer_issueno']=$requestid;
	extract($_REQUEST['tpl_asset_transfer_receipt']);
	
	if($s->RecordEdit('tpl_asset_transfer_receipt',$_REQUEST['tpl_asset_transfer_receipt'],"id=".$_REQUEST['tpl_asset_transfer_receipt']['id']))
	{
	$last_id=$_REQUEST['reid'];
	$sql=DB_query("select id from tpl_asset_transfer_issue_details where tpl_asset_transfer_issue_id='".$requestid."'",$db);
	while($rowdata=DB_fetch_array($sql))
	{
	$a[]=$rowdata['id'];	
	}
	//print_r($last_id); exit;
	if($s->DeleteRecord('tpl_asset_transfer_receipt_details',"tpl_asset_transfer_receipt_id=".$_REQUEST['tpl_asset_transfer_receipt']['id']))
	{
	for($i=0;$i<count($_REQUEST['asset_code_prefix']);$i++)
	{
	$last_id=$_REQUEST['reid'];
	//print_r($last_id); exit;
	if($_REQUEST['issue_qty'][$i]==$_REQUEST['receipt_qty'][$i])
	{
	//echo "if";
	$condition1="id='".$a[$i]."'";
	
	$status="N";
	//print_r($balance_arr);
	//exit;
	$balance_arr['status']=$status;
	$update1=$s->EditRecord(tpl_asset_transfer_issue_details,$balance_arr,$condition1);
	$row1=DB_query($update1,$db);
	}
	else
	{
	if($_REQUEST['issue_qty'][$i]>$_REQUEST['receipt_qty'][$i])
	{
	//echo "else";
	$query_row=DB_fetch_array(DB_query("SELECT * FROM tpl_asset_transfer_issue where id='".$_REQUEST['tpl_asset_transfer_receipt']['transfer_issueno']."'",$db));
	
	$query_row1=DB_fetch_array(DB_query("SELECT * FROM tpl_asset_transfer_issue_details where tpl_asset_transfer_issue_id='".$query_row['id']."'",$db));
	
	$condition1="id='".$query_row1['id']."'";
	
	$qty_bal=$_REQUEST['issue_qty'][$i]-$_REQUEST['receipt_qty'][$i];
	//echo $qty_bal;
	//exit;
	if($qty_bal==0)
	{
	$status="N";
	}
	else 
	{
	$status="Y";	
	}					
	$balance_arr['issue_qty']=$qty_bal;
	$balance_arr['status']=$status;
	//print_r($balance_arr);
	//exit;
	$update1=$s->EditRecord(tpl_asset_transfer_issue_details, $balance_arr , $condition1);
	$row1=DB_query($update1,$db);
	$num5=$query_row1['tpl_asset_transfer_issue_id'];
	if(!$row1)
	{
	die('Error'.DB_error());
	}
	//exit;
	//print_r($balance_arr); exit;	
	}
	}
	$insert_details=array('id'=>'',
	'tpl_asset_transfer_receipt_id'=>$last_id,
	'asset_code_prefix'=>$_REQUEST['asset_code_prefix'][$i],
	'asset_group'=>$_REQUEST['asset_group'][$i],
	'deprec_mst_name'=>$_REQUEST['deprec_mst_name'][$i],
	'project'=>$_REQUEST['project'][$i],
	'asset_amount'=>$_REQUEST['asset_amount'][$i],
	'issue_qty'=>$_REQUEST['issue_qty'][$i],
	'receipt_qty'=>$_REQUEST['receipt_qty'][$i]);
	if(!empty($insert_details['tpl_asset_transfer_receipt_id']))
	{
	$s->RecordInsert('tpl_asset_transfer_receipt_details',$insert_details);
	}
	}
	}	 
	//echo"select * from tpl_asset_transfer_issue_details where tpl_asset_transfer_issue_id='".$requestid."'";
	//	exit;
	$sqldata=DB_query("select * from tpl_asset_transfer_issue_details where tpl_asset_transfer_issue_id='".$requestid."'",$db);
	$numdata=DB_num_rows($sqldata);
	while($data=DB_fetch_array($sqldata))
	{
	if($data['status']=='N')
	{
	$col=$col+1;	
	}
	}		
	if($col==$numdata)
	{				
	$update=DB_query("update tpl_asset_transfer_issue set status='1' where id='".$requestid."'",$db);	
	}   
	else 
	{
	$update=DB_query("update tpl_asset_transfer_issue set status='0' where id='".$requestid."'",$db);
	}  
	}
	//print_r($_REQUEST);exit;
	unset($_REQUEST['tpl_asset_transfer_receipt']);			
	$_SESSION['success_msg']="Saved Successfully";
	echo "<script>location.href='$url_cancel'</script>";			
}	?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Fixed Asset Transfer Receipt Add</h1>
<form name="" id="" method="post">
<table width="100%" cellpadding="3" cellspacing="0" border="0">
<tr class="row1">
	<td class="tabledata1">Site</td>
	<td colspan="3">
	<select name="site" id="site" style="width:150px;">
	<option value="0">Select Site</option>
	<?php        
	$query4=DB_query("Select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
	while($row4=DB_fetch_array($query4))
	{
	$s='';
	if($receipt_project==$row4['loccode'])
	$s="selected";
	?>
	<option value="<?php echo $row4['loccode'];?>" <?php echo $s;?>><?php echo $row4['locationname'];?></option>
	<?php }  ?>
	</select>     
	</td>
</tr>
<tr class="row1">
	<td class="tabledata1">Receipt Site </td>
	<td>
	<select name="receipt_project" id="receipt_project" style="width:150px;" onchange="getdata(this.value)">
	<option value="0">Select Site</option>
	<?php        
	$query4=DB_query("Select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	while($row4=DB_fetch_array($query4))
	{
	$s='';
	if($receipt_project==$row4['loccode'])
	$s="selected";
	?>
	<option value="<?php echo $row4['loccode'];?>" <?php echo $s;?>><?php echo $row4['locationname'];?></option>
	<?php }  ?>
	</select>       
	</td>		  	          	
	<td class="tabledata1">Transfer Issue No.</td>
	<td  id="TransIssueNo">
	<select name="transfer_issueno" id="transfer_issueno" style="width:150px;" onchange="getassectdata(this.value)" >
	<option value="0">Select Issue No</option>
	</select>  
	</td>
</tr>
<tr class="row1">
	<td class="tabledata1">Transfer Receipt No.</td>
	<td><input type="text" value="<?php  echo 'Auto Generated';?>" style="width:150px;" readonly=""   name="transfer_receiptno" id="transfer_receiptno" >
	</td>
	<td class="tabledata1">Transfer Receipt Date</td>
	<td><input type="text" value="" style="width:150px;" name="transfer_receipt_date" id="transfer_receipt_date" class="datepicker">
	</td>
</tr>

<tr class="row1">
	<td class="tabledata1" colspan="4">
		<div id="data12">
		<table width="100%" cellpadding="3" cellspacing="0" border="1">
<tr class="row1">
	<td class="tabledata">Select</td>
	<td class="tabledata">Asset Code</td>
	<td class="tabledata">Asset Name</td>
	<td class="tabledata">Asset Location</td>
	<td class="tabledata">Asset Amount</td>
	<td class="tabledata">Issue Qty</td>
	<td class="tabledata">Receive Qty</td>
</tr>
<tr class="row1">
<?php 
if(isset($_REQUEST['action']) && isset($_REQUEST['reid'])){
$i=0;	
$sql_query=DB_query("SELECT * FROM tpl_asset_transfer_receipt_details WHERE tpl_asset_transfer_receipt_id='".$_REQUEST['reid']."'",$db);
while($row_receipt=DB_fetch_array($sql_query)){
?>
<tr class="row1">
	<td align='center'><?php echo $i+1;?></td>
	<td align='center'><?php echo $row_receipt['asset_code_prefix'];?>
	<input type="hidden" name="asset_group[]" value="<?php echo $row_receipt['asset_group']; ?>" />
	<input type="hidden" name="asset_code_prefix[]" id="asset_code_prefix" value="<?php echo $row_receipt['asset_code_prefix'];?>">
	</td>
	<td align='center'><?php echo $row_receipt['deprec_mst_name'];?>
	<input type="hidden" name="deprec_mst_name[]" id="deprec_mst_name" value="<?php echo $row_receipt['deprec_mst_name'];?>">
	</td>
	<td align='center'><?php echo $row_receipt['project'];?>
	<input type="hidden" name="project[]" id="project" value="<?php echo $row_receipt['project'];?>">
	</td>
	<td align='center'><?php echo $row_receipt['asset_amount'];?>
	<input type="hidden" name="asset_amount[]" id="asset_amount" value="<?php echo $row_receipt['asset_amount'];?>">
	</td>
	<td align='center'><?php echo $row_receipt['issue_qty'];?>
	<input type="hidden" name="issue_qty[]" id="issue_qty_<?php echo $i; ?>" value="<?php echo $row_receipt['issue_qty'];?>">
	</td>
	<td align='center'>
	<input type="text" name="receipt_qty[]" id="receipt_qty_<?php echo $i; ?>" value="<?php echo $row_receipt['receipt_qty'];?>" onblur="return cal(<?php echo $i;?>);" >
	</td>	     		
</tr>  
   <?php $i++; } } ?>
 </table>
 		</div>
	</td>
</tr>
<tr class="row1">
	<td style="text-align:center;" class="tabledata1" colspan="4">
		<input type="submit" value="Save" name="<?php if($_REQUEST['action']=='edit') echo 'update'; else echo 'save'; ?>" class="search" > 
	<a href="<?php echo $url_cancel;?>"><input type="button" value="Cancel" name="button" class="search"></a>
	</td>
</tr>
 
</table>
</form>
</div>
</div>
<script language="JavaScript">
function checkdata(str)
{	
	
if(document.getElementById("select_"+str).checked)
{	
document.getElementById("cost_"+str).removeAttribute('readOnly');	
}	
else
{
document.getElementById("cost_"+str).setAttribute('readOnly',true);		
}
}

function cal(str)
{
	
var k=parseFloat(document.getElementById("issue_qty_"+str).value);
var k1=parseFloat(document.getElementById("receipt_qty_"+str).value);
if(k1>k)
{
alert("Receive Qty Should not greater then Issue Qty");
document.getElementById("receipt_qty_"+str).value='';
document.getElementById("receipt_qty_"+str).focus();
return false;	
}
return true;	
}
  
function getdata(str) 
{	 
	if (str.length==0)
	{
	document.getElementById("TransIssueNo").innerHTML="";
	return;
	}
	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)
	{
	document.getElementById("TransIssueNo").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","GetIssueNo.php?site="+str,true);
	xmlhttp.send();
} 


	
function getassectdata(str)
{
	var xmlhttp;
	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.split("__");	 
	//alert(data);
	document.getElementById("data12").innerHTML=data[0];
	}
	}
	xmlhttp.open("GET","getassettabledata.php?site="+str,true);
	xmlhttp.send();
}


function chk_issueQty(i)
{
	var issue_qty=parseFloat(document.getElementById("issue_qty_"+i).value);
	var Recie_qty=parseFloat(document.getElementById("receipt_qty_"+i).value);
	if(Recie_qty>issue_qty)
	{
		alert("You Can't Recieve Greater Qty Than Issue Qty");
		document.getElementById("receipt_qty_"+i).value='';
		document.getElementById("receipt_qty_"+i).focus();
		return false;
	}
}

</script>
