1. Import

This commit is contained in:
2026-03-29 10:34:57 +02:00
parent b0e00c1259
commit a1129565af
4899 changed files with 3007593 additions and 0 deletions

View File

@@ -0,0 +1,324 @@
<?php
/*=======================================================================
*
* tracking_amazon_IFTSTA.php
*
=======================================================================*/
// Generates an IFTSTA document according to specified tracking IDs and a "from timestamp"
function generateTrackingIFTSTA($trackingIDs, $createTimeFrom) {
global $db, $PHP_SELF;
// *****************
// * Define events *
// *****************
// Event messages
$eventMsgArray["EVENT_101"] = "Shipment has left seller facility and is in transit"; // Electronic Manifest transmitted by Amazon to the carrier and the carriers truck has departed the Amazon/seller facility
$eventMsgArray["EVENT_102"] = "Shipment received by carrier"; // Origin scan which confirms possession by the carrier
$eventMsgArray["EVENT_201"] = "Arrival Scan"; // Arrived at any of the carrier facilities
$eventMsgArray["EVENT_202"] = "Departure Scan"; // Departed any of the carrier facilities
$eventMsgArray["EVENT_203"] = "Arrived at destination country"; // Arrived at the destination country
$eventMsgArray["EVENT_204"] = "Initiated customs clearance process"; // Customs has started to clear the package
$eventMsgArray["EVENT_205"] = "Completed customs clearance process"; // Customs has cleared the package
$eventMsgArray["EVENT_206"] = "In transit to pickup location"; // Package is in transit to pickup location
$eventMsgArray["EVENT_207"] = "Problem resolved and shipment is in transit"; // Resolution of exception scan and package is in transit again
$eventMsgArray["EVENT_208"] = "Your selected store is closed for delivery and your shipment has been rerouted to a nearby store. The carrier will provide more information about the new store"; // Customer pick-up location is closed and the package was rerouted to a nearby pick-up location
$eventMsgArray["EVENT_300"] = "NOT DISPLAYED"; // Tendered to alternate carrier, line haul complete
$eventMsgArray["EVENT_301"] = "Delivered"; // Delivered to the customer directly
$eventMsgArray["EVENT_302"] = "Out for delivery"; // On the last leg of delivery to the customer
$eventMsgArray["EVENT_304"] = "Delivery attempted"; // Carrier attempted delivery, but unable to deliver to customer
$eventMsgArray["EVENT_306"] = "Customer contacted to arrange delivery"; // Carrier has contacted the customer to arrange delivery of the package
$eventMsgArray["EVENT_307"] = "Delivery appointment scheduled"; // Carrier has scheduled delivery of the package with the customer
$eventMsgArray["EVENT_308"] = "Available for pickup"; // Package is available for pickup from pickup location
$eventMsgArray["EVENT_309"] = "Returned to seller"; // Package was delivered back to sellers facility
$eventMsgArray["EVENT_310"] = "Tendered to local postal carrier for final delivery"; // Local postal carrier will perform final delivery to the customer. If assigned a new tracking id, carrier name and tracking id should be provided.
$eventMsgArray["EVENT_311"] = "Tendered to local postal carrier for final delivery and no proof of delivery will be provided"; // Local postal carrier will perform final delivery to the customer but they will not scan the package as delivered (international use).
$eventMsgArray["EVENT_401"] = "Incorrect address"; // Destination address does not exist or is incorrect
$eventMsgArray["EVENT_402"] = "Customs clearance delay"; // Any situation which causes a delay clearing customs
$eventMsgArray["EVENT_403"] = "Customer moved"; // Consignee no longer lives at the delivery address and left no forwarding address
$eventMsgArray["EVENT_404"] = "Delay in delivery due to external factors"; // This represents a definite delay in delivery and will trigger correspondences to the customer to notify them of the delay
$eventMsgArray["EVENT_405"] = "Shipment damaged"; // Carrier damaged the package
$eventMsgArray["EVENT_406"] = "Held by carrier"; // The package is being held by the carrier
$eventMsgArray["EVENT_407"] = "Customer refused delivery"; // Consignee refused to accept package
$eventMsgArray["EVENT_408"] = "Returning to seller"; // Amazon has requested the package be returned to Amazons facility
$eventMsgArray["EVENT_409"] = "Lost by carrier"; // Carrier lost the package
$eventMsgArray["EVENT_411"] = "Paperwork received - did not receive shipment"; // Carrier received electronic or paper manifest, but the shipment has not been picked up
$eventMsgArray["EVENT_412"] = "Received shipment - did not receive paperwork"; // Carrier received the package, but not the electronic/paper manifest. This is sent only when the shipment cannot move until the problem is resolved
$eventMsgArray["EVENT_413"] = "Customer refused shipment due to customs charges"; // Customer has refused shipment due to customs duties, taxes and fees charges.
$eventMsgArray["EVENT_414"] = "Possible delay in delivery due to arrival at incorrect carrier facility"; // Package was mis-sorted and delivery maybe delayed
$eventMsgArray["EVENT_415"] = "Received from prior carrier"; // Carrier has received the package from the previous carrier
$eventMsgArray["EVENT_416"] = "Undeliverable"; // Package could not be delivered, and Amazon has instructed the carrier to destroy the package or follow alternate instructions
$eventMsgArray["EVENT_418"] = "Returning to seller due to incorrect address"; // Unable to complete delivery due to incorrect address and the shipment is being returned to the seller
$eventMsgArray["EVENT_419"] = "Returning to seller because customer refused delivery"; // Customer refused delivery and the shipment is being returned to the seller
$eventMsgArray["EVENT_420"] = "Delay in delivery due to weather or natural disaster"; // Shipment delivery is delayed because of weather or natural disaster
$eventMsgArray["EVENT_421"] = "Unable to forward shipment to different address"; // Forwarding attempt failed
$eventMsgArray["EVENT_422"] = "Shipment forwarded to a different delivery address"; // Carrier has forwarded the package to an address
$eventMsgArray["EVENT_423"] = "Shipment damaged and will not be delivered"; // Carrier has identified the package as damaged, will not complete the delivery, and will dispose of the package
$eventMsgArray["EVENT_424"] = "Held for payment"; // Delivery of the package cannot be completed until payment is received by Carrier
$eventMsgArray["EVENT_425"] = "Possible delay in delivery due to extra carrier processing"; // There is possible delay in delivery because the carrier had to perform extra processing outside of the norm
$eventMsgArray["EVENT_426"] = "Confiscated or seized by an official government authority"; // The package will not be delivered nor returned due to being seized by an official government authority
$eventMsgArray["EVENT_427"] = "Held by carrier seller input required"; // The package is being held by the carrier because they require additional information from Amazon
$eventMsgArray["EVENT_428"] = "Address issue corrected by carrier but delivery could be delayed."; // The carrier was able to correct the customers address without any input from customer or Amazon
// ---------------------------------------------------------------------------
// Error messages
$errMsgArray["ERROR_101"] = "INVALID TRACKING NUMBER"; // Request contains an invalid tracking number
$errMsgArray["ERROR_102"] = "NO TRACKING INFO FOUND"; // No data exists in the carriers system for the given tracking number
$errMsgArray["ERROR_201"] = "INVALID USERID/PASSWORD"; // Request contains an invalid user ID or password
$errMsgArray["ERROR_202"] = "INVALID API VERSION"; // Request contains an incompatible API version
$errMsgArray["ERROR_203"] = "XML DOCUMENT NOT WELL FORMED"; // Request is not a well-formed XML document
$errMsgArray["ERROR_204"] = "XML DOCUMENT NOT VALID"; // Request is not a valid XML document
$errMsgArray["ERROR_301"] = "TRACKING SERVICE NOT AVAILABLE"; // Generic error code to handle any situation where Amazon
// ---------------------------------------------------------------------------
// Delivery location codes
$deliveryLocCodes["AS_INSTRUCTED"] = "As instructed"; // Package was delivered as per instructions
$deliveryLocCodes["CARPORT"] = "Carport"; // Package was left inside a carport
$deliveryLocCodes["CUSTOMER_PICKUP"] = "Picked up by customer"; // Package was picked up by customer
$deliveryLocCodes["DECK"] = "Deck"; // Package was left on an outside deck
$deliveryLocCodes["DOCK"] = "Shipping dock"; // Package was left on dock
$deliveryLocCodes["FRONT_DESK"] = "Front desk"; // Package was left at front desk
$deliveryLocCodes["FRONT_DOOR"] = "Front door"; // Package was left in or near the front door
$deliveryLocCodes["GARAGE"] = "Garage"; // Package was left in or near the garage door
$deliveryLocCodes["GUARD"] = "Guard"; // Package was handed to a security guard
$deliveryLocCodes["MAILBOX"] = "Mailbox"; // Package was left inside the residences mailbox
$deliveryLocCodes["MAIL_ROOM"] = "Mail room"; // Package was left in mail room
$deliveryLocCodes["MAIL_SLOT"] = "Mail slot"; // Package was left in mail slot
$deliveryLocCodes["MC_BOY"] = "Delivered to male child"; // Package was handed directly to a male child.
$deliveryLocCodes["MC_GIRL"] = "Delivered to female child"; // Package was handed directly to a female child.
$deliveryLocCodes["MC_MAN"] = "Delivered to male adult"; // Package was handed directly to a man
$deliveryLocCodes["MC_WOMAN"] = "Delivered to female adult"; // Package was handed directly to a woman
$deliveryLocCodes["NEIGHBOR"] = "Left with neighbor"; // Package was handed directly to a neighbor
$deliveryLocCodes["OUTBUILDING"] = "Outbuilding"; // Package was left in an outbuilding
$deliveryLocCodes["PATIO"] = "Patio"; // Package was left on an outside patio
$deliveryLocCodes["PORCH"] = "Porch"; // Package was left on an outside porch
$deliveryLocCodes["REAR_DOOR"] = "Rear door"; // Package was in or near the rear door
$deliveryLocCodes["RECEIVER"] = "Resident"; // Package was handed to resident
$deliveryLocCodes["RECEPTIONIST"] = "Receptionist"; // Package was handed to a receptionist
$deliveryLocCodes["SECURE_LOCATION"] = "Secure location"; // Package was left in a secure location
$deliveryLocCodes["SIDE_DOOR"] = "Side door"; // Package was left at side door
// Pickup location codes
$pickupLocCodes["CARRIER_FACILITY"] = "Available for pickup at carrier facility"; // Package can be picked up from carriers hub
$pickupLocCodes["CONVENIENCE_STORE"] = "Available for pickup at local convenience store"; // Package can be picked up at a local store
$pickupLocCodes["LOCAL_POST_OFFICE"] = "Available for pickup at your local post office"; // Package can be picked up at local post office
// Event messages for IFTSTA
$eventMsgIFTSTA["xxx"] = "..."; // Electronic Manifest transmitted by Amazon to the carrier and the carriers truck has departed the Amazon/seller facility
$eventMsgIFTSTA["xxx"] = "..."; // Origin scan which confirms possession by the carrier
// Mapping array of event numbers
$mapEventNo["101"] = "xxx";
$mapEventNo["102"] = "xxx";
$mapEventNo["201"] = "xxx";
$mapEventNo["202"] = "xxx";
$mapEventNo["301"] = "xxx";
$mapEventNo["302"] = "xxx";
$mapEventNo["304"] = "xxx";
$mapEventNo["306"] = "xxx";
$mapEventNo["307"] = "xxx";
$mapEventNo["308"] = "xxx";
$mapEventNo["309"] = "xxx";
$mapEventNo["401"] = "xxx";
$mapEventNo["403"] = "xxx";
$mapEventNo["404"] = "xxx";
$mapEventNo["405"] = "xxx";
$mapEventNo["406"] = "xxx";
$mapEventNo["407"] = "xxx";
$mapEventNo["408"] = "xxx";
// *************************
// * Get new tracking data *
// *************************
$response = ""; // Init response to be returned
$trackingIDsLen = count($trackingIDs);
if ($trackingIDsLen > 0 && $createTimeFrom != "") :
// Init arrays
$deliveryAddress = array();
$eventCounter = array();
$tratId = array();
$jbId = array();
$trSort = array();
$tratState = array();
$tratRemark = array();
$tratpType = array();
$tratpState = array();
$tratpRemark = array();
$tratpCreatetime = array();
$trSignname = array();
$trHsno = array();
$adStreet = array();
$adZipcode = array();
$adCity = array();
$adCountry = array();
for ($j = 0; $j < $trackingIDsLen; $j++) :
$trackingID = trim($trackingIDs[$j]);
if ($trackingID != "") :
// Get tracking ID from "tourarticle"
$sqlquery = "SELECT trat.trat_id, trat.jb_id, trat.tr_sort, trat.trat_state, trat.trat_remark,"
. " tratp.tratp_type, tratp.tratp_state, tratp.tratp_remark, tratp.tratp_createtime,"
. " tr.tr_signname, tr.tr_hsno,"
. " ad.ad_street, ad.ad_zipcode, ad.ad_city, ad.ad_country"
. " FROM phoenix.tourarticle AS trat, phoenix.tourarticleprocess AS tratp, phoenix.tour AS tr, phoenix.address AS ad"
. " WHERE trat.trat_serialno = '" . $trackingID . "' AND"
. " trat.trat_id = tratp.trat_id AND"
. " tratp.tratp_createtime >= '" . $createTimeFrom . "' AND"
. " trat.jb_id = tr.jb_id AND"
. " trat.tr_sort = tr.tr_sort AND"
. " tr.ad_id = ad.ad_id"
. " ORDER BY tratp.tratp_createtime";
$result = $db_conn->query($sqlquery);
if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
$i = 0;
$eventCounter[$j] = 0;
while ($row = $result->fetch_assoc()):
$tratId[$j][$i] = $row["trat_id"]; // [$j] = Index per tracking ID in "trat_serialno"
$jbId[$j][$i] = $row["jb_id"];
$trSort[$j][$i] = $row["tr_sort"];
$tratState[$j][$i] = $row["trat_state"];
$tratRemark[$j][$i] = $row["trat_remark"];
$tratpType[$j][$i] = $row["tratp_type"];
$tratpState[$j][$i] = $row["tratp_state"];
$tratpRemark[$j][$i] = $row["tratp_remark"];
$tratpCreatetime[$j][$i] = $row["tratp_createtime"];
$trSignname[$j][$i] = $row["tr_signname"];
$trHsno[$j][$i] = $row["tr_hsno"];
$adStreet[$j][$i] = $row["ad_street"];
$adZipcode[$j][$i] = $row["ad_zipcode"];
$adCity[$j][$i] = $row["ad_city"];
$adCountry[$j][$i] = $row["ad_country"];
$i++;
$eventCounter[$j]++; // How many events per tracking ID
endwhile;
$result->free();
// -------------------------------------
// Get delivery address and data of the end customer
$sqlquery = "SELECT trat.jb_id, trat.tr_sort,"
. " tr.tr_signname, tr.tr_hsno, tr.tr_person,"
. " ad.ad_street, ad.ad_zipcode, ad.ad_city, ad.ad_country"
. " FROM phoenix.tourarticle AS trat, phoenix.tour AS tr, phoenix.address AS ad"
. " WHERE trat.trat_serialno = '" . $trackingID . "' AND"
. " trat.jb_id = tr.jb_id AND"
. " trat.tr_sort = tr.tr_sort AND"
. " tr.ad_id = ad.ad_id"
. " ORDER BY trat.jb_id, trat.tr_sort";
$result = $db_conn->query($sqlquery);
if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
// The last row is the youngest order with the corresponding station no.
while ($row = $result->fetch_assoc()):
$deliveryAddress["jb_id"] = $row["jb_id"];
$deliveryAddress["tr_sort"] = $row["tr_sort"];
$deliveryAddress["ad_street"] = $row["ad_street"];
$deliveryAddress["tr_hsno"] = $row["tr_hsno"];
$deliveryAddress["ad_zipcode"] = $row["ad_zipcode"];
$deliveryAddress["ad_city"] = $row["ad_city"];
$deliveryAddress["ad_country"] = $row["ad_country"];
$deliveryAddress["tr_person"] = $row["tr_person"];
endwhile;
$result->free();
endif;
endfor; // End loop tracking IDs
// *********************
// * Generate response *
// *********************
$response .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$response .= "<AmazonTrackingResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"AmazonTrackingResponse.xsd\">";
$response .= "<APIVersion>3.1</APIVersion>";
// List tracking events
$eventResponse = "";
for ($j = 0; $j < $trackingIDsLen; $j++) :
$eventResponse .= "<TrackingNumber>" . $trackingIDs[$j] . "</TrackingNumber>";
$eventResponse .= "<PackageDestinationLocation>";
$eventResponse .= "<City>" . $deliveryAddress["ad_city"] . "</City>";
$eventResponse .= "<StateProvince></StateProvince>";
$eventResponse .= "<PostalCode>" . $deliveryAddress["ad_zipcode"] . "</PostalCode>";
$eventResponse .= "<CountryCode>" . $deliveryAddress["ad_country"] . "</CountryCode>";
$eventResponse .= "</PackageDestinationLocation>";
$eventResponse .= "<TrackingEventHistory>";
for ($i = 0; $i < $eventCounter[$j]; $i++) :
// Get event number of current (tourarticle)process entry mapped to IFTSTA
$tmpEventNo = $mapEventNo[$tratpType[$j][$i]];
if ($tmpEventNo != "" && is_numeric($tmpEventNo)) :
$eventResponse .= "<TrackingEventDetail>";
$eventResponse .= "<EventCode>EVENT_" . $tmpEventNo . "</EventCode>";
$eventResponse .= "<EventCodeDesc>" . $eventMsgIFTSTA[$tmpEventNo] . "</EventCodeDesc>";
$eventResponse .= "<EventDateTime>" . substr($tratpCreatetime[$j][$i],0,10) . "T" . substr($tratpCreatetime[$j][$i],11,8) . "+01:00" . "</EventDateTime>";
$eventResponse .= "<EventLocation>";
$eventResponse .= "<City>" . $adCity[$j][$i] . "</City>";
$eventResponse .= "<StateProvince>" . "" . "</StateProvince>";
$eventResponse .= "<PostalCode>" . $adZipcode[$j][$i] . "</PostalCode>";
$eventResponse .= "<CountryCode>" . ($adCountry[$j][$i] == "" ? "DE" : $adCountry[$j][$i]) . "</CountryCode>";
$eventResponse .= "</EventLocation>";
$eventResponse .= "<AdditionalLocationInfo>" . "" . "</AdditionalLocationInfo>";
$eventResponse .= "<SignedForByName>" . $trSignname[$j][$i] . "</SignedForByName>";
$eventResponse .= "</TrackingEventDetail>";
endif;
endfor;
$eventResponse .= "</TrackingEventHistory>";
endfor;
if ($eventResponse != "") :
$response .= "<PackageTrackingInfo>";
$response .= $eventResponse;
$response .= "</PackageTrackingInfo>";
endif;
$response .= "</AmazonTrackingResponse>";
// $response = urlencode($response);
$responseLen = strlen($response);
endif;
return $response;
}
// ************************************************************************************************************************************
// Generate response for TEST
$response = generateTrackingIFTSTA(array("123456789"), "2011-09-08 00:00:00");
echo $response;
// ************************************************************************************************************************************
?>