<?
/*по этому отчему вопрос - показывать ли сотрудников, которые не проходили МО в выбранной группе*/

class report_itog7_local extends c_report
{  var $title='Статистика прохождения МО (Магнит)' ;
   var $filter=array() ;
   //var $url_report='/cab/reports/itog6/' ;
   public $use_fusioncharts=1 ;
   //var $PDF_master='TCPDF' ;  // короткие отчеты выгружвать через обычный  PDF
   var $recs_org ;
   var $mo_data_min ;
   var $mo_data_max ;
   var $view_by_space_filter=0 ;
   var $allow_export_to_csv=1 ;
   var $allow_set_to_cron=1 ;
   var $mo_type=[0=>'по сессиям МО',1=>'по сеансам МО'] ;

   function setting()
   { $data_info=execSQL_van('select min(c_data) as min_data,max(c_data) as max_data from obj_site_mo_session') ;
     $this->mo_data_min=$data_info['min_data'] ;
     $this->mo_data_max=$data_info['max_data'] ;
     $t=$this->mo_data_min ;
     if ($this->mo_data_max>$this->mo_data_min) while($t<$this->mo_data_max)
        {   $ts=getdate($t) ;
            $cur_month=mktime(0,0,0,$ts['mon'],1,$ts['year']) ;
            $arr_from[$cur_month]=get_month_year($cur_month,'month year') ;
            $t=mktime(0,0,0,$ts['mon']+1,1,$ts['year']) ;
        }

     $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) ;
     if (!$this->filter['mo_type']) $this->filter['mo_type']=0 ;

     ?><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">
                       <div class="row">
                           <? $this->filter_section_select('group_id',$GLOBALS['LS_person_group_title'].':',2,$arr_groups) ;
                              $this->filter_section_select_multiple('org_ids','Организация:',3,$arr_orgs_ids) ;
                              $this->filter_section_select_multiple('otdel_ids','Подразделение:',3,$arr_otdel_ids) ;
                              $this->filter_section_select('month','Месяц:',2,$arr_from) ;
                              $this->filter_section_select('mo_type','Тип подсчета:',2,$this->mo_type,['nospace_element'=>1]) ;
                           ?>
                   </div>
               </fieldset>
               <?$this->panel_buttons();?>
           </form>
        </div>
     <?
   }

 function get_arr_info_filters()
 { $info=parent::get_arr_info_filters() ;
   $info['Тип подсчета']=$this->mo_type[$this->filter['mo_type']] ;
   return $info ;
 }

    function create_usl_select_obj($_params=array(),&$options=array())
      {  //damp_array($this->filter,1,-1) ;
         $usl=array() ;
         if (!$this->filter['mo_type']) $usl[]='t1.model>0' ; else $usl[]='t1.status in (2,3)' ;
         $usl[]='t2.pkey>0' ;

         if ($this->filter['group_id'])   $usl[]='t1.group_id in ('.ACCOUNTS()->tree[$this->filter['group_id']]->get_list_child().')' ;

         //if (MEMBER()->personal_viewer_name) $usl['account_personal_filter']='t1.member_id in (select pkey from '.MEMBER()->personal_viewer_name.')' ;


         if ($this->filter['month'])
         {

             $ts=getdate($this->filter['month']) ;
                         $cur_month=mktime(0,0,0,$ts['mon'],1,$ts['year']) ;
                         $arr_from[$cur_month]=get_month_year($cur_month,'month year') ;
                         $t=mktime(0,0,0,$ts['mon']+1,1,$ts['year'])-1 ;
             $usl[]='t1.c_data>="'.$this->filter['month'].'" and t1.c_data<='.$t ;
             //echo 'от '.date('d.m.Y H:i:s',$this->filter['month']).' до '.date('d.m.Y H:i:s',$t).'<br>' ;

         }
         if ($this->filter['name'])              $usl[]='obj_name like "'.$this->filter['name'].'%"' ;

         if ($this->filter['org_ids'])          $usl[]='t2.parent2 in ('.implode(',',array_keys($this->filter['org_ids'])).')' ;
         if ($this->filter['otdel_ids'])        $usl[]='t2.parent3 in ('.implode(',',array_keys($this->filter['otdel_ids'])).')' ;

         if ($this->filter['working_id'])          $usl[]='working="'.$this->filter['working_id'].'"' ;

         $usl_res=implode(' and ',$usl) ;
         //damp_array($usl,1,-1) ;
         return($usl_res) ;
      }


 function get_cnt_items($usl,$options=array())
 { //$options['debug']=1 ;
   $this->cnt_items=execSQL_value('select count(*) from 
                                   (select t1.group_id,member_id
                                   from '.ESMO()->table_mo_sessions.' t1 
                                   left join view_personals t2 on t2.pkey=t1.member_id
                                   where '.$usl.'
                                   group by t1.group_id,t1.member_id) as t3 
                                   ',$options) ;
    $this->cnt_mo=execSQL_value('select sum(cnt_mo) from 
                                   (select t1.group_id,member_id,count(t1.pkey) as cnt_mo
                                   from '.ESMO()->table_mo_sessions.' t1 
                                   left join view_personals t2 on t2.pkey=t1.member_id
                                   where '.$usl.'
                                   group by t1.group_id,t1.member_id) as t3 
                                   ',$options) ;
   return($this->cnt_items) ;
 }

 function get_items($usl,$options=array())
 { $limit=($options['limit'])? ' '.$options['limit']:'' ;
   //$order=($options['order'])? ' order by '.$options['order']:' order by '.$this->mo_sort ;
   $order=' order by t1.group_id,t2.parent2,t2.parent3,t2.obj_name';
   //$options['debug']=1 ;
   $options['no_indx']=1 ;
   //if (_IS_SYSTEM_IP) $options['debug']=1 ;
   $recs=execSQL('select t1.group_id,t1.member_id, count(t1.pkey) as cnt_mo,
                  t2.obj_name,t2._org_name,t2._otdel_name,t2.working,t2.dr,t2.uid, 
                  t3.obj_name as group_name 
                  from '.ESMO()->table_mo_sessions.' t1
                  left join view_personals t2 on t2.pkey=t1.member_id 
                  left join obj_site_account t3 on t3.pkey=t1.group_id 
                  where '.$usl.' 
                  group by t1.group_id,t1.member_id
                  '.$order.$limit,$options) ;
   if (_sizeof($recs) and !$options['no_public_info']) foreach($recs as $id=>$rec) ESMO()->prepare_public_info_to_mo($recs[$id],$options) ;
   return($recs) ;
 }

// шаблон - вывод ссылками без разделитея
 function print_template_HTML($list_recs,$options=array())
 {  $mo_title=($this->filter['mo_type'])? 'Кол-во сеансов МО':'Кол-во сессий МО' ;
    echo 'Общее '.$mo_title.' за весь период: <strong>'.$this->cnt_mo.'</strong><br>' ;
    ?><table class="basic full fz_small auto" >
       <colgroup><col id="c0"><col id="c1"><col id="c3"><col id="c4"><col id="c11"><col id="c12"><col id="c13"></colgroup>
       <tr><th>АТП МО</th>
           <th>РЦ МО</th>
           <th>Подразделение</th>
           <th>Должность</th>
           <th>ФИО<br>сотрудника</th>
           <th><?echo $mo_title?></th>
         </tr>
       <?
       $i=$this->first_obj ; ;
       if (_sizeof($list_recs)) foreach($list_recs as $rec)
       {   ?><tr class=item>
               <td class="left"><?echo $rec['group_name'];?></td>
               <td class="left"><?echo $rec['_org_name']?></td>
               <td class="left"><?echo $rec['_otdel_name']?></td>
               <td class="left"><?echo _IL('IL_works')->recs[$rec['working']]['obj_name']?></td>
               <td class="left"><?echo $rec['obj_name']?></td>
               <td><?echo $rec['cnt_mo']?></td>
           </tr><?
           $i++ ;
       }
     ?></table><?

 }

    function print_template_CSV($list_recs,$options=array())
      { $mo_title=($this->filter['mo_type'])? 'Кол-во сеансов МО':'Кол-во сессий МО' ;
        $i=$options['from'] ;
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
          { $data=array() ;
            $data[$GLOBALS['LS_person_group_title']]=$rec['group_name'] ;
            $data['РЦ МО']=$rec['_org_name'] ;
            $data['Подразделение']=$rec['_otdel_name'] ;
            $data['Должность']=_IL('IL_works')->recs[$rec['working']]['obj_name'] ;
            $data['ФИО']=$rec['obj_name'];
            $data['ДР']=$rec['dr'];
            $data['UID']=$rec['uid'];
            $data[$mo_title]=$rec['cnt_mo'] ;

            $this->put_data_to_CSV($data,$i) ;
            $i++ ;
          }

      }


}

?>
