<?php
//include_once("c_esmo_gateway_upload.php");
include_once("c_ot.php");
//include_once(_DIR_TO_ENGINE.'/admin/i_admin_system.php') ;
class c_page_cab_ot_sout 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_VPF=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
                  ') ;

     $vpf_by_parent=group_by_field('otdel_id',$recs_VPF) ;

     $level2=execSQL('select pkey,number,obj_name,level,parent1,parent2,parent3,parent4 from list_vpf where clss=233 and level=2 order by number ') ;
     execSQL_update('delete from obj_site_personals where pkey in (9594,9596,9598)') ;
     $recs_deps=execSQL('select * from obj_site_personals where clss in (1,207,208) order by obj_name') ;
     if (_sizeof($recs_VPF)) foreach($recs_VPF as $rec_VPF)   $recs_deps[$rec_VPF['otdel_id']]['_is_set_vpf']=1 ;
     //print_2x_arr($recs_deps) ;




     //TREE('obj_site_personals')->upload_tree_objects('clss in (1,207,208)',array('debug'=>2,'order'=>'obj_name')) ;
     //damp_array(TREE('obj_site_personals')->recs) ;
     //TREE('obj_site_personals')->create_model(array()) ;
     //damp_array(TREE('obj_site_personals')) ;

     $this->convert_list_to_tree2($recs_deps,$arr)    ;
     $this->full_tree_vpf_status($arr)    ;
     //damp_array($arr) ;
     $arr_deps=array() ;
     $this->convert_tree_to_list($arr,$arr_deps,'') ;
     //damp_array($arr_deps) ;

     $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() ;  $org_otdel_working_rec=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_rec[$rec['org_id']][$rec['otdel_id']][$rec['work_id']]=$rec ;
       $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'] ;
     }

     ?><table class="basic fz_small auto" id="table_ball_select">
             <tr>
                 <th rowspan="2">Организация</th>
                 <th rowspan="2">Подразделение</th>
                 <th rowspan="2">Профессия работника</th>
                 <th colspan="4">Классы условий труда</th>
                 <th rowspan="2" class="vert"><div>Итоговый класс УТ</div></th>
                 <th rowspan="2" class="vert"><div>Итоговый класс УТ с учетом СИЗ</div></th>
                 <th rowspan="2" class="vert"><div>Повышенный размер оплаты труда</div></th>
                 <th rowspan="2" class="vert"><div>Ежегодный дополнительный оплачиваемый отпуск</div></th>
                 <th rowspan="2" class="vert"><div>Сокращенная продолжительность рабочего времени</div></th>
                 <th rowspan="2" class="vert"><div>Молоко или другие равноценные пищевые продукты</div></th>
                 <th rowspan="2" class="vert"><div>Лечебно-профилактическое питание</div></th>
                 <th rowspan="2" class="vert"><div>Льготное пенсионное обеспечение</div></th>
                 <th rowspan="2" class="vert"><div>Итоговый класс УТ по предыдущей АРМ</div></th>
                 <th rowspan="2" class="vert"><div>Изменение (АРМ - СОУТ)</div></th>
                 <th colspan="6">Факт льготы</th>
                 <th rowspan="2" class="vert"><div>Кол-во работающих</div></th>
                 <th rowspan="2" class="vert"><div>Из них женщин</div></th>
             </tr>
             <tr>
                 <?foreach($level2 as $rec) if (isset($cnt_to_level[$rec['pkey']])){?><th class="vert"><div><?echo $rec['obj_name']?></div></th><?}?>
                 <th class="vert"><div>Повышенный размер оплаты труда</div></th>
                 <th class="vert"><div>Ежегодный дополнительный оплачиваемый отпуск</div></th>
                 <th class="vert"><div>Сокращенная продолжительность рабочего времени</div></th>
                 <th class="vert"><div>Молоко или другие равноценные пищевые продукты</div></th>
                 <th class="vert"><div>Лечебно-профилактическое питание</div></th>
                 <th class="vert"><div>Льготное пенсионное обеспечение</div></th>
             </tr>
          <?

     if (_sizeof($arr_deps)) foreach($arr_deps as $dep_id=>$dep_name)
     //if (_sizeof($org_otdel_working)) foreach($org_otdel_working as $org_id=>$arr_otdels)
     {   ?><tr><td colspan="10" class="left"><?echo $dep_name?></td></tr><?

         //$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)
            if (_sizeof($vpf_by_parent[$dep_id])) foreach($vpf_by_parent[$dep_id] as $vpf_rec)
            {  ?><tr>
                    <td class="left"><span class="name v2" cmd="ot/get_panel_setting_vpf" dep_id="<?echo $vpf_rec['parent']?>" otdel_id="<?echo $otdel_id?>" working_id="<?echo $work_id?>"><?echo $work_name?></span></td>
                    <? $ball_res=0 ;
                    foreach($level2 as $rec)
                    {   $max_ball=0 ;
                        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>$max_ball) $max_ball=$ball ;
                          if ($ball>$ball_res) $ball_res=$ball ;
                        }
                        if ($max_ball)
                        { $t=$max_ball-(int)$max_ball ;
                          if (!$t) $max_ball=(int)$max_ball ;
                          ?><td><?echo $max_ball?></td><?
                        }
                        else {?><td class="space"></td><?}
                    }
                    ?>
                    <td><?
                        if ($ball_res)
                        { $t=$ball_res-(int)$ball_res ;
                          if (!$t) $ball_res=(int)$ball_res ;
                          echo $ball_res ;
                        }
                        ?>
                    </td>
                    <td><?echo $this->show_element_input('ball_safe',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['ball_safe'])?></td>
                    <td><?echo $this->show_element_checkbox('oplata',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['oplata'])?></td>
                    <td><?echo $this->show_element_checkbox('otpusk',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['otpusk'])?></td>
                    <td><?echo $this->show_element_checkbox('worktime',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['worktime'])?></td>
                    <td><?echo $this->show_element_checkbox('milk',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['milk'])?></td>
                    <td><?echo $this->show_element_checkbox('pitanie',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['pitanie'])?></td>
                    <td><?echo $this->show_element_checkbox('pensia',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['pensia'])?></td>
                    <td><?echo $this->show_element_input('ball_old',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['ball_old'])?></td>
                    <td></td>
                    <td><?echo $this->show_element_checkbox('oplata',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['oplata'])?></td>
                    <td><?echo $this->show_element_checkbox('otpusk',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['otpusk'])?></td>
                    <td><?echo $this->show_element_checkbox('worktime',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['worktime'])?></td>
                    <td><?echo $this->show_element_checkbox('milk',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['milk'])?></td>
                    <td><?echo $this->show_element_checkbox('pitanie',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['pitanie'])?></td>
                    <td><?echo $this->show_element_checkbox('pensia',$org_otdel_working_rec[$org_id][$otdel_id][$work_id]['pensia'])?></td>
                    <td></td>
                    <td></td>
                 </tr>

               <?
            }
         }
     }
     ?></table>
        <style type="text/css">
            table.basic th.vert
            { padding:0;
              text-align: center;
              vertical-align: middle;
              width: 2em;
              font-weight:normal;
              line-height:1.1em;
              position:relative;


            }

            table.basic th.vert div
            { -moz-transform: rotate(-90.0deg);  /* FF3.5+ */
              -o-transform: rotate(-90.0deg);  /* Opera 10.5 */
              -webkit-transform: rotate(-90.0deg);  /* Saf3.1+, Chrome */
              filter:  progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083);  /* IE6,IE7 */
              -ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083)"; /* IE8 */
              margin-left: -0.1em;
              margin-right: -0.1em;

            }

            table.basic input[type="text"]{width:50px;text-align:center;}

        </style>
        <script type="text/javascript">
            $j(document).ready(function() {
              //$j('.vert').css('height', $j('.vert').width());
              //
              $j('.vert').css('height','130px');
              //$j('.vert div').css('width','130px');
              $j('.vert').css('width','50px');
              //
            });
        </script>
      <?

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


 function full_tree_vpf_status(&$arr)
 {  $vpf_parent=0 ;
    if (_sizeof($arr)) foreach($arr as $id=>$node_info)
    {
        unset($node_info['name']) ;
        unset($node_info['level']) ;
        unset($node_info['vpf']) ;
        $vpf=0 ;
        //Допуск уже запрещенif (_sizeof($node_info)) $vpf=$this->full_tree_vpf_status($arr[$id]) ;
        $arr[$id]['vpf2']=$vpf ;
        if ($vpf) $vpf_parent=1 ;
    }
    return($vpf_parent) ;

 }

    function convert_list_to_tree2(&$recs,&$arr,$parent=0,$arr_allow_ids=array())
    { if (_sizeof($recs))  foreach($recs as $id=>$rec) if ($rec['parent']==$parent) { $arr[$rec['pkey']]=array('name'=>$rec['obj_name'],'level'=>$rec['level'],'vpf'=>$rec['_is_set_vpf']) ; unset($recs[$id]) ; }
      if (_sizeof($arr)) foreach ($arr as $child_pkey=>$arr_child_ids) if ($child_pkey!='name' and $child_pkey!='level' and $child_pkey!='vpf') $this->convert_list_to_tree2($recs,$arr[$child_pkey],$child_pkey,$arr_allow_ids) ;
    }


 function convert_tree_to_list($arr,&$recs,$name)
 { if (_sizeof($arr)) foreach($arr as $id=>$node_info)
   { if ($node_info['level']>1) $recs[$id]=($name)? $name.' / '.$node_info['name'].'['.$node_info['level'].']': $node_info['name'];
     unset($node_info['name']) ;
     unset($node_info['level']) ;
     unset($node_info['vpf']) ;
     if (_sizeof($node_info)) $this->convert_tree_to_list($node_info,$recs,$recs[$id]) ;
   }
 }

 function show_element_checkbox($name,$value)
 {
   ?><input type="hidden" name="data[oplata]" value="0"><input type="checkbox" name="data[<?echo $name?>]" value="1" <?if ($value) echo 'checked'?>><?
 }

 function show_element_input($name,$value)
 {
   ?><input type="text" name="data[<?echo $name?>]" value="<? echo $value?>"><?
 }

    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>
       <?
    }

}


?>
