<?php
include_once(_DIR_TO_CLASS.'/c_page.php') ;
class c_page_cab_personal_dyblicates_finder extends c_page
{
  public $allow_access_to_rol=11 ;
  var $h1='Анализ дубликатов физических лиц' ;

  var $source_people='' ;
  var $SPGT ;


    function second_menu_config()
     {
        $this->second_menu['/cab/personal/dyblicates/finder/']='Контроль дубликатов ФЛ' ;

     }

  function block_main()
  { $this->page_title() ;
    //$this->SPGT=SPGT() ;

    //$this->SPGT->init() ;
    ?><h2>Поиск дублей по ФИО в персонале</h2><?
    $this->setting() ;
    $this->personal_dubl() ;


    if (method_exists($this, $_POST['cmd'])) $this->$_POST['cmd']() ;
  }

function setting($options=array())
{

  ?><div id="panel_filter">
              <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="/cab/spgt/dubl/">
                   <fieldset>
                       <div class="row">
                           <section class="col col-2">
                               <label class="label">наличие пройденных МО:</label>
                               <label class="select">
                                   <select name="filter[mo]">
                                       <option value="0">Все</option>
                                       <option value="1" <?if ($this->filter['mo']==1) echo 'selected'?>>с МО</option>
                                       <option value="2" <?if ($this->filter['mo']==2) echo 'selected'?>>без МО</option>
                                   </select>
                               </label>
                           </section>
                           <section class="col col-2">
                               <label class="label">Показывать:</label>
                               <label class="select">
                                   <select name="filter[view_mode]">
                                       <option value="0">Все</option>
                                       <option value="1" <?if ($this->filter['view_mode']==1) echo 'selected'?>>Один из HR_ID=0</option>
                                       <option value="2" <?if ($this->filter['view_mode']==2) echo 'selected'?>>Все HR_ID=0</option>
                                       <option value="3" <?if ($this->filter['view_mode']==3) echo 'selected'?>>Все HR_ID разные</option>
                                   </select>
                               </label>
                           </section>
                           <section class="col col-2">
                               <label class="label">Автоудаление сотрудников без МО:</label>
                               <label class="select">
                                   <select name="filter[delete]">
                                       <option value="0">Нет</option>
                                       <option value="1" <?if ($this->filter['delete']==1) echo 'selected'?>>Да</option>
                                   </select>
                               </label>
                           </section>
                       </div>
                   </fieldset>
                  <div id="panel_buttons"><input type="submit" class="button v2" cmd="set_filter" value="Получить данные">
               </form>
   </div>
  <?
}

    function personal_dubl()
    {   $dubl_all=execSQL_line('select distinct(obj_name) from obj_site_personals where clss=211 group by obj_name having count(pkey)>1') ;
        echo 'Найдено дубликатов по ФИО - <strong>'._sizeof($dubl_all).'</strong><br>' ;
        $recs=execSQL_row('select distinct(obj_name),count(pkey) as cnt from obj_site_personals where clss=211 group by obj_name having cnt>1 order by obj_name limit 10 ') ;
        $i=1 ;
       if (_sizeof($recs)) foreach($recs as $name=>$cnt)
       { $recs=execSQL('select pkey,obj_name,c_data,dr,driving,tab_number,enabled,fired_data,uid,_image_name,propusk,_org_name,_otdel_name,
                        (select count(pkey) from obj_site_mo_session where member_id=t1.pkey) as cnt_mo,
                        (select max(c_data) from obj_site_mo_session where member_id=t1.pkey) as last_mo
                        from view_personals t1 where obj_name="'.$name.'" 
                        order by c_data') ;

         $view_by_mo=0 ;  $no_merge=0 ; $last_mo_pers_id=0 ; $last_mo_pers_data=0 ;
         $data=array() ;
         if (_sizeof($recs)) foreach($recs as $id=>$rec)
         {
           $data[$id]['ID']=$rec['pkey'] ;
           $data[$id]['UID']=$rec['uid'] ;
           $data[$id]['Пропуск']=$rec['propusk'] ;
           $data[$id]['СНИЛС']=$rec['snils'] ;
           $data[$id]['Фото']=($rec['_image_name'])? '<img src="/public/personals/200/'.$rec['_image_name'].'">':'' ;
           $data[$id]['ФИО']='<a href="/cab/personal/person/'.$rec['pkey'].'/" class="new_window">'.$rec['obj_name'].'</a>' ;
           $data[$id]['ДР']=$rec['dr'] ;
           $data[$id]['Организация']=$rec['_org_name'] ;
           $data[$id]['Подразделение']=$rec['_otdel_name'] ;
           $data[$id]['Статус']=($rec['enabled'])? 'Работает':'Уволен ('.date('d.m.Y H:i',$rec['fired_data']).')' ;

           $data[$id]['Число МО']=($rec['cnt_mo'])? $rec['cnt_mo']:'-' ;
           $data[$id]['Дата последнего МО']=($rec['cnt_mo'])? date('d.m.Y H:i',$rec['last_mo']):'-' ;
           $data[$id]['Дата создания записи']=($rec['c_data'])? date('d.m.Y H:i',$rec['c_data']):'-' ;

           if (!$this->filter['mo']) $view_by_mo=1 ;
           elseif ($this->filter['mo']==1 and $rec['cnt_mo']>0) $view_by_mo=1 ;
           elseif ($this->filter['mo']==2 and !$rec['cnt_mo']) $view_by_mo=1 ;
           if ($last_mo_pers_data<$rec['last_mo']) { $last_mo_pers_data=$rec['last_mo'] ; $last_mo_pers_id=$id ;}
           if ($rec['last_mo']) $recs[$id]['last_mo']=date('d.m.Y H:i',$rec['last_mo']) ; else $rec['last_mo']='-' ;
           unset($recs[$id]['tkey']) ;
         }
         if ($this->filter['view_mode']==1 and (!_sizeof($arr_HR1) or _sizeof($arr_HR1)==_sizeof($recs)))  $view_by_mo=0 ;
         if ($this->filter['view_mode']==2 and _sizeof($arr_HR1)>0)  $view_by_mo=0 ;
         if ($this->filter['view_mode']==3 and _sizeof($arr_HR1)!=_sizeof($recs))  $view_by_mo=0 ;
         if ($view_by_mo)
         {   ?><br><br><h2><?echo $i.'. '.$name?></h2><? $i++ ;
             print_2x_arr($data) ;
             if (_sizeof($arr_HR1)==_sizeof($recs) or _sizeof($arr_HR2)==_sizeof($recs)) echo '<div class="green">Все дубликаты имеют разные HR_ID, удаление дубликатов не требуется</div>' ;
             elseif (_sizeof($recs)) foreach($recs as $rec)
             if (!$rec['cnt_mo'] and !$rec['_image_name'] and !$rec['HR_ID'])
             { if (!$this->filter['delete'])
               {?><div class="red bol">Дубль без МО и без фото: <?echo $rec['pkey']?> - требует удаления</div><?
                $no_merge=1 ;
               }
               else
               { ?><div class="red bol">Удаляем дубль без МО и без фото: <?echo $rec['pkey']?></div><?
                 execSQL_update('delete from obj_site_personals where pkey='.$rec['pkey']) ;
                 $no_merge=1 ;
               }
             }
            if (!$no_merge) {?><button class="v2" cmd="spgt/merge_persons" ids="<?echo implode(',',array_keys($recs))?>" main_id="<?echo $last_mo_pers_id?>">Объединить персоны</button> в <?echo $last_mo_pers_id?><? }
         }
       }
    }

}
?>
