<?php 
$url_cancel= "index.php?route=ToolPlants/Transaction/fixed_assets_transfer_issue_index".$rurl;
$url_add= "index.php?route=ToolPlants/Transaction/fixed_assets_transfer_issue".$rurl;


if($_REQUEST['save'])
{	
	if($_REQUEST['updateHead']==0){
	$data['issue_date']=FormatDateForSQL($_REQUEST['issue_date']);
	$data['issued_by'] = $_REQUEST['issued_by'];
	$data['exp_return_date']=FormatDateForSQL($_REQUEST['exp_return_date']);
	$data['remarks']=$_REQUEST['remarks'];
	$condition = 'id="'.$_REQUEST['issueid'].'"';
	$s->RecordEdit('tpl_asset_transfer_issue',$data,$condition);

	}	
	
		for($i=0;$i<count($_REQUEST['issue_row_id']);$i++)
		{
			$data1['issue_qty']=$_REQUEST['issue_qty'][$i];
			$cond1 = "id='".$_REQUEST['issue_row_id'][$i]."'";
			$s->RecordEdit('tpl_asset_transfer_issue_details',$data1,$cond1);

			$issue = $_REQUEST['pre_pend'][$i]+$_REQUEST['issue_qty'][$i];
			
			if($_REQUEST['approved_qty'][$i]==$issue and $_REQUEST['issue_qty'][$i] !=0)
			{
				$dataArray2['status']='Issued';
			}elseif($_REQUEST['issue_qty'][$i] !=0)
			{
				$dataArray2['status']='Partial Issued';
			}else{
				$dataArray2['status']='Pending';
			}
			
			$dataArray2['issue_qty']=$issue;
			$cond2="id='".$_REQUEST['asset_row_id'][$i]."'";
			$cond3="id='".$_REQUEST['apr_row_id'][$i]."'";
			$s->RecordEdit('tpl_asset_transfer_request_details', $dataArray2, $cond2);
			$s->RecordEdit('tpl_asset_transfer_request_approval_details', $dataArray2, $cond3);
		}
	$count=0;
	$count1=0;
	//echo "select status from tpl_asset_transfer_request_details where asset_req_id='".$_REQUEST['rid']."'";
	$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 Issued")
		{
			$count1=$count1+1;
		}
	}
	
	
	if($count==$numrecord)
	{
		$dataArray1['issued_status']='Issued';
	}elseif($count1>0)
	{
		$dataArray1['issued_status']='Parial Issued';
	}
	
	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>";			
	
	
	
}
if(isset($_REQUEST['action']) && isset($_REQUEST['isid']))
{
	if($_REQUEST['action']=='edit' && !empty($_REQUEST['isid']))
	{	
		$recEdit=DB_fetch_array($s->GetRecordById('tpl_asset_transfer_issue','id='.$_REQUEST['isid']));
		extract($recEdit);
		
	}
}

$sqlreq = "select T.requested_site, T.request_date, T.request_no, T.returnable, T.requested_by, T.purpose, T.issued_status, 	
		  I.issue_date,I.issued_by, I.exp_return_date, I.issue_no, I.remarks, A.approved_site,A.approved_by, A.id as aprid 
		  FROM tpl_asset_transfer_issue I 
		  INNER JOIN tpl_asset_transfer_request T ON T.id=I.transfer_reqno
		  INNER JOIN tpl_asset_transfer_request_approval A ON A.request_id=T.id 
		  WHERE I.id='".$_REQUEST['issueid']."'";

$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 Issue Update </h1>
<form name="" id="" method="post">
<h3>General Details</h3>
<table width="100%" cellpadding="3" cellspacing="0" border="0">
<tr class="row1">
	<td width="13%"><strong>Issue Branch</strong></td>
	<td width="20%"><?php echo $s->GetFieldValue('locations','loccode="'.$reqRow['approved_site'].'"','locationname'); ?>
		<input type="hidden" name="issue_site" value="<?php echo $reqRow['approved_site'];?>" />	</td>
	<td width="13%" ><strong>ATN Date</strong> <font style="color:#FF0000;">*</font></td>
	<td width="20%"><input type="text" name="issue_date" id="issue_date" value="<?php echo ($reqRow['issue_date'] !='')?ConvertSQLDate($reqRow['issue_date']):''; ?>" class="<?php echo ($reqRow['issued_status'] =='Pending') ? 'datepicker' :''; ?>" <?php echo ($reqRow['issued_status'] =='Pending') ? '' :'readonly'; ?> /></td>
	<td width="13%"><strong>ATN No.</strong> <font style="color:#FF0000;">*</font></td>
	<td><input type="text" name="issue_no" id="issue_no" value="<?php echo ($reqRow['issue_no'] !='')? $reqRow['issue_no'] : "Auto Generated";?>" readonly="" /></td>
</tr>
<tr class="row1">
	<td><strong>Requested Branch</strong></td>
	<td><?php echo $s->GetFieldValue('locations','loccode="'.$reqRow['requested_site'].'"','locationname'); ?></td>
	<td><strong>ATR No.</strong></td>
	<td id="TransReqNo"><?php echo $reqRow['request_no'];?></td>
	<td><strong>ATR Date</strong></td>
	<td id="reqs_date"><?php echo ($reqRow['request_date'] !='')?ConvertSQLDate($reqRow['request_date']):'';?></td>
</tr>
<tr class="row1">
	<td><strong>Requested By</strong></td>
	<td id="reqs_by"><?php echo $s->GetFieldValue('empmaster','id="'.$reqRow['requested_by'].'"','emp_name');?></td>
	<td><strong>Purpose</strong></td>
	<td id="purp"><?php echo $reqRow['purpose'];?></td>
	<td><strong>Returnable Y/N</strong></td>
	<td><?php echo $reqRow['returnable'];?></td>
</tr>
<tr class="row1">
	<td valign="top"><strong>Expected Return Date</strong></td>
	<td valign="top"><input type="text" name="exp_return_date" id="exp_return_date" value="<?php echo ($reqRow['exp_return_date'] !='') ? ConvertSQLDate($reqRow['exp_return_date']):''; ?>" class="<?php echo ($reqRow['issued_status'] =='Pending') ? 'datepicker' :''; ?>" <?php echo ($reqRow['issued_status'] =='Pending') ? '' :'readonly'; ?>/></td>
    <td valign="top"><strong>Issued By</strong></td>
    <td valign="top">
		<select name="issued_by" id="issued_by">
			<option value="0">--Select--</option>
		<?php 
			$empsql = "select id, emp_name from empmaster where emp_site='".$reqRow['approved_site']."' ORDER BY emp_name";
			$empres = DB_query($empsql,$db);
			while($empRow = DB_fetch_array($empres)){?>
		<option value="<?php echo $empRow['id'];?>" <?php echo ($empRow['id']==$reqRow['issued_by']) ? "selected" : ""; ?>><?php echo $empRow['emp_name'];?></option>
		<?php }?>
		</select></td>
	<td valign="top"><strong>Remarks</strong></td>
	<td><textarea name="remarks" id="remarks" <?php echo ($reqRow['issued_status'] =='Pending') ? '' :'readonly'; ?>><?php echo $reqRow['remarks']; ?></textarea></td>
</tr>	
</table>
<br />
<h3>Assets Details</h3>
<table width="100%" cellpadding="3" cellspacing="0" border="1" style="border:1px solid #808080">
<tr >
	<th width="3%">S. No.</th>
	<th width="7%">Asset Code</th>
	<th>Asset Name</th>
	<th width="10%">Asset Group</th>
				<?php 
	   $title1=DB_query("select * from tbl_title_master order by serial_no ASC limit 0,3",$db);
	   while($r=DB_fetch_array($title1))
	   { 
	    ?>
    <th width="8%"><?php echo $r['title_name'];?></th>
    <?php } ?>
	<th width="7%">Approved Qty</th>
	<th width="7%">Pending Qty</th>
	<th width="7%">Issue Qty</th>
</tr> 
<?php
$s=new db_query();

$result2=DB_query("select * from tpl_asset_transfer_issue_details where issue_id='".$_REQUEST['issueid']."' and approved_id='".$_REQUEST['aprid']."'",$db);
$i=0;
$updateHead = 0;

while($myrow2=DB_fetch_array($result2))
{
	$readonly = '';
	if($myrow2['received_qty']>0){
		$readonly = 'readonly';
		$updateHead++;
	}

	$sqlasset = "SELECT * FROM machine_center WHERE id='".$myrow2['asset_id']."'";
	$resAst = DB_query($sqlasset,$db);
	$assetRow = DB_fetch_array($resAst);

	$sqlG=DB_query("Select * from machine_group_master where id='".$assetRow['under_group']."' ",$db); 
	$myrow_group=DB_fetch_array($sqlG);
	
	$totIssued =$s->GetFieldValue('tpl_asset_transfer_request_approval_details','id="'.$myrow2['apr_row_id'].'"','issue_qty'); 
?>
		<tr>
			<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['id'];?>" />
			<input type="hidden" name="apr_row_id[]" id="apr_row_id_<?php echo $i;?>" value="<?php echo $myrow2['apr_row_id'];?>" /></td>
			<td><?php echo $assetRow['code'];?></td>
			<td><?php echo $assetRow['name'];?></td>
			<td><?php echo $myrow_group['machine_gp_location'];?></td>
			<?php 
   $title1=DB_query("select * from tbl_title_master order by serial_no ASC limit 0,3",$db);
   while($r=DB_fetch_array($title1))
   { 
    ?>
    <td><?php echo $assetRow[$r['db_field_name']];?></td>
    <?php } ?>
			<td><?php echo $myrow2['approved_qty'];?><input type="hidden" name="approved_qty[]" id="approved_qty_<?php echo $i;?>" value="<?php echo $myrow2['approved_qty'];?>" /></td>
			<td id="pendRow_<?php echo $i;?>"><?php echo $pending = $myrow2['approved_qty']-$totIssued;?></td>
			<input type="hidden" name="pnd_qty[]" id="pnd_qty_<?php echo $i;?>" value="<?php echo $pending;?>" />
			<input type="hidden" name="pre_pend[]" id="pre_pend_<?php echo $i;?>" value="<?php echo $totIssued-$myrow2['issue_qty'];?>" />
			<input type="hidden" name="pre_issue[]" id="pre_issue_<?php echo $i;?>" value="<?php echo $myrow2['issue_qty'];?>" />
			<td><?php echo $myrow2['issue_qty'];?><input type="hidden" name="issue_qty[]" id="issue_qty_<?php echo $i; ?>" onkeyup="validateQty('<?php echo $i;?>','<?php echo $pending;?>','<?php echo $readonly;?>','<?php echo $myrow2['issue_qty'];?>')" value="<?php echo $myrow2['issue_qty'];?>" <?php echo $readonly;?>/></td>
		</tr>
		<?php $i++;
		$editcount = $i;
		}  ?>
		</table>
		<input type="hidden" value="<?php echo $editcount;?>" name="editcnt" id="editcnt" />	</td>
</tr>
<tr>
	<td  style="text-align:center;" colspan="4">
	  <input type="submit" value="Update" name="save" class="search" onclick="return validate();" /> 
	<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="issueid" value="<?php echo $_REQUEST['issueid'];?>" />
<input type="hidden" name="updateHead" value="<?php echo $updateHead;?>" />
</form>

</div>
</div>
<script language="JavaScript">
	
function validate()
{
	if(document.getElementById("issue_date").value=='')
	{
		alert("Select Issue Date");
		return false;
	}
	
	
	var assets = document.getElementById("editcnt").value;
	for( var i=0; i<=assets; i++)
	{
		if(document.getElementById("issue_qty_"+i).value =='')
		{
			alert("Enter Issued Quantity");
			document.getElementById("issue_qty_"+i).focus();
			return false;
		}
	}
}

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

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

$.noConflict();
$(function() {
$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' ,changeMonth: true,
		changeYear: true,
		yearRange: '-80:+20'});
});
</script>