<?php
include_once("c_ot.php");
class c_page_cab_ot_inrm 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()
 {   ?><button class="v1" cmd="update_pmo_data1">Обновить даты ПМО для участия без сообщений</button><?
    //$recs=execSQL_line('select distinct(inrm) from obj_site_workers') ;
     $arr_rm_vpf2=array() ;$arr_rm_vpf_period=array() ;
     $arr_rm_vpf=execSQL('select t1.inrm,t1.vpf_id,t2.number,t2.parent2 as section_id,t2.period 
                          from list_rm_fvp t1 
                          left join list_vpf t2 on t2.pkey=t1.vpf_id
                          order by t1.inrm,t2.number 
                          ',0,1) ;
     if (_sizeof($arr_rm_vpf)) foreach($arr_rm_vpf as $rec_rm_vpf)
         { $arr_rm_vpf2[$rec_rm_vpf['inrm']][$rec_rm_vpf['section_id']][$rec_rm_vpf['vpf_id']]='<span class="period_'.$rec_rm_vpf['period'].'">'.trim($rec_rm_vpf['number']).'</span>' ;

           if ($rec_rm_vpf['period']>0 and $rec_rm_vpf['inrm'] and !$arr_rm_vpf_period[$rec_rm_vpf['inrm']]) $arr_rm_vpf_period[$rec_rm_vpf['inrm']]=$rec_rm_vpf['period'] ;
           if ($rec_rm_vpf['period']>0 and $arr_rm_vpf_period[$rec_rm_vpf['inrm']]>0 and $rec_rm_vpf['period']<$arr_rm_vpf_period[$rec_rm_vpf['inrm']]) $arr_rm_vpf_period[$rec_rm_vpf['inrm']]=$rec_rm_vpf['period'] ;
         }

     //$recs=execSQL('select distinct(personal_id),count(pkey) as cnt from obj_site_events_pass where event_id=6 group by personal_id having cnt>1',2) ;
     //$recs=execSQL('select distinct(personal_id),count(pkey) as cnt from obj_site_workers group by personal_id having cnt>1',2) ;
     //$recs=execSQL('select distinct(inrm),count(pkey) as cnt from list_inrm group by inrm having cnt>1',2) ;

     $usl_exists_message='t4.pkey is null' ;
     //$usl_exists_message='not t4.pkey is null' ;
     $recs=execSQL('select t1.pkey,t1.personal_id,t2.inrm,t1.status,t1.data_from as c_data,t1.data_to as r_data,t1.period as pmo_period,t3.period*12 as inrm_period,
              t4.pkey as message_id,t4.obj_name as message_text,t4.status as message_status
              from obj_site_events_pass t1
              left join obj_site_workers t2 on t2.personal_id=t1.personal_id 
              left join list_inrm t3 on t3.inrm=t2.inrm 
              left join obj_site_personal_mess t4 on t4.personal_id=t1.personal_id and t4.source_pkey=t1.pkey 
              where t1.event_id=4 and t1.status>1 and t2.inrm>"" and t1.period!=t3.period*12 and '.$usl_exists_message.'  
              order by t1.pkey
              
            ',2,1) ;


     if (_sizeof($recs)) foreach ($recs as $rec)
     {  // назначаем время окончания ПМО в соответствии с новым сроком ПМО
        $t_start=getdate($rec['c_data']) ;
        $t_end=mktime(23,59,59,$t_start['mon']+$rec['inrm_period'],$t_start['mday'],$t_start['year']) ;
          if ($t_end!=$rec['r_data'])
        { echo 'personal_id: '.$rec['personal_id'].': '.$rec['pmo_period'].' => '.$rec['inrm_period'].' <spab class="greeb"></spab>, PMO date: '.date('d.m.Y',$rec['r_data']).' => '.date('d.m.Y',$t_end).'<br>' ;
          execSQL_update('update obj_site_events_pass set data_to='.$t_end.',period='.$rec['inrm_period'].' where pkey='.$rec['pkey']) ;
        }
        else
        { echo 'personal_id: '.$rec['personal_id'].': '.$rec['pmo_period'].' => '.$rec['inrm_period'].'<br>' ;
          execSQL_update('update obj_site_events_pass set period='.$rec['inrm_period'].' where pkey='.$rec['pkey']) ;
        }

     }

 }

 function update_pmo_data1($options=array())
 {

  }

}

?>
