-
Notifications
You must be signed in to change notification settings - Fork 27
/
Data.php
executable file
·301 lines (261 loc) · 8.79 KB
/
Data.php
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
namespace MyParcelNL\Magento\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Module\ModuleListInterface;
use Magento\Store\Model\ScopeInterface;
use MyParcelNL\Magento\Model\Settings\AccountSettings;
use MyParcelNL\Sdk\src\Model\Carrier\AbstractCarrier;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierDHLEuroplus;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierDHLForYou;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierDHLParcelConnect;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierDPD;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierPostNL;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierUPS;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
use MyParcelNL\Sdk\src\Model\Consignment\DropOffPoint;
use MyParcelNL\Sdk\src\Services\CheckApiKeyService;
class Data extends AbstractHelper
{
public const MODULE_NAME = 'MyParcelNL_Magento';
public const XML_PATH_GENERAL = 'myparcelnl_magento_general/';
public const XML_PATH_POSTNL_SETTINGS = 'myparcelnl_magento_postnl_settings/';
public const XML_PATH_DHLFORYOU_SETTINGS = 'myparcelnl_magento_dhlforyou_settings/';
public const XML_PATH_DHLEUROPLUS_SETTINGS = 'myparcelnl_magento_dhleuroplus_settings/';
public const XML_PATH_DHLPARCELCONNECT_SETTINGS = 'myparcelnl_magento_dhlparcelconnect_settings/';
public const XML_PATH_UPS_SETTINGS = 'myparcelnl_magento_ups_settings/';
public const XML_PATH_DPD_SETTINGS = 'myparcelnl_magento_dpd_settings/';
public const XML_PATH_LOCALE_WEIGHT_UNIT = 'general/locale/weight_unit';
public const DEFAULT_WEIGHT = 1000;
public const CARRIERS_XML_PATH_MAP
= [
CarrierPostNL::NAME => self::XML_PATH_POSTNL_SETTINGS,
CarrierDHLForYou::NAME => self::XML_PATH_DHLFORYOU_SETTINGS,
CarrierDHLEuroplus::NAME => self::XML_PATH_DHLEUROPLUS_SETTINGS,
CarrierDHLParcelConnect::NAME => self::XML_PATH_DHLPARCELCONNECT_SETTINGS,
CarrierUPS::NAME => self::XML_PATH_UPS_SETTINGS,
CarrierDPD::NAME => self::XML_PATH_DPD_SETTINGS,
];
/**
* @var \Magento\Framework\Module\ModuleListInterface
*/
private $moduleList;
/**
* @var CheckApiKeyService
*/
private $checkApiKeyService;
/**
* Get settings by field
*
* @param Context $context
* @param ModuleListInterface $moduleList
* @param CheckApiKeyService $checkApiKeyService
*/
public function __construct(
Context $context,
ModuleListInterface $moduleList,
CheckApiKeyService $checkApiKeyService
)
{
parent::__construct($context);
$this->moduleList = $moduleList;
$this->checkApiKeyService = $checkApiKeyService;
}
/**
* Get settings by field
*
* @param $field
* @param null $storeId
*
* @return mixed
*/
public function getConfigValue($field, $storeId = null)
{
return $this->scopeConfig->getValue($field, ScopeInterface::SCOPE_STORE, $storeId);
}
/**
* Get general settings
*
* @param string $code
* @param null|int $storeId
*
* @return mixed
*/
public function getGeneralConfig(string $code = '', int $storeId = null)
{
return $this->getConfigValue(self::XML_PATH_GENERAL . $code, $storeId);
}
/**
* @throws \Exception
*/
public function getDropOffPoint(AbstractCarrier $carrier): ?DropOffPoint
{
$accountSettings = AccountSettings::getInstance();
$carrierConfiguration = $accountSettings->getCarrierConfigurationByCarrier($carrier);
if (! $carrierConfiguration) {
return null;
}
$dropOffPoint = $carrierConfiguration->getDefaultDropOffPoint();
if ($dropOffPoint && null === $dropOffPoint->getNumberSuffix()) {
$dropOffPoint->setNumberSuffix('');
}
return $dropOffPoint;
}
/**
* Get default settings
*
* @param string $carrier
* @param string $code
* @param null $storeId
*
* @return mixed
*/
public function getStandardConfig(string $carrier, string $code = '', $storeId = null)
{
return $this->getConfigValue(self::CARRIERS_XML_PATH_MAP[$carrier] . $code, $storeId);
}
/**
* Get carrier setting
*
* @param string $code
* @param string $carrier
*
* @return mixed
*/
public function getCarrierConfig(string $code, string $carrier)
{
$settings = $this->getConfigValue($carrier . $code);
if (null === $settings) {
$value = $this->getConfigValue($carrier . $code);
if (null === $value) {
$this->_logger->critical('Can\'t get setting with path:' . $carrier . $code);
}
return $value;
}
return $settings;
}
/**
* Get the version number of the installed module
*
* @return string
*/
public function getVersion(): string
{
$moduleCode = self::MODULE_NAME;
$moduleInfo = $this->moduleList->getOne($moduleCode);
return (string) $moduleInfo['setup_version'];
}
/**
* Check if api key is correct
*/
public function apiKeyIsCorrect(): bool
{
$apiKey = $this->getApiKey();
return $this->checkApiKeyService->setApiKey($apiKey)
->apiKeyIsCorrect();
}
/**
* @return null|string
*/
public function getApiKey(): ?string
{
return $this->getGeneralConfig('api/key');
}
/**
* Check if global API Key isset
*
* @return bool
*/
public function hasApiKey(): bool
{
$apiKey = $this->getApiKey();
return isset($apiKey);
}
/**
* Get date in YYYY-MM-DD HH:MM:SS format
*
* @param string|null $date
*
* @return string|null
*/
public function convertDeliveryDate(?string $date): ?string
{
if (! $date) {
return null;
}
$checkoutDate = json_decode($date, true)['date'] ?? substr($date, 0, 10);
$deliveryDate = strtotime(date('Y-m-d', strtotime($checkoutDate)));
$currentDate = strtotime(date('Y-m-d'));
if ($deliveryDate <= $currentDate) {
return date('Y-m-d H:i:s', strtotime('now +1 day'));
}
return $checkoutDate;
}
/**
* Get delivery type and when it is null use 'standard'
*
* @param int|null $deliveryType
*
* @return int
*/
public function checkDeliveryType(?int $deliveryType): int
{
if (! $deliveryType) {
return AbstractConsignment::DELIVERY_TYPE_STANDARD;
}
return $deliveryType;
}
/**
* @param int $orderId
* @param string $status
*/
public function setOrderStatus(int $orderId, string $status): void
{
$order = ObjectManager::getInstance()
->create('\Magento\Sales\Model\Order')
->load($orderId);
$order->setState($status)
->setStatus($status);
$order->save();
}
public function consignmentHasShipmentOption(AbstractConsignment $consignment, string $shipmentOption): bool
{
/**
* Business logic determining what shipment options to show, if any.
*/
if (AbstractConsignment::CC_NL === $consignment->getCountry()) {
return $consignment->canHaveShipmentOption($shipmentOption);
}
// For PostNL in Belgium - only recipient-only/signature is available
if (AbstractConsignment::CC_BE === $consignment->getCountry() && CarrierPostNL::NAME === $consignment->getCarrierName()) {
return in_array($shipmentOption, [
AbstractConsignment::SHIPMENT_OPTION_ONLY_RECIPIENT,
AbstractConsignment::SHIPMENT_OPTION_SIGNATURE], true);
}
// No shipment options available in any other case
return false;
}
/**
* Get the correct weight type
*
* @param null|float $weight
*
* @return int
*/
public function convertToGrams(?float $weight): int
{
$weightType = $this->getGeneralConfig('print/weight_indication');
if ('kilo' === $weightType) {
return (int) ($weight * 1000);
}
return (int) $weight ?: self::DEFAULT_WEIGHT;
}
/**
* @return string|null
*/
public function getExportMode(): ?string
{
return $this->getGeneralConfig('print/export_mode');
}
}