-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_fetch_status.php
More file actions
79 lines (69 loc) · 2.84 KB
/
Copy path03_fetch_status.php
File metadata and controls
79 lines (69 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* Example 03 - Track a shipment: fetch its normalized status and full event log.
* ---------------------------------------------------------------------------
* `fetchStatus()` calls SUUS `getEvents` and returns a StatusResult with:
* - status : a normalized ShipmentStatus enum (see mapping below)
* - rawLatestCode : the most recent native SUUS event code
* - events[] : the full StatusEvent history (code, description, location, time)
*
* SUUS native codes are mapped to a small, stable set of ShipmentStatus values:
* Created <- J_CR, KOL, M_KOL
* InTransit <- LOAD, ZALF, ZAL, M_DYS, WTRF, ...
* Delivered <- ROZF, UNDI, UNLO
* Cancelled <- ANUL
* Failed <- ZWRON, ZTF
*
* NOTE: in the sandbox, getEvents always returns PRJ000001 - run this against
* PRODUCTION with a real shipment number to see actual events.
*
* Run:
* SUUS_LOGIN=ws_xxx SUUS_PASSWORD=xxx php examples/03_fetch_status.php OPLKRI2600895
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use VeryCodeCom\Suus\SuusClient;
use VeryCodeCom\Suus\Enum\ShipmentStatus;
use VeryCodeCom\Suus\Exception\SuusException;
// getEvents returns real data only on production.
$client = SuusClient::production(
login: getenv('SUUS_LOGIN') ?: 'ws_yourlogin',
password: getenv('SUUS_PASSWORD') ?: 'your_password',
);
$shipmentNo = $argv[1] ?? 'OPLKRI2600895';
try {
$status = $client->fetchStatus($shipmentNo);
echo "Shipment : {$shipmentNo}\n";
echo "Status : {$status->status->value}\n";
echo "Latest code : {$status->rawLatestCode}\n";
echo "Delivered? : " . ($status->isDelivered() ? 'yes' : 'no') . "\n";
echo "Final state?: " . ($status->isFinal() ? 'yes' : 'no') . "\n";
// React to the normalized status with an exhaustive match.
$message = match ($status->status) {
ShipmentStatus::Pending => 'Order registered, awaiting pickup.',
ShipmentStatus::Created => 'Shipment created and scheduled.',
ShipmentStatus::InTransit => 'On its way to the recipient.',
ShipmentStatus::Delivered => 'Delivered to the recipient. ',
ShipmentStatus::Cancelled => 'Order was cancelled.',
ShipmentStatus::Failed => 'Delivery failed / returned to sender.',
};
echo "Summary : {$message}\n";
echo "\nEvent history (oldest -> newest):\n";
if ($status->events === []) {
echo " (no events yet)\n";
}
foreach ($status->events as $event) {
$line = sprintf(
' [%s] %-6s %s',
$event->occurredAt->format('Y-m-d H:i'),
$event->rawCode,
$event->description,
);
if ($event->location !== '') {
$line .= " ({$event->location})";
}
echo $line . "\n";
}
} catch (SuusException $e) {
echo "SUUS error: {$e->getMessage()}\n";
}