<?php 
/*=======================================================================
 *
 * locate_order_data_request.php
 *
 * Autor: Marc Vollmann
 *
  =======================================================================*/


include_once ("../include/mcglobal.inc.php");
// include_once ("../include/caglobal.inc.php");    // For pricing and invoice-text only
// include_once ('../include/email/htmlMimeMail.php');
// include_once ("../include/services_func.inc.php");
include_once ("../include/inc_parseXML.inc.php");
// include_once ("../include/inc_mtf_func.inc.php");
include_once ("../include/inc_customer.inc.php");
include_once ("../include/inc_job.inc.php");
// include_once ("../include/inc_vehicle.inc.php");
// include_once ("../include/inc_disposition.inc.php");
// include_once ("../include/inc_disposition_DEV.inc.php");
// include_once ("../geo/geocode.inc.php");
$standalone = "YES";
include_once ("../include/ranking.inc.php");


// Write logdata into log file
$currentTime = getDateTime("0");
$currentDate = getDateTime("3");

$debug = false;
$debugOut = "";

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

// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "locateOrderDataReq", "selectedLanguage"));

if ($locateOrderDataReq == "") :
   if (isset($argv[1])) : 
      $locateOrderDataReq = $argv[1];
   endif;
endif;

function locateOrderDataRequestLogFile ($msg, $logLevel = 0) {
  XMLRequestLogFile($msg, $logLevel, $logFile = "locateOrderDataReq.log");
}


$locateOrderDataReq = urldecode($locateOrderDataReq);
$locateOrderDataReq = str_replace("'", "", $locateOrderDataReq);
$locateOrderDataReq = mcEncode($locateOrderDataReq);
$locateOrderDataReq = str_replace("'", "", $locateOrderDataReq);
// echo $locateOrderDataReq . "<br><br>";

locateOrderDataRequestLogFile($currentTime);
locateOrderDataRequestLogFile($locateOrderDataReq);


if ($selectedLanguage == "" || !is_numeric($selectedLanguage)) : $selectedLanguage = "1"; endif;  // Default in English
$languageSelected = $selectedLanguage;

getLanguage(__FILE__);
getCurrentScript(__FILE__);

$err = array();
$locateOrderDataResponse = "";
$xmlLocateOrderData = "";


// ---------------------------------------------------------------------------

// GLOBAL MODE OF USAGE
$globalParUseRelatedCustomer = getParameterValue("0", "GLOBAL_USE_RELATED_CUSTOMER", "0");

// *** Parse request ***
$dataArray = xml2array($locateOrderDataReq);
if ($debug) :
   echo "Order data:<br><br>\n\n";
   print_r($dataArray); echo "<br><br><br>";
endif;


// *** Convert result array to usable parameters ***
// Authentication data
$sessionId      = convertSpecial($dataArray["xml"]["locate_order_data"]["auth"]["session_id"]);
$passPhrase     = convertSpecial($dataArray["xml"]["locate_order_data"]["auth"]["passphrase"]);

if (checkRequestAuthenticationData2($sessionId, $passPhrase)) :

   $debugOut .= "Authentication okay!<br><br>\n";

   // Get costcenter data
   $operation = convertSpecial($dataArray["xml"]["locate_order_data"]["operation"]);
   $debugOn   = convertSpecial($dataArray["xml"]["locate_order_data"]["debug_on"]);
   
   $debugOut .= "operation: " . $operation . "<br>\n";
   $debugOut .= "address_type: " . $address_type . "<br>\n";
   

   if (!$debug && $debugOn == "ON" || $debugOn == "YES") :
      $debug = true;
      
      echo "csId: " . $csId . "<br>";
      echo "csEid: " . $csEid . "<br>";
   endif;

// *********************************************************
/*
function getJobsByLocatingDummy ($gpsLong, $gpsLat, $radius) {
	$retArr = array();
	$retArr[] = array(10000, 53.077746, 10.003352, "Pension Haus Monika", "Nöllestraße",  "38A", "29646", "Bispingen");
	$retArr[] = array(10001, 53.115125, 9.791724, "Kulturverein Schneverdingen e. V.", "Oststraße",  "31", "29640", "Schneverdingen");
	$retArr[] = array(10002, 52.983248, 9.832366, "Bahnhof", "Am Bahnhof", "", "29614", "Soltau");
	// ........		
	return $retArr;
}
*/
// *********************************************************

   if ($operation == "SET") :

      // TO BE DONE!


   else :

      $gpsLat = convertSpecial($dataArray["xml"]["locate_order_data"]["gps_lat"]);
      $gpsLong = convertSpecial($dataArray["xml"]["locate_order_data"]["gps_long"]);
      $radius = convertSpecial($dataArray["xml"]["locate_order_data"]["radius"]);
      
      if ($debug) :
         echo "gpsLat: " . $gpsLat . "<br>";
         echo "gpsLong: " . $gpsLong . "<br><br>";
         echo "radius: " . $radius . "<br><br>";
      endif;

      // Check radius to get jobs for
      if ($radius == "" || !is_numeric($radius)) :
         $radius = "50";
      endif;

      if ($gpsLong != "" && $gpsLat != "") :
      
         $orderNumberArray = getJobsByLocating($gpsLong, $gpsLat, $radius);
         $orderNumberArrayLen = count($orderNumberArray);

         // Iterate array
         $xmlLocateOrderData .= "<locate_order_data>";
         
         if ($orderNumberArrayLen > 0) :
            $xmlLocateOrderData .= "<order_list>";
         
            for ($i = 0; $i < $orderNumberArrayLen; $i++) :

                $xmlLocateOrderData .= "<order>";

                $xmlLocateOrderData .= "<order_no>" . $orderNumberArray[$i][0] . "</order_no>";
                $xmlLocateOrderData .= "<gps_lat>" . $orderNumberArray[$i][1] . "</gps_lat>";
                $xmlLocateOrderData .= "<gps_long>" . $orderNumberArray[$i][2] . "</gps_long>";
                $xmlLocateOrderData .= "<name>" . $orderNumberArray[$i][3] . "</name>";
                $xmlLocateOrderData .= "<street>" . $orderNumberArray[$i][4] . "</street>";
                $xmlLocateOrderData .= "<house_no>" . $orderNumberArray[$i][5] . "</house_no>";
                $xmlLocateOrderData .= "<zipcode>" . $orderNumberArray[$i][6] . "</zipcode>";
                $xmlLocateOrderData .= "<city>" . $orderNumberArray[$i][7] . "</city>";
                $xmlLocateOrderData .= "<distance_km>" . $orderNumberArray[$i][8] . "</distance_km>";

                $xmlLocateOrderData .= "</order>";
            endfor;
            $xmlLocateOrderData .= "</order_list>";
         endif;
      endif;
      $xmlLocateOrderData .= "</locate_order_data>";

      if ($debug) :
         echo "RESPONSE: <br>" . $xmlLocateOrderData . "<br><br>";
      endif;
   endif;

else :
  // Authentication data not ok
  $err[] = array("101", getLngt("Die Authentifizierungsdaten sind nicht in Ordnung."));
endif;
// print_r($err);

// Check existence of at least one error
$errLen = count($err);

// Output
$locateOrderDataResponse .= "<xml>";   
if ($errLen > 0) :
   locateOrderDataRequestLogFile("ERRORS", 1);
   $locateOrderDataResponse .= "<state>NOT OK</state>";
   $locateOrderDataResponse .= "<errors>";
   for ($i = 0; $i < $errLen; $i++) :
       $locateOrderDataResponse .= "<error>";
       $locateOrderDataResponse .= "<err_no>" . $err[$i][0] . "</err_no>";
       $locateOrderDataResponse .= "<err_desc><![CDATA[" . mcEncode($err[$i][1]) . "]]></err_desc>";
       $locateOrderDataResponse .= "</error>";
       if ($debug) :
          $locateOrderDataResponse .= "<debug>" . $debugOut . "</debug>";
       endif;
       locateOrderDataRequestLogFile($err[$i][0] . " : " . mcEncode($err[$i][1]), 1);
   endfor;
   $locateOrderDataResponse .= "</errors>";
else :
   $locateOrderDataResponse .= "<state>OK</state>" . $xmlLocateOrderData;
   if ($debug) :
      $locateOrderDataResponse .= "<debug>" . $debugOut . "</debug>";
   endif;
endif;
$locateOrderDataResponse .= "</xml>";

// Log entries
locateOrderDataRequestLogFile($locateOrderDataResponse);    // Write response into log
locateOrderDataRequestLogFile("___________________________________________________________________________________");

// Return output
echo $locateOrderDataResponse;



/*
// REQUEST
<xml>
  <locate_order_data>                           // Request for all locateOrderData of the customer
    <auth>                                      // Abschnitt für Authentifikation des Requests
      <session_id>[mdtr]</session_id>           // Zusätzliche Session-ID, die mit dem Auftragnehmer ausgehandelt wurde   [mandatory]
      <passphrase>[mdtr]</passphrase>           // Per Algorithmus generierte Phrase, die per one-way-encryption ermittelt wird sowohl für den Versand als auch zum Abgleich bei Eingang
    </auth>                                     
                                                
    <debug_on>...</debug_on>                    // Activates debug output ("ON" or "YES")
                                                
    <operation>GET|SET</operation>              // Operation (GET or SET costcenter(s), default is "GET" if emty tag)

    <gps_lat><![CDATA[....]]></gps_lat>         // GSP latitude
    <gps_long><![CDATA[....]]></gps_long>       // GSP longitude
    <radius><![CDATA[....]]></radius>           // Radius to be requested for jobs

  </locate_order_data>
</xml>


// RESPONSE for GET
<xml>
  <state>OK</state>

  <locate_order_data>

    <order_list>

      <order>                                   // Single order
        <order_no>....</order_no>               // votian (requested) order number
        <gps_lat>....</gps_lat>                 // 
        <gps_long>....</gps_long>               // 
        <name>....</name>                       // 
        <street>....</street>                   // 
        <house_no>....</house_no>               // 
        <zipcode>....</zipcode>                 // 
        <city>....</city>                       // 
      </order>

      <order>                                   // Next single order
        ....
      </order>

    </order_list>

  </locate_order_data>
</xml>
*/
?>

