<?php
/*=======================================================================
 *
 * auto_response_timematters.php
 *
 * CUSTOMER: time:matters
 *
  =======================================================================*/


// Special implementation for customer
function sendResponse($stateEvent, $timestamp, $jbId, $trId, $trSort, $tr1CommissionNo = "", $crId = "", $crSid, $trSignName, $trSign, $gdc_01 = "", $gdc_02 = "", $gdc_03 = "", $gdc_04 = "", $gdc_05 = "") {
  global $server, $port;

  $isProductive = true;

  $server = "https://startrax.time-matters.com/partnerinfo/json";
  $password = "KA1nbUD2KdJnc2b";

  // Different servers for different stateEvents
  if ($stateEvent == "0" || $stateEvent == "1") :
     if ($isProductive) :
        $server = "https://startrax.time-matters.com/partnerinfo/json";  // Productive
        $password = "KA1nbUD2KdJnc2b";
     else :
        $server = "https://traxint.time-matters.com/partnerinfo/json";  // Test
        $password = "MKN}ft9FBe3]8n1i";
     endif;
     $numOfStations = getCountOfTable("tour", "jb_id = '" . $jbId . "'");
     if ($numOfStations != "" && is_numeric($numOfStations) && $numOfStations >= 3) :
        if ($isProductive) :
           $server = "https://startrax.time-matters.com/partnerinfo/json";  // Productive
           $password = "KA1nbUD2KdJnc2b";
        else :
           $server = "https://traxint.time-matters.com/partnerinfo/json";  // Test
           $password = "MKN}ft9FBe3]8n1i";
        endif;
     endif;
  elseif ($stateEvent == "2") :
     if ($isProductive) :
        $server = "https://startrax.time-matters.com/partnerinfo/json";  // Productive
        $password = "KA1nbUD2KdJnc2b";
     else :
        $server = "https://traxint.time-matters.com/partnerinfo/json";  // Test
        $password = "MKN}ft9FBe3]8n1i";
     endif;
  endif;
  
  // $crEid = "";
  // if ($crId != "" && $crId != "0") :
  //    $crEid = getFieldValueFromId("courier","cr_id",$crId,"cr_eid");
  // endif;

  $shipmentId = $gdc_01;  // Init

  // Convert timestamp from localtime to UTC
  $timestamp = gmdate("Y-m-d H:i:s", mktime(substr($timestamp, 11, 2), substr($timestamp, 14,2), substr($timestamp, 17, 2), substr($timestamp, 5, 2), substr($timestamp, 8,2), substr($timestamp, 0, 4)));
  $timestamp = substr($timestamp,0,10). "T" . substr($timestamp,11) . "Z";

  // Location zipcode
  $adId = getFieldValueFromId("tour","tr_id",$trId,"ad_id");
  $adZipcode = getFieldValueFromId("address","ad_id",$adId,"ad_zipcode");

  // Taketime "job" (UTC)
  $jbTaketime = getFieldValueFromId("job","jb_id",$jbId,"jb_taketime");
  $jbTaketime = gmdate("Y-m-d H:i:s", mktime(substr($jbTaketime, 11, 2), substr($jbTaketime, 14,2), substr($jbTaketime, 17, 2), substr($jbTaketime, 5, 2), substr($jbTaketime, 8,2), substr($jbTaketime, 0, 4)));
  $jbTaketime = substr($jbTaketime,0,10). "T" . substr($jbTaketime,11) . "Z";

  // Finishtime "tour" (UTC)
  $trFinishtime = getFieldValueFromId("tour","tr_id",$trId,"tr_finishtime");
  $trFinishtime = gmdate("Y-m-d H:i:s", mktime(substr($trFinishtime, 11, 2), substr($trFinishtime, 14,2), substr($trFinishtime, 17, 2), substr($trFinishtime, 5, 2), substr($trFinishtime, 8,2), substr($trFinishtime, 0, 4)));
  $trFinishtime = substr($trFinishtime,0,10). "T" . substr($trFinishtime,11) . "Z";

  $requestHeader = array();
  $requestBody = "";

  // if ($stateEvent != "2" || $shipmentId != "") :
  if ($shipmentId != "") :

      // Define header request
      // $requestHeader[] = "PUT /" . $shipmentId . " HTTP/1.1\r\n";
      // $requestHeader[] = "Host: " . $server . "\r\n";
      // $requestHeader[] = "Host: " . $server . $shipmentId . "\r\n";
      $requestHeader[] = "Content-Type: application/json; charset=utf-8\r\n";
      // $requestHeader[] = "Authorization: Boxture " . $apiKey . " \r\n";
      $requestHeader[] = "Content-Length: LENGTH\r\n";

      $shipments = array();

      $tmp_array = array();
      $tmp_array['user'] = "Stadtbote";
      $tmp_array['password'] = $password;
      $tmp_array['created'] = $timestamp;
      $tmp_array['routeID'] = $shipmentId;
      $tmp_array['orderNumber'] = $tr1CommissionNo;
      $tmp_array['trackingPoint'] = array();
      $tmp_array['trackingPoint']['weight'] = $gdc_02;
      $tmp_array['trackingPoint']['pieces'] = $gdc_03;
      $tmp_array['trackingPoint']['handover'] = $trSignName;
      $tmp_array['trackingPoint']['locationZip'] = $adZipcode;
      $tmp_array['trackingPoint']['type'] = "actual";

      if ($stateEvent == "0") {
      	  $tmp_array['trackingPoint']['comment'] = "Order confirmed";
      	  $tmp_array['trackingPoint']['utcdateTime'] = $jbTaketime;
      	  $tmp_array['trackingPoint']['event'] = "confirm";
      }
      else if ($stateEvent == "1") {
      	  $tmp_array['trackingPoint']['comment'] = "Shipment pickup";
      	  $tmp_array['trackingPoint']['utcdateTime'] = $trFinishtime;
      	  $tmp_array['trackingPoint']['event'] = "pickup";

      }
      else if ($stateEvent == "2") {
      	  $tmp_array['trackingPoint']['comment'] = "Shipment delivery";
      	  $tmp_array['trackingPoint']['utcdateTime'] = $trFinishtime;
      	  $tmp_array['trackingPoint']['event'] = "delivery";
      	

      	  // Get event state from GDC
      	  $eventCode = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = 'tr' AND gdc_gen_fieldname = 'del_code' AND gdc_obj_id = '" . $trId . "'");

      	  if ($eventCode != "" && $eventCode != "0" && is_numeric($eventCode)) :

             // Reasons
             if ($eventCode == "13") :
                $eventText = "Customs refusal";
             elseif ($eventCode == "24") :
                $eventText = "Examination required by relevant authority";
             elseif ($eventCode == "63") :
                $eventText = "Weather conditions";
             elseif ($eventCode == "31") :
                $eventText = "Industrial dispute"; 
             elseif ($eventCode == "88") :
                $eventText = "Accident involving means of transport"; 
             elseif ($eventCode == "58") :
                $eventText = "Transit delay"; 
             elseif ($eventCode == "7") :
                $eventText = "Business closed"; 
             elseif ($eventCode == "19") :
                $eventText = "Discrepancy"; 
             elseif ($eventCode == "30") :
                $eventText = "Incorrect address"; 
             elseif ($eventCode == "39") :
                $eventText = "No recipient contact information"; 
             elseif ($eventCode == "38") :
                $eventText = "New delivery arrangements"; 
             elseif ($eventCode == "95") :
                $eventText = "Excess goods delivered"; 
             elseif ($eventCode == "43") :
                $eventText = "Package not ready"; 
             elseif ($eventCode == "21") :
                $eventText = "Due to customer"; 
             elseif ($eventCode == "37") :
                $eventText = "Missing and/or incorrect documents"; 
             elseif ($eventCode == "1") :
                $eventText = "Address ex delivery area"; 
             elseif ($eventCode == "8") :
                $eventText = "Changed schedule"; 
             elseif ($eventCode == "41") :
                $eventText = "Not loaded"; 
             elseif ($eventCode == "2") :
                $eventText = "After transport departed"; 
             elseif ($eventCode == "67") :
                $eventText = "Administrative error"; 
             elseif ($eventCode == "62") :
                $eventText = "Unknown"; 
             elseif ($eventCode == "17") :
                $eventText = "Departure delay"; 
             elseif ($eventCode == "35") :
                $eventText = "Mechanical breakdown"; 
             elseif ($eventCode == "70") :
                $eventText = "Undefined incident attributed to logistic service provider"; 
             elseif ($eventCode == "23") :
                $eventText = "Equipment failure"; 
             elseif ($eventCode == "36") :
                $eventText = "Mechanical inspection"; 
             elseif ($eventCode == "33") :
                $eventText = "Lost goods/consignments/equipment"; 
             elseif ($eventCode == "53") :
                $eventText = "Sorted wrong route"; 
             elseif ($eventCode == "87") :
                $eventText = "Error";
             else :
                $eventText = "";
             endif;

             $tmp_array['delayCode'] = $eventCode;
             $tmp_array['delayReason'] = $eventText;
             $tmp_array['timestamps'] = array('undeliverableAt' => $timestamp);
          endif;
      }

      $frameArray = array();
      $frameArray['trackingInformation'] = $tmp_array;

      $shipments[] = $frameArray;
          
      $requestBody .= json_encode($shipments);
      // $requestBody .= array_to_json($shipments);
      $requestBody = substr($requestBody,1,-1);  // Remove "[" and "]"
      // $requestBody = urlencode($requestBody);

      $requestBodyLen = strlen($requestBody);

      $requestHeaderLen = count($requestHeader);
      for ($i = 0; $i < $requestHeaderLen; $i++) :
          if (!(strpos($requestHeader[$i], "LENGTH") === false)) :
             $requestHeader[$i] = str_replace("LENGTH", $requestBodyLen, $requestHeader[$i]);
          endif;
      endfor;
  else :
//    die();
  endif;
// echo implode("\n", $requestHeader) . "\n" . $requestBody . "\n"; die();
  $requestConfigArray = array();
  if ($requestBody != "") :

     $requestConfigArray["CURLOPT_URL"] = $server;
     // $requestConfigArray["CURLOPT_HTTPHEADER"] = $requestHeader;
     $requestConfigArray["CURLOPT_HTTPHEADER"] = array('Content-Type: application/json');
     $requestConfigArray["CURLOPT_RETURNTRANSFER"] = true;
     $requestConfigArray["CURLOPT_CUSTOMREQUEST"] = "PUT";
     $requestConfigArray["CURLOPT_POSTFIELDS"] = $requestBody;  // JSON
     $requestConfigArray["CURLOPT_PUT"] = true;
  endif;

  return $requestConfigArray;
}

/*
cs_id    cs_eid       emp_id   usr_id
833194   STB975597    834135   841591
819825   STDD908923   819943   825800
823862   STE900608    824266   830604
823978   STF909226    824386   830734
819783   STH910930    819901   825754
832514   STHB922142   833410   840785
203463   STHH927621   203464   208443
823922   STL908468    824329   830671
839904   STM900029    841607   850094

---------------------------------------------------------------

LIVE-SERVER

Berlin
    <auth>
      <customer>STB975597</customer>
      <account>STB975597</account>
      <password>B20211</password>
      <session_id>179db74797bde5f379b623ab73ee7c64eccbc87e4b5ce2fe28308fd9f2a7baf3d71eb342c50dca2186bce37ddca08cd1d41d8cd98f00b204e9800998ecf8427e</session_id>
      <costcenter_name></costcenter_name>
    </auth>

Dresden
    <auth>
      <customer>STDD908923</customer>
      <account>STDD908923</account>
      <password>DD70766</password>
      <session_id>2642c4088801f315789ed587f791f3f21679091c5a880faf6fb5e6087eb1b2dc05400adb9a25903c9bd4ab3399aa48c4d41d8cd98f00b204e9800998ecf8427e</session_id>
      <costcenter_name></costcenter_name>
    </auth>

Essen:
    <auth>
      <customer>STE900608</customer>
      <account>STE900608</account>
      <password>E19785</password>
      <session_id>9338acdf75d9aba376431a59973effaa8f14e45fceea167a5a36dedd4bea25432db4042d5f9efa9891bc2622189de8d9d41d8cd98f00b204e9800998ecf8427e</session_id>
      <costcenter_name></costcenter_name>
    </auth>

Frankfurt
    <auth>
      <customer>STF909226</customer>
      <account>STF909226</account>
      <password>F74661</password>
      <session_id>f0cd63afa3dd55eb9f7857db09d52ce3e4da3b7fbbce2345d7772b0674a318d5510125b94deae7feac2dbf11b70f99bad41d8cd98f00b204e9800998ecf8427e</session_id>
      <costcenter_name></costcenter_name>
    </auth>

Stuttgart
    <auth>
      <customer>STS910215</customer>
      <account>STS910215</account>
      <password>S87519</password>
      <session_id>322e03b788964c64c6a1806a36f21daf6512bd43d9caa6e02c990b0a82652dcab0dbfc9186e2efe4069de2755ebed87ed41d8cd98f00b204e9800998ecf8427e</session_id>
      <costcenter_name></costcenter_name>
    </auth>

Hamburg
    <auth>
      <customer>STHH927621</customer>
      <account>STHH927621</account>
      <password>HH56963</password>
      <session_id>0d4caa61a340cc953d6d0ec97ecd6180c81e728d9d4c2f636f067f89cc14862c79ee52e282692e10fbecc3a8bd2e1bdad41d8cd98f00b204e9800998ecf8427e</session_id>
      <costcenter_name></costcenter_name>
    </auth>

Hannover:
    <auth>
      <customer>STH910930</customer>
      <account>STH910930</account>
      <password>H29323</password>
      <session_id>89fcd07f20b6785b92134bd6c1d0fa42a87ff679a2f3e71d9181a67b7542122ccb47e8e20c0fa2f947d91b63e7017433d41d8cd98f00b204e9800998ecf8427e</session_id>
      <costcenter_name></costcenter_name>
    </auth>

Bremen
    <auth>
      <customer>STHB922142</customer>
      <account>STHB922142</account>
      <password>HB87513</password>
      <session_id>a41db61e2728ef963614a8c8755b9b9ac4ca4238a0b923820dcc509a6f75849b73470fc91e4e42f4c465bf4b92c3f118d41d8cd98f00b204e9800998ecf8427e</session_id>
      <costcenter_name></costcenter_name>
    </auth>

Leipzig
    <auth>
      <customer>STL908468</customer>
      <account>STL908468</account>
      <password>L87588</password>
      <session_id>30b6b8f7a8e3dba528fe76dec93d773ac9f0f895fb98ab9159f51fd0297e236db378d36bb689620d5819b127d0915efb67da9e2d2a17a760f08626cc02145f08</session_id>
      <costcenter_name></costcenter_name>
    </auth>
    
München
    <auth>
      <customer>STM900029</customer>
      <account>STM900029</account>
      <password>M82357</password>
      <session_id>9a02387b02ce7de2dac4b925892f68fb45c48cce2e2d7fbdea1afc51c7c6ad26f2d8c3218e07bfda1506998e80643362b086ab3a6b9acfc960265b6a0f7c7005</session_id>
      <costcenter_name></costcenter_name>
    </auth>
    
-----------------------------------------------------------------
    
TEST-SERVER

Berlin  (cs_id: 833194)
    <auth>
      <customer>STB975597</customer>
      <account>test2537</account>
      <password>B20211</password>
      <session_id>179db74797bde5f379b623ab73ee7c64eccbc87e4b5ce2fe28308fd9f2a7baf3d71eb342c50dca2186bce37ddca08cd1d41d8cd98f00b204e9800998ecf8427e</session_id>
      <costcenter_name></costcenter_name>
    </auth>

Hamburg  (cs_id: 203463)
    <auth>
      <customer>STHH927621</customer>
      <account>STHH927621</account>
      <password>HH56963</password>
      <session_id>0d4caa61a340cc953d6d0ec97ecd6180c81e728d9d4c2f636f067f89cc14862c79ee52e282692e10fbecc3a8bd2e1bdad41d8cd98f00b204e9800998ecf8427e</session_id>
      <costcenter_name></costcenter_name>
    </auth>
*/
?>