43 lines
1.4 KiB
PHP
43 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\POSBundle\Util;
|
|
|
|
use KupShop\OrderDiscountBundle\Util\DiscountManager;
|
|
use KupShop\OrderingBundle\Entity\Purchase\PurchaseState;
|
|
|
|
readonly class PosOrderDiscounts
|
|
{
|
|
public function __construct(protected DiscountManager $discountsManager)
|
|
{
|
|
}
|
|
|
|
public function applyBonusDataToPurchaseState(PurchaseState $purchaseState, array &$filter, ?int $bonusPoints = null): void
|
|
{
|
|
if (!findModule(\Modules::BONUS_PROGRAM) || !$purchaseState->getProducts()) {
|
|
return;
|
|
}
|
|
|
|
$this->discountsManager->setPurchaseState($purchaseState);
|
|
|
|
try {
|
|
$discountsEntities = $this->discountsManager->loadDiscountsEntities($this->discountsManager->getDiscountForCheck());
|
|
} catch (\Exception) {
|
|
$discountsEntities = [];
|
|
}
|
|
|
|
$allowed = ['bonus_points_earning', 'bonus_program', 'bonus_points'];
|
|
foreach ($discountsEntities as $discount) {
|
|
foreach ($discount->getActions() as $action) {
|
|
if (in_array($action['type'] ?? false, $allowed)) {
|
|
$filter = array_unique(array_merge($filter, [$discount->id]));
|
|
}
|
|
if (($action['type'] ?? false) == 'bonus_program' && $bonusPoints) {
|
|
$this->discountsManager->setActionHandlersData([$action['id'] => ['bonus_points' => "{$bonusPoints}"]]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|