<?
// вниамение!!!
// во всех фильтрах:
// data_from, data_to - дата в текстовом формате
// time_from, time_to - дата в UNIX формате

class report_persons_without_mo extends c_report
{
 var $title='Сотрудники без мед. осмотров' ;
// var $view_only_model=1 ;
 var $allow_export_to_csv=1 ;
 var $max_personal_parent_cnt=0;
 var $person_usl='';

 function setting()
{ if (isset($this->filter['time_from'])) $this->filter['data_from']=date('d.m.Y',$this->filter['time_from']);
  if (isset($this->filter['time_to'])) $this->filter['data_to']=date('d.m.Y',$this->filter['time_to']);
  //$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_input_text('name','ФИО:',3,array('auto_complete'=>"autocomplette_personal_name")) ;
//                    $this->filter_section_select('group_id','Группа:',2,$arr_groups) ;
                    $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_select_multiple('org_ids','Организация:',2,$arr_orgs_ids) ;
                    ?>
                </div>
            </fieldset>
            <?$this->panel_buttons();?>
        </form>
     </div>
  <?
}

    function panel_info_filter() {
        $_str=array() ; $info=array() ; $title='' ;
        //if (MEMBER()->rol==2) $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['dep_id'])                          $info['Подразделение']=PERSONAL()->get_full_dep_name_by_dep_id($this->filter['dep_id'])  ;
        if ($this->filter['org_id'])                          $info['организация']=PERSONAL()->get_name_by_id($this->filter['org_id']) ;
        if ($this->filter['org_ids'])                          $info['организация']=PERSONAL()->get_org_names_by_ids(array_keys($this->filter['org_ids']))  ;
        if ($this->filter['otdel_id'])                        $info['подразделение']=PERSONAL()->get_name_by_id($this->filter['otdel_id']) ;
        if ($this->filter['name'])                            $info['ФИО']='"'.$this->filter['name'].'"' ;
        if ($this->filter['group_id'])                        $info['Группа']=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 create_usl_select_obj($_params,&$options) {
        $_usl=array() ;

        $_person_usl=[];

        $_person_usl[]='t1.clss=211';
        $_person_usl[]='t1.enabled=1';
        $_person_usl[]='t2.member_id is null';

        $_usl['status']='status in (2,3)' ;  // только завершенные МО
        $_usl['dopusk']='dopusk in (1,2,3)' ;

        if ($this->filter['data_from'])     {
            $_usl[]='c_data>='.strtotime($this->filter['data_from']) ;
            if ($this->filter['debug']) echo 'Дата от '.date('d.m.Y H:i:s',strtotime($this->filter['data_from'])).'<br>' ;
        }
        if ($this->filter['data_to'])       {
            $arr=explode(' ',trim($this->filter['data_to'])) ;
            $time_to=(!$arr[1])? strtotime($this->filter['data_to'].' 23:59:59'):strtotime($this->filter['data_to']) ;
            $_usl[]='c_data<='.$time_to ;
            if ($this->filter['debug']) echo 'Дата до '.date('d.m.Y H:i:s',$time_to) ;
        }

        if ($this->filter['org_ids'])          {
//            $usl[]='parent2 in ('.implode(',',array_keys($this->filter['org_ids'])).')' ;
            $_person_usl[]='t1.parent2 in ('.implode(',',array_keys($this->filter['org_ids'])).')' ;
        }

        if (_sizeof($_usl)) $usl=implode(' and ',$_usl) ;
        if (_sizeof($_person_usl)) $this->person_usl=implode(' and ',$_person_usl) ;

        return($usl) ;
    }

    function get_cnt_items($usl,$options=array()) {
        $cnt=execSQL_value('
            select count(t1.pkey) from '.PERSONAL()->table_name.' t1
                left outer join (
                   select count(pkey) as cnt_mo,member_id from '.ESMO()->table_name.' where '.$usl.' group by member_id
                ) t2
                on t1.pkey=t2.member_id
            where '.$this->person_usl,$options);

        return($cnt) ;
    }

    function get_items($usl,$options=array()) {
        $limit=($options['limit'])? ' '.$options['limit']:'' ;
        $order=($options['order'])? ' order by '.$options['order']:' order by obj_name';

        $recs=execSQL('
            select t1.* from '.PERSONAL()->view_personal.' t1
                left outer join (
                   select count(pkey) as cnt_mo,member_id from '.ESMO()->table_name.' where '.$usl.' group by member_id
                ) t2
                on t1.pkey=t2.member_id
            where '.$this->person_usl.$order.$limit,$options);

        return($recs) ;
    }

  function check_date_for_skip_mo(&$member_rec,$last_mo_date,$date_for_compare)
  {
    if (($date_for_compare-$last_mo_date)>(60*60*$GLOBALS['LS_setting_smena_after']))
        $member_rec.=date('d.m.Y',$last_mo_date+60*60*$GLOBALS['LS_setting_smena_after']).'<br>';
  }

 // шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  {
    ?>
      <table class="basic full auto" >
        <colgroup>
            <col id="c0">
        </colgroup>
        <tr>
          <th>№</th>
          <th>ФИО<br>сотрудника</th>
          <th>Должность</th>
          <th>Организация</th>
          <th>Подразделение</th>
          <?
          if ($this->max_personal_parent_cnt>=2) {
              for($j=2;$j<=$this->max_personal_parent_cnt;$j++) {
                  echo '<th>Подразделение<br>(уровень '.$j.')</th>';
              }
          }
          ?>
          </tr>
        <?
        $i=$this->first_obj ; ;
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        {
            ?><tr class=item>
                <td><?echo $i;  $i++ ;?></td>
                <td><?echo '<a href="/cab/personal/person/'.$rec['pkey'].'/">'.$rec['obj_name'].'</a>'?></td>
                <td><?echo _IL('IL_works')->recs[$rec['working']]['obj_name']?></td>
                <td><?echo $rec['_org_name']?></td>
                <td><?echo $rec['_otdel_name']?></td>
                <?
                if ($this->max_personal_parent_cnt>0) {
                    for($j=2;$j<=$this->max_personal_parent_cnt;$j++) {
                        echo '<td>'._IL('ARR_deps')->recs_names[$rec['personal_parent'.($j+2)]].'</td>';
                    }
                }
                ?>
            </tr><?
        }
      ?></table><?

     //damp_array($rec,1,-1) ;
  }

    function print_template_CSV($list_recs,$options=array())
    {
        $i=0 ;
      if (_sizeof($list_recs)) foreach($list_recs as $rec)
        {
          $data=array() ;

          $data['ФИО сотрудника']=$rec['obj_name']?$rec['obj_name']:'' ;
          $data['Должность']=_IL('IL_works')->recs[$rec['working']]['obj_name']?_IL('IL_works')->recs[$rec['working']]['obj_name']:'' ;
          $data['Организация']=$rec['_org_name']?$rec['_org_name']:'' ;
          $data['Подразделение']=$rec['_otdel_name']?$rec['_otdel_name']:'' ;
          if ($this->max_personal_parent_cnt>0) {
            for($j=2;$j<=$this->max_personal_parent_cnt;$j++) {
                $data['Подразделение (уровень '.$j.')']=_IL('ARR_deps')->recs_names[$rec['personal_parent'.($j+2)]];
            }
          }

          if (_sizeof($data)) foreach($data as $fname=>$value) $data[$fname]=iconv('UTF-8','windows-1251',$value) ;
          // для первой строки выводим заголовки
          if (!$i)
          {   $arr_title=array_keys($data) ;
              foreach($arr_title as $i=>$title) $arr_title[$i]=iconv('UTF-8','windows-1251',$title) ;
              fputcsv($this->file_csv,$arr_title,';');
          }
          // вывод данных
          fputcsv($this->file_csv,$data,';');
          $i++ ;
        }

    }


}
?>
