-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAdditionalFeeConfiguration.php
103 lines (86 loc) · 2.6 KB
/
AdditionalFeeConfiguration.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
<?php
/**
* See LICENSE.md for license details.
*/
declare(strict_types=1);
namespace Dhl\Paket\Model\AdditionalFee;
use Dhl\Paket\Model\Carrier\Paket;
use Magento\Framework\Phrase;
use Magento\Quote\Model\Quote;
use Netresearch\ShippingCore\Api\AdditionalFee\AdditionalFeeConfigurationInterface;
use Netresearch\ShippingCore\Api\Data\ShippingSettings\ShippingOption\Selection\SelectionInterface;
use Netresearch\ShippingCore\Model\ShippingSettings\ShippingOption\Selection\QuoteSelectionManager;
class AdditionalFeeConfiguration implements AdditionalFeeConfigurationInterface
{
/**
* @var QuoteSelectionManager
*/
private $quoteSelectionManager;
/**
* @var AdditionalFeeProvider
*/
private $feeProvider;
/**
* @var float
*/
private $serviceAdjustment;
public function __construct(
QuoteSelectionManager $quoteSelectionManager,
AdditionalFeeProvider $feeProvider
) {
$this->quoteSelectionManager = $quoteSelectionManager;
$this->feeProvider = $feeProvider;
}
private function calculateAdjustmentAmount(Quote $quote): float
{
if (!is_float($this->serviceAdjustment)) {
$fees = $this->feeProvider->getAmounts($quote->getStoreId());
$selections = $this->quoteSelectionManager->load((int) $quote->getShippingAddress()->getId());
$serviceCodes = array_unique(
array_map(
static function (SelectionInterface $selection) {
return $selection->getShippingOptionCode();
},
$selections
)
);
$serviceAdjustment = 0;
foreach ($serviceCodes as $serviceCode) {
$serviceAdjustment += $fees[$serviceCode] ?? 0;
}
$this->serviceAdjustment = round($serviceAdjustment, 2);
}
return $this->serviceAdjustment;
}
/**
* @return string
*/
public function getCarrierCode(): string
{
return Paket::CARRIER_CODE;
}
/**
* @param Quote $quote
* @return bool
*/
public function isActive(Quote $quote): bool
{
$adjustment = $this->calculateAdjustmentAmount($quote);
return !empty($adjustment);
}
/**
* @param Quote $quote
* @return float
*/
public function getServiceCharge(Quote $quote): float
{
return $this->calculateAdjustmentAmount($quote);
}
/**
* @return Phrase
*/
public function getLabel(): Phrase
{
return __('DHL Shipping Service Adjustment');
}
}