<?php
// страница не используется, в будущем на удаление
include_once("c_ot.php");
//include_once(_DIR_TO_ENGINE.'/admin/i_admin_system.php') ;
class c_page_cab_ot_ball extends c_page_cab_ot
{
 public $h1='Баллы ВПФ' ;
 public $objPHPExcel ;
 public $aSheet ;
 public $csv_headers ;
 public $db_headers ;
 public $setting ;
 public $table_name ;
 public $setting_name ;

 function block_main()
 {   ?><script src="/AddOns/jstree/dist/jstree.min.js"></script>
      <link rel="stylesheet" href="/AddOns/jstree/dist/themes/default/style.css" />
    <?

   $this->page_title() ;
   $this->setting() ;
   if (!_sizeof($this->filter)) return ;
   //damp_array($this->filter,1,-1) ;
   $_usl=array() ;
   $_usl[]='t1.clss=232' ;
   if ($this->filter['org_ids']) $_usl[]='org_id in ('.implode(',',array_keys($this->filter['org_ids'])).')' ;
   if ($this->filter['otdel_ids']) $_usl[]='otdel_id in ('.implode(',',array_keys($this->filter['otdel_ids'])).')' ;
   $usl=implode(' and ',$_usl) ;
   $recs=execSQL('select t1.*,
                  t4.obj_name as org_name, 
                  t5.obj_name as otdel_name, 
                  t2.obj_name as work_name, 
                  t3.number as vpf_number,t3.obj_name as vpf_name,t3.parent2 as vpf_parent2 
                  from list_workings_fvp t1
                  left join list_workings t2 on t2.pkey=t1.work_id 
                  left join list_vpf t3 on t3.pkey=t1.vpf_id 
                  left join obj_site_personals t4 on t4.pkey=t1.org_id 
                  left join obj_site_personals t5 on t5.pkey=t1.otdel_id 
                  where '.$usl.'
                  order by org_name,otdel_name,work_name
                  ') ;

     $level2=execSQL('select pkey,number,obj_name,level,parent1,parent2,parent3,parent4 from list_vpf where clss=233 and level=2 order by number ',2) ;

     $arr_vpf=execSQL('select t1.org_id,t1.otdel_id,t1.work_id,t1.vpf_id,t2.number,t2.parent2 as section_id
                       from list_workings_fvp t1
                       left join list_vpf t2 on t2.pkey=t1.vpf_id') ;

     $recs=execSQL('select * from list_ot_sout order by obj_name') ;
     $arr2=array() ;
     // считаем сколько должностей в каждой организации
     if (_sizeof($recs)) foreach($recs as $rec_ot_sout)   $arr2[$rec_ot_sout['dep_id']][$rec_ot_sout['work_id']]++ ;



     $cnt_to_level=array() ;  $vpf_by_level=array() ; $org_otdel_working=array() ; $org_names=array() ; $otdel_names=array() ; $org_otdel_working_vpf_ball=array() ;$org_otdel_working_vpf_pkey=array() ;
     foreach($recs as $rec)
     { //$cnt_to_level[$rec['vpf_parent2']]++ ;
       //$vpf_by_level[$rec['vpf_parent2']][$rec['vpf_id']]=array('number'=>$rec['vpf_number'],'name'=>$rec['vpf_name']) ;
       //$org_otdel_working[$rec['org_id']][$rec['otdel_id']][$rec['work_id']]=$rec['work_name'] ;
       $org_otdel_working_vpf_ball[$rec['org_id']][$rec['otdel_id']][$rec['work_id']][$rec['vpf_id']]=($rec['ball'])? $rec['ball']:0 ;
       $org_otdel_working_vpf_pkey[$rec['org_id']][$rec['otdel_id']][$rec['work_id']][$rec['vpf_id']]=$rec['pkey'] ;
       $org_names[$rec['org_id']]=$rec['org_name'] ;
       $otdel_names[$rec['otdel_id']]=$rec['otdel_name'] ;
     }

     //damp_array($level2) ;
     ?><table class="basic fz_small auto" id="table_ball_select">
             <tr>
                 <th rowspan="2">Организация/Подразделение</th>
                 <th rowspan="2">Должность</th>
                 <?foreach($level2 as $rec) if (isset($cnt_to_level[$rec['pkey']])){?><th colspan="<?echo _sizeof($vpf_by_level[$rec['pkey']])?>"><?echo $rec['obj_name']?></th><?}?>
             </tr>
             <tr>
                 <?foreach($level2 as $rec) if (isset($vpf_by_level[$rec['pkey']])) foreach($vpf_by_level[$rec['pkey']] as $vpf_id=>$rec_vpf)  {?><th title="<?echo $rec_vpf['number'].' '.$rec_vpf['name']?>"><?echo $rec_vpf['number']?></th><?}?>
             </tr>
          <?

     //if (_sizeof($org_otdel_working)) foreach($org_otdel_working as $org_id=>$arr_otdels)
     $last_dep=0 ;
     if (_sizeof($recs)) foreach($recs as $rec_ot_sout)
     {   $otdel_id=$rec_ot_sout['dep_id']   ;
         $work_id=$rec_ot_sout['work_id']   ;



         //$view_td_org=1 ;
         // считаем число должностей у всех подразделений организации (без уникальности)
         //$cnt_otdel_works_in_org=0 ;
         //if (_sizeof($arr_otdels)) foreach($arr_otdels as $otdel_id=>$arr_works) $cnt_otdel_works_in_org+=_sizeof($arr_works) ;

         //if (_sizeof($arr_otdels)) foreach($arr_otdels as $otdel_id=>$arr_works)
         {  $view_td_otdel=1 ;
           // if (_sizeof($arr_works)) foreach($arr_works as $work_id=>$work_name)
            {  ?><tr>
                <?  if ($last_dep!=$otdel_id)
                                { ?><td class="left" rowspan="<?echo _sizeof($arr2[$rec_ot_sout['dep_id']])?>"><?echo $rec_ot_sout['obj_name']?> <a class="fa fa-sliders v2" cmd="ot/get_panel_setting_vpf" otdel_id="<?echo $otdel_id?>">ред.</a></td><?
                                  $last_dep=$otdel_id ;
                                }
                           ?>


                <?/*if ($view_td_org){?><td class="left" rowspan="<?echo $cnt_otdel_works_in_org?>"><span class="name v2" cmd="ot/get_panel_setting_vpf" org_id="<?echo $org_id?>"><?echo $org_names[$org_id]?></span></td><? $view_td_org=0 ; }*/?>
                <?/*if ($view_td_otdel){?><td class="left" rowspan="<?echo _sizeof($arr_works)?>"><span class="name v2" cmd="ot/get_panel_setting_vpf" org_id="<?echo $org_id?>" otdel_id="<?echo $otdel_id?>"><?echo $otdel_names[$otdel_id]?></span></td><? $view_td_otdel=0 ; }*/?>
                    <td class="left"><span class="name v2" cmd="ot/get_panel_setting_vpf" otdel_id="<?echo $otdel_id?>" working_id="<?echo $work_id?>"><?echo _IL('IL_works')->recs[$rec_ot_sout['work_id']]['obj_name']?></span></td>
                    <?foreach($level2 as $rec) if (isset($vpf_by_level[$rec['pkey']])) foreach($vpf_by_level[$rec['pkey']] as $vpf_id=>$rec_vpf) if (isset($org_otdel_working_vpf_ball[$org_id][$otdel_id][$work_id][$vpf_id]))
                    {
                      $ball=$org_otdel_working_vpf_ball[$org_id][$otdel_id][$work_id][$vpf_id];
                      if ($ball)
                      { $t=$ball-(int)$ball ;
                        if (!$t) $ball=(int)$ball ;
                      } else $ball=0 ;
                      ?><td class="vpf_ball" vpf_pkey=<?echo $org_otdel_working_vpf_pkey[$org_id][$otdel_id][$work_id][$vpf_id]?> title="<?echo $rec_vpf['number'].' '.$rec_vpf['name']?>"><?echo $ball?></td><?
                    }
                    else
                    {?><td class="space"></td><?}?>
                 </tr>
               <?
            }
         }
     }
     ?></table><?

     //execSQL('select * from list_workings_fvp where org_id=9156 order by org_id,otdel_id,work_id',2) ;
 }

    function setting($options=array())
   {   if (!is_array($options['disabled_filter'])) $options['disabled_filter']=array() ;
       $arr_orgs_ids=PERSONAL()->get_arr_orgs_to_cur_account('',array('view_disabled'=>$this->filter['status'])) ;
       $arr_otdel_ids=(_sizeof($this->filter['org_ids']))? PERSONAL()->get_arr_otdels_to_cur_account($this->filter['org_ids']):array() ;
       ?><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('org_ids','Организация:',4,$arr_orgs_ids) ;
                               $this->filter_section_select_multiple('otdel_ids','Подразделение:',4,$arr_otdel_ids) ;
                            ?>
                        </div>
                    </fieldset>
                   <div id="panel_buttons"><input type="submit" class="button v2" cmd="set_filter" value="<?echo ($this->editing)? 'Получить данные':'Применить'?>"></div>
                </form>
         </div><br>
       <?
    }

}



?>



?>
