<?php
class report_itog1 extends c_report
{
 var $title='Общий отчет' ;
 var $filter=array() ;
 //var $PDF_master='TCPDF' ;  // короткие отчеты выгружвать через обычный  PDF

 function setting($options=array())
{
  $now=getdate() ;
  if (!$this->filter['data_from']) $this->filter['data_from']=date('d.m.Y',mktime(0,0,0,$now['mon']-1,1,$now['year']));
  if (!$this->filter['data_to'])   $this->filter['data_to']=date('d.m.Y',mktime(0,0,0,$now['mon'],1,$now['year'])-1);

  ?><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">
                           <?$arr_groups=execSQL_row('select pkey,obj_name,clss from obj_site_account where clss=206')?>
                           <section class="col col-2">
                               <label class="label">Группа:</label>
                               <label class="select">
                                   <select name="filter[group_id]"><option value="0">Все</option><?
                                     if (_sizeof($arr_groups)) foreach($arr_groups as $id=>$name) {?><option value="<?echo $id?>" <?if($this->filter['group_id']==$id) echo 'selected'?>><?echo $name?></option><?}?>
                                   </select>
                               </label>
                           </section>
                           <section class="col col-2">
                               <label class="label">От:</label>
                               <label class="input"><input type="text" name="filter[data_from]" class="datetimepicker" data-required value="<?echo $this->filter['data_from']?>"></label>
                           </section>
                           <section class="col col-2">
                               <label class="label">До:</label>
                               <label class="input"><input type="text" name="filter[data_to]" class="datetimepicker" data-required value="<?echo $this->filter['data_to']?>"></label>
                           </section>
                       </div>
                   </fieldset>
                   <?$this->panel_buttons();?>
               </form>
   </div>
  <?
}

 function HTML_report_body($options=array())
{   $res=array() ;  $arr_mo_to_orgs=array() ; $arr_mo_to_otdel=array() ; $arr_mo_to_orgs2=array() ; $arr_org_names=array() ; $arr_otdel_names=array() ; $arr_work_names=array() ;
    if ($this->filter['data_from']) $options['data_from']=$this->filter['data_from'] ;
    if ($this->filter['data_to'])   $options['data_to']=$this->filter['data_to'] ;
    if ($this->filter['group_id'])  $options['group_id']=$this->filter['group_id'] ;

    //$options['order']='t4.indx,t3.indx,c_data asc' ;
    $options['order']='indx,c_data asc' ;
    $options['no_public_info']=1 ;
    list($recs,$stats)=ESMO()->get_mo_recs($options) ;
    if (_sizeof($recs)) foreach($recs as $rec_mo)
    {  //$rec_member=PERSONAL()->get_personal_by_id($rec_mo['member_id']) ;
       if ($rec_mo['member_id'])
       {   $org_id=$rec_mo['_org_id'] ;
           $otdel_id=$rec_mo['_otdel_id'] ;
           $work_id=$rec_mo['working'] ;
       // количество МО работников
       $arr_mo_to_orgs[$org_id]++ ;
       $arr_mo_to_otdel[$org_id][$otdel_id]++ ;
       // название организации
           $arr_org_names[$org_id]=$rec_mo['_org_name'] ;
       // название отдела
           $arr_otdel_names[$otdel_id]=$rec_mo['_otdel_name'] ;
       //категория сотрудников
           $arr_work_names[$work_id]=_IL('IL_works')->recs[$rec_mo['working']]['obj_name'] ; // $rec_member['_work_name'] ;
       // Результат на ЭСМО
       if ($rec_mo['status']==2) $res[$org_id][$otdel_id][$work_id]['res1']['y']++ ;
       if ($rec_mo['status']==3) $res[$org_id][$otdel_id][$work_id]['res1']['n']++ ;
       // Результат после прохождения врача
       if ($rec_mo['dopusk']==1) $res[$org_id][$otdel_id][$work_id]['res2']['y']++ ;
       if ($rec_mo['dopusk']==2) $res[$org_id][$otdel_id][$work_id]['res2']['n']++ ;
       // отстранение по алкоголю
       if ($rec_mo['alko']==1)   $res[$org_id][$otdel_id][$work_id]['res3']++ ;
       // отстранено по ССЗ
       if ($rec_mo['question']==1)   $res[$org_id][$otdel_id][$work_id]['res4']++ ;
       // отстранено по другим причинам
       if ($rec_mo['alko']==0 and $rec_mo['question']==0 and $rec_mo['dopusk']==2)   $res[$org_id][$otdel_id][$work_id]['res5']++ ;
    }
    }
    if (_sizeof($res)) foreach($res as $org_id=>$arr_otdel)
        if (_sizeof($arr_otdel)) foreach($arr_otdel as $otdel_id=>$arr_works)
         { $arr_mo_to_orgs2[$org_id]+=_sizeof($arr_works)+1 ;
         }
    //damp_array($arr_mo_to_orgs2) ;

    //damp_array($res) ;
    //damp_array($res) ;
    //damp_array($arr_org_names) ;
    //damp_array($arr_otdel_names) ;
    //damp_array($arr_mo_to_orgs) ;
    ?><div id="reports_itog1">
      <table class="basic fz_small">
         <tr><th rowspan="2">№</th>
             <th rowspan="2">Кол-во МО</th>
             <th rowspan="2">Организация</th>
             <th rowspan="2">Подразделение</th>
             <th rowspan="2">Категория сотрудников</th>
             <th colspan="2">Результат на ЭСМО</th>
             <th colspan="2">Результат после прохождения врача</th>
             <th rowspan="2">Отстранено по алкоголю</th>
             <th rowspan="2">Отстранено по ССЗ</th>
             <th rowspan="2">Отстранено по другим причинам</th>
         </tr>
         <tr><th>Допущено</th>
             <th>Не допущено</th>
             <th>Допущено</th>
             <th>Не допущено</th>
         </tr>
     <? $i=$this->first_obj ; ; $itog=array() ;
        if (_sizeof($arr_mo_to_orgs)) foreach($arr_mo_to_orgs as $org_id=>$mo_count)
         {   $i++ ;   $itog_org=array() ;  $k=0 ;
             if (_sizeof($arr_mo_to_otdel[$org_id]))
             {    foreach($arr_mo_to_otdel[$org_id] as $otdel_id=>$mo_count2)
                      {
                           $sum=array() ; $line=0 ; $cnt_otdel=0 ;
                             if (_sizeof($res[$org_id][$otdel_id]))
                             {   foreach($res[$org_id][$otdel_id] as $work_id=>$rec_work)
                                 { ?><tr>
                                     <?if ($k==0){?><td><?echo $i?></td>
                                                    <td><?echo $mo_count ; $itog['mo_count']+=$mo_count ; ?></td>
                                                    <td class="left"><?echo $arr_org_names[$org_id]?></td><?
                                                    $k++ ;
                                                 }
                                      else{?><td></td><td></td><td></td><?}
                                      if (!$cnt_otdel){?><td class="left"><?echo $arr_otdel_names[$otdel_id]?></td><?}else{?><td></td><?}?>
                                     <td class="left"><? echo ($work_id)? $arr_work_names[$work_id]:'другие работники'  ; ?></td>
                                 <td><? echo $rec_work['res1']['y'] ; $sum['1y']+=$rec_work['res1']['y'] ; ?></td>
                                 <td><? echo $rec_work['res1']['n'] ; $sum['1n']+=$rec_work['res1']['n'] ; ?></td>
                                 <td><? echo $rec_work['res2']['y'] ; $sum['2y']+=$rec_work['res2']['y'] ; ?></td>
                                 <td><? echo $rec_work['res2']['n'] ; $sum['2n']+=$rec_work['res2']['n'] ; ?></td>
                                 <td><? echo $rec_work['res3'] ;      $sum['3']+=$rec_work['res3'] ; ?></td>
                                 <td><? echo $rec_work['res4'] ;      $sum['4']+=$rec_work['res4'] ; ?></td>
                                 <td><? echo $rec_work['res5'] ;      $sum['5']+=$rec_work['res5'] ; ?></td>
                                </tr>
                               <?
                               $line++ ;
                                   $cnt_otdel++ ;
                             }
                                if ($cnt_otdel>1)
                                {?><tr class="subitog2">
                                  <td></td>
                                  <td></td>
                                  <td></td>
                                  <td></td>
                              <td class="left">ИТОГО</td>
                                  <td><? if ($sum['1y']) echo $sum['1y'] ; $itog['1y']+=$sum['1y'] ; $itog_org['1y']+=$sum['1y'] ; ?></td>
                                  <td><? if ($sum['1n']) echo $sum['1n'] ; $itog['1n']+=$sum['1n'] ; $itog_org['1n']+=$sum['1n'] ; ?></td>
                                  <td><? if ($sum['2y']) echo $sum['2y'] ; $itog['2y']+=$sum['2y'] ; $itog_org['2y']+=$sum['2y'] ; ?></td>
                                  <td><? if ($sum['2n']) echo $sum['2n'] ; $itog['2n']+=$sum['2n'] ; $itog_org['2n']+=$sum['2n'] ; ?></td>
                                  <td><? if ($sum['3']) echo $sum['3'] ; $itog['3']+=$sum['3'] ; $itog_org['3']+=$sum['3'] ; ?></td>
                                  <td><? if ($sum['4']) echo $sum['4'] ; $itog['4']+=$sum['4'] ; $itog_org['4']+=$sum['4'] ; ?></td>
                                  <td><? if ($sum['5']) echo $sum['5'] ; $itog['5']+=$sum['5'] ; $itog_org['5']+=$sum['5'] ; ?></td>
                              </tr>
                                <?}
                                $itog['1y']+=$sum['1y'] ; $itog_org['1y']+=$sum['1y'] ;
                                $itog['1n']+=$sum['1n'] ; $itog_org['1n']+=$sum['1n'] ;
                                $itog['2y']+=$sum['2y'] ; $itog_org['2y']+=$sum['2y'] ;
                                $itog['2n']+=$sum['2n'] ; $itog_org['2n']+=$sum['2n'] ;
                                $itog['3']+=$sum['3'] ; $itog_org['3']+=$sum['3'] ;
                                $itog['4']+=$sum['4'] ; $itog_org['4']+=$sum['4'] ;
                                $itog['5']+=$sum['5'] ; $itog_org['5']+=$sum['5'] ;
                             }
                      }
                      ?><tr class="subitog1">
                              <td></td>
                             <td></td>
                             <td></td>
                             <td class="left">ИТОГО</td>
                              <td></td>
                              <td><?if ($itog_org['1y']) echo $itog_org['1y']?></td>
                              <td><?if ($itog_org['1n']) echo $itog_org['1n']?></td>
                              <td><?if ($itog_org['2y']) echo $itog_org['2y']?></td>
                              <td><?if ($itog_org['2n']) echo $itog_org['2n']?></td>
                              <td><?if ($itog_org['3']) echo $itog_org['3']?></td>
                              <td><?if ($itog_org['4']) echo $itog_org['4']?></td>
                              <td><?if ($itog_org['5']) echo $itog_org['5']?></td>
                           </tr>
                         <?
             }

         }
         ?><tr class="itog">
             <td class="left">ИТОГО</td>
             <td><?echo $itog['mo_count']?></td>
             <td></td>
             <td></td>
             <td></td>
             <td><?echo $itog['1y']?></td>
             <td><?echo $itog['1n']?></td>
             <td><?echo $itog['2y']?></td>
             <td><?echo $itog['2n']?></td>
             <td><?echo $itog['3']?></td>
             <td><?echo $itog['4']?></td>
             <td><?echo $itog['5']?></td>
         </tr>
       </table>
       <style type="text/css">
           div#reports_itog1{}
           div#reports_itog1 table{width:100%;table-layout:auto;border-collapse:collapse;}
           div#reports_itog1 th{background:#dddddd;color:black;font-weight:bold;border:1px solid #aaaaaa;}
           div#reports_itog1 td{padding:5px;border:1px solid #aaaaaa;text-align:center;}
           div#reports_itog1 td.left{text-align:left;}
           div#reports_itog1 tr.subitog1 td{font-weight:bold;background:#f1f1f1;text-align:center;}
           div#reports_itog1 tr.subitog1 td.left{text-align:left;}
           div#reports_itog1 tr.subitog2 td{font-weight:bold;background:#fafafa;text-align:center;}
           div#reports_itog1 tr.subitog2 td.left{text-align:left;}
           div#reports_itog1 tr.itog td{font-weight:bold;background:#dddddd;}
        </style>
       </div>
    <?
    $debug_db=0 ;
}



}

?>
