<?php
class report_meeting_person_details extends c_report
{
 var $title='Ведомость участия сотрудника в собраниях' ;
 var $view_by_space_filter=1 ;
 var $event_id=0 ;
 var $personal_id=0 ;
 var $lession_id=0 ;
 var $rec_lession=array() ;
 var $rec_event=array() ;
 var $event_ext=array() ;
 var $rec_personal;
 var $allow_export_to_csv=1 ;
 var $report_script=_DIR_EXT.'/events_meeting/report_meeting_person_details.php' ;

 function init($options=array())
 { if (!function_exists('EVENTS')) return ;
   $this->rec_personal=PERSONAL()->get_personal_by_id($this->personal_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_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_multiple('theme_ids','Темы собрания:',2,$arr_themes) ;
                             $this->filter_section_input_text('name','ФИО:',2,array('auto_complete'=>"autocomplette_personal_name")) ;
                             $this->filter_section_select('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 setting_to_personal($options=array())
 {
     // так как название темы может быть свободным, не можем использовать ID для идентификации темы
     $arr_themes=execSQL_row('select distinct(obj_name) as theme_name,obj_name,theme_id from obj_site_lession_themes where parent in (select distinct(parent) from obj_site_lession_persons where personal_id='.$this->personal_id.') order by theme_name') ;
     $arr_groups=execSQL_row('select distinct(t1.group_id) as group_id,t2.obj_name as group_name 
                              from obj_site_lession t1 
                              left join obj_site_account t2 on t2.pkey=t1.group_id
                              where t1.pkey in (select distinct(parent) from obj_site_lession_persons where personal_id='.$this->personal_id.') 
                              order by group_name') ;

      ?><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_multiple('theme_ids','Темы собрания:',2,$arr_themes) ;
                             $this->filter_section_select('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>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

  function create_usl_select_obj($_params,&$options)
  {  //damp_array($this->filter) ;
     $usl=array() ;
     $usl[]='t1.status=4' ;
     $usl[]='t3.ext="events_meeting/i_event_meeting"' ;
     $usl[]='t1.personal_id='.$this->personal_id ;

     if ($this->filter['group_id'])
      {   $arr1=ACCOUNTS()->tree[$this->filter['group_id']]->get_arr_child() ;
          $usl[]='t2.group_id in ('.implode(',',$arr1).')'  ;
      }

      if (_sizeof($this->filter['theme_ids']))
      {   $arr=array() ;
          foreach ($this->filter['theme_ids'] as $name=>$use) $arr[]='obj_name="'.$name.'"' ;
          $usl_theme='('.implode(' or ',$arr).' )' ;
          $arr_themes=execSQL_row('select parent,obj_name 
                                   from obj_site_lession_themes 
                                   where parent in ( select distinct(parent) 
                                                     from obj_site_lession_persons where personal_id='.$this->personal_id.'
                                                    )
                                   and '.$usl_theme.'                 
                                   ') ;
          if (_sizeof($arr_themes)) $usl[]='t2.pkey in ('.implode(',',array_keys($arr_themes)).')' ;

      }


     if ($this->filter['data_from'])        $usl[]='t1.c_data>='.strtotime($this->filter['data_from']) ;
     if ($this->filter['data_to'])          $usl[]='t1.c_data<='.strtotime($this->filter['data_to']) ;

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

    function panel_info_filter()
     { $_str=array() ; $info=array() ; $title='' ;
       if ($this->filter['group_id'])        $info['Группа']=ACCOUNTS()->get_member_name_by_id($this->filter['group_id']) ;
       //if ($this->filter['personal_id'])     $info['Сотрудник']=PERSONAL()->get_name_by_id($this->filter['personal_id']) ;
       //if ($this->filter['name'])              $info['Наименование']='"'.htmlspecialchars_decode($this->filter['name']).'"' ;
       //if ($this->filter['type'])              $info['Тип мероприятия']='"'.$this->arr_types[$this->filter['type']].'"' ;
       //if ($this->filter['status'])            $info['Cтатус']='"'.$_SESSION['ARR_events_status'][$this->filter['status']].'"' ;
       //if ($this->status)                      $info['Cтатус']='"'.$_SESSION['ARR_events_status'][$this->status].'"' ;
       if ($this->filter['data_from'])         $info['дата от']='"'.$this->filter['data_from'].'"' ;
       if ($this->filter['data_to'])           $info['дата до']='"'.$this->filter['data_to'].'"' ;
       if (_sizeof($info)) foreach($info as $title=>$value) $_str[]=$title.': <strong>'.$value.'</strong>' ;
       if (_sizeof($_str)) $title='<p class=center>'.implode(' ',$_str).'</p>' ;
       return $title ;
     }


  function get_cnt_items($usl,$options=array())
  { $cnt=execSQL_value('select count(t1.pkey)
                        from  obj_site_lession_persons t1
                        left join obj_site_lession t2 on t2.pkey=t1.parent 
                        left join obj_site_events_schedule t3 on t3.pkey=t1.event_id 
                        where '.$usl.'
                        ',$options) ;
    return($cnt) ;

  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    $options['debug']=0 ;
    $order=($options['order'])? ' order by '.$options['order']:' order by t1.pkey desc' ;
 	$list_recs=execSQL('select t1.*,t1.parent as lession_id,t2.obj_name as lession_name,t3.obj_name as event_name,t3.status as event_status,t4.group_id,t5.obj_name as group_name,t6.pkey as doc_id 
                        from  obj_site_lession_persons t1
                        left join obj_site_lession t2 on t2.pkey=t1.parent
                        left join obj_site_events_schedule t3 on t3.pkey=t1.event_id 
                        left join obj_site_lession t4 on t4.pkey=t1.parent 
                        left join obj_site_account t5 on t5.pkey=t4.group_id 
                        left join obj_site_docs t6 on t6.lession_id=t1.parent 
                        where '.$usl.$order.$limit,$options) ;
    return($list_recs) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { //$lession_ids=array() ; $arr_themes=array() ; $arr_themes_grp=array() ;
    //if (_sizeof($list_recs)) foreach($list_recs as $rec) $lession_ids[]=$rec['parent'] ;
    //if (_sizeof($lession_ids)) $arr_themes=execSQL('select pkey,parent,obj_name from obj_site_lession_themes where parent in ('.implode(',',$lession_ids).')') ;
    //if (_sizeof($arr_themes)) foreach($arr_themes as $rec) $arr_themes_grp[$rec['parent']][]=$rec['obj_name'] ;
    $id=($options['id'])? 'id='.$options['id']:'' ;
    //print_2x_arr($list_recs) ;
    $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><th>Присутствие на собрании</th><th>Наименование собрания</th><!--<th>Темы собрания</th>--></tr><?
          if (_sizeof($list_recs)) foreach($list_recs as $rec)
          {   ?><tr>
                  <td><?echo $this->rec_personal['pkey']?></td>
              <td class="left"><a href="<?echo $this->rec_personal['__href_window']?>" class="new_window" target="_blank" title="win_second"><?echo $this->rec_personal['obj_name']?></a>
                  <br><?echo '<span class="nowrap small">'.$this->rec_personal['uid'].'</span>'?>
                  <?if (!$this->rec_personal['enabled'])echo '<br><br><span class="red bold">УВОЛЕН</span> ('.date('d.m.Y H:i',$this->rec_personal['fired_data']).')'?>
                  <?if ($this->rec_personal['enabled'] and $rec['fired_data_pause']>0){?><div class="personal_fired_pause">Отложенное увольнение <span>до <?echo date('d.m.Y',$this->rec_personal['fired_data_pause'])?></span></div><?}?>
              </td>
                  <td><?echo $this->rec_personal['dr']?></td>
                  <td class="left"><?echo $this->rec_personal['_org_name']?></td>
                  <td class="left"><?echo $this->rec_personal['_otdel_name']?></td>
                  <td class="center"><?echo $rec['group_name']?></td>
                  <td class="center"><?echo date('d.m.Y H:i',$rec['c_data'])?></td>
                  <td class="center"><?if ($rec['doc_id']){?><a href="/window/docs/pdf/<?echo $rec['doc_id']?>/" class="new_window"><?echo $rec['lession_name']?></a><?}else echo $rec['lession_name'] ;
                      ?><?if ($rec['event_status']==5) echo '<div class="comment">Архив</div>'?></td>
                  <?/*<td class="center"><? if (_sizeof($arr_themes_grp[$rec['parent']])) echo implode('<br>',$arr_themes_grp[$rec['parent']])?></td>*/?>
              </tr><?
              $i++;
          }
      ?></table><?
  }

    function print_template_CSV($list_recs,$options=array())
       { //$lession_ids=array() ; $arr_themes=array() ; $arr_themes_grp=array() ;
         //if (_sizeof($list_recs)) foreach($list_recs as $rec) $lession_ids[]=$rec['parent'] ;
         //if (_sizeof($lession_ids)) $arr_themes=execSQL('select pkey,parent,obj_name from obj_site_lession_themes where parent in ('.implode(',',$lession_ids).')') ;
         //if (_sizeof($arr_themes)) foreach($arr_themes as $rec) $arr_themes_grp[$rec['parent']][]=$rec['obj_name'] ;

           //damp_array($list_recs,1,-1) ;
         $i=$options['from'] ;
         if (_sizeof($list_recs)) foreach($list_recs as $rec)
           { $data=array() ;
             $data['ФИО сотрудника']=$this->rec_personal['obj_name'] ;
             $data['ДатаРожд']=$this->rec_personal['dr'] ;
             $data['Организация']=$this->rec_personal['_org_name'] ;
             $data['Подразделение']=$this->rec_personal['_otdel_name'] ;
             $data['GUID']=$this->rec_personal['uid'] ;
             $data['Пропуск']=$this->rec_personal['propusk'] ;
             $data['Место проведения собрания']=$rec['group_name'] ;
             $data['Присутствие на собрании']=date('d.m.Y H:i',$rec['c_data']) ;
             $data['Наименование собрания']=$rec['event_name'] ;
             //$data['Темы собрания']=(_sizeof($arr_themes_grp[$rec['parent']]))? implode(', ',$arr_themes_grp[$rec['parent']]):'' ;


             $this->put_data_to_CSV($data,$i) ;
             $i++ ;
           }

       }

}

?>
