<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Preventive Maintenance Chart</h1>
<form name="" id="" method="POST">
<table width="100%" cellpadding="3" cellspacing="0" border="0">
<tr class="row1">
  <td width="13%" style="text-align:right;"><strong>Branch:</strong></td>         
    <td width="20%"><select name="site" id="site" style="width:98%;">
          <option value="0">Select Branch</option>
          <?php
        $query4=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
      while($row4=DB_fetch_array($query4))
        {
          $s='';
          if($_REQUEST['site']==$row4['loccode'])
          $s="selected";
          ?>
          <option value=<?php echo $row4['loccode'];?> <?php echo $s;?>><?php echo $row4['locationname'];?></option>
        <?php }
      ?>
          </select>
      </td>
      <td width="13%" style="text-align:right;"><strong>Year:</strong></td>
      <td width="20%"><select name="year" id="year" style="width:98%;">
        <option value="0">--Select--</option>
      <?php        
        for($j=date('Y'); $j>=1990; $j--){?>
        <option value="<?php echo $j;?>" <?php echo ($_REQUEST['year']== $j ) ? "selected" : "";?>><?php echo $j;?></option>
      <?php } ?>
       </select></td>
    <td width="13%" style="text-align:right;"><strong>Month:</strong></td>
    <td><select name="month" id="month" style="width:98%;">
        <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>
</tr>

<tr class="row1">
  <td style="text-align:right;"><strong>Asset Group:</strong></td>
    <td><select name="asset_group" id="asset_group" style="width:98%;" onclick="get_asset_name(this.value);">
         <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_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 style="text-align:right;"><strong>Asset Name:</strong></td>
    <td><select name="asset_name" id="asset_name" style="width:98%;">
        <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 colspan="2">&nbsp;</td>            
</tr>
<tr class="row1">
  <td colspan="6" style="text-align:center;">
  <input type="submit" name="Search" class="search" value="Search"  onclick="return validate()" /></td>
</tr>
</table>
<?php if(isset($_POST['Search']))
{       $month = $_REQUEST['month'];
        $year = $_REQUEST['year'];
      
  $filter = array();
  
  if(isset($_REQUEST['site']) && $_REQUEST['site']!= '0') 
  $filter[]="location='".$_REQUEST['site']."'";
        
      //  if(isset($_REQUEST['year']) && $_REQUEST['year']!= '0') 
     // $filter[]="YEAR(date)='".$_REQUEST['year']."'";
        
      //  if(isset($_REQUEST['month']) && $_REQUEST['month']!= '0') 
  //$filter[]="MONTH(date)='".$_REQUEST['month']."'";
        
  if(isset($_REQUEST['asset_group']) && $_REQUEST['asset_group']!= '0') 
  $filter[]="under_group='".$_REQUEST['asset_group']."'";
        
  if(isset($_REQUEST['asset_name']) && $_REQUEST['asset_name']!= '0') 
  $filter[]="id='".$_REQUEST['asset_name']."'";
  
  
  //if($_REQUEST['year']!='') 
  //$filter[]="date='".FormatDateFORSQL($_REQUEST['frmdate'])."'";
   
  //if(isset($_REQUEST['slip_no']) && $_REQUEST['slip_no']!='0')
  //$filter[]="R.slip_no='".$_REQUEST['slip_no']."'";
  
  if($filter!='')
  {     
  $filter1 = implode(" and ",$filter);  
         $sqlq = "SELECT * FROM machine_center  WHERE CompanyId='".$_SESSION['ACGCompanyId']."' AND $filter1";
  }
   else
   {
    $sqlq = "SELECT *  FROM machine_center   WHERE CompanyId='".$_SESSION['ACGCompanyId']."'";
   }
     
  $_SESSION['prevent_maintence'] = $sqlq;
  $sql=DB_query($sqlq,$db);
  $s=new db_query();  
?>
<p align="right"><a href="preventive_maintenance_report_print.php?site=<?php echo $_REQUEST['site'];?>&year=<?php echo $_REQUEST['year'];?>&month=<?php echo $_REQUEST['month'];?>&asset_group=<?php echo $_REQUEST['asset_group'];?>&asset_name=<?php echo $_REQUEST['asset_name'];?>" target="_blank" class="search">Print</a></p>

<?php
if($_REQUEST['year']!='0')
{

$i=1;
 $num=DB_num_rows($sql);
if($num>0)
{
  $serve_dt1='';
while($row=DB_fetch_array($sql))
  {
   $reqDet_sql = "SELECT B.service_days AS service_time,B.asset_id AS assetid,B.sub_break_down_id AS sub_break_down_id ,B.last_service_date as service_date
      ,A.asset_group as under_group,C.break_down_id AS breakdown FROM tbl_prementive_sub_details B
       inner join tbl_prementive_details C on C.id=B.tbl_prementive_details_id
       inner join tbl_prementive A on A.id=C.tbl_prementive_id
       where A.asset_id='".$row['id']."' group by B.sub_break_down_id ";
     $result = DB_query($reqDet_sql,$db);     
     if(DB_num_rows($result)>0)
     {
      $row_details=DB_fetch_array($result);  
       $service_period=$row_details['service_time'];    
         $serve_dt=$row_details['service_date'];
     }
     else
     {
       $serve_dt='';
      // $row['service_date'];
      $service_period='';
      //$row['service_time'];
     }
  
  $data1=date('Y-m-d');
 // $n=explode('-',$serve_dt);
  $var=date('n',strtotime($serve_dt));

  $startyear=$y = date('Y',strtotime($row['date']));
  $startyear1=$y = date('Y',strtotime($data1));
  $diff=$startyear1-$startyear;
  if($_REQUEST['year']!='' && $_REQUEST['year']!='0' )
  {
  $endyear=$year; 
  $startyear=$year;
  }
  else
  {
  $endyear=date('Y',strtotime(date('Y-m-d'))); 
  $startyear=date('Y',strtotime($row['date']));
  }
  if($_REQUEST['month']!='' && $_REQUEST['month']!='0' )
  {
  $month=$_REQUEST['month']; 
  $month12=$_REQUEST['month'];
  }
  else
  {
  $month=1; 
  $month12=12;
  }  

  $_SESSION['year']=$_REQUEST['year'];
  $_SESSION['month']=$_REQUEST['month'];  
  
       $date1=date('Y-m-d');
       $ts1 = strtotime($date1);
       $ts2 = strtotime($serve_dt);

       $seconds_diff = $ts1 - $ts2;

        $yearday=floor($seconds_diff/3600/24);
        if($service_period!='')
        $daycount=  round($yearday/$service_period); 
        else
       $daycount=  0;  
        
    for($i=$startyear;$i<=($endyear);$i++)
    {     
     $d1='';     
    
         for($k1=1;$k1<=($daycount);$k1++)
        {          
       for($j=$month; $j<=$month12; $j++)
      {     
          $d1=date('Y-m-d', strtotime($serve_dt. ' + '.$service_period*$k1.' days'));   

          if(date('m',strtotime($d1))==$j && date('Y',strtotime($d1))==$i)               
          $serve_dt1[$row['id']][$i][$j][]= $d1;    
        }            
     }     
    }
   $serve_dt11[]=$row['id'];  
   }   
   ?>

  <table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#808080 1px solid; border-collapse:collapse;">
<tr>
    <th width="15%" style="text-align:center;" valign="top">Asset Name</th>
    <th colspan="12" valign="top" style="text-align:center;">Months <?php echo $_REQUEST['year']; ?></th>
    </tr>
  <tr>
    <th align="left" valign="top"></th>
    <?php for($u=$month; $u<=$month12; $u++){?>
    <th align="left" valign="top" style="text-align:center;"><?php echo date('F', mktime(0, 0, 0, $u));?></th>
    <?php } ?>
    </tr>

<?php
   
     for($i=$startyear;$i<=($endyear);$i++)
     {   
     for($t=0;$t<=count($serve_dt11);$t++)
    {   
    if(!empty($serve_dt1[$serve_dt11[$t]][$i]))
    {  
   ?>
  <tr bgcolor="#FFFFFF">
    <td align="left" valign="top"><?php echo $s->getField('machine_center','id',$serve_dt11[$t],'name'); ?></td>
    <?php for($j=$month; $j<=$month12; $j++){?>
    <td align="left" valign="top" style="text-align:center; padding:0;">
    <div id="amc-tab">
    <table width="100%" cellspacing="0" cellpadding="3" border="0">
    <?php     if(!empty($serve_dt1)!='') 
                {
                if($serve_dt1[$serve_dt11[$t]][$i][$j][0]!='')
                {
                 foreach($serve_dt1[$serve_dt11[$t]][$i][$j] as $key=>$value)
                 { ?>
               <tr>
               <td><?php echo  ConvertSQLDate($value); ?></td>
               </tr>
               <?php     
                 }
                 }
                }
                ?>
                </table>
                </div></td><?php } ?>
               </tr>  
    <?php } } ?>
    <?php   } ?>
  </table>  
    
  <?php } else { ?>
  <p align="center"><font color="#FF0000" style="font-weight:bold; font-size:14px;"><?php echo "No Records Found"; ?></font></p> 

<?php } }  else { 
$i=1;
$num=DB_num_rows($sql);
if($num>0)
{
while($row=DB_fetch_array($sql))
  {

    $reqDet_sql = "SELECT B.service_days AS service_time,B.asset_id AS assetid,B.sub_break_down_id AS sub_break_down_id ,B.last_service_date as service_date ,A.asset_group as under_group,C.break_down_id AS breakdown FROM tbl_prementive_sub_details B,tbl_prementive A,tbl_prementive_details C    where A.asset_id='".$row['id']."' group by B.sub_break_down_id ";
     $result = DB_query($reqDet_sql,$db);     
     if(DB_num_rows($result)>0)
     {
      $row_details=DB_fetch_array($result);  
       $service_period=$row_details['service_time'];    
         $serve_dt=$row_details['service_date'];
     }
     else
     {
       $serve_dt=$row['service_date'];
      $service_period=$row['service_time'];
     }
 //  $serve_dt=$row['date'];
   $data1=date('Y-m-d');
 // $n=explode('-',$serve_dt);
  $var=date('n',strtotime($serve_dt));
 // $service_period=$row['service_time'];
  $startyear=$y = date('Y',strtotime($row['date']));
  $startyear1=$y = date('Y',strtotime($data1));
  $diff=$startyear1-$startyear;
  if($_REQUEST['year']!='' && $_REQUEST['year']!='0' )
  {
  $endyear=$year; 
  $startyear=$year;
  }
  else
  {
  $endyear=date('Y',strtotime(date('Y-m-d'))); 
  $startyear=date('Y',strtotime($row['date']));
  }
  if($_REQUEST['month']!='' && $_REQUEST['month']!='0' )
  {
  $month=$_REQUEST['month']; 
  $month12=$_REQUEST['month'];
  }
  else
  {
  $month=1; 
  $month12=12;
  }  
       $date1=date('Y-m-d');
       $ts1 = strtotime($date1);
       $ts2 = strtotime($serve_dt);

       $seconds_diff = $ts1 - $ts2;

        $yearday=floor($seconds_diff/3600/24);
        if($service_period!='')
        $daycount=  round($yearday/$service_period); 
      else
        $daycount=0;
?>

  <table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#808080 1px solid; border-collapse:collapse;">
     <tr>
     <th colspan="14" align="left" style="text-align:left;"><b><?php echo $row['name']; ?></b>
      &nbsp;<img src="images/orange-right-arrow-button.png" height="20" width="20" />&nbsp;<b><?php echo $row['code']; ?></b></th>
    </tr>
    <tr>   
    <th width="10%"  style="text-align:center;" valign="top"><strong>Years</strong></th>
    <th width="10%"  style="text-align:center;" valign="top">Sub Break Down</th>  
    <th colspan="12" valign="top" style="text-align:center;"><strong>Months</strong></th>
    </tr>
    <tr bgcolor="#fff">
    <td width="10%" align="left" valign="top"></td>
    <td width="10%" align="left" valign="top"></td>
    <?php for($i=$month; $i<=$month12; $i++){?>
    <td align="left" valign="top"><strong><?php echo date('F', mktime(0, 0, 0, $i));?></strong>
   </td>
    <?php } ?>
    </tr>
    <?php for($i=$startyear;$i<=($endyear);$i++)
    {     
     $d1='';
     $serve_dt1='';
         for($k1=1;$k1<=($daycount);$k1++)
        {          
       for($j=$month; $j<=$month12; $j++)
      {       
          $d1=date('Y-m-d', strtotime($serve_dt. ' + '.$service_period*$k1.' days'));    
                                          if(date('m',strtotime($d1))==$j && date('Y',strtotime($d1))==$i)               
                                        $serve_dt1[$i][$row['id']][$j][]= $d1;       
        }    
     }
     ?>   
  <tr bgcolor="#FFFFFF">    
    <td align="left" valign="top"><b><?php echo $i; ?></b></td>
     <td align="left" valign="top"><b></b></td>
    <?php for($j=$month; $j<=$month12; $j++){?>
                <td align="left" valign="top">
    <?php     if(!empty($serve_dt1)!='') 
                {
                if($serve_dt1[$i][$row['id']][$j][0]!='')
                echo ConvertSQLDate($serve_dt1[$i][$row['id']][$j][0]);         
                }
                ?></td>
      <?php } ?>
  </tr>  
    <?php } ?>
</table>
   <?php $i++; }} else { ?>
  <p style="text-align: center;"><font color="#FF0000" style="font-weight:bold; font-size:14px;"><?php echo "No Records Found"; ?></font></p>  
<?php }  } }?> 

</div>
</div>
<script>
function validate()
{
  if(document.getElementById("site").value=='0')
   {
    alert("Please Select Site");
    return false;
   }
   if(document.getElementById("year").value=='0')
   {
    alert("Please Select Year ");
    return false;
   }
   return true;

}

function get_asset_name(str)  
{ 
  
if(str=="")
  {
  document.getElementById("asset_name").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {     
       document.getElementById("asset_name").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_asset_name.php?group="+str,true);
xmlhttp.send();
} 


</script>