<?php
class report_magnit_2 extends c_report
{
 var $title='Суточный отчет по отстраненным' ;
 var $filter=array() ;
 //var $PDF_master='TCPDF' ;  // короткие отчеты выгружвать через обычный  PDF

 function setting($options=array())
{
  $now=getdate() ;
  if (!$this->filter['data_from']) $this->filter['data_from']=date('d.m.Y',mktime(0,0,0,$now['mon']-1,1,$now['year']));
  if (!$this->filter['data_to'])   $this->filter['data_to']=date('d.m.Y',mktime(0,0,0,$now['mon'],1,$now['year'])-1);

  $arr_groups=ACCOUNTS()->get_arr_groups_to_cur_account() ;
  $arr_orgs_ids=PERSONAL()->get_arr_orgs_to_cur_account() ;

  ?><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('group_id',$GLOBALS['LS_person_group_title'].':',2,$arr_groups) ;
                            $this->filter_section_select_multiple('atp_ids','Организация - АТП:',3,$arr_orgs_ids,array('match'=>'/ООО \"Сельта\"(.+?)/is')) ;
                            $this->filter_section_select_multiple('rc_ids','Организация - РЦ:',3,$arr_orgs_ids,array('match'=>'/АО \"Тандер\"(.+?)РЦ/is')) ;
                            $this->filter_section_input_text('data_from','От:',2,array('class'=>'datetimepicker')) ;
                            $this->filter_section_input_text('data_to','До:',2,array('class'=>'datetimepicker')) ;
                       ?>
                       </div>
                   </fieldset>
                   <?$this->panel_buttons();?>
               </form>
   </div>
  <?
}

function panel_info_filter()
 { $_str=array() ; $info=array() ; $title='' ;
   if (!$this->filter['group_id']) $this->filter['group_id']=MEMBER()->cur_group_id ;

   if ($this->filter['data_from'])                       $info['c']=$this->filter['data_from'] ;
   if ($this->filter['data_to'])                         $info['по']=$this->filter['data_to'] ;
   if ($this->filter['atp_ids'])                         $info['АТП']=PERSONAL()->get_org_names_by_ids(array_keys($this->filter['atp_ids']))  ;
   if ($this->filter['rc_ids'])                          $info['РЦ']=PERSONAL()->get_org_names_by_ids(array_keys($this->filter['rc_ids']))  ;
   if ($this->filter['group_id'])                        $info['Группа']=ACCOUNTS()->get_member_name_by_id($this->filter['group_id'])  ;
   if ($this->filter['org_mode']=='atp')                 $info['Тип подразделений']='АТП' ;
   if ($this->filter['org_mode']=='rc')                  $info['Тип подразделений']='РЦ' ;
   if (_sizeof($info)) foreach($info as $title=>$value)   if (is_array($value)) $_str[]=$title.': <strong>'.implode(', ',$value).'</strong>' ; else $_str[]=$title.': <strong>'.$value.'</strong>' ;
   if (_sizeof($_str)) $title='<p class=center>'.implode(' ',$_str).'</p>' ;
   return $title ;

 }


 function HTML_report_body($options=array())
{   $res=array() ;

    if ($this->filter['data_from']) $options['data_from']=$this->filter['data_from'] ;
    if ($this->filter['data_to'])   $options['data_to']=$this->filter['data_to'] ;
    if ($this->filter['group_id'])  $options['group_id']=$this->filter['group_id'] ;
    if ($this->filter['atp_ids'])    $options['org_ids']=implode(',',array_keys($this->filter['atp_ids'])) ;
    if ($this->filter['rc_ids'])    $options['org_ids']=implode(',',array_keys($this->filter['rc_ids'])) ;
    // далее в ссылках надо передвать не отдельные параметры  atp_ids и  rc_ids а единй массив органиазций org_ids
    if (!_sizeof($this->filter['atp_ids'])) $this->filter['atp_ids']=array() ;
    if (!_sizeof($this->filter['rc_ids'])) $this->filter['rc_ids']=array() ;
    $this->filter['org_ids']=$this->filter['atp_ids']+$this->filter['rc_ids'] ;
    unset($this->filter['atp_ids'],$this->filter['rc_ids']) ;

    //$org_ids=array() ;
    //if ($this->filter['org_mode']=='atp') $org_ids=execSQL_line("select pkey from obj_site_personals where obj_name like 'ООО \"Сельта\"%'") ;
    //if ($this->filter['org_mode']=='rc')  $org_ids=execSQL_line("select pkey from obj_site_personals where obj_name like 'АО \"Тандер\"%'") ;
    //if (_sizeof($org_ids))  $options['org_ids']=implode(',',$org_ids) ;


    //$options['order']='t4.indx,t3.indx,c_data asc' ;
    $options['order']='indx,c_data asc' ;
    $options['no_public_info']=1 ;
    $options['dopusk_arr']=array(1=>1,2=>1,3=>1) ;
    $options['only_model']='all' ;

    //list($recs,$stats)=ESMO()->get_mo_recs($options) ;
    $usl=$usl=ESMO()->get_usl_select_mo_by_filter($options); ;
    $recs=execSQL('select pkey,member_id,group_id,dopusk,res_a,res_s,res_d,res_p from view_mo_session where '.$usl.' order by pkey') ;
    //print_2x_arr($recs) ;
    $arr_groups=execSQL_row('select pkey,obj_name from obj_site_account where clss=206 order by obj_name') ;


    if (_sizeof($recs)) foreach($recs as $rec_mo)
    {  if ($rec_mo['member_id'])
       {   $group_id=$rec_mo['group_id'] ;
           $personal_id=$rec_mo['member_id'] ;
           // Результат после прохождения врача
           if ($rec_mo['dopusk']) $res[$group_id]['cnt_all_uni'][$personal_id]++ ;
           if ($rec_mo['dopusk']==2 or $rec_mo['dopusk']==3)
           { // число отстраненных водителей (уник)
             $res[$group_id]['cnt_denited_uni'][$personal_id]++ ;
             // число отстраненний
             $res[$group_id]['cnt_denited']++ ;
             // считаем причины отстраненний по приоритету
             if ($rec_mo['res_a']>1)                                $res[$group_id]['a']++ ;
             else if ($rec_mo['res_s']>1 or $rec_mo['res_d']>1)     $res[$group_id]['sd']++ ;
             else if ($rec_mo['res_p']>1)                           $res[$group_id]['p']++ ;
             else                                                   $res[$group_id]['other']++ ;
           }
       }
    }
    //damp_array($res,1,-1) ;
    //damp_array($arr_groups,1,-1) ;

    ?><table class="basic fz_small auto" style="margin:0 auto;">
         <tr><th rowspan="2">АПТ</th>
             <th rowspan="2">Кол-во, согласно штатному расписанию</th>
             <th rowspan="2">Кол-во осмотренных (уникальных)</th>
             <th rowspan="2">Кол-во отстранненных (уникальных)</th>
             <th rowspan="2">Кол-во отстраннений (допуск запрещен)</th>
             <th colspan="4">Причины</th>
             <th rowspan="2">% отстраненных от ШР</th>
             <th rowspan="2">% отстраненных от общего числа осмотренных</th>
         </tr>
         <tr><th>Давление</th>
             <th>Пульс</th>
             <th>Алкогольное/наркотическое<br>опьянение</th>
             <th>Прочие</th>
         </tr>
     <? if (_sizeof($arr_groups)) foreach($arr_groups as $group_id=>$name)
        //if (_sizeof($res)) foreach($res as $group_id=>$rec)
         {   //echo '$group_id='.$group_id.'<br>' ;
             $rec=$res[$group_id] ;
             //print_r($rec) ; echo '<br>' ;
             ?><tr>
                 <td class="left nowrap"><?echo $arr_groups[$group_id]?></td>
                 <td></td>
                 <td><?echo (_sizeof($rec['cnt_all_uni']))? _sizeof($rec['cnt_all_uni']):'-'?></td>
                 <td><?echo (_sizeof($rec['cnt_denited_uni']))? _sizeof($rec['cnt_denited_uni']):'-';?></td>
                 <td><?echo $rec['cnt_denited']?></td>
                 <td><a target=_blank href="<?echo $this->get_filter_to_params(array('group_id'=>$group_id,'d'=>1,'no_a'=>1,'dopusk'=>array(3,4)),array('url_report'=>'/cab/reports/report_mo3/')) ;?>"><?echo $rec['sd']?></a></td>
                 <td><a target=_blank href="<?echo $this->get_filter_to_params(array('group_id'=>$group_id,'p'=>1,'no_a'=>1,'dopusk'=>array(3,4)),array('url_report'=>'/cab/reports/report_mo3/')) ;?>"><?echo $rec['p']?></a></td>
                 <td><a target=_blank href="<?echo $this->get_filter_to_params(array('group_id'=>$group_id,'a'=>1,'dopusk'=>array(3,4)),array('url_report'=>'/cab/reports/report_mo3/')) ;?>"><?echo $rec['a']?></a></td>
                 <td><a target=_blank href="<?echo $this->get_filter_to_params(array('group_id'=>$group_id,'other'=>1,'dopusk'=>array(3,4)),array('url_report'=>'/cab/reports/report_mo3/')) ;?>"><?echo $rec['other']?></a></td>
                 <td></td>
                 <td><?echo (_sizeof($rec['cnt_all_uni']))? round(_sizeof($rec['cnt_denited_uni'])/_sizeof($rec['cnt_all_uni'])*100).'%':'-'?></td>
               </tr>
              <?
         }
     ?></table><?
   }



}

?>
