<?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;

if($_REQUEST['Save'] !='')
{
	if($_REQUEST['log_id'] ==''){
	$data['asset_type'] = $_REQUEST['asset_type'];
	$data['asset_code'] = $_REQUEST['asset_code'];
	$data['log_book_month'] = $_REQUEST['month'];
	$data['log_book_year'] = $_REQUEST['year'];
	$data['created'] = date('Y-m-d');
	$data['created_by'] = $_SESSION['UserID'];
	$data['CompanyId'] = $_SESSION['ACGCompanyId'];
		
	$s->RecordInsert('tpl_fixed_asset_logbook',$data);
	$last_id=DB_Last_Insert_Id($db,"tpl_fixed_asset_logbook","id");
	}else{
	$last_id = $_REQUEST['log_id'];
	
	}
	if($last_id !='')
	{
		$count = count($_REQUEST['start_meter_reading']);
		
		for($cnt=0; $cnt<$count; $cnt++){
		 if($_REQUEST['start_meter_reading'][$cnt] !=''){
			$data1['log_id'] = $last_id;
			$data1['asset_code'] = $_REQUEST['asset_code'];
			$data1['log_book_date'] = FormatDateForSQL($_REQUEST['log_book_date'][$cnt]);
			$data1['start_meter_reading'] = $_REQUEST['start_meter_reading'][$cnt];
			$data1['end_meter_reading'] = $_REQUEST['end_meter_reading'][$cnt];
			$data1['total_reading'] = $_REQUEST['total_reading'][$cnt];
			$data1['oil_consumption'] = $_REQUEST['oil_consumption'][$cnt];
			$data1['uom'] = $_REQUEST['uom'][$cnt];
			$data1['site'] = $_REQUEST['site'][$cnt];
			
			$s->RecordInsert('tpl_fixed_asset_logbook_details',$data1);
			
		 }
		}
	}
	$_SESSION['success_msg']="Saved Successfully";
	echo "<script>location.href='$url_cancel'</script>";
}
$monthdata=array("01"=>"January","02"=>"February","03"=>"March","04"=>"April","05"=>"May"
	,"06"=>"June","07"=>"July","08"=>"August","09"=>"September","10"=>"October"
	,"11"=>"November","12"=>"December");

?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
 <div class="tab_div">
  <h1>Fixed Asset Log Book Entry</h1>
  <form name="" id="" method="POST">
	<table width="100%" cellspacing="0" cellpadding="0" border="0" class="small">
	  <tr class="row1">
		<td style="padding:5px; text-align:left;" colspan="8" align="center">
			<a href="<?php echo $url_add;?>"></a>	  </tr>
	  <tr class="row1">
	  	<td width="7%"><strong>Group</strong></td>
		<td width="18%">
			<select name="under_group" id="under_group">
				 <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['under_group']==$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="11%"><strong>Asset Name<font style="color:#FF0000;">*</font></strong></td>
		<td width="16%">
			<select name="asset_name" id="asset_name">
				<option value="0">--Select--</option>
			<?php
				$sqlAssets = "SELECT id, name, code 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['name']." (".$assetRow['code'].")";?></option>
			<?php } ?>
			</select>
		</td>
		<td width="9%"><strong>Month<font style="color:#FF0000;">*</font></strong></td>
		<td width="16%">
			<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 width="7%"><strong>Year<font style="color:#FF0000;">*</font></strong></td>
		<td width="16%">
			<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 class="tabledata1" colspan="8" style="text-align:center;">
		<input type="submit" name="Search" class="search" value="Proceed" onclick="return validate();" />	</td>
	  </tr>

	  <tr class="row1"><td colspan="8">&nbsp;</td></tr>
	  
	  <tr class="row1">
		<td colspan="8">
<table width="100%" border="1" cellspacing="1" cellpadding="5" align="center">
	<tr>
		<th><strong>Date</strong></th>
		<th><strong>Start Meter Reading<font style="color:#FF0000;">*</font></strong></th>
		<th><strong>End Meter Reading<font style="color:#FF0000;">*</font></strong></th>
		<th><strong>Total Reading</strong></th>
		<th><strong>Oil Consumption<font style="color:#FF0000;">*</font></strong></strong></th>
		<th><strong>UOM</strong></th>
		<th><strong>Site</strong></th>
   </tr>
   
<?php 

if($_REQUEST['Search'] !=''){
    $month = $_REQUEST['month'];
    $year = $_REQUEST['year'];
    $filter = array();
    if(isset($_REQUEST['under_group']) && $_REQUEST['under_group']!= '0')	
    {
    $filter[]="under_group='".$_REQUEST['under_group']."'"; 
    }
    if(isset($_REQUEST['asset_name']) && $_REQUEST['asset_name']!='0'){
    $filter[]="id='".$_REQUEST['asset_name']."'";}
    if($_REQUEST['month']!='' && $_REQUEST['month']!= '0'){
    $filter[]="MONTH(manchine_opening_date)='".$_REQUEST['month']."'";}
    if($_REQUEST['year']!='' && $_REQUEST['year']!= '0'){
    $filter[]="YEAR(manchine_opening_date)='".$_REQUEST['year']."'";}
    if(!empty($filter))
	{			
	$filter1 = implode(" and ",$filter);
       $sql1 = "SELECT * FROM machine_center WHERE CompanyId='".$_SESSION['ACGCompanyId']."' AND $filter1 ORDER BY id desc";
        $res1 = DB_query($sql1,$db);
	}
 
$error_msg = "";
if(DB_num_rows($res1)>0){
 $myrow=DB_fetch_array($res1);
 $day1=explode("-",$myrow['manchine_opening_date']);
 $day=$day1[2];
 $num = cal_days_in_month(CAL_GREGORIAN, $day1[1], $day1[0]);
 //echo $nn=$num-$day1;
for($day;$day<=$num;$day++)
{
 

?>
   <tr class="row1">
	<td><input type="text" name="log_book_date[]" id="log_book_date_<?php echo $k;?>" value="<?php echo ConvertSQLDate($myrow['manchine_opening_date']);?>" readonly="" /></td>
	<td><input type="text" name="start_meter_reading[]" id="start_meter_reading_<?php echo $k;?>" value="<?php echo $myrow['average_production_qty'];?>" onfocus="setStartReading('<?php echo $k;?>')" readonly /></td>
	<td><input type="text" name="end_meter_reading[]" id="end_meter_reading_<?php echo $k;?>" value="" /></td>
	<td><input type="text" name="total_reading[]" id="total_reading_<?php echo $k;?>" value="" readonly="" onfocus="calTotal('<?php echo $k;?>');" /></td>
	<td><input type="text" name="oil_consumption[]" id="oil_consumption_<?php echo $k;?>" value="" /></td>
	<td><input type="text" name="uom[]" id="uom_<?php echo $k;?>" value="" readonly="" /></td>
	<td><select name="site[]" id="site_<?php echo $k;?>">
		<?php
			$sitesql = "SELECT loccode,locationname FROM locations WHERE CompanyId='".$_SESSION['ACGCompanyId']."'";
			$res = DB_query($sitesql,$db);
			while($siteRow = DB_fetch_array($res)){?>
		<option value="<?php echo $siteRow['loccode'];?>" <?php echo ($siteRow['loccode']==$site) ? "selected" : "";?>>
			<?php echo $siteRow['locationname'];?>
		</option>
		<?php }?>
		</select>	
	</td>
  </tr>
  	<input type="hidden" name="log_id" value="<?php echo $log_id;?>" />
  
</table>
		  </td>
		</tr>
		<tr>
		  <td colspan="8">&nbsp;</td>
		</tr>
		
		<?php }?>
                <tr class="row1">
		  <td colspan="8" style="text-align:center" align="center">
		  	<input type="submit" name="Save" value="Save" id="Save" class="search" onclick="return validateReading();" />
			<input type="hidden" name="startDate" id="startDate" value="<?php echo $startDate;?>" />
			<input type="hidden" name="endDate" id="endDate" value="<?php echo $endDate;?>" />			
		</td>
		</tr>
                    <?php  }else{  ?>
  <tr><td colspan="7"><font color="#990000"><strong><?php echo $error_msg;?></strong></font></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']);
?>

<script type="text/javascript">
function validate(){

	if(document.getElementById("asset_code").value==0)
	{
		alert("Select Asset Name");
		document.getElementById("asset_code").focus();
		return false;
	}
	if(document.getElementById("month").value==0)
	{
		alert("Select Month");
		document.getElementById("month").focus();
		return false;
	}
	if(document.getElementById("year").value==0)
	{
		alert("Select Year");
		document.getElementById("year").focus();
		return false;
	}
}

function validateReading(){

	var start = document.getElementById("startDate").value;
	var end = document.getElementById("endDate").value;
	//alert(start+" = > "+end);
	for(var i=start; i<=end; i++){
	
		if(document.getElementById("start_meter_reading_"+i).value==''){
			alert("Start Meter Reading Can Not Be Empty");
			document.getElementById("start_meter_reading_"+i).focus();
			return false;
		}
		if(document.getElementById("end_meter_reading_"+i).value==''){
			alert("End Meter Reading Can Not Be Empty");
			document.getElementById("end_meter_reading_"+i).focus();
			return false;
		}
		if(document.getElementById("total_reading_"+i).value==''){
			alert("Total Reading Can Not Be Empty");
			document.getElementById("total_reading_"+i).focus();
			return false;
		}
		
		if(document.getElementById("oil_consumption_"+i).value==''){
			alert("Start Meter Reading Can Not Be Empty");
			document.getElementById("oil_consumption_"+i).focus();
			return false;
		}else if(document.getElementById("total_reading_"+i).value >0 && document.getElementById("oil_consumption_"+i).value==0 ){
			alert("Enter Valid Oil Consumption Value");
			document.getElementById("oil_consumption_"+i).focus();
			return false;	
		}
	}
}

function calTotal(id){

	var startReading = parseInt(document.getElementById("start_meter_reading_"+id).value);
	var endReading = parseInt(document.getElementById("end_meter_reading_"+id).value);
	if(isNormalInteger(endReading) && endReading !=''){
	if(startReading>endReading){
		document.getElementById("end_meter_reading_"+id).value='';
		document.getElementById("total_reading_"+id).value = '';
		document.getElementById("end_meter_reading_"+id).focus();
		alert("End Meter Reading Can Not Be Less than Start Meter Reading");
	}else{
		var total = endReading-startReading;
		document.getElementById("total_reading_"+id).value = total;
		if(total == 0){
			document.getElementById("oil_consumption_"+id).value = total;
			document.getElementById("oil_consumption_"+id).readOnly = true;
		}
	}
	}else{
		document.getElementById("end_meter_reading_"+id).value='';
		document.getElementById("total_reading_"+id).value = '';
		document.getElementById("end_meter_reading_"+id).focus();
		alert("Enter only numbers");
	}
}

function setStartReading(id){

	var start = document.getElementById("startDate").value;
	if(id==start){
		return;
	}else{
		var lastid = parseInt(id)-1; 
		document.getElementById("start_meter_reading_"+id).value=document.getElementById("end_meter_reading_"+lastid).value;
	}

}

function isNormalInteger(str) {
    return /^\+?(0|[1-9]\d*)$/.test(str);
}
</script>
