Skip to content

Commit

Permalink
feat: update insurance settings (#856)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianSDV authored Aug 1, 2024
1 parent 84aaae1 commit 129bd68
Show file tree
Hide file tree
Showing 8 changed files with 521 additions and 415 deletions.
41 changes: 0 additions & 41 deletions Model/Source/AbstractInsurancePossibilities.php

This file was deleted.

90 changes: 90 additions & 0 deletions Model/Source/CarrierInsurancePossibilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Magento\Model\Source;

use Exception;
use Magento\Framework\Data\OptionSourceInterface;
use MyParcelNL\Sdk\src\Factory\ConsignmentFactory;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
use MyParcelNL\Sdk\src\Services\CountryCodes;

class CarrierInsurancePossibilities implements OptionSourceInterface
{
protected $type;
protected $carrier;

/**
* @param string $carrierName
* @param string $type
* @throws Exception
*/
public function __construct(string $carrierName, string $type)
{
$this->type = $type;
$this->carrier = ConsignmentFactory::createByCarrierName($carrierName);
}

/**
* @return array
* @throws Exception
*/
public function toOptionArray(): array
{
return array_map(static function ($value, $key) {
return [
'value' => $key,
'label' => $value,
];
}, $this->toArray(), array_keys($this->toArray()));
}

/**
* @return array
* @throws Exception
*/
public function toArray(): array
{
$cc = $this->getCc();

return array_reduce($this->getInsurancePossibilitiesArray($cc), static function ($array, $insuranceValue) {
$array[$insuranceValue] = $insuranceValue;

return $array;
}, [0]);
}

/**
* @throws Exception
*/
protected function getInsurancePossibilitiesArray(?string $cc = null): array
{
return $this->carrier->getInsurancePossibilities($cc);
}

/**
* @return string|null
*/
private function getCc(): ?string
{
$cc = null;
if ($this->type === 'local') {
$cc = $this->carrier->getLocalCountryCode();
}

if ($this->type === AbstractConsignment::CC_BE) {
$cc = AbstractConsignment::CC_BE;
}

if ($this->type === CountryCodes::ZONE_EU) {
$cc = CountryCodes::ZONE_EU;
}

if ($this->type === CountryCodes::ZONE_ROW) {
$cc = CountryCodes::ZONE_ROW;
}

return $cc;
}
}
109 changes: 45 additions & 64 deletions Model/Source/DefaultOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use MyParcelNL\Magento\Helper\Checkout;
use MyParcelNL\Magento\Helper\Data;
use MyParcelNL\Magento\Model\Sales\Repository\PackageRepository;
use MyParcelNL\Sdk\src\Factory\ConsignmentFactory;
use MyParcelNL\Sdk\src\Factory\DeliveryOptionsAdapterFactory;
use MyParcelNL\Sdk\src\Model\Carrier\AbstractCarrier;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierFactory;
Expand All @@ -29,14 +30,27 @@ class DefaultOptions
{
// Maximum characters length of company name.
private const COMPANY_NAME_MAX_LENGTH = 50;
/** @deprecated */
private const INSURANCE_BELGIUM = 'insurance_belgium_custom';
private const INSURANCE_BELGIUM_AMOUNT = 500;
/** @deprecated */
private const INSURANCE_EU_AMOUNT_50 = 'insurance_eu_50';
/** @deprecated */
private const INSURANCE_EU_AMOUNT_500 = 'insurance_eu_500';
/** @deprecated */
private const INSURANCE_AMOUNT_100 = 'insurance_100';
/** @deprecated */
private const INSURANCE_AMOUNT_250 = 'insurance_250';
/** @deprecated */
private const INSURANCE_AMOUNT_500 = 'insurance_500';
/** @deprecated */
private const INSURANCE_AMOUNT_CUSTOM = 'insurance_custom';

private const INSURANCE_FROM_PRICE = 'insurance_from_price';
private const INSURANCE_LOCAL_AMOUNT = 'insurance_local_amount';
private const INSURANCE_BELGIUM_AMOUNT = 'insurance_belgium_amount';
private const INSURANCE_EU_AMOUNT = 'insurance_eu_amount';
private const INSURANCE_ROW_AMOUNT = 'insurance_row_amount';
private const INSURANCE_PERCENTAGE = 'insurance_percentage';
public const DEFAULT_OPTION_VALUE = 'default';

/**
Expand Down Expand Up @@ -96,18 +110,7 @@ public function hasDefault(string $option, string $carrier): bool
return true;
}

$total = self::$order->getGrandTotal();
$settings = self::$helper->getStandardConfig($carrier, 'default_options');
$activeKey = "{$option}_active";

if (! isset($settings[$activeKey])) {
return false;
}

$priceKey = "{$option}_from_price";

return '1' === $settings[$activeKey]
&& (! ($settings[$priceKey] ?? false) || $total > (int) $settings[$priceKey]);
return false;
}

/**
Expand Down Expand Up @@ -173,84 +176,62 @@ public function hasDefaultOptionsWithoutPrice(string $carrier, string $option):
/**
* Get default value of insurance based on order grand total
*
* @param string $carrier
* @param string $carrier
*
* @return int
* @throws \Exception
*/
public function getDefaultInsurance(string $carrier): int
{
$shippingAddress = self::$order->getShippingAddress();
$shippingCountry = $shippingAddress ? $shippingAddress->getCountryId() : AbstractConsignment::CC_NL;

if (AbstractConsignment::CC_NL === $shippingCountry) {
return $this->getDefaultLocalInsurance($carrier);
return $this->getInsurance($carrier, self::INSURANCE_LOCAL_AMOUNT, $shippingCountry);
}

if (AbstractConsignment::CC_BE === $shippingCountry) {
return $this->getDefaultBeInsurance($carrier);
}

return $this->getDefaultEuInsurance($carrier);
}

/**
* @param string $carrier
*
* @return int
*/
private function getDefaultEuInsurance(string $carrier): int
{
if ($this->hasDefault(self::INSURANCE_EU_AMOUNT_500, $carrier)) {
return 500;
return $this->getInsurance($carrier, self::INSURANCE_BELGIUM_AMOUNT, $shippingCountry);
}

if ($this->hasDefault(self::INSURANCE_EU_AMOUNT_50, $carrier)) {
return 50;
}

return 0;
}

/**
* @param string $carrier
*
* @return int
*/
private function getDefaultBeInsurance(string $carrier): int
{
if ($this->hasDefault(self::INSURANCE_BELGIUM, $carrier)) {
return self::$helper->getConfigValue(Data::CARRIERS_XML_PATH_MAP[$carrier] . 'default_options/insurance_belgium_custom_amount');
if (in_array($shippingCountry, AbstractConsignment::EURO_COUNTRIES)) {
return $this->getInsurance($carrier, self::INSURANCE_EU_AMOUNT, $shippingCountry);
}

return $this->hasDefault(self::INSURANCE_BELGIUM, $carrier) ? self::$helper->getConfigValue(
Data::CARRIERS_XML_PATH_MAP[$carrier] . 'default_options/insurance_belgium_custom_amount'
) : 0;
return $this->getInsurance($carrier, self::INSURANCE_ROW_AMOUNT, $shippingCountry);
}

/**
* @param string $carrier
*
* @return int
* @throws Exception
*/
private function getDefaultLocalInsurance(string $carrier): int
private function getInsurance(string $carrierName, string $priceKey, string $shippingCountry): int
{
if ($this->hasDefault(self::INSURANCE_AMOUNT_CUSTOM, $carrier)) {
return self::$helper->getConfigValue(Data::CARRIERS_XML_PATH_MAP[$carrier] . 'default_options/insurance_custom_amount');
$total = self::$order->getGrandTotal();
$settings = self::$helper->getStandardConfig($carrierName, 'default_options');
$totalAfterPercentage = $total * (($settings[self::INSURANCE_PERCENTAGE] ?? 0) / 100);

if (! isset($settings[$priceKey])
|| $settings[$priceKey] === 0
|| $totalAfterPercentage < $settings[self::INSURANCE_FROM_PRICE]) {
return 0;
}

if ($this->hasDefault(self::INSURANCE_AMOUNT_500, $carrier)) {
return 500;
}
$carrier = ConsignmentFactory::createByCarrierName($carrierName);
$insuranceTiers = $carrier->getInsurancePossibilities($shippingCountry);
sort($insuranceTiers);

if ($this->hasDefault(self::INSURANCE_AMOUNT_250, $carrier)) {
return 250;
}
$insurance = 0;
foreach ($insuranceTiers as $insuranceTier) {
$totalPriceFallsIntoTier = $totalAfterPercentage <= $insuranceTier;
$atMaxInsuranceTier = $insuranceTier >= $settings[$priceKey];

if ($this->hasDefault(self::INSURANCE_AMOUNT_100, $carrier)) {
return 100;
if ($totalPriceFallsIntoTier || $atMaxInsuranceTier) {
$insurance = $insuranceTier;
break;
}
}

return 0;
return $insurance;
}

/**
Expand Down
15 changes: 0 additions & 15 deletions Model/Source/PostNLInsurancePossibilities.php

This file was deleted.

Loading

0 comments on commit 129bd68

Please sign in to comment.