Skip to content

Commit

Permalink
Changed rounding/number parsing in price range quries
Browse files Browse the repository at this point in the history
  • Loading branch information
leszczuu committed Nov 19, 2020
1 parent bc965ae commit f723c40
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
31 changes: 31 additions & 0 deletions spec/QueryBuilder/HasPriceBetweenQueryBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,35 @@ function it_builds_query(
'max_price' => '1000',
])->shouldBeAnInstanceOf(Range::class);
}

function it_converts_fractional_currency_properly(
PriceNameResolverInterface $priceNameResolver,
ChannelContextInterface $channelContext,
ChannelInterface $channel,
CurrencyContextInterface $currencyContext,
CurrencyInterface $currency,
ConcatedNameResolverInterface $channelPricingNameResolver
): void {
$channel->getCode()->willReturn('web');
$channelContext->getChannel()->willReturn($channel);
$priceNameResolver->resolveMinPriceName()->willReturn('min_price');
$priceNameResolver->resolveMaxPriceName()->willReturn('max_price');
$channel->getBaseCurrency()->willReturn($currency);
$currency->getCode()->willReturn('USD');
$currencyContext->getCurrencyCode()->willReturn('USD');

$channelPricingNameResolver->resolvePropertyName('web')->willReturn('web');

$range = $this->buildQuery([
'min_price' => '1,23',
'max_price' => '1000,51',
]);

$range->getParam('web')->shouldReturn(
[
'gte' => 123,
'lte' => 100051,
]
);
}
}
4 changes: 3 additions & 1 deletion src/QueryBuilder/HasPriceBetweenQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\PriceNameResolverInterface;
use Elastica\Query\AbstractQuery;
use Elastica\Query\Range;
use Sylius\Bundle\MoneyBundle\Form\DataTransformer\SyliusMoneyTransformer;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Currency\Context\CurrencyContextInterface;
Expand Down Expand Up @@ -89,6 +90,7 @@ private function resolveBasePrice(string $price): int

private function convertFromString(string $price): int
{
return (int) round((int) $price * 100, 2);
$transformer = new SyliusMoneyTransformer(2, false, SyliusMoneyTransformer::ROUND_HALF_UP, 100);
return $transformer->reverseTransform($price);
}
}

0 comments on commit f723c40

Please sign in to comment.