<?php
class report_magnit_3 extends c_report
{
 var $title='Отчет Предприятие 3' ;
 var $filter=array() ;
 var $allow_export_to_csv=1 ;

 function setting($options=array())
{
  if (!$this->filter['SAD']) $this->filter['SAD']=160;
  if (!$this->filter['DAD']) $this->filter['DAD']=100;
  if (!$this->filter['PULS']) $this->filter['PULS']=100;

  $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_select_multiple('atp_ids','Организация - АТП:',2,$arr_orgs_ids,array('match'=>'/ООО \"Сельта\"(.+?)/is')) ;
                                $this->filter_section_select_multiple('rc_ids','Организация - РЦ:',2,$arr_orgs_ids,array('match'=>'/АО \"Тандер\"(.+?)РЦ/is')) ;
                                $this->filter_section_input_text('SAD','САД, включительно от:',2,array('required'=>1)) ;
                                $this->filter_section_input_text('DAD','ДАД, включительно от:',2,array('required'=>1)) ;
                                $this->filter_section_input_text('PULS','Пульс, включительно от:',2,array('required'=>1)) ;
                                $this->filter_section_select_multiple('group_medic','Группа по болезни:',2,_IL('IL_group_medic')->recs) ;
                           ?>
                       </div>
                   </fieldset>
                   <?$this->panel_buttons();?>
               </form>
   </div>
  <?
}

function panel_info_filter()
 { $_str=array() ; $info=array() ; $title='' ;
   if ($this->filter['atp_ids'])                         $info['АТП']=PERSONAL()->get_org_names_by_ids(array_keys($this->filter['atp_ids']))  ;
   if ($this->filter['rc_ids'])                          $info['РЦ']=PERSONAL()->get_org_names_by_ids(array_keys($this->filter['rc_ids']))  ;
   if ($this->filter['SAD'])                             $info['САД']='>='.$this->filter['SAD']  ;
   if ($this->filter['DAD'])                             $info['ДАД']='>='.$this->filter['DAD']  ;
   if ($this->filter['PULS'])                            $info['Пульс']='>='.$this->filter['PULS']  ;
   //damp_array($this->filter) ;

   if (_sizeof($this->filter['group_medic']))
   { $arr_group_name=array() ;
     foreach($this->filter['group_medic'] as $group_id=>$flag) $arr_group_name[]=_IL('IL_group_medic')->recs[$group_id]['obj_name'] ;
     if (_sizeof($arr_group_name)) $info['группа по болезни']=implode(',',$arr_group_name);
   }

   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)
      {  //damp_array($this->filter) ;
         $usl=array() ;

         $usl[]='o_tkey='.PERSONAL()->tkey ;
         $usl[]='o_clss=211' ;
         $usl[]='p_tkey='._IL('IL_group_medic')->recs[1]['tkey'] ;
         $usl[]='not t3.pkey is null' ;
         $usl[]='t3.clss=211' ;
         $usl[]='t3.enabled=1' ;

         if (_sizeof($this->filter['group_medic']))
         { $arr_group_ids=array() ;
           foreach($this->filter['group_medic'] as $group_id=>$flag) $arr_group_ids[]=_IL('IL_group_medic')->recs[$group_id]['pkey'] ;
           if (_sizeof($arr_group_ids)) $usl[]='t1.p_id in ('.implode(',',$arr_group_ids).')';
         }

         if (_sizeof($this->filter['atp_ids'])) $usl[]='t3.parent2 in ('.implode(',',array_keys($this->filter['atp_ids'])).')' ;
         if (_sizeof($this->filter['rc_ids']))  $usl[]='t3.parent2 in ('.implode(',',array_keys($this->filter['rc_ids'])).')' ;
         $usl_res=implode(' and ',$usl) ;
         //damp_array($usl) ;
         return($usl_res) ;
      }

      function get_cnt_items($usl,$options=array())
      { //$options['debug']=2 ;
        $cnt=execSQL_value('select count(o_id),
                           (select count(pkey) from obj_site_mo_session t2 where t2.member_id=t1.o_id and t2.c_data>t1.c_data and systolic>='.$this->filter['SAD'].') as cnt_s,
                           (select count(pkey) from obj_site_mo_session t2 where t2.member_id=t1.o_id and t2.c_data>t1.c_data and diastolic>='.$this->filter['DAD'].') as cnt_d,
                           (select count(pkey) from obj_site_mo_session t2 where t2.member_id=t1.o_id and t2.c_data>t1.c_data and pulse>='.$this->filter['PULS'].') as cnt_p
                           from  obj_site_link t1
                           left join obj_site_personals t3 on t3.pkey=t1.o_id 
                           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 obj_name' ;
        $order=' order by obj_name' ;
        $list_rec=execSQL('select t1.pkey,t1.o_id as personal_id,t1.c_data,p_id,
                              (select count(pkey) from obj_site_mo_session t2 where t2.member_id=t1.o_id and t2.c_data>t1.c_data and systolic>='.$this->filter['SAD'].') as cnt_s,
                              (select count(pkey) from obj_site_mo_session t2 where t2.member_id=t1.o_id and t2.c_data>t1.c_data and diastolic>='.$this->filter['DAD'].') as cnt_d,
                              (select count(pkey) from obj_site_mo_session t2 where t2.member_id=t1.o_id and t2.c_data>t1.c_data and pulse>='.$this->filter['PULS'].') as cnt_p,
                              t3.obj_name,t3.dr,t3._org_name,t3._otdel_name,t3.uid,t3.propusk,
                              t4.obj_name as group_name
                              from obj_site_link t1
                              left join view_personals t3 on t3.pkey=t1.o_id 
                              left join obj_site_list t4 on t4.pkey=t1.p_id 
                              where '.$usl.$order.$limit,$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>GUID</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['obj_name']?></td>
                      <td><?echo $rec['dr']?></td>
                      <td><?echo $rec['uid']?></td>
                      <td><?echo $rec['propusk']?></td>
                      <td><?echo $rec['_org_name']?></td>
                      <td><?echo $rec['_otdel_name']?></td>
                      <td><?echo $rec['cnt_s']?></td>
                      <td><?echo $rec['cnt_d']?></td>
                      <td><?echo $rec['cnt_p']?></td>
                      <td><?echo $rec['group_name']?></td>
                      <td><?echo date('d.m.Y H:i',$rec['c_data'])?><??></td>
                  </tr><?
              }
          ?></table><?
          //damp_array($rec) ;
      }


   function print_template_CSV($list_recs,$options=array())
   {  //damp_array($list_recs,1,-1) ;
     $i=$options['from'] ;
     if (_sizeof($list_recs)) foreach($list_recs as $rec)
       { $data=array() ;
         $data['ФИО сотрудника']=$rec['obj_name'] ;
         $data['ДатаРожд']=$rec['dr'] ;
         $data['Организация']=$rec['_org_name'] ;
         $data['Подразделение']=$rec['_otdel_name'] ;
         $data['GUID']=$rec['uid'] ;
         $data['Пропуск']=$rec['propusk'] ;
         $data['САД']=$rec['cnt_s'] ;
         $data['ДАД']=$rec['cnt_d'] ;
         $data['Пульс']=$rec['cnt_p'] ;
         $data['Группа']=$rec['group_name'] ;
         $data['Дата предоставления']=date('d.m.Y H:i',$rec['c_data']) ;

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

   }



}

?>
