<?php
class report_get_persons extends c_report
{
 var $title='Сотрудники' ;
 var $view_by_space_filter=1 ;
 var $allow_org_ids=1 ;
 var $usl_select_org='' ;
    var $allow_export_to_csv=1 ;
    var $show_on_page_peports=0 ;

 function setting($options=array())
 {   if (!is_array($options['disabled_filter'])) $options['disabled_filter']=array() ;
     //$recs_orgs=execSQL('select * from obj_site_personals where parent=1 and clss=207 order by obj_name') ;
     list($arr_orgs_ids,$arr_otdel_ids,$arr_works_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="<?echo _CUR_REPORT_DIR?>">
                 <?if ($this->filter['ids']){?><input type=hidden name=filter[ids] value="(<?echo implode(';',array_keys($this->filter['ids']))?>)"><?}?>
                 <fieldset>
                      <div class="row" id="person_info">
                          <?
                          $this->filter_section_input_text('uid','UID сотрудника:',2) ;
                          $this->filter_section_input_text('name','Фамилия Имя Отчество:',2,array('auto_complete'=>"autocomplette_personal_name")) ;
                          $this->filter_section_input_text('propusk','Номер пропуска:',2) ;
                          $this->filter_section_select_multiple('org_ids','Организация:',2,$arr_orgs_ids) ;
                          $this->filter_section_select_multiple('otdel_ids','Подразделение:',2,$arr_otdel_ids) ;
                          ?>
                  </fieldset>
                 <?$this->panel_button_apply();?>
              </form>
       </div><br>
     <?
  }

 function setting_small($options=array())
 {    // внимание! для не администратора тут только сам оператор
      ?><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" id="person_info">
                           <section class="col col-2">
                               <label class="label">ФИО</label>
                               <label class="input">
                                    <input type="text" name="filter[name]" placeholder="" SetAutoComplete cmd="autocomplette_personal_name" value="<?echo $this->filter['name']?>">
                               </label>
                           </section>
                          <section class="col col-2">
                              <label class="label">Номер пропуска</label>
                              <label class="input">
                                  <input type="text" name="filter[propusk]" placeholder="" LANG="LAT" value="<?echo $this->filter['propusk']?>">
                              </label>
                          </section>
                          <section class="col col-2">
                              <label class="label">Должность</label>
                              <? $_usl=array() ;
                                 $_usl[]='clss=211' ;
                                 if ($this->filter['otdel_id']) $_usl[]='_otdel_id in ('.$this->filter['otdel_id'].')' ;
                                 $usl=implode(' and ',$_usl) ;
                                 $works_ids=execSQL_row('select distinct(working),1 from '.PERSONAL()->view_personal.' where '.$usl) ;
                              ?>
                              <label class="select">
                                  <select name="filter[working]" class="js-basic-multiple js-states form-control"><option></option>
                                     <?foreach(_IL('IL_works')->recs as $rec) if (!_sizeof($works_ids) or isset($works_ids[$rec['id']])) {?><option value="<?echo $rec['id']?>" <?if ($this->filter['working']==$rec['id']) echo 'selected'?>><?echo $rec['obj_name']?></option><?} ?>
                                  </select>
                              </label>
                          </section>
                          <section class="col col-2">
                              <label class="label">Смена</label>
                              <label class="select">
                                  <select name="filter[smena]"><option></option>
                                     <option value="cur" <?if ($this->filter['smena']=='cur') echo 'selected'?>>Текущая</option>
                                     <option value="prev" <?if ($this->filter['smena']=='prev') echo 'selected'?>>Предыдущая</option>
                                  </select>
                              </label>
                          </section>
                        </div>
                  </fieldset>
                  <?$this->panel_button_apply();?>
              </form>
       </div><br>
     <?
  }

  function create_usl_select_obj($_params,&$options)
  {  //damp_array($this->filter,1,-1) ;
     $usl=array() ;
     $usl[]='clss=211' ;
      if ($this->filter['name'])              $usl[]='obj_name like "'.$this->filter['name'].'%"' ;
      if ($this->filter['org_id'])            $usl[]='parent2='.$this->filter['org_id'] ;
      if ($this->filter['org_ids'])           if (!is_array($this->filter['org_ids'])) $usl[]='parent2 in ('.$this->filter['org_ids'].')' ;
      else                                    $usl[]='parent2 in ('.implode(',',array_keys($this->filter['org_ids'])).')' ;
      if ($this->filter['otdel_id'])          $usl[]='parent3='.$this->filter['otdel_id'] ;
      if ($this->filter['otdel_ids'])         if (!is_array($this->filter['otdel_ids'])) $usl[]='parent3 in ('.$this->filter['otdel_ids'].')' ;
                                              else                                       $usl[]='parent3 in ('.implode(',',array_keys($this->filter['otdel_ids'])).')' ;
      if ($this->filter['propusk'])           $usl[]='propusk="'.$this->filter['propusk'].'"' ;
      if ($this->filter['client_id'])        $usl[]='client_id='.$this->filter['client_id'] ;
      if ($this->filter['uid'])              $usl[]='uid="'.$this->filter['uid'].'"' ;
     $usl_res=implode(' and ',$usl) ;
     //damp_array($usl,1,-1) ;
     return($usl_res) ;
  }

 function panel_info_filter()
 { $_str=array() ; $info=array() ; $title='' ;
   if ($this->filter['name'])                            $info['ФИО']='"'.$this->filter['name'].'"' ;
   if ($this->filter['client_id'])                       $info['клиент']=ACCOUNTS()->get_member_name_by_id($this->filter['client_id']) ;
   if ($this->filter['org_id'])                          $info['Организация']=PERSONAL()->get_org_names_by_ids($this->filter['org_id']) ;
   if ($this->filter['otdel_id'])                        $info['Подразделение']=PERSONAL()->get_org_names_by_ids($this->filter['otdel_id']) ;
   if ($this->filter['org_ids'])                         $info['Организации']=PERSONAL()->get_org_names_by_ids($this->filter['org_ids']) ;
   if ($this->filter['otdel_ids'])                       $info['Подразделения']=PERSONAL()->get_org_names_by_ids($this->filter['otdel_ids']) ;
   if ($this->filter['uid'])                             $info['UID сотрудника']='"'.$this->filter['uid'].'"' ;
   if ($this->filter['propusk'])                         $info['номер пропуска']='"'.$this->filter['propusk'].'"' ;
   if (_sizeof($info)) foreach($info as $title=>$value) $_str[]=$title.': <strong>'.$value.'</strong>' ;
   if (_sizeof($_str)) $title='<p class=center>'.implode(' ',$_str).'</p>' ;
   return $title ;
 }


  function get_cnt_items($usl,$options=array())
  { $cnt=execSQL_value('select count(pkey)
                        from  '.PERSONAL()->view_personal.'
                        where '.$usl,$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 obj_name' ;
    //echo $usl ;
 	$list_rec=execSQL('select *
                       from  '.PERSONAL()->view_personal.'
                       where '.$usl.$order.$limit,$options) ;
    if (_sizeof($list_rec)) foreach($list_rec as $id=>$rec) PERSONAL()->prepare_public_info_to_personal($list_rec[$id],$options) ;
    return($list_rec) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;
    ?><table class="list_personal_table_img basic fz_small full auto"<?echo $id?>>
        <colgroup><col id="c1"><col id="c2"><col id="c3"><col id="c4"></colgroup>
        <tr><?if ($options['chech_check']){?><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>
                <?if ($options['chech_check']){?><td><input type="checkbox" name="_check[]" value="<?echo $rec['_reffer']?>"></td><?}?>
                <td class="id"><?echo $rec['propusk']?><?if ($rec['demo_access']){?><div class="demo_access" title="Сотрудник может проходить неограниченное число МО"></div><?}?></td>
                <td class="img"><a  href="<?echo $rec['__href_window']?>" class="new_window" target="_blank" title="win_second"><img src="<?echo img_clone($rec,'small')?>"></a></td>
                <td class="left"><a href="<?echo $rec['__href_window']?>" class="new_window" target="_blank" title="win_second"><?echo $rec['obj_name']?></a>
                    <br><?echo '<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']).')'?>
                </td>
                <td class="dr"><?echo $rec['dr']?></td>
                <td class="age"><?echo $rec['age']?></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>
            </tr><?
        }
      ?></table><?
      //damp_array($rec) ;
  }


  function print_template_CSV($list_recs,$options=array())
      { $i=$options['from'] ;
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        {   $data=array() ;
    	    $data['№']=$i+1 ;
            $data['Пропуск']=$rec['propusk']  ;
            $data['Табельный номер']=$rec['tabnumber']  ;
    	    $data['ФИО']=$rec['obj_name']  ;
            $data['UID']=$rec['uid']  ;
            $data['Дата рождения']=$rec['dr']  ;

            $data['Возраст']=$rec['age']  ;
    	    $data['Организация']=$rec['_org_name'] ;
    	    $data['Подразделение']=$rec['_otdel_name'] ;
            $data['Должность']=$rec['_work_name'] ;
            // вносим данные в csv
            $this->put_data_to_CSV($data,$i) ;
    	    $i++ ;
	}

     }



    function print_template_XML($list_recs,$options=array())
    { if (_sizeof($list_recs)) foreach($list_recs as $rec)
      {   $item=add_element($this->xml_doc,$this->xml,'item','',array('id'=>$rec['pkey'])) ;
          add_element($this->xml_doc,$item,'propusk',$rec['propusk']) ;
          add_element($this->xml_doc,$item,'tabnumber',$rec['tab_number']) ;
          add_element($this->xml_doc,$item,'photo',($rec['_image_name'])? img_clone($rec,'source'):'') ;
          add_element($this->xml_doc,$item,'fio',$rec['obj_name']) ;
          add_element($this->xml_doc,$item,'uid',$rec['uid']) ;
          add_element($this->xml_doc,$item,'enabled',($rec['enabled'])? 1:0) ;
          add_element($this->xml_doc,$item,'dr',$rec['dr']) ;
          add_element($this->xml_doc,$item,'pol',$rec['pol']) ;
          add_element($this->xml_doc,$item,'age',$rec['age']) ;
          add_element($this->xml_doc,$item,'phone',$rec['phone']) ;
          add_element($this->xml_doc,$item,'org_name',$rec['_org_name']) ;
          add_element($this->xml_doc,$item,'otdel_name',$rec['_otdel_name']) ;
          add_element($this->xml_doc,$item,'org_id',$rec['_org_id']) ;
          add_element($this->xml_doc,$item,'otdel_id',$rec['_otdel_id']) ;
          add_element($this->xml_doc,$item,'working',_IL('IL_works')->recs[$rec['working']]['obj_name']) ;
      }
    }

    function print_template_JSON($list_recs,$options=array())
       { $result=array() ;
         if (_sizeof($list_recs)) foreach($list_recs as $rec)
           $result[]=array('propusk'=>$rec['propusk'],
                           'tabnumber'=>$rec['tab_number'],
                           'photo'=>($rec['_image_name'])? img_clone($rec,'source'):'',
                           'fio'=>$rec['obj_name'],
                           'uid'=>$rec['uid'],
                           'enabled'=>($rec['enabled'])? 1:0,
                           'dr'=>$rec['dr'],
                           'pol'=>$rec['pol'],
                           'age'=>$rec['age'],
                           'org_name'=>$rec['_org_name'],
                           'otdel_name'=>$rec['_otdel_name'],
                           'org_id'=>$rec['_org_id'],
                           'otdel_id'=>$rec['_otdel_id'],
                           'working'=>_IL('IL_works')->recs[$rec['working']]['obj_name']
                          ) ;
           $text_json_out=json_encode($result) ;
           // возвращает ответ устройству
           echo $text_json_out ;

       }

    function manual_tags_in()
    {
        ?><table class="basic fz_normal left">
                    <tr><td>uid</td><td>Уникальный идентификатор сотрудника</td></tr>
                    <tr><td>name</td><td>ФИО</td></tr>
                    <tr><td>org_ids</td><td>ID организации, несколько организаций указывается через запятую</td></tr>
                    <tr><td>otdel_ids</td><td>ID подразделения, несколько подразделений указываются через запятую</td></tr>
                    <tr><td>propusk</td><td>Номер пропуска</td></tr>
        </table><?
    }

    function manual_tags_out()
    {
        ?><table class="basic fz_normal left">
                    <tr><td>fio</td><td>ФИО сотрудника</td></tr>
                    <tr><td>uid</td><td>Ваш уникальный идентификатор сотрудника.</td></tr>
                    <tr><td>fio</td><td>ФИО сотрудника</td></tr>
                    <tr><td>phone</td><td>Телефон сотрудника</td></tr>
                    <tr><td>propusk</td><td>Номер пропуска</td></tr>
                    <tr><td>tabnumber</td><td>Табельный номер</td></tr>
                    <tr><td>enabled</td><td>Состояние, 0 - заблокирован, 1-активный</td></tr>
                    <tr><td>dr</td><td>Дата рождения сотрудника, формат ДД.ММ.ГГГГ</td></tr>
                    <tr><td>pol</td><td>Пол сотрудника, 0=М, 1=Ж</td></tr>
                    <tr><td>org_name</td><td>Название организации сотрудника</td></tr>
                    <tr><td>org_id</td><td>ID организации сотрудника (ЭСМО)</td></tr>
                    <tr><td>otdel_name</td><td>Название подразделения организации</td></tr>
                    <tr><td>otdel_id</td><td>ID подразделения организации (ЭСМО)</td></tr>
                    <tr><td>working</td><td>Должность</td></tr>
                    <tr><td>photo</td><td>url фото</td></tr>
          </table><?
    }




}

?>
