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

class report_lamp_events extends c_report
{
 var $title='Выдаваемые лампы - события' ;
 var $view_only_model=1 ;
 var $view_by_space_filter=0;
 var $mo_sort='pkey' ;

 function setting($options=array())
{

  $arr_groups=ACCOUNTS()->get_arr_groups_to_cur_account() ;
  $account_recs=mine()->get_lamp_accounts();

  ?><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_input_text('data_from','От:',2,array('class'=>'datetimepicker')) ;
                    $this->filter_section_input_text('data_to','До:',2,array('class'=>'datetimepicker')) ;
                    ?>
                </div>
                <div class="row">
                    <?
                    $this->filter_section_input_text('lamp_number','Номер лампы:',2) ;
                    $this->filter_section_select('account_id','Ламповщик:',2,$account_recs) ;
                    ?>
                </div>
            </fieldset>
            <?if ($options['panel_button_apply']) $this->panel_button_apply(); else $this->panel_buttons();?>
        </form>
     </div>
  <?
}

function panel_info_filter()
 {
    $_str=array() ; $info=array() ; $title='' ;
    if ($this->filter['name'])                            $info['ФИО']='"'.$this->filter['name'].'"' ;
    if ($this->filter['lamp_number'])                     $info['Номер лампы']='"'.$this->filter['lamp_number'].'"' ;
    if ($this->filter['data_from'])                       $info['c']=$this->filter['data_from'] ;
    if ($this->filter['data_to'])                         $info['по']=$this->filter['data_to'] ;
    if ($this->filter['group_id'])                        $info['Группа']=ACCOUNTS()->get_member_name_by_id($this->filter['group_id'])  ;
    if ($this->filter['account_id'])                      $info['Ламповщик']=ACCOUNTS()->get_member_name_by_id($this->filter['account_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>' ;
    $this->report_info=$info ;
    return $title ;
 }

 function create_usl_select_obj($_params,&$options)
 {
    $_usl=[];
    $_usl[]='clss=267';
    if ($this->filter['time_from']) $_usl[]='c_data>='.$this->filter['time_from'];
    if ($this->filter['time_to']) $_usl[]='c_data>='.$this->filter['time_to'];
    if ($this->filter['data_from']) $_usl[]='c_data>='.strtotime($this->filter['data_from']) ;
    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['group_id'] and isset(ACCOUNTS()->tree[$this->filter['group_id']])) $_usl[]=(isset(ACCOUNTS()->tree[$this->filter['group_id']]))?  'group_id in ('.ACCOUNTS()->tree[$this->filter['group_id']]->get_list_child().')':'group_id in ('.$this->filter['group_id'].')' ;
    if ($this->filter['account_id']) $_usl[]='account_id='.$this->filter['account_id'];
    if ($this->filter['name']) {
        $arr_ids=execSQL_line('select pkey from obj_site_personals where clss=211 and obj_name like "%'.addslashes($this->filter['name']).'%"') ;
        if (_sizeof($arr_ids))  $_usl[]='personal_id in ('.implode(',',$arr_ids).')' ;
        else                   $_usl[]='personal_id=-1' ;
    }
    if ($this->filter['lamp_number']) {
        $arr_ids=execSQL_line('select pkey from '.MINE()->lamp_table.' where obj_name="'.addslashes($this->filter['lamp_number']).'"') ;
        if (_sizeof($arr_ids))  $_usl[]='lamp_id in ('.implode(',',$arr_ids).')' ;
        else                   $_usl[]='lamp_id=-1' ;
    }

    if (_sizeof($_usl)) $usl=implode(' and ',$_usl) ;

    return($usl) ;
 }

  function get_cnt_items($usl,$options=array())
  { //$options['debug']=2 ;
    if (!$GLOBALS['LS_lamp_counting']) return 0;
    $this->cnt_items=execSQL_value('select count(pkey) from '.MINE()->lamp_table_events.' where '.$usl,$options) ;
    return($this->cnt_items) ;
  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ; $lamp_recs=[]; $account_recs=[];
    //$order=($options['order'])? ' order by '.$options['order']:' order by '.$this->mo_sort ;
    $order=' order by pkey desc' ;
//    $options['debug']=2 ;
    $recs=execSQL('select * from '.MINE()->lamp_table_events.' where '.$usl.$order.$limit,$options) ;
    if (_sizeof($recs)) foreach($recs as $id=>$rec) {
        $recs[$id]['_personal_info']=PERSONAL()->get_personal_by_id($rec['personal_id']);
        if (!isset($account_recs[$rec['account_id']])) $account_recs[$rec['account_id']]=ACCOUNTS()->get_member_name_by_id($rec['account_id']) ;
        $recs[$id]['_account_name']=$account_recs[$rec['account_id']];
        if (!isset($lamp_recs[$rec['lamp_id']])) $lamp_recs[$rec['lamp_id']]=MINE()->get_lamp_number_by_id($rec['lamp_id']) ;
        $recs[$id]['_lamp_name']=$lamp_recs[$rec['lamp_id']];
    }
    return($recs) ;
  }

 // шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  {
    ?>
    <div id="reports_mo">
      <table class="basic fz_small full auto" >
        <tr>
            <th>№</th>
            <th>Группа</th>
            <th>Дата</th>
            <th>Ламповщик</th>
            <th>Сотрудник</th>
            <th>Тип</th>
            <th>Номер лампы</th>
        </tr>
        <?
        $i=$this->first_obj ;
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        {
            if (!isset($recs_account[$rec['account_id']])) $recs_account[$rec['account_id']]=ACCOUNTS()->get_member_name_by_id($rec['account_id']) ;
            ?>
            <tr class=item>
                <td><?echo $i; $i++?></td>
                <td><?echo ACCOUNTS()->get_group_name($rec['group_id'])?></td>
                <td><?echo date('d.m.Y H:i:s',$rec['c_data'])?></td>
                <td><?echo $rec['_account_name']?></td>
                <td><?echo $rec['_personal_info']['obj_name']?></td>
                <td><?echo $_SESSION['ARR_lamp_events'][$rec['event_type']]?></td>
                <td><?echo $rec['_lamp_name']?></td>
            </tr>
            <?
        }
      ?>
      </table>
    </div>
    <?
  }


}
?>
