Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update insurance settings #856

Merged
merged 18 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions Model/Source/AbstractInsurancePossibilities.php

This file was deleted.

85 changes: 85 additions & 0 deletions Model/Source/CarrierInsurancePossibilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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 string $type;
protected AbstractConsignment $carrier;

/**
* @param string $carrierName
* @param string $type
* @throws Exception
*/
public function __construct(string $carrierName, string $type)
{
$this->type = $type;
$this->carrier = consignmentFactory::createByCarrierName($carrierName);
EdieLemoine marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @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]);
}
/**
EdieLemoine marked this conversation as resolved.
Show resolved Hide resolved
* @throws Exception
*/
protected function getInsurancePossibilitiesArray(?string $cc = null): array
{
return $this->carrier->getInsurancePossibilities($cc);
}

/**
* @return string|null
*/
public function getCc(): ?string
EdieLemoine marked this conversation as resolved.
Show resolved Hide resolved
{
$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) {
EdieLemoine marked this conversation as resolved.
Show resolved Hide resolved
$cc = CountryCodes::ZONE_EU;
}
if ($this->type === CountryCodes::ZONE_ROW) {
$cc = CountryCodes::ZONE_ROW;
}
return $cc;
}
}
123 changes: 59 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,41 @@ 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';
/**
EdieLemoine marked this conversation as resolved.
Show resolved Hide resolved
* @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";
EdieLemoine marked this conversation as resolved.
Show resolved Hide resolved
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 +124,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 +190,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
FlorianSDV marked this conversation as resolved.
Show resolved Hide resolved
*/
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->getInsurance($carrier, self::INSURANCE_BELGIUM_AMOUNT, $shippingCountry);
}

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;
}

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);
EdieLemoine marked this conversation as resolved.
Show resolved Hide resolved
$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