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

 // локальные переменные отчета
 var $arr_mo_to_orgs=array() ;
 var $rules=array() ;
 var $res=array() ;
 var $arr_org_names=array() ;

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

  $arr_groups=ACCOUNTS()->get_arr_groups_to_cur_account() ;
  list($arr_orgs_ids,$arr_otdel_ids,$arr_works_ids)=PERSONAL()->get_org_otdels_works_to_cur_account($this->filter) ;

  ?><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('org_ids','Организация:',2,$arr_orgs_ids) ;
                             $this->filter_section_select_multiple('otdel_ids','Подразделение:',2,$arr_otdel_ids) ;
                             $this->filter_section_select_multiple('working','Должность:',2,$arr_works_ids) ;
                      ?></div><div class="row"><?
                             $this->filter_section_select('tk','Тип контроля:',2,$_SESSION['ARR_mo_tk_type'],array('use_id_plus_1'=>1)) ;
                             $this->filter_section_input_text('data_from','От:',2,array('class'=>'datetimepicker')) ;
                             $this->filter_section_input_text('data_to','До:',2,array('class'=>'datetimepicker')) ;
                             $this->filter_section_input_text('age_from','Возраст, от:',2) ;
                             $this->filter_section_input_text('age_to','Возраст, до:',2) ;
                          ?>
                      </div>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
      </div>
  <?
}

function panel_info_filter()
{ $_str=array() ; $info=array() ; $title='' ;
  //if ($this->filter['member'])                        $info['механик']=ACCOUNTS()->get_member_name_by_id($this->filter['member'] )  ;
  if ($this->filter['data_from'])                       $info['c']=$this->filter['data_from'] ;
  if ($this->filter['data_to'])                         $info['по']=$this->filter['data_to'] ;
  if ($this->filter['age_from'])                        $info['возраст c']=$this->filter['age_from'] ;
  if ($this->filter['age_to'])                          $info['возраст по']=$this->filter['age_to'] ;
  if ($this->filter['group_id'])                        $info[$GLOBALS['LS_person_group_title'].':']=ACCOUNTS()->get_group_name($this->filter['group_id'])  ;
  if ($this->filter['org_ids'])                         $info['Организация']=PERSONAL()->get_org_names_by_ids(array_keys($this->filter['org_ids']))  ;
  if ($this->filter['otdel_ids'])                       $info['Позразделение']=PERSONAL()->get_org_names_by_ids(array_keys($this->filter['otdel_ids']))  ;
  if ($this->filter['tk'])                              $info['Тип контроля']=$_SESSION['ARR_mo_tk_type'][$this->filter['tk']-1]  ;
  if (_sizeof($this->filter['working']))  foreach($this->filter['working'] as $working_id=>$value) $info['Должность'][]=_IL('IL_works')->recs[$working_id]['obj_name'] ;

   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='<div class=center>'.implode(' ',$_str).'</div>' ;
  return $title ;
}



function prepare_data($options=array())
{   $this->res=array() ; $this->arr_mo_to_orgs=array() ; $this->arr_org_names=array() ; $arr_work_names=array() ;  $cur_level=0 ;

    $this->rules=array() ;                                       // нижние значения идут через >=, верхние через <
    $this->rules['Оптимальное АД<br>ниже 120']            =array('s_min'=>0,  's_max'=>120,'d_min'=>0, 'd_max'=>80) ;
    $this->rules['Нормальное АД<br>120-129']              =array('s_min'=>120,'s_max'=>130,'d_min'=>80,'d_max'=>85) ;
    $this->rules['Высокое нормальное АД<br>130–139']      =array('s_min'=>130,'s_max'=>140,'d_min'=>85,'d_max'=>90) ;
    $this->rules['АГ I степени тяжести 140–159']          =array('s_min'=>140,'s_max'=>160,'d_min'=>90,'d_max'=>100) ;
    $this->rules['АГ II степени тяжести 160–179']         =array('s_min'=>160,'s_max'=>180,'d_min'=>100,'d_max'=>110) ;
    $this->rules['АГ III степени тяжести 180 и выше']     =array('s_min'=>180,'s_max'=>999,'d_min'=>110,'d_max'=>999) ;
    //$this->rules['Давление не измерялось']                =array('s_min'=>-1,'s_max'=>0,'d_min'=>-1,'d_max'=>0) ;
    //$this->rules['Изолированная систолическая гипертензия']       =array('s_min'=>140,                          'd_max'=>90) ;

    $options['order']='indx,c_data asc' ;
    $_usl=array() ;
    $_usl[]='model>0' ;
    if ($this->filter['data_from']) $_usl[]='c_data>='.strtotime($this->filter['data_from']) ;
    if ($this->filter['data_to']) $_usl[]='c_data<='.strtotime($this->filter['data_to']) ;
    if ($this->filter['age_from']) $_usl[]='personal_age>='.$this->filter['age_from'] ;
    if ($this->filter['age_to']) $_usl[]='personal_age<='.$this->filter['age_to'] ;
    if ($this->filter['data_to']) $_usl[]='c_data<='.strtotime($this->filter['data_to']) ;
    if ($this->filter['group_id'] and isset(ACCOUNTS()->tree[$this->filter['group_id']]))  $_usl[]='group_id in ('.ACCOUNTS()->tree[$this->filter['group_id']]->get_list_child().')' ;
    if ($this->filter['tk']==1)         $_usl[]='tk=0' ;
    if ($this->filter['tk']==2)         $_usl[]='tk=1' ;
    if ($this->filter['tk']==3)         $_usl[]='tk=2' ;
    if ($this->filter['org_ids'])       $_usl[]='personal_parent2 in ('.implode(',',array_keys($this->filter['org_ids'])).')' ;
    if ($this->filter['otdel_ids'])     $_usl[]='personal_parent3 in ('.implode(',',array_keys($this->filter['otdel_ids'])).')' ;

    if ($this->filter['dep_id'])
    { $cur_level=execSQL_value('select level from obj_site_personals where pkey='.$this->filter['dep_id']) ;
      //echo 'dep_id='.$this->filter['dep_id'].' level='.$cur_level.'<br>' ;
      $_usl[]='personal_parent'.$cur_level.'='.$this->filter['dep_id'] ;
    }
    if (_sizeof($this->filter['working'])) $_usl[]='working in ('.implode(',',array_keys($this->filter['working'])).')' ;
    //damp_array($_usl,1,-1) ;

    $usl=implode(' and ',$_usl) ;
    $recs=execSQL('select pkey,model,working,systolic,parent,personal_parent1,personal_parent2,personal_parent3,personal_parent4,personal_parent5,personal_parent6,personal_parent7,personal_parent8,personal_parent9 from view_mo_session where '.$usl,array('debug'=>0)) ;
    if (_sizeof($recs)) foreach($recs as $rec_mo)
    {  $org_id=0 ;
       if ($this->filter['dep_id']) $org_id=$rec_mo['personal_parent'.($cur_level+1)] ;
       if (!$org_id) $org_id=$rec_mo['personal_parent'.$cur_level] ;
       if (!$org_id) $org_id=$rec_mo['parent'] ;
       $work_id=$rec_mo['working'] ;
       $cnt_rules=0 ;
       $ok=0 ;
       // пропускаем МО через правила
       if (_sizeof($this->rules)) foreach($this->rules as $title=>$params)
       {   // если попали в правило
           if (!$rec_mo['systolic']) $rec_mo['systolic']=0 ;
           if ($rec_mo['systolic']>=$params['s_min'] and $rec_mo['systolic']<$params['s_max']/* and  $rec_mo['diastolic']>=$params['d_min'] and $rec_mo['diastolic']<$params['d_max']*/)
           {   // Результат на ЭСМО
               $m=$rec_mo['model'] ; //$ok=0 ;
               if ($m==1 or $m==3)                                      { $this->res[$org_id][$title]['res1']['e1']++ ; $ok=1 ; }
               if ($m==2 or $m==4 or $m==5 or $m==6 or $m==7 or $m==8)  { $this->res[$org_id][$title]['res1']['e2']++ ; $ok=1 ; }
               // Результат после прохождения врача
               if ($m==5 or $m==7)                                      { $this->res[$org_id][$title]['res2']['f1']++ ; $ok=1 ; }
               if ($m==6 or $m==8)                                      { $this->res[$org_id][$title]['res2']['f2']++ ; $ok=1 ; }
               if ($m==2 or $m==4)                                      { $this->res[$org_id][$title]['res2']['f3']++ ; $ok=1 ; }
               if ($m==9)                                               { $this->res[$org_id][$title]['res2']['f4']++ ; $ok=1 ; }
               if ($m==10)                                              { $this->res[$org_id][$title]['res2']['f5']++ ; $ok=1 ; }

               //if (!$ok) echo 'Модель не попала в отчет по '.$title.', '.$rec_mo['pkey'].'<br>' ;

               //
               $cnt_rules++ ;
               //$arr_mo[]=$rec_mo ;
               //$flag=1 ;
           }
       }
       // если измерение не попало ни в один из вариантов
       /*if (!$cnt_rules)
          {   // Результат на ЭСМО
              if ($rec_mo['status']==2) $this->res[$org_id]['Другие варианты']['res1']['y']++ ;
              if ($rec_mo['status']==3) $this->res[$org_id]['Другие варианты']['res1']['n']++ ;
              // Результат после прохождения врача
              if ($rec_mo['dopusk']==1) $this->res[$org_id]['Другие варианты']['res2']['y']++ ;
              if ($rec_mo['dopusk']==2) $this->res[$org_id]['Другие варианты']['res2']['n']++ ;
          }
       */

      if ($ok)
      {
           // количество МО работников
           $this->arr_mo_to_orgs[$org_id]++ ;
           // название организации
           if ($org_id) $this->arr_org_names[$org_id]=PERSONAL()->get_name_by_id($org_id) ; //$rec_mo['_org_name'] ;
           //категория сотрудников
           $arr_work_names[$work_id]=_IL('IL_works')->recs[$rec_mo['working']]['obj_name'] ; // $rec_member['_work_name'] ;

      } // else $recs_after[]=$rec_mo ;

    }
}

function HTML_report_body($options=array())
{

    $this->prepare_data($options) ;

    //print_2x_arr($recs_after) ;
    ?><table class="basic fz_small full auto">
         <tr><th rowspan="2">№</th>
             <th rowspan="2">Место работы</th>
             <th rowspan="2">Кол-во МО</th>
             <th rowspan="2">Категории АД<br>САД, мм.рт.ст<br>ДАД, мм.рт.ст</th>
             <th colspan="2">Результат на ЭСМО</th>
             <th colspan="5">Результат после прохождения врача</th>
             <th rowspan="2">% допущенных</th>
         </tr>
         <tr><th>Допущено<div class="comment small"><span class="nowrap">МО1+</span>&nbsp;&nbsp;
                                                    <span class="nowrap">МО2+</span>&nbsp;&nbsp;
                         </div>
             </th>
             <th>Не допущено<div class="comment small"><span class="nowrap">МО1-</span>&nbsp;&nbsp;
                                                       <span class="nowrap">МО1-МО2-</span>&nbsp;&nbsp;
                                                       <span class="nowrap">МО1-МО2-РМВ+</span>&nbsp;&nbsp;
                                                       <span class="nowrap">МО1-МО2-РМВ-</span>&nbsp;&nbsp;
                                                       <span class="nowrap">МО1-РМВ+</span>&nbsp;&nbsp;
                                                       <span class="nowrap">МО1-РМВ-</span>
                            </div>
             </th>
             <th>Допущено<div class="comment small"><span class="nowrap">МО1-МО2-РМВ+</span>&nbsp;&nbsp;
                                                    <span class="nowrap">МО1-РМВ+</span>&nbsp;&nbsp;
                         </div>
             </th>
             <th>Не допущено<div class="comment small"><span class="nowrap">МО1-МО2-РМВ-</span>&nbsp;&nbsp;
                                                       <span class="nowrap">МО1-РМВ-</span>&nbsp;&nbsp;
                            </div>
             </th>
             <th>Уклонилось<div class="comment small"><span class="nowrap">МО1-</span>&nbsp;&nbsp;
                                                      <span class="nowrap">МО1-МО2-</span>&nbsp;&nbsp;
                           </div>
             </th>
             <th><span class="nowrap">РМВ+</span></th>
             <th><span class="nowrap">РМВ-</span></th>
         </tr>
     <? //damp_array($this->arr_mo_to_orgs,1,-1);
        $i=0 ; $itog=array() ;
        if (_sizeof($this->arr_mo_to_orgs)) foreach($this->arr_mo_to_orgs as $org_id=>$mo_count)
         {   $i++ ;
             ?><tr><td rowspan="<?echo _sizeof($this->rules)+1?>"><?echo $i?></td>
                 <td class="left" rowspan="<?echo _sizeof($this->rules)+1?>">
                     <? //if (!$org_id) $org_id=1 ;
                        $childs_orgs_cnt=execSQL_value('select count(pkey) from obj_site_personals where parent='.$org_id.' and enabled=1 and clss in (207,208)') ;
                        if ($childs_orgs_cnt){?><a  target=_blank href="<?echo $this->get_filter_to_params(array('dep_id'=>$org_id))?>"><?echo $this->arr_org_names[$org_id]?></a><?}
                        else echo $this->arr_org_names[$org_id] ;
                     ?>
                 </td>
                 <td rowspan="<?echo _sizeof($this->rules)+1?>"><a  target=_blank href="<?echo $this->get_filter_to_params(array('dep_id'=>$org_id,'model_all'=>1,'_model'=>'(1;3)'),array('url_report'=>'/cab/reports/report_mo_sess/'))?>"><?echo $mo_count ; $itog['mo_count']+=$mo_count ; ?></a></td>
                 <?  $sum=array() ; $line=0 ;
                     if (_sizeof($this->rules)) foreach($this->rules as $title=>$params)
                     { if ($line) echo '<tr>' ;
                       $rec_work=$this->res[$org_id][$title] ;
                       ?><td class="left"><? echo $title ;?></td>
                         <td><? if ($rec_work['res1']['e1']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(1;3)','min_s'=>$params['s_min'],'max_s'=>$params['s_max']),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$rec_work['res1']['e1'].'</a>' ; $sum['e1']+=$rec_work['res1']['e1'] ; ?></td>
                         <td><? if ($rec_work['res1']['e2']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(2;4;5;6;7;8)','min_s'=>$params['s_min'],'max_s'=>$params['s_max']),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$rec_work['res1']['e2'].'</a>' ; $sum['e2']+=$rec_work['res1']['e2'] ; ?></td>
                         <td><? if ($rec_work['res2']['f1']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(5;7)','min_s'=>$params['s_min'],'max_s'=>$params['s_max']),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$rec_work['res2']['f1'].'</a>' ; $sum['f1']+=$rec_work['res2']['f1'] ; ?></td>
                         <td><? if ($rec_work['res2']['f2']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(6;8)','min_s'=>$params['s_min'],'max_s'=>$params['s_max']),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$rec_work['res2']['f2'].'</a>' ; $sum['f2']+=$rec_work['res2']['f2'] ; ?></td>
                         <td><? if ($rec_work['res2']['f3']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(2;4)','min_s'=>$params['s_min'],'max_s'=>$params['s_max']),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$rec_work['res2']['f3'].'</a>' ; $sum['f3']+=$rec_work['res2']['f3'] ; ?></td>
                         <td><? if ($rec_work['res2']['f4']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(9)','min_s'=>$params['s_min'],'max_s'=>$params['s_max']),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$rec_work['res2']['f4'].'</a>' ; $sum['f4']+=$rec_work['res2']['f4'] ; ?></td>
                         <td><? if ($rec_work['res2']['f5']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(10)','min_s'=>$params['s_min'],'max_s'=>$params['s_max']),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$rec_work['res2']['f5'].'</a>' ; $sum['f5']+=$rec_work['res2']['f5'] ; ?></td>
                         <td><? echo round((($rec_work['res1']['e1']+$rec_work['res2']['f1']+$rec_work['res2']['f3']+$rec_work['res2']['f4'])/$mo_count*100),1).' %' ; ?></td>
                        </tr>
                       <?
                       $line++ ;
                     }
                    ?><tr class="subitog">
                      <td class="left">ИТОГО</td>
                      <td><? if ($sum['e1']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(1;3)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$sum['e1'].'</a>' ; $itog['e1']+=$sum['e1'] ; ?></td>
                      <td><? if ($sum['e2']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(2;4;5;6;7;8)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$sum['e2'].'</a>' ; $itog['e2']+=$sum['e2'] ; ?></td>
                      <td><? if ($sum['f1']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(5;7)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$sum['f1'].'</a>' ; $itog['f1']+=$sum['f1'] ; ?></td>
                      <td><? if ($sum['f2']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(6;8)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$sum['f2'].'</a>' ; $itog['f2']+=$sum['f2'] ; ?></td>
                      <td><? if ($sum['f3']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(2;4)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$sum['f3'].'</a>' ; $itog['f3']+=$sum['f3'] ; ?></td>
                      <td><? if ($sum['f4']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(9)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$sum['f4'].'</a>' ; $itog['f4']+=$sum['f4'] ; ?></td>
                      <td><? if ($sum['f5']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('dep_id'=>$org_id,'model'=>'(10)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$sum['f5'].'</a>' ; $itog['f5']+=$sum['f5'] ; ?></td>
                      <td><? echo round((($sum['e1']+$sum['f1']+$sum['f3']+$sum['f4'])/$mo_count*100),1).' %' ; ?></td>
                      </tr>
                    <?
                 ?>
           <?
         }
         if (_sizeof($this->arr_mo_to_orgs))
         {?><tr class="itog">
             <td></td>
             <td>ИТОГО</td>
             <td><?echo $itog['mo_count']?></td>
             <td></td>
             <td><?if ($itog['e1']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('model'=>'(1;3)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$itog['e1'].'</a>'?></td>
             <td><?if ($itog['e2']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('model'=>'(2;4;5;6;7;8)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$itog['e2'].'</a>'?></td>
             <td><?if ($itog['f1']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('model'=>'(5;7)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$itog['f1'].'</a>'?></td>
             <td><?if ($itog['f2']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('model'=>'(6;8)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$itog['f2'].'</a>'?></td>
             <td><?if ($itog['f3']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('model'=>'(2;4)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$itog['f3'].'</a>'?></td>
             <td><?if ($itog['f4']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('model'=>'(9)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$itog['f4'].'</a>'?></td>
             <td><?if ($itog['f5']) echo '<a  target=_blank href="'.$this->get_filter_to_params(array('model'=>'(10)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$itog['f5'].'</a>'?></td>
             <td><? echo round((($itog['e1']+$itog['f1']+$itog['f3']+$itog['f4'])/$itog['mo_count']*100),1).' %' ; ?></td>
         </tr><?}?>
       </table>
      <?
    if (!_sizeof($this->arr_mo_to_orgs)) {?><div class="alert" style="text-align:center">Данных по Вашему запросу не найдено</div><?}
  }

  function CSV_report_body($options=array())
  {
    $this->prepare_data($options) ;
      $i=$options['from'] ;  $itog=array() ;
    if (_sizeof($this->arr_mo_to_orgs)) foreach($this->arr_mo_to_orgs as $org_id=>$mo_count)
    {     $data=array() ;
          $data['№']=$i+1 ;
          $data['Организация']=$this->arr_org_names[$org_id] ;
          $data['Число МО']=$mo_count ;                                 $itog['mo_count']+=$mo_count ;
          $sum=array() ;
          if (_sizeof($this->rules)) foreach($this->rules as $title=>$params)
             { $rec_work=$this->res[$org_id][$title] ;
               $data['№']=$i+1 ;
               $data['Категории АД']=$title ;
               $data['ЭСМО-допущено']=$rec_work['res1']['e1'] ;         $sum['e1']+=$rec_work['res1']['e1'] ;
               $data['ЭСМО-не допущено']=$rec_work['res1']['e2'] ;      $sum['e2']+=$rec_work['res1']['e2'] ;
               $data['РМВ-допущено']=$rec_work['res2']['f1'] ;          $sum['f1']+=$rec_work['res2']['f1'] ;
               $data['РМВ-не допущено']=$rec_work['res2']['f2'] ;       $sum['f2']+=$rec_work['res2']['f2'] ;
               $data['Уклонилось']=$rec_work['res2']['f3'] ;            $sum['f3']+=$rec_work['res2']['f3'] ;
               $data['РМВ+']=$rec_work['res2']['f4'] ;                  $sum['f4']+=$rec_work['res2']['f4'] ;
               $data['РМВ-']=$rec_work['res2']['f5'] ;                  $sum['f5']+=$rec_work['res2']['f5'] ;
               $data['процент допущенных']=round((($rec_work['res1']['e1']+$rec_work['res2']['f1']+$rec_work['res2']['f3']+$rec_work['res2']['f4'])/$mo_count*100),1).' %' ;
               $this->put_data_to_CSV($data,$i) ;
               $i++ ;
             }
          $data['№']=$i+1 ;
          $data['Категории АД']='ИТОГО' ;
          $data['ЭСМО-допущено']=$sum['e1'];                            $itog['e1']+=$sum['e1'] ;
          $data['ЭСМО-не допущено']=$sum['e2'];                         $itog['e2']+=$sum['e2'] ;
          $data['РМВ-допущено']=$sum['f1'];                             $itog['f1']+=$sum['f1'] ;
          $data['РМВ-не допущено']=$sum['f2'];                          $itog['f2']+=$sum['f2'] ;
          $data['Уклонилось']=$sum['f3'];                               $itog['f3']+=$sum['f3'] ;
          $data['РМВ+']=$sum['f4'];                                     $itog['f4']+=$sum['f4'] ;
          $data['РМВ-']=$sum['f5'];                                     $itog['f5']+=$sum['f5'] ;
          $data['процент допущенных']=round((($sum['e1']+$sum['f1']+$sum['f3']+$sum['f4'])/$mo_count*100),1).' %' ;
          $this->put_data_to_CSV($data,$i) ;
          $i++ ;
    }

    $data['№']=$i+1 ;
    $data['Организация']='ИТОГО' ;
    $data['Число МО']=$itog['mo_count'] ;
    $data['Категории АД']='' ;
    $data['ЭСМО-допущено']=$itog['e1'];
    $data['ЭСМО-не допущено']=$itog['e2'];
    $data['РМВ-допущено']=$itog['f1'];
    $data['РМВ-не допущено']=$itog['f2'];
    $data['Уклонилось']=$itog['f3'];
    $data['РМВ+']=$itog['f4'];
    $data['РМВ-']=$itog['f5'];
    $data['процент допущенных']=round((($itog['e1']+$itog['f1']+$itog['f3']+$itog['f4'])/$itog['mo_count']*100),1).' %' ;
    $this->put_data_to_CSV($data,$i) ;

  }

}

?>
