<?php
class report_docs_protokol extends c_report
{
 var $title='Журнал регистрации протоколов контроля трезвости' ;
 var $view_by_space_filter=1 ;
 var $allow_org_ids=1 ;
 var $usl_select_org='' ;


 function setting($options=array())
     {   $arr_groups=ACCOUNTS()->get_arr_groups_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_select('group_id',$GLOBALS['LS_person_group_title'].':',2,$arr_groups) ;
                            $this->filter_section_input_text('name','ФИО:',2,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>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

  function create_usl_select_obj($_params,&$options)
  {  //damp_array($this->filter) ;
     if (!$this->filter['group_id']) $this->filter['group_id']=MEMBER()->cur_group_id ;

     $usl=array() ;
     $usl[]='clss=215' ;
     $usl[]='report_code="protokol_trezvosti"' ;
     $usl[]='sign_data!="" and not sign_data is null' ;
       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['group_id'])        $usl[]='group_id in ('.ACCOUNTS()->get_childs_id($this->filter['group_id']).')' ;
     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']) ;


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

 function panel_info_filter()
 { $_str=array() ; $info=array() ; $title='' ;
 if (MEMBER()->rol==2) $this->filter['group_id']=MEMBER()->cur_group_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 ($this->filter['data_from'])                     $info['дата от']='"'.$this->filter['data_from'].'"' ;
     if ($this->filter['data_to'])                     $info['дата до']='"'.$this->filter['data_to'].'"' ;
   if (_sizeof($info)) foreach($info as $title=>$value) $_str[]=$title.': <strong>'.$value.'</strong>' ;
   if (_sizeof($_str)) $title='<p class=center>'.implode(' ',$_str).'</p>' ;
   return $title ;
 }

  function get_cnt_items($usl,$options=array())
  { $cnt=execSQL_value('select count(pkey)
                        from  obj_site_docs
                        where '.$usl,$options) ;
    return($cnt) ;
  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    $order=($this->filter['order'])? ' order by '.$this->filter['order']:' order by pkey desc' ;
 	$list_rec=execSQL('select *
                       from  obj_site_docs
                       where '.$usl.$order.$limit,$options) ;
    if (_sizeof($list_rec)) foreach($list_rec as $id=>$rec) DOCS()->prepare_public_info_to_doc($list_rec[$id],$options) ;
    return($list_rec) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$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><th>№ протокола</th><th>АТП</th><th>Подпись медицинского работника</th></tr><?
          $i=1 ;
          if (_sizeof($list_recs)) foreach($list_recs as $rec)
          {   list($mo_id,$tkey)=explode('.',$rec['obj_reffer']) ;
              $rec_mo=ESMO()->get_mo_by_id($mo_id) ;
              $rec_personal=PERSONAL()->get_personal_by_id($rec_mo['personal_id']) ;
              $rec_doctor=ACCOUNTS()->get_member_by_id($rec_mo['doctor_id']) ;
              ?><tr>
                  <td><?echo $i; $i++;?></td>
                  <td class=left><?echo $rec_personal['obj_name']?></td>
                  <td><?echo $rec_personal['dr']?></td>
                  <td class=left><?echo $rec_personal['_work_name']?></td>
                  <td><?echo date('d.m.Y',$rec['c_data'])?></td>
                  <td><?echo date('H:i:s',$rec['c_data'])?></td>
                  <td class="left"><?
                  ?><div class="left"><?
                        $arr=array() ;
                        //if ($rec_mo['comment']) $arr=explode('<br>',$rec_mo['comment']) ;
                        //if ($rec_mo['simptoms']) $arr[]=_IL('IL_diagnoz')->recs[$rec_mo['simptoms']]['obj_name'];
                        //if ($rec_mo['status']==3) $arr[]=$rec_mo['_status_name'] ;
                        //if ($rec_mo['doctor_fixed_comment']) $arr[]=$rec_mo['doctor_fixed_comment'] ;
                        //if ($rec_mo['doctor_comment']) $arr[]=$rec_mo['doctor_comment'] ;
                        if ($rec_mo['alko_value']) $arr[]=$rec_mo['alko_value'] ;
                        if (_sizeof($arr)) { echo '<ul>' ; if (_sizeof($arr)) foreach($arr as $value)  echo '<li>'.$value.'</li>' ; echo '</ul>' ; }
                  ?>
              </td>
              <td><a href="<?echo $rec_mo['__href_window']?>doc/[doc_id=<? echo $rec['pkey']?>]/" class="new_window"><?echo $rec['indx']?></a></td>
              <td><?echo ACCOUNTS()->get_group_name($rec['group_id'])?></td>
              <td><?echo $rec_doctor['obj_name']?><br><?if($rec_doctor['signature']){?><img src="<?echo img_clone($rec_doctor['signature'],'source')?>" width="100"><?}?></td>
              </tr><?
          }
      ?></table><?
      //damp_array($rec   ) ;
      //damp_array($rec_personal) ;
      //damp_array($rec_doctor) ;
  }

    function _print_template_XML($list_recs,$options=array())
       { //damp_array($list_recs) ;
         //print_r($list_recs) ; trace() ;
         if (_sizeof($list_recs)) foreach($list_recs as $rec)
         {   $item=add_element($this->xml_doc,$this->xml,'item','',array('id'=>$rec['pkey'])) ;
             add_element($this->xml_doc,$item,'fio',$rec['member_name']) ;
             add_element($this->xml_doc,$item,'dr',$rec['member_dr']) ;
             add_element($this->xml_doc,$item,'terminal_name',$rec['terminal_name']) ;
             add_element($this->xml_doc,$item,'time_MO',$rec['_data']) ;
             add_element($this->xml_doc,$item,'mo_id',$rec['pkey']) ;
             add_element($this->xml_doc,$item,'question',$rec['_question']) ;
             add_element($this->xml_doc,$item,'alko',$rec['_alko']) ;
             add_element($this->xml_doc,$item,'systolic',$rec['_systolic']) ;
             add_element($this->xml_doc,$item,'diastolic',$rec['_diastolic']) ;
             add_element($this->xml_doc,$item,'pulse',$rec['_pulse']) ;
             add_element($this->xml_doc,$item,'temperature',$rec['temperature']) ;
             add_element($this->xml_doc,$item,'doctor_comment',$rec['doctor_comment']) ;
             add_element($this->xml_doc,$item,'mo_result_id',$rec['status']) ;
             add_element($this->xml_doc,$item,'mo_result_text',$rec['_status_name']) ;
             add_element($this->xml_doc,$item,'mo_dopusk_id',$rec['dopusk']) ;
             add_element($this->xml_doc,$item,'mo_dopusk_text',$rec['_dopusk_name_full']) ;
         }
       }

       function _print_template_JSON($list_recs,$options=array())
          { $result=array() ;
            if (_sizeof($list_recs)) foreach($list_recs as $rec)
              $result[]=array('fio'=>$rec['member_name'],
                              'uid'=>$rec['member_uid'],
                              'dr'=>$rec['member_dr'],
                              'terminal_name'=>$rec['terminal_name'],
                              'time_MO'=>$rec['_data'],
                              'mo_id'=>$rec['pkey'],
                              'question'=>$rec['_question'],
                              'alko'=>$rec['_alko'],
                              'systolic'=>$rec['_systolic'],
                              'diastolic'=>$rec['_diastolic'],
                              'pulse'=>$rec['_pulse'],
                              'temperature'=>$rec['temperature'],
                              'doctor_comment'=>$rec['doctor_comment'],
                              'mo_result_id'=>$rec['status'],
                              'mo_result_text'=>$rec['_status_name'],
                              'mo_dopusk_id'=>$rec['dopusk'],
                              'mo_dopusk_text'=>$rec['_dopusk_name_full']
                             ) ;
              $text_json_out=json_encode($result) ;
              // возвращает ответ устройству
              echo $text_json_out ;

          }

    function _manual_tags_in()
    {
        ?><table class="basic fz_normal left">
                    <tr><td>uid</td><td>Уникальный идентификатор сотрудника</td></tr>
        </table><?
    }

    function _manual_tags_out()
    {
        ?><table class="basic fz_normal left">
                    <tr><td>time_now</td><td>текущее время сервера</td></tr>
                    <tr><td>time_from</td><td>время, от которого происходил поиск МО</td></tr>
                    <tr><td>fio</td><td>ФИО сотрудника</td></tr>
                    <tr><td>dr</td><td>дата рождения сотрудника</td></tr>
                    <tr><td>terminal_name</td><td>терминал, на котором сотрудник проходил МО</td></tr>
                    <tr><td>time_MO</td><td>дата прохождения МО</td></tr>
                    <tr><td>mo_id</td><td>id сессии МО</td></tr>
                    <tr><td>question</td><td>жалобы на состояние здоровья</td></tr>
                    <tr><td>alko</td><td>алкоголь</td></tr>
                    <tr><td>systolic</td><td>систолическое давление</td></tr>
                    <tr><td>diastolic</td>Диастолическое давление<td></td></tr>
                    <tr><td>pulse</td><td>пульс</td></tr>
                    <tr><td>temperature</td><td>температура</td></tr>
                    <tr><td>doctor_comment</td><td>комментарий врача</td></tr>
                    <tr><td>mo_result_id</td><td>id результата МО на терминале (1: осмотр окончен, успешно, 2: осмотр окончен, ошибка, 3: осмотр не проводился)</td></tr>
                    <tr><td>mo_result_text</td><td>текстовое описание результата МО</td></tr>
                    <tr><td>mo_dopusk_id</td><td>id допуска(1: допуск получен, 2: допуск запрещен)</td></tr>
                    <tr><td>mo_dopusk_text</td><td>текстовое описание допуска</td></tr>
          </table>
        <?
    }




}

?>
