<?php 
$url_cancel= "index.php?route=ToolPlants/Transaction/fixed_assets_transfer_receive_index".$rurl;
$url_add= "index.php?route=ToolPlants/Transaction/fixed_assets_transfer_receive".$rurl;
if($_REQUEST['update'])
{	
	$data['receive_date']=FormatDateForSQL($_REQUEST['receive_date']);
	$data['received_by']=$_REQUEST['received_by'];
	$data['remarks']=$_REQUEST['remarks'];
	$cond = "id='".$_REQUEST['recid']."'"; 
	$s->RecordEdit('tpl_asset_transfer_receive',$data,$cond);
	
//print_r($data);
	
		for($i=0;$i<count($_REQUEST['asset_id']);$i++)
		{
			$data1['received_qty']=$_REQUEST['received_qty'][$i];
			$cond1 = "id='".$_REQUEST['receive_row_id'][$i]."'";

			$s->RecordEdit('tpl_asset_transfer_receive_details',$data1,$cond1);
			
			$receive = $_REQUEST['pre_pend'][$i]+$_REQUEST['received_qty'][$i];
			if($_REQUEST['issue_qty'][$i]==$receive and $_REQUEST['received_qty'][$i] !=0)
			{
				$dataArray2['status']='Received';
			}elseif($_REQUEST['received_qty'][$i] !=0)
			{
				$dataArray2['status']='Partial Received';
			}else{
				$dataArray2['status']='Pending';
			}
			
			$dataArray2['received_qty']=$receive;
			$cond2="id='".$_REQUEST['issue_row_id'][$i]."'";
			$s->RecordEdit('tpl_asset_transfer_issue_details', $dataArray2, $cond2);
			$dataArray2['received_qty']=$receive;
			$cond3="id='".$_REQUEST['asset_row_id'][$i]."'";
			$s->RecordEdit('tpl_asset_transfer_request_details', $dataArray2, $cond3);			
		}
	$count=0;
	$count1=0;
	$SQLUpdate=DB_query("select status from tpl_asset_transfer_request_details where asset_req_id='".$_REQUEST['rid']."'",$db);
	$numrecord=DB_num_rows($SQLUpdate);
	while($row=DB_fetch_array($SQLUpdate))
	{
		if($row['status']=='Issued')
		{
			$count=$count+1;
		}
		if($row['status']=='Partial Received')
		{
			$count1=$count1+1;
		}
	}
	
	if($count==$numrecord)
	{
		$dataArray1['received_status']='Received';
	}elseif($count1>0)
	{
		$dataArray1['received_status']='Partial Received';
	}
	
	if(count($dataArray1)>0){
		$cond1="id='".$_REQUEST['rid']."'";
		$s->RecordEdit('tpl_asset_transfer_request', $dataArray1, $cond1);
	}
	
	$_SESSION['success_msg']="Saved Successfully";
	echo "<script>location.href='$url_cancel'</script>";			
}

$sqlreq = "select R.receive_date, R.receive_no, R.remarks as ReceiveRemarks, R.received_by, T.requested_site, T.request_date, T.request_no, T.requested_by, I.remarks, I.id as issueid, A.approved_by 
		   from tpl_asset_transfer_receive R 
		   INNER JOIN tpl_asset_transfer_issue I ON I.id=R.issue_id
     	   INNER JOIN tpl_asset_transfer_request_approval A ON A.id=I.approval_no
		   INNER JOIN tpl_asset_transfer_request T ON T.id=A.request_id
		   WHERE R.id='".$_REQUEST['recid']."'";

$result = DB_query($sqlreq,$db);

$reqRow = DB_fetch_array($result);
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Fixed Asset Transfer Receive Add</h1>
<form name="" id="" method="post">
<table width="100%" cellpadding="3" cellspacing="0" border="0">
<tr class="row1">
	<td class="tabledata1" colspan="6">General Details</td>
</tr>
<tr class="row1">
	<td width="12%" class="tabledata1">Receive Site</td>
	<td width="18%"><?php echo $s->GetFieldValue('locations','loccode="'.$reqRow['requested_site'].'"','locationname'); ?>
		<input type="hidden" name="receive_site" value="<?php echo $reqRow['requested_site'];?>" />
	</td>
	<td width="13%" class="tabledata1">Receive Date<font style="color:#FF0000;">*</font></td>
	<td width="26%"><input type="text" name="receive_date" id="receive_date" value="<?php echo ($reqRow['receive_date']!='')?ConvertSQLDate($reqRow['receive_date']):''; ?>" class="datepicker" style="width:150px;" /></td>
	<td width="12%" class="tabledata1">Receive No. <font style="color:#FF0000;">*</font></td>
	<td width="19%"><input type="text" name="receive_no" id="receive_no" value="<?php echo ($reqRow['receive_no'] !='')? $reqRow['receive_no'] : "Auto Generated";?>" readonly="" /></td>
</tr>	

<tr class="row1">
	<td class="tabledata1">Requested Site</td>
	<td><?php echo $s->GetFieldValue('locations','loccode="'.$reqRow['requested_site'].'"','locationname'); ?></td>
	<td class="tabledata1">Transfer Req. No.</td>
	<td id="TransReqNo"><?php echo $reqRow['request_no'];?></td>
	<td class="tabledata1">Requested Date</td>
	<td id="reqs_date"><?php echo ($reqRow['request_date'] !='')?ConvertSQLDate($reqRow['request_date']):'';?></td>
</tr>
<tr class="row1">
	<td class="tabledata1">Requested By</td>
	<td id="reqs_by"><?php echo $reqRow['requested_by'];?></td>
	<td class="tabledata1">Approved By</td>
	<td id="purp"><?php echo $s->GetFieldValue('www_users','id="'.$reqRow['approved_by'].'"','realname');?></td>
	<td class="tabledata1">Received By</td>
	<td><input type="text" name="received_by" id="received_by" value="<?php echo $reqRow['received_by'];?>" /></td>
</tr>	

<tr class="row1">
	<td class="tabledata1">Issue Remarks</td>
	<td colspan="5"><?php echo $reqRow['remarks']; ?></td>
</tr>		  

<tr class="row1">
	<td class="tabledata1">Remarks</td>
	<td colspan="5"><textarea style="width:635px;" name="remarks"  id="remarks"><?php echo $reqRow['ReceiveRemarks']; ?></textarea></td>
</tr>		  
<tr class="row1">
	<td class="tabledata1" colspan="6">
	Assets Details
	</td>
</tr>
<tr class="row1">
	<td class="tabledata1" colspan="6">
	<table width="100%" border="0" cellspacing="0" cellpadding="3" >
<tr class="row1">
	<td class="tabledata">Select</td>
	<td class="tabledata">Asset Code</td>
	<td class="tabledata">Asset Name</td>
	<td class="tabledata">Asset Group</td>
	<td class="tabledata">Serial No.</td>
	<td class="tabledata">Model No.</td>
	<td class="tabledata">Vehicle No.</td>
	<td class="tabledata">Issue Qty</td>
	<td class="tabledata">Pending Qty</td>
	<td class="tabledata">Receive Qty</strong></td>
		
</tr> 
<?php
$s=new db_query();
$result2=DB_query("select * from tpl_asset_transfer_receive_details where receive_id='".$_REQUEST['recid']."' and status='Pending'",$db);
$i=0;
$updateHead = 0;

while($myrow2=DB_fetch_array($result2))
{
 //echo "<pre>";print_r($myrow2);
	$sqlasset = "SELECT A.asset_name, A.asset_code_prefix, A.serial_no, A.vehicle_no, A.model_no, M.asset_capitalisation as MGROUP
				 FROM tpl_fixed_asset_definition A
				 INNER JOIN tpl_materialcategorydefinition M ON M.id = A.asset_group
				 WHERE A.id='".$myrow2['asset_id']."'";
	$resAst = DB_query($sqlasset,$db);
	$assetRow = DB_fetch_array($resAst);
	
	$totReceive =$s->GetFieldValue('tpl_asset_transfer_issue_details','id="'.$myrow2['issue_id'].'"','received_qty'); 
?>
		<tr class="row1">
			<td><?php  echo $i+1;?>
			<input type="hidden" name="asset_row_id[]" id="asset_row_id_<?php echo $i;?>" value="<?php echo $myrow2['asset_row_id'];?>" />
			<input type="hidden" name="issue_row_id[]" id="issue_row_id_<?php echo $i;?>" value="<?php echo $myrow2['issue_row_id'];?>" />
			<input type="hidden" name="receive_row_id[]" id="receive_row_id_<?php echo $i;?>" value="<?php echo $myrow2['id'];?>" />
			</td>
			<td><?php echo $assetRow['asset_code_prefix'];?><input type="hidden" name="asset_id[]" id="asset_id_<?php echo $i;?>" value="<?php echo $myrow2['asset_code'];?>" /></td>
			<td><?php echo $assetRow['asset_name'];?></td>
			<td><?php echo $assetRow['MGROUP'];?></td>
			<td><?php echo $assetRow['serial_no'];?></td>
			<td><?php echo $assetRow['model_no'];?></td>
			<td><?php echo $assetRow['vehicle_no'];?></td>
			<td><?php echo $myrow2['issue_qty'];?><input type="hidden" name="issue_qty[]" id="issue_qty_<?php echo $i;?>" value="<?php echo $myrow2['issue_qty'];?>" style="width:90px;" /></td>
			<td id="pendRow_<?php echo $i;?>"><?php echo $pending = $myrow2['issue_qty']-$totReceive;?>
			</td><input type="hidden" name="pnd_qty[]" id="pnd_qty_<?php echo $i;?>" value="<?php echo $totReceive-$myrow2['received_qty'];?>" style="width:90px;" />
		<input type="hidden" name="pre_pend[]" id="pre_pend_<?php echo $i;?>" value="<?php echo $totReceive-$myrow2['received_qty'];?>" style="width:90px;" />
		<input type="hidden" name="pre_issue[]" id="pre_issue_<?php echo $i;?>" value="<?php echo $myrow2['received_qty'];?>" style="width:90px;" />

			<td><input type="text" name="received_qty[]" id="received_qty_<?php echo $i; ?>" onkeyup="validateQty('<?php echo $i;?>','<?php echo $pending;?>','<?php echo $myrow2['received_qty'];?>')" value="<?php echo $myrow2['received_qty'];?>"/></td>
		</tr>
		<?php $i++;
		$editcount = $i;
		}  ?>
		</table>
		<input type="hidden" value="<?php echo $editcount;?>" name="editcnt" id="editcnt" />
	</td>
</tr>
<tr class="row1">
	<td class="tabledata1" style="text-align:center;" colspan="6">
	<input type="submit" value="<?php if($_REQUEST['action']=='Edit') echo 'update'; else echo 'save'; ?>" name="<?php if($_REQUEST['action']=='Edit') echo 'update'; else echo 'save'; ?>" class="search" onclick="return validates();">    <a href="<?php echo $url_cancel;?>"><input type="button" value="Cancel" name="button" class="search"></a>
	</td>
</tr>
</table>
<input type="hidden" name="rid" value="<?php echo $_REQUEST['rid'];?>" />
<input type="hidden" name="issue_id" value="<?php echo $reqRow['issueid'];?>" />

</form>
<script language="JavaScript">
	
function validate()
{
	if(document.getElementById("receive_date").value=='')
	{
		alert("Select Receive Date");
		return false;
	}
	
	
	var assets = document.getElementById("editcnt").value;
	for( var i=0; i<=assets; i++)
	{
		if(document.getElementById("received_qty_"+i).value =='')
		{
			alert("Enter Received Quantity");
			document.getElementById("received_qty_"+i).focus();
			return false;
		}
	}
}

function validateQtydd(apQty,reqQty)
{
	if(!document.getElementById(apQty).value.match(/^\d+/))
	{
		alert("Only digits are allowed");
		document.getElementById(apQty).value = '';
	}
	var approvedQty = parseInt(document.getElementById(apQty).value);
	var requestedQty = parseInt(reqQty);
	if(approvedQty>requestedQty)
	{
		alert("Received quantity must be less than or equal to Issued quantity");
		document.getElementById(apQty).value = '';
	}
}


function validateQty(id,reqQty,isuqty)
{
	if(!document.getElementById("received_qty_"+id).value.match(/^\d+/))
	{
		alert("Only digits are allowed");
		document.getElementById("received_qty_"+id).value = isuqty;
	}
	var approvedQty = parseInt(document.getElementById("received_qty_"+id).value);
	var requestedQty = parseInt(reqQty)+parseInt(isuqty);

	if(approvedQty>requestedQty)
	{
		alert("Receive quantity must be less than or equal to Issued quantity");
		document.getElementById("received_qty_"+id).value = isuqty;
	}else{
		var pending = requestedQty - approvedQty;
		document.getElementById("pnd_qty_"+id).value = pending;
		document.getElementById("pendRow_"+id).innerHTML = pending;		
	}
}


</script>