<?php
include_once(_DIR_EXT.'/personal/report_personals.php');
class report_personals_pmo_opo extends report_personals
{
 public $view_by_space_filter=1 ;
 var $title='Отчет по сотрудникам мероприятия ПМО и ОПО' ;
 var $allow_export_to_csv=1 ;
 var $arr_events_types=[0=>'ПМО или ОПО',
                        1=>'ПМО',
                        2=>'ОПО',
                        3=>'ПМО и ОПО',
                        ];
 var $arr_status_style=[0=>'',
                       1=>'color:#aaaaaa;', //'Подлежит ОПО',
                       2=>'color:#aaaaaa;', //'Направлен на ОПО',
                       22=>'color:#aaaaaa;', //'Идет подготовка документов',
                       3=>'color:#aaaaaa;', //'Документы подготовлены',
                       4=>'color:#aaaaaa;', //'Уведомлен',
                       5=>'color:#aaaaaa;', //'Заблокирован по уведомлению',
                       6=>'color:#aaaaaa;', //'Документы получены',
                       7=>'color:#aaaaaa;', //'Заблокирован по прохождению ОПО',
                       8=>'color:green;', //'ОПО пройден, годен',
                       9=>'color:red;', //'ОПО пройден, негоден'
                    ] ;
 var $fields_set=     [1=>'ID',
                       2=>'Организация',
                       3=>'Подразделение',
                       4=>'Должность',
                       5=>'GUID',
                       6=>'ФИО',
                       7=>'Дата рождения',
                       8=>'Возраст',
                       9=>'Группа сотрудника',
                       10=>'Статус ПМО текущий',
                       11=>'Дата присвоения статуса ПМО',
                       12=>'Периодичность прохождения ПМО',
                       13=>'Дата предыдущего ПМО',
                       14=>'Дата следующего ПМО',
                       15=>'Статус ОПО текущий',
                       16=>'Дата присвоения статуса ОПО',
                       17=>'Дата предыдущего ОПО',
                       18=>'Дата следующего ОПО',

                    ] ;


  function setting($options=array())
   {   if (!is_array($options['disabled_filter'])) $options['disabled_filter']=array() ;
       if ($this->filter['reffer']) $this->personals_filter_reffer=$this->filter['reffer'] ;

       $arr_groups=ACCOUNTS()->get_arr_groups_to_cur_account() ;
       $options2=[] ;
       $options2['debug']=0 ;
       if ($this->personals_filter_reffer) $options2['filter_reffer']=$this->personals_filter_reffer ;
       if ($this->personals_filter_viewer) $options2['viewer_name']=$this->personals_filter_viewer ;
       list($arr_orgs_ids,$arr_otdel_ids,$arr_works_ids,$viewer_ids)=PERSONAL()->get_org_otdels_works_to_cur_account($this->filter,$options2) ;
       //$arr_reffers=PERSONALS_FILTER()->get_arr_filter_reffers() ;
       // $viewer_ids будет иметь значение, только если был задан $this->filter['reffer'] или $this->personals_filter_reffer
       if ($viewer_ids) $this->viewer_ids=$viewer_ids ;
       $form=new i_form;
       $form->form_open();
       $form->fieldset_open() ;
       $form->row_open() ;
       //$this->filter_section_select('reffer','REFFER:',2,$arr_reffers) ;
       $this->filter_section_select('group_id',$GLOBALS['LS_person_group_title'].':',2,$arr_groups) ;
       $this->filter_section_select_multiple('org_ids','Организация:',4,$arr_orgs_ids) ;
       $this->filter_section_select_multiple('otdel_ids','Подразделение:',4,$arr_otdel_ids) ;
       $this->filter_section_select_multiple('working_ids','Должность:',2,$arr_works_ids) ;
       $form->row_close_open() ;
       $this->filter_section_input_text('name','ФИО:',4,array('auto_complete'=>"autocomplette_personal_name")) ;
       //$this->filter_section_input_text('propusk','Пропуск:',2) ;
       //$this->filter_section_input_text('tab_number','Таб.номер:',2) ;
       $this->filter_section_input_text('uid','UID:',2) ;
       $this->filter_section_select('person_status','Дополнительный признак',2,$this->dop_props) ;
       $this->filter_section_select_multiple('fields_set','Скрывать поля',2,$this->fields_set) ;
       $form->row_close_open() ;
       $this->filter_section_select_multiple('group_pers','Группа сотрудников:',2,_IL('IL_groups')->recs) ;
       $this->filter_section_select_multiple('group_medic','Группа по болезни:',3,_IL('IL_group_medic')->recs) ;
       $this->filter_section_input_text('age_from','Возраст, от:',2) ;
       $this->filter_section_input_text('age_to','Возраст, до:',2) ;
       $this->filter_section_select('events_type','Тип мероприятия',2,$this->arr_events_types,['nospace_element'=>1]) ;
       $form->row_close() ;

       $form->fieldset_close();
       $this->panel_buttons();
       $form->form_close() ;
    }

  function create_usl_select_obj($_params=array(),&$options=array())
    {   $this->left_join=[] ;
        $this->unions=[] ;

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

        if ($this->viewer_ids) $this->left_join['viewer_ids']=['join'=>'left join '.$this->viewer_ids.' vids on vids.pkey=fl.pkey','usl'=>'not vids.pkey is null'] ;


        $rec_event_PMO=EVENTS()->get_event_by_id($GLOBALS['LS_esmo_PMO_event']) ;
        $rec_event_OPO=EVENTS()->get_event_by_id($GLOBALS['LS_esmo_OPO_event']) ;

        switch($this->filter['events_type'])
        { case 3: $this->left_join['viewer_mpo']=['join'=>'left join '.$rec_event_PMO['viewer_name'].' pmo on pmo.pkey=fl.pkey','usl'=>'not pmo.pkey is null'] ;
                  $this->left_join['viewer_opo']=['join'=>'left join '.$rec_event_OPO['viewer_name'].' opo on opo.pkey=fl.pkey','usl'=>'not opo.pkey is null'] ;
                  break ;
          case 2: $this->left_join['viewer_opo']=['join'=>'left join '.$rec_event_OPO['viewer_name'].' opo on opo.pkey=fl.pkey','usl'=>'not opo.pkey is null'] ;
                  break ;
          case 1: $this->left_join['viewer_mpo']=['join'=>'left join '.$rec_event_PMO['viewer_name'].' pmo on pmo.pkey=fl.pkey','usl'=>'not pmo.pkey is null'] ;
                  break ;
          default:$this->left_join['viewer_mpo']=['join'=>'left join '.$rec_event_PMO['viewer_name'].' pmo on pmo.pkey=fl.pkey','usl'=>'(not pmo.pkey is null or not opo.pkey is null)'] ;
                  $this->left_join['viewer_opo']=['join'=>'left join '.$rec_event_OPO['viewer_name'].' opo on opo.pkey=fl.pkey','usl'=>'1'] ;

        }
        //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() ;

        $usl_res=(_sizeof($usl))? implode(' and ',$usl):'1' ;


       $this->prepare_SQL_left_join() ;
       return($usl_res) ;
    }

  function get_select_sql($usl)
      { $sql=
            'select fl.*,
             pass_pmo.status as pmo_status,pass_pmo.r_data as pmo_r_data,pass_pmo.data_from as pmo_data_from,pass_pmo.data_to as pmo_data_to,
             pass_opo.status as opo_status,pass_opo.r_data as opo_r_data,pass_opo.data_from as opo_data_from,pass_opo.data_to as opo_data_to
             from '.$this->viewer_name.' fl
             left join obj_site_workers so on so.personal_id=fl.pkey
             left join obj_site_events_pass pass_pmo on pass_pmo.event_id='.$GLOBALS['LS_esmo_PMO_event'].' and pass_pmo.personal_id=fl.pkey
             left join obj_site_events_pass pass_opo on pass_opo.event_id='.$GLOBALS['LS_esmo_OPO_event'].' and pass_opo.personal_id=fl.pkey
             '.$this->join_table.'
             where '.$usl.'
             '.$this->join_usl.'
             group by fl.pkey
             ' ;
          return ($sql);
      }


  // шаблон - вывод ссылками без разделитея
    function print_template_HTML($list_recs,$options=array())
    { $id=($options['id'])? 'id='.$options['id']:'' ;
      $rec_event_PMO=EVENTS()->get_event_by_id($GLOBALS['LS_esmo_PMO_event']) ;
      ?><table class="list_personal_table_img basic fz_small full auto"<?echo $id?>>
          <tr>
            <?if (!$this->filter['fields_set'][1]){?><th>ID</th><?}?>
            <?if (!$this->filter['fields_set'][2]){?><th>Организация</th><?}?>
            <?if (!$this->filter['fields_set'][3]){?><th>Подразделение</th><?}?>
            <?if (!$this->filter['fields_set'][4]){?><th>Должность</th><?}?>
            <?if (!$this->filter['fields_set'][5]){?><th>GUID</th><?}?>
            <?if (!$this->filter['fields_set'][6]){?><th>ФИО</th><?}?>
            <?if (!$this->filter['fields_set'][7]){?><th>Дата рождения</th><?}?>
            <?if (!$this->filter['fields_set'][8]){?><th>Возраст</th><?}?>
            <?if (!$this->filter['fields_set'][9]){?><th>Группа сотрудника</th><?}?>
            <?if (!$this->filter['fields_set'][10]){?><th>Статус ПМО текущий</th><?}?>
            <?if (!$this->filter['fields_set'][11]){?><th>Дата присвоения статуса</th><?}?>
            <?if (!$this->filter['fields_set'][12]){?><th>Периодичность прохождения ПМО</th><?}?>
            <?if (!$this->filter['fields_set'][13]){?><th>Дата предыдущего ПМО</th><?}?>
            <?if (!$this->filter['fields_set'][14]){?><th>Дата следующего ПМО</th><?}?>
            <?if (!$this->filter['fields_set'][15]){?><th>Статус ОПО текущий</th><?}?>
            <?if (!$this->filter['fields_set'][16]){?><th>Дата присвоения статуса</th><?}?>
            <?if (!$this->filter['fields_set'][17]){?><th>Дата предыдущего ОПО</th><?}?>
            <?if (!$this->filter['fields_set'][18]){?><th>Дата следующего ОПО</th><?}?>
          </tr><? ;
          if (_sizeof($list_recs)) foreach($list_recs as $rec)
          {   $rec['__href_window']='/window/personal/person/'.$rec['pkey'].'/' ;

              $pmo_ot_info=EVENT_PMO()->get_pmo_len_info_to_person($rec_event_PMO,$rec['pkey']) ;
                ?><tr class=item>
    <?if (!$this->filter['fields_set'][1]){?><td class="id"><?echo $rec['pkey']?></td><?}?>
    <?if (!$this->filter['fields_set'][2]){?><td class="left"><?echo $rec['_org_name']?></td><?}?>
    <?if (!$this->filter['fields_set'][3]){?><td class="left"><?echo $rec['_otdel_name']?></td><?}?>
    <?if (!$this->filter['fields_set'][4]){?><td class="left"><?echo _IL('IL_works')->recs[$rec['working']]['obj_name']?>
                      <?if ($rec['working2']) echo '<br><strong>Совмещение: </strong>'._IL('IL_works')->recs[$rec['working2']]['obj_name']?>


                  </td><?}?>
    <?if (!$this->filter['fields_set'][5]){?><td class="age"><?echo $rec['uid']?></td><?}?>
    <?if (!$this->filter['fields_set'][6]){?><td class="left"><a href="<?echo $rec['__href_window']?>" class="new_window" target="_blank" title="win_second"><?echo $rec['obj_name']?></a></td><?}?>
    <?if (!$this->filter['fields_set'][7]){?><td class="dr"><?echo $rec['dr']?></td><?}?>
    <?if (!$this->filter['fields_set'][8]){?><td class="age"><?echo $rec['age']?></td><?}?>
    <?if (!$this->filter['fields_set'][9]){?><td><?echo (_sizeof($rec['_groups_medic']))? implode(', ',$rec['_groups_medic']):''?></td><?}?>
                  <!-- ПМО -->
    <?if (!$this->filter['fields_set'][10]){?><td style="<?echo $this->arr_status_style[$rec['pmo_status']]?>"><?echo ($rec['pmo_status'])? EVENT_PMO()->arr_status[$rec['pmo_status']]:'-'?></td><?}?>
    <?if (!$this->filter['fields_set'][11]){?><td><?echo ($rec['pmo_r_data'])? date('d.m.Y',$rec['pmo_r_data']):'-'?></td><?}?>
    <?if (!$this->filter['fields_set'][12]){?><td><?echo $pmo_ot_info['srok']?></td><?}?>
    <?if (!$this->filter['fields_set'][13]){?><td></td><?}?>
    <?if (!$this->filter['fields_set'][14]){?><td><?echo (($rec['pmo_status']==8 or $rec['pmo_status']==9) and $rec['pmo_data_to'])? date('d.m.Y',$rec['pmo_data_to']):'-'?></td><?}?>
                  <!-- ОПО -->
    <?if (!$this->filter['fields_set'][15]){?><td style="<?echo $this->arr_status_style[$rec['opo_status']]?>"><?echo ($rec['opo_status'])? EVENT_OPO()->arr_status[$rec['opo_status']]:'-'?></td><?}?>
    <?if (!$this->filter['fields_set'][16]){?><td><?echo ($rec['opo_r_data'])? date('d.m.Y',$rec['opo_r_data']):'-'?></td><?}?>
    <?if (!$this->filter['fields_set'][17]){?><td></td><?}?>
    <?if (!$this->filter['fields_set'][18]){?><td><?echo (($rec['opo_status']==8 or $rec['opo_status']==9) and $rec['opo_data_to'])? date('d.m.Y',$rec['opo_data_to']):'-'?></td><?}?>
              </tr><?
          }
        ?></table><?
        //damp_array($rec) ;
    }


  function print_template_CSV($list_recs,$options=array())
  { $i=$options['from'] ;
    $rec_event_PMO=EVENTS()->get_event_by_id($GLOBALS['LS_esmo_PMO_event']) ;
    if (_sizeof($list_recs)) foreach($list_recs as $rec)
    {   $pmo_ot_info=EVENT_PMO()->get_pmo_len_info_to_person($rec_event_PMO,$rec['pkey']) ;
        $data=array() ;
        $data['№']=$i+1 ;
      if (!$this->filter['fields_set'][1]) $data['ID']=$rec['pkey']  ;
      if (!$this->filter['fields_set'][2]) $data['Организация']=$rec['_org_name'] ;
      if (!$this->filter['fields_set'][3]) $data['Подразделение']=$rec['_otdel_name'] ;
      if (!$this->filter['fields_set'][4]) $data['Должность']=_IL('IL_works')->recs[$rec['working']]['obj_name'] ;
      if (!$this->filter['fields_set'][5]) $data['GUID']=$rec['uid']  ;
      if (!$this->filter['fields_set'][6]) $data['ФИО']=$rec['obj_name']  ;
      if (!$this->filter['fields_set'][7]) $data['Дата рождения']=$rec['dr']  ;
      if (!$this->filter['fields_set'][8]) $data['Возраст']=$rec['age']  ;
      if (!$this->filter['fields_set'][9]) $data['Группа сотрудника']=(_sizeof($rec['_groups_medic']))? implode(', ',$rec['_groups_medic']):''  ;
        // ПМО
      if (!$this->filter['fields_set'][10]) $data['Статус ПМО текущий']=($rec['pmo_status'])? EVENT_PMO()->arr_status[$rec['pmo_status']]:'-'  ;
      if (!$this->filter['fields_set'][11]) $data['Дата присвоения статуса ПМО']=($rec['pmo_r_data'])? date('d.m.Y',$rec['pmo_r_data']):'-'  ;
      if (!$this->filter['fields_set'][12]) $data['Периодичность прохождения ПМО']=$pmo_ot_info['srok']  ;
      if (!$this->filter['fields_set'][13]) $data['Дата предыдущего ПМО']=$rec['']  ;
      if (!$this->filter['fields_set'][14]) $data['Дата следующего ПМО']=(($rec['pmo_status']==8 or $rec['pmo_status']==9) and $rec['pmo_data_to'])? date('d.m.Y',$rec['pmo_data_to']):'-'  ;
        // ОПО
      if (!$this->filter['fields_set'][15]) $data['Статус ОПО текущий']=($rec['opo_status'])? EVENT_OPO()->arr_status[$rec['opo_status']]:'-'  ;
      if (!$this->filter['fields_set'][16]) $data['Дата присвоения статуса']=($rec['opo_r_data'])? date('d.m.Y',$rec['opo_r_data']):'-'  ;
      if (!$this->filter['fields_set'][17]) $data['Дата предыдущего ОПО']=$rec['']  ;
      if (!$this->filter['fields_set'][18]) $data['Дата следующего ОПО']=(($rec['opo_status']==8 or $rec['opo_status']==9) and $rec['opo_data_to'])? date('d.m.Y',$rec['opo_data_to']):'-'  ;

        // вносим данные в csv
        $this->put_data_to_CSV($data,$i) ;
        $i++ ;
    }
  }






}


?>
