<?php
class report_docs_napravlenie extends c_report
{
 var $title='Журнал учета справок и направлений' ;
 var $view_by_space_filter=1 ;
 var $allow_org_ids=1 ;
 var $usl_select_org='' ;
 var $allow_export_to_csv=1 ;


 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="napravlenie" or report_code="napravlenie_narko" or report_code="spravka")' ;
     $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  view_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  view_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 prepare_data(&$list_recs)
   { if (_sizeof($list_recs)) foreach($list_recs as $i=>$rec)
     {  $content=($rec['content'])? unserialize($rec['content']):array() ;
        if (!isset($group_name[$rec['group_id']])) $group_name[$rec['group_id']]=ACCOUNTS()->get_group_name($rec['group_id']) ;
        if (!isset($recs_doctor[$rec['member_id']])) $recs_doctor[$rec['member_id']]=ACCOUNTS()->get_member_by_id($rec['member_id']) ;
        $rec_doctor=$recs_doctor[$rec['member_id']] ;
        //$list_recs[$i]['_member_img_DTW']=(file_exists(_DIR_TO_ROOT.'/public/mo_photo/'.$rec['mo_id'].'_DTW.png'))? $rec['_member_img_DTW']=_PATH_TO_SITE.'/public/mo_photo/'.$rec['mo_id'].'_DTW.png':'' ;
        //$list_recs[$i]['_member_img_DTW']=ESMO()->get_mo_img_dir_by_id($rec['mo_id'],'url','DTW.png',array('no_default_img'=>1)) ; ;
        $list_recs[$i]['_member_img_DTW']=ESMO()->get_mo_DTW_img_dir_by_id($rec['mo_id'],'url') ;

        $list_recs[$i]['date']=date('d.m.Y',$rec['c_data']) ;
        $list_recs[$i]['group_name']=$group_name[$rec['group_id']] ;
        $list_recs[$i]['mo_url']='/window/mo/'.$rec['mo_id'].'/doc/[doc_id='.$rec['pkey'].']/' ;
        $list_recs[$i]['_personal_work']=_IL('IL_works')->recs[$rec['_personal_work_id']]['obj_name'] ;
        $list_recs[$i]['_arr_diagnoz']=array() ;
        if ($content['p2'])         $list_recs[$i]['_arr_diagnoz'][]=$content['p2']  ;
        if ($content['diagnoz'])    $list_recs[$i]['_arr_diagnoz'][]=$content['diagnoz']  ;
        $list_recs[$i]['_doctor_name']=$rec_doctor['obj_name'] ;
        $list_recs[$i]['_doctor_signature']=$rec_doctor['signature'] ;

        // совместимость со старой версией
        //list($mo_id,$tkey)=explode('.',$rec['obj_reffer']) ;
        //if (!$rec['mo_id'])
        //{ $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']) ;
   //     }

     }
   }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;

    $this->prepare_data($list_recs) ;

    ?><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></tr><?
          if (_sizeof($list_recs)) foreach($list_recs as $rec)
          {   ?><tr>
                  <td><?echo $rec['date']?></td>
                  <td><?echo $rec['group_name']?></td>
                  <td class=left><a href="<?echo $rec['mo_url']?>" class="new_window"><?echo $rec['obj_name']?></a></td>
                  <td class=left><?echo $rec['_personal_name']?></td>
                  <td><?echo $rec['_personal_dr']?></td>
                  <td class=left><?echo _IL('IL_works')->recs[$rec['_personal_work_id']]['obj_name']?></td>
                  <td class="left"><?
                  ?><div class="left"><?
                     if (_sizeof($rec['_arr_diagnoz'])) { echo '<ul>' ; foreach($rec['_arr_diagnoz'] as $value)  echo '<li>'.$value.'</li>' ; echo '</ul>' ; }
                  ?>
              </td>
              <td class=left><?echo $rec['_doctor_name']?><br><?if($rec['_doctor_signature']){?><img src="<?echo img_clone($rec['_doctor_signature'],'source')?>" width="100"><?}?></td>
              <td><? if ($rec['_member_img_DTW']){?><img src="<?echo $rec['_member_img_DTW']?>" width="100"><?}?></td>
              </tr><?
          }
      ?></table><?
      //damp_array($rec_mo) ;
      //damp_array($rec_personal) ;
      //damp_array($rec_doctor) ;
  }

  function print_template_CSV($list_recs,$options=array())
  { $this->prepare_data($list_recs) ;
    $i=$options['from'] ;
    if (_sizeof($list_recs)) foreach($list_recs as $rec)
      { $data=array() ;
        $data['Дата']=$rec['date'];
        $data['АТП']=$rec['group_name'] ;
        $data['№ направления']=$rec['obj_name'] ;
        $data['UID']=$rec['_personal_uid'] ;
        $data['ФИО']=$rec['_personal_name'] ;
        $data['ДатаРожд']=$rec['_personal_dr'] ;
        $data['Организация']=$rec['_personal_org_name'] ;
        $data['Должность']=$rec['_personal_work'] ;
        $data['Причина отстранения']=(_sizeof($rec['_arr_diagnoz']))? implode(',',$rec['_arr_diagnoz']):'' ;
        $data['Подпись медицинского работника']= $rec['_doctor_name'] ;

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

  }




}

?>
