<?php
class report_meeting_persons extends c_report
{
 var $title='Отчет по сотрудникам (собрания)' ;
 var $view_by_space_filter=1 ;
 var $event_id=0 ;
 var $lession_id=0 ;
 var $rec_lession=array() ;
 var $rec_event=array() ;
 var $event_ext=array() ;
 var $personal_mode=array(0=>'Все',1=>'Только присутствующие на собрании',2=>'Только отстутствующие на собрании') ;
 var $sql_select_personal_from_event ;
 var $allow_export_to_csv=1 ;
    var $report_script=_DIR_EXT.'/events_meeting/report_meeting_persons.php' ;

 function init($options=array())
 { if (!function_exists('EVENTS')) return ;
   //if ($this->filter['event_id']) $this->event_id=$this->filter['event_id'];
   if (!$this->event_id) $this->event_id=execSQL_value('select pkey from obj_site_events_schedule where status!=5 and enabled=1 and ext="events_meeting/i_event_meeting" order by pkey limit 1') ;
   if ($this->event_id) $this->rec_event=EVENTS()->get_event_by_id($this->event_id)  ;
 }

 function setting($options=array())
 {
     list($arr_orgs_ids,$arr_otdel_ids,$arr_workd_ids)=PERSONAL()->get_org_otdels_works_to_cur_account($this->filter,['filter_reffer'=>$this->rec_event['_reffer']]) ;

      //$arr_events=execSQL('select pkey,obj_name,status from obj_site_events_schedule where clss=222 and status!=5 and ext="events_meeting/i_event_meeting" and obj_name!="" order by obj_name') ;
      if ($this->event_id) $arr_themes=execSQL('select pkey,obj_name from obj_site_events_themes where parent='.$this->event_id.' and obj_name!="" order by obj_name') ;
      $arr_groups=ACCOUNTS()->get_arr_groups_to_cur_account() ;
      //echo _CUR_REPORT_DIR ;
      ?><div id="panel_filter">
             <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                  <fieldset>
                      <div class="row">
                          <? //$this->filter_section_select('event_id','Cобрание:',2,$arr_events) ;
                             $this->filter_section_select_multiple('theme_ids','Темы собрания:',2,$arr_themes) ;
                             $this->filter_section_input_text('name','ФИО:',2,array('auto_complete'=>"autocomplette_personal_name")) ;
                             $this->filter_section_select('lession_group_id','Место проведения собрания:',2,$arr_groups) ;
                             $this->filter_section_input_text('data_from','Дата собрания, от:',2,array('class'=>'datetimepicker')) ;
                             $this->filter_section_input_text('data_to','Дата собрания, до:',2,array('class'=>'datetimepicker')) ;
                          ?>
                        </div>
                      <div class="row">
                          <?$this->filter_section_select_multiple('org_ids','Организация:',2,$arr_orgs_ids) ;
                            $this->filter_section_select_multiple('otdel_ids','Подразделение:',2,$arr_otdel_ids) ;
                            $this->filter_section_select_multiple('working_ids','Должность:',2,$arr_workd_ids) ;
                            $this->filter_section_select('personal_mode','Отбор сотрудников:',2,$this->personal_mode) ;
                          ?>
                      </div>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

  function create_usl_select_obj($_params,&$options)
  {  //damp_array($this->filter) ;
     $usl=array() ;
     $usl[]='clss=211' ;
     //$usl[]='event_id='.$this->event_id ;
      //damp_array($this) ;
     if ($this->event_id and $this->rec_event['_ext_info']['usl_select']) $usl[]=$this->rec_event['_ext_info']['usl_select'] ;

      if (MEMBER()->personal_viewer_name) $usl['account_personal_filter']='pkey in (select pkey from '.MEMBER()->personal_viewer_name.')' ;

     if ($this->filter['lession_id']) $usl[]='lession_id='.$this->filter['lession_id'] ;
     if ($this->filter['personal_id'])        $usl[]='pkey='.$this->filter['personal_id'] ;

      if ($this->filter['name'])            $usl[]='obj_name like "%'.$this->filter['name'].'%"' ;
      if ($this->filter['org_ids'])         $usl[]='parent2 in ('.implode(',',array_keys($this->filter['org_ids'])).')' ;
      if ($this->filter['otdel_ids'])       $usl[]='parent3 in ('.implode(',',array_keys($this->filter['otdel_ids'])).')' ;
      if (_sizeof($this->filter['working_ids'])) $usl[]='working in ('.implode(',',array_keys($this->filter['working_ids'])).')' ;

      $_usl_select_lession=array() ;
      $_usl_select_lession[]='status in (4,5)' ;
      if ($this->event_id) $_usl_select_lession[]='event_id='.$this->event_id ;
      if ($this->filter['lession_group_id']) $_usl_select_lession[]='group_id='.$this->filter['lession_group_id'] ;
      if ($this->filter['data_from'])        $_usl_select_lession[]='time_from>='.strtotime($this->filter['data_from']) ;
      if ($this->filter['data_to'])          $_usl_select_lession[]='time_from<='.strtotime($this->filter['data_to']) ;


      $_usl_select_events=array() ;
      $_usl_select_events[]='status=4' ;
      $_usl_select_events[]='event_id='.$this->event_id ;


      if (_sizeof($_usl_select_lession)) $_usl_select_events[]='parent in (select pkey from obj_site_lession where '.implode(' and ',$_usl_select_lession).')' ;
      if ($this->filter['theme_ids']) $_usl_select_events[]='parent in (select parent from obj_site_lession_themes where event_id='.$this->event_id.' and theme_id in ('.implode(',',array_keys($this->filter['theme_ids'])).'))' ;

      $usl_select_events=implode(' and ',$_usl_select_events) ;
      $this->sql_select_personal_from_event='select distinct(personal_id) from  obj_site_lession_persons where '.$usl_select_events ;
      if ($this->filter['personal_mode']==1) $usl[]='pkey in ('.$this->sql_select_personal_from_event.')' ;
      if ($this->filter['personal_mode']==2) $usl[]='not pkey in ('.$this->sql_select_personal_from_event.')' ;
       //$recs_lession_stats=execSQL_line('select distinct(personal_id) from  obj_site_lession_persons where '.$usl_select_events,2) ;

     $usl_res=implode(' and ',$usl) ;
     //damp_array($usl,1,-1) ;
     //damp_array($_usl_select_events,1,-1) ;
     return($usl_res) ;
  }

 function panel_info_filter()
 { $_str=array() ; $title='' ;
   $info=$this->get_arr_info_filters() ;
   if ($this->filter['lession_group_id'])   $info['Место проведения инструктажа']=ACCOUNTS()->get_member_name_by_id($this->filter['lession_group_id'])  ;
   if ($this->filter['personal_mode'])   $info['Отбор сотрудников']=$this->personal_mode[$this->filter['personal_mode']];
   if ($this->filter['theme_ids'])
   { $arr_names=execSQL_line('select obj_name from obj_site_events_themes where pkey  in ('.implode(', ',array_keys($this->filter['theme_ids'])).')') ;
     $info['Темы занятий']=implode(', ',$arr_names) ;
   }

   if (_sizeof($info)) foreach($info as $title=>$value) $_str[]=$title.': <strong>'.$value.'</strong>' ;
   if (_sizeof($_str)) $title='<div class=center>'.implode(' ',$_str).'</div>' ;
   return $title ;
   }



  function get_cnt_items($usl,$options=array())
  { //$options['debug']=2 ;
    $cnt=execSQL_value('select count(pkey)
                        from  view_personals
                        where '.$usl.'
                        ',$options) ;
    return($cnt) ;

  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    //$options['debug']=1 ;
    // дополнительная выборка по тем, кто прошел занятие
    $recs_lession_stats=execSQL_line($this->sql_select_personal_from_event) ;
    //$order=($this->filter['order'])? ' order by '.$this->filter['order']:' order by pkey desc' ;
    $order=($options['order'])? ' order by '.$options['order']:' order by obj_name desc' ;
 	$list_recs=execSQL('select * from  view_personals where '.$usl.$order.$limit,$options) ;
    if (_sizeof($list_recs)) foreach($list_recs as $id=>$rec) PERSONAL()->prepare_public_info_to_personal($list_recs[$id],$options) ;
    if (_sizeof($recs_lession_stats)) foreach($recs_lession_stats as $personal_id) if (isset($list_recs[$personal_id])) $list_recs[$personal_id]['_event_pass']=1 ;
    //print_2x_arr($list_recs) ;
    return($list_recs) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  {
    $id=($options['id'])? 'id='.$options['id']:'' ;
    $i=1 ;
    ?><table class="basic fz_small full auto"<?echo $id?>>
        <tr><th>ID</th><th>ФИО</th><th>Дата рождения</th><th>Организация</th><th>Подразделение</th><th>Присутствие на собрании</th></tr><?
          if (_sizeof($list_recs)) foreach($list_recs as $rec)
          {   ?><tr>
                  <td><?echo $rec['pkey']?></td>
              <td class="left"><a href="<?echo $rec['__href_window']?>" class="new_window" target="_blank" title="win_second"><?echo $rec['obj_name']?></a>
                  <br><?echo '<span class="nowrap small">'.$rec['uid'].'</span>'?>
                  <?if (!$rec['enabled'])echo '<br><br><span class="red bold">УВОЛЕН</span> ('.date('d.m.Y H:i',$rec['fired_data']).')'?>
                  <?if ($rec['enabled'] and $rec['fired_data_pause']>0){?><div class="personal_fired_pause">Отложенное увольнение <span>до <?echo date('d.m.Y',$rec['fired_data_pause'])?></span></div><?}?>
              <?  // проверки
                  //$lession_ids=execSQL_line('select parent from obj_site_lession_persons where event_id='.$this->event_id.' and personal_id='.$rec['pkey'].' and status=4',2) ;
                  //if (_sizeof($lession_ids)) $theme_ids=execSQL('select pkey,parent,obj_name from obj_site_lession_themes where event_id='.$this->event_id.' and parent in ('.implode(',',$lession_ids).')',2) ;
                  //if (_sizeof($lession_ids)) $theme_ids=execSQL('select pkey,obj_name,group_id from obj_site_lession where pkey in ('.implode(',',$lession_ids).')',2) ;
              ?>
              </td>
                  <td><?echo $rec['dr']?></td>
                  <td class="left"><?echo $rec['_org_name']?></td>
                  <td class="left"><?echo $rec['_otdel_name']?></td>
                  <td class="center"><?echo ($rec['_event_pass'])? 'Да':'Нет'?></td>
              </tr><?
              $i++;
          }
      ?></table><?
  }

    function print_template_CSV($list_recs,$options=array())
          { $i=$options['from'] ;
            if (_sizeof($list_recs)) foreach($list_recs as $rec)
            {   $data=array() ;
        	    $data['№']=$i+1 ;
                $data['Пропуск']=$rec['propusk']  ;
                $data['GUID']=$rec['uid']  ;
        	    $data['ФИО']=$rec['obj_name']  ;
                $data['Дата рождения']=$rec['dr']  ;
                $data['Возраст']=$rec['age']  ;
                $data['Пол']=($rec['pol'])? 'Женский':'Мужской'  ;
        	    $data['Организация']=$rec['_org_name'] ;
        	    $data['Подразделение']=$rec['_otdel_name'] ;
        	    $data['Должность']= _IL('IL_works')->recs[$rec['working']]['obj_name'] ;
                $data['Уволен']=($rec['enabled'])? '':'Да' ;
                $data['Присутствие на собрании']=($rec['_event_pass'])? 'Да':'Нет' ;
                // вносим данные в csv
                $this->put_data_to_CSV($data,$i) ;
        	    $i++ ;
    	}

         }


}

?>
