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

  function view_as_HTML($options=array())
  { $_usl=array() ;
    $_usl[]='event_id='.$this->event_id ;
    if ($this->usl_select_event_pass) $_usl[]=$this->usl_select_event_pass ;
    if ($this->usl_select_personal) $_usl[]=$this->usl_select_personal ;

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

    $stats=execSQL('select t1._org_id,t1._org_name,t1.event_status,count(t1.pkey) as cnt
                    from view_event_personals t1
                    left join '.MEMBER()->personal_viewer_name.' av on av.pkey=t1.pkey
                    where '.$usl.' and not av.pkey is null 
                    group by t1._org_id,t1.event_status 
                    order by t1._org_name 
                    ',0,1) ;
    $arr=group_by_field('_org_id',$stats,'event_status') ;

    $id=($options['id'])? 'id='.$options['id']:'' ;
    ?><table class="basic fz_small full auto"<?echo $id?>>
        <tr><th style="text-align:left;">Организация: <input type="text" id="table_fast_search_in_td"><th>Всего</th><th>Документ выдан</th><th>Сдача просрочена</th><th>Документ сдан</th><th>КВ</th></tr><?
          if (_sizeof($arr)) foreach($arr as $org_id=>$rec_stats) if ($org_id>0)
          {   $cnt_all=0 ;
              foreach($rec_stats as $rec_status) $cnt_all+=$rec_status['cnt'] ;
              $cnt_start=$rec_stats[2]['cnt']+$rec_stats[3]['cnt'] ;
              $cnt_block=$rec_stats[4]['cnt'];
              $cnt_end=$rec_stats[5]['cnt'];
              $kv=$cnt_end/$cnt_all*100 ;
              ?><tr class="item">
                  <td class="left"><?echo $rec_status['_org_name']?></td>
                  <td><?echo ($cnt_all)? $cnt_all:'-'?></td>
                  <td><?echo ($cnt_start)? $cnt_start:'-'?></td>
                  <td><?echo ($cnt_block)? $cnt_block:'-'?></td>
                  <td><?echo ($cnt_end)? $cnt_end:'-'?></td>
                  <td><?echo round($kv,2)?> %</td>
              </tr><?
          }
      ?></table><?
  }


}

?>
