<?
 function panel_rmv_stats()
 { ?><div id="panel_rmv_stats"><?
     //$from=(MEMBER()->info['smena_in'])? MEMBER()->info['smena_in']:MEMBER()->info['time_login'] ;
     //echo 'MEMBER()->cur_group_id='.MEMBER()->cur_group_id.'<br>' ;
     $from=(MEMBER()->info['smena_in']>0)? MEMBER()->info['smena_in']:0 ;
     
     $terminal_ids=DEVICE()->get_terminals_ids(MEMBER()->cur_zone_id,'kt') ;
     if (!$terminal_ids) { ?><div class="alert">В вашей рабочей группе отсутствуют терминалы</div><? return ;}

     if (!$from) $from=0 ;
     //$usl_based='c_data>='.$from.' and (terminal_id in ('.$terminal_ids.') or terminal_id is null)' ;
     // переводим условие на работу с группами, а не терминалами
     $usl_based='c_data>='.$from.' and group_id='.MEMBER()->cur_group_id.' ' ;
     // только для доктора
     // // заремирован отбор по доктору, так как фельдщера должны видеть все МО в пределах своей группы
     if (MEMBER()->rol==2) $usl_based.='and ((doctor_id='.MEMBER()->id.' or dopusk_user_id='.MEMBER()->id.' or hand_check_user_id='.MEMBER()->id.') or doctor_id is null)' ;

     $stats=execSQL_row('select dopusk,count(DISTINCT(member_id)) from obj_site_mo_session where '.$usl_based.' and member_id>0  group by dopusk order by  dopusk desc') ;
     //damp_array($stats) ;
     //execSQL('select DISTINCT(member_id) from obj_site_mo_session where member_id>0 and c_data>='.MEMBER()->info['smena_in'].'  ',2) ;
     $count_personal=execSQL_value('select count(DISTINCT(member_id)) from obj_site_mo_session where '.$usl_based.' and status in (1,2,3)') ;
     //execSQL('select member_id from obj_site_mo_session where  status=3 and c_data>='.MEMBER()->info['smena_in'].' and member_id not in (select DISTINCT(member_id) from obj_site_mo_session where c_data>='.MEMBER()->info['smena_in'].' and status in (1,2,3))',2) ;
     //execSQL('select * from obj_site_mo_session where member_id=0 and c_data>='.MEMBER()->info['smena_in'],2) ;
     $now=getdate() ;
     $start_check=mktime($now['hours']-2,$now['minutes'],$now['seconds'],$now['mon'],$now['mday'],$now['year']);
     //$count_to_check=execSQL_value('select count(DISTINCT(member_id)) from obj_site_mo_session where '.$usl_based.' and (status=3 or (status=4 and (reason="nonRule" or reason="failedMO"))) and (dopusk is null or dopusk=0) and tk=0 and not member_id in (select distinct(member_id) from obj_site_mo_session where c_data>='.$from.'                     and dopusk in (1,2))') ;
     $count_to_check=execSQL_value('select count(DISTINCT(member_id)) 
                                    from obj_site_mo_session 
                                    where c_data>='.$start_check.' and group_id='.MEMBER()->cur_group_id.' 
                                          and (status=3 or (status=4 and (reason="nonRule" or reason="failedMO"))) 
                                          and (dopusk is null or dopusk=0) 
                                          and tk in (0,2) 
                                          and not member_id in (select distinct(member_id) from obj_site_mo_session where c_data>='.$start_check.' and dopusk in (1,2))') ;

     //execSQL('select DISTINCT(member_id) as member_id,(select obj_name from obj_site_personals where pkey=member_id) as member_name from obj_site_mo_session where c_data>='.MEMBER()->info['smena_in'].' and status=3  and (dopusk is null or dopusk=0) and  tk=0',2,-1) ;
     $s2=$stats[1] ; if (!$s2) $s2=0 ;
     $s3=$stats[2] ; if (!$s3) $s3=0 ;
     $s1=$s2+$s3 ;
     $d=time()-MEMBER()->info['smena_in'] ;
     ?><table id="mo_stats">
       <tr><td>Время начала смены</td><td>Длительность смены</td><td>Число сотрудников</td><td>Ожидается на проверку</td><td>Пройдено МО</td><td>Допущено</td><td>Не допущено</td></tr>
       <tr><td><?echo date('d.m.Y H:i',MEMBER()->info['smena_in'])?></td>
           <td><?echo gmstrftime('%H:%M',$d)?></td>
           <td><?echo $count_personal?></td>
           <td class="v1" href="/cab/rmv/[skoro,cur_smena]/"><?echo $count_to_check?></td>
           <td class="v1" href="/cab/rmv/[cur_smena]/"><?echo $s1?></td>
           <td class="v1" href="/cab/rmv/[dopusk=(1),cur_smena]/"><?echo $s2?></td>
           <td class="v1" href="/cab/rmv/[dopusk=(2),cur_smena]/"><?echo $s3?></td>
       </tr>
     </table>
     <?
   ?></div><?
 }

// select t1.*,t2.obj_name as member_name,t2.dr as member_dr,t2.pol as member_pol,t2.demo_access as member_demo_access,t5.obj_name as terminal_name,t2.pkey as member_id,t2.working,t2.age,t2.propusk,t3.pkey as _otdel_id,t3.obj_name as _otdel_name,t4.pkey as _ord_id,t4.obj_name as _org_name
//  from obj_site_mo_session t1
//  left join obj_site_personals t2 on t2.pkey=t1.member_id
//  left join obj_site_personals t3 on t3.pkey=t2.parent
//  left join obj_site_personals t4 on t4.pkey=t3.parent
//  left join obj_site_account t5 on t5.id=t1.terminal_id and t5.clss=203
//                                                           where t1.clss=250 and t1.c_data>=1440664604 and (t1.terminal_id in (1) or t1.terminal_id is null) and (t1.doctor_id=318 or dopusk_user_id=318 or hand_check_user_id=318) and (t1.status=3 or (t1.status=4 and (reason="nonRule" or reason="failedMO"))) and (t1.dopusk is null or t1.dopusk=0) and t1.tk=0 and not member_id in (select distinct(member_id) from obj_site_mo_session where c_data>=1440664604 and dopusk in (1,2)) and (t1.report_id=0 or t1.report_id is null) order by t1.pkey desc
//select count(DISTINCT(member_id)) from obj_site_mo_session where                    c_data>=1440664604 and                                                                                                                                 (status=3 or (   status=4 and (reason="nonRule" or reason="failedMO"))) and (   dopusk is null or    dopusk=0) and    tk=0 and not member_id in (select distinct(member_id) from obj_site_mo_session where c_data>=1440664604 and dopusk in (1,2))
?>