Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function revert(PromotionSubjectInterface $subject, PromotionInterface $promotion)
 {
     foreach ($promotion->getActions() as $action) {
         $this->getActionCommandByType($action->getType())->revert($subject, $action->getConfiguration(), $promotion);
     }
     $subject->removePromotion($promotion);
 }
 function it_decrements_promotion_usage_if_promotion_was_used(OrderInterface $order, PromotionInterface $promotion)
 {
     $order->getPromotions()->willReturn([$promotion]);
     $promotion->getUsed()->willReturn(5);
     $promotion->setUsed(4)->shouldBeCalled();
     $this->decrementPromotionUsage($order);
 }
Exemplo n.º 3
0
 /**
  * @param PromotionInterface $promotion
  * @param string $header
  *
  * @return NodeElement
  */
 private function getPromotionFieldsWithHeader(PromotionInterface $promotion, $header)
 {
     $tableAccessor = $this->getTableAccessor();
     $table = $this->getElement('table');
     $fields = $tableAccessor->getFieldFromRow($table, $tableAccessor->getRowWithFields($table, ['code' => $promotion->getCode()]), $header);
     return $fields;
 }
 /**
  * {@inheritdoc}
  */
 public function isEligible(PromotionSubjectInterface $promotionSubject, PromotionInterface $promotion)
 {
     if (!$promotion->isCouponBased()) {
         throw new UnsupportedPromotionException('Only coupon based promotions can be evaluated by this checker.');
     }
     if (!$promotionSubject instanceof OrderInterface) {
         return false;
     }
     $coupon = $promotionSubject->getPromotionCoupon();
     if (!$coupon instanceof CouponInterface) {
         return false;
     }
     if ($promotion !== $coupon->getPromotion()) {
         return false;
     }
     $couponUsageLimit = $coupon->getPerCustomerUsageLimit();
     if (0 === $couponUsageLimit) {
         return true;
     }
     $customer = $promotionSubject->getCustomer();
     if (null === $customer) {
         return false;
     }
     $placedOrdersNumber = $this->orderRepository->countByCustomerAndCoupon($customer, $coupon);
     // <= because we need to include the cart orders as well
     return $placedOrdersNumber <= $couponUsageLimit;
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function createForPromotion(PromotionInterface $promotion)
 {
     Assert::true($promotion->isCouponBased(), sprintf('Promotion with name %s is not coupon based.', $promotion->getName()));
     $coupon = $this->factory->createNew();
     $coupon->setPromotion($promotion);
     return $coupon;
 }
Exemplo n.º 6
0
 /**
  * @param PromotionInterface $promotion
  * @param string $type
  *
  * @return AdjustmentInterface
  */
 protected function createAdjustment(PromotionInterface $promotion, $type = AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT)
 {
     $adjustment = $this->adjustmentFactory->createNew();
     $adjustment->setType($type);
     $adjustment->setLabel($promotion->getName());
     $this->originator->setOrigin($adjustment, $promotion);
     return $adjustment;
 }
Exemplo n.º 7
0
 function it_creates_a_coupon_and_assigns_a_promotion_to_id(FactoryInterface $factory, PromotionInterface $promotion, PromotionCouponInterface $coupon)
 {
     $factory->createNew()->willReturn($coupon);
     $promotion->getName()->willReturn('Christmas sale');
     $promotion->isCouponBased()->willReturn(true);
     $coupon->setPromotion($promotion)->shouldBeCalled();
     $this->createForPromotion($promotion)->shouldReturn($coupon);
 }
Exemplo n.º 8
0
 /**
  * @param OrderItemUnitInterface $unit
  * @param PromotionInterface $promotion
  */
 private function removeUnitOrderPromotionAdjustmentsByOrigin(OrderItemUnitInterface $unit, PromotionInterface $promotion)
 {
     foreach ($unit->getAdjustments(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT) as $adjustment) {
         if ($promotion->getCode() === $adjustment->getOriginCode()) {
             $unit->removeAdjustment($adjustment);
         }
     }
 }
 function it_returns_false_if_subject_coupons_is_not_eligible(PromotionCouponEligibilityCheckerInterface $promotionCouponEligibilityChecker, PromotionCouponAwarePromotionSubjectInterface $promotionSubject, PromotionInterface $promotion, PromotionCouponInterface $promotionCoupon)
 {
     $promotion->isCouponBased()->willReturn(true);
     $promotionSubject->getPromotionCoupon()->willReturn($promotionCoupon);
     $promotionCoupon->getPromotion()->willReturn($promotion);
     $promotionCouponEligibilityChecker->isEligible($promotionSubject, $promotionCoupon)->willReturn(false);
     $this->isEligible($promotionSubject, $promotion)->shouldReturn(false);
 }
Exemplo n.º 10
0
 /**
  * @param PromotionInterface $promotion
  *
  * @return AdjustmentInterface
  */
 protected function createAdjustment(PromotionInterface $promotion)
 {
     $adjustment = $this->adjustmentFactory->createNew();
     $adjustment->setType(AdjustmentInterface::PROMOTION_ADJUSTMENT);
     $adjustment->setDescription($promotion->getDescription());
     $this->originator->setOrigin($adjustment, $promotion);
     return $adjustment;
 }
 /**
  * @param PromotionInterface $promotion
  * @param string $type
  *
  * @return AdjustmentInterface
  */
 protected function createAdjustment(PromotionInterface $promotion, $type = AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT)
 {
     /** @var AdjustmentInterface $adjustment */
     $adjustment = $this->adjustmentFactory->createNew();
     $adjustment->setType($type);
     $adjustment->setLabel($promotion->getName());
     $adjustment->setOriginCode($promotion->getCode());
     return $adjustment;
 }
 /**
  * {@inheritdoc}
  */
 public function isEligible(PromotionInterface $promotion)
 {
     if (null === ($usageLimit = $promotion->getUsageLimit())) {
         return true;
     }
     if ($promotion->getUsed() < $usageLimit) {
         return true;
     }
     return false;
 }
Exemplo n.º 13
0
 /**
  * {@inheritdoc}
  */
 public function execute(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion)
 {
     if (!$subject instanceof OrderInterface && !$subject instanceof OrderItemInterface) {
         throw new UnexpectedTypeException($subject, 'Sylius\\Component\\Core\\Model\\OrderInterface or Sylius\\Component\\Core\\Model\\OrderItemInterface');
     }
     $adjustment = $this->repository->createNew();
     $adjustment->setAmount(-$configuration['amount']);
     $adjustment->setLabel(OrderInterface::PROMOTION_ADJUSTMENT);
     $adjustment->setDescription($promotion->getDescription());
     $subject->addAdjustment($adjustment);
 }
Exemplo n.º 14
0
 function it_reverts_all_actions_registered(ServiceRegistryInterface $registry, PromotionActionCommandInterface $action, PromotionSubjectInterface $subject, PromotionInterface $promotion, PromotionActionInterface $actionModel)
 {
     $configuration = [];
     $registry->get('test_action')->willReturn($action);
     $promotion->getActions()->willReturn([$actionModel]);
     $actionModel->getType()->willReturn('test_action');
     $actionModel->getConfiguration()->willReturn($configuration);
     $action->revert($subject, $configuration, $promotion)->shouldBeCalled();
     $subject->removePromotion($promotion)->shouldBeCalled();
     $this->revert($subject, $promotion);
 }
Exemplo n.º 15
0
 function it_applies_fixed_discount_as_promotion_adjustment($adjustmentRepository, OrderInterface $order, AdjustmentInterface $adjustment, PromotionInterface $promotion)
 {
     $adjustmentRepository->createNew()->willReturn($adjustment);
     $promotion->getDescription()->willReturn('promotion description');
     $adjustment->setAmount(-500)->shouldBeCalled();
     $adjustment->setLabel(OrderInterface::PROMOTION_ADJUSTMENT)->shouldBeCalled();
     $adjustment->setDescription('promotion description')->shouldBeCalled();
     $order->addAdjustment($adjustment)->shouldBeCalled();
     $configuration = array('amount' => 500);
     $this->execute($order, $configuration, $promotion);
 }
Exemplo n.º 16
0
 function it_should_revert_all_actions_registered(ServiceRegistryInterface $registry, PromotionActionInterface $action, PromotionSubjectInterface $subject, PromotionInterface $promotion, ActionInterface $actionModel)
 {
     $configuration = array();
     $registry->get(ActionInterface::TYPE_FIXED_DISCOUNT)->shouldBeCalled()->willReturn($action);
     $promotion->getActions()->shouldBeCalled()->willReturn(array($actionModel));
     $actionModel->getType()->shouldBeCalled()->willReturn(ActionInterface::TYPE_FIXED_DISCOUNT);
     $actionModel->getConfiguration()->shouldBeCalled()->willReturn($configuration);
     $action->revert($subject, $configuration, $promotion)->shouldBeCalled();
     $subject->removePromotion($promotion)->shouldBeCalled();
     $this->revert($subject, $promotion);
 }
 /**
  * {@inheritdoc}
  */
 public function isEligible(PromotionSubjectInterface $promotionSubject, PromotionInterface $promotion)
 {
     if (!$promotion->hasRules()) {
         return true;
     }
     foreach ($promotion->getRules() as $rule) {
         if (!$this->isEligibleToRule($promotionSubject, $rule)) {
             return false;
         }
     }
     return true;
 }
 function it_applies_percentage_discount_as_promotion_adjustment($adjustmentRepository, $originator, OrderInterface $order, AdjustmentInterface $adjustment, PromotionInterface $promotion)
 {
     $order->getPromotionSubjectTotal()->willReturn(10000);
     $adjustmentRepository->createNew()->willReturn($adjustment);
     $promotion->getDescription()->willReturn('promotion description');
     $adjustment->setAmount(-2500)->shouldBeCalled();
     $adjustment->setLabel(AdjustmentInterface::PROMOTION_ADJUSTMENT)->shouldBeCalled();
     $adjustment->setDescription('promotion description')->shouldBeCalled();
     $originator->setOrigin($adjustment, $promotion)->shouldBeCalled();
     $order->addAdjustment($adjustment)->shouldBeCalled();
     $configuration = array('percentage' => 0.25);
     $this->execute($order, $configuration, $promotion);
 }
Exemplo n.º 19
0
 function it_does_not_applies_bigger_discount_than_promotion_subject_total($adjustmentFactory, $originator, OrderInterface $order, AdjustmentInterface $adjustment, PromotionInterface $promotion)
 {
     $adjustmentFactory->createNew()->willReturn($adjustment);
     $promotion->getDescription()->willReturn('promotion description');
     $order->getPromotionSubjectTotal()->willReturn(1000);
     $adjustment->setAmount(-1000)->shouldBeCalled();
     $adjustment->setType(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT)->shouldBeCalled();
     $adjustment->setLabel('promotion description')->shouldBeCalled();
     $originator->setOrigin($adjustment, $promotion)->shouldBeCalled();
     $order->addAdjustment($adjustment)->shouldBeCalled();
     $configuration = ['amount' => 1500];
     $this->execute($order, $configuration, $promotion);
 }
 /**
  * {@inheritdoc}
  */
 public function isEligible(PromotionSubjectInterface $promotionSubject, PromotionInterface $promotion)
 {
     $now = new \DateTime();
     $startsAt = $promotion->getStartsAt();
     if (null !== $startsAt && $now < $startsAt) {
         return false;
     }
     $endsAt = $promotion->getEndsAt();
     if (null !== $endsAt && $now > $endsAt) {
         return false;
     }
     return true;
 }
 function it_reverts_order_units_order_promotion_adjustments(AdjustmentInterface $firstAdjustment, AdjustmentInterface $secondAdjustment, OrderInterface $order, OrderItemInterface $item, OrderItemUnitInterface $unit, PromotionInterface $promotion)
 {
     $order->countItems()->willReturn(1);
     $order->getItems()->willReturn([$item]);
     $item->getUnits()->willReturn([$unit]);
     $unit->getAdjustments(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT)->willReturn([$firstAdjustment, $secondAdjustment]);
     $promotion->getCode()->willReturn('PROMOTION');
     $firstAdjustment->getOriginCode()->willReturn('PROMOTION');
     $secondAdjustment->getOriginCode()->willReturn('OTHER_PROMOTION');
     $unit->removeAdjustment($firstAdjustment)->shouldBeCalled();
     $unit->removeAdjustment($secondAdjustment)->shouldNotBeCalled();
     $this->revert($order, [], $promotion);
 }
Exemplo n.º 22
0
 function it_applies_only_exclusive_promotion(PreQualifiedPromotionsProviderInterface $preQualifiedPromotionsProvider, PromotionEligibilityCheckerInterface $promotionEligibilityChecker, PromotionApplicatorInterface $promotionApplicator, PromotionSubjectInterface $subject, PromotionInterface $promotion, PromotionInterface $exclusivePromotion)
 {
     $subject->getPromotions()->willReturn([]);
     $preQualifiedPromotionsProvider->getPromotions($subject)->willReturn([$promotion, $exclusivePromotion]);
     $exclusivePromotion->isExclusive()->willReturn(true);
     $promotionEligibilityChecker->isEligible($subject, $promotion)->willReturn(true);
     $promotionEligibilityChecker->isEligible($subject, $exclusivePromotion)->willReturn(true);
     $promotionApplicator->apply($subject, $exclusivePromotion)->shouldBeCalled();
     $promotionApplicator->apply($subject, $promotion)->shouldNotBeCalled();
     $promotionApplicator->revert($subject, $promotion)->shouldNotBeCalled();
     $promotionApplicator->revert($subject, $exclusivePromotion)->shouldNotBeCalled();
     $this->process($subject);
 }
Exemplo n.º 23
0
 function it_should_apply_only_exclusive_promotion($repository, $checker, $applicator, PromotionSubjectInterface $subject, PromotionInterface $promotion, PromotionInterface $exlusivePromotion)
 {
     $subject->getPromotions()->shouldBeCalled()->willReturn(array());
     $repository->findActive()->shouldBeCalled()->willReturn(array($promotion, $exlusivePromotion));
     $exlusivePromotion->isExclusive()->shouldBeCalled()->willReturn(true);
     $checker->isEligible($subject, $promotion)->shouldBeCalled()->willReturn(true);
     $checker->isEligible($subject, $exlusivePromotion)->shouldBeCalled()->willReturn(true);
     $applicator->apply($subject, $exlusivePromotion)->shouldBeCalled();
     $applicator->apply($subject, $promotion)->shouldNotBeCalled();
     $applicator->revert($subject, $promotion)->shouldNotBeCalled();
     $applicator->revert($subject, $exlusivePromotion)->shouldNotBeCalled();
     $this->process($subject);
 }
 /**
  * {@inheritdoc}
  */
 public function isEligible(PromotionSubjectInterface $promotionSubject, PromotionInterface $promotion)
 {
     if (!$promotion->isCouponBased()) {
         throw new UnsupportedPromotionException('Only coupon based promotions can be evaluated by this checker.');
     }
     if (!$promotionSubject instanceof CouponAwarePromotionSubjectInterface) {
         return false;
     }
     if (null === $promotionSubject->getPromotionCoupon()) {
         return false;
     }
     return $promotion === $promotionSubject->getPromotionCoupon()->getPromotion();
 }
 function it_applies_percentage_discount_as_promotion_adjustment(FactoryInterface $adjustmentFactory, $originator, OrderInterface $order, AdjustmentInterface $adjustment, PromotionInterface $promotion)
 {
     $order->getPromotionSubjectTotal()->willReturn(10000);
     $adjustmentFactory->createNew()->willReturn($adjustment);
     $promotion->getName()->willReturn('Test promotion');
     $adjustment->setAmount(-2500)->shouldBeCalled();
     $adjustment->setType(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT)->shouldBeCalled();
     $adjustment->setLabel('Test promotion')->shouldBeCalled();
     $originator->setOrigin($adjustment, $promotion)->shouldBeCalled();
     $order->addAdjustment($adjustment)->shouldBeCalled();
     $configuration = ['percentage' => 0.25];
     $this->execute($order, $configuration, $promotion);
 }
Exemplo n.º 26
0
 function it_should_apply_only_exclusive_promotion($activePromotionsProvider, $checker, $applicator, PromotionSubjectInterface $subject, PromotionInterface $promotion, PromotionInterface $exclusivePromotion)
 {
     $subject->getPromotions()->shouldBeCalled()->willReturn([]);
     $activePromotionsProvider->getPromotions($subject)->willReturn([$promotion, $exclusivePromotion]);
     $exclusivePromotion->isExclusive()->shouldBeCalled()->willReturn(true);
     $checker->isEligible($subject, $promotion)->shouldBeCalled()->willReturn(true);
     $checker->isEligible($subject, $exclusivePromotion)->shouldBeCalled()->willReturn(true);
     $applicator->apply($subject, $exclusivePromotion)->shouldBeCalled();
     $applicator->apply($subject, $promotion)->shouldNotBeCalled();
     $applicator->revert($subject, $promotion)->shouldNotBeCalled();
     $applicator->revert($subject, $exclusivePromotion)->shouldNotBeCalled();
     $this->process($subject);
 }
 function it_does_not_check_more_rules_if_one_has_returned_false(RuleCheckerInterface $cartQuantityRuleChecker, RuleCheckerInterface $itemTotalRuleChecker, RuleInterface $cartQuantityRule, RuleInterface $itemTotalRule, PromotionInterface $promotion, PromotionSubjectInterface $subject, ServiceRegistryInterface $rulesRegistry)
 {
     $promotion->hasRules()->willReturn(true);
     $promotion->getRules()->willReturn([$cartQuantityRule, $itemTotalRule]);
     $cartQuantityRule->getType()->willReturn(RuleInterface::TYPE_CART_QUANTITY);
     $cartQuantityRule->getConfiguration()->willReturn([]);
     $itemTotalRule->getType()->willReturn(RuleInterface::TYPE_ITEM_TOTAL);
     $itemTotalRule->getConfiguration()->willReturn([]);
     $rulesRegistry->get(RuleInterface::TYPE_CART_QUANTITY)->willReturn($cartQuantityRuleChecker);
     $rulesRegistry->get(RuleInterface::TYPE_ITEM_TOTAL)->willReturn($itemTotalRuleChecker);
     $cartQuantityRuleChecker->isEligible($subject, [])->willReturn(false);
     $itemTotalRuleChecker->isEligible($subject, [])->shouldNotBeCalled();
     $this->isEligible($subject, $promotion)->shouldReturn(false);
 }
 function it_does_not_check_more_rules_if_one_has_returned_false(ServiceRegistryInterface $rulesRegistry, RuleCheckerInterface $firstRuleChecker, RuleCheckerInterface $secondRuleChecker, RuleInterface $firstRule, RuleInterface $secondRule, PromotionInterface $promotion, PromotionSubjectInterface $subject)
 {
     $promotion->hasRules()->willReturn(true);
     $promotion->getRules()->willReturn([$firstRule, $secondRule]);
     $firstRule->getType()->willReturn('first_rule');
     $firstRule->getConfiguration()->willReturn([]);
     $secondRule->getType()->willReturn('second_rule');
     $secondRule->getConfiguration()->willReturn([]);
     $rulesRegistry->get('first_rule')->willReturn($firstRuleChecker);
     $rulesRegistry->get('second_rule')->willReturn($secondRuleChecker);
     $firstRuleChecker->isEligible($subject, [])->willReturn(false);
     $secondRuleChecker->isEligible($subject, [])->shouldNotBeCalled();
     $this->isEligible($subject, $promotion)->shouldReturn(false);
 }
Exemplo n.º 29
0
 public function revert(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion)
 {
     if (!$subject instanceof OrderInterface) {
         return;
     }
     if (!$promotion instanceof OriginAwareInterface) {
         return;
     }
     /** @var AdjustmentInterface $adjustment */
     foreach ($subject->getAdjustments() as $adjustment) {
         if ($adjustment->getOriginId() === $promotion->getOriginId() && $adjustment->getOriginType() === $promotion->getOriginType() && $adjustment->getType() === $this->getAdjustmentType()) {
             $subject->removeAdjustment($adjustment);
             break;
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function isEligible(PromotionSubjectInterface $promotionSubject, PromotionInterface $promotion)
 {
     if (!$promotion->isCouponBased()) {
         return true;
     }
     if (!$promotionSubject instanceof PromotionCouponAwarePromotionSubjectInterface) {
         return false;
     }
     $promotionCoupon = $promotionSubject->getPromotionCoupon();
     if (null === $promotionCoupon) {
         return false;
     }
     if ($promotion !== $promotionCoupon->getPromotion()) {
         return false;
     }
     return $this->promotionCouponEligibilityChecker->isEligible($promotionSubject, $promotionCoupon);
 }