<?php
class report_lessions_jurnal 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 $allow_export_to_csv=1 ;
 var $arr_personal_status=[0=>'Только действующие',1=>'Вместе с уволенными'] ;

 function init($options=array())
 { if (!function_exists('EVENTS')) return ;
   if ($this->filter['lession_id']) $this->rec_lession=EVENTS()->get_lession_by_id($this->filter['lession_id'])  ;
   if ($this->rec_lession['event_id']) $this->rec_event=EVENTS()->get_event_by_id($this->rec_lession['event_id'])  ;
   else  if ($this->event_id)           $this->rec_event=EVENTS()->get_event_by_id($this->event_id)  ;
   //damp_array($this) ;
 }
 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']]) ;

      if ($this->event_ext) $arr_instruktions=execSQL('select pkey,obj_name from obj_site_events_schedule where clss=222 and obj_name!="" and ext="'.$this->event_ext.'" order by c_data desc') ;
      else                  $arr_instruktions=array() ;
      $arr_groups=ACCOUNTS()->get_arr_groups_to_cur_account() ;
      ?><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('event_ids','Инструктажи:',2,$arr_instruktions) ;
                             $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_select('personal_status','Статус сотрудников:',2,$this->arr_personal_status) ;
                             $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) ;
                          ?>
                      </div>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

    function setting_small($options=array())
     {
       //damp_array(ENGINE()->reports_list['napravlenie']['']) ;
          ?><div id="panel_filter">
                 <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                      <?$this->panel_buttons($options);?>
                  </form>
           </div><br>
         <?
     }

    function _panel_buttons($options=array())
             { ?><div id="panel_buttons">
                 <!--<input type="submit" class="button v2" cmd="set_filter" value="<?echo ($this->editing)? 'Получить данные':'Сформировать отчет'?>">&nbsp;&nbsp;--><?
                 if ($options['hidden_params']) foreach($options['hidden_params'] as $name=>$value) echo '<input type="hidden" name="'.$name.'" value="'.$value.'">' ;
               if (!$this->editing)
               //$doc_report_url=$this->get_filter_to_params(array('event_id'=>$this->event_id),array('url_report'=>'/cab/reports/report_training/')) ;
               {?><button class="button v2" cmd="docs/doc_create" report_code="<?echo $this->report_class?>">Сохранить в документах</button>&nbsp;&nbsp;<??>
                                    <!--<button class="button v2" cmd="report/view_as_html">HTML</button>&nbsp;&nbsp;-->
                                    <!--<button class="button v2" cmd="report/view_as_pdf" mode="<?echo $this->PDF_master?>">PDF</button>&nbsp;&nbsp;-->
                                    <?if ($this->allow_export_to_excel){?><button class="button v2" cmd="report/view_as_xls">Excel</button>&nbsp;&nbsp;<?}?>
                                    <?if ($this->allow_export_to_csv){?><button class="button v2" cmd="report/view_as_CSV">Excel CSV</button>&nbsp;&nbsp;<?}?>
                                    <?if ($this->allow_set_to_cron){?><button class="button v2" cmd="report/set_report_to_cron" report_code="<?echo $this->report_class?>">Планировщик</button>&nbsp;&nbsp;<?}?>
                                    <!--<button class="button v2" cmd="report/to_print">Печать</button>&nbsp;&nbsp;-->
                                    <?$this->panel_report_buttons();?>
                                  <?}
               ?></div><?
             }


  function create_usl_select_obj($_params,&$options)
  {  //damp_array($this->filter) ;
     $usl=array() ;
     $usl[]='sign_fname!=""' ;
     //$usl[]='clss=226' ;
     if ($this->event_id) $usl[]='event_id='.$this->event_id ;
     if ($this->event_ext) $usl[]='event_ext="'.$this->event_ext.'"' ;
     if (_sizeof($this->filter['event_ids'])) $usl[]='event_id in ('.implode(', ',array_keys($this->filter['event_ids'])).')' ;
     if ($this->filter['lession_id']) $usl[]='lession_id='.$this->filter['lession_id'] ;
     if ($this->filter['personal_id'])        $usl[]='personal_id='.$this->filter['personal_id'] ;
     if ($this->filter['data_from'])        $usl[]='lession_time_from>='.strtotime($this->filter['data_from']) ;
     if ($this->filter['data_to'])          $usl[]='lession_time_from<='.strtotime($this->filter['data_to']) ;
      if ($this->filter['lession_group_id'])        $usl[]='lession_group_id='.$this->filter['lession_group_id'] ;
      if ($this->filter['name'])            $usl[]='tvp2.obj_name like "%'.$this->filter['name'].'%" and not tvp2.pkey is null' ;
      if ($this->filter['org_ids'])         $usl[]='tvp2.parent2 in ('.implode(',',array_keys($this->filter['org_ids'])).')' ;
      if ($this->filter['otdel_ids'])       $usl[]='tvp2.parent3 in ('.implode(',',array_keys($this->filter['otdel_ids'])).')' ;
      if (_sizeof($this->filter['working_ids'])) $usl[]='tvp2.working in ('.implode(',',array_keys($this->filter['working_ids'])).')' ;
      $usl[]='not tvp.pkey is null' ;
      $usl[]='not tvp2.pkey is null' ;


     $usl_res=implode(' and ',$usl) ;
     //damp_array($usl) ;
     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['event_ids'])
   { $arr_names=execSQL_line('select obj_name from obj_site_events_schedule where pkey  in ('.implode(', ',array_keys($this->filter['event_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())
  { $personal_viewer_name=($this->filter['personal_status'])? MEMBER()->personal_viewer_name.'_all':MEMBER()->personal_viewer_name ;
    $cnt=execSQL_value('select count(t1.pkey)
                        from  view_lession_personals t1
                        left join '.$personal_viewer_name.' tvp on tvp.pkey=t1.pkey
			left join obj_site_personals tvp2 on tvp2.pkey=t1.pkey
                        where '.$usl,$options) ;
    return($cnt) ;

  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    $personal_viewer_name=($this->filter['personal_status'])? MEMBER()->personal_viewer_name.'_all':MEMBER()->personal_viewer_name ;
    $options['debug']=0 ;
    $options['no_indx']=1 ;
    //$order=($this->filter['order'])? ' order by '.$this->filter['order']:' order by pkey desc' ;
    $order=($options['order'])? ' order by '.$options['order']:' order by lession_time_from desc' ;
 	$list_rec=execSQL('select t1.* 
                       from  view_lession_personals t1
                       left join '.$personal_viewer_name.' tvp on tvp.pkey=t1.pkey
			left join obj_site_personals tvp2 on tvp2.pkey=t1.pkey
                       where '.$usl.$order.$limit,$options) ;
    //if (_sizeof($list_rec)) foreach($list_rec as $id=>$rec) EVENTS()->prepare_public_info_to_event($list_rec[$id],$options) ;
    return($list_rec) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;
    $i=1 ;

      //damp_array($rec_lektor) ;
      //damp_array($this->rec_lession) ;

    ?><table class="basic fz_small full auto"<?echo $id?>>
        <tr><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)
          {   $rec_lektor=ACCOUNTS()->get_member_by_id($rec['lession_account_id']) ;
              $rec_event=EVENTS()->get_event_by_id($rec['event_id']) ;
              ?><tr>
                  <td><?echo $i?></td>
                  <td><?echo date('d.m.Y H:i',$rec['lession_time_from'])?><??></td>
                  <td class="left"><?echo $rec['obj_name']?>
                  <td><?echo $rec['dr']?></td>
                  <td class="left"><?echo $rec_event['__type_name'].' "'.$rec_event['obj_name'].'"'?></td>
                  <td class="left"><?echo $rec_lektor['obj_name'].', '.$rec_lektor['working']?></td>
                  <td><?if($rec_lektor['signature']){?><img src="<?echo img_clone($rec_lektor['signature'],'source')?>" width="100"><?}?></td>
                  <td><?if ($rec['sign_fname']) echo '<img src="'._PATH_TO_SITE.'/public/instruktions_sign/'.$rec['sign_fname'].'" width=200>' ?></td>
              </tr><?
              $i++;
          }
      ?></table><?
      //damp_array($rec) ;
  }


}

?>
