72 lines
2.2 KiB
PHP
72 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace External\PompoBundle\Overrides\BonusProgram\Utils;
|
|
|
|
use KupShop\BonusProgramBundle\Utils\BonusProvider;
|
|
use KupShop\OrderingBundle\Util\Discount\DiscountGenerator;
|
|
use Query\Operator;
|
|
|
|
class BonusExchangeUtil extends \KupShop\BonusProgramBundle\Utils\Exchange\BonusExchangeUtil
|
|
{
|
|
public function __construct(?BonusProvider $bonusProvider, DiscountGenerator $discountGenerator)
|
|
{
|
|
if (!$bonusProvider) {
|
|
return;
|
|
}
|
|
|
|
parent::__construct($bonusProvider, $discountGenerator);
|
|
}
|
|
|
|
protected function generateUserCoupon(array $item, int $discountId, int $userId): array
|
|
{
|
|
// Pokud je to partnersky kupon, tak negenerujeme novy, ale priradime uz existujici
|
|
if (($item['partner'] ?? 'N') === 'Y') {
|
|
if (!($couponId = $this->getAvailableCouponId($discountId))) {
|
|
throw new \RuntimeException('Available coupon was not found');
|
|
}
|
|
|
|
sqlQueryBuilder()
|
|
->update('discounts_coupons')
|
|
->set('date_activated', 'NOW()')
|
|
->directValues(
|
|
[
|
|
'id_user' => $userId,
|
|
]
|
|
)
|
|
->where(
|
|
Operator::equals(
|
|
[
|
|
'id' => $couponId,
|
|
]
|
|
)
|
|
)->execute();
|
|
|
|
return ['count' => 1, 'coupons' => []];
|
|
}
|
|
|
|
return parent::generateUserCoupon($item, $discountId, $userId);
|
|
}
|
|
|
|
private function getAvailableCouponId(int $discountId): ?int
|
|
{
|
|
$id = sqlQueryBuilder()
|
|
->select('id')
|
|
->from('discounts_coupons')
|
|
->andWhere(Operator::equals(['id_discount' => $discountId]))
|
|
->andWhere('id_user IS NULL AND id_order_purchased IS NULL AND id_order_used IS NULL')
|
|
->andWhere(Operator::equals(['used' => 'N']))
|
|
->andWhere('date_from <= NOW() OR date_from IS NULL')
|
|
->andWhere('date_to >= NOW() OR date_to IS NULL')
|
|
->orderBy('id', 'ASC')
|
|
->execute()->fetchOne();
|
|
|
|
if ($id) {
|
|
return (int) $id;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|