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

// шаблон - вывод ссылками без разделитея
  function view_as_HTML($options=array())
  { $debug=(_IS_SYSTEM_IP)? 0:0;
    $_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[]='personal_id in (select pkey from '.MEMBER()->personal_viewer_name.')' ;


    $usl=implode(' and ',$_usl) ;

    $stats=execSQL('select t1._org_id,t1._org_name,t1._otdel_id,t1._otdel_name,t1.event_status,count(t1.pkey) as cnt
                    from view_event_personals t1
                    where '.$usl.' 
                    group by t1._org_id,t1._otdel_id,t1.event_status
                    order by t1._org_name,t1._otdel_name 
                     ',0,1) ;
    foreach($stats as $rec)
    { $arr[$rec['_org_id']][$rec['_otdel_id']][$rec['event_status']]=$rec['cnt'] ;
      $arr_org_names[$rec['_org_id']]=$rec['_org_name'] ;
      $arr_otdel_names[$rec['_otdel_id']]=$rec['_otdel_name'] ;
    }
    //$arr=group_by_field('parent2',$stats,'event_status') ;
    //damp_array($arr) ;
    //damp_array($arr_org_names) ;
    //damp_array($arr_otdel_names) ;


    $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><th>Проходят</th><th>Прошли</th><th>КВ</th></tr><?
          if (_sizeof($arr)) foreach($arr as $org_id=>$rec_stats_org)
          {   $i=0 ;
              if (_sizeof($rec_stats_org)) foreach($rec_stats_org as $otdel_id=>$arr_status)
              {   $cnt_all=0 ;
                  if (_sizeof($arr_status)) foreach($arr_status as $status=>$cnt) $cnt_all+=$cnt ;
                  $cnt_in=$arr_status[1] ;
                  $cnt_start=$arr_status[2]+$arr_status[3] ;
                  $cnt_process=$arr_status[4]+$arr_status[5]+$arr_status[6] ;
                  $cnt_end=$arr_status[7]+$arr_status[8];
                  $kv=$cnt_end/$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 ($cnt_in)? $cnt_in:'-'?></td>
                      <td><?echo ($cnt_start)? $cnt_start:'-'?></td>
                      <td><?echo ($cnt_process)? $cnt_process:'-'?></td>
                      <td><?echo ($cnt_end)? $cnt_end:'-'?></td>
                      <td><?echo round($kv,2)?> %</td>
                  </tr><?
                  $i++ ;
              }
          }
      ?></table><?
      //damp_array($rec) ;
  }


}

?>
