<?php
// специальный отчет для добавления сотрудников в группы
include_once(_DIR_EXT.'/events_docs/i_event_docs.php');
class report_personals_in_event_docs_get extends c_report
{
 var $title='Выдача документа - сданные документы' ;
 var $view_by_space_filter=1 ;
 var $allow_org_ids=1 ;
 var $usl_select_org='' ;
 var $rec_event=array() ;
 var $event_group_id=0 ;
 var $event_id=0 ;
 var $group_id=0 ;
 var $personal_id=0 ;
 var $status=array() ;
 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) ;

 }

 function setting($options=array())
 {   if (!is_array($options['disabled_filter'])) $options['disabled_filter']=array() ;
     $arr_groups=ACCOUNTS()->get_arr_groups_to_cur_account() ;

     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']]) ;

     ?><div id="panel_filter">
             <form id="sky-form" class="sky-form">
                 <input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                 <input type="hidden" name="filter[event_id]" value="<?echo $this->filter['event_id']?>">
                 <fieldset>
                     <div class="row">
                       <? $this->filter_section_select('group_id',$GLOBALS['LS_person_group_title'].':',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>
                     <div class="row">
                       <? $this->filter_section_select_multiple('group_pers','Группа сотрудников:',3,_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>
                 </fieldset>
                 <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }


  function create_usl_select_obj($_params=array(),&$options=array())
  {  //damp_array($this->filter,1,-1) ;
     $usl=array() ;
     $usl[]='clss=211' ;
     $usl[]='enabled=1' ;
     $usl[]='event_status=5' ;
     if ($this->event_id)                   $usl[]='event_id='.$this->event_id ;
     if ($this->filter['event_id'])         $usl[]='event_id='.$this->filter['event_id'] ;
      if ($this->group_id)                   $usl[]='event_group_id in ('.$this->group_id.')' ;
     if ($this->filter['group_id'])         $usl[]='event_group_id in ('.$this->filter['group_id'].')' ;
     if ($this->doc_id)                     $usl[]='doc_id='.$this->doc_id ;
     if ($this->personal_id)                $usl[]='personal_id='.$this->personal_id ;
     if (_sizeof($this->status))            $usl[]='event_status in ('.implode(',',$this->status).')' ;
     if (_sizeof($this->filter['status']) and is_array($this->filter['status']))            $usl[]='event_status in ('.implode(',',$this->filter['status']).')' ;
     if (_sizeof($this->filter['status']) and !is_array($this->filter['status']))            $usl[]='event_status='.$this->filter['status'] ;
     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 ($this->filter['age_from'])        $usl[]='age>='.$this->filter['age_from'];
     if ($this->filter['age_to'])          $usl[]='age<='.$this->filter['age_to'];
     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']='personal_id in (select pkey from '.MEMBER()->personal_viewer_name.')' ;
     $usl_gb=array() ;
     if (_sizeof($this->filter['group_medic'])) foreach($this->filter['group_medic'] as $group_id=>$flag) $usl_gb[]='personal_id in (select pkey from view_personals_medic_group_'.$group_id.')' ;
     if (_sizeof($this->filter['group_pers'])) foreach($this->filter['group_pers'] as $group_id=>$flag) $usl_gb[]='personal_id in (select pkey from view_personals_group_'.$group_id.')' ;
     if (_sizeof($usl_gb)) $usl[]='('.implode(' or ',$usl_gb).')' ;

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

  function get_arr_info_filters()
  { $info=parent::get_arr_info_filters() ;
    //if ($this->filter['status'])  $info['Статус']=EVENT_PMO()->arr_status[$this->filter['status']] ;
    return $info ;
  }


 function get_cnt_items($usl,$options=array())
  { //$options['debug']=2 ;
    $cnt=execSQL_value('select count(pkey)
                        from  view_event_personals_docs
                        where '.$usl,$options) ;
    return($cnt) ;
  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
      $order=' order by event_person_id desc' ;
    //echo $usl ;
    //$options['debug']=2 ;
    $options['no_indx']=1 ;
 	$list_rec=execSQL('select *
                       from view_event_personals_docs
                       where '.$usl.$order.$limit,$options) ;
    //echo 'size='._sizeof($list_rec).'<br>' ;
    $options['no_group']=1 ;
    if (_sizeof($list_rec)) foreach($list_rec as $id=>$rec) PERSONAL()->prepare_href_info_to_personal($list_rec[$id],$options) ;
    return($list_rec) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;
    $arr_personal_ids=array() ;
    //print_r($list_recs) ;
      //print_2x_arr($list_recs,1,-1) ;
    ?><table class="list_personal_table_img 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>
            <th>Документ</th>
            <th>Скан</th>
        </tr>
        <?
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        {   ?><tr class=item>
                <td><a href="/window/events/event_pass/<?echo $rec['event_pass_id']?>/" class="new_window"><?echo $rec['event_pass_id']?></a></td>
                <td><?if ($rec['event_group_id']) echo $rec['event_pass_group_name']?></td>
                <td class="left"><a href="<?echo $rec['__href_window']?>" class="new_window" target="_blank" title="win_second"><?echo $rec['obj_name']?></a>
                    <?  /*if ($rec['uid']) echo '<br><span class="nowrap small">'.$rec['uid'].'</span>' ;*/
                        if (!$rec['enabled'])echo '<br><br><span class="red bold">УВОЛЕН</span> ('.date('d.m.Y H:i',$rec['fired_data']).')' ;
                        if ($rec['dr'])echo '<br>'.$rec['dr'] ;
                        /*if ($rec['age'])echo '<br>'.$rec['age'].' '.get_case_by_count_1_2_5($rec['age'],'год,года,лет');*/
                        /*if ($rec['propusk'])echo '<br>'.$rec['propusk'] ;*/
                    ?>
                </td>
                <td class="left"><?echo $rec['_org_name']?></td>
                <td class="left"><? echo $rec['_otdel_name']?></td>
                <td class="left"><?echo _IL('IL_works')->recs[$rec['working']]['obj_name']?></td>
                <td class="nowrap"><?echo date('d.m.Y H:i',$rec['event_pass_c_data'])?></td>
                <td><?
                    $close_data=($rec['event_close_data'])? $rec['event_close_data']:$rec['mess_close_data'] ;
                    echo date('d.m.Y',$close_data) ;
                    ?>
                </td>
                <td class="center nowrap">
                    <a href="/window/personal/message/<?echo $rec['mess_id']?>/" class="new_window" target="_blank" title="window_messages">История уведомлений</a>
                </td>
                <td><?if ($rec['mess_doc_id']){?><a href="/window/docs/pdf/<?echo $rec['mess_doc_id']?>/" class="new_window"><?echo $rec['mess_doc_name']?></a><?}?></td>
                <td><?if ($rec['scan_id']){?><a href="/window/esmo_magnit/pdd/scan/<?echo $rec['scan_id']?>/" class="new_window">Открыть скан</a><?}?></td>
            </tr><?
        }
      ?></table>
      <style type="text/css">
                      table.list_personal_table_img{}
                      table.list_personal_table_img td.cnt_mess{font-size:22px;color:green;}
                      table.list_personal_table_img td.cnt_mess a{text-decoration:none;}
                      table.list_personal_table_img td.cnt_mess a:hover{text-decoration:underline;}
                      table.list_personal_table_img div.view_cnt{font-size:24px;margin:5px 0;}
                      table.list_personal_table_img div.view_data{font-size:14px;margin:5px 0;}
                      table.list_personal_table_img div.mess_status{font-size:14px;margin:5px 0;font-weight:bold;}
                      table.list_personal_table_img div.mo_alert{color:white;font-size:15px;margin-top:10px;background:darkred;padding:10px;}
                table.list_personal_table_img td span.cnt{font-size:22px;color:green;}
                table.list_personal_table_img div.mo_alert{color:white;font-size:15px;margin-top:10px;background:darkred;padding:10px;}
                  </style>
      <?
      //damp_array($rec) ;
  }

   function print_template_CSV($list_recs,$options=array())
   { $i=$options['from'] ;

     if (_sizeof($list_recs)) foreach($list_recs as $rec) if ($rec['event_pass_id'])
       { //$block_data=($rec['mess_block_from'])? $rec['mess_block_from']:$rec['mess_max_data'] ;
         $block_data=$rec['mess_block_from'] ;
         $close_data=($rec['event_close_data'])? $rec['event_close_data']:$rec['mess_close_data'] ;
         $data=array() ;
         //$data['№']=$i+1 ;
         $data['№']=$rec['event_pass_id'] ;
         $data['АТП']=($rec['event_group_id'])? $rec['event_pass_group_name']:'' ;
         $data['Организация']=$rec['_org_name'] ;
         $data['Подразделение']=$rec['_otdel_name'] ;
         $data['ФИО']=$rec['obj_name'] ;
         $data['ДатаРожд']=$rec['dr'] ;
         $data['Дата выдачи']=date('d.m.Y H:i',$rec['event_pass_c_data']) ;
         $data['Дата блокировки']=date('d.m.Y H:i',$block_data) ;
         $data['Число дней до истечения']=($block_data>time())?  floor(($block_data-time())/(60*60*24)):0  ;
         $data['Статус']=EVENT_DOCS()->arr_status[$rec['event_status']] ;
         $data['Документ сдан']=($rec['event_status']==5 and $close_data)? date('d.m.Y',$close_data):'-' ;
         $this->put_data_to_CSV($data,$i) ;
         $i++ ;
       }

   }
}

?>
