<?php
class report_training4 extends c_report
{
 var $title='Журнал учета ежегодных занятий по программе повышения квалификации с водителями' ;
 var $view_by_space_filter=1 ;
 var $event_id=0 ;
 var $personal_id=1 ;
 var $rec_personal=array() ;
 var $rec_event=array() ;
 var $sign_before_print=1 ;
 //var $allow_export_to_csv=1 ;

 function init($options=array())
 { if ($this->event_id) $this->rec_event=EVENTS()->get_event_by_id($this->event_id)  ;
  // if ($this->personal_id) $this->rec_personal=PERSONAL()->get_personal_by_id($this->personal_id)  ;
  // if ($this->filter['name'])
  // { $this->rec_personal=PERSONAL()->get_personal_by_name($this->filter['name'])  ;
  //   $this->personal_id=$this->rec_personal['pkey'] ;
  // }
   if ($this->filter['event_id'])
   { $this->rec_event=EVENTS()->get_event_by_id($this->filter['event_id'])  ;
     $this->event_id=$this->rec_event['pkey'] ;
   }
 }
 function setting($options=array())
 {   $arr_instruktions=execSQL('select pkey,obj_name from obj_site_events_schedule where clss=222 and obj_name!="" and ext like "%instruction/i_event_training%" order by c_data desc') ;

     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_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">
                          <? if (!$options['no_view_event_select']) $this->filter_section_select('event_id','Мероприятия:',2,$arr_instruktions) ;
                             $this->filter_section_select('lession_group_id','Место проведения мероприятия:',2,$arr_groups) ;
                             $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','Подразделение:',2,$arr_otdel_ids) ;
                             $this->filter_section_select_multiple('working_ids','Должность:',2,$arr_workd_ids) ;
                          ?>
                      </div>

                  </fieldset>
                  <?$this->panel_buttons($options);?>
              </form>
       </div><br>
     <?
  }

  function create_usl_select_obj($_params,&$options)
   {  //damp_array($this->filter,1,-1) ;
      $usl=array() ;
      $usl[]='event_id='.$this->event_id ;
      $usl[]='lession_person_status=4';
      //if ($this->ids)              $usl[]='pkey in ('.$this->ids.')' ;
      if ($this->filter['name'])              $usl[]='obj_name like "'.$this->filter['name'].'%"' ;
      if ($this->filter['personal_id'])       $usl[]='pkey='.$this->filter['personal_id'].'' ;
      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[]='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'])).')' ;
      // в этом журнале не должно быть ограничения по видимости сотрудников, так как он показывает список уже прошедших занятие
      if (MEMBER()->personal_viewer_name) $usl['account_personal_filter']='(lession_group_id in ('.MEMBER()->cur_groups_visible.') or  pkey in (select pkey from '.MEMBER()->personal_viewer_name.'))' ;



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

   function panel_info_filter()
    { $_str=array() ; $title='' ;  $info=array() ;
      if ($this->filter['lession_group_id'])   $info['Место проведения']=ACCOUNTS()->get_member_name_by_id($this->filter['lession_group_id'])  ;
      if ($this->event_id)
      { $arr_names=execSQL_line('select obj_name from obj_site_events_schedule where pkey='.$this->event_id) ;
        $info['Учебная программа']=implode(', ',$arr_names) ;
      }
      $info2=$this->get_arr_info_filters() ;
      $info=array_merge($info,$info2) ;
      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(distinct(pkey)) from  view_lession_personals where '.$usl,$options) ;
     return($cnt) ;
   }

   var $personals_lessions ;

   function get_items($usl,$options=array())
   { $limit=($options['limit'])? ' '.$options['limit']:'' ;
     $order=($this->filter['order'])? ' order by '.$this->filter['order']:' order by c_data desc' ;
     //echo $usl ;
      $options['debug']=0 ;
  	  $this->personals_lessions=execSQL_row('select pkey,max(lession_id) as last_lession_id from view_lession_personals where '.$usl.' group by pkey '.$limit,$options) ;
  	  if (_sizeof($this->personals_lessions))
      {   $options['no_indx']=1 ;
          $list_recs=execSQL('select pkey,obj_name,dr,_org_name,_otdel_name,working from view_personals where pkey in ('.implode(',',array_keys($this->personals_lessions)).')',$options) ;
      }
  	  else $list_recs=array() ;
     if (_sizeof($list_recs)) foreach($list_recs as $id=>$rec) PERSONAL()->prepare_public_info_to_personal_v2($list_recs[$id],$options) ;
     return($list_recs) ;
   }


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

         if (_CUR_PAGE_NUMBER==1)
         {   ?><br><br>
               <h2>Сведения о темах и количестве часов ежегодных занятий по программе повышения квалификации с водителями АТП</h2><?
             $this->report_training3_body() ;
             ?><br><br><h2>Сведения о сотрудниках проходившие ежегодные занятия по программе повышения квалификации с водителями АТП</h2><?
         }



         $arr_lession_ids=array_unique($this->personals_lessions) ;
         $recs_sign=execSQL_row('select personal_id,sign_fname from obj_site_lession_persons where parent in ('.implode(',',$arr_lession_ids).')') ;
         $recs_status=execSQL_row('select personal_id,status from obj_site_events_pass where personal_id in ('.implode(',',array_keys($this->personals_lessions)).') and event_id='.$this->event_id) ;
         $lession_sert=execSQL_row('select lession_id,sert_id from obj_site_docs where lession_id in ('.implode(',',$arr_lession_ids).') and report_code="lession_personal_status"') ;
         $lession_data=execSQL_row('select pkey,time_from from obj_site_lession where pkey in ('.implode(',',$arr_lession_ids).')') ;
         $i=0 ;
         ?><table class="report_personals_training basic fz_small full auto"<?echo $id?>>
            <thead>
             <tr><th>№</th><th>Фамилия Имя Отчество</th><th>Дата рождения</th><th>Организация</th><th>Подразделение</th><th>Должность</th><th>Дата зачета</th><th>Зачет/Не зачет</th><th>ЭЦП инструктора</th><th>Подпись обучаемого</th></tr>
            </thead>
           <tbody>
           <?
             if (_sizeof($list_recs)) foreach($list_recs as $rec)
             {   $personal_id=$rec['pkey'] ;
                 $i++ ;
                 ?><tr class=item>
                     <td><?echo $i?></td>
                     <td class="left"><a href="<?echo $rec['__href_window']?>" class="new_window" target="_blank" title="win_second"><?echo $rec['obj_name']?></a></td>
                     <td class="left"><?echo $rec['dr']?></td>
                     <td class="left"><?echo $rec['_org_name']?></td>
                     <td class="left"><?echo $rec['_otdel_name']?></td>
                     <td class="left"><?echo $rec['_work_name']?></td>
                     <td><?echo ($recs_status[$personal_id]==7)? date('d.m.Y',$lession_data[$this->personals_lessions[$personal_id]]):'-'?></td>
                     <td><?echo ($recs_status[$personal_id]==7)? 'Зачет':'Незачет'?></td>
                     <td><?echo $lession_sert[$this->personals_lessions[$personal_id]]?></td>
                    <td><?if ($recs_sign[$personal_id]) echo '<img src="'._PATH_TO_SITE.'/public/instruktions_sign/'.$recs_sign[$personal_id].'" width=200>' ?></td>
                 </tr><?
             }
           ?></tbody></table>
             <style type="text/css">
                 table{-fs-table-paginate: paginate;}
                 table.report_personals_training th.small{font-size:10px;font-weight: normal;line-height:1em;vertical-align:top;text-align:left;}
                 thead {display: table-header-group;break-inside:avoid}
             </style>
           <?
       }

    function report_training3_body()
      {   if (!$this->rec_event['pkey']) return ;
          $recs_themes=execSQL('select * from obj_site_events_themes where parent='.$this->rec_event['pkey'].' and clss=225 order by indx') ;
           ?><table class="basic fz_small full auto"><tr class="head"><th>№</th><th>Тема занятий</th><th>Количество часов</th></tr><?
               if (_sizeof($recs_themes)) foreach($recs_themes as $rec)
               { ?><tr>
                     <td style="width:10%;" class="indx center"><?echo $rec['indx']?></td>
                     <td style="width:35%;" class="left"><?echo $rec['obj_name']?></td>
                     <td style="width:5%;" class="center"><?echo $rec['length']?> мин.</td>
                   </tr>
                 <?
               }
               ?>
                </table>
                <style type="text/css">
                   table.basic td.indx{text-align:center;}
                   table.basic td.indx:hover i{display:inline;cursor:pointer;}
                   table.basic ul{margin:0 0 0 20px;;padding:0;}
                   table.basic ul li{padding:0;margin:0;}
                </style>
               <?
      }

}

?>
