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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
*/
namespace Temando\Shipping\ViewModel\Batch;
use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Temando\Shipping\Model\BatchProviderInterface;
use Temando\Shipping\ViewModel\CoreApiInterface;
use Temando\Shipping\ViewModel\DataProvider\BatchUrl;
use Temando\Shipping\ViewModel\DataProvider\CoreApiAccess;
use Temando\Shipping\ViewModel\DataProvider\CoreApiAccessInterface;
use Temando\Shipping\ViewModel\DataProvider\EntityUrlInterface;
use Temando\Shipping\ViewModel\DataProvider\ShippingApiAccess;
use Temando\Shipping\ViewModel\DataProvider\ShippingApiAccessInterface;
use Temando\Shipping\ViewModel\ShippingApiInterface;
/**
* View model for batch list JS component.
*
* @package Temando\Shipping\ViewModel
* @author Rhodri Davies <rhodri.davies@temando.com>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link http://www.temando.com/
*/
class BatchNew implements ArgumentInterface, CoreApiInterface, ShippingApiInterface
{
/**
* @var CoreApiAccess
*/
private $coreApiAccess;
/**
* @var ShippingApiAccess
*/
private $shippingApiAccess;
/**
* @var UrlInterface
*/
private $urlBuilder;
/**
* @var BatchUrl
*/
private $batchUrl;
/**
* @var BatchProviderInterface
*/
private $batchProvider;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var Json
*/
private $serializer;
/**
* BatchNew constructor.
* @param CoreApiAccess $coreApiAccess
* @param ShippingApiAccess $shippingApiAccess
* @param UrlInterface $urlBuilder
* @param BatchUrl $batchUrl
* @param BatchProviderInterface $batchProvider
* @param StoreManagerInterface $storeManager
* @param ScopeConfigInterface $scopeConfig
* @param Json $serializer
*/
public function __construct(
CoreApiAccess $coreApiAccess,
ShippingApiAccess $shippingApiAccess,
UrlInterface $urlBuilder,
BatchUrl $batchUrl,
BatchProviderInterface $batchProvider,
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig,
Json $serializer
) {
$this->coreApiAccess = $coreApiAccess;
$this->shippingApiAccess = $shippingApiAccess;
$this->urlBuilder = $urlBuilder;
$this->batchUrl = $batchUrl;
$this->batchProvider = $batchProvider;
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
$this->serializer = $serializer;
}
/**
* @return CoreApiAccessInterface
*/
public function getCoreApiAccess(): CoreApiAccessInterface
{
return $this->coreApiAccess;
}
/**
* @return ShippingApiAccessInterface
*/
public function getShippingApiAccess(): ShippingApiAccessInterface
{
return $this->shippingApiAccess;
}
/**
* @return string
*/
public function getOrderListEndpoint(): string
{
$endpoint = $this->urlBuilder->getDirectUrl("rest/V1/orders", ['_secure' => true]);
return $endpoint;
}
/**
* @return EntityUrlInterface|BatchUrl
*/
public function getBatchUrl(): EntityUrlInterface
{
return $this->batchUrl;
}
/**
* Prepare component init order data.
*
* The component only needs the IDs, more details will be fetched via the
* salesOrderRepositoryV1 endpoint. Only the weight unit is not available
* there so we pass it right in here.
*
* @return string
*/
public function getOrderData(): string
{
$data = [];
$weightUnits = [];
$orders = $this->batchProvider->getOrders();
foreach ($orders as $order) {
$data[$order->getEntityId()] = [];
$data[$order->getEntityId()]['id'] = $order->getEntityId();
try {
$storeCode = $this->storeManager->getStore($order->getStoreId())->getCode();
if (!isset($weightUnits[$storeCode])) {
$weightUnit = $this->scopeConfig->getValue(
DirectoryHelper::XML_PATH_WEIGHT_UNIT,
ScopeInterface::SCOPE_STORE,
$storeCode
);
$weightUnits[$storeCode] = $weightUnit;
}
$data[$order->getEntityId()]['weight_unit'] = $weightUnits[$storeCode];
} catch (NoSuchEntityException $exception) {
$weightUnit = $this->scopeConfig->getValue(DirectoryHelper::XML_PATH_WEIGHT_UNIT);
$data[$order->getEntityId()]['weight_unit'] = $weightUnit;
}
}
return $this->serializer->serialize($data);
}
}