17
17
use Elastica \Query \AbstractQuery ;
18
18
use Elastica \Query \Range ;
19
19
use Sylius \Component \Channel \Context \ChannelContextInterface ;
20
+ use Sylius \Component \Core \Model \ChannelInterface ;
21
+ use Sylius \Component \Currency \Context \CurrencyContextInterface ;
22
+ use Sylius \Component \Currency \Converter \CurrencyConverterInterface ;
20
23
21
24
final class HasPriceBetweenQueryBuilder implements QueryBuilderInterface
22
25
{
@@ -29,23 +32,33 @@ final class HasPriceBetweenQueryBuilder implements QueryBuilderInterface
29
32
/** @var ChannelContextInterface */
30
33
private $ channelContext ;
31
34
35
+ /** @var CurrencyContextInterface */
36
+ private $ currencyContext ;
37
+
38
+ /** @var CurrencyConverterInterface */
39
+ private $ currencyConverter ;
40
+
32
41
public function __construct (
33
42
PriceNameResolverInterface $ priceNameResolver ,
34
43
ConcatedNameResolverInterface $ channelPricingNameResolver ,
35
- ChannelContextInterface $ channelContext
44
+ ChannelContextInterface $ channelContext ,
45
+ CurrencyContextInterface $ currencyContext ,
46
+ CurrencyConverterInterface $ currencyConverter
36
47
) {
37
48
$ this ->channelPricingNameResolver = $ channelPricingNameResolver ;
38
49
$ this ->priceNameResolver = $ priceNameResolver ;
39
50
$ this ->channelContext = $ channelContext ;
51
+ $ this ->currencyContext = $ currencyContext ;
52
+ $ this ->currencyConverter = $ currencyConverter ;
40
53
}
41
54
42
55
public function buildQuery (array $ data ): ?AbstractQuery
43
56
{
44
57
$ dataMinPrice = $ data [$ this ->priceNameResolver ->resolveMinPriceName ()];
45
58
$ dataMaxPrice = $ data [$ this ->priceNameResolver ->resolveMaxPriceName ()];
46
59
47
- $ minPrice = $ dataMinPrice ? $ this ->getPriceFromString ($ dataMinPrice ) : 0 ;
48
- $ maxPrice = $ dataMaxPrice ? $ this ->getPriceFromString ($ dataMaxPrice ) : PHP_INT_MAX ;
60
+ $ minPrice = $ dataMinPrice ? $ this ->resolveBasePrice ($ dataMinPrice ) : 0 ;
61
+ $ maxPrice = $ dataMaxPrice ? $ this ->resolveBasePrice ($ dataMaxPrice ) : PHP_INT_MAX ;
49
62
50
63
$ channelCode = $ this ->channelContext ->getChannel ()->getCode ();
51
64
$ propertyName = $ this ->channelPricingNameResolver ->resolvePropertyName ($ channelCode );
@@ -59,7 +72,22 @@ public function buildQuery(array $data): ?AbstractQuery
59
72
return $ rangeQuery ;
60
73
}
61
74
62
- private function getPriceFromString (string $ price ): int
75
+ private function resolveBasePrice (string $ price ): int
76
+ {
77
+ $ price = $ this ->convertFromString ($ price );
78
+ /** @var ChannelInterface $channel */
79
+ $ channel = $ this ->channelContext ->getChannel ();
80
+ $ currentCurrencyCode = $ this ->currencyContext ->getCurrencyCode ();
81
+ $ channelBaseCurrencyCode = $ channel ->getBaseCurrency ()->getCode ();
82
+
83
+ if ($ currentCurrencyCode !== $ channelBaseCurrencyCode ) {
84
+ $ price = $ this ->currencyConverter ->convert ($ price , $ currentCurrencyCode , $ channelBaseCurrencyCode );
85
+ }
86
+
87
+ return $ price ;
88
+ }
89
+
90
+ private function convertFromString (string $ price ): int
63
91
{
64
92
return (int ) round ($ price * 100 , 2 );
65
93
}
0 commit comments