<h3><?php echo $page_title;?></h3>

<?php 
$url_cancel= "index.php?route=ToolPlants/fixed_assets/fixed_assets_transfer_issue_index".$rurl;
$url_add= "index.php?route=ToolPlants/maintenance/AMCEntry".$rurl;



$table="tpl_amc_entry";

// begin request no Will Be Generated On Saving
	$sql='select * from tpl_amc_entry where project ="'.$_REQUEST['project'].'" ';
	$qyprefix=DB_query("Select prefix from locations Where loccode ='".$_REQUEST['project']."'",$db);
	$row_prefix=DB_fetch_array($qyprefix);
	 $projectprefix=$row_prefix['prefix'];
//echo $sql;
 	$result = DB_query($sql,$db);
	$numrows = DB_num_rows($result);
	
	
	$request_date=$_REQUEST['amc_date'];
	
	if($numrows>0)
	{
		$i=0;
		$strmax = "";
		$myrow=DB_Fetch_array($result);
		if(!empty($_REQUEST['project']))
		{
			
			if($numrows>0)
			{ 
				$maxval = $numrows;
			}
			else
			{
				$maxval=0;
			}
			$strChar = "AMCNO".$Prefix_code."/".$projectprefix."/".$request_date."/";
		}
	}
	else
	{
		$strChar = "AMCNO".$Prefix_code."/".$projectprefix."/".$request_date."/";
	}
	$strFinal = $maxval+1;
	$strBothFinal = $strChar.str_pad($strFinal,4,"0",STR_PAD_LEFT);
	$strBothFinal;
	//echo $strBothFinal; exit;
	// end request no Will Be Generated On Saving

if(isset($_REQUEST['Save']))
{
     $data['vendorid']=$_REQUEST['vendorid']; 
     $data['assetgroup']=$_REQUEST['assetgroup']; 
	$data['project']=$_REQUEST['project']; 
	$data['amc_refno']=$strBothFinal; 
 	$data['amc_date']=FormatDateForSQL($_REQUEST['amc_date']); 
	$data['remark']=$_REQUEST['remark']; 
    if($_REQUEST['action']=='AddNew'){
     $r=$s->RecordInsert($table, $data);
	$last_id=DB_Last_Insert_Id($db,$table,"id");
		
	for($i=0;$i<count($_REQUEST['asset_code']);$i++)
			{	
					    $insert_consistency_details=array('id'=>'',
						'tbl_amc_entry_id'=>$last_id,
						'asset_group'=>$_REQUEST['asset_group'][$i],
						'asset_code'=>$_REQUEST['asset_code'][$i],
						'asset_name'=>$_REQUEST['deprec_mst_name'][$i],
						'amc_period_s_date'=>FormatDateForSQL($_REQUEST['amc_period_s_date'][$i]),
						'amc_period_e_date'=>FormatDateForSQL($_REQUEST['amc_period_e_date'][$i]),
						'amc_amount'=>$_REQUEST['amc_amount'][$i],
						'additional_amt'=>$_REQUEST['amc_amount_billed'][$i],
						'additional_amt_billed'=>$_REQUEST['amc_additional_amt'][$i],
						'amc_amount_billed'=>$_REQUEST['amc_additional_amt_billed'][$i],
						'total_amt'=>$_REQUEST['total_amt'][$i],
						'bal_amt'=>$_REQUEST['bal_amt'][$i]);
					if(!empty($insert_consistency_details['tbl_amc_entry_id']))
					{
						$s->RecordInsert('tbl_amc_entry_details',$insert_consistency_details);
					}
			
			}
	 if($r)
	 {
	  unset($_SESSION['manvi']);
	  $s->pageLocation($url_add.'&daction=Insert');
	 }
	 }
	 elseif($_REQUEST['action']=='Edit')
	 {
		$data['vendorid']=$_REQUEST['vendorid']; 
		$data['assetgroup']=$_REQUEST['assetgroup']; 
		$data['project']=$_REQUEST['project']; 
		$data['amc_date']=FormatDateForSQL($_REQUEST['amc_date']); 
		$data['remark']=$_REQUEST['remark'];  
		$cond="id='".$_REQUEST['Vid']."'";
		$r=$s->RecordEdit($table, $data, $cond);
		if($s->DeleteRecord('tbl_amc_entry_details',"tbl_amc_entry_id=".$_REQUEST['Vid']))
			{
			$last_id=$_REQUEST['Vid'];
				
				for($i=0;$i<count($_REQUEST['asset_code']);$i++)
				{			
					$insert_consistency_details=array('id'=>'',
						'tbl_amc_entry_id'=>$last_id,
						'asset_group'=>$_REQUEST['asset_group'][$i],
						'asset_code'=>$_REQUEST['asset_code'][$i],
						'asset_name'=>$_REQUEST['deprec_mst_name'][$i],
						'amc_period_s_date'=>FormatDateForSQL($_REQUEST['amc_period_s_date'][$i]),
						'amc_period_e_date'=>FormatDateForSQL($_REQUEST['amc_period_e_date'][$i]),
						'amc_amount'=>$_REQUEST['amc_amount'][$i],
						'additional_amt'=>$_REQUEST['amc_amount_billed'][$i],
						'additional_amt_billed'=>$_REQUEST['amc_additional_amt'][$i],
						'amc_amount_billed'=>$_REQUEST['amc_additional_amt_billed'][$i],
						'total_amt'=>$_REQUEST['total_amt'][$i],
						'bal_amt'=>$_REQUEST['bal_amt'][$i]);
						
					if(!empty($insert_consistency_details['tbl_amc_entry_id']))
					{
					$s->RecordInsert('tbl_amc_entry_details',$insert_consistency_details);
					}
				}
			}	  
		unset($_SESSION['manvi']);
		$s->pageLocation($url_add.'&daction=Updated');

	}

	 
}

				if($_REQUEST['action']=='Delete')
				{
				   $condition="id='".$_REQUEST['Vid']."'";
				   $condition1="tbl_amc_entry_id='".$_REQUEST['Vid']."'";
				   $r=$s->DeleteRecord($table,  $condition);
				   $r=$s->DeleteRecord('tbl_amc_entry_details',$condition1);
				   $s->pageLocation($redirect_page.'&daction=Deleted');
				}

		if($_REQUEST['action']=='Edit')
		{
			$condition="id='".$_REQUEST['Vid']."'";
		   $result= $s->GetRecordById($table,  $condition);
		   while($rowdata=DB_fetch_array( $result))
		   {
			$vendorid=$rowdata['vendorid']; 
			$assetgroup=$rowdata['assetgroup']; 
			$project=$rowdata['project']; 
			$amc_refno=$rowdata['amc_refno']; 
			$amc_date=ConvertSQLDate($rowdata['amc_date']); 
			$remark=$rowdata['remark']; 
		   }
		}
		else {
			$vendorid=$_REQUEST['vendorid'];
			$project=$_REQUEST['project'];
			$assetgroup=$_REQUEST['assetgroup']; 
			$project=$_REQUEST['project']; 
			$amc_date=$_REQUEST['amc_date']; 
			$remark=$_REQUEST['remark']; 
		}
?>

<?php 
if($_REQUEST['daction'])
{
  echo "<div class='success'>Record ".$_REQUEST['daction']." Successfully</div>";
}
  if(isset($_REQUEST['action']) &&($_REQUEST['action']=='AddNew' || $_REQUEST['action']=='Edit'))
  {
?> 
<form action="" method="post">
<table width="100%" border="0" cellpadding="4" cellspacing="4">
  <tr  valign="top">
    <th>Vendor</th>
    
      <td width="33%">
      	<select name="vendorid" id="vendor" style="width:100%;">
			<option value="">Select</option>
			 <?php
		    $query4=DB_query("SELECT * FROM suppliers",$db);
			while($row4=DB_fetch_array($query4))
				{
					$s='';
					if($vendorid==$row4['id'])
					$s="selected";
					?>	
					 <option value="<?php echo $row4['id'];?>" <?php echo $s;?> ><?php echo $row4['vendor_name'];?></option>
				<?php }  ?>
		   </select> 
		    </td>        
        <th>Project</th>
      <td width="32%">
      	<select name="project" id="project" style="width:100%;">
			<option value="">Select</option>
			 <?php
		    $query4=DB_query("Select * from locations ",$db);
			while($row4=DB_fetch_array($query4))
				{
					$s='';
					if($project==$row4['loccode'])
					$s="selected";
					?>
					<option value="<?php echo $row4['loccode'];?>" <?php echo $s;?>><?php echo $row4['locationname'];?></option>
				<?php }
		  ?>
			</select>
      	</td>
  </tr>
   <tr  valign="top">
    <th>AMC Ref No &amp; Date </th>
    <td><input name="amc_refno" type="text"  value="<?php if($_REQUEST['action']!='AddNew') echo $amc_refno; else echo "Auto Generated"; ?>" size="10" readonly="true"/>
	     <input name="amc_date" type="text" id="amc_date"  class="datepicker" value="<?php echo $amc_date;?>" size="10"/>
    
    </td> 
    <th>Asset Group</th>
    <td><?php  echo $ClassMaterial->Material($assetgroup);?></td>
  </tr>
   <tr  valign="top">
    <th>Remark</th>
    <td colspan="3">
	<textarea name="remark" id="remark" rows="2" cols="40"><?php  echo $remark;?></textarea>
	 </td>
  </tr>
   <tr  valign="top">
    
    <td colspan="4" style="text-align: left;"><input type="button" value="Add Item" name="item_popup" class="add"  onclick="open_items()"> </td>
   
  </tr>
</table> 

<div align="center">
<div style="overflow:auto; width:800px;" >
<table width="100%" border="0" align="center">
<tr>
<td bgcolor="#c6c6c6" ><table  border="0" cellspacing="1" cellpadding="5" align="center">
  <tr>
  	<th><strong>Select</strong></th>
  	<th><strong>Asset Group</strong></th>
   	<th><strong>Asset Code</strong></th>
    <th><strong>Asset Name</strong></th>
    <th><strong>AMC Period Start Date</strong></th>
   	<th><strong>AMC Period End Date</strong></th>
   	<th><strong>AMC Amount</strong></th>
   	<th><strong>AMC Amount Billed</strong></th>
   	<th><strong>Additional Amount</strong></th>
   	<th><strong>Additional Amount Billed</strong></th>
    <th><strong>Total Amt</strong></th>
    <th><strong>Balance Amt</strong></th>
  </tr>
  <?php if($_REQUEST['Vid']!='')
   { 
  
   $result2=DB_query("select * from tbl_amc_entry_details where tbl_amc_entry_id='".$_REQUEST['Vid']."'",$db);
   $i=0;
  $_SESSION['qutotation_itemcode']=array();
  while($myrow2=DB_fetch_array($result2))
  {
  	$asset=DB_query("select * from tpl_materialcategorydefinition where id='".$myrow2['asset_group']."' ",$db);
	$asset1=DB_fetch_array($asset);
	$assetgroupt=$asset1['asset_capitalisation'];	
  	$_SESSION['qutotation_itemcode'][]=$myrow2['id'];
	?>
	<tr bgcolor="#ffffff">
<td><input type="checkbox" name="select[]" id="select_<?php echo $i;?>"   onchange="checkdata(<?php echo $i; ?>)"> 	</td>
<td><?php echo $assetgroupt; ?><input type="hidden" name="asset_group[]" value="<?php echo $myrow2['asset_group']; ?>" /></td>
<td><?php echo $myrow2['asset_code']; ?><input type="hidden" name="asset_code[]" id="asset_code_<?php echo $i; ?>" value="<?php echo $myrow2['asset_code']; ?>" /></td>
<td><?php echo $myrow2['asset_name']; ?><input type="hidden" name="deprec_mst_name[]" id="deprec_mst_name_<?php echo $i;?>" value="<?php echo $myrow2['asset_name'];?>"/>	</td>
<td><input type="text" name="amc_period_s_date[]" id="amc_period_s_date_<?php echo $i;?>"  class="datepicker" value="<?php echo ConvertSQLDate($myrow2['amc_period_s_date']); ?>" /></td>
<td><input type="text" name="amc_period_e_date[]" id="amc_period_e_date_<?php echo $i;?>" class="datepicker" value="<?php echo ConvertSQLDate($myrow2['amc_period_e_date']); ?>"/></td>
<td><input type="text" name="amc_amount[]" id="amc_amount_<?php echo $i;?>"  readonly value="<?php echo $myrow2['amc_amount']; ?>"/></td>
<td><input type="text" name="amc_amount_billed[]" id="amc_amount_billed_<?php echo $i;?>"  readonly value="<?php echo $myrow2['amc_amount_billed']; ?>"/></td>
<td><input type="text" name="amc_additional_amt[]" id="amc_additional_amt_<?php echo $i;?>"  readonly value="<?php echo $myrow2['additional_amt']; ?>"/></td>
<td><input type="text" name="amc_additional_amt_billed[]" id="amc_additional_amt_billed_<?php echo $i;?>"  readonly value="<?php echo $myrow2['additional_amt_billed']; ?>"/></td>
<td><input type="text" name="total_amt[]" id="total_amt_<?php echo $i;?>"  readonly value="<?php echo $myrow2['total_amt']; ?>" onfocus="calamt(<?php echo $i; ?>)"/></td>
<td><input type="text" name="bal_amt[]" id="bal_amt_<?php echo $i;?>"  readonly value="<?php echo $myrow2['bal_amt']; ?>" onfocus="calbal(<?php echo $i; ?>)"/></td>
</tr> 
<?php $i++;}  ?>
<input type="hidden" name="count" id="count" value="<?php echo $i;?>" />
 <?php 
if($_SESSION['manvi']!='')
{
$itemlist=array();
$itemlist=explode('#',$_SESSION['manvi']);

for($j=0,$k=$i;$j<count($itemlist);$j++,$k++)
{				
$yy=explode(",",$itemlist[$j]);
$_SESSION['qutotation_itemcode'][]=$yy[0];	
$itemquery=DB_query("select * from tpl_fixed_asset_definition where id='".$yy[0]."'" , $db);
while($item_row=DB_fetch_array($itemquery))
{
$asset=DB_query("select * from tpl_materialcategorydefinition where id='".$item_row['asset_group']."' ",$db);
$asset1=DB_fetch_array($asset);
$assetgroupt=$asset1['asset_capitalisation'];		
?>  
<tr bgcolor="#ffffff">
<td><input type="checkbox" name="select[]" id="select_<?php echo $k;?>"   onchange="checkdata(<?php echo $k; ?>)"> 	</td>
<td><?php echo $assetgroupt; ?><input type="hidden" name="asset_group[]"  id="asset_group_<?php echo $k;?>" value="<?php echo $item_row['asset_group']; ?>"></td>
<td><?php echo $item_row['asset_code_prefix']; ?><input type="hidden" name="asset_code[]" id="asset_code_<?php echo $k;?>" value="<?php echo $item_row['asset_code_prefix'];?>"/></td>
<td><?php echo $item_row['asset_name'];?><input type="hidden" name="deprec_mst_name[]" id="deprec_mst_name_<?php echo $k;?>" value="<?php echo $item_row['asset_name'];?>"/>	</td>
<td><input type="text" name="amc_period_s_date[]" id="amc_period_s_date_<?php echo $k;?>"  class="datepicker" /></td>
<td><input type="text" name="amc_period_e_date[]" id="amc_period_e_date_<?php echo $k;?>" class="datepicker" /></td>
<td><input type="text" name="amc_amount[]" id="amc_amount_<?php echo $k;?>"  readonly /></td>
<td><input type="text" name="amc_amount_billed[]" id="amc_amount_billed_<?php echo $k;?>"  readonly /></td>
<td><input type="text" name="amc_additional_amt[]" id="amc_additional_amt_<?php echo $k;?>"  readonly/></td>
<td><input type="text" name="amc_additional_amt_billed[]" id="amc_additional_amt_billed_<?php echo $k;?>"  readonly/></td>
<td><input type="text" name="total_amt[]" id="total_amt_<?php echo $k;?>"  readonly onfocus="calamt(<?php echo $k; ?>)"/></td>
<td><input type="text" name="bal_amt[]" id="bal_amt_<?php echo $k;?>"  readonly onfocus="calbal(<?php echo $k; ?>)"/></td>
</tr>
 <?php  } }  }?>
 <input type="hidden" name="counter" value="<?php echo ($i+$j);?>" id="counter" />
 <?php } else {
 if($_SESSION['manvi']!='')
{
$j=1;
$itemlist=array();
$itemlist=explode('#',$_SESSION['manvi']);
for($i=0;$i<count($itemlist);$i++)
	{
$itemquery=DB_query("select * from tpl_fixed_asset_definition where id='".$itemlist[$i]."'" , $db);
while($item_row=DB_fetch_array($itemquery))
	{
		$asset=DB_query("select * from tpl_materialcategorydefinition where id='".$item_row['asset_group']."' ",$db);
		$asset1=DB_fetch_array($asset);
		$assetgroupt=$asset1['asset_capitalisation'];	
?>
<tr bgcolor="#ffffff">
<td><input type="checkbox" name="select[]" id="select_<?php echo $j;?>"   onchange="checkdata(<?php echo $j; ?>)"> 	</td>
<td><?php echo $assetgroupt; ?><input type="hidden" name="asset_group[]"  id="asset_group_<?php echo $j;?>" value="<?php echo $item_row['asset_group']; ?>"></td>
<td><?php echo $item_row['asset_code_prefix']; ?><input type="hidden" name="asset_code[]" id="asset_code_<?php echo $j;?>" value="<?php echo $item_row['asset_code_prefix'];?>"/></td>
<td><?php echo $item_row['asset_name'];?><input type="hidden" name="deprec_mst_name[]" id="deprec_mst_name_<?php echo $j;?>" value="<?php echo $item_row['asset_name'];?>"/>	</td>
<td><input type="text" name="amc_period_s_date[]" id="amc_period_s_date_<?php echo $j;?>"  class="datepicker" /></td>
<td><input type="text" name="amc_period_e_date[]" id="amc_period_e_date_<?php echo $j;?>" class="datepicker" /></td>
<td><input type="text" name="amc_amount[]" id="amc_amount_<?php echo $j;?>"  readonly /></td>
<td><input type="text" name="amc_amount_billed[]" id="amc_amount_billed_<?php echo $j;?>"  readonly /></td>
<td><input type="text" name="amc_additional_amt[]" id="amc_additional_amt_<?php echo $j;?>"  readonly/></td>
<td><input type="text" name="amc_additional_amt_billed[]" id="amc_additional_amt_billed_<?php echo $j;?>"  readonly/></td>
<td><input type="text" name="total_amt[]" id="total_amt_<?php echo $j;?>"  readonly onfocus="calamt(<?php echo $j; ?>)"/></td>
<td><input type="text" name="bal_amt[]" id="bal_amt_<?php echo $j;?>"  readonly onfocus="calbal(<?php echo $j; ?>)"/></td>
</tr> <?php } $j++;}}}?> </table>
</td></tr></table></div></div>	
<p align="center"><input type="submit" name="Save" value="Save" /></p>
</form>
<?php } 
else
{?>
<a href="<?php echo $url_add?>&action=AddNew">Addnew</a>
<table width="100%" border="1" cellpadding="2" cellspacing="2">
  <tr>
    <th>Vendor </th>
    <th>AMC Ref No  </th>  
	<th>AMC Date</th> 
    <th>Project</th>
	<th>Asset Group</th> 
    <th>Edit</th>
    <th>Delete</th>
  </tr>
<?php 
  	$condition="1=1";
   $result= $s->GetRecordById($table,  $condition);
   while($rowdata=DB_fetch_array( $result))
   {	
?>
  <tr>
    <td><?php echo $supp=$s->getField('suppliers','id',$rowdata['vendorid'],'vendor_name'); ?></td>
    <td><?php echo $rowdata['amc_refno'] ?></td>
	 <td><?php echo $rowdata['amc_date'] ?></td>
	  <td><?php echo $rowdata['project'] ?></td>
	  <td><?php echo $asset=$s->getField('tpl_materialcategorydefinition','id',$rowdata['assetgroup'],'asset_capitalisation');?></td>
	  <td><a href="<?php echo $url_add?>&action=Edit&Vid=<?php echo  $rowdata['id'] ?>"><img src="images/icon-edit.png" width="10" height="10"></a></td>
   <td> <a href="<?php echo $url_add?>&action=Delete&Vid=<?php echo  $rowdata['id'] ?>"><img src="images/Icon_Delete.png" width="10" height="10"></a></td>
  </tr>
<?php } ?>  
</table>
 <?php } ?>
<script language="JavaScript">
function open_items()
{
window.open("tpl_amc_entry.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&Vid=<?php echo $_REQUEST['Vid']; ?>&action1=<?php echo $_REQUEST['action']; ?>","item_popup_window","height=500,width=700,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}	
function checkdata(str)
{	
if(document.getElementById("select_"+str).checked)
{	
document.getElementById("amc_amount_"+str).removeAttribute('readOnly');	
document.getElementById("amc_amount_billed_"+str).removeAttribute('readOnly');	
document.getElementById("amc_additional_amt_"+str).removeAttribute('readOnly');	
document.getElementById("amc_additional_amt_billed_"+str).removeAttribute('readOnly');	
document.getElementById("total_amt_"+str).removeAttribute('readOnly');
document.getElementById("bal_amt_"+str).removeAttribute('readOnly');	
}	
else
{
document.getElementById("amc_amount_"+str).setAttribute('readOnly',true);	
document.getElementById("amc_amount_billed_"+str).setAttribute('readOnly',true);	
document.getElementById("amc_additional_amt_"+str).setAttribute('readOnly',true);	
document.getElementById("amc_additional_amt_billed_"+str).setAttribute('readOnly',true);	
document.getElementById("total_amt_"+str).setAttribute('readOnly',true);	
document.getElementById("bal_amt_"+str).setAttribute('readOnly',true);	
}
}
function calamt(k)
{
var billamt=document.getElementById("amc_amount_billed_"+k).value;	
var additionalamt=document.getElementById("amc_additional_amt_"+k).value;	
var amc_billamt=document.getElementById("amc_additional_amt_billed_"+k).value;
if(billamt==''){ billamt=0;}
if(additionalamt==''){ additionalamt=0; }
if(amc_billamt==''){ amc_billamt=0;}

var amt=parseFloat(billamt)+parseFloat(additionalamt)+parseFloat(amc_billamt);
document.getElementById("total_amt_"+k).value=amt.toFixed(2);
}
function calbal(j)
{	
var amtdata=document.getElementById("total_amt_"+j).value;
var amc_amount=document.getElementById("amc_amount_"+j).value;
var balamt=parseFloat(amc_amount)-parseFloat(amtdata);

document.getElementById("bal_amt_"+j).value=balamt.toFixed(2);	
	
	
}

</script>
 
