<?php

$__functions['init'][]      ='_pl_admin_vars' ;
$__functions['install'][]	='_pl_install_modul' ;

function _pl_admin_vars()
{
	$_SESSION['descr_clss'][780]['name']='Клиент (участник обмена)' ;
	$_SESSION['descr_clss'][780]['parent']=1 ;
	$_SESSION['descr_clss'][780]['fields']['obj_name']=array('type'=>'varchar(255)') ;
	$_SESSION['descr_clss'][780]['fields']['full_name']=array('type'=>'varchar(256)') ;
	$_SESSION['descr_clss'][780]['fields']['inn']=array('type'=>'varchar(11)') ;
	$_SESSION['descr_clss'][780]['fields']['email']=array('type'=>'varchar(255)') ;
	$_SESSION['descr_clss'][780]['fields']['phone']=array('type'=>'varchar(255)') ;
	$_SESSION['descr_clss'][780]['fields']['ext_info']=array('type'=>'text') ;

	$_SESSION['descr_clss'][781]['name']='Клиент (участник обмена)' ;
	$_SESSION['descr_clss'][781]['parent']=1 ;
	$_SESSION['descr_clss'][781]['fields']['obj_name']=array('type'=>'text') ;
	$_SESSION['descr_clss'][781]['fields']['client_id']=array('type'=>'int(11)') ;
	$_SESSION['descr_clss'][781]['index']['client_id']='client_id' ;

	$_SESSION['descr_clss'][782]['name']='ПЛ - лог запросов' ;
	$_SESSION['descr_clss'][782]['parent']=1 ;
	$_SESSION['descr_clss'][782]['fields']['ip']=array('type'=>'varchar(20)') ;
	$_SESSION['descr_clss'][782]['fields']['obj_name']=array('type'=>'varchar(20)') ;
	$_SESSION['descr_clss'][782]['fields']['url']=array('type'=>'varchar(255)') ;
	$_SESSION['descr_clss'][782]['fields']['post']=array('type'=>'varchar(255)') ;
	$_SESSION['descr_clss'][782]['fields']['result_status']=array('type'=>'int') ;

	$_SESSION['descr_clss'][783]['name']='ПЛ - лог пакетов' ;
	$_SESSION['descr_clss'][783]['parent']=1 ;
	$_SESSION['descr_clss'][783]['fields']['packet_mode']=array('type'=>'int(11)','default'=>1,'index'=>'packet_mode') ;
	$_SESSION['descr_clss'][783]['fields']['packet_type']=array('type'=>'int(11)','default'=>0,'index'=>'packet_type') ;
	$_SESSION['descr_clss'][783]['fields']['ip']=array('type'=>'varchar(20)') ;
	$_SESSION['descr_clss'][783]['fields']['result_status']=array('type'=>'int') ;
	$_SESSION['descr_clss'][783]['fields']['file_uid']=array('type'=>'varchar(255)') ;
	$_SESSION['descr_clss'][783]['fields']['knd']=array('type'=>'int') ;
	$_SESSION['descr_clss'][783]['fields']['titul_date_from']=array('type'=>'int','default'=>0,'index'=>'titul_date_from') ;
	$_SESSION['descr_clss'][783]['fields']['titul_date_to']=array('type'=>'int','default'=>0,'index'=>'titul_date_to') ;
	$_SESSION['descr_clss'][783]['fields']['pl_number']=array('type'=>'varchar(32)') ;
	$_SESSION['descr_clss'][783]['fields']['trip_type']=array('type'=>'int') ;
	$_SESSION['descr_clss'][783]['fields']['pl_uid']=array('type'=>'varchar(40)') ;
	$_SESSION['descr_clss'][783]['fields']['mo_type2_necessarily']=array('type'=>'int') ;
	$_SESSION['descr_clss'][783]['fields']['pl_date']=array('type'=>'int') ;
	$_SESSION['descr_clss'][783]['fields']['car_number']=array('type'=>'varchar(10)') ;
	$_SESSION['descr_clss'][783]['fields']['car_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][783]['fields']['pl_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][783]['fields']['status']=array('type'=>'int','default'=>0,'index'=>'status') ;
	$_SESSION['descr_clss'][783]['fields']['xml_md']=array('type'=>'varchar(32)') ;
	$_SESSION['descr_clss'][783]['fields']['xml_size']=array('type'=>'int') ;
	$_SESSION['descr_clss'][783]['fields']['sign_size']=array('type'=>'int') ;
	$_SESSION['descr_clss'][783]['fields']['error']=array('type'=>'int') ;
	$_SESSION['descr_clss'][783]['fields']['sign_uid']=array('type'=>'varchar(32)') ;
	$_SESSION['descr_clss'][783]['fields']['titul1_id']=array('type'=>'int','default'=>0,'index'=>'titul1_id') ;

	$_SESSION['descr_clss'][784]['name']='ПЛ - путевые листы' ;
	$_SESSION['descr_clss'][784]['parent']=1 ;
	$_SESSION['descr_clss'][784]['fields']['number']=array('type'=>'varchar(32)') ;
	$_SESSION['descr_clss'][784]['fields']['uid']=array('type'=>'varchar(40)') ;
	$_SESSION['descr_clss'][784]['fields']['date']=array('type'=>'int') ;
	$_SESSION['descr_clss'][784]['fields']['driver_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][784]['fields']['car_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][784]['fields']['mo1_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][784]['fields']['mo2_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][784]['fields']['packet_titul1_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][784]['fields']['packet_titul2_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][784]['fields']['packet_titul3_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][784]['fields']['packet_titul4_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][784]['fields']['packet_titul5_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][784]['fields']['packet_titul6_id']=array('type'=>'int') ;

	$_SESSION['descr_clss'][785]['name']='ПЛ - водители титула' ;
	$_SESSION['descr_clss'][785]['parent']=1 ;
	$_SESSION['descr_clss'][785]['fields']['packet_id']=array('type'=>'int(11)','index'=>'packet_id') ;
	$_SESSION['descr_clss'][785]['fields']['driver_license_number']=array('type'=>'varchar(32)') ;
	$_SESSION['descr_clss'][785]['fields']['driver_license_seria']=array('type'=>'varchar(32)') ;
	$_SESSION['descr_clss'][785]['fields']['driver_license_data']=array('type'=>'varchar(12)') ;
	$_SESSION['descr_clss'][785]['fields']['driver_fio']=array('type'=>'varchar(256)') ;
	$_SESSION['descr_clss'][785]['fields']['driver_name1']=array('type'=>'varchar(256)') ;
	$_SESSION['descr_clss'][785]['fields']['driver_name2']=array('type'=>'varchar(256)') ;
	$_SESSION['descr_clss'][785]['fields']['driver_name3']=array('type'=>'varchar(256)') ;
	$_SESSION['descr_clss'][785]['fields']['driver_uid']=array('type'=>'varchar(256)') ;
	$_SESSION['descr_clss'][785]['fields']['driver_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][785]['fields']['pl_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][785]['fields']['mo_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][785]['fields']['mo_date']=array('type'=>'int') ;
	$_SESSION['descr_clss'][785]['fields']['mo_group_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][785]['fields']['mo_doctor_id']=array('type'=>'int') ;


	$_SESSION['descr_clss'][786]['name']='ПЛ - лог операций' ;
	$_SESSION['descr_clss'][786]['parent']=1 ;
	$_SESSION['descr_clss'][786]['fields']['packet_id']=array('type'=>'int(11)','index'=>'packet_id') ;
	$_SESSION['descr_clss'][786]['fields']['pl_id']=array('type'=>'int(11)','index'=>'packet_id') ;
	$_SESSION['descr_clss'][786]['fields']['mo_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][786]['fields']['group_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][786]['fields']['doctor_id']=array('type'=>'int') ;
	$_SESSION['descr_clss'][786]['fields']['personal_id']=array('type'=>'int','index'=>'personal_id') ;
	$_SESSION['descr_clss'][786]['fields']['changes']=array('type'=>'text') ;
	$_SESSION['descr_clss'][786]['fields']['cnt']=array('type'=>'int') ;

}

function _pl_install_modul($DOT_root)
{   // персонал компании
    $pattern=array() ;
    $pattern['table_title']		        = 	'ПЛ - участники обмена' ;
    $pattern['use_clss']		        = 	'780' ;
    $pattern['table_name']				= 	'pl_clients' ;
    $pattern['table_clss']				= 	101 ;
    $pattern['table_parent']			= 	$DOT_root;
    $id_DOT_personal=create_table_by_pattern($pattern) ;

    $pattern=array() ;
    $pattern['table_title']				= 	'ПЛ - участники обмена - доп.инф.' ;
    $pattern['use_clss']				= 	'781' ;
    $pattern['table_name']				= 	'pl_clients_ext_info' ;
    $pattern['table_clss']				= 	101 ;
    $pattern['table_parent']			= 	$id_DOT_personal;
    $id_image=create_table_by_pattern($pattern) ;

    $pattern=array() ;
    $pattern['table_title']		        = 	'ПЛ - лог запросов' ;
    $pattern['use_clss']		        = 	'782' ;
    $pattern['table_name']				= 	'pl_req_log' ;
    $pattern['table_clss']				= 	101 ;
    $pattern['table_parent']			= 	$DOT_root;
    create_table_by_pattern($pattern) ;

    // персонал компании
   $pattern=array() ;
    $pattern['table_title']		        = 	'ПЛ - лог пакетов' ;
    $pattern['use_clss']		        = 	'783' ;
    $pattern['table_name']				= 	'pl_packets' ;
   $pattern['table_clss']				= 	101 ;
   $pattern['table_parent']			= 	$DOT_root;
    $id_DOT_personal=create_table_by_pattern($pattern) ;


    // Сообщения сотрудникам
   $pattern=array() ;
   $pattern['table_title']		        = 	'ПЛ - путевые листы' ;
   $pattern['use_clss']		            = 	'784' ;
   $pattern['table_name']				= 	'pl_put_list' ;
   $pattern['table_clss']				= 	101 ;
   $pattern['table_parent']			= 	$DOT_root;
   create_table_by_pattern($pattern) ;

    // Сообщения сотрудникам
   $pattern=array() ;
   $pattern['table_title']		        = 	'ПЛ - водители титула' ;
   $pattern['use_clss']		            = 	'785' ;
   $pattern['table_name']				= 	'pl_packet_driver' ;
   $pattern['table_clss']				= 	101 ;
   $pattern['table_parent']			= 	$DOT_root;
   create_table_by_pattern($pattern) ;

   $pattern=array() ;
   $pattern['table_title']		        = 	'ПЛ - лог операций' ;
   $pattern['use_clss']		            = 	'786' ;
   $pattern['table_name']				= 	'pl_log' ;
   $pattern['table_clss']				= 	101 ;
   $pattern['table_parent']			= 	$DOT_root;
   create_table_by_pattern($pattern) ;

	//ENGINE()->reg_cron('Обработка входящих XML титул 1','PL/titul1_parsing','*/1 * * * *',array('enabled'=>0));
	ENGINE()->reg_cron('Проверка МО для ПЛ и отправка на подпись','PL/titul_check_mo_send_to_sign','*/1 * * * *',array('enabled'=>0));
	ENGINE()->reg_cron('Проверка подписи XML титул','PL/titul_check_sign','*/1 * * * *',array('enabled'=>0));
	ENGINE()->reg_cron('Проверка срока действия титулов','PL/titul_check_status_7','0 */1 * * *',array('enabled'=>0));

	$id_sect=SETUP_add_section_to_site_setting('Путевой лист') ;
	SETUP_add_rec_to_site_setting($id_sect,"Мед.организация в титул по умолчанию",2,'LS_pl_med_org_def_id');
	SETUP_add_rec_to_site_setting($id_sect,"Разрешить дежурному фельдшеру группы подписывать МО предыщущей смены",0,'LS_pl_allow_active_doctor_sign_all',array('el'=>'checkbox')) ;

	check_exists_dir('/public/pl_titul');


	if (SETUP_LEVEL('db_updates') and ESMO_STATUS!='SETUP_FIRST')
	{
		$recs=execSQL('select * from pl_packets  where titul_date>0 and (titul_date_from=0 or titul_date_to=0) order by pkey desc limit 100') ;
		if (sizeof($recs)) foreach ($recs as $rec)
		{
			$now=getdate($rec['titul_date']) ;
			$titul_date_to=mktime(23,59,59,$now['mon'],$now['mday'],$now['year']);
			$data=['titul_date_from'=>$rec['titul_date'],'titul_date_to'=>$titul_date_to] ;
			update_rec_in_table('pl_packets', $data, 'pkey='.$rec['pkey']);
		}

		$cnt=execSQL_value('select count(pkey) from pl_packet_driver  where mo_doctor_id=0 or mo_doctor_id is null or mo_date is null or mo_date=0') ;
		if ($cnt)
		{
			execSQL_update('update pl_packet_driver t1
											left join obj_site_mo_session t2 on t2.pkey=t1.mo_id
											set t1.mo_doctor_id=t2.doctor_id,t1.mo_group_id=t2.group_id,t1.mo_date=t2.c_data
											where not t2.pkey is null and (t1.mo_doctor_id=0 or t1.mo_doctor_id is null or t1.mo_date is null or t1.mo_date=0)
											',['debug'=>0]);
			execSQL_update('update pl_packet_driver t1
											left join obj_site_mo_session_now t2 on t2.pkey=t1.mo_id
											set t1.mo_doctor_id=t2.doctor_id,t1.mo_group_id=t2.group_id,t1.mo_date=t2.c_data
											where not t2.pkey is null and (t1.mo_doctor_id=0 or t1.mo_doctor_id is null or t1.mo_date is null or t1.mo_date=0)
											',['debug'=>0]);
		}
	}


}

// новая версия, на магните работать не будет
function _pl_install_modul_new()
{

	SETUP()->reg_ext('pl','Путевой лист') ;

	check_exists_dir('/public/pl_titul');

	//ENGINE()->reg_cron('Обработка входящих XML титул 1','PL/titul1_parsing','*/1 * * * *',array('enabled'=>0));
	ENGINE()->reg_cron('Проверка МО для ПЛ','PL/titul_check_mo','*/1 * * * *',array('enabled'=>0));
	ENGINE()->reg_cron('Проверка подписи XML титул','PL/titul_check_sign','*/1 * * * *',array('enabled'=>0));
	ENGINE()->reg_cron('Проверка срока действия титулов','PL/titul_check_status_7','0 */1 * * *',array('enabled'=>0));

	$id_sect=SETUP_add_section_to_site_setting('Путевой лист') ;
	SETUP_add_rec_to_site_setting($id_sect,"Мед.организация в титул по умолчанию",2,'LS_pl_med_org_def_id');

	include_once('i_pl_client.php') ;
	\esmo\pl\i_pl_client::reg_class() ;
	\esmo\pl\i_pl_client::create_table_for_clss() ;

	include_once('i_pl_client_ext_info.php') ;
	\esmo\pl\i_pl_client_ext_info::reg_class() ;
	\esmo\pl\i_pl_client_ext_info::create_table_for_clss() ;

	include_once('i_pl_req_log.php') ;
	\esmo\pl\i_pl_req_log::reg_class() ;
	\esmo\pl\i_pl_req_log::create_table_for_clss() ;

	include_once('i_pl_packets.php') ;
	\esmo\pl\i_pl_packets::reg_class() ;
	\esmo\pl\i_pl_packets::create_table_for_clss() ;

	include_once('i_pl_packet_driver.php') ;
	\esmo\pl\i_pl_packet_driver::reg_class() ;
	\esmo\pl\i_pl_packet_driver::create_table_for_clss() ;

	include_once('i_pl_put_list.php') ;
	\esmo\pl\i_pl_put_list::reg_class() ;
	\esmo\pl\i_pl_put_list::create_table_for_clss() ;

	include_once('i_pl_log.php') ;
	\esmo\pl\i_pl_log::reg_class() ;
	\esmo\pl\i_pl_log::create_table_for_clss() ;

	if (SETUP_LEVEL('db_updates') and ESMO_STATUS!='SETUP_FIRST')
	{
		$recs=execSQL('select * from pl_packets  where titul_date>0 and (titul_date_from=0 or titul_date_to=0) order by pkey desc limit 100') ;
		if (sizeof($recs)) foreach ($recs as $rec)
		{
			$now=getdate($rec['titul_date']) ;
			$titul_date_to=mktime(23,59,59,$now['mon'],$now['mday'],$now['year']);
			$data=['titul_date_from'=>$rec['titul_date'],'titul_date_to'=>$titul_date_to] ;
			update_rec_in_table('pl_packets', $data, 'pkey='.$rec['pkey']);
		}

		$cnt=execSQL_value('select count(pkey) from pl_packet_driver  where mo_doctor_id=0 or mo_doctor_id is null or mo_date is null or mo_date=0') ;
		if ($cnt)
		{
			execSQL_update('update pl_packet_driver t1
											left join obj_site_mo_session t2 on t2.pkey=t1.mo_id
											set t1.mo_doctor_id=t2.doctor_id,t1.mo_group_id=t2.group_id,t1.mo_date=t2.c_data
											where not t2.pkey is null and (t1.mo_doctor_id=0 or t1.mo_doctor_id is null or t1.mo_date is null or t1.mo_date=0)
											',['debug'=>0]);
			execSQL_update('update pl_packet_driver t1
											left join obj_site_mo_session_now t2 on t2.pkey=t1.mo_id
											set t1.mo_doctor_id=t2.doctor_id,t1.mo_group_id=t2.group_id,t1.mo_date=t2.c_data
											where not t2.pkey is null and (t1.mo_doctor_id=0 or t1.mo_doctor_id is null or t1.mo_date is null or t1.mo_date=0)
											',['debug'=>0]);
		}
	}


}




?>
