| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter; |
| 6 |
use MyParcelNL\Sdk\src\Factory\ConsignmentFactory; |
| 7 |
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment; |
| 8 |
use MyParcelNL\Sdk\src\Support\Arr; |
| 9 |
use WPO\WC\PostNL\Compatibility\Order as WCX_Order; |
| 10 |
use WPO\WC\PostNL\Compatibility\Product as WCX_Product; |
| 11 |
|
| 12 |
class OrderSettings |
| 13 |
{ |
| 14 |
private const DEFAULT_COLLO_AMOUNT = 1; |
| 15 |
private const FIRST_INSURANCE = 1; |
| 16 |
public const DEFAULT_BELGIAN_INSURANCE = 500; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter |
| 20 |
*/ |
| 21 |
private $deliveryOptions; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var \WC_Order |
| 25 |
*/ |
| 26 |
private $order; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var string|null |
| 30 |
*/ |
| 31 |
private $carrier; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractShipmentOptionsAdapter|null |
| 35 |
*/ |
| 36 |
private $shipmentOptions; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var float |
| 40 |
*/ |
| 41 |
private $weight; |
| 42 |
|
| 43 |
/** |
| 44 |
* @var int |
| 45 |
*/ |
| 46 |
private $digitalStampRangeWeight; |
| 47 |
|
| 48 |
/** |
| 49 |
* @var bool |
| 50 |
*/ |
| 51 |
private $ageCheck; |
| 52 |
|
| 53 |
/** |
| 54 |
* @var bool |
| 55 |
*/ |
| 56 |
private $insured; |
| 57 |
|
| 58 |
/** |
| 59 |
* @var int |
| 60 |
*/ |
| 61 |
private $insuranceAmount; |
| 62 |
|
| 63 |
/** |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
private $labelDescription; |
| 67 |
|
| 68 |
/** |
| 69 |
* @var bool |
| 70 |
*/ |
| 71 |
private $largeFormat; |
| 72 |
|
| 73 |
/** |
| 74 |
* @var bool |
| 75 |
*/ |
| 76 |
private $onlyRecipient; |
| 77 |
|
| 78 |
/** |
| 79 |
* @var string |
| 80 |
*/ |
| 81 |
private $packageType; |
| 82 |
|
| 83 |
/** |
| 84 |
* @var bool |
| 85 |
*/ |
| 86 |
private $returnShipment; |
| 87 |
|
| 88 |
/** |
| 89 |
* @var bool |
| 90 |
*/ |
| 91 |
private $signature; |
| 92 |
|
| 93 |
/** |
| 94 |
* @var int |
| 95 |
*/ |
| 96 |
private $colloAmount; |
| 97 |
|
| 98 |
/** |
| 99 |
* @var array |
| 100 |
*/ |
| 101 |
private $extraOptions; |
| 102 |
|
| 103 |
/** |
| 104 |
* @var string |
| 105 |
*/ |
| 106 |
private $shippingCountry; |
| 107 |
|
| 108 |
/** |
| 109 |
* @param WC_Order $order |
| 110 |
* @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter|array|null $deliveryOptions |
| 111 |
* |
| 112 |
* @throws \JsonException |
| 113 |
* @throws \Exception |
| 114 |
*/ |
| 115 |
public function __construct( |
| 116 |
WC_Order $order, |
| 117 |
$deliveryOptions = null |
| 118 |
) { |
| 119 |
$this->order = $order; |
| 120 |
|
| 121 |
$this->setDeliveryOptions($deliveryOptions); |
| 122 |
$this->carrier = $this->deliveryOptions->getCarrier() ?? WCPN_Data::DEFAULT_CARRIER; |
| 123 |
$this->shipmentOptions = $this->deliveryOptions->getShipmentOptions(); |
| 124 |
$this->shippingCountry = WCX_Order::get_prop($order, 'shipping_country'); |
| 125 |
$this->extraOptions = WCX_Order::get_meta($order, WCPOST_Admin::META_SHIPMENT_OPTIONS_EXTRA); |
| 126 |
|
| 127 |
$this->setAllData(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @param bool $inGrams |
| 132 |
* |
| 133 |
* @return float |
| 134 |
*/ |
| 135 |
public function getWeight($inGrams = false): float |
| 136 |
{ |
| 137 |
return $inGrams |
| 138 |
? WCPN_Export::convertWeightToGrams($this->weight) |
| 139 |
: $this->weight; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @return int |
| 144 |
*/ |
| 145 |
public function getDigitalStampRangeWeight(): int |
| 146 |
{ |
| 147 |
return $this->digitalStampRangeWeight; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @return bool |
| 152 |
*/ |
| 153 |
public function hasAgeCheck(): bool |
| 154 |
{ |
| 155 |
return $this->ageCheck; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* @return int |
| 160 |
*/ |
| 161 |
public function getColloAmount(): int |
| 162 |
{ |
| 163 |
return $this->colloAmount; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* @return bool |
| 168 |
*/ |
| 169 |
public function isInsured(): bool |
| 170 |
{ |
| 171 |
return $this->insured; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* @return int |
| 176 |
*/ |
| 177 |
public function getInsuranceAmount(): int |
| 178 |
{ |
| 179 |
return $this->insuranceAmount; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* @return mixed|string |
| 184 |
*/ |
| 185 |
public function getLabelDescription(): string |
| 186 |
{ |
| 187 |
return $this->labelDescription; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* @return bool |
| 192 |
*/ |
| 193 |
public function hasLargeFormat(): bool |
| 194 |
{ |
| 195 |
return $this->largeFormat; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* @return bool |
| 200 |
*/ |
| 201 |
public function hasOnlyRecipient(): bool |
| 202 |
{ |
| 203 |
return $this->onlyRecipient; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* @return string |
| 208 |
*/ |
| 209 |
public function getPackageType(): string |
| 210 |
{ |
| 211 |
return $this->packageType; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* @return bool |
| 216 |
*/ |
| 217 |
public function hasReturnShipment(): bool |
| 218 |
{ |
| 219 |
return $this->returnShipment; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* @return bool |
| 224 |
*/ |
| 225 |
public function hasSignature(): bool |
| 226 |
{ |
| 227 |
return $this->signature; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* @throws \Exception |
| 232 |
*/ |
| 233 |
private function setAllData(): void |
| 234 |
{ |
| 235 |
$this->setPackageType(); |
| 236 |
$this->setColloAmount(); |
| 237 |
$this->setLabelDescription(); |
| 238 |
|
| 239 |
$this->setAgeCheck(); |
| 240 |
// $this->setLargeFormat(); |
| 241 |
$this->setOnlyRecipient(); |
| 242 |
$this->setReturnShipment(); |
| 243 |
$this->setSignature(); |
| 244 |
|
| 245 |
$this->setInsuranceData(); |
| 246 |
|
| 247 |
$this->setWeight(); |
| 248 |
$this->setDigitalStampRangeWeight(); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* @return void |
| 253 |
*/ |
| 254 |
private function setWeight(): void |
| 255 |
{ |
| 256 |
$weight = $this->extraOptions['weight'] ?? null; |
| 257 |
|
| 258 |
if (null === $weight && $this->order->meta_exists(WCPOST_Admin::META_ORDER_WEIGHT)) { |
| 259 |
$weight = $this->order->get_meta(WCPOST_Admin::META_ORDER_WEIGHT); |
| 260 |
} |
| 261 |
|
| 262 |
$this->weight = (float) $weight; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* @return void |
| 267 |
*/ |
| 268 |
private function setAgeCheck(): void |
| 269 |
{ |
| 270 |
$settingName = "{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_AGE_CHECK; |
| 271 |
$ageCheckFromShipmentOptions = $this->shipmentOptions->hasAgeCheck(); |
| 272 |
$ageCheckOfProduct = $this->getAgeCheckOfProduct(); |
| 273 |
$ageCheckFromSettings = (bool) WCPOST()->setting_collection->getByName($settingName); |
| 274 |
|
| 275 |
$this->ageCheck = $ageCheckFromShipmentOptions ?? $ageCheckOfProduct ?? $ageCheckFromSettings; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Gets product age check value based on if it was explicitly set to either true or false. It defaults to inheriting from the default export settings. |
| 280 |
* |
| 281 |
* @return ?bool |
| 282 |
*/ |
| 283 |
private function getAgeCheckOfProduct(): ?bool |
| 284 |
{ |
| 285 |
$hasAgeCheck = null; |
| 286 |
|
| 287 |
foreach ($this->order->get_items() as $item) { |
| 288 |
$product = $item->get_product(); |
| 289 |
|
| 290 |
if (! $product) { |
| 291 |
continue; |
| 292 |
} |
| 293 |
|
| 294 |
$productAgeCheck = WCX_Product::get_meta($product, WCPOST_Admin::META_AGE_CHECK, true); |
| 295 |
|
| 296 |
if ($productAgeCheck === WCPOST_Admin::PRODUCT_OPTIONS_ENABLED) { |
| 297 |
return true; |
| 298 |
} |
| 299 |
|
| 300 |
if ($productAgeCheck === WCPOST_Admin::PRODUCT_OPTIONS_DISABLED) { |
| 301 |
$hasAgeCheck = false; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
return $hasAgeCheck; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* @return void |
| 310 |
*/ |
| 311 |
private function setColloAmount(): void |
| 312 |
{ |
| 313 |
$this->colloAmount = (int) ($this->extraOptions['collo_amount'] ?? self::DEFAULT_COLLO_AMOUNT); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* @return void |
| 318 |
*/ |
| 319 |
private function setDigitalStampRangeWeight(): void |
| 320 |
{ |
| 321 |
$savedWeight = $this->extraOptions["digital_stamp_weight"] ?? null; |
| 322 |
$orderWeight = $this->getWeight(true); |
| 323 |
$weight = (float) ($savedWeight ?? $orderWeight); |
| 324 |
$results = Arr::where( |
| 325 |
WCPN_Data::getDigitalStampRanges(), |
| 326 |
static function ($range) use ($weight) { |
| 327 |
return $weight > $range['min']; |
| 328 |
} |
| 329 |
); |
| 330 |
|
| 331 |
if (empty($results)) { |
| 332 |
$digitalStampRangeWeight = Arr::first(WCPN_Data::getDigitalStampRanges())['average']; |
| 333 |
} else { |
| 334 |
$digitalStampRangeWeight = Arr::last($results)['average']; |
| 335 |
} |
| 336 |
|
| 337 |
$this->digitalStampRangeWeight = $digitalStampRangeWeight; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Sets insured and insuranceAmount. |
| 342 |
* |
| 343 |
* @return void |
| 344 |
*/ |
| 345 |
private function setInsuranceData(): void |
| 346 |
{ |
| 347 |
$isInsured = false; |
| 348 |
$insuranceAmount = 0; |
| 349 |
|
| 350 |
$isDefaultInsured = (bool) $this->getCarrierSetting(WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED); |
| 351 |
$isDefaultInsuredFromPrice = $this->getCarrierSetting(WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED_FROM_PRICE); |
| 352 |
$isDefaultInsuredForBE = (bool) $this->getCarrierSetting(WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED_FOR_BE); |
| 353 |
$orderTotalExceedsInsuredFromPrice = (float) $this->order->get_total() >= (float) $isDefaultInsuredFromPrice; |
| 354 |
$insuranceFromDeliveryOptions = $this->shipmentOptions->getInsurance(); |
| 355 |
$isBelgium = AbstractConsignment::CC_BE === $this->getShippingCountry(); |
| 356 |
|
| 357 |
$carrier = ConsignmentFactory::createByCarrierName($this->carrier); |
| 358 |
$amountPossibilities = $carrier::INSURANCE_POSSIBILITIES_LOCAL; |
| 359 |
|
| 360 |
if ($insuranceFromDeliveryOptions && $insuranceFromDeliveryOptions >= $amountPossibilities[self::FIRST_INSURANCE]) { |
| 361 |
$isInsured = (bool) $insuranceFromDeliveryOptions; |
| 362 |
$insuranceAmount = $insuranceFromDeliveryOptions; |
| 363 |
} elseif ($isDefaultInsured && $isBelgium) { |
| 364 |
$isInsured = $insuranceFromDeliveryOptions === 0 ? false : $isDefaultInsuredForBE; |
| 365 |
$insuranceAmount = $isInsured ? self::DEFAULT_BELGIAN_INSURANCE : 0; |
| 366 |
} elseif ($isDefaultInsured && $orderTotalExceedsInsuredFromPrice && $insuranceFromDeliveryOptions !== 0) { |
| 367 |
$isInsured = true; |
| 368 |
$insuranceAmount = $this->getCarrierSetting(WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED_AMOUNT); |
| 369 |
} |
| 370 |
|
| 371 |
$this->insured = $isInsured; |
| 372 |
$this->insuranceAmount = (int) $insuranceAmount; |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* @return void |
| 377 |
*/ |
| 378 |
private function setLabelDescription(): void |
| 379 |
{ |
| 380 |
$defaultValue = "Order: " . $this->order->get_id(); |
| 381 |
$valueFromSetting = WCPOST()->setting_collection->getByName(WCPOST_Settings::SETTING_LABEL_DESCRIPTION); |
| 382 |
$valueFromOrder = $this->shipmentOptions->getLabelDescription(); |
| 383 |
|
| 384 |
$this->labelDescription = (string) ($valueFromOrder ?? $valueFromSetting ?? $defaultValue); |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* @return void |
| 389 |
*/ |
| 390 |
private function setLargeFormat(): void |
| 391 |
{ |
| 392 |
$this->largeFormat = (bool) WCPN_Export::getChosenOrDefaultShipmentOption( |
| 393 |
$this->shipmentOptions->hasLargeFormat(), |
| 394 |
"{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_LARGE_FORMAT |
| 395 |
); |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* @return void |
| 400 |
*/ |
| 401 |
private function setOnlyRecipient(): void |
| 402 |
{ |
| 403 |
$this->onlyRecipient = (bool) WCPN_Export::getChosenOrDefaultShipmentOption( |
| 404 |
$this->shipmentOptions->hasOnlyRecipient(), |
| 405 |
"{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_ONLY_RECIPIENT |
| 406 |
); |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* @return void |
| 411 |
*/ |
| 412 |
private function setSignature(): void |
| 413 |
{ |
| 414 |
$this->signature = (bool) WCPN_Export::getChosenOrDefaultShipmentOption( |
| 415 |
$this->shipmentOptions->hasSignature(), |
| 416 |
"{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_SIGNATURE |
| 417 |
); |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* @return void |
| 422 |
* @throws \Exception |
| 423 |
*/ |
| 424 |
private function setPackageType(): void |
| 425 |
{ |
| 426 |
$packageType = WCPOST()->export->getPackageTypeFromOrder($this->order, $this->deliveryOptions); |
| 427 |
$this->packageType = $packageType; |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* @return void |
| 432 |
*/ |
| 433 |
private function setReturnShipment(): void |
| 434 |
{ |
| 435 |
$this->returnShipment = (bool) WCPN_Export::getChosenOrDefaultShipmentOption( |
| 436 |
$this->shipmentOptions->isReturn(), |
| 437 |
"{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_RETURN |
| 438 |
); |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* @param string $settingName |
| 443 |
* |
| 444 |
* @return mixed |
| 445 |
*/ |
| 446 |
private function getCarrierSetting(string $settingName) |
| 447 |
{ |
| 448 |
return WCPOST()->setting_collection->getByName("{$this->carrier}_" . $settingName); |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* @return string |
| 453 |
*/ |
| 454 |
public function getShippingCountry(): string |
| 455 |
{ |
| 456 |
return $this->shippingCountry; |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* @return \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter |
| 461 |
*/ |
| 462 |
public function getDeliveryOptions(): AbstractDeliveryOptionsAdapter |
| 463 |
{ |
| 464 |
return $this->deliveryOptions; |
| 465 |
} |
| 466 |
|
| 467 |
/** |
| 468 |
* @return array |
| 469 |
*/ |
| 470 |
public function getExtraOptions(): array |
| 471 |
{ |
| 472 |
return $this->extraOptions; |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter|array|null |
| 477 |
|
| 478 |
* |
| 479 |
* @throws \Exception |
| 480 |
*/ |
| 481 |
private function setDeliveryOptions($deliveryOptions = null): void |
| 482 |
{ |
| 483 |
if (is_a($deliveryOptions, AbstractDeliveryOptionsAdapter::class)) { |
| 484 |
$this->deliveryOptions = $deliveryOptions; |
| 485 |
} else { |
| 486 |
$this->deliveryOptions = WCPOST_Admin::getDeliveryOptionsFromOrder($this->order, (array) $deliveryOptions); |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
|