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

 function init($options=array())
 {   //trace() ;
 }

 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() ;
  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_input_text('name','ФИО:',3,array('auto_complete'=>"autocomplette_personal_name")) ;
                            $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_input_text('ag_min','Минимальное число МО для АГ:',2) ;
                            $this->filter_section_input_text('min_proc','Пороговый процент:',2) ;
                            $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['data_from'])                       $info['c']=$this->filter['data_from'] ;
   if ($this->filter['data_to'])                         $info['по']=$this->filter['data_to'] ;
   if ($this->filter['org_id'])                          $info['организация']=PERSONAL()->get_name_by_id($this->filter['org_id']) ;
   if ($this->filter['org_name'])                        $info['название организации']=$this->filter['org_name']  ;
   if ($this->filter['org_ids'])                         $info['организация']=PERSONAL()->get_org_names_by_ids(array_keys($this->filter['org_ids']))  ;
   if ($this->filter['group_id'])                        $info[$GLOBALS['LS_person_group_title']]=ACCOUNTS()->get_member_name_by_id($this->filter['group_id'])  ;
   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 prepare_data($options=array())
{ if (!$this->filter['group_id']) $this->filter['group_id']=MEMBER()->cur_group_id ;

  $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']='group_id in ('.ACCOUNTS()->get_childs_id($this->filter['group_id']).')' ;
    if ($this->filter['org_id'])    $options['org_id']=$this->filter['org_id'] ;
    if ($this->filter['org_ids'])  $options['org_ids']=implode(',',array_keys($this->filter['org_ids'])) ;

    if ($this->filter['org_name'])
    {   $arr=explode(' ',$this->filter['org_name']) ; $_usl=array() ;
        if (_sizeof($arr)) foreach($arr as $name) $_usl[]='obj_name like "%'.$name.'%"' ;
        $usl_select_org='('.implode(' and ',$_usl).')' ;
        $org_ids=execSQL_line('select pkey from obj_site_personals where clss=207 and enabled=1 and '.$usl_select_org.' order by obj_name') ;
        $_usl=array() ;
        if (_sizeof($org_ids))
        { foreach($org_ids as $org_id) $_usl[]='personal_parent2='.$org_id ;
          $options['use_usl']='('.implode(' or ',$_usl).')' ;
        }
        else $options['use_usl']='false' ;
    }

    $rules['ag1']=array('s_min'=>140,'s_max'=>160,'title'=>'АГ I степени тяжести<br>140–159') ;
    $rules['ag2']=array('s_min'=>160,'s_max'=>180,'title'=>'АГ II степени тяжести<br>160–179') ;
    $rules['ag3']=array('s_min'=>180,'s_max'=>10000,'title'=>'АГ III степени тяжести<br>180 и выше') ;

    //$options['order']='t4.indx,t3.indx,c_data asc' ;

    $options['order']='indx,c_data asc' ;
    $options['no_public_info']=1 ;
    $options['view_name']='view_mo_session22' ;
    $options['fields']='pkey,personal_id,member_id,model,group_id,dopusk,systolic,res_a,res_p,res_t,question' ;
    $options['only_model']='1,2,3,4,5,6,7,8,9,10' ;
    $options['dopusk']='1,2,3' ;

    list($recs,$stats)=ESMO()->get_mo_recs($options) ;
    $res2=array() ;
    if (_sizeof($recs)) foreach($recs as $rec_mo) if ($rec_mo['personal_id'] and $rec_mo['model'])
    {  $group_id=$rec_mo['group_id'] ;
       $personal_id=$rec_mo['member_id'] ;
       // считаем только по допускам
       //if ($rec_mo['dopusk']==1 or $rec_mo['dopusk']==2)
       {    //$res[$group_id][$personal_id]['dopusk']['all']++ ;
            $res[$group_id][$personal_id]['cnt_mo']++ ;
            $res2[$personal_id]['cnt_mo']++ ;

           if ($rec_mo['systolic']>=$rules['ag3']['s_min'] and $rec_mo['systolic']<$rules['ag3']['s_max'])      { $res[$group_id][$personal_id]['ag3']++ ; $res2[$personal_id]['ag_cnt']++ ; $res2[$personal_id]['ag3']++ ; }
           elseif ($rec_mo['systolic']>=$rules['ag2']['s_min'] and $rec_mo['systolic']<$rules['ag2']['s_max'])  { $res[$group_id][$personal_id]['ag2']++ ; $res2[$personal_id]['ag_cnt']++ ; $res2[$personal_id]['ag2']++ ;}
           elseif ($rec_mo['systolic']>=$rules['ag1']['s_min'] and $rec_mo['systolic']<$rules['ag1']['s_max'])  { $res[$group_id][$personal_id]['ag1']++ ; $res2[$personal_id]['ag_cnt']++ ; $res2[$personal_id]['ag1']++ ;}
           else                                                                                                 { $res[$group_id][$personal_id]['ag_norm']++ ; $res2[$personal_id]['ag_norm']++ ; }

           if ($rec_mo['res_a']>1) { $res[$group_id][$personal_id]['alco']++ ; $res2[$personal_id]['alco']++ ; }
           if ($rec_mo['res_p']>1) { $res[$group_id][$personal_id]['puls']++ ; $res2[$personal_id]['puls']++ ; }
           if ($rec_mo['res_t']>1) { $res[$group_id][$personal_id]['temp']++ ; $res2[$personal_id]['temp']++ ; }
           if ($rec_mo['question']) { $res[$group_id][$personal_id]['other']++ ; $res2[$personal_id]['other']++ ; }
       }

       // отстранено по ССЗ
       //if ($rec_mo['question']==1)   $res[$group_id][$personal_id]['res4']++ ;
       // отстранено по другим причинам
       //if ($rec_mo['alko']==0 and $rec_mo['question']==0 and $rec_mo['dopusk']==2)   $res[$group_id][$personal_id]['res5']++ ;
    }


   //damp_array($res) ;
//    damp_array($res2) ;
   $itog=array() ;

    if (_sizeof($res2)) foreach($res2 as $personal_id=>$info)
    {
      if ($info['ag3']>0) { $res2[$personal_id]['ag2']=0 ; $res2[$personal_id]['ag1']=0 ; $res2[$personal_id]['ag_norm']=0 ; ;}
      elseif ($info['ag2']>0) { $res2[$personal_id]['ag1']=0 ; $res2[$personal_id]['ag_norm']=0 ; ; }
      elseif ($info['ag1']>0) { $res2[$personal_id]['ag_norm']=0 ; }

    }

//damp_array($res2) ;


    if (_sizeof($res2)) foreach($res2 as $personal_id=>$info)
    {
        $ag_proc=round($info['ag_cnt']*100/$info['cnt_mo']) ;
       $res2[$personal_id]['ad_proc']=$ag_proc ;
       if ($info['cnt_mo']<$this->filter['ag_min'] or $ag_proc<$this->filter['min_proc'])
       {
        $res2[$personal_id]['no_view_ag']=1 ;
        $itog['ag_norm']++ ;
        }
       else
       {

         if ($info['ag3']>0) { $itog['ag3']++ ;  ;}
         elseif ($info['ag2']>0) { $itog['ag2']++ ; }
         elseif ($info['ag1']>0) { $itog['ag1']++ ; }
         elseif ($info['ag_norm']>0) $itog['ag_norm']++ ;
    }
       $itog['personal_cnt']++ ;
       if ($info['alco']) $itog['alco']++ ;
       if ($info['puls']) $itog['puls']++ ;
       if ($info['temp']) $itog['temp']++ ;
       if ($info['other']) $itog['other']++ ;

    }


  // damp_array($res) ; damp_array($res2) ;
   //damp_array($itog) ;

     //$itog=array() ;
      $arr_pers_ids=array() ;
        if (_sizeof($res)) foreach($res as $group_id=>$arr_group_personal)
         {
             $sum=array() ; $arr_pers_ids2=array() ;
             if (_sizeof($arr_group_personal)) foreach($arr_group_personal as $personal_id=>$arr_personal_info)
             {
                 $sum['personal_cnt']++ ;
                 $cnt_ag_all=$arr_personal_info['ag1']+$arr_personal_info['ag2']+$arr_personal_info['ag3'] ;
                 $ag_proc=round($res2[$personal_id]['ag_cnt']*100/$res2[$personal_id]['cnt_mo']) ;


                if (!$res2[$personal_id]['no_view_ag'])
                {   if      ($arr_personal_info['ag3'])   { $sum['ag3']++ ; $arr_pers_ids['ag3'][$personal_id]=$personal_id ; $arr_pers_ids2['ag3'][$personal_id]=$personal_id ; }
                    else if ($arr_personal_info['ag2'] and !$res2[$personal_id]['ag3'])   { $sum['ag2']++ ; $arr_pers_ids['ag2'][$personal_id]=$personal_id ; $arr_pers_ids2['ag2'][$personal_id]=$personal_id ; }
                    else if ($arr_personal_info['ag1']  and !$res2[$personal_id]['ag3']  and !$res2[$personal_id]['ag2'])   { $sum['ag1']++ ; $arr_pers_ids['ag1'][$personal_id]=$personal_id ; $arr_pers_ids2['ag1'][$personal_id]=$personal_id ; }
                    else                                    $sum['ag_norm']++ ;
                } else $sum['ag_norm']++ ;



                  if ($arr_personal_info['alco'])           { $sum['alco']++ ;  $arr_pers_ids['alco'][]=$personal_id ; $arr_pers_ids2['alco'][]=$personal_id ; }
                  if ($arr_personal_info['puls'])           { $sum['puls']++ ;  $arr_pers_ids['puls'][]=$personal_id ; $arr_pers_ids2['puls'][]=$personal_id ; }
                  if ($arr_personal_info['temp'])           { $sum['temp']++ ;  $arr_pers_ids['temp'][]=$personal_id ; $arr_pers_ids2['temp'][]=$personal_id ; }
                  if ($arr_personal_info['other'])          { $sum['other']++ ; $arr_pers_ids['other'][]=$personal_id ; $arr_pers_ids2['other'][]=$personal_id ; }
             }
             $rec_data=array() ;
             $rec_data[1]=ACCOUNTS()->get_group_name($group_id) ;
             $rec_data[2]=$sum['personal_cnt'] ;
             $rec_data[3]=$sum['ag_norm'] ;
             $rec_data[4]=array('value'=>$sum['ag1'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids2['ag1']),array('url_report'=>'/cab/reports/report_personals/'))) ;
             $rec_data[5]=array('value'=>$sum['ag2'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids2['ag2']),array('url_report'=>'/cab/reports/report_personals/'))) ;
             $rec_data[6]=array('value'=>$sum['ag3'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids2['ag3']),array('url_report'=>'/cab/reports/report_personals/'))) ;
             $rec_data[7]=array('value'=>$sum['puls'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids2['puls']),array('url_report'=>'/cab/reports/report_personals/'))) ;
             $rec_data[8]=array('value'=>$sum['alco'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids2['alco']),array('url_report'=>'/cab/reports/report_personals/'))) ;
             $rec_data[9]=array('value'=>$sum['temp'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids2['temp']),array('url_report'=>'/cab/reports/report_personals/'))) ;
             $rec_data[10]=array('value'=>$sum['other'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids2['other']),array('url_report'=>'/cab/reports/report_personals/'))) ;
             $data[]=$rec_data ;

            // $itog['personal_cnt']+=$sum['personal_cnt'] ;
            // $itog['ag_norm']+=$sum['ag_norm'] ;
            // $itog['ag1']+=$sum['ag1'] ;
            // $itog['ag2']+=$sum['ag2'] ;
            // $itog['ag3']+=$sum['ag3'] ;
            // $itog['alco']+=$sum['alco'] ;
            // $itog['puls']+=$sum['puls'] ;
            // $itog['temp']+=$sum['temp'] ;
            // $itog['other']+=$sum['other'] ;
         }
        $rec_data=array() ;
        $rec_data[1]='ИТОГО' ;
        $rec_data[2]=$itog['personal_cnt'] ;
        $rec_data[3]=$itog['ag_norm'] ;
        $rec_data[4]=array('value'=>$itog['ag1'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids['ag1']),array('url_report'=>'/cab/reports/report_personals/'))) ;
        $rec_data[5]=array('value'=>$itog['ag2'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids['ag2']),array('url_report'=>'/cab/reports/report_personals/'))) ;
        $rec_data[6]=array('value'=>$itog['ag3'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids['ag3']),array('url_report'=>'/cab/reports/report_personals/'))) ;
        $rec_data[7]=array('value'=>$itog['puls'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids['puls']),array('url_report'=>'/cab/reports/report_personals/'))) ;
        $rec_data[8]=array('value'=>$itog['alco'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids['alco']),array('url_report'=>'/cab/reports/report_personals/'))) ;
        $rec_data[9]=array('value'=>$itog['temp'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids['temp']),array('url_report'=>'/cab/reports/report_personals/'))) ;
        $rec_data[10]=array('value'=>$itog['other'],'url'=>$this->get_filter_to_params(array('ids'=>$arr_pers_ids['other']),array('url_report'=>'/cab/reports/report_personals/'))) ;
        $data['subitog1']=$rec_data ;

//damp_array($itog);

//$arr_ii=array_intersect($arr_pers_ids['ag1'],$arr_pers_ids['ag2'],$arr_pers_ids['ag3']) ;
//print_r($arr_ii);
//damp_array($arr_pers_ids[ag3]);

    return($data) ;
}

 function HTML_report_body($options=array())
{   $data=$this->prepare_data($options) ;

    ?><div id="reports_itog1">
      <table class="basic fz_small">
         <tr><th>№</th>
             <th>АТП</th>
             <th>Осмотрено за период, человек</th>
             <th>АГ норм</th>
             <th>АГ1</th>
             <th>АГ2</th>
             <th>АГ3</th>
             <th>Пульс</th>
             <th>Алкоголь</th>
             <th>Температура</th>
             <th>Прочее</th>
         </tr>
     <? if (_sizeof($data)) foreach($data as $row_id=>$rec_data)
         {   ?><tr <?if (!is_numeric($row_id)) echo 'class='.$row_id?>>
                   <td><?if (is_numeric($row_id)) echo $row_id?></td>
                   <td><?if ($rec_data[1]) echo $rec_data[1]?></td>
                   <td><?if ($rec_data[2]) echo $rec_data[2]?></td>
                   <td><?if ($rec_data[3]) echo $rec_data[3]?></td>
                   <td><?if ($rec_data[4]['value']){?><a href="<?echo $rec_data[4]['url']?>" target=_BLANK><?echo $rec_data[4]['value']?></a><?}?></td>
                   <td><?if ($rec_data[5]['value']){?><a href="<?echo $rec_data[5]['url']?>" target=_BLANK><?echo $rec_data[5]['value']?></a><?}?></td>
                   <td><?if ($rec_data[6]['value']){?><a href="<?echo $rec_data[6]['url']?>" target=_BLANK><?echo $rec_data[6]['value']?></a><?}?></td>
                   <td><?if ($rec_data[7]['value']){?><a href="<?echo $rec_data[7]['url']?>" target=_BLANK><?echo $rec_data[7]['value']?></a><?}?></td>
                   <td><?if ($rec_data[8]['value']){?><a href="<?echo $rec_data[8]['url']?>" target=_BLANK><?echo $rec_data[8]['value']?></a><?}?></td>
                   <td><?if ($rec_data[9]['value']){?><a href="<?echo $rec_data[9]['url']?>" target=_BLANK><?echo $rec_data[9]['value']?></a><?}?></td>
                   <td><?if ($rec_data[10]['value']){?><a href="<?echo $rec_data[10]['url']?>" target=_BLANK><?echo $rec_data[10]['value']?></a><?}?></td>
             <?
         }
       ?></table>
       <style type="text/css">
           div#reports_itog1{}
           div#reports_itog1 table{width:100%;table-layout:auto;border-collapse:collapse;}
           div#reports_itog1 th{background:#dddddd;color:black;font-weight:bold;border:1px solid #aaaaaa;}
           div#reports_itog1 td{padding:5px;border:1px solid #aaaaaa;text-align:center;}
           div#reports_itog1 td.left{text-align:left;}
           div#reports_itog1 tr.subitog1 td{font-weight:bold;background:#f1f1f1;text-align:center;}
           div#reports_itog1 tr.subitog1 td.left{text-align:left;}
           div#reports_itog1 tr.subitog2 td{font-weight:bold;background:#fafafa;text-align:center;}
           div#reports_itog1 tr.subitog2 td.left{text-align:left;}
           div#reports_itog1 tr.itog td{font-weight:bold;background:#dddddd;}
        </style>
       </div>
    <?
    $debug_db=0 ;
}


  function CSV_report_body($options=array())
  { $data=$this->prepare_data($options) ;
    $i=0 ;
    if (_sizeof($data)) foreach($data as $row_id=>$rec_data)
     {    $data=array() ;
          $data['№']=(is_numeric($row_id))? $row_id+1:'' ;
          $data['АТП']=$rec_data[1];
          $data['Осмотрено за период, человек']=$rec_data[2];
          $data['АГ норм']=$rec_data[3];
          $data['АГ1']=$rec_data[4]['value'];
          $data['АГ2']=$rec_data[5]['value'];
          $data['АГ3']=$rec_data[6]['value'];
          $data['Пульс']=$rec_data[7]['value'];
          $data['Алкоголь']=$rec_data[8]['value'];
          $data['Температура']=$rec_data[9]['value'];
          $data['Прочее']=$rec_data[10]['value'];
          $this->put_data_to_CSV($data,$i) ;
          $i++ ;
    }

  }


}

?>
