|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace floor12\DalliApi; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use floor12\DalliApi\Enum\DalliOrderStatus; |
|
9
|
|
|
use floor12\DalliApi\Models\Item; |
|
10
|
|
|
use floor12\DalliApi\Models\Response\DalliOrderStatusEvent; |
|
11
|
|
|
|
|
12
|
|
|
class OrderStatusDispatcher |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var Item[] */ |
|
15
|
|
|
protected $items = []; |
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
private $xmlBody; |
|
|
|
|
|
|
18
|
|
|
/** @var int|null */ |
|
19
|
|
|
private $statusTimestamp; |
|
|
|
|
|
|
20
|
|
|
/** @var string|null */ |
|
21
|
|
|
private $statusName; |
|
|
|
|
|
|
22
|
|
|
/** @var string|null */ |
|
23
|
|
|
private $statusId; |
|
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
2 |
|
private $paymentType; |
|
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
2 |
|
private $deliveredTo; |
|
|
|
|
|
|
28
|
2 |
|
/** @var string */ |
|
29
|
2 |
|
private $externalBarCode; |
|
|
|
|
|
|
30
|
2 |
|
/** |
|
31
|
|
|
* @var \$1|false|\SimpleXMLElement |
|
|
|
|
|
|
32
|
2 |
|
*/ |
|
33
|
|
|
private $xml; |
|
34
|
2 |
|
/** |
|
35
|
2 |
|
* @var DalliOrderStatusEvent[] |
|
36
|
2 |
|
*/ |
|
37
|
2 |
|
private $statuses; |
|
38
|
2 |
|
|
|
39
|
|
|
/** |
|
40
|
2 |
|
* @param string $xmlBody |
|
41
|
|
|
* @throws Exception |
|
42
|
2 |
|
*/ |
|
43
|
|
|
public function __construct(string $xmlBody) |
|
44
|
2 |
|
{ |
|
45
|
2 |
|
$this->xml = simplexml_load_string($xmlBody); |
|
46
|
2 |
|
if ($this->xml === false) |
|
47
|
2 |
|
throw new Exception('XML body is not valid.'); |
|
48
|
2 |
|
|
|
49
|
2 |
|
foreach ($this->xml->order->statushistory->status as $statusItem) { |
|
50
|
2 |
|
$this->statuses[] = new DalliOrderStatusEvent( |
|
51
|
2 |
|
trim($statusItem[0]), |
|
52
|
2 |
|
$statusItem['eventstore'], |
|
53
|
2 |
|
strtotime($statusItem['eventtime'])); |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
2 |
|
foreach ($this->xml->order->items->item as $item) { |
|
58
|
|
|
$orderItem = new Item(); |
|
59
|
|
|
$orderItem->setBarcode((string)$item['barcode']) |
|
60
|
|
|
->setQuantity((int)$item['quantity']) |
|
61
|
|
|
->setReturn(boolval($item['returns'])) |
|
62
|
|
|
->setRetprice((float)$item['retprice']); |
|
63
|
1 |
|
$this->items[] = $orderItem; |
|
64
|
|
|
} |
|
65
|
1 |
|
} |
|
66
|
1 |
|
|
|
67
|
1 |
|
/** |
|
68
|
|
|
* @param string|int $barcode |
|
69
|
|
|
* @return bool|null |
|
70
|
1 |
|
*/ |
|
71
|
|
|
public function isItemReturned($barcode): ?bool |
|
72
|
1 |
|
{ |
|
73
|
|
|
if (isset($this->items[$barcode])) |
|
74
|
|
|
return $this->items[$barcode]->isReturned(); |
|
75
|
1 |
|
return null; |
|
76
|
|
|
} |
|
77
|
1 |
|
|
|
78
|
|
|
public function getStatusId(): ?string |
|
79
|
|
|
{ |
|
80
|
1 |
|
if ($this->statuses) { |
|
|
|
|
|
|
81
|
|
|
return $this->statuses[0]->getStatusId(); |
|
82
|
1 |
|
} |
|
83
|
|
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getStatusName(): ?string |
|
87
|
|
|
{ |
|
88
|
1 |
|
if ($this->getStatusId()) |
|
89
|
|
|
DalliOrderStatus::getLabel($this->getStatusId()); |
|
90
|
1 |
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function getStatusTimestamp(): ?int |
|
94
|
|
|
{ |
|
95
|
|
|
if ($this->statuses) { |
|
|
|
|
|
|
96
|
|
|
return $this->statuses[0]->getTimestemp(); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
return null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getItems(): array |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->items; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return Item[] |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getReturnedItems(): array |
|
110
|
|
|
{ |
|
111
|
|
|
$returned = []; |
|
112
|
|
|
foreach ($this->items as $item) { |
|
113
|
|
|
if ($item->isReturned()) |
|
114
|
|
|
$returned[] = $item; |
|
115
|
|
|
} |
|
116
|
|
|
return $returned; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getOrderHistory(): ?array |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->statuses; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|