<?php
include_once('i_daily_briefing_ui.php');

class report_daily_briefing_journal extends c_report
{
    var $title='Журнал прохождения ежедневного инструктажа' ;
    var $view_by_space_filter=1 ;
    var $report_paginator=0 ;
    var $no_view_title=1 ;
    var $briefing_id=0 ;
    var $report_script=_DIR_EXT.'/daily_briefing/report_daily_briefing_journal.php' ;

    function init($options=array())
    {
        if (!$this->filter['briefing_id'] and $this->briefing_id) $this->filter['briefing_id']=$this->briefing_id ;
        $this->briefing_id=(int)$this->filter['briefing_id'] ;
        if (!$this->filter['data_from']) $this->filter['data_from']=date('d.m.Y') ;
        if (!$this->filter['data_to']) $this->filter['data_to']=$this->filter['data_from'] ;
    }

    function setting($options=array())
    {
        ?>
        <div id="panel_filter">
            <form id="sky-form" class="sky-form">
							  <input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                <input type="hidden" name="report_code" value="<?echo htmlspecialchars($this->report_code)?>">
                <fieldset>
                    <div class="row">
                        <? $this->filter_section_input_text('data_from','Дата инструктажа:',3,array('class'=>'datetimepicker')); ?>
                    </div>
                </fieldset>
                <?$this->panel_buttons();?>
            </form>
        </div>
        <br>
        <?
    }

    function create_usl_select_obj($_params,&$options)
    {
        $usl=array() ;
        $usl[]='1=1' ;
        if ($this->briefing_id) $usl[]='t1.briefing_id='.(int)$this->briefing_id ;
        if (!$this->filter['all_groups']) $usl[]='t1.group_id='.(int)MEMBER()->cur_group_id ;
        if ($this->filter['status']) $usl[]='t1.status='.(int)$this->filter['status'] ;
        if ($this->filter['data_from']) $usl[]='t1.arrive_time>='.(int)strtotime($this->filter['data_from'].' 00:00:00') ;
        if ($this->filter['data_to']) $usl[]='t1.arrive_time<='.(int)strtotime($this->filter['data_to'].' 23:59:59') ;
        if ((int)MEMBER()->rol===9) {
            $mid=(int)MEMBER()->id;
            $usl[]='(t1.account_arrive_id='.$mid.' or t1.account_sign_id='.$mid.')';
        }
        return(implode(' and ',$usl)) ;
    }

    function panel_info_filter()
    {
        $_str=array() ;
        if ($this->filter['data_from']) $_str[]='Дата инструктажа: <strong>'.htmlspecialchars($this->filter['data_from']).'</strong>' ;
        if (_sizeof($_str)) return '<div class="center">'.implode(' ',$_str).'</div>' ;
        return '' ;
    }

    function get_cnt_items($usl,$options=array())
    {
        $cnt=execSQL_value('select count(t1.pkey) from '.DAILY_BRIEFING()->table_pass.' t1 where '.$usl,$options) ;
        return($cnt) ;
    }

    function get_items($usl,$options=array())
    {
        $limit=($options['limit'])? ' '.$options['limit']:'' ;
        $order=($options['order'])? ' order by '.$options['order']:' order by arrive_time desc' ;
        $list_rec=execSQL('select t1.*,fl.obj_name as personal_name,fl._org_name,fl._otdel_name,fl.working as personal_working
                           from '.DAILY_BRIEFING()->table_pass.' t1
                           left join view_personals fl on fl.pkey=t1.personal_id
                           where '.$usl.$order.$limit,$options) ;
        if (_sizeof($list_rec)) foreach($list_rec as $id=>$rec) $list_rec[$id]=DAILY_BRIEFING()->prepare_pass_rec($rec) ;
        return($list_rec) ;
    }

    function print_template_HTML($list_recs,$options=array())
    {
        $id=($options['id'])? ' id="'.htmlspecialchars($options['id']).'"':'' ;
        ?>
        <table class="basic fz_small full auto"<? echo $id ?>>
            <tr>
                <th>Дата</th>
                <th>Сотрудник</th>
                <th>Организация</th>
                <th>Подразделение</th>
                <th>Должность</th>
                <th>Статус</th>
                <th>Подпись</th>
            </tr>
            <? if (_sizeof($list_recs)) foreach($list_recs as $rec) { ?>
                <tr>
                    <td><? echo ($rec['arrive_time'])? date('d.m.Y H:i',$rec['arrive_time']):'' ?></td>
                    <td class="left"><? echo htmlspecialchars($rec['personal_name']) ?></td>
                    <td class="left"><? echo htmlspecialchars($rec['_org_name']) ?></td>
                    <td class="left"><? echo htmlspecialchars($rec['_otdel_name']) ?></td>
                    <td class="left"><? echo htmlspecialchars(_IL('IL_works')->recs[$rec['personal_working']]['obj_name']) ?></td>
                    <td><? echo htmlspecialchars($rec['_status_name']) ?></td>
                    <td><? if ($rec['sign_fname']) { ?><img src="<? echo _PATH_TO_SITE.htmlspecialchars($rec['__sign_src']) ?>?r=<? echo (int)rand(1,10000) ?>" width="150" alt=""><? } ?></td>
                </tr>
            <? } else { ?>
                <tr><td colspan="7" class="center">По заданным условиям записи не найдены</td></tr>
            <? } ?>
        </table>
        <?
    }
}
?>
