<?php
class report_personals_planned_instruction extends c_report
{
 var $title='Сотрудники, запланированные на инструктаж' ;
 var $view_by_space_filter=1 ;
 var $allow_export_to_csv=1 ;
 var $arr_temp_fired=array(0=>'Без сотрудников в отложенном увольнении',1=>'Все сотрудники') ;
 var $arr_types=array('esmo_magnit_instruction.i_event_instruction1'=>'Сезонный инструктаж',
                      'esmo_magnit_instruction.i_event_instruction2'=>'Вводный инструктаж',
                      'esmo_magnit_instruction.i_event_instruction3'=>'Специальный инструктаж',
                      'esmo_magnit_instruction.i_event_training'=>'Учебная программа'
             ) ;
 var $arr_type_ids=array('esmo_magnit_instruction/i_event_instruction1'=>1,
                         'esmo_magnit_instruction/i_event_instruction2'=>2,
                         'esmo_magnit_instruction/i_event_instruction3'=>3,
                         'esmo_magnit_instruction/i_event_training'=>4
                 ) ;
 function setting($options=array())
 {
    $_usl_events=array('clss=222','ext like "esmo_magnit_instruction/%"') ;
    if ($this->filter['type']) $_usl_events[]='ext="'.addslashes(str_replace('.','/',$this->filter['type'])).'"' ;
    $arr_instruktions=execSQL('select pkey,obj_name from obj_site_events_schedule where '.implode(' and ',$_usl_events).' order by obj_name') ;
    list($arr_orgs_ids,$arr_otdel_ids,$arr_workd_ids)=PERSONAL()->get_org_otdels_works_to_cur_account($this->filter) ;
    ?><div id="panel_filter">
            <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?php echo _CUR_REPORT_DIR?>">
                 <fieldset>
                     <div class="row">
                         <?php $this->filter_section_input_text('name','ФИО:',2,array('auto_complete'=>"autocomplette_personal_name")) ;
                           $this->filter_section_select_multiple('org_ids','Организация:',2,$arr_orgs_ids) ;
                           $this->filter_section_select_multiple('otdel_ids','Подразделение:',3,$arr_otdel_ids) ;
                           $this->filter_section_select_multiple('working_ids','Должность:',3,$arr_workd_ids) ;
                         ?>
                     </div>
                     <div class="row">
                         <?php $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) ;
                           ?>
                     </div>
                     <div class="row">
                         <?php $this->filter_section_input_text('uid','UID сотрудника:',2) ;
                           $this->filter_section_select('type','Тип инструктажа:',2,$this->arr_types) ;
                           $this->filter_section_select('event_id','Наименование инструктажа:',4,$arr_instruktions) ;
                           $this->filter_section_select('temp_fired','Отложенное увольнение:',2,$this->arr_temp_fired,array('nospace_element'=>1)) ;
                           ?>
                     </div>
                 </fieldset>
                 <?php $this->panel_buttons($options);?>
             </form>
      </div><br><?php
 }

 function panel_report_buttons($options=array())
 {
    ?><button class="button v2" cmd="report/view_as_JSON">JSON</button>&nbsp;&nbsp;<?php
 }

 function create_usl_select_obj($_params,&$options=array())
 {
    $usl=array() ;
    $usl[]='t1.enabled=1' ;
    $usl[]='t1.status in (2,3)' ;
    $usl[]='e.ext like "esmo_magnit_instruction/%"' ;

    if ($this->filter['event_id'])    $usl[]='e.pkey='.(int)$this->filter['event_id'] ;
    if ($this->filter['type'])       $usl[]='e.ext="'.addslashes(str_replace('.','/',$this->filter['type'])).'"' ;

    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) ;
    $this->prepare_SQL_left_join() ;

    return((_sizeof($usl))? implode(' and ',$usl):'1') ;
 }

 function get_cnt_items($usl,$options=array())
 {
    $having_block_not_past='having report_block_data is null
                                 or report_block_data = 0
                                 or report_block_data >= UNIX_TIMESTAMP(CURDATE())' ;
    $cnt=execSQL_value('select count(*) from (
                        select if(t1.block_data>0,
                                  t1.block_data,
                                  min(case when tm.block_from>0 then tm.block_from else null end)
                                 ) as report_block_data
                        from obj_site_events_pass t1
                        left join view_personals fl on fl.pkey=t1.personal_id
                        left join obj_site_workers so on so.personal_id=t1.personal_id
                        left join obj_site_events_schedule e on e.pkey=t1.event_id
                        left join obj_site_personal_mess tm on tm.source_pkey=t1.pkey and tm.source_tkey=t1.tkey and tm.personal_id=t1.personal_id
                        '.$this->join_table.'
                        where '.$usl.$this->join_usl.'
                          and not fl.pkey is null
                          and not so.pkey is null
                          and not e.pkey is null
                        group by t1.pkey
                        '.$having_block_not_past.'
                        ) cnt_sub',$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 report_block_data desc, e.obj_name, fl.obj_name' ;
    $list_recs=execSQL('select fl.*,
                               t1.pkey as event_pass_id,
                               t1.event_id,
                               t1.personal_id,
                               t1.status as event_status,
                               t1.block_data,
                               e.obj_name as event_name,
                               e.ext as event_ext,
                               e.group_id as event_group_id,
                               if(t1.block_data>0,
                                  t1.block_data,
                                  min(case when tm.block_from>0 then tm.block_from else null end)
                                 ) as report_block_data
                        from obj_site_events_pass t1
                        left join view_personals fl on fl.pkey=t1.personal_id
                        left join obj_site_workers so on so.personal_id=t1.personal_id
                        left join obj_site_events_schedule e on e.pkey=t1.event_id
                        left join obj_site_personal_mess tm on tm.source_pkey=t1.pkey and tm.source_tkey=t1.tkey and tm.personal_id=t1.personal_id
                        '.$this->join_table.'
                        where '.$usl.$this->join_usl.'
                          and not fl.pkey is null
                          and not so.pkey is null
                          and not e.pkey is null
                        group by t1.pkey
                        having report_block_data is null
                            or report_block_data = 0
                            or report_block_data >= UNIX_TIMESTAMP(CURDATE())
                        '.$order.$limit,$options) ;

    if (_sizeof($list_recs)) foreach($list_recs as $id=>$rec) $this->prepare_report_row($list_recs[$id]) ;
    return($list_recs) ;
 }

 function prepare_report_row(&$rec)
 {
    $type_key=str_replace('/','.',$rec['event_ext']) ;
    $rec['instruction_type']=$this->arr_types[$type_key] ;
    if (!$rec['instruction_type']) $rec['instruction_type']=$rec['event_ext'] ;
    $rec['instruction_type_id']=($this->arr_type_ids[$rec['event_ext']])? $this->arr_type_ids[$rec['event_ext']]:0 ;
    $rec['instruction_name']=($rec['event_name'])? $rec['event_name']:$rec['instruction_type'].' ID '.$rec['event_id'] ;
    $rec['instruction_id']=$rec['event_id'] ;
    $rec['block_date_text']=($rec['report_block_data'])? date('d.m.Y',$rec['report_block_data']):'' ;
 }

 function get_export_row($rec)
 {
    return(array('ФИО'=>$rec['obj_name'],
                 'UID сотрудника'=>$rec['uid'],
                 'тип инструктажа'=>$rec['instruction_type'],
                 'ID типа инструктажа'=>$rec['instruction_type_id'],
                 'наименование инструктажа'=>$rec['instruction_name'],
                 'ID инструктажа'=>$rec['instruction_id'],
                 'дата блокировки'=>$rec['block_date_text']
           )) ;
 }

 function print_template_HTML($list_recs,$options=array())
 {
    $id=($options['id'])? 'id='.$options['id']:'' ;
    ?><table class="basic fz_small full auto"<?php echo $id?>>
        <tr>
            <th>ФИО</th>
            <th>UID сотрудника</th>
            <th>Тип инструктажа</th>
            <th>ID типа инструктажа</th>
            <th>Наименование инструктажа</th>
            <th>ID инструктажа</th>
            <th>Дата блокировки</th>
        </tr>
        <?php
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        {   ?><tr>
                <td class="left"><?php echo $rec['obj_name']?></td>
                <td><?php echo $rec['uid']?></td>
                <td class="left"><?php echo $rec['instruction_type']?></td>
                <td><?php echo $rec['instruction_type_id']?></td>
                <td class="left"><?php echo $rec['instruction_name']?></td>
                <td><?php echo $rec['instruction_id']?></td>
                <td><?php echo $rec['block_date_text']?></td>
            </tr><?php
        }
    ?></table><?php
 }

 function print_template_CSV($list_recs,$options=array())
 {
    $i=$options['from'] ;
    if (_sizeof($list_recs)) foreach($list_recs as $rec)
    {
        $this->put_data_to_CSV($this->get_export_row($rec),$i) ;
        $i++ ;
    }
 }

 function print_template_JSON($list_recs,$options=array())
 {
    $result=array() ;
    if (_sizeof($list_recs)) foreach($list_recs as $rec) $result[]=$this->get_export_row($rec) ;
    echo json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE) ;
 }
}

?>
