<?php
// отчет по участникам мероприятий с флагом dyn_pass (когда сотрудники добавляются в таблицу obj_site_events_pass по мере участия в мероятии)
include_once(_DIR_EXT.'/events/report_personals_in_event.php') ;
class report_personals_in_event_dyn extends report_personals_in_event
{
 var $title='Участники мероприятия' ;
 var $view_by_space_filter=1 ;
 var $allow_org_ids=1 ;
 var $rec_event=array() ;
 var $obj_event=null ;
 var $event_group_id=0 ;
 var $event_id=0 ;
 var $personal_id=0 ;
 var $group_id=0 ;
 var $status=array() ;
 var $pass_status=array() ;
 var $arr_pass_status=array() ;
 var $arr_temp_fired=array(0=>'Без сотрудников в отложенном увольнении',1=>'Все сотрудники') ;
 var $allow_export_to_csv=1 ;
 //var $report_script=_DIR_EXT.'/esmo_magnit_instruction/report_personals_in_event_instruction.php' ;
 var $filter_set=['name','org_ids','otdel_ids','working_ids','group_pers','group_medic','age_from','age_to','age_to','temp_fired','pass_status'];

 // для показа статусов
 //var $viewer_name='view_event_personals' ;


 function init($create_options=array())
 {
    if ($this->event_id)
    { $this->rec_event=EVENTS()->get_event_by_id($this->event_id) ;
      $this->obj_event=EVENTS()->get_event_obj_by_id($this->event_id) ;
      $this->arr_pass_status=$this->obj_event->arr_status ;
      $this->personals_filter_viewer=$this->rec_event['viewer_name'] ;
      //damp_array($this);
      unset($this->arr_pass_status[0]) ;
      //$this->viewer_name_usl='fl.event_id='.$this->event_id ;
      parent::init($create_options);
      //$this->debug=0 ;

    }
 }

  function create_usl_select_obj($_params=array(),&$options=array())
    {  if ($options['debug']){?><strong>FILTER</strong><br><? damp_array($this->filter,1,-1) ;}
       $usl=array() ;
       //$usl[]='t1.enabled=1';
       //if ($this->event_id)                        $usl[]='t1.event_id='.$this->event_id ;
       //if ($this->personal_id)                     $usl[]='t1.personal_id='.$this->personal_id ;

       if ($this->only_status==1)                  $usl[]='(t1.status=1 or t1.pkey is null)' ;
       if (_sizeof($this->status))                 $usl[]='t1.status in ('.implode(',',$this->status).')' ;
       if (_sizeof($this->pass_status))            $usl[]='t1.status in ('.implode(',',$this->pass_status).')' ;
       if (_sizeof($this->filter['status_ids']))   $usl[]='t1.status in ('.implode(',',array_keys($this->filter['status_ids'])).')' ;

       if (!$this->filter['temp_fired']) $this->filter['without_temp_fired']=1 ; // по умолчанию показываем сотрудников без отложенного увольнения

       // условия для сотрудников по ФЛ и СО набираются одинаково для всех отчетов, где есть выбор по сотрудникам
       PERSONAL()->get_usl_select_FL($this->filter,$usl,$this->left_join) ; // стандартные условия для отбора ФЛ + отбор по фильру аккаунта
       PERSONAL()->get_usl_select_SO($this->filter,$usl,$this->left_join) ; // стандартные условия для отбора СО

        //damp_array($options);
       if ($options['debug'])
       {
           ?><strong>main table usl</strong><br><?  damp_array($usl,1,-1) ;
           if (_sizeof($this->left_join)) {?><strong>LEFT JOIN</strong><br><? damp_array($this->left_join,1,-1);}
           if (_sizeof($this->unions))    {?><strong>UNIONS</strong><br><? damp_array($this->unions,1,-1);}
       }

       $this->prepare_SQL_left_join() ;

       //damp_array($usl);
       $usl_res=(_sizeof($usl))? implode(' and ',$usl):'1' ;
       return($usl_res) ;
    }

  function get_count_sql($usl)
  { //damp_array($this);
    $sql=  'select count(distinct(fl.pkey))
             from  '.$this->rec_event['viewer_name'].' event_viewer
             left join obj_site_events_pass t1 on t1.personal_id=event_viewer.pkey and t1.event_id='.$this->event_id.' and t1.enabled=1
             left join obj_site_personals fl on fl.pkey=event_viewer.pkey
             left join obj_site_workers so on so.personal_id=event_viewer.pkey
             '.$this->join_table.'
             where '.$usl.' and not fl.pkey is null and not so.pkey is null ' ;
     return ($sql);
  }

  function get_select_sql($usl)
    { $sql=
          'select fl.*,
           t1.pkey as event_pass_id,t1.status as event_pass_status,t1.r_data as data_event_pass_status_change 
           from '.$this->rec_event['viewer_name'].' event_viewer
           left join obj_site_events_pass t1 on t1.personal_id=event_viewer.pkey and t1.event_id='.$this->event_id.' and t1.enabled=1
           left join obj_site_personals fl on fl.pkey=event_viewer.pkey
           left join obj_site_workers so on so.personal_id=fl.pkey
           '.$this->join_table.'           
           where '.$usl.' and not fl.pkey is null and not so.pkey is null
           '.$this->join_usl.' 
           group by fl.pkey
           ' ;
        return ($sql);
   }

  function get_cnt_items($usl,$options=array())
   { //$options['debug']=0 ;s
       //trace() ;
     $options['debug']=$this->debug ;
     $sql=       'select count(distinct(fl.pkey))
                  from  '.$this->rec_event['viewer_name'].' event_viewer
                  left join obj_site_events_pass t1 on t1.personal_id=event_viewer.pkey and t1.event_id='.$this->event_id.' and t1.enabled=1
                  left join obj_site_personals fl on fl.pkey=event_viewer.pkey
                  left join obj_site_workers so on so.personal_id=event_viewer.pkey
                  '.$this->join_table.'
                  where '.$usl.' and not fl.pkey is null and not so.pkey is null ' ;

     $cnt=execSQL_value($sql,$options) ;
     return($cnt) ;
   }

  function get_items($usl,$options=array())
    { $limit=($options['limit'])? ' '.$options['limit']:'' ;
      $order=($this->filter['order'])? ' order by '.$this->filter['order']:' order by obj_name' ;
      //$options['debug']=2 ;
      //if (!isset($options['no_indx'])) $options['no_indx']=1 ;
      $sql=     'select fl.*,
                 t1.pkey as event_pass_id,t1.status as event_pass_status,t1.r_data as data_event_pass_status_change 
                 from '.$this->rec_event['viewer_name'].' event_viewer
                 left join obj_site_events_pass t1 on t1.personal_id=event_viewer.pkey and t1.event_id='.$this->event_id.' and t1.enabled=1
                 left join obj_site_personals fl on fl.pkey=event_viewer.pkey
                 left join obj_site_workers so on so.personal_id=fl.pkey
                 '.$this->join_table.'           
                 where '.$usl.' and not fl.pkey is null and not so.pkey is null
                 '.$this->join_usl.' 
                 group by fl.pkey
                 ' ;
      $list_rec=execSQL($sql.$order.$limit,$options) ;

   	PERSONAL()->full_personals_SO_info($list_rec) ; // заполняем информацию по СО в записи по ФЛ
      PERSONAL()->full_personals_medic_groups($list_rec) ; // заполняем информацию по медгруппам в записи по ФЛ
      PERSONAL()->full_org_othdels_work_names($list_rec) ; // заполняем информацию по медгруппам в записи по ФЛ

      if (_sizeof($list_rec)) foreach($list_rec as $id=>$rec) PERSONAL()->prepare_public_info_to_personal_v2($list_rec[$id],$options) ;
      return($list_rec) ;
    }




}

?>
