<?php
function export($dateFrom, $dateTo, $client_id, $gmt, $imei, $order_code, $locale) {
	
	global $db;
	global $db2;
	global $db3;
	global $db4;
	
	$where1 = "";
	if($imei){
		$where1 = " AND td.Login = '".$imei."' ";
	}
	$where2 = "";
	if($order_code){
		$where2 = " AND c.`Code` = '".$order_code."' ";
	}
	
	$query = "SELECT 
  f.ID,
  f.Closed,
  f.`Name`,
  f.`Distance`,
  f.`Time`,
  f.`StartTime`,
  f.`EndTime`,
  f.`Comments`,
  p.`Name`,
  wc.`car_name`,
  wc.imei,
  
	wc.`fuel`,
	wc.`model`,
	tfu.`Name`,
	wc.`atp`,
	wc.`company`,
	wc.`fuel_consumption`,
	wc.`tel_number`,
	wc.`kmh_kost`,
  
	CONCAT_WS(' ', d.`surname`, d.`name`),
	d.`tel_number`,
	d.`description`,
	d.`email`,
	d.`code`,
  
  ep.`Name`,
  s.`Start`,
  s.`End`
FROM
  `tms_facttrip` f 
  LEFT JOIN `tms_project` p 
    ON p.`Project_ID` = f.`Project_Id` 
  LEFT JOIN `tms_car` tc 
    ON f.`Car_ID` = tc.`Car_ID` 
  LEFT JOIN `tms_device` td 
    ON tc.`Device_id` = td.`ID` 
  LEFT JOIN `wm_car_info` wc 
    ON wc.`imei` = td.`Login` 
  LEFT JOIN `wm_drivers` d 
    ON d.`id` = wc.`driver_id`
   LEFT JOIN `tms_trip` t
   ON t.Trip_ID=f.`TripId`
   LEFT JOIN `tms_forwarder` fd ON fd.`Forwarder_ID` = t.`Forwarder_ID`
	LEFT JOIN `tms_employee` ep ON ep.`Employee_ID` = fd.`Employee_ID`
LEFT JOIN `tms_checkpoint` c ON c.`FactTripID` = f.`ID`	
LEFT JOIN tms_speedometer_value s ON s.`DeviceId` = td.ID AND DATE(s.`Date`) = DATE(p.`Date`)
-- LEFT JOIN tms_speedometer_value sv ON td.`ID` = sv.`DeviceId` AND DATE(sv.`Date`) = DATE(c.`EstimateTime`)
LEFT JOIN `tms_fuel` tfu ON tfu.`Fuel_ID` = wc.`fuel_type`
WHERE p.`Client_ID` = ".$client_id." AND (p.`Date` >= '".$dateFrom."' AND p.`Date` <= '".$dateTo."') ".$where1." ".$where2." GROUP BY f.ID ASC;";
	writeToLogFile($query);
	if ($stmt = $db->prepare($query)) {
		$stmt->execute();
		$stmt->bind_result($ID,$Closed,$Name,$Distance,$Time,$StartTime,$EndTime,$Comments,$projectName,$car_name,$imei,$car_fuel,$car_model,$car_fuel_type,$car_atp,$car_company,$car_fuel_consumption,$car_tel_number,$car_kmh_kost,$driver,$driver_tel_number,$driver_description,$driver_email,$driver_code,$forwarder,$odomStart,$odomEnd);
		$stmt->store_result();
		if($stmt->num_rows()>0){
			
		$dom = new domDocument("1.0", "UTF-8");
		$dom->preserveWhiteSpace = false;
		$dom->formatOutput = true;

		$root = $dom->createElement("TRIPS");
		$dom->appendChild($root);
			
		while ($stmt->fetch()) {
					
		$TRIP = $dom->createElement("TRIP");
		$root->appendChild($TRIP);
		
		$TRIP_CODE = $dom->createElement("TRIP_CODE", $ID);
		$TRIP->appendChild($TRIP_CODE);
		$PROJECT_CODE = $dom->createElement("PROJECT_CODE", $project);
		$TRIP->appendChild($PROJECT_CODE);
		
		$CAR = $dom->createElement("CAR", $car_name);
		$TRIP->appendChild($CAR);
		$CODE_CAR = $dom->createElement("CODE_CAR", $imei);
		$TRIP->appendChild($CODE_CAR);
		$FUEL_CAR = $dom->createElement("FUEL_CAR", $car_fuel);
		$TRIP->appendChild($FUEL_CAR);
		$MODEL_CAR = $dom->createElement("MODEL_CAR", $car_model);
		$TRIP->appendChild($MODEL_CAR);
		
		if (!is_numeric($car_fuel_type)) {
			$obj = json_decode($car_fuel_type);
			if ($obj) {
				if (property_exists($obj, $locale)) {
					$car_fuel_type = $obj->$locale;
				}
			}
		}
		
		$FUEL_TYPE_CAR = $dom->createElement("FUEL_TYPE_CAR", $car_fuel_type);
		$TRIP->appendChild($FUEL_TYPE_CAR);
		$ATP_CAR = $dom->createElement("ATP_CAR", $car_atp);
		$TRIP->appendChild($ATP_CAR);
		$COMPANY_CAR = $dom->createElement("COMPANY_CAR", $car_company);
		$TRIP->appendChild($COMPANY_CAR);
		$CONSUMPTION_CAR = $dom->createElement("CONSUMPTION_CAR", $car_fuel_consumption);
		$TRIP->appendChild($CONSUMPTION_CAR);
		$PHONE_CAR = $dom->createElement("PHONE_CAR", $car_tel_number);
		$TRIP->appendChild($PHONE_CAR);
		$COST_KMH_CAR = $dom->createElement("COST_KMH_CAR", $car_kmh_kost);
		$TRIP->appendChild($COST_KMH_CAR);
		
		$DRIVER = $dom->createElement("DRIVER", $driver);
		$TRIP->appendChild($DRIVER);
		$PHONE_DRIVER = $dom->createElement("PHONE_DRIVER", $driver_tel_number);
		$TRIP->appendChild($PHONE_DRIVER);
		$EMAIL_DRIVER = $dom->createElement("EMAIL_DRIVER", $driver_email);
		$TRIP->appendChild($EMAIL_DRIVER);
		$CODE_DRIVER = $dom->createElement("CODE_DRIVER", $driver_code);
		$TRIP->appendChild($CODE_DRIVER);
		$INFO_DRIVER = $dom->createElement("INFO_DRIVER", $driver_description);
		$TRIP->appendChild($INFO_DRIVER);
		
		//$EXPEDITOR = $dom->createElement("EXPEDITOR", 0);
		//$TRIP->appendChild($EXPEDITOR);
		
		$FORWARDER = $dom->createElement("FORWARDER", $forwarder);
		$TRIP->appendChild($FORWARDER);
		
		$TRIP_NAME = $dom->createElement("TRIP_NAME", $Name);
		$TRIP->appendChild($TRIP_NAME);
		
		$TRIP_COMMENTS = $dom->createElement("COMMENTS", $Comments);
		$TRIP->appendChild($TRIP_COMMENTS);
		
		$ODOM_START = $dom->createElement("ODOM_START", $odomStart);
		$TRIP->appendChild($ODOM_START);
		
		$ODOM_END = $dom->createElement("ODOM_END", $odomEnd);
		$TRIP->appendChild($ODOM_END);
		
		$TRIP_DIST_PLAN = $dom->createElement("TRIP_DIST_PLAN", round(($Distance/1000),2));
		$TRIP->appendChild($TRIP_DIST_PLAN);
		$TRIP_DURATION_PLAN = $dom->createElement("TRIP_DURATION_PLAN", round($Time,2));
		$TRIP->appendChild($TRIP_DURATION_PLAN);
		$START_TIME_PLAN = $dom->createElement("START_TIME_PLAN", $StartTime);
		$TRIP->appendChild($START_TIME_PLAN);
		$FINISH_TIME_PLAN = $dom->createElement("FINISH_TIME_PLAN", $EndTime);
		$TRIP->appendChild($FINISH_TIME_PLAN);
		
		
						$query4 = "SELECT SUM(cc.`CargoValueWeight`),SUM(cc.`CargoValueCount`),SUM(cc.`Cost`) FROM `tms_checkpointcargo` cc 
						LEFT JOIN `tms_checkpoint` c ON cc.`CheckPoint_ID` = c.`Id`
						LEFT JOIN `tms_facttrip` ft ON ft.`ID` = c.`FactTripID`
						WHERE ft.`ID` = ".$ID.";";
						if ($stmt4 = $db4->prepare($query4)) 
						{
							$stmt4->execute();
							$stmt4->bind_result($ValueWeight,$ValueCount,$ValueCost);
							$stmt4->fetch();
							
							$TRIP_ORDERS_WEIGHT_PLAN = $dom->createElement("TRIP_ORDERS_WEIGHT_PLAN", $ValueWeight);
							$TRIP->appendChild($TRIP_ORDERS_WEIGHT_PLAN);
							$TRIP_ORDERS_COST_PLAN = $dom->createElement("TRIP_ORDERS_COST_PLAN", $ValueCost);
							$TRIP->appendChild($TRIP_ORDERS_COST_PLAN);
							$TRIP_ORDERS_COUNT_PLAN = $dom->createElement("TRIP_ORDERS_COUNT_PLAN", $ValueCount);
							$TRIP->appendChild($TRIP_ORDERS_COUNT_PLAN);

							$stmt4->close();
						}
						
						$query4 = "SELECT SUM(cc.`CargoValueWeight`),SUM(cc.`CargoValueCount`),SUM(cc.`Cost`) FROM `tms_checkpointcargo` cc 
						LEFT JOIN `tms_checkpoint` c ON cc.`CheckPoint_ID` = c.`Id`
						LEFT JOIN `tms_facttrip` ft ON ft.`ID` = c.`FactTripID`
						WHERE cc.`State` = 2 AND ft.`ID` = ".$ID.";";
						if ($stmt4 = $db4->prepare($query4)) 
						{
							$stmt4->execute();
							$stmt4->bind_result($ValueWeight,$ValueCount,$ValueCost);
							$stmt4->fetch();
							
							$TRIP_ORDERS_WEIGHT_FACT = $dom->createElement("TRIP_ORDERS_WEIGHT_FACT", $ValueWeight);
							$TRIP->appendChild($TRIP_ORDERS_WEIGHT_FACT);
							$TRIP_ORDERS_COST_FACT = $dom->createElement("TRIP_ORDERS_COST_FACT", $ValueCost);
							$TRIP->appendChild($TRIP_ORDERS_COST_FACT);
							$TRIP_ORDERS_COUNT_FACT = $dom->createElement("TRIP_ORDERS_COUNT_FACT", $ValueCount);
							$TRIP->appendChild($TRIP_ORDERS_COUNT_FACT);

							$stmt4->close();
						}
		
		
		
		
		
		
				//$query2 = "SELECT c.`Id`,c.`Name`,c.`GeoX`,c.`GeoY`,c.`PointState`,c.`OrderInTrip`,c.`Distance`,c.`Time`,c.`EstimateTime`,c.`DurationInPoint`,c.`Code`,c.`ClientName`,c.`PointState`,`IsOrderPoint`,`OrderInTrip`,c.`DriverComments`,(SELECT MAX(`OrderInTrip`) FROM `tms_checkpoint` WHERE `FactTripID` = ".$ID."),(SELECT MIN(`OrderInTrip`) FROM `tms_checkpoint` WHERE `FactTripID` = ".$ID.") FROM `tms_checkpoint` c WHERE c.`FactTripID` = ".$ID." ORDER BY `OrderInTrip` ASC;";
				$query2 = "SELECT c.`Id`,c.`Name`,c.`GeoX`,c.`GeoY`,c.`PointState`,c.`OrderInTrip`,c.`Distance`,c.`Time`,c.`EstimateTime`,c.`DurationInPoint`,c.`Code`,c.`ClientName`,c.`PointState`,`IsOrderPoint`,`OrderInTrip`,c.`DriverComments`,(SELECT MAX(`OrderInTrip`) FROM `tms_checkpoint` WHERE `FactTripID` = ".$ID."),(SELECT MIN(`OrderInTrip`) FROM `tms_checkpoint` WHERE `FactTripID` = ".$ID."),wh.`Code`
				FROM `tms_checkpoint` c
INNER JOIN `tms_facttrip` ft ON ft.`ID`=c.`FactTripID`
LEFT JOIN tms_order o ON (o.`Code`= c.`Code` AND o.`Project_ID` = ft.`Project_Id`)
LEFT JOIN tms_warehouse wh ON (o.`Warehouse_ID` = wh.`Warehouse_ID` AND wh.DeletedRecord = 0)
WHERE c.`FactTripID` = ".$ID." ".$where2." ORDER BY `OrderInTrip` ASC;";
				writeToLogFile($query2);
				if ($stmt2 = $db2->prepare($query2)) {
				if ($stmt2->execute()) {	
				$stmt2->bind_result($Id,$cName,$cGeoX,$cGeoY,$cPointState,$cOrderInTrip,$cDistance,$cTime,$cEstimateTime,$cDurationInPoint,$cCode,$cClientName,$cPointState,$IsOrderPoint,$OrderInTrip,$DriverComments,$maxOrderInTrip,$minOrderInTrip,$whCode);
				$stmt2->store_result();
				if($stmt2->num_rows()>0){
					
					$ORDERS = $dom->createElement("ORDERS");
					$TRIP->appendChild($ORDERS);
					
					$firstWarehouse = '';
					$firstPointState = '';
					$i=0;
					while($stmt2->fetch())
					{
						$ORDER = $dom->createElement("ORDER");
						$ORDERS->appendChild($ORDER);
						
						$ID_POINT = $dom->createElement("ID_POINT",$Id);
						$ORDER->appendChild($ID_POINT);
						
						if(!$cCode || $cCode==""){ $cCode = "-"; }
						$CODE = $dom->createElement("CODE",$cCode);
						$ORDER->appendChild($CODE);
						
						$SHOPCODE = $dom->createElement("SHOPCODE",$whCode);
						$ORDER->appendChild($SHOPCODE);
						
						$CLIENTCODE = $dom->createElement("CLIENTCODE",0);
						$ORDER->appendChild($CLIENTCODE);
						
						
						$IN_TRIP_NUMBER = $dom->createElement("IN_TRIP_NUMBER",$cOrderInTrip);
						$ORDER->appendChild($IN_TRIP_NUMBER);
						
						$PLAN_DIST = $dom->createElement("PLAN_DIST",round(($cDistance/1000),2));
						$ORDER->appendChild($PLAN_DIST);
						
						$DELIVERY_DATE_PLAN = $dom->createElement("DELIVERY_DATE_PLAN",$cEstimateTime);
						$ORDER->appendChild($DELIVERY_DATE_PLAN);
						
						$cEstimateTime2 = strtotime($cEstimateTime) + ($cDurationInPoint * 60);
						$DELIVERY_OUTDATE_PLAN = $dom->createElement("DELIVERY_OUTDATE_PLAN",date('Y-m-d H:i:s',$cEstimateTime2));
						$ORDER->appendChild($DELIVERY_OUTDATE_PLAN);
						
						$STATUS_VISIT = $dom->createElement("STATUS_VISIT",$cPointState);
						$ORDER->appendChild($STATUS_VISIT);
						
						
						
						if($i==0){ $firstPointState = $cPointState; }
						//$cPointStateText = "";
						
						writeToLogFile(' --> '.$i.'  firstWarehouse '.$firstWarehouse.' firstPointState '.$firstPointState.'  IsOrderPoint '.$IsOrderPoint.'  cPointState '.$cPointState);
						
						if( $firstPointState == 0 ){
							$cPointStateText = "1";
							//$cPointStateText = "Заказ принят";
						}elseif( ($firstPointState != 0) && $cPointState == 0 ){
							$cPointStateText = "2";
							//$cPointStateText = "Курьер в пути";
						}elseif($firstPointState == 1 && ($cPointState == 1 || $cPointState == 4)){
							$cPointStateText = "4";
							//$cPointStateText = "Заказ получен";
						}elseif($firstPointState == 1 && ($cPointState == 2 || $cPointState == 5 || $cPointState == 6)){
							$cPointStateText = "5";
							//$cPointStateText = "Заказ отменен";
						}elseif($firstPointState == 1 && ($cPointState == 3)){
							$cPointStateText = "6";
							//$cPointStateText = "Заказ принят частично";
							
							$query4 = "SELECT `Name` FROM `tms_checkpointcargo` WHERE `CheckPoint_ID` = ".$Id." AND `State`<>2";
							if ($stmt4 = $db4->prepare($query4)) {
							$stmt4->execute();
							$stmt4->bind_result($Name);
							while($stmt4->fetch())
							{
								$PARTIALLY_CARGO = $dom->createElement("PARTIALLY_CARGO",$Name);
								$ORDER->appendChild($PARTIALLY_CARGO);
							}
							$stmt4->close();
							}else{
								echo '<?xml version="1.0" encoding="utf-8"?><RESULT>Ошибка выполнения</RESULT>'; exit;
							}
						}
						
						$STATUS_POINT = $dom->createElement("STATUS_POINT",$cPointStateText);
						$ORDER->appendChild($STATUS_POINT);
						
						//if($IsOrderPoint == 0){ $IsOrderPoint = 'Склад'; }else{ $IsOrderPoint = 'Магазин'; }
						$TYPE_POINT = $dom->createElement("TYPE_POINT",$IsOrderPoint);
						$ORDER->appendChild($TYPE_POINT);
						$query4 = "SELECT SUM(`Cost`) FROM `tms_checkpointcargo` WHERE `CheckPoint_ID` = ".$Id." AND `State` = 2";
						if ($stmt4 = $db4->prepare($query4)) 
						{
							$stmt4->execute();
							$stmt4->bind_result($Cost);
							$stmt4->fetch();

							$CLIENT_PAYMENT = $dom->createElement("CLIENT_PAYMENT", $Cost);
							$ORDER->appendChild($CLIENT_PAYMENT);

							$stmt4->close();
						}

						$DRIVER_COMMENTS = $dom->createElement("COUR_COMMENTS", $DriverComments);
						$ORDER->appendChild($DRIVER_COMMENTS);
						
						
						
						$_PRODUCTS = $dom->createElement("PRODUCTS");
						$ORDER->appendChild($_PRODUCTS);
						
							$query4 = "SELECT `Name`,`CargoValueVolume`,`CargoValueWeight`,`CargoValueCount`,`Cost` FROM `tms_checkpointcargo` WHERE `CheckPoint_ID` = ".$Id."";
							if ($stmt4 = $db4->prepare($query4)) {
							$stmt4->execute();
							$stmt4->bind_result($Name,$Volume,$Weight,$Count,$Cost);
							while($stmt4->fetch())
							{
								
		$_PRODUCT = $dom->createElement("PRODUCT");
		$_PRODUCTS->appendChild($_PRODUCT);
		
		$_PROD_NAME = $dom->createAttribute("PROD_NAME");
		$_PROD_NAME->value = $Name;
		$_PRODUCT->appendChild($_PROD_NAME);
		
		$_PROD_CODE = $dom->createAttribute("PROD_CODE");
		$_PROD_CODE->value = 0;
		$_PRODUCT->appendChild($_PROD_CODE);
		
		$_WEIGHT = $dom->createAttribute("WEIGHT");
		$_WEIGHT->value = $Weight;
		$_PRODUCT->appendChild($_WEIGHT);
		
		$_VOLUME = $dom->createAttribute("VOLUME");
		$_VOLUME->value = $Volume;
		$_PRODUCT->appendChild($_VOLUME);
		
		$_COUNT = $dom->createAttribute("COUNT");
		$_COUNT->value = $Count;
		$_PRODUCT->appendChild($_COUNT);
		
		$_COST = $dom->createAttribute("COST");
		$_COST->value = $Cost;
		$_PRODUCT->appendChild($_COST);
								
							}
							$stmt4->close();
							}
						
		
		
						
						
						// ++++++++++ 
						//$query3 = "SELECT c.`OrderInTrip`,c.`DurationInPoint`,c.`GeoX`,c.`GeoY`,e.`date`,ADDTIME(e.`date`,'0 ".$gmt.":0:0'),e.`GeoX`,e.`GeoY` FROM tms_events e INNER JOIN `tms_checkpoint` c ON c.`Id` = e.ParamInt WHERE e.ParamInt = ".$Id." AND (e.EventType=28 OR e.EventType=13 OR e.EventType=14 OR e.EventType=15 OR e.EventType=4 OR e.EventType=19) ORDER BY e.`date` DESC LIMIT 1;";
/*$query3 = "SELECT c.`OrderInTrip`,c.`DurationInPoint`,c.`GeoX`,c.`GeoY`,e.`date`,ADDTIME(e.`date`,'0 ".$gmt.":0:0') AS DATE_FACT,e.`GeoX`,e.`GeoY`
,(SELECT ADDTIME(e.`date`,'0 ".$gmt.":0:0') FROM tms_events e INNER JOIN `tms_checkpoint` c ON c.`Id` = e.ParamInt WHERE e.ParamInt = ".$Id." AND (e.EventType=5 OR e.EventType=18 OR e.EventType=19) ORDER BY e.`date` DESC LIMIT 1) AS OUTDATE_FACT
FROM tms_events e INNER JOIN `tms_checkpoint` c ON c.`Id` = e.ParamInt
WHERE e.ParamInt = ".$Id."
AND (e.EventType=4 OR e.EventType=13 OR e.EventType=14 OR e.EventType=15)
ORDER BY e.`date` DESC LIMIT 1;";*/


$query3 = "SELECT c.`OrderInTrip`,c.`DurationInPoint`,c.`GeoX`,c.`GeoY`,e.`date`,ADDTIME(e.`date`,'0 ".$gmt.":0:0') AS DATE_FACT,e.`GeoX`,e.`GeoY`
,(SELECT ADDTIME(e.`date`,'0 ".$gmt.":0:0') FROM tms_events e INNER JOIN `tms_checkpoint` c ON c.`Id` = e.ParamInt WHERE e.ParamInt = ".$Id." AND (e.EventType=4 OR e.EventType=13 OR e.EventType=14 OR e.EventType=15) ORDER BY e.`date` DESC LIMIT 1) AS INDATE_FACT
,(SELECT ADDTIME(e.`date`,'0 ".$gmt.":0:0') FROM tms_events e INNER JOIN `tms_checkpoint` c ON c.`Id` = e.ParamInt WHERE e.ParamInt = ".$Id." AND (e.EventType=5 OR e.EventType=18 OR e.EventType=19) ORDER BY e.`date` DESC LIMIT 1) AS OUTDATE_FACT
FROM tms_events e INNER JOIN `tms_checkpoint` c ON c.`Id` = e.ParamInt
WHERE e.ParamInt = ".$Id."
ORDER BY e.`date` DESC LIMIT 1;";

						writeToLogFile($query3);
						if ($stmt3 = $db3->prepare($query3)) 
						{
							if ($stmt3->execute()) 
							{	
								$stmt3->bind_result($ccOrderInTrip,$ccDurationInPoint,$ccGeoX,$ccGeoY,$edate,$edateGMT,$eGeoX,$eGeoY,$indateGMT,$outedateGMT);
								$stmt3->store_result();
								if($stmt3->num_rows()>0)
								{
									$stmt3->fetch();
									if($ccOrderInTrip == $minOrderInTrip)
									{ 
										$startedate = $edate;
										$START_TIME_FACT = $dom->createElement("START_TIME_FACT", $edateGMT);
										$TRIP->appendChild($START_TIME_FACT);
									}
									elseif($ccOrderInTrip == $maxOrderInTrip)
									{
										$FINISH_TIME_FACT = $dom->createElement("FINISH_TIME_FACT", $edateGMT);
										$TRIP->appendChild($FINISH_TIME_FACT);
										
										$TRIP_DURATION_FACT = $dom->createElement("TRIP_DURATION_FACT", (strtotime($edate)-strtotime($startedate))/60 );
										$TRIP->appendChild($TRIP_DURATION_FACT);
										
										$TRIP_DIST_FACT = $dom->createElement("TRIP_DIST_FACT", 0);
										$TRIP->appendChild($TRIP_DIST_FACT);
									}
									
									//$DELIVERY_DATE_FACT = $dom->createElement("DELIVERY_DATE_FACT",$edate);
									//$DELIVERY_DATE_FACT = $dom->createElement("DELIVERY_DATE_FACT",$edateGMT);
									$DELIVERY_DATE_FACT = $dom->createElement("DELIVERY_DATE_FACT",$indateGMT);
									$ORDER->appendChild($DELIVERY_DATE_FACT);
									
									
									if(!$outedateGMT || $outedateGMT==""){
										//$edate2 = strtotime($edate) + ($ccDurationInPoint * 60);
										//$edate2 = strtotime($edateGMT) + ($ccDurationInPoint * 60);
										$edate2 = strtotime($indateGMT) + ($ccDurationInPoint * 60);
										$DELIVERY_OUTDATE_FACT = $dom->createElement("DELIVERY_OUTDATE_FACT",date('Y-m-d H:i:s',$edate2));
									}else{
										$DELIVERY_OUTDATE_FACT = $dom->createElement("DELIVERY_OUTDATE_FACT",$outedateGMT);
									}
									
									
									$ORDER->appendChild($DELIVERY_OUTDATE_FACT);
									
									$FACT_DIST = $dom->createElement("FACT_DIST",0);
									$ORDER->appendChild($FACT_DIST);
									
									$FACT_GEOX = $dom->createElement("FACT_GEOX",$eGeoX);
									$ORDER->appendChild($FACT_GEOX);
									
									$FACT_GEOY = $dom->createElement("FACT_GEOY",$eGeoY);
									$ORDER->appendChild($FACT_GEOY);
									
								}
								else
								{
									
									if($OrderInTrip == $minOrderInTrip)
									{
										$startedate = $cEstimateTime;
										$START_TIME_FACT = $dom->createElement("START_TIME_FACT", $cEstimateTime);
										$TRIP->appendChild($START_TIME_FACT);
									}
									elseif($OrderInTrip == $maxOrderInTrip)
									{
										$FINISH_TIME_FACT = $dom->createElement("FINISH_TIME_FACT", $cEstimateTime);
										$TRIP->appendChild($FINISH_TIME_FACT);
										
										$TRIP_DURATION_FACT = $dom->createElement("TRIP_DURATION_FACT", (strtotime($cEstimateTime)-strtotime($startedate))/60 );
										$TRIP->appendChild($TRIP_DURATION_FACT);
										
										$TRIP_DIST_FACT = $dom->createElement("TRIP_DIST_FACT", 0);
										$TRIP->appendChild($TRIP_DIST_FACT);
									}
								}
							}
							$stmt3->close();
						}else{
							echo '<?xml version="1.0" encoding="utf-8"?><RESULT>Ошибка выполнения</RESULT>'; exit;
						}
						$i++;
					}
				}	
				}
				$stmt2->close();
				}else{
					echo '<?xml version="1.0" encoding="utf-8"?><RESULT>Ошибка выполнения</RESULT>'; exit;
				}
	
		
		}
		}else{
			//echo 'Рейсов не найдено'; exit;
			echo '<?xml version="1.0" encoding="utf-8"?><RESULT>Рейсов не найдено</RESULT>'; exit;
			}
		$stmt->close();
		return  $dom->saveXML();
	}else{
		//echo 'Ошибка выполнения';
		echo '<?xml version="1.0" encoding="utf-8"?><RESULT>Ошибка выполнения</RESULT>'; exit;
	}
	
	$db->close();
	$db2->close();
	$db3->close();
	$db4->close();
	return false;
}