<?php 
$url_cancel = "index.php?route=ToolPlants/fixed_assets/fixed_assets_log_book_entry_index".$rurl;
$url_add = "index.php?route=ToolPlants/fixed_assets/fixed_assets_log_book_entry".$rurl;
$url_edit = "index.php?route=ToolPlants/fixed_assets/fixed_assets_log_book_entry_edit".$rurl;
?>
<?php
	if($_REQUEST['action']=='delete' && !empty($_REQUEST['lid']))
	{
		if($s->DeleteRecord('tpl_fixed_asset_logbook',"id=".$_REQUEST['lid']))
		{
			$s->DeleteRecord('tpl_fixed_asset_logbook_details',"log_id=".$_REQUEST['lid']);				
			$_SESSION['success_msg']="Delete Successfully";		
		}
	}
	
	if($_REQUEST['action']=='delDetails' && !empty($_REQUEST['id']))
	{
			$s->DeleteRecord('tpl_fixed_asset_logbook_details',"id=".$_REQUEST['id']);				
			$_SESSION['success_msg']="Delete Successfully";		
	}
if($_REQUEST['Cancel']!='')
{
	unset($_REQUEST);
}

?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
 <div class="tab_div">
  <h1><?php echo $s->getField('modules_form','id',$_REQUEST['frmid'],'description');?></h1>
  <form name="" id="" method="POST">
  <p align="left"><a href="<?php echo $url_add;?>"><input type="button" name="AddNew" class="search" value="Add Asset Log" /></a></p>
	<table width="100%" cellspacing="0" cellpadding="3" border="0">
	 	  <tr class="row1">
	  	<td width="13%" align="right">Group</td>
		<td width="35%">
			<select name="asset_type" id="asset_type" >
				<option value="0">Select Asset Group</option>
          <?php
		  $asset_type=DB_query("Select * from machine_group_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
			while($asset_type_res=DB_fetch_array($asset_type))
				{
					$se='';
					if($_REQUEST['asset_type']==$asset_type_res['id'])
					$se="selected";
					?>
					<option value=<?php echo $asset_type_res['id'];?> <?php echo $se;?>><?php echo $asset_type_res['machine_gp_location'];?></option>
				<?php }
	 ?>
			</select>		</td>
		<td width="13%" align="right">Asset Name</td>
		<td><select name="asset_name" id="asset_name" >
          <option value="0">--Select--</option>
          <?php
		$sqlAssets = "SELECT id, name as asset_name, code as asset_code_prefix from machine_center WHERE CompanyId='".$_SESSION['ACGCompanyId']."'";
		$res = DB_query($sqlAssets,$db);
		while($assetRow = DB_fetch_array($res)){?>
          <option value="<?php echo $assetRow['id'];?>" <?php echo ($_REQUEST['asset_name']== $assetRow['id'] ) ? "selected" : "";?>><?php echo $assetRow['asset_name']." (".$assetRow['asset_code_prefix'].")";?></option>
          <?php } ?>
        </select></td>
        </tr>
        <tr class="row1">
		<td align="right">Month</td>
		<td>
		  <select name="month" id="month" >
				<option value="0">--Select--</option>
			<?php
				
				for($i=1; $i<=12; $i++){?>
				<option value="<?php echo $i;?>" <?php echo ($_REQUEST['month']== $i ) ? "selected" : "";?>><?php echo date('F', mktime(0, 0, 0, $i));?></option>
			<?php } ?>
			</select>		</td>
		<td align="right">Year</td>
		<td>
		  <select name="year" id="year" >
				<option value="0">--Select--</option>
			<?php
				
				for($j=date('Y'); $j>=2000; $j--){?>
				<option value="<?php echo $j;?>" <?php echo ($_REQUEST['year']== $j ) ? "selected" : "";?>><?php echo $j;?></option>
			<?php } ?>
			</select></td>
	  </tr>
	  <tr class="row1">
		<td colspan="4" style="text-align:center;">
		<input type="submit" name="Search" class="search" value="Search" /></td>
	  </tr>
	</table>
    <br />
	  
	  <?php 
	  if($_REQUEST['Search'] !=''){?>
	 
			<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
			<tr>
				<th width="3%">S. No.</th>
				<th width="7%">Date</th>
				<th width="10%">Start Meter Reading</th>
			    <th width="10%">End Meter Reading</th>
				<th width="8%">Total Reading</th>
				<th width="10%">Reading unit</th>
			    <th width="10%">Fuel Consumption</th>
			    <th width="8%">Fuel Unit</th>
			    <th width="8%">Fuel Type</th>
			    <th>Working Branch</th>
			    <th width="6%">Action</th>
			</tr>
<?php 
	$filter = array();
	
	if($_REQUEST['month']!='' and $_REQUEST['month']!= 0) 
	 $filter[]="L.log_book_month='".$_REQUEST['month']."'";
	 
	 if($_REQUEST['year']!='' and $_REQUEST['year']!= 0) 
	 $filter[]="L.log_book_year='".$_REQUEST['year']."'";
	 
	if(isset($_REQUEST['asset_name']) && $_REQUEST['asset_name']!='0')
	$filter[]="L.asset_code='".$_REQUEST['asset_name']."'";
	
	if(isset($_REQUEST['asset_type']) && $_REQUEST['asset_type']!='0')
	$filter[]="L.asset_type='".$_REQUEST['asset_type']."'";
	
	if($filter!='')
	{			
	$filter1 = implode(" and ",$filter);	
	}
	if($filter1=='')
	{
	$filter1 ="1=1";
	}
	
	$sql= "select L.id as lid, A.name, L.log_book_month, L.log_book_year, L.asset_type, L.asset_code 
		   from tpl_fixed_asset_logbook L
		   INNER JOIN machine_center A ON A.id=L.asset_code
		   WHERE L.CompanyID='".$_SESSION['ACGCompanyId']."' and
		   L.log_book_year Between year('".$_SESSION['financialYrStart']."') and year('".$_SESSION['financialYrEnd']."')
		     and  $filter1";
			   
	//echo $sql;		   
		   
	$record=DB_query($sql,$db);
	$numrows1 = DB_num_rows($record);
	if($numrows1>0){
	$i=0;
	while($row=DB_fetch_array($record))
	{
		$delEntry = 0;
		$i++;
		extract($row);
		$year1 = $log_book_year;
		$month1 = $log_book_month;
		if($month1==12)
		{
			$month1 = 1;
			$year1 = (int)$year1+1;
		}else{
			$month1 = $month1+1;
		} 
		  $sqchk = "SELECT id FROM tpl_fixed_asset_logbook WHERE asset_code='".$asset_code."' and log_book_month='".$month1."' and log_book_year='".$year1."'";
		
		$res = DB_query($sqchk,$db);
		if(DB_num_rows($res)>0){
			$delEntry = 1;
		}
		//echo $i."<br>";
		//echo $numrows1;

	?>
	   <tr>
	   		<td colspan="10"><strong>Log Book Entry For <?php echo $name;?> For <?php echo date('F', mktime(0, 0, 0, $log_book_month)).", ".$log_book_year;?></strong></td>
			<td style="text-align:center;">
			<?php if($i==$numrows1 && $_REQUEST['asset_name']!='0' && $delEntry==0){?>
			<a href="<?php echo $url_edit;?>&action=edit&lid=<?php echo $lid;?>">
				<img src="images/icon-edit.png" title="Edit" /></a> | <a href="<?php echo $url_cancel;?>&action=delete&lid=<?php echo $lid;?>">
			<img src="images/Icon_Delete.png" title="Delete" onClick="return confirm('Are you sure you wish to delete this Record?')" /></a>
			<?php }?></td>	
	   </tr>
	   <tr>
<?php  $sql1= "select * from tpl_fixed_asset_logbook_details WHERE log_id='".$lid."' order by id";
		   
	$record1=DB_query($sql1,$db);
	$numrows2 = DB_num_rows($record1);
	if($numrows2>0){
	$j=0;
	while($row1=DB_fetch_array($record1))
	{
	$j++;
	extract($row1);
	?>
	   <tr>
			<td><?php echo $j;?></td>
			<td><?php if($log_book_date!='') echo date('d/m/Y',strtotime($log_book_date)); else echo ""; ?> </td>
			<td><?php echo $start_meter_reading;?></td>
			<td><?php echo $end_meter_reading;?></td>
			<td><?php echo $total_reading;?></td>
			<td><?php echo $reading_unit;?></td>
			<td><?php echo $oil_consumption;?></td>
			<td><?php echo $fuel_unit;?></td>
			<td><?php echo $fuel_type;?></td>
			<td><?php echo $site;?></td>
			<td style="text-align:center;">
			<?php if($i==$numrows1 && $j==$numrows2 && $_REQUEST['asset_name']!='0' && $delEntry==0){?>
				<a href="<?php echo $url_cancel;?>&action=delDetails&id=<?php echo $id;?>">
			<img src="images/Icon_Delete.png" title="Delete" onClick="return confirm('Are you sure you wish to delete this Record?')" /></a>
			<?php }?></td>
	  </tr><?php }?> 
		<?php }else{?>
		<tr>
		  <td style="text-align:center;" colspan="11">No Record Found</td>
		</tr>
		<?php }?>
			
	<?php }
	}else{?>
		<tr>
		  <td style="text-align:center;" colspan="11">No Record Found</td>
		</tr>
		<?php }	
		}
	?>
	</table>
	 
  </form>
 </div>
</div>

<?php if(isset($_SESSION['success_msg']))
{ ?>
	<script> alert("<?php echo ($_SESSION['success_msg']);?>");</script>
<?php 
}	unset($_SESSION['success_msg']);
?>


