<?php
//header('Content-Type: text/html; charset=utf-8');
session_start();
header("Content-Type: application/xml; charset=utf-8");

//error_reporting(0);
set_time_limit(900);

define('PATH_TO_SERVISE', '/app/Logist/files/');
define('ORDER_FILE_PATH', $_SERVER['DOCUMENT_ROOT'].PATH_TO_SERVISE);
define('CHECK_TIME_OUT', 900);
define('SESSION_TIME_OUT', 2);

include "../settings.php";
include "logger.php";
include "importAPIFunction.php";

$db = new mysqli ($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);
$db->query("SET CHARSET SET utf8");
$db->query("SET NAMES 'utf8'");
if (mysqli_connect_errno()) {	
	exit('Error connecting to DB');
}




$json = file_get_contents('php://input');

writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' importData <> import file_get_contents <> '.$json, 'import');

/*foreach($_REQUEST as $key => $value) {
	writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' importData <> import key <> '.$_REQUEST[$key], 'import' );
	writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' importData <> import value <> '.$value, 'import' );
}*/

if( isset($_REQUEST['login']) && isset($_REQUEST['password']) ){
	
	$login = $_REQUEST['login'];
	$password = $_REQUEST['password'];
	
	$query = "SELECT u.client_id,c.`is_blocked` FROM `wm_users` u INNER JOIN `wm_clients` c ON u.`client_id` = c.`id` WHERE `login` = '".$login."' AND `password` = '".md5($password)."';";
	writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> importDataLogist <> login '.$query, 'import' );
	if($stmt = $db->prepare($query)){
	if($stmt->execute()){
		$stmt->bind_result($client_id,$blocked);
		$stmt->store_result();
		if($stmt->num_rows()>0){
			$stmt->fetch();
			
			if($blocked==1){
				//echo '<RESULT><ERROR_MESSAGE>Пользователь с таким логином заблокирован</ERROR_MESSAGE><MESSAGE></MESSAGE></RESULT>';
				//result_message_xml_short(-1,'Пользователь с таким логином заблокирован','');
				result_message_xml(-1,'Пользователь с таким логином заблокирован',0,0,0);
				exit;
			}
			
		}else{
			//echo '<RESULT><ERROR_MESSAGE>Авторизация не прошла, проверьте логин и пароль</ERROR_MESSAGE><MESSAGE></MESSAGE></RESULT>';
			//result_message_xml_short(-1,'Авторизация не прошла, проверьте логин и пароль','');
			result_message_xml(-1,'Авторизация не прошла, проверьте логин и пароль',0,0,0);
			exit;
		}
		$stmt->close();
			
		$idSession = session_id();
		$query = "INSERT INTO tms_soap_session_id (`session_id`,`instance_id`) VALUES ('".$idSession."',".$client_id.")";
		writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> importDataLogist <> tms_soap_session_id '.$query, 'import' );
		$db->query($query);
		//echo '<RESULT><ERROR_MESSAGE></ERROR_MESSAGE><MESSAGE>'.$idSession.'</MESSAGE></RESULT>';
		//result_message_xml_short(0,'',$idSession);
		result_message_xml(0,0,0,0,0,$idSession);
		exit;
		
	}else{
		//echo '<RESULT><ERROR_MESSAGE>Ошибка выполнения</ERROR_MESSAGE><MESSAGE></MESSAGE></RESULT>';
		//result_message_xml_short(-1,'Ошибка выполнения','');
		result_message_xml(-1,'Ошибка выполнения',0,0,0);
		exit;
		}
	}else{
		//echo '<RESULT><ERROR_MESSAGE>Ошибка выполнения</ERROR_MESSAGE><MESSAGE></MESSAGE></RESULT>';
		//result_message_xml_short(-1,'Ошибка выполнения','');
		result_message_xml(-1,'Ошибка выполнения',0,0,0);
		exit;
		}
	
}elseif( isset($_REQUEST['key']) ){
	
	try{
	
	if( isValidMd5($_REQUEST['key']) ){
	
	// проверка ключа сесии
	$check_key_result = check_key($_REQUEST['key']);
	if(!$check_key_result && $check_key_result == ""){
	
	// выборка instance_id клиента по ключу сесии
	$instance_id = get_instanceid($_REQUEST['key']);
	if($instance_id){
	
	
	
	
	// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	// ------------------------------------------------------------------------------------
	// проверка статуса заявок inque_orders - INQUE_ORDERS, ERROR_ORDERS, UNATTACHED_ORDERS
	if(isset($_REQUEST['inque_orders'])){
		
		$INQUE_ORDERS=array(); $ERROR_ORDERS=array(); $UNATTACHED_ORDERS=array(); $INTRIP_ORDERS=array();
		$ERROR_ORDERS_ID=array();
		$ERROR_ORDERS_MESSAGE=array();
		$ATTRIBUTE_ORDERS=array();
		
	$query = "SELECT o.`Code`,o.`CustomString6` FROM `tms_order` o
INNER JOIN `tms_project` p ON p.`Project_ID`=o.`Project_ID`
LEFT JOIN `tms_zone` z ON z.`Zone_ID` = o.`Zone_ID`
WHERE (p.`Client_ID` = ".$instance_id." OR p.`Client_ID` = -".$instance_id.") AND DATE(p.`Date`) >= DATE(NOW()) AND o.`IsInTrip` <> 1
AND o.`Code` NOT IN (SELECT `Code` FROM `tms_import_order_status` WHERE `State` = 2 AND DATE(`time`) >= DATE(NOW()));";
	writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> inque_orders=1 <> INQUE_ORDERS '.$query, 'import' );
	if($stmt = $db->prepare($query)){
	if($stmt->execute()){
		$stmt->bind_result($code_inque,$order_type);
		while($stmt->fetch()){
			array_push($INQUE_ORDERS,$code_inque);
			//array_push($ATTRIBUTE_ORDERS,$order_type);
			$ATTRIBUTE_ORDERS[] = array('ORDER_TYPE'=>$order_type);
		}
		$stmt->close();
	}
	}
	
	$query = "SELECT `Code`,ID,`Message`,`order_type` FROM tms_import_order_status o
INNER JOIN `tms_project` p ON p.`Project_ID`=o.`Project_ID`
WHERE (o.`instanceID` = ".$instance_id." OR o.`instanceID` = -".$instance_id.") AND o.`State` = 1 AND o.`Sent` = 0 AND DATE(p.`Date`) >= DATE(NOW())";
	writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> inque_orders=1 <> ERROR_ORDERS '.$query, 'import' );
	if($stmt = $db->prepare($query)){
	if($stmt->execute()){
		$stmt->bind_result($code_error,$id_error,$message,$order_type);
		while($stmt->fetch()){
			array_push($ERROR_ORDERS,$code_error);
			array_push($ERROR_ORDERS_ID,$id_error);
			array_push($ERROR_ORDERS_MESSAGE,$message);
			//array_push($ATTRIBUTE_ORDERS,$order_type);
			$ATTRIBUTE_ORDERS[] = array('ORDER_TYPE'=>$order_type);
		}
		$stmt->close();
		
		$query = "UPDATE tms_import_order_status SET `Sent` = 1 WHERE ID IN (".implode(",", $ERROR_ORDERS_ID).");";
		if($stmt = $db->prepare($query)){
		if($stmt->execute()){
			$stmt->close();
		}
		}
	}
	}
	
	$query = "SELECT i.`OrderCode`,o.`CustomString6`
FROM `tms_impexp_ordersource` i
INNER JOIN `tms_project` p ON p.`Project_ID`=i.`Project_ID`
LEFT JOIN `tms_order` o ON i.`LinkedOrderID` = o.`Order_ID`
WHERE (p.`Client_ID` = ".$instance_id." OR p.`Client_ID` = -".$instance_id.") AND DATE(p.`Date`) >= DATE(NOW()) AND o.`Order_ID` IS NULL
AND o.`Code` NOT IN (SELECT `Code` FROM `tms_import_order_status` WHERE `State` = 2 AND DATE(`time`) >= DATE(NOW()));";
	writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> inque_orders=1 <> UNATTACHED_ORDERS '.$query, 'import' );
	if($stmt = $db->prepare($query)){
	if($stmt->execute()){
		$stmt->bind_result($code_unattached,$order_type);
		while($stmt->fetch()){
			array_push($UNATTACHED_ORDERS,$code_unattached);
			//array_push($ATTRIBUTE_ORDERS,$order_type);
			$ATTRIBUTE_ORDERS[] = array('ORDER_TYPE'=>$order_type);
		}
		$stmt->close();
	}
	}
	
	$query = "SELECT o.`Code`,o.`CustomString6` FROM `tms_order` o
INNER JOIN `tms_project` p ON p.`Project_ID`=o.`Project_ID`
LEFT JOIN `tms_zone` z ON z.`Zone_ID` = o.`Zone_ID`
WHERE (p.`Client_ID` = ".$instance_id." OR p.`Client_ID` = -".$instance_id.") AND DATE(p.`Date`) >= DATE(NOW()) AND o.`IsInTrip` = 1
AND o.`Code` NOT IN (SELECT `Code` FROM `tms_import_order_status` WHERE `State` = 2 AND DATE(`time`) >= DATE(NOW()));";
	writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> intrip_orders=1 <> INTRIP_ORDERS '.$query, 'import' );
	if($stmt = $db->prepare($query)){
	if($stmt->execute()){
		$stmt->bind_result($code_intrip,$order_type);
		while($stmt->fetch()){
			array_push($INTRIP_ORDERS,$code_intrip);
			//array_push($ATTRIBUTE_ORDERS,$order_type);
			$ATTRIBUTE_ORDERS[] = array('ORDER_TYPE'=>$order_type);
		}
		$stmt->close();
	}
	}
	
	
	result_message_xml(0,'',0,0,0,"",$INQUE_ORDERS,$ERROR_ORDERS,$UNATTACHED_ORDERS,$INTRIP_ORDERS,$ERROR_ORDERS_MESSAGE,$ATTRIBUTE_ORDERS);
	$db->close();
	exit;
	

	// ------------------------------------------------------------------------------------
	// скрыть заявки ранне выгружены в с иситему
	}elseif(isset($_REQUEST['hide_orders'])){
	
		$xml_post = $json;
	
		if($xml_post || $xml_post!=""){
			
			$xml_post = str_replace('&#65279;', '', $xml_post);
			$xml_post = str_replace('&amp;#045;', '-', $xml_post);
			$xml_post =  str_replace ( '@@@' , '&amp;' ,  $xml_post);
				
			// проверка хмл
			checkXML($xml_post);
			//$xml = simplexml_load_string($xml_post);
			
				$xml = new SimpleXMLElement($xml_post);
				$values = "";
				//foreach ($xml->HIDE_ORDERS as $value) {
				foreach ($xml as $value) {
					//$values .= "(0,'".$value->ORDER_CODE."',2,".$instance_id.",1),";
					$values .= "(0,'".$value."',2,".$instance_id.",1),";
				}
					$values=substr($values, 0, strlen($values)-1);
					$query = "INSERT INTO tms_import_order_status (`Project_ID`,`Code`,`State`,`instanceID`,`Sent`) VALUES ".$values.";";
					writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> hide_orders=1 <> '.$query, 'import' );
					if($stmt = $db->prepare($query)){
					if($stmt->execute()){
						$stmt->close();
					}
					}
					
				result_message_xml(0,'',0,0,0);
				exit;
			
		}else{
			result_message_xml(-1,'Ошибка, XML не может быть пустым',0,0,0);
			exit;
		}
	
	
	
	/*
	// ------------------------------------------------------------------------------------
	// проверка статуса заявок inque_orders - INQUE_ORDERS, ERROR_ORDERS, UNATTACHED_ORDERS
	}elseif(isset($_REQUEST['intrip_orders'])){
		
		$INQUE_ORDERS=array(); $ERROR_ORDERS=array(); $UNATTACHED_ORDERS=array(); $INTRIP_ORDERS=array();
		
		$query = "SELECT o.`Code` FROM `tms_order` o
INNER JOIN `tms_project` p ON p.`Project_ID`=o.`Project_ID`
LEFT JOIN `tms_zone` z ON z.`Zone_ID` = o.`Zone_ID`
WHERE (p.`Client_ID` = ".$instance_id." OR p.`Client_ID` = -".$instance_id.") AND DATE(p.`Date`) >= DATE(NOW()) AND o.`IsInTrip` = 1;";
	writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> intrip_orders=1 <> INQUE_ORDERS '.$query, 'import' );
	if($stmt = $db->prepare($query)){
	if($stmt->execute()){
		$stmt->bind_result($code_intrip);
		while($stmt->fetch()){
			array_push($INTRIP_ORDERS,$code_intrip);
		}
		$stmt->close();
	}
	}
	
	result_message_xml(0,'',0,0,0,"",$INQUE_ORDERS,$ERROR_ORDERS,$UNATTACHED_ORDERS,$INTRIP_ORDERS);
	$db->close();
	exit;
	*/
	// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	
	
	
	// ------------------------------------------------------------------------------------
	// импорт заявок
	}else{
	
	
	
	
	/*function file_get_contents_utf8($fn) {
     $content = file_get_contents($fn);
      return mb_convert_encoding($content, 'UTF-8',
          mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
	}*/
	
	//header('Content-Encoding: gzip');
	//$opts = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
	//$context = stream_context_create($opts);
	//$data = file_get_contents('php://input', false, $context);
	
	//$data = file_get_contents('php://input');
	//$data = mb_convert_encoding($data, 'HTML-ENTITIES', "UTF-8");
	//$data = str_replace('&#65279;', '', $data);
	//writeToLogFile( ' -> '.date('Y-m-d H:i:s').' importDataLogist <> file_get_contents <> '.$data );
	//$xml_post = trim($data);
	
	
	//if(isset($_POST['xml'])){
	
	$key = $_REQUEST['key'];
	//$xml_post = $_REQUEST['xml'];
	$xml_post = $_POST['xml'];
	$xml_post = trim(stripcslashes($xml_post));
	
	
	
	if(!isset( $_REQUEST['project'] ) || $_REQUEST['project']==""){
		$project_id = false;
	}else{
		$project_id = $_REQUEST['project'];
		$project_id = trim(rtrim($project_id));
	}
	
	if(!isset( $_REQUEST['time'] ) || $_REQUEST['time']==""){
		$start_time = '09:00';
	}else{ $start_time = $_REQUEST['time']; }
	
	
	if(!isset( $_REQUEST['date_project'] ) || $_REQUEST['date_project']==""){
		$date_project = date('Y-m-d');
	}else{
		//if( (bool)strtotime($_REQUEST['date_project']) ){
			$date_project = $_REQUEST['date_project'];
		//}else{
		//	echo '<RESULT>Проверьте дату проекта</RESULT>'; exit;
		//}
	}
	
	if( !isset( $_REQUEST['name_project'] ) || $_REQUEST['name_project']=="" ){
		$name_project = null;
	}else{
		//$name_project = addslashes(trim(rtrim($_REQUEST['name_project'])));
		$name_project = $_REQUEST['name_project'];
	}
	
	
	if(!isset($xml_post) || $xml_post==""){
		$xml_post = $json;
		$xml_post = str_replace('&#65279;', '', $xml_post);
		$xml_post = str_replace('&amp;#045;', '-', $xml_post);
	}
	
	if($xml_post || $xml_post!=""){
	
	checkXML($xml_post);
	
	/*
	$query = "SELECT instance_id FROM `tms_soap_session_id` WHERE TIME>=SUBTIME(NOW(), '0".SESSION_TIME_OUT.":00:00') AND `session_id`='".$key."' ORDER BY `time` DESC LIMIT 1";
	writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> importDataLogist <> key tms_soap_session_id '.$query, 'import' );
	if($stmt = $db->prepare($query)){
	if($stmt->execute()){
		$stmt->bind_result($instance_id);
		$stmt->store_result();
		if($stmt->num_rows()>0){
			$stmt->fetch();
	*/		
			$xml = import($instance_id,$xml_post,$project_id,$start_time,$date_project,$name_project);
			writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> import <> import '.$xml, 'import' );
			if($xml){
				//echo '<RESULT>'.$xml.'</RESULT>';
				//result_message_xml_short(0,'',$xml);
				
				
			// ------------------------------------------------------------------------------------
			// проверка статуса заявок после импорта - INQUE_ORDERS, ERROR_ORDERS, UNATTACHED_ORDERS
		
			$ProjectID = $_SESSION['ProjectID'];
			
			$ERROR_ORDERS_ID=array();
			$DEL_ORDERS_ID=array();
			$count_error = 0;
			$count_deleted = 0;
			$message_error = "";
			
			$count_insert = 0;
			$code_error = 0;
			
		
			$query = "SELECT COUNT(i.`OrderCode`) FROM `tms_impexp_ordersource` i WHERE i.`Project_ID` = ".$ProjectID." AND i.`OrderCode` NOT IN (SELECT `Code` FROM `tms_import_order_status` WHERE `State` = 2 AND DATE(`time`) >= DATE(NOW()));";
			writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> inque_orders=1 <> INQUE_ORDERS '.$query, 'import' );
			if($stmt = $db->prepare($query)){
			if($stmt->execute()){
				$stmt->bind_result($count_insert);
				while($stmt->fetch()){

				}
				$stmt->close();
			}
			}
	/*
			$query = "SELECT `Code`,ID,`Message`,`order_type` FROM tms_import_order_status o WHERE (o.`instanceID` = ".$instance_id." OR o.`instanceID` = -".$instance_id.") AND o.`State` = 1 AND o.`Sent` = 0 AND Project_ID = ".$ProjectID.";";
			writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> inque_orders=1 <> ERROR_ORDERS '.$query, 'import' );
			if($stmt = $db->prepare($query)){
			if($stmt->execute()){
				$stmt->bind_result($code_error,$id_error,$message,$order_type);
				while($stmt->fetch()){
					$count_error++;
					array_push($ERROR_ORDERS_ID,$id_error);
					$message_error .= $code_error.", ".$order_type.", ".$message.";\n";
				}
				$stmt->close();
				
				$query = "UPDATE tms_import_order_status SET `Sent` = 1 WHERE ID IN (".implode(",", $ERROR_ORDERS_ID).");";
				if($stmt = $db->prepare($query)){
				if($stmt->execute()){
					$stmt->close();
				}
				}
			}
			}
	
			$query = "SELECT `Code`,ID,`Message`,`order_type` FROM tms_import_order_status o WHERE (o.`instanceID` = ".$instance_id." OR o.`instanceID` = -".$instance_id.") AND o.`State` = 3 AND o.`Sent` = 0 AND Project_ID = ".$ProjectID.";";
			writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> inque_orders=1 <> ERROR_ORDERS '.$query, 'import' );
			if($stmt = $db->prepare($query)){
			if($stmt->execute()){
				$stmt->bind_result($code_error,$id_del,$message,$order_type);
				while($stmt->fetch()){
					$count_deleted++;
					array_push($DEL_ORDERS_ID,$id_del);
					//$message_error .= $code_error.", ".$order_type.", ".$message.";\n";
				}
				$stmt->close();
				
				$query = "UPDATE tms_import_order_status SET `Sent` = 1 WHERE ID IN (".implode(",", $DEL_ORDERS_ID).");";
				if($stmt = $db->prepare($query)){
				if($stmt->execute()){
					$stmt->close();
				}
				}
			}
			}
	*/		
		
		
			/*		
					$INQUE_ORDERS=array(); $ERROR_ORDERS=array(); $UNATTACHED_ORDERS=array(); $INTRIP_ORDERS=array();
					$ERROR_ORDERS_ID=array();
					$ERROR_ORDERS_MESSAGE=array();
					$ATTRIBUTE_ORDERS=array();
							
				$query = "SELECT i.`OrderCode`,i.`CustomString6` FROM `tms_impexp_ordersource` i WHERE i.`Project_ID` = ".$ProjectID." AND i.`OrderCode` NOT IN (SELECT `Code` FROM `tms_import_order_status` WHERE `State` = 2 AND DATE(`time`) >= DATE(NOW()));";
				writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> inque_orders=1 <> INQUE_ORDERS '.$query, 'import' );
				if($stmt = $db->prepare($query)){
				if($stmt->execute()){
					$stmt->bind_result($code_inque,$order_type);
					while($stmt->fetch()){
						array_push($INQUE_ORDERS,$code_inque);
						//array_push($ATTRIBUTE_ORDERS,$order_type);
						$ATTRIBUTE_ORDERS[] = array('ORDER_TYPE'=>$order_type);
					}
					$stmt->close();
				}
				}

				$query = "SELECT `Code`,ID,`Message`,`order_type` FROM tms_import_order_status o WHERE (o.`instanceID` = ".$instance_id." OR o.`instanceID` = -".$instance_id.") AND o.`State` = 1 AND o.`Sent` = 0 AND Project_ID = ".$ProjectID.";";
				writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> inque_orders=1 <> ERROR_ORDERS '.$query, 'import' );
				if($stmt = $db->prepare($query)){
				if($stmt->execute()){
					$stmt->bind_result($code_error,$id_error,$message,$order_type);
					while($stmt->fetch()){
						array_push($ERROR_ORDERS,$code_error);
						array_push($ERROR_ORDERS_ID,$id_error);
						array_push($ERROR_ORDERS_MESSAGE,$message);
						//array_push($ATTRIBUTE_ORDERS,$order_type);
						$ATTRIBUTE_ORDERS[] = array('ORDER_TYPE'=>$order_type);
					}
					$stmt->close();
					
					$query = "UPDATE tms_import_order_status SET `Sent` = 1 WHERE ID IN (".implode(",", $ERROR_ORDERS_ID).");";
					if($stmt = $db->prepare($query)){
					if($stmt->execute()){
						$stmt->close();
					}
					}
				}
				}
			*/		
			
			
			/*
			$xmlTask = '<Data InstanceID="'.$instance_id.'" >
				<p type="WorkWithOrders2">
				<p type="WorkWithOrders2"  ProjectID="'.$ProjectID.'"/>
				</p>
			</Data>';
			
			$query = "INSERT INTO `tms_web_tasks` (`task`,`user_id`,`in_progress`) VALUES ('".$xmlTask."',-".$instance_id.",0)";
			writeToLogFile(' ---> '.date('Y-m-d H:i:s').' importDataLogist <> Insert web_task orders '.$query, 'import');
			if ($stmt = $db->prepare($query)) {
			if ($stmt->execute()) {
				//$last_id = $db->insert_id;
				$stmt->close();
			}
			}
			*/
				
				
				result_message_xml($count_error,$message_error,$count_insert,0,$count_deleted,$xml);
				
				//result_message_xml(0,'',0,0,0,"",$INQUE_ORDERS,$ERROR_ORDERS,$UNATTACHED_ORDERS,$INTRIP_ORDERS,$ERROR_ORDERS_MESSAGE,$ATTRIBUTE_ORDERS);
				$db->close();
				
				//result_message_xml(0,'',0,0,0);
				
				writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> importDataLogist <> <RESULT>'.$xml.'</RESULT>', 'import' );
				exit;
			}else{
				//echo '<RESULT>Ошибка импорта</RESULT>';
				//result_message_xml_short(-1,'Ошибка импорта','');
				result_message_xml(-1,'Ошибка импорта, не удалсь выполнить задачу импорта заявок, превышено время ожидания ответа от сервера',0,0,0);
				writeToLogFile( ' ---> '.date('Y-m-d H:i:s').' <> importDataLogist <> <RESULT>Ошибка импорта, не удалсь выполнить задачу импорта заявок</RESULT>', 'import' );
				
				///--------- reserve ---
				/*
				$xmlTask = '<Data InstanceID="'.$instance_id.'" >
					<p type="WorkWithOrders2">
					<p type="WorkWithOrders2"  ProjectID="'.$ProjectID.'"/>
					</p>
				</Data>';
				
				$query = "INSERT INTO `tms_web_tasks` (`task`,`user_id`,`in_progress`) VALUES ('".$xmlTask."',-".$instance_id.",-2)";
				writeToLogFile(' ---> '.date('Y-m-d H:i:s').' importDataLogist <> Insert web_task orders '.$query, 'import');
				if ($stmt = $db->prepare($query)) {
				if ($stmt->execute()) {
					$stmt->close();
				}
				}
				*/
				///--------- reserve ---
				
				exit;
			}
	
	
	
	}else{
		//echo '<RESULT>Ошибка, XML не может быть пустым</RESULT>';
		//result_message_xml_short(-1,'Ошибка, XML не может быть пустым','');
		result_message_xml(-1,'Ошибка, XML не может быть пустым',0,0,0);
		exit;
	}
	
	}
	
	}else{
		result_message_xml(-1,'Доступ закрыт. Авторизируйтесь',0,0,0);
		exit;
	}
	
	}else{
		echo $check_key_result;	exit;
	}
	
	
	}else{
		result_message_xml(-1,'EMPTY KEY',0,0,0);
		exit;
	}
	
	}catch(Exception $e){
		result_message_xml(-1,$e->getMessage(),0,0,0);
	}
	
}else{
	result_message_xml(-1,'Пустой запрос',0,0,0);
	exit;
}
