<?php 
include("includes/stockAssetCalculation.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'])
{	
	
    
    $sql="select * from tpl_asset_transfer_issue where site='".$_REQUEST['issue_site']."' ORDER BY id desc limit 1";
	$qyprefix=DB_query("Select prefix from locations Where loccode ='".$_REQUEST['issue_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['issue_no']);
	$maxval=$Explo[2];
	$strFinal = $maxval+1;
	$strBothFinal = $projectprefix."/"."TRFI".'/'.str_pad($strFinal,4,"0",STR_PAD_LEFT);
	}
	else
	{
	$strBothFinal= $projectprefix."/"."TRFI".'/'.'0001';
	}
	
	$data['issue_date']=FormatDateForSQL($_REQUEST['issue_date']);
	$data['exp_return_date']=FormatDateForSQL($_REQUEST['exp_return_date']);
	$data['issue_no']=$strBothFinal;
	$data['approval_no'] = $_REQUEST['aprid'];
	$data['transfer_reqno']=$_REQUEST['rid'];
	$data['site']=$_REQUEST['issue_site'];
	$data['issued_by'] = $_REQUEST['issued_by'];
	$data['remarks']=$_REQUEST['remarks'];
	$data['status']='Pending';
	$data['created']=date('Y-m-d');
	$data['created_by']=$_SESSION['UserID'];
	$s->RecordInsert('tpl_asset_transfer_issue',$data);
	$last_id=DB_Last_Insert_Id($db,"tpl_asset_transfer_issue","id");
	//echo "<pre>";
	//print_r($data);
	if($last_id !='')
	{
		for($i=0;$i<count($_REQUEST['asset_id']);$i++)
		{
			
                        $data1['issue_id']=$last_id;
			$data1['requested_id']=$_REQUEST['rid'];
			$data1['approved_id']=$_REQUEST['aprid'];
			$data1['asset_id']=$_REQUEST['asset_id'][$i];
			$data1['asset_row_id']=$_REQUEST['asset_row_id'][$i];
			$data1['apr_row_id']=$_REQUEST['apr_row_id'][$i];
			$data1['approved_qty']=1;
			$data1['issue_qty']=1;
                        $data1['required_qty']=1;
                        if($_REQUEST['status'][$i]!=''){
			$data1['status']=$_REQUEST['status'][$i];
                        $dataArray2['status']=$_REQUEST['status'][$i];			
			$dataArray2['issue_qty']=1;
                        }
			$s->RecordInsert('tpl_asset_transfer_issue_details',$data1);
			
			
			$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;$count2=0;$count3=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']=='Approved')
		{
			$count1=$count1+1;
		}
                if($row['status']=='Rejected')
		{
			$count2=$count2+1;
		}
                if($row['status']=='Pending')
		{
			$count3=$count3+1;
		}
	}
	
	if($count+$count2==$numrecord)
	{
		$dataArray1['issued_status']='Issued';
	}elseif($count>0 && ($count1>0 || $count2>0 || $count3>0))
	{
		$dataArray1['issued_status']='Partial Issued';
	}
	
	if(count($dataArray1)>0){
		$cond1="id='".$_REQUEST['rid']."'";
		$s->RecordEdit('tpl_asset_transfer_request', $dataArray1, $cond1);
	}
       // echo "select status from tpl_asset_transfer_issue_details where issue_id='".$last_id."'";
       
         $count=0;
	$count1=0;$count2=0;$count3=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_approval_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']=='Approved')
		{
			$count1=$count1+1;
		}
                if($row['status']=='Rejected')
		{
			$count2=$count2+1;
		}
                if($row['status']=='Pending')
		{
			$count3=$count3+1;
		}
	}
	
	if($count+$count2==$numrecord)
	{
		$dataAa['status']='Issued';
	}
        elseif($count>0 && ($count1>0 || $count2>0 || $count3>0))
	{
		$dataAa['status']='Partial Issued';
	}
	
	if(count($dataAa)>0){
		$cond1="request_id ='".$_REQUEST['rid']."'";
		$s->RecordEdit('tpl_asset_transfer_request_approval', $dataAa, $cond1);
	}
        
        
        
       // echo "select status from tpl_asset_transfer_issue_details where issue_id='".$last_id."'";
        $count4=0;$count5=0;
        $SQLUp=DB_query("select status from tpl_asset_transfer_issue_details where issue_id='".$last_id."'",$db);
	$numre=DB_num_rows($SQLUp);
	while($roww=DB_fetch_array($SQLUp))
	{
		if($roww['status']=='Issued')
		{
			$count4=$count4+1;
                       // echo $count4;
		}
		if($roww['status']=='')
		{
			$count5=$count5+1;
                       // echo $count5;
		}
	}
	if($count4==$numre)
	{
		$dataArray3['status']='Issued';
	}elseif($count5>0){
		$dataArray3['status']='Partial Issued';	
	}
	if(count($dataArray3)>0){
		$cond4="id='".$last_id."'";
		$s->RecordEdit('tpl_asset_transfer_issue', $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.*,A.approved_site,A.approved_by, A.id as aprid from tpl_asset_transfer_request T INNER JOIN tpl_asset_transfer_request_approval A ON A.request_id=T.id WHERE T.id='".$_REQUEST['rid']."' AND A.id='".$_REQUEST['aprid']."'";

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

$reqRow = DB_fetch_array($result);
?>
<style type="text/css">
.center{text-align:left;}
</style>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Fixed Asset Transfer Issue Add</h1>
<form name="" id="" method="post">
<table width="100%" cellpadding="3" cellspacing="0" border="0">
<tr>
	<td class="tabledata1" colspan="4">
	<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>Issue Branch</strong></th>
    <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>
		<th width="13%"><strong>ATN No. <font style="color:#FF0000;">*</font></strong></th>
    <td width="20%"><input type="text" name="issue_no" id="issue_no" value="<?php echo ($issue_no !='')? $issue_no : "Auto Generated";?>" readonly=""  style="width:100%;"/></td>
    <th><strong>ATN Date<font style="color:#FF0000;">*</font></strong></th>
    <td width="20%"><input type="text" name="issue_date" id="issue_date" value="<?php echo ($issue_date !='')?ConvertSQLDate($issue_date):''; ?>" class="datepicker" style="width:100%;" />    </td>
    
  </tr>
  <tr>
    <th><strong>Requested Branch</strong></th>
    <td><?php echo $s->GetFieldValue('locations','loccode="'.$reqRow['requested_site'].'"','locationname'); ?>
		<input type="hidden" name="requested_site" id="requested_site" value="<?php echo $reqRow['requested_site'];?>" />
	</td>
    <th><strong>ATR No.</strong></th>
    <td><?php echo $reqRow['request_no'];?></td>
    <th><strong>ATR Date</strong></th>
    <td><?php echo ($reqRow['request_date'] !='')?ConvertSQLDate($reqRow['request_date']):'';?></td>
  </tr>
  <tr>
    <th><strong>Requested By</strong></th>
    <td><?php echo $s->GetFieldValue('empmaster','id="'.$reqRow['requested_by'].'"','emp_name');?></td>
    <th><strong>Purpose</strong></th>
    <td><?php echo $reqRow['purpose'];?></td>
    <th><strong>Returnable Y/N</strong></th>
    <td><?php echo $reqRow['returnable'];?></td>
  </tr>
  <tr>
    <th><strong>Issued By</strong></th>
    <td>
		<select name="issued_by" id="issued_by" style="width:100%;" >
			<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['emp_name'];?></option>
		<?php }?>
		
		
		</select>
	</td>
    <th><strong>Expected Return Date</strong></th>
    <td><input type="text" name="exp_return_date" id="exp_return_date" value="<?php echo ($exp_return_date !='') ? ConvertSQLDate($exp_return_date):''; ?>" class="datepicker" style="width:100%;" /></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr>
  <tr>
    <th><strong>Remarks</strong></th>
    <td colspan="5"><textarea style="width:100%;"   name="remarks"  id="remarks"><?php echo $remarks; ?></textarea></td>
    
  </tr>
</table>
</fieldset>
	
	</td>
</tr>
<tr>
	<td colspan="4">
	<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 align="center">S. No.</th>
    <th align="center">Asset Code</th>
    <th width="20%" align="center">Asset Name</th>
    <th align="center">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><strong><?php echo $r['title_name'];?></strong></th>
    <?php } ?>
    <th align="left">Select</th>
  </tr>
  <?php
$s=new db_query();
//echo "select * from tpl_asset_transfer_request_approval_details where approved_id='".$reqRow['aprid']."' and status!='Issued'";

$result2=DB_query("select * from tpl_asset_transfer_request_approval_details where approved_id='".$reqRow['aprid']."' and status!='Issued' and status!='Rejected' ",$db);
$i=0;
while($myrow2=DB_fetch_array($result2))
{

 	$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 align="center" class="center"><?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="apr_row_id[]" id="apr_row_id_<?php echo $i;?>" value="<?php echo $myrow2['id'];?>" /></td>
    <td align="center" class="center"><?php echo $assetRow['code'];?>
      <input type="hidden" name="asset_id[]" id="asset_id_<?php echo $i;?>" value="<?php echo $myrow2['asset_id'];?>" /></td>
    <td align="center" class="center"><?php echo $assetRow['name'];?></td>
    <td align="center" class="center"><?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="Issued" /></td>
  </tr>
  <?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="4">
	<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="aprid" value="<?php echo $_REQUEST['aprid'];?>" />
</form>
<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(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("Issue quantity must be less than or equal to Approved quantity");
		document.getElementById(apQty).value = '';
	}
}



</script>