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 |
|
if ($this->xml->order) { |
49
|
2 |
|
|
50
|
2 |
|
foreach ($this->xml->order->statushistory->status as $statusItem) { |
51
|
2 |
|
$this->statuses[] = new DalliOrderStatusEvent( |
52
|
2 |
|
trim($statusItem[0]), |
53
|
2 |
|
$statusItem['eventstore'], |
54
|
2 |
|
strtotime($statusItem['eventtime'])); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
|
58
|
|
|
foreach ($this->xml->order->items->item as $item) { |
59
|
|
|
$orderItem = new Item(); |
60
|
|
|
$orderItem->setBarcode((string)$item['barcode']) |
61
|
|
|
->setQuantity((int)$item['quantity']) |
62
|
|
|
->setReturn(boolval($item['returns'])) |
63
|
1 |
|
->setRetprice((float)$item['retprice']); |
64
|
|
|
$this->items[] = $orderItem; |
65
|
1 |
|
} |
66
|
1 |
|
} |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
1 |
|
* @param string|int $barcode |
71
|
|
|
* @return bool|null |
72
|
1 |
|
*/ |
73
|
|
|
public function isItemReturned($barcode): ?bool |
74
|
|
|
{ |
75
|
1 |
|
if (isset($this->items[$barcode])) |
76
|
|
|
return $this->items[$barcode]->isReturned(); |
77
|
1 |
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function getStatusId(): ?string |
81
|
|
|
{ |
82
|
1 |
|
if ($this->statuses) { |
|
|
|
|
83
|
|
|
return $this->statuses[0]->getStatusId(); |
84
|
|
|
} |
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function getStatusName(): ?string |
89
|
|
|
{ |
90
|
1 |
|
if ($this->getStatusId()) |
91
|
|
|
DalliOrderStatus::getLabel($this->getStatusId()); |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getStatusTimestamp(): ?int |
96
|
|
|
{ |
97
|
|
|
if ($this->statuses) { |
|
|
|
|
98
|
|
|
return $this->statuses[0]->getTimestamp(); |
99
|
|
|
} |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getItems(): array |
104
|
|
|
{ |
105
|
|
|
return $this->items; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return Item[] |
110
|
|
|
*/ |
111
|
|
|
public function getReturnedItems(): array |
112
|
|
|
{ |
113
|
|
|
$returned = []; |
114
|
|
|
foreach ($this->items as $item) { |
115
|
|
|
if ($item->isReturned()) |
116
|
|
|
$returned[] = $item; |
117
|
|
|
} |
118
|
|
|
return $returned; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getOrderHistory(): ?array |
122
|
|
|
{ |
123
|
|
|
return $this->statuses; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getStatuses(): array |
127
|
|
|
{ |
128
|
|
|
return $this->statuses; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getExternalBarCode() |
132
|
|
|
{ |
133
|
|
|
return $this->externalBarCode; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|