<?php
include_once("c_esmo_master.php");
class c_page_cab_esmo_master_stats2 extends c_page_cab_esmo_master
{
  public $h1='Рабочее место мастера - Эффективность работы ЭСМО' ;

 function block_main()
 { $this->page_title() ;
   $this->panel_source_info() ;
   $this->panel_graf_itog6() ;
   /*?><br><h2>График работы ЭСМО</h2><?
   include_once('panel_stats.php');
   $this->panel_stats_graf() ;

   ?><br><h2>Интенсивность проведения МО на объектах</h2><?
   */


 }


function get_full_zone_path($zone_id,$to_level=1)
{
  if (is_object(PERSONAL()->tree[$zone_id])) return(implode(' / ',PERSONAL()->tree[$zone_id]->get_arr_parent_name(array('to_level'=>$to_level)))) ;
  else return('') ;
}

 function panel_graf_itog6()
 {
     include_once(_DIR_EXT.'/aspmo_reports/report_itog6.php') ;
     $report_obj=new report_itog6() ;
     $report_obj->filter['otdel_id']=implode(',',$this->otdel_ids) ;
     $report_obj->filter['time_from']=1472677200 ;
     $report_obj->filter['time_to']=1475269199 ;
     $report_obj->view_table=0 ;
     $report_obj->view_as_HTML() ;

 }

 public $arr_smena=array(array('from_h'=>16,'from_m'=>0,'to_h'=>22, 'to_m'=>59,'day'=>-1, 'name'=>3),
                         array('from_h'=>23,'from_m'=>0,'to_h'=>23, 'to_m'=>59,'day'=>-1,'name'=>1),
                         array('from_h'=>0, 'from_m'=>0,'to_h'=>5,  'to_m'=>59,'day'=>0, 'name'=>1),
                         array('from_h'=>6, 'from_m'=>0,'to_h'=>15, 'to_m'=>59,'day'=>0, 'name'=>2),
                         array('from_h'=>16,'from_m'=>0,'to_h'=>22, 'to_m'=>59,'day'=>0, 'name'=>3),
                         array('from_h'=>23,'from_m'=>0,'to_h'=>23, 'to_m'=>59,'day'=>0, 'name'=>1),
                         array('from_h'=>0, 'from_m'=>0,'to_h'=>7,  'to_m'=>59,'day'=>+1, 'name'=>1),
                         array('from_h'=>8, 'from_m'=>0,'to_h'=>15, 'to_m'=>59,'day'=>+1, 'name'=>2),

            ) ;


function get_smena_indx($time)
{   $now=getdate() ;
    $smena_indx=0 ;
    foreach($this->arr_smena as $indx=>$rec_smena)
    { $time_from=mktime($rec_smena['from_h'],$rec_smena['from_m'],0,$now['mon'],$now['mday']+$rec_smena['day'],$now['year']);
      $time_to=mktime($rec_smena['to_h'],$rec_smena['to_m'],59,$now['mon'],$now['mday']+$rec_smena['day'],$now['year']);
      //echo $indx.': '.date('d.m.Y H:i:s',$time_from).' - '.date('d.m.Y H:i:s',$time_to).'<br>' ;
      if ($time>=$time_from and $time<=$time_to)
      { $smena_indx=$indx ;
        //echo '<strong>$smena_indx='.$smena_indx.'</strong><br>' ;
      }
    }
    //$prev_smena_indx=($smena_indx)? $smena_indx-1:_sizeof($this->arr_smena)-1 ;
    $prev_smena_indx=$smena_indx-1 ;
    if ($this->arr_smena[$smena_indx]['name']==$this->arr_smena[$prev_smena_indx]['name']) $prev_smena_indx-- ;
    return(array($smena_indx,$prev_smena_indx)) ;
}

function get_smena_time($indx)
{ $indx1=$indx ; $indx2=$indx ;
  if ($this->arr_smena[$indx]['name']==$this->arr_smena[$indx-1]['name']) {$indx1=$indx-1 ; $indx2=$indx ; }
  if ($this->arr_smena[$indx]['name']==$this->arr_smena[$indx+1]['name']) {$indx1=$indx ; $indx2=$indx+1 ; }
  $now=getdate() ;
  $rec_smena=$this->arr_smena[$indx1] ;
  $time_from=mktime($rec_smena['from_h'],$rec_smena['from_m'],0,$now['mon'],$now['mday']+$rec_smena['day'],$now['year']);
  $rec_smena=$this->arr_smena[$indx2] ;
  $time_to=mktime($rec_smena['to_h'],$rec_smena['to_m'],59,$now['mon'],$now['mday']+$rec_smena['day'],$now['year']);
  return(array($time_from,$time_to)) ;
}

function panel_stats()
 { //$now=getdate() ;
   //$start_day=mktime(0,0,0,$now['mon'],$now['mday'],$now['year']);
   //if (!MEMBER()->use_auch_as_smena) $options['from_time']=mktime($now['hours']-12,$now['minutes'],0,$now['mon'],$now['mday'],$now['year']);
   //print_2x_arr($this->arr_smena) ;
   //$time=time() ;
   //$time=mktime(12,30,0,$now['mon'],$now['mday'],$now['year']);
   //echo date('d.m.Y H:i:s',$time).'<br>' ;
   list($cur_smena_indx,$prev_smena_indx)=$this->get_smena_indx(time()) ;
   //echo '$smena_indx='.$cur_smena_indx.'<br>' ;
   //echo '$prev_smena='.$prev_smena_indx.'<br>' ;
   list($cur_smena_from,$cur_smena_to)=$this->get_smena_time($cur_smena_indx) ;
   //echo 'cur_smena: '.date('d.m.Y H:i:s',$cur_smena_from).' - '.date('d.m.Y H:i:s',$cur_smena_to).'<br>' ;
   list($prev_smena_from,$prev_smena_to)=$this->get_smena_time($prev_smena_indx) ;
   //echo 'prev_smena: '.date('d.m.Y H:i:s',$prev_smena_from).' - '.date('d.m.Y H:i:s',$prev_smena_to).'<br>' ;


   $usl_based='c_data>='.$cur_smena_from.' and c_data<='.$cur_smena_to.' and group_id='.$this->group_id.' and _otdel_id in ('.implode(',',$this->otdel_ids).') ' ;
   $stats['cur_smena']=execSQL_row('select dopusk,count(pkey) from view_mo_session where '.$usl_based.' and member_id>0 and model>0  group by dopusk order by dopusk') ;
   $usl_based='c_data>='.$prev_smena_from.' and c_data<='.$prev_smena_to.' and group_id='.$this->group_id.' and _otdel_id in ('.implode(',',$this->otdel_ids).') ' ;
   $stats['prev_smena']=execSQL_row('select dopusk,count(pkey) from view_mo_session where '.$usl_based.' and member_id>0 and model>0  group by dopusk order by dopusk') ;


   ?><div id="panel_rmv_stats"><table class="basic full">
     <tr><th>Смена</th><th>Допущено</th><th>Не допущено</th></tr>
     <tr><td><strong>Предыдущая смена</strong><br><span class="small">Время МО: <?echo date('d.m.Y H:i',$prev_smena_from).' - '.date('d.m.Y H:i',$prev_smena_to)?></span></td>
           <td><?echo ($stats['prev_smena'][1])? $stats['prev_smena'][1]:'-'?></td>
           <td><?echo ($stats['prev_smena'][2])? '<strong class="red">'.$stats['prev_smena'][2].'</strong>':'-'?></td>
     </tr>
     <tr><td><strong>Текущая смена</strong><br><span class="small">Время МО: <?echo date('d.m.Y H:i',$cur_smena_from).' - '.date('d.m.Y H:i',$cur_smena_to)?></span></td>
           <td><?echo ($stats['cur_smena'][1])? $stats['cur_smena'][1]:'-'?></td>
           <td><?echo ($stats['cur_smena'][2])? '<strong class="red">'.$stats['cur_smena'][2].'</strong>':'-'?></td>
     </tr>
   </table><?

   ?></div><?
 }


}



?>
