<?php
class report_meeting_info_by_orgs extends c_report
{
 var $title='Статистика прохождения мероприятия по организациям' ;
 var $view_by_space_filter=1 ;
 var $event_id ;
 var $rec_event;

// шаблон - вывод ссылками без разделитея
  function view_as_HTML($options=array())
  { $this->rec_event=EVENTS()->get_event_by_id($this->event_id)  ;
    // собирааем статистику по участникам, прошедгих собрание
    $_usl=array() ; $arr_org_names=array() ;  $arr_otdel_names=array() ; $arr=array() ;
    $_usl[]='event_id='.$this->event_id ;
    $_usl[]='(fired_data=0 or fired_data is null)';
    if (MEMBER()->personal_viewer_name) $_usl['account_personal_filter']='pkey in (select pkey from '.MEMBER()->personal_viewer_name.')' ;
    //damp_array($_usl)  ;
    $usl=implode(' and ',$_usl) ;
    $stats=execSQL('select t1._org_id,t1._org_name,t1._otdel_id,t1._otdel_name,t1.lession_status,count(distinct(t1.pkey)) as cnt
                    from view_lession_personals t1
                    where '.$usl.' 
                    group by t1._org_id,t1._otdel_id,t1.lession_status
                    order by t1._org_name,t1._otdel_name 
                     ',0,1) ;
    foreach($stats as $rec) $arr[$rec['_org_id']][$rec['_otdel_id']][$rec['lession_status']]=$rec['cnt'] ;

    // собираем статистику по участникам, подлежащим собранию
    $_usl=array() ;
    //damp_array($this->rec_event['_ext_info']) ;
    if ($this->rec_event['_ext_info']['usl_select']) $_usl[]=$this->rec_event['_ext_info']['usl_select'] ;
    else                                             $_usl[]='clss=211 and enabled=1' ;

    if (MEMBER()->personal_viewer_name) $_usl['account_personal_filter']='pkey in (select pkey from '.MEMBER()->personal_viewer_name.')' ;
    $usl=implode(' and ',$_usl) ;
    $arr_all=array() ;
    $stats_org_otdels=execSQL('select t1._org_id,t1._org_name,t1._otdel_id,t1._otdel_name,count(distinct(t1.pkey)) as cnt
                          from view_personals t1
                          where '.$usl.'   
                          group by t1._org_id,t1._otdel_id
                          order by t1._org_name,t1._otdel_name 
                           ',0,1) ;
    foreach($stats_org_otdels as $rec)
    { $arr_all[$rec['_org_id']][$rec['_otdel_id']]=$rec['cnt'] ;
      $arr_org_names[$rec['_org_id']]=$rec['_org_name'] ;
      $arr_otdel_names[$rec['_otdel_id']]=$rec['_otdel_name'] ;

    }

    $id=($options['id'])? 'id='.$options['id']:'' ;
    ?><table class="basic fz_small full auto"<?echo $id?>>
        <tr><th>Организация</th><th>Подразделение</th><th>Всего</th><th>Присутствовало на собрании</th><th>КВ</th></tr><?
          if (_sizeof($arr_all)) foreach($arr_all as $org_id=>$rec_stats_org)
          {   $i=0 ;
              if (_sizeof($rec_stats_org)) foreach($rec_stats_org as $otdel_id=>$cnt_all)
              {   $arr_status=$arr[$org_id][$otdel_id];
                  $kv=$arr_status[4]/$cnt_all*100 ;
                  ?><tr>
                      <?if (!$i) {?><td class="left" style="vertical-align:top;" rowspan="<?echo _sizeof($rec_stats_org)?>"><?echo $arr_org_names[$org_id]?></td><?}?>
                      <td class="left"><?echo $arr_otdel_names[$otdel_id]?></td>
                      <td><?echo ($cnt_all)? $cnt_all:'-'?></td>
                      <td><?echo ($arr_status[4])? $arr_status[4]:'-'?></td>
                      <td><?echo round($kv,2)?> %</td>
                  </tr><?
                  $i++ ;
              }
          }
      ?></table><?
      //damp_array($rec) ;
  }


}

?>
