<?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['save'])
{		
	$sql="select * from tpl_asset_transfer_receive where receive_site='".$_REQUEST['receive_site']."'";
  
	$qyprefix=DB_query("Select prefix from locations Where loccode ='".$_REQUEST['receive_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['receive_no']);
	$maxval=$Explo[2];
	$strFinal = $maxval+1;
	$strBothFinal = $projectprefix."/"."TRFRV".'/'.str_pad($strFinal,4,"0",STR_PAD_LEFT);
	}
	else
	{
	$strBothFinal= $projectprefix."/"."TRFRV".'/'.'0001';
	}
	
	$data['receive_date']=FormatDateForSQL($_REQUEST['receive_date']);
	$data['issue_id']=$_REQUEST['issue_id'];
	$data['requested_id']=$_REQUEST['rid'];
	$data['receive_no']=$strBothFinal;
	$data['received_by']=$_REQUEST['received_by'];
	$data['receive_site']=$_REQUEST['receive_site'];
	$data['remarks']=$_REQUEST['remarks'];
	$data['status']='Pending';
	$data['created']=date('Y-m-d');
	$data['created_by']=$_SESSION['UserID'];
	$data['CompanyId']=$_SESSION['ACGCompanyId'];
	$s->RecordInsert('tpl_asset_transfer_receive',$data);
	$last_id=DB_Last_Insert_Id($db,"tpl_asset_transfer_receive","id");
	
	//print_r($data);
	if($last_id !='')
	{
		for($i=0;$i<count($_REQUEST['asset_id']);$i++)
		{
			$data1['receive_id']=$last_id;
			$data1['issue_id']=$_REQUEST['issue_row_id'][$i];
			$data1['issue_row_id']=$_REQUEST['issue_row_id'][$i];
			$data1['requested_id']=$_REQUEST['rid'];
			$data1['asset_id']=$_REQUEST['asset_id'][$i];
			$data1['asset_row_id']=$_REQUEST['asset_row_id'][$i];
			//$data1['required_qty']=$_REQUEST['required_qty'][$i];
			//$data1['issue_qty']=$_REQUEST['issue_qty'][$i];
			//$data1['received_qty']=$_REQUEST['received_qty'][$i];
                        $data1['required_qty']=1;
                         $data1['approved_qty']=1;
                        $data1['issue_qty']=1;
                        $data1['received_qty']=1;
			if($_REQUEST['status'][$i]){
			$data1['status']=$_REQUEST['status'][$i];
                        $dataArray2['status']=$_REQUEST['status'][$i];			
			$dataArray2['received_qty']=1;
                        }
			$s->RecordInsert('tpl_asset_transfer_receive_details',$data1);
			
			//$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);
                       // $cond2="id='".$_REQUEST['issue_row_id'][$i]."'";

			$cond3="id='".$_REQUEST['asset_row_id'][$i]."'";
                   
			//$s->RecordEdit('tpl_asset_transfer_issue_details', $dataArray2, $cond2);
                        $s->RecordEdit('tpl_asset_transfer_request_details', $dataArray2, $cond3);
                        $cond4="id='".$_REQUEST['issue_row_id'][$i]."'";
                         $s->RecordEdit('tpl_asset_transfer_issue_details', $dataArray2, $cond4);
			
			
		}
	}
	$count=0;
	$count1=0;$count2=0;$count3=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']=='Received')
		{
			$count=$count+1;
		}
		if($row['status']=='Issued')
		{
			$count1=$count1+1;
		}
                if($row['status']=='Rejected')
		{
			$count2=$count2+1;
		}
                if($row['status']=='Pending')
		{
			$count3=$count3+1;
		}
	}
        
        if($count+$count2==$numrecord)
	{
		$dataArray1['received_status']='Received';
	}elseif($count>0 && ($count1>0 || $count2>0 || $count3>0))
	{
		$dataArray1['received_status']='Partial Received';
	}
        
	
	if(count($dataArray1)>0){
		$cond1="id='".$_REQUEST['rid']."'";
		$s->RecordEdit('tpl_asset_transfer_request', $dataArray1, $cond1);
	}
        
        $count=0;
	$count1=0;$count2=0;$count3=0;
	$SQLUpdates=DB_query("select status from tpl_asset_transfer_issue_details where requested_id='".$_REQUEST['rid']."'",$db);
	$numrecords=DB_num_rows($SQLUpdates);
	while($rows=DB_fetch_array($SQLUpdates))
	{
		if($rows['status']=='Received')
		{
			$count=$count+1;
		}
		if($rows['status']=='Issued')
		{
			$count1=$count1+1;
		}
                if($rows['status']=='Rejected')
		{
			$count2=$count2+1;
		}
                if($rows['status']=='Pending')
		{
			$count3=$count3+1;
		}
	}
        
        if($count+$count2==$numrecords)
	{
		$dataArray4['status']='Received';
	}elseif($count>0 && ($count1>0 || $count2>0 || $count3>0))
	{
		$dataArray4['status']='Partial Received';
	}
        
	
	if(count($dataArray4)>0){
                $cond4="transfer_reqno='".$_REQUEST['rid']."'";
		$s->RecordEdit('tpl_asset_transfer_issue', $dataArray4, $cond4);
	}
	
	
        $count4=0;$count5=0;
        $SQLUp=DB_query("select status from tpl_asset_transfer_receive_details where receive_id='".$last_id."'",$db);
	$numre=DB_num_rows($SQLUp);
	while($roww=DB_fetch_array($SQLUp))
	{
		if($roww['status']=='Received')
		{
			$count4=$count4+1;
                        $count4;
		}
		if($roww['status']=='')
		{
			$count5=$count5+1;
                        $count5;
		}
	}
	if($count4==$numre)
	{
		$dataArray3['status']='Received';
	}elseif($count5>0){
		$dataArray3['status']='Partial Received';	
	}
	if(count($dataArray3)>0){
		$cond4="id='".$last_id."'";
		$s->RecordEdit('tpl_asset_transfer_receive', $dataArray3, $cond4);
	}
      
      
        
	//exit;
	$_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.requested_by, I.transfer_reqno, I.remarks, I.id as issueid, A.approved_by 
		   from tpl_asset_transfer_request T 
		   LEFT JOIN tpl_asset_transfer_issue I ON I.transfer_reqno=T.id 
     	   INNER JOIN tpl_asset_transfer_request_approval A ON A.request_id=T.id
		   WHERE I.id='".$_REQUEST['rid']."'";

$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>
	<td colspan="6">
	<fieldset><legend style="font-size:13px;"><strong>General Details</strong></legend>
<table width="100%" align="center" border="1" cellpadding="2" cellspacing="1" style="border:#808080 1px solid;">
  <tr>
    <th width="13%"><strong>Branch</strong></th>
    <td width="20%"><?php echo $s->GetFieldValue('locations','loccode="'.$reqRow['requested_site'].'"','locationname'); ?>
		<input type="hidden" name="receive_site" value="<?php echo $reqRow['requested_site'];?>" />    </td>
		<th width="13%"><strong>ARN No. <font style="color:#FF0000;">*</font></strong></th>
    <td width="20%"><input type="text" name="receive_no" id="receive_no" value="<?php echo ($receive_no !='')? $receive_no : "Auto Generated";?>" readonly="" style="width:100%;" /></td>
    <th><strong>ARN Date<font style="color:#FF0000;">*</font></strong></th>
    <td width="20%"><input type="text" name="receive_date" id="receive_date" value="<?php echo ($receive_date !='')?ConvertSQLDate($receive_date):date("d/m/Y"); ?>" class="datepicker" style="width:100%;" />    </td>
    
  </tr>
  <tr>
    <th><strong>ATR No.</strong></th>
    <td><?php echo $reqRow['request_no'];?><input type="hidden" value="<?php echo $reqRow['transfer_reqno'];?>" name="request_id"  /></td>
    <th><strong>ATR Date</strong></th>
    <td><?php echo ($reqRow['request_date'] !='')?ConvertSQLDate($reqRow['request_date']):'';?></td>
    <th><strong>Requested By</strong></th>
    <td><?php echo $s->GetFieldValue('empmaster','id="'.$reqRow['requested_by'].'"','emp_name');?></td>
  </tr>
  <tr>
    
    <th><strong>Approved By</strong></th>
    <td><?php echo $s->GetFieldValue('www_users','id="'.$reqRow['approved_by'].'"','realname');?></td>
    <th><strong>Received By</strong></th>
    <td><select name="received_by" id="received_by" style="width:100%;">
			<option value="0">--Select--</option>
			<?php
			$empsql = "select id, emp_name from empmaster where emp_site='".$reqRow['requested_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']==$requested_by) ? "selected" : ""; ?>><?php echo $empRow['emp_name'];?></option>
		<?php }
			?>
	</select>		
	</td>
  </tr>
  <tr>
    <th><strong>Remark</strong></th>
    <td colspan="2"><textarea style="width:100%;"   name="remarks"  id="remarks"><?php echo $remarks; ?></textarea></td>
    <th><strong>Issued Remark</strong></th>
    <td colspan="2"><?php echo $reqRow['remarks']; ?></td>
  </tr>
</table>
</fieldset>
	
	</td>
</tr>
<tr>
	<td colspan="6">
	<fieldset><legend style="font-size:13px;"><strong>Assets Details</strong></legend>
	<table width="100%" border="1" cellpadding="2" cellspacing="1" style="border:#808080 1px solid;">
  <tr>
    <th>S. No.</th>
    <th>Asset Code</th>
    <th width="20%">Asset Name</th>
    <th width="15%">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><?php echo $r['title_name'];?></th>
    <?php } ?>
    <!--<th>Issue Qty</th>
    <th>Pending Qty</th>-->
    <th>Select</th>
  </tr>
  <?php
$s=new db_query();
$result2=DB_query("select * from tpl_asset_transfer_issue_details where issue_id='".$reqRow['issueid']."' and status='Issued'",$db);
$i=0;
while($myrow2=DB_fetch_array($result2))
{
 //echo "<pre>";print_r($myrow2);
	$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);
?>
  <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'];?>" /></td>
    <td><?php echo $assetRow['code'];?><input type="hidden" name="asset_id[]" id="asset_id_<?php echo $i;?>" value="<?php echo $myrow2['asset_id'];?>" /></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 align="left"><input type="radio" name="status[<?php echo $i; ?>]" id="status_<?php echo $i; ?>" value="Received" />
    <input type="hidden" name="issue_qty[]" id="issue_qty_<?php echo $i;?>" value="<?php echo $myrow2['issue_qty'];?>" style="width:90px;" />
    <input type="hidden" name="pnd_qty[]" id="pnd_qty_<?php echo $i;?>" value="<?php echo $myrow2['received_qty'];?>" style="width:90px;" />
</td>
  	<?php $i++;
		$editcount = $i;
		}  ?>
</table>
<input type="hidden" value="<?php echo $editcount;?>" name="editcnt" id="editcnt" />
</fieldset>
	
	</td>
</tr>		  


<tr>
	<td class="tabledata1" style="text-align:center;" colspan="6">
	<input type="submit" value="Save" name="<?php if($_REQUEST['action']=='edit') echo 'update'; else echo '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="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 validateQty(apQty,reqQty,recqty)
{
	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)+parseInt(recqty);
	if(approvedQty>requestedQty)
	{
		alert("Received quantity must be less than or equal to Issued quantity");
		document.getElementById(apQty).value = '';
	}
}



</script>