<?php
include_once(_DIR_TO_CLASS."/c_page.php");
class c_page_cab_personal_merge extends c_page
{
  public $h1='Объединение учетных записей физических лиц' ;

  var $arr_mode=array(1=>'по UID',2=>'по ФИО+ДР',3=>'по пропуску') ;

 function set_head_tags()
 { parent::set_head_tags() ;

 }

    function setting($options=array())
       {   if ($this->filter['personal_id'] and !$this->filter['name']) $this->filter['name']=PERSONAL()->get_name_by_id($this->filter['personal_id']) ;
           ?><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('search_mode','Искать по:',2,$this->arr_mode) ; ?>
                              </div>
                        </fieldset>
                       <div id="panel_buttons"><input type="submit" class="button v2" cmd="set_filter" value="Искать"></div>
                    </form>
             </div><br>
           <?
        }


 function block_main()
 { $this->page_title() ;
   $this->setting() ;
   $title='' ; $fname='' ;  $recs=array() ;

   if ($this->filter['search_mode']==1)
   {    $recs_dubl_by_uid=execSQL('select t1.*,
                                 (select count(pkey) from obj_site_mo_session where member_id=t1.pkey) as cnt_mo,
                                 (select count(pkey) from obj_site_personal_mess where personal_id=t1.pkey) as cnt_mess,
                                 (select count(pkey) from obj_site_events_pass where personal_id=t1.pkey) as cnt_event_pass,
                                 (select count(pkey) from obj_site_docs where personal_id=t1.pkey) as cnt_docs,
                                 (select count(pkey) from obj_site_personals_image where parent=t1.pkey) as cnt_img,
                                 (select count(pkey) from obj_site_mo_log where personal_id=t1.pkey) as cnt_log,
                                 (select count(pkey) from obj_site_scan where personal_id=t1.pkey) as cnt_scan,
                                 (select count(pkey) from obj_site_lession_persons where personal_id=t1.pkey) as cnt_lession
                                 from view_personals t1 
                                 where t1.clss=211 and t1.uid in (select uid from obj_site_personals group by uid having count(pkey)>1)') ;
        $recs=group_by_field('uid',$recs_dubl_by_uid) ;
        $title='UID' ;
       $fname='UID' ;
   }

   if ($this->filter['search_mode']==2)
   {   $recs_dubl_fio=execSQL('select obj_name,dr from obj_site_personals where clss=211 group by obj_name,dr having count(pkey)>1',0,1) ;
       $i=0 ;
       echo 'Всего найдено '._sizeof($recs_dubl_fio).' двойников<br>' ;
       if (_sizeof($recs_dubl_fio)) foreach($recs_dubl_fio as $rec)
       {   //$rec['obj_name'].'_'.$rec['dr']
          $recs[$i]=execSQL('select * from view_personals where clss=211 and obj_name="'.$rec['obj_name'].'" and dr="'.$rec['dr'].'"',0,1) ;
          if ($i>1000)
          { echo '<div class="alert">Показаны первые 1000 аккунтов из списка двойников</div><br>' ;
            break ;

          }
          $i++ ;
       }
       $title='ФИО+ДР' ;
       $fname='obj_name' ;
   }

   if ($this->filter['search_mode']==3)
   {   $recs_dubl=execSQL_line('select propusk from obj_site_personals where clss=211 and propusk!="" group by propusk having count(pkey)>1',0,1) ;
       $i=0 ;
       echo 'Всего найдено '._sizeof($recs_dubl).' дублей<br>' ;
       if (_sizeof($recs_dubl)) foreach($recs_dubl as $propusk)
       {   //$rec['obj_name'].'_'.$rec['dr']
          $recs[$i]=execSQL('select * from view_personals where clss=211 and propusk="'.$propusk.'"',0,1) ;
          if ($i>1000)
          { echo '<div class="alert">Показаны первые 1000 аккунтов из списка дублей</div><br>' ;
            break ;

          }
          $i++ ;
       }
       $title='пропуск' ;
       $fname='propusk' ;
   }

   //damp_array($recs);

   echo '<div class="green">Найдено дублей: '._sizeof($recs).'</div>' ;

   $personal_ids=array() ;
   if (_sizeof($recs)) foreach($recs as $i1=>$recs_personal) foreach($recs_personal as $i2=>$rec_person) $personal_ids[]=$rec_person['pkey'] ;
   if (_sizeof($personal_ids))
     {
         $stats_cnt_mo=execSQL_row('select member_id,count(pkey) from obj_site_mo_session where member_id in ('.implode(',',$personal_ids).') group by member_id') ;
         $stats_cnt_mess=execSQL_row('select personal_id,count(pkey) from obj_site_personal_mess where personal_id in ('.implode(',',$personal_ids).')  group by personal_id') ;
         $stats_cnt_event_pass=execSQL_row('select personal_id,count(pkey) from obj_site_events_pass where personal_id in ('.implode(',',$personal_ids).')  group by personal_id') ;
         $stats_cnt_docs=execSQL_row('select personal_id,count(pkey) from obj_site_docs where personal_id in ('.implode(',',$personal_ids).') group by personal_id') ;
         $stats_cnt_img=execSQL_row('select parent,count(pkey) from obj_site_personals_image where parent in ('.implode(',',$personal_ids).') group by parent') ;
         $stats_cnt_log=execSQL_row('select personal_id,count(pkey) from obj_site_mo_log where personal_id in ('.implode(',',$personal_ids).') group by personal_id') ;
         $stats_cnt_scan=execSQL_row('select personal_id,count(pkey) from obj_site_scan where personal_id in ('.implode(',',$personal_ids).') group by personal_id') ;
         $stats_cnt_lession=execSQL_row('select personal_id,count(pkey) from obj_site_lession_persons where personal_id in ('.implode(',',$personal_ids).') group by personal_id') ;

         foreach($recs as $i1=>$recs_personal) foreach($recs_personal as $i2=>$rec_person)
         { $recs[$i1][$i2]['cnt_mo']=$stats_cnt_mo[$rec_person['pkey']] ;
           $recs[$i1][$i2]['cnt_mess']=$stats_cnt_mess[$rec_person['pkey']] ;
           $recs[$i1][$i2]['cnt_event_pass']=$stats_cnt_event_pass[$rec_person['pkey']] ;
           $recs[$i1][$i2]['cnt_docs']=$stats_cnt_docs[$rec_person['pkey']] ;
           $recs[$i1][$i2]['cnt_img']=$stats_cnt_img[$rec_person['pkey']] ;
           $recs[$i1][$i2]['cnt_log']=$stats_cnt_log[$rec_person['pkey']] ;
           $recs[$i1][$i2]['cnt_scan']=$stats_cnt_scan[$rec_person['pkey']] ;
           $recs[$i1][$i2]['cnt_lession']=$stats_cnt_lession[$rec_person['pkey']] ;
         }
   }




   //damp_array($recs) ;
   if (_sizeof($recs)) foreach($recs as $recs_personal)
   {   $form_id='form_'.ENGINE()->generate_UID() ;
       ?><form id="<?echo $form_id?>"><h2><?echo $title?>: <?echo $recs_personal[0][$fname]?></h2><table class="basic fz_small">
       <tr><th>Главный аккаунт</th><th>Выбор</th><th>ID</th><th>Статус</th><th>Пропуск</th><th>Фото</th><th>ФИО</th><th>Организация</th><th>Отдел</th><th>Должность</th>
           <th>Группы сотрудника</th><th>Фото</th><th>МО</th><th>Cообщений</th><th>Мероприятий</th><th>Занятий</th><th>Документы</th><th>Сканы</th><th>Журнал событий</th>
       </tr>
       <?
       if (_sizeof($recs_personal)) foreach($recs_personal as $i=>$rec_personal)
       {  PERSONAL()->prepare_public_info($rec_personal) ;
          ?><tr><td><input type="radio" name="check_person" <?if (!$i) echo 'checked'?> value="<?echo $rec_personal['pkey']?>"></td>
                <td><input type="checkbox" name="person_ids[]" checked value="<?echo $rec_personal['pkey']?>"></td>
                <td><?echo $rec_personal['pkey']?></td>
                <td><?echo ($rec_personal['enabled'])? 'Действующий':'Уволен '.(($rec_personal['fired_data'])? '<br>'.date('d.m.Y',$rec_personal['fired_data']):'')?></td>
                <td><?echo $rec_personal['propusk']?></td>
                <td><?if ($rec_personal['_image_name']) echo '<img src="'.img_clone($rec_personal,'small').'">'?></td>
                <td class="left"><a href="<?echo $rec_personal['__href_window']?>" class="new_window"><?echo $rec_personal['obj_name']?></a><br><?echo $rec_personal['dr']?></td>
                <td class="left"><?echo $rec_personal['_org_name']?></td>
                <td class="left"><?echo $rec_personal['_otdel_name']?></td>
                <td class="left"><?echo $rec_personal['_work_name']?></td>
                <td><?echo $rec_personal['_groups_names']?></td>
                <td><?echo $rec_personal['cnt_img']?></td>
                <td><?echo $rec_personal['cnt_mo']?></td>
                <td><?echo $rec_personal['cnt_mess']?></td>
                <td><?echo $rec_personal['cnt_event_pass']?></td>
                <td><?echo $rec_personal['cnt_lession']?></td>
                <td><?echo $rec_personal['cnt_docs']?></td>
                <td><?echo $rec_personal['cnt_scan']?></td>
                <td><?echo $rec_personal['cnt_log']?></td>
             </tr>
          <?
       }
       ?></table><br>
         <input type="submit" class="v2" cmd="aspmo_personal/personal_merge_apply" form_id='<?echo $form_id?>' value="Объединить физических лиц"></form><br>
       <?

   }
   else {?><div class="alert">Дубликатов учетных записей физических лиц по <?echo $title?> не найдено</div><?}

 }

}



?>
