84 lines
2.8 KiB
PHP
84 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\PreordersBundle\Service;
|
|
|
|
use KupShop\CatalogBundle\ProductList\ProductCollection;
|
|
use KupShop\KupShopBundle\Context\ContextManager;
|
|
use KupShop\KupShopBundle\Context\PriceLevelContext;
|
|
use KupShop\PreordersBundle\Entity\Preorder;
|
|
use KupShop\PricelistBundle\Context\PricelistContext;
|
|
|
|
class DynamicPrices
|
|
{
|
|
public function __construct(
|
|
private readonly ContextManager $contextManager,
|
|
) {
|
|
}
|
|
|
|
public function inject(
|
|
ProductCollection $into,
|
|
\ProductList $from,
|
|
Preorder $preorder,
|
|
string $outKey = 'dynamic_prices',
|
|
): void {
|
|
$ranges = $this->getContextRanges($preorder);
|
|
|
|
foreach ($ranges as $contexts) {
|
|
$dynamicProducts = $this->contextManager->activateContexts($contexts, fn () => $from->getProducts());
|
|
|
|
foreach ($dynamicProducts as /* @var \Product $product */ $product) {
|
|
if ($product instanceof \Variation) {
|
|
$current = $into[$product->id]->variations[$product->variationId][$outKey] ?? [];
|
|
$current[] = array_merge([], ['price' => $product->getProductPrice()]);
|
|
|
|
$into[$product->id]->variations[$product->variationId][$outKey] = $current;
|
|
continue;
|
|
}
|
|
|
|
$current = $into[$product->id][$outKey] ?? [];
|
|
$current[] = array_merge([], ['price' => $product->getProductPrice()]);
|
|
|
|
$into[$product->id][$outKey] = $current;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function getContextRanges(Preorder $preorder, array $additional = []): array
|
|
{
|
|
$ranges = $preorder->getSettings()['dynamic_prices'] ?? [];
|
|
array_unshift($ranges, $preorder);
|
|
|
|
$resultingRanges = [];
|
|
foreach ($ranges as $range) {
|
|
$contexts = $additional;
|
|
|
|
$priceLevel = match (true) {
|
|
PreorderUtil::fieldIsComposedOfDigits($range, 'id_price_level') => (int) $range['id_price_level'],
|
|
|
|
PreorderUtil::fieldIsComposedOfDigits($preorder, 'id_price_level') => (int) $preorder['id_price_level'],
|
|
|
|
default => ContextManager::FORCE_EMPTY,
|
|
};
|
|
|
|
$pricelist = match (true) {
|
|
PreorderUtil::fieldIsComposedOfDigits($range, 'id_pricelist') => (int) $range['id_pricelist'],
|
|
|
|
PreorderUtil::fieldIsComposedOfDigits($preorder, 'id_pricelist') => (int) $preorder['id_pricelist'],
|
|
|
|
default => ContextManager::FORCE_EMPTY,
|
|
};
|
|
|
|
$contexts += [
|
|
PriceLevelContext::class => $priceLevel,
|
|
PricelistContext::class => $pricelist,
|
|
];
|
|
|
|
$resultingRanges[] = $contexts;
|
|
}
|
|
|
|
return $resultingRanges;
|
|
}
|
|
}
|