<?php session_name('CAKEPHP'); session_start();
$PageSecurity = 4;
$PageName = 'Add Items';
include('includes/DefinePOClass.php');
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/DefineSuppTransClass.php');
//include('includes/header.inc');
include('includes/function_lib.php');
include('includes/stockAssetCalculation.php');
?>
<script language="javascript">
function getdata()
{
var k=window.opener.document.getElementById('project').value;
var t=window.opener.document.getElementById('request_date').value;
var t1=window.opener.document.getElementById('remarks').value;
document.getElementById("project").value=k;
document.getElementById("request_date").value=t;
document.getElementById("remarks").value=t1;

}

function getAssets(id,code,name,group,avail,ctr)
{
	var dup='empty';
	var itemcount=parseInt(window.opener.document.getElementById('hiditemcount').value)+1;
	var edit_cnt=window.opener.document.getElementById('edit_cnt').value;
	
	if(edit_cnt=='')
		{var counter=1;} 
	else if(edit_cnt>itemcount)
		{var counter=edit_cnt;
		var itemcount=parseFloat(counter)+parseFloat(itemcount);
	} else  {var counter=edit_cnt;}
	if(document.getElementById("row_id_"+ctr).checked==true){
		if(window.opener.document.getElementById('asset_code').value=='')
		{
			if(edit_cnt==''){var counter=0;}
			for(var j=0; j<counter; j++)
			{
				if(window.opener.document.getElementById('asset_code_'+j).value==id)
				{
					alert("Duplicate Value"); 
					document.getElementById("row_id_"+ctr).checked=false;
					var dup = "dup";
					break;
				}
			}
			if(dup=='empty'){
				window.opener.document.getElementById('asset_code').value = id;
				window.opener.document.getElementById('assetcode').value = code;
				window.opener.document.getElementById('assetname').value = name;
				window.opener.document.getElementById('asset_group').value = group;
				window.opener.document.getElementById('avail_qty').value = avail;
				
			}
		}
		else
		{
			for(var i=counter; i<=itemcount; i++)
			{	if(edit_cnt==''){var counter=0;}
				for(var j=0; j<counter; j++)
				{
					if(window.opener.document.getElementById('asset_code_'+j).value==id)
					{
						alert("Duplicate Value"); 
						document.getElementById("row_id_"+ctr).checked=false;
						var dup = "dup";
						break;
					}
				}
				if(window.opener.document.getElementById('asset_code').value==id)
					{
						alert("Duplicate Value");
						document.getElementById("row_id_"+ctr).checked=false;
						break;
					}
				if(window.opener.document.getElementById('asset_code_'+i).value==id)
					{
						alert("Duplicate Value"); 
						document.getElementById("row_id_"+ctr).checked=false;
						break;
					}
				if(dup=='empty'){				
					if(window.opener.document.getElementById('asset_code_'+i).value=='')
					{
						window.opener.document.getElementById('asset_code_'+i).value = id;
						window.opener.document.getElementById('assetcode_'+i).value = code;
						window.opener.document.getElementById('assetname_'+i).value = name;
						window.opener.document.getElementById('asset_group_'+i).value = group;
						window.opener.document.getElementById('avail_qty_'+i).value = avail;



						break;
						break;
					}
					else
					{
						continue;
					}
				}
			}
		}
	}
	else
	{
		if(window.opener.document.getElementById('asset_code').value==document.getElementById("row_id_"+ctr).value)
		{
			window.opener.document.getElementById('asset_code').value = id;
			window.opener.document.getElementById('assetcode').value = code;
			window.opener.document.getElementById('assetname').value = name;
			window.opener.document.getElementById('asset_group').value = group;
			window.opener.document.getElementById('avail_qty').value = avail;
		}
		else
		{
			for(var i=counter; i<itemcount; i++)
			{
				if(window.opener.document.getElementById('asset_code_'+i).value==document.getElementById("row_id_"+ctr).value)
				{
					window.opener.document.getElementById('asset_code_'+i).value = id;
					window.opener.document.getElementById('assetcode_'+i).value = code;
					window.opener.document.getElementById('assetname_'+i).value = name;
					window.opener.document.getElementById('asset_group_'+i).value = group;
					window.opener.document.getElementById('avail_qty_'+i).value = avail;
				
				}
			}
		}
	}
}

</script>
<?php 
if($_REQUEST['Select'] != '')
{	
	$item_array=array();
	$item_array=$_REQUEST['row_id'];
	$_SESSION['manvi']=$item_array;
	//print_r($_SESSION['manvi']);	
	$itemlist=implode("#",$_SESSION['manvi']);
	//$_SESSION['manvi']=$itemlist;
	?>
<script type="text/javascript">
opener.window.location="index.php?route=ToolPlants/Transaction/asset_breakdown_management&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&rid=<?php echo $_REQUEST['reid'];?>&param=<?php echo $_REQUEST['row_id'];; ?>
&action=<?php echo $_REQUEST['action1']; ?>&NewRequisition=Yes&project=<?php echo $_REQUEST['project']; ?>
&request_date=<?php echo $_REQUEST['request_date']; ?>&remarks=<?php echo $_REQUEST['remarks']; ?>";self.close();

</script>
<?php 
}
?>
<?php
 //---------------------------Search Starts here-------------------------------
 $function_filter[] = $_REQUEST['site'];
 $function_filter[] = '';
 $function_filter[] = '--';


		$sql_search = "Select * from tpl_fixed_asset_definition where CompanyId='".$_SESSION['ACGCompanyId']."'";
		
		//echo $sql_search ;
			$search_result = DB_query($sql_search, $db);
			//echo $sql_search;
	
			
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<body>
<form name="form1"  method="post">
 <input style="margin:0px 0px 10px 0px;" type="submit" name="Select" id="Select" value="OK">
<input type="text" name="project" value="" id="project"/>
<input type="hidden" name="request_date" value="" id="request_date"/>
<input type="hidden" name="remarks" value="" id="remarks"/>
<table width="100%" border="1" cellpadding="2" cellspacing="1" style="border:#808080 1px solid;">
<tr>
	<td colspan="7" style="font-weight:bold; color:#0033FF;">
	Heavy Machinary Assets</td>
</tr>
<tr>
	<th bgcolor="#b7b7b7">Select</th>
	<th bgcolor="#b7b7b7">Assect Code</th>
	<th bgcolor="#b7b7b7">Assect Group</th>
	<th bgcolor="#b7b7b7">Assect Name</th>
	<th bgcolor="#b7b7b7">Serial No</th>
	<th bgcolor="#b7b7b7">UOM</th>
	<th bgcolor="#b7b7b7">Available Qty</th>
</tr>
<?Php
	$i=0;
  	if($i%2==0)
	{$bg="#CCCCCC";}
	else
	$bg="#C8BECB";


	if(DB_num_rows($search_result)>0)
	{
	 	while($myrow = DB_fetch_array($search_result))
	 	{?>
        <?php 	
 	 $parameter=$myrow['id'];
  //$avalQty=$s->GetFieldValue('opening_stock_For_Toolsplants_machine',"machine_id='".$myrow['id']."'",'stock');
  
	
	$op = openingBalance($function_filter,$myrow['id']);
	
	$out_1 = issueSlip($function_filter,$myrow['id']);
	
	$in_2 = receivedSiteToSite($function_filter,$myrow['id']);
	$invalue = $op+$in_2;
	
	$outvalue = $out_1;
	$tot = trim($invalue)-trim($outvalue);
	
	
	$avalQty=$tot;
  
  
  
  ?>
<tr>
	<td><input type="checkbox" name="row_id[<?php echo $i;?>]" id="row_id_<?php echo $i;?>" value="<?php echo $parameter;?>"<?php echo $c;?> onClick="getAssets('<?php echo $myrow['id'];?>','<?php echo $myrow['asset_code_prefix'];?>','<?php echo $myrow['asset_name'];?>','<?php echo $s->getField('tpl_materialcategorydefinition','id',$myrow['asset_group'],'asset_capitalisation'); ?>','<?php echo $avalQty;?>','<?php echo $i;?>');"/></td>
	<td><?php echo $myrow['asset_code_prefix']; ?></td>
	<td><strong><?php echo $s->getField('tpl_materialcategorydefinition','id',$myrow['asset_group'],'asset_capitalisation'); ?></strong></td>
	<td><?php echo $myrow['asset_name'];?></td>
	<td><?php echo $myrow['serial_no']; ?></td>
	<td><?php echo $s->getField('unitsofmeasure','unitid',$myrow['unit_of_measurement'],'unitname'); ?></td>
	<td><?php echo $avalQty; ?></td>         
</tr>
        <?php $i++;
	 } 
  }
	else
	{?>
	 <tr>
      <td colspan="9"><h3 align="center">No Items Found</h3></td>
    </tr>
        <?php 
	}
?>
</table>
</form>
</body>
