62 lines
1.8 KiB
PHP
62 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace KupShop\OrderDiscountBundle\Triggers;
|
|
|
|
use KupShop\KupShopBundle\Context\UserContext;
|
|
use KupShop\KupShopBundle\Query\JsonOperator;
|
|
use KupShop\OrderingBundle\Entity\Purchase\PurchaseState;
|
|
use Query\Operator;
|
|
use Query\QueryBuilder;
|
|
|
|
class UserUsesCountTrigger extends AbstractTrigger
|
|
{
|
|
protected static $type = 'user_uses_count';
|
|
protected static $position = 55;
|
|
protected $adminTemplate = 'triggers/user_uses_count.tpl';
|
|
|
|
/** @var UserContext */
|
|
private $userContext;
|
|
|
|
/**
|
|
* @return $this
|
|
*
|
|
* @required
|
|
*/
|
|
public function setUserContext(UserContext $userContext)
|
|
{
|
|
$this->userContext = $userContext;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getDiscountFilterSpec(PurchaseState $purchaseState)
|
|
{
|
|
if ($user = $this->userContext->getActive()) {
|
|
$email = $user->email;
|
|
} else {
|
|
$email = $purchaseState->getCustomData('email');
|
|
}
|
|
|
|
if ($email) {
|
|
return function (QueryBuilder $qb) use ($email) {
|
|
$specs = [
|
|
Operator::equals(['odt.type' => static::getType()]),
|
|
];
|
|
|
|
$qb->setParameter('invoice_email', $email);
|
|
$qb_count = sqlQueryBuilder()
|
|
->select('COUNT(*)')
|
|
->from('orders', 'o')
|
|
->join('o', 'order_discounts_orders', 'odo', 'o.id = odo.id_order')
|
|
->where('odo.id_order_discount = odt.id_order_discount')
|
|
->andWhere('invoice_email=:invoice_email');
|
|
$specs[] = '('.$qb_count->getSQL().') >= '.JsonOperator::value('odt.data', 'max_uses_count');
|
|
|
|
return Operator::andX($specs);
|
|
};
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|