<?
 function panel_stats()
 { $list_group=execSQL('select t1.pkey,t1.obj_name,t2.obj_name as doctor_name,t2.smena_in 
                            from obj_site_account t1 
                            left join obj_site_account t2 on t2.group_id=t1.pkey and t2.smena=1 
                            where t1.clss=206 and t1.enabled=1 and t1.pkey!=1 
                            order by t1.obj_name') ;

   $now=getdate() ;
   $start_day=mktime(0,0,0,$now['mon'],$now['mday'],$now['year']);

   ?><div id="panel_rmv_stats"><table class="basic full">
     <tr><th>Группа терминалов</th><th>Дежурный фельдшер</th><th>Время начала смены</th><th>Длительность смены</th><th>Проверено сотрудников</th><th>Ожидается на проверку</th><th>Пройдено МО</th><th>Допущено</th><th>Не допущено</th></tr>
     <?
     if (_sizeof($list_group)) foreach($list_group as $rec)
     {
         

     $usl_based='c_data>='.$start_day.' and group_id='.$rec['pkey'].' ' ;

     $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 c_data desc') ;
     $count_personal=execSQL_value('select count(DISTINCT(member_id)) from obj_site_mo_session where '.$usl_based.' and status in (1,2,3)') ;
     $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>='.$start_day.' 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()-$rec['smena_in'] ;
     ?><tr></tr>
       
       <tr><td><?echo $rec['obj_name']?></td>
           <td class="left"><?echo $rec['doctor_name']?></td>
           <td><?echo date('d.m.Y H:i',$rec['smena_in'])?></td>
           <td><?echo gmstrftime('%H:%M',$d)?></td>
           <td><?echo $count_personal?></td>
           <td><?echo $count_to_check?></td>
           <td><?echo $s1?></td>
           <td><?echo $s2?></td>
           <td><?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.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))
?>
