<?php
$__functions['init'][]='_ot_site_vars' ;
$__functions['boot_site'][]='_ot_site_boot' ;

function OT() {return($_SESSION['ot_system']);$_SESSION['ot_system']=new c_ot_system();return($_SESSION['ot_system']);} ;


function _ot_site_vars()
{

}

function _ot_site_boot($options)
{
  create_system_modul_obj('ot',$options) ;
}


class c_ot_system
{
  function __construct($create_options=array())
  {
    ENGINE()->reg_ext('ot','Охрана труда')  ;
  }

	function declare_menu_items_1($page)
   {

       $page->menu['/cab/ot/']=array('title'=>'Охрана труда',
                                          'class'=>'fas fa-life-ring',
                                          //'items'=>array('/cab/docs/'=>array('title'=>'Документы','class'=>'fa fa-book')

                                          //              )
                                          ) ;
   }


 function remove_last_point($num)
 {
     if (substr(trim($num),-1)=='.') $str=substr(trim($num),0,strlen(trim($num))-1) ;
     else                            $str=$num ;
     return($str) ;
 }

 function get_period_PMO_by_vpf($inrm)
 {
   $value=execSQL_value('select min(t2.period) 
                         from list_rm_fvp t1
                         left join list_vpf t2 on t2.pkey=t1.vpf_id
                         where t1.inrm="'.$inrm.'" and t2.period>0',0,1) ;
   if (!$value) $value=12 ;
  if ($value==1) $value=12 ;
if ($value==2) $value=24 ;
   return($value) ;
 }

 function get_id_period_PMO_by_vpf($inrm)
 {
   $value=execSQL_value('select min(t2.period) 
                         from list_rm_fvp t1
                         left join list_vpf t2 on t2.pkey=t1.vpf_id
                         where t1.inrm="'.$inrm.'" and t2.period>0',0,1) ;
  if (!$value) $value=12 ;
  if ($value==1) $value=12 ;
  if ($value==2) $value=24 ;
   return($value) ;
 }

 function get_arr_302_29()
 {
    $recs=execSQL('select * from ot_diff_302_29') ;
    $arr=[] ;
    foreach($recs as $rec) if ($rec['A_staryy_nomer'])
    {   $old_num=$rec['A_staryy_nomer'] ;
        $old_num=OT()->remove_last_point($old_num) ;
        $old_num=str_replace('Приложение 2 п.','2/',$old_num) ;
        $new_num=($rec['C_novyy_nomer'])? $rec['C_novyy_nomer']:'-' ;
        $new_num=OT()->remove_last_point($new_num) ;
        $arr[$old_num][]=$new_num ;
    }
    $arr_str=[] ;
    foreach($arr as $id=>$arr2) if (sizeof($arr2)) $arr_str[trim($id)]=implode(', ',$arr2) ;
    return($arr_str) ;
 }

function set_inrm_to_personals()
   {
       $recs_persons_types=execSQL('select parent1,parent2,parent3,parent4,parent5,parent6,parent7,parent8,parent9,work_id
                                    from obj_site_workers 
                                    where work_id>0
                                      group by parent1,parent2,parent3,parent4,parent5,parent6,parent7,parent8,parent9,work_id
                                    
                                    ',0,1) ;
       if (_sizeof($recs_persons_types)) foreach($recs_persons_types as $rec)
       {   $otdel_id=0 ;
           $work_id=($rec['work_id'])? $rec['work_id']:0 ;
           if ($rec['parent9']) $otdel_id=$rec['parent9'] ;
           else if ($rec['parent8']) $otdel_id=$rec['parent8'] ;
           else if ($rec['parent7']) $otdel_id=$rec['parent7'] ;
           else if ($rec['parent6']) $otdel_id=$rec['parent6'] ;
           else if ($rec['parent5']) $otdel_id=$rec['parent5'] ;
           else if ($rec['parent4']) $otdel_id=$rec['parent4'] ;
           else if ($rec['parent3']) $otdel_id=$rec['parent3'] ;
           $inrm=$rec['parent2'].'.'.$otdel_id.'.'.$work_id ;
           if (!$otdel_id) $otdel_id=$rec['parent2'] ;

           $sql='update obj_site_workers set inrm="'.$inrm.'" where parent2='.$rec['parent2'].' and parent='.$otdel_id.' and work_id='.$rec['work_id'] ;
           //echo $sql.'<br>' ;

           execSQL_update($sql) ;
       }

   }



}

?>
