<?php 
error_reporting(0);
$monthdata=array("01"=>"Jan","02"=>"Feb","03"=>"Mar","04"=>"Apr","05"=>"May","06"=>"Jun","07"=>"July","08"=>"Aug","09"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Dec");
$year1=date('Y');
$pri_year=2000;
?>
<div style="display:none" id="div_id"></div>
<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'],'frm_name'); ?></h1>
<form method="post" action="">      
      <table width="100%" align="center" border="0" cellspacing="0" cellpadding="3">
          <tr class="row1">
            <td width="13%" align="right">Branch</td>
            <td width="35%"><select name="site" id="site" > 
                <option value="">Select</option>
                    <?php  $ss1="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
                           $site_sql=DB_query($ss1,$db);
                           while($site_row=DB_fetch_array($site_sql))
                           {
                              $p='';
                              if($site_row['loccode'] == $_POST['site'])  
                                  $p="selected";?>
                              <option value="<?php echo $site_row['loccode']; ?>" <?php echo $p;?>><?php echo $site_row['locationname']; ?></option>
                     <?php } ?>
               </select></td>
            <td width="13%" align="right">Asset</td>
            <td><select name="item" id="item" >
                <option value="0">All</option>
                  <?php $SQLEmp=DB_query("SELECT * FROM  machine_center",$db);
                   while($EmpId=DB_fetch_array($SQLEmp))
                  { ?>
                    <option value="<?php echo $EmpId['id']; ?>"<?php if($EmpId['id'] == $_POST['item']) { echo 'selected="selected"'; } ?>><?php echo $EmpId['name']; ?></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 foreach($monthdata as $key=>$value)
            {
            ?>
            <option value="<?php echo $key; ?>" <?php if($_REQUEST['month']==$key) { ?> selected<?php } ?>><?php echo $value; ?></option>
            <?php
            }
            ?>
           </select></td>
           <td align="right">Year</td>
            <td><select name="year" id="year" >
              <option value="0">Select</option>
              <?php
               for($i=2020;$i>=$pri_year;$i--)
               {
              ?>
              <option value="<?php echo $i; ?>" <?php if($_REQUEST['year']==$i) { ?> selected <?php } ?>><?php echo $i; ?></option>
              <?php } ?>
           </select></td>
        </tr>
        <tr class="row1">
          <td align="center" style="text-align:center;" colspan="4"><input type="submit" name="view" value="Search" class="search" ></td>
        </tr>
  </table>
</form>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
<tr>
  <th width="4%">S. No.</th>
  <th width="10%">Asset Code</th>
  <th>Asset Name</th>
  <?php 
   $title='';
   $title1=DB_query("select * from tbl_title_master order by serial_no ASC limit 0,3",$db);
   while($r=DB_fetch_array($title1))
   { 
    $title[]=$r['db_field_name'];
    ?>
    <th width="9%"><?php echo $r['title_name'];?></th>
    <?php } ?>
  <th width="10%">Break Down</th>
  <th width="10%">Sub Break Down Type</th>
  <th width="10%">Maintenance Date</th>
  <th width="7%" style="text-align:center;">Action</th>
</tr>
  <?php 
   if(isset($_POST['view']))
   {
     $filter=array();
    

     if($_REQUEST['site']!='0')
     $filter[]="A.site='".$_REQUEST['site']."'";
     if($_REQUEST['item']!='0')
     $filter[]="A.asset_id='".$_REQUEST['item']."'";
   //print_r($filter);
      if(!empty($filter))
     $filter1=implode(" and ",$filter);
     else
     $filter1="1=1"; 
     $i=1;
     $servicedays=0;
    
     $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=B.tbl_prementive_id    where ".$filter1." group by B.sub_break_down_id ";

          $result = DB_query($reqDet_sql,$db);     
    if(DB_num_rows($result)>0)
    {      
          while($assetRow = DB_fetch_array($result))
           {
            $day1='';
            $assetRow['date']=$s->getField('machine_center','id',$assetRow['assetid'],'date');
            $maintenance_date1='';   $servicedays='';$day1='';

            $assetgroup=$s->getField('machine_group_master','id',$assetRow['under_group'],'machine_gp_location');     
             
             //echo "hiq";
            $tpl_check=DB_query("SELECT A.maintance_date AS service_date FROM tpl_breakdown_mgmt A INNER JOIN tpl_breakdown_mgmt_machine B ON A.id=B.master_id INNER JOIN tpl_breakdown_mgmt_machine_breakdown C 
                 ON B.id=C.parent_id where C.sub_breakdown_id='".$assetRow['sub_break_down_id']."' and C.breakdown_id='".$assetRow['breakdown']."' and B.asset_id='".$assetRow['assetid']."' order by A.id DESC",$db);

              if(DB_num_rows($tpl_check)>0)
              {
                // echo "hi";
               $row_asset=DB_fetch_array($tpl_check);

                $day1=$row_asset['service_date'];   
                $servicedays=$assetRow['service_time']; 
              }
              else
              {
                 $day1=$assetRow['service_date'];   
                 $servicedays=$assetRow['service_time']; 
              }
             // echo "no";
              // echo $servicedays;
                 $date1=date('Y-m-d');
                 $ts1 = strtotime($date1);
                 $ts2 = strtotime($assetRow['service_date']);
                 $seconds_diff = $ts1 - $ts2;
                 $yearday=floor($seconds_diff/3600/24);
                  //echo $day1;
                
                 if(!empty($day1))   $daycount=  round($yearday/$servicedays);   else   $daycount= 0;
                  if($daycount==0)   $daycount=1;
               //   echo "<br>". $daycount;
                if($daycount<0) $daycount=0;
                  // echo "<br>". $daycount;
                if($daycount!=0)
                {
                for($k1=1;$k1<=($daycount);$k1++)
                  {     
               $maintenance_date1[$assetRow['assetid']][]=date('Y-m-d', strtotime($day1. ' + '.$servicedays*$k1.' days')); 
                  }
                }
               
               
                      if(!empty($maintenance_date1[$assetRow['assetid']]))
                      {     
                      for($k=0;$k<count($maintenance_date1[$assetRow['assetid']]);$k++)
                        {   
                          $maintenance_date=$maintenance_date1[$assetRow['assetid']][$k];  
                         $code=$s->getField('machine_center','id',$assetRow['assetid'],'code');
                         $assetname=$s->getField('machine_center','id',$assetRow['assetid'],'name');

                         $field1=$s->getField('machine_center','id',$assetRow['assetid'],$title[0]);
                         $field2=$s->getField('machine_center','id',$assetRow['assetid'],$title[1]);
                         $field3=$s->getField('machine_center','id',$assetRow['assetid'],$title[2]);

                         $subbreakdown_name=$s->getField('tbl_sub_break_type','id',$assetRow['sub_break_down_id'],'sub_breaktype');
                          $beakdown=$s->getField('break_down_type','id',$assetRow['breakdown'],'breakdown_reason');
                          if(!in_array($assetRow['sub_break_down_id'],$subassetid[$assetRow['assetid']][$maintenance_date]))
                         $subassetid[$assetRow['assetid']][$maintenance_date][]=$assetRow['sub_break_down_id'];
                      
                         $asset_data[]=array("id"=>$assetRow['assetid'],"asset_code"=>$code,"asset_name"=>$assetname,"field1"=>$field1,"field2"=>$field2,"field3"=>$field3,"subbreakdown"=>$subbreakdown_name,"maintenance_date"=>$maintenance_date,"group"=>$assetgroup,"breakdown"=>$beakdown,"subbreakdownid"=>$assetRow['sub_break_down_id'],"breakdown_id"=>$assetRow['breakdown']);


                        }
                      }
//echo "<pre>";
                      //print_r($asset_data);
  
                  }

       
  }


 
 $group='';$data_mainten=array();$assetid=array();$subassetid1='';

if(!empty($asset_data))
{
foreach($asset_data as $key=>$val)
{
     if($_REQUEST['month']!='0' && $_REQUEST['year']=='0')
     $date_format=date('Y')."-".$_REQUEST['month'];
     if($_REQUEST['month']!='0' && $_REQUEST['year']!='0')
     $date_format=$_REQUEST['year']."-".$_REQUEST['month'];
     if($_REQUEST['month']=='0' && $_REQUEST['year']!='0')
     $date_format=$_REQUEST['year']."-".date('m');

     if($_REQUEST['month']=='0' && $_REQUEST['year']=='0')
     $date_format=date('Y-m');

     $data_format=date('Y',strtotime($val['maintenance_date']))."-".date('m',strtotime($val['maintenance_date']));
 
   // echo "<br>". $date_format;
    
        $check_jobcard=DB_query("SELECT tpl_breakdown_mgmt.id FROM tpl_breakdown_mgmt INNER JOIN tpl_breakdown_mgmt_machine  ON tpl_breakdown_mgmt.id=tpl_breakdown_mgmt_machine.master_id INNER JOIN tpl_breakdown_mgmt_machine_breakdown   ON tpl_breakdown_mgmt.id=tpl_breakdown_mgmt_machine_breakdown.parent_id    where  maintenance_type='Preventive' and asset_id='".$val['id']."'  and asset_rec_date_time>='".$val['maintenance_date']."'    and sub_breakdown_id='".$val['subbreakdownid']."' order by asset_id,sub_breakdown_id desc limit 0,1",$db);
          $numcheck=DB_num_rows($check_jobcard);               
    

         $sql_check=DB_query("SELECT tpl_breakdown_mgmt_assig.completion_status FROM tpl_breakdown_mgmt_assig 
         INNER JOIN tpl_breakdown_mgmt_details ON tpl_breakdown_mgmt_assig.id=tpl_breakdown_mgmt_details.master_id 
         WHERE job_id=(SELECT tpl_breakdown_mgmt.id FROM tpl_breakdown_mgmt INNER JOIN tpl_breakdown_mgmt_machine  ON tpl_breakdown_mgmt.id=tpl_breakdown_mgmt_machine.master_id INNER JOIN tpl_breakdown_mgmt_machine_breakdown   ON tpl_breakdown_mgmt.id=tpl_breakdown_mgmt_machine_breakdown.parent_id    where  maintenance_type='Preventive' and asset_id='".$val['id']."'  and asset_rec_date_time>='".$val['maintenance_date']."'    and sub_breakdown_id='".$val['subbreakdownid']."' order by asset_id,sub_breakdown_id desc limit 0,1) AND  tpl_breakdown_mgmt_details.completed_date>='".$val['maintenance_date']."' GROUP BY tpl_breakdown_mgmt_assig.id",$db);
//echo "<br>".DB_num_rows($sql_check);
if(DB_num_rows($sql_check)<=0 && $data_format==$date_format)
{    
 //echo "dfdf"; 
if($group!=$val['group'])
  {?>
<tr bgcolor="#c1e0c0">
  <td colspan="11"><?php echo $val['group']; ?></font></td>
  </tr>
  <?php $group=$val['group']; } ?>
  <?php  
  if(!in_array($val['maintenance_date'],$data_mainten)  ||  !in_array($val['id'],$assetid))
  {
    if(count($subassetid[$val['id']][$val['maintenance_date']])>1)
    $breaddonwid=implode(",",$subassetid[$val['id']][$val['maintenance_date']]);
    else
    $breaddonwid=$subassetid[$val['id']][$val['maintenance_date']][0];   
  ?>
    <tr>
    <td><?php  echo $i;?></td>
    <td><?php  echo $val['asset_code'];?></td>
    <td><?php  echo $val['asset_name'] ;?></td>
    <td><?php  echo $val['field1'] ;?></td>
    <td><?php  echo $val['field2'] ;?></td>
    <td><?php  echo $val['field3'] ;?></td>
    <td><?php  echo $val['breakdown'];?></td>
    <td style="padding:0px;" valign="top">
    <div id="amc-tab">
    <table width="100%" border="0" cellspacing="0" cellpadding="3">
    <?php foreach($subassetid[$val['id']][$val['maintenance_date']] as $k1=>$v1)
    {
     ?>
      <tr>
      <td><?php echo $s->getField('tbl_sub_break_type','id',$v1,'sub_breaktype') ;?></td></tr>
      <?php } ?></table>
      </div>
      </td>     
      <td><?php  echo ConvertSQLDate($val['maintenance_date']);   ?></td>              
      <td style="text-align:center;"><?php //echo "numrow".$numcheck."---".$val['maintenance_date']."-------".date('Y-m-d'); ?><?php if($numcheck<=0 && $val['maintenance_date']<=date('Y-m-d') ) { ?><a href="index.php?route=ToolPlants/Transaction/asset_breakdown_management_add&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>&action=add&id=<?php echo $val['id']; ?>&sub_breakdown_id=<?php echo $breaddonwid;  ?>&breakdown=<?php echo $val['breakdown_id'];?>&maint_date=<?php echo $val['maintenance_date'];  ?>&maintenance_type=Preventive"><input type="button" name="AddJob" value="Add Job" class="search" /></a>      
       <?php } else { echo ""; } ?>
      </td> 
    </tr>
    <?php $i++;   $data_mainten[]=$val['maintenance_date'] ; $assetid[]=$val['id'];   }  } } } else {
      ?>
      <tr><td colspan="7">No Record Found</td></tr>
   <?php } }
  ?>
  
  </table>

</div>
</div>
