|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Stockbase\Integration\StockbaseApi\Client; |
|
5
|
|
|
|
|
6
|
|
|
use Magento\Sales\Api\Data\OrderItemInterface; |
|
7
|
|
|
use Webmozart\Assert\Assert; |
|
8
|
|
|
use DivideBV\PHPDivideIQ\DivideIQ; |
|
9
|
|
|
use Magento\Sales\Api\Data\OrderInterface; |
|
10
|
|
|
use Stockbase\Integration\Model\Config\StockbaseConfiguration; |
|
11
|
|
|
use Stockbase\Integration\Model\StockItemReserve; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Stockbase API client. |
|
15
|
|
|
*/ |
|
16
|
|
|
class StockbaseClient |
|
17
|
|
|
{ |
|
18
|
|
|
const STOCKBASE_STOCK_ENDPOINT = 'stockbase_stock'; |
|
19
|
|
|
const STOCKBASE_IMAGES_ENDPOINT = 'stockbase_images'; |
|
20
|
|
|
const STOCKBASE_ORDER_REQUEST_ENDPOINT = 'stockbase_orderrequest'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var DivideIQ |
|
24
|
|
|
*/ |
|
25
|
|
|
private $divideIqClient; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var StockbaseConfiguration |
|
29
|
|
|
*/ |
|
30
|
|
|
private $stockbaseConfiguration; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* StockbaseClient constructor. |
|
34
|
|
|
* @param DivideIQ $divideIqClient |
|
35
|
|
|
* @param StockbaseConfiguration $stockbaseConfiguration |
|
36
|
4 |
|
*/ |
|
37
|
|
|
public function __construct( |
|
38
|
|
|
DivideIQ $divideIqClient, |
|
39
|
|
|
StockbaseConfiguration $stockbaseConfiguration |
|
40
|
4 |
|
) { |
|
41
|
4 |
|
$this->divideIqClient = $divideIqClient; |
|
42
|
4 |
|
$this->stockbaseConfiguration = $stockbaseConfiguration; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Gets current Stockbase stock state. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \DateTime|null $since |
|
49
|
|
|
* @param \DateTime|null $until |
|
50
|
|
|
* @return object |
|
51
|
|
|
* @throws \Exception |
|
52
|
1 |
|
*/ |
|
53
|
|
|
public function getStock(\DateTime $since = null, \DateTime $until = null) |
|
54
|
1 |
|
{ |
|
55
|
1 |
|
$data = []; |
|
56
|
1 |
|
if ($since !== null) { |
|
57
|
|
|
$data['Since'] = $since->getTimestamp(); |
|
58
|
1 |
|
} |
|
59
|
1 |
|
if ($until !== null) { |
|
60
|
|
|
$data['Until'] = $until->getTimestamp(); |
|
61
|
|
|
} |
|
62
|
1 |
|
|
|
63
|
|
|
return $this->divideIqClient->request(self::STOCKBASE_STOCK_ENDPOINT, $data); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Gets images for specified EANs. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string[] $eans |
|
70
|
|
|
* @return object |
|
71
|
|
|
* @throws \Exception |
|
72
|
1 |
|
*/ |
|
73
|
|
|
public function getImages(array $eans) |
|
74
|
1 |
|
{ |
|
75
|
|
|
Assert::allNumeric($eans); |
|
76
|
|
|
|
|
77
|
1 |
|
$data = [ |
|
78
|
|
|
'ean' => implode(',', $eans), |
|
79
|
|
|
]; |
|
80
|
1 |
|
|
|
81
|
|
|
return $this->divideIqClient->request(self::STOCKBASE_IMAGES_ENDPOINT, $data); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Downloads a file using current client configuration and saves it at the specified destination. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string|\GuzzleHttp\Url $uri File URI. |
|
88
|
|
|
* @param string|resource|\GuzzleHttp\Stream\StreamInterface $destination Destination where the file should be saved to. |
|
89
|
|
|
* @return null |
|
90
|
|
|
*/ |
|
91
|
|
|
public function downloadImage($uri, $destination) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->divideIqClient->download($uri, $destination); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Creates an order on Stockbase from reserved items for specified Magento order. |
|
98
|
|
|
* |
|
99
|
|
|
* @param OrderInterface $order |
|
100
|
|
|
* @param StockItemReserve[] $reservedStockbaseItems |
|
101
|
|
|
* @return object |
|
102
|
|
|
* @throws \Exception |
|
103
|
2 |
|
*/ |
|
104
|
|
|
public function createOrder(OrderInterface $order, array $reservedStockbaseItems) |
|
105
|
2 |
|
{ |
|
106
|
2 |
|
$orderPrefix = $this->stockbaseConfiguration->getOrderPrefix(); |
|
107
|
2 |
|
$shippingAddress = $order->getShippingAddress(); |
|
108
|
2 |
|
$now = new \DateTime('now', new \DateTimeZone('UTC')); |
|
109
|
|
|
$orderLines = []; |
|
110
|
2 |
|
|
|
111
|
2 |
|
$orderLineNumber = 0; |
|
112
|
2 |
|
foreach ($reservedStockbaseItems as $reserve) { |
|
113
|
2 |
|
$orderLineNumber++; |
|
114
|
2 |
|
$orderLineData = [ |
|
115
|
2 |
|
'Number' => $orderLineNumber, // Number starting from 1 |
|
116
|
2 |
|
'EAN' => $reserve->getEan(), |
|
117
|
|
|
'Amount' => (int) $reserve->getAmount(), |
|
118
|
|
|
]; |
|
119
|
|
|
|
|
120
|
|
|
/** @var OrderItemInterface $orderItem */ |
|
121
|
2 |
|
$orderItem = array_filter($order->getItems(), function(OrderItemInterface $item) use($reserve) { |
|
122
|
2 |
|
return $item->getItemId() == $reserve->getOrderItemId(); |
|
123
|
2 |
|
}); |
|
124
|
|
|
$orderItem = reset($orderItem); |
|
125
|
|
|
if ($orderItem && $orderItem->getRowTotal() !== null) { |
|
126
|
|
|
$orderLineData['Price'] = $orderItem->getRowTotal(); |
|
127
|
|
|
} |
|
128
|
2 |
|
$orderLines[] = $orderLineData; |
|
129
|
2 |
|
} |
|
130
|
2 |
|
|
|
131
|
|
|
$orderHeader = [ |
|
132
|
|
|
'OrderNumber' => $orderPrefix.'#'.$order->getRealOrderId(), |
|
133
|
2 |
|
'TimeStamp' => $now->format('Y-m-d h:i:s'), |
|
134
|
2 |
|
'Attention' => $order->getCustomerNote() ? $order->getCustomerNote() : ' ', |
|
135
|
2 |
|
]; |
|
136
|
2 |
|
|
|
137
|
2 |
|
$orderDelivery = [ |
|
138
|
|
|
'Person' => [ |
|
139
|
|
|
'FirstName' => $shippingAddress->getFirstname(), |
|
140
|
|
|
'Surname' => $shippingAddress->getLastname(), |
|
141
|
|
|
'Company' => $shippingAddress->getCompany() ?: ' ', |
|
142
|
2 |
|
], |
|
143
|
2 |
|
'Address' => [ |
|
144
|
2 |
|
'Street' => $shippingAddress->getStreetLine(1), |
|
145
|
|
|
'StreetNumber' => $shippingAddress->getStreetLine(2) ?: '-', |
|
146
|
|
|
'ZipCode' => $shippingAddress->getPostcode(), |
|
147
|
2 |
|
'City' => $shippingAddress->getCity(), |
|
148
|
2 |
|
'CountryCode' => $shippingAddress->getCountryId(), |
|
149
|
1 |
|
], |
|
150
|
1 |
|
]; |
|
151
|
1 |
|
|
|
152
|
1 |
|
$orderRequest = [ |
|
153
|
1 |
|
'OrderHeader' => $orderHeader, |
|
154
|
|
|
'OrderLines' => $orderLines, |
|
155
|
|
|
'OrderDelivery' => $orderDelivery, |
|
156
|
|
|
]; |
|
157
|
1 |
|
|
|
158
|
|
|
$response = $this->divideIqClient->request(self::STOCKBASE_ORDER_REQUEST_ENDPOINT, $orderRequest, 'POST'); |
|
159
|
|
|
if ($response->{'StatusCode'} != 1) { |
|
160
|
1 |
|
$message = ''; |
|
161
|
|
|
if (isset($response->{'Items'}) && is_array($response->{'Items'})) { |
|
162
|
|
|
foreach ($response->{'Items'} as $item) { |
|
163
|
|
|
if ($item->{'StatusCode'} != 1) { |
|
164
|
|
|
$message .= ' '.trim($item->{'ExceptionMessage'}); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
throw new StockbaseClientException('Failed sending order to stockbase.'.$message); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $response; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|