<?
class report_itog5 extends c_report
{  var $title='Отчет по отстранениям по алкоголю пофамильно' ;
   var $data=array() ;
   var $itog=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('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')) ;
                            ?>
                        </div>
                    </fieldset>
                    <?$this->panel_buttons();?>
                </form>
        </div>
    <?
  }

 function get_arr_info_filters()
 { $info=parent::get_arr_info_filters() ;
   if ($this->filter['min_mo'])  $info['Минимальное число МО']=$this->filter['min_mo'] ;
   if ($this->filter['min_proc'])  $info['Пороговый процент']=$this->filter['min_proc'] ;
   return $info ;
}

function create_usl_select_obj($_params,&$options=array())
 {  $_usl=array() ;
    $_usl[]='res_a>1' ;
    $_usl[]='model>0' ; //$_usl[]='model in (2,4,6,8,10)' ;
    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['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['dep_id'])
    { $cur_level=execSQL_value('select level from obj_site_personals where pkey='.$this->filter['dep_id']) ;
    $_usl[]='personal_parent'.$cur_level.'='.$this->filter['dep_id'] ;
    }
    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 (!$options['dont_use_member_personal_filter'] and MEMBER()->personal_viewer_name) $_usl['account_personal_filter']='personal_id in (select pkey from '.MEMBER()->personal_viewer_name.')' ;

    $usl_res=implode(' and ',$_usl) ;
    return($usl_res) ;
 }

function prepare_data($options=array())
{
    $usl=$this->create_usl_select_obj(array()) ;
    $recs=execSQL('select * from view_mo_session where '.$usl.' order by personal_parent1,personal_parent2,personal_parent3,personal_parent4,personal_parent5,personal_parent6,personal_parent7,personal_parent8,personal_parent9',array('debug'=>0)) ;

    //print_2x_arr($recs) ;
    $arr_membes_id=array() ;
    // получаем id персонала, отстраннего по результам МО
    if (_sizeof($recs)) foreach($recs as $rec_mo) if ($rec_mo['alko']==1  and (!$rec_mo['dopusk'] or $rec_mo['dopusk']==2 or $rec_mo['dopusk']==3))
    {   if (!$rec_mo['dopusk'] or $rec_mo['dopusk']==3)  $arr_membes_id[$rec_mo['member_id']][3]++  ;    // если нет никакого решения по допуску - клиент не прошел МО до конца
        if ($rec_mo['model']==6 or $rec_mo['model']==8 or $rec_mo['model']==10)   $arr_membes_id[$rec_mo['member_id']][2]++  ;    // если был у врача
        if ($rec_mo['model']==6 or $rec_mo['model']==8 or $rec_mo['model']==2 or $rec_mo['model']==4)  $arr_membes_id[$rec_mo['member_id']][1]++  ;    // если был у врача
    }

    $recs_personals=array() ;
    if (_sizeof($arr_membes_id))
    {  $ids=implode(',',array_keys($arr_membes_id)) ;
       $recs_personals=PERSONAL()->get_personal_list(array('id'=>$ids,'and_disabled'=>1)) ;
    }

    //damp_array($recs_personals,1,-1) ;

    $i=1 ; $this->itog=array() ; $this->data=array() ;
    if (_sizeof($recs_personals)) foreach($recs_personals as $rec_personal)
         {   $arr=$arr_membes_id[$rec_personal['pkey']] ;
             $this->data[$i]['col1']=$i ;
             $this->data[$i]['col2']=$rec_personal['_org_name'] ;
             $this->data[$i]['col21']=$rec_personal['_otdel_name'] ;
             $this->data[$i]['col3']='<a class="new_window" target="_blank" title="win_second" href="'.$rec_personal['__href_window'].'">'.$rec_personal['obj_name'].'</a>'.((!$rec_personal['enabled'])? '<span class="red"> (Уволен)</span>':'') ;
             $this->data[$i]['col4']='<a target=_blank href="'.$this->get_filter_to_params(array('personal_id'=>$rec_personal['pkey'],'a'=>1,'model'=>'(2;4;6;8)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$arr[1].'</a>' ; $this->itog[1]+=$arr[1] ;
             $this->data[$i]['col5']='<a target=_blank href="'.$this->get_filter_to_params(array('personal_id'=>$rec_personal['pkey'],'a'=>1,'model'=>'(6;8;10)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$arr[2].'</a>' ; $this->itog[2]+=$arr[2] ;
             $this->data[$i]['col6']='<a target=_blank href="'.$this->get_filter_to_params(array('personal_id'=>$rec_personal['pkey'],'a'=>1,'dopusk'=>'(1;4)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$arr[3].'</a>' ; $this->itog[3]+=$arr[3] ;
            $i++ ;
         }

    //damp_array($this->data,1,-1) ;
}

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

    ?><table class="basic fz_small full auto">
         <tr><th>№</th>
             <th>Организация</th>
             <th>Подразделение</th>
             <th>ФИО</th>
             <th>Результаты на ЭСМО</th>
             <th>Результаты на РМВ</th>
             <th>Уклонился</th>
         </tr>
     <? $i=1 ; $itog=array() ;
        if (_sizeof($this->data)) foreach($this->data as $rec)
         {   ?><tr><td><?echo $rec['col1']?></td>
                   <td class="left"><?echo $rec['col2']?></td>
                   <td class="left"><?echo $rec['col21']?></td>
                   <td class="left"><?echo $rec['col3']?></td>
                   <td><? if ($rec['col4']) echo $rec['col4'] ;?></td>
                   <td><? if ($rec['col5']) echo $rec['col5'] ;?></td>
                   <td><? if ($rec['col6']) echo $rec['col6'] ;?></td>
                </tr>
            <?
            $i++ ;
         }
         if (_sizeof($this->data))
         {?><tr class="itog">
              <td></td>
              <td></td>
              <td></td>
              <td>ИТОГО</td>
              <td><? if ($this->itog[1]) echo '<a target=_blank href="'.$this->get_filter_to_params(array('a'=>1,'model'=>'(2;4;6;8)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$this->itog[1].'</a>' ; ?></td>
              <td><? if ($this->itog[2]) echo '<a target=_blank href="'.$this->get_filter_to_params(array('a'=>1,'model'=>'(6;8;10)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$this->itog[2].'</a>' ; ?></td>
              <td><? if ($this->itog[3]) echo '<a target=_blank href="'.$this->get_filter_to_params(array('a'=>1,'dopusk'=>'(1;4)'),array('url_report'=>'/cab/reports/report_mo_sess/')).'">'.$this->itog[3].'</a>' ; ?></td>
          </tr>
         <?}?>
       </table>
    <?
    if (!_sizeof($this->data)) {?><div class="alert" style="text-align:center">Данных по Вашему запросу не найдено</div><?}
}

function CSV_report_body($options=array())
  { $this->prepare_data($options) ;
    $i=0 ;
    if (_sizeof($this->data)) foreach($this->data as $rec)
    {     $data=array() ;
          $data['№']=$rec['col1'] ;
          $data['Организация']=$rec['col2'];
          $data['Подразделение']=$rec['col21'];
          $data['ФИО']=strip_tags($rec['col3']) ;
          $data['Результаты на ЭСМО']=strip_tags($rec['col4']) ;
          $data['Результаты на РМВ']=strip_tags($rec['col5']) ;
          $data['Уклонился']=strip_tags($rec['col6']) ;
          $this->put_data_to_CSV($data,$i) ;
          $i++ ;
    }
    $data=array() ;
    $data['№']='' ;
    $data['Подразделение']='ИТОГО' ;
    $data['ФИО']='' ;
    $data['Результаты на ЭСМО']=$this->itog[1];
    $data['Результаты на РМВ']=$this->itog[2];
    $data['Уклонился']=$this->itog[3];
    $this->put_data_to_CSV($data,$i) ;
  }

}

?>
