コード例 #1
0
 function it_does_nothing_if_adjustment_amount_would_be_0(IntegerDistributorInterface $integerDistributor, OrderInterface $order, PromotionInterface $promotion)
 {
     $order->countItems()->willReturn(0);
     $order->getPromotionSubjectTotal()->willReturn(0);
     $integerDistributor->distribute(Argument::any())->shouldNotBeCalled();
     $this->execute($order, ['percentage' => 0.1], $promotion);
 }
コード例 #2
0
 /**
  * @param OrderItemInterface $item
  * @param PromotionInterface $promotion
  * @param int $itemPromotionAmount
  */
 private function applyAdjustmentsOnItemUnits(OrderItemInterface $item, PromotionInterface $promotion, $itemPromotionAmount)
 {
     $splitPromotionAmount = $this->distributor->distribute($itemPromotionAmount, $item->getQuantity());
     $i = 0;
     foreach ($item->getUnits() as $unit) {
         $promotionAmount = $splitPromotionAmount[$i++];
         if (0 === $promotionAmount) {
             continue;
         }
         $this->addAdjustment($promotion, $unit, $promotionAmount);
     }
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function execute(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion)
 {
     if (!$this->isSubjectValid($subject)) {
         return;
     }
     $this->isConfigurationValid($configuration);
     $promotionAmount = $this->calculateAdjustmentAmount($subject->getPromotionSubjectTotal(), $configuration['percentage']);
     if (0 === $promotionAmount) {
         return;
     }
     $splitPromotion = $this->distributor->distribute($promotionAmount, $subject->countItems());
     $this->unitsPromotionAdjustmentsApplicator->apply($subject, $promotion, $splitPromotion);
 }
コード例 #4
0
 /**
  * {@inheritdoc}
  */
 public function execute(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion)
 {
     if (!$subject instanceof OrderInterface) {
         throw new UnexpectedTypeException($subject, OrderInterface::class);
     }
     $filteredItems = $this->priceRangeFilter->filter($subject->getItems()->toArray(), $configuration);
     $filteredItems = $this->taxonFilter->filter($filteredItems, $configuration);
     foreach ($filteredItems as $item) {
         $promotionAmount = (int) round($item->getTotal() * $configuration['percentage']);
         $distributedAmounts = $this->distributor->distribute($promotionAmount, $item->getQuantity());
         $this->setUnitsAdjustments($item, $distributedAmounts, $promotion);
     }
 }
コード例 #5
0
 /**
  * {@inheritdoc}
  */
 public function apply(OrderInterface $order, ZoneInterface $zone)
 {
     foreach ($order->getItems() as $item) {
         $quantity = $item->getQuantity();
         if (0 === $quantity) {
             throw new \InvalidArgumentException('Cannot apply tax to order item with 0 quantity.');
         }
         $taxRate = $this->taxRateResolver->resolve($item->getVariant(), ['zone' => $zone]);
         if (null === $taxRate) {
             continue;
         }
         $totalTaxAmount = $this->calculator->calculate($item->getTotal(), $taxRate);
         $splitTaxes = $this->distributor->distribute($totalTaxAmount, $quantity);
         $i = 0;
         foreach ($item->getUnits() as $unit) {
             if (0 === $splitTaxes[$i]) {
                 continue;
             }
             $this->addAdjustment($unit, $splitTaxes[$i], $taxRate->getLabel(), $taxRate->isIncludedInPrice());
             $i++;
         }
     }
 }
コード例 #6
0
 /**
  * {@inheritdoc}
  */
 public function apply(OrderInterface $order, ZoneInterface $zone)
 {
     foreach ($order->getItems() as $item) {
         $quantity = $item->getQuantity();
         if (0 === $quantity) {
             continue;
         }
         $taxRate = $this->taxRateResolver->resolve($item->getVariant(), ['zone' => $zone]);
         if (null === $taxRate) {
             continue;
         }
         $totalTaxAmount = $this->calculator->calculate($item->getTotal(), $taxRate);
         $splitTaxes = $this->distributor->distribute($totalTaxAmount, $quantity);
         $units = $item->getUnits();
         $units->first();
         foreach ($splitTaxes as $key => $tax) {
             if (0 === $tax) {
                 continue;
             }
             $unit = $this->getNextUnit($units);
             $this->addAdjustment($unit, $tax, $taxRate->getLabel(), $taxRate->isIncludedInPrice());
         }
     }
 }
コード例 #7
0
 function it_does_not_distribute_0_amount_to_unit_even_if_its_middle_element(AdjustmentFactoryInterface $adjustmentFactory, AdjustmentInterface $adjustment, IntegerDistributorInterface $distributor, OrderInterface $order, OrderItemInterface $coltItem, OrderItemUnitInterface $firstColtUnit, OrderItemUnitInterface $secondColtUnit, PromotionInterface $promotion)
 {
     $order->countItems()->willReturn(1);
     $order->getItems()->willReturn(new ArrayCollection([$coltItem->getWrappedObject()]));
     $coltItem->getQuantity()->willReturn(2);
     $distributor->distribute(1, 2)->willReturn([1, 0]);
     $coltItem->getUnits()->willReturn(new ArrayCollection([$firstColtUnit->getWrappedObject(), $secondColtUnit->getWrappedObject()]));
     $promotion->getName()->willReturn('Winter guns promotion!');
     $promotion->getCode()->willReturn('WINTER_GUNS_PROMOTION');
     $adjustmentFactory->createWithData(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT, 'Winter guns promotion!', 1)->willReturn($adjustment);
     $adjustment->setOriginCode('WINTER_GUNS_PROMOTION')->shouldBeCalled();
     $firstColtUnit->addAdjustment($adjustment)->shouldBeCalled();
     $secondColtUnit->addAdjustment(Argument::any())->shouldNotBeCalled();
     $this->apply($order, $promotion, [1]);
 }
コード例 #8
0
 function it_does_not_apply_taxes_with_amount_0(CalculatorInterface $calculator, AdjustmentFactoryInterface $adjustmentsFactory, IntegerDistributorInterface $distributor, TaxRateResolverInterface $taxRateResolver, Collection $items, Collection $units, OrderInterface $order, OrderItemInterface $orderItem, OrderItemUnitInterface $unit1, OrderItemUnitInterface $unit2, ProductVariantInterface $productVariant, TaxRateInterface $taxRate, ZoneInterface $zone)
 {
     $order->getItems()->willReturn($items);
     $items->count()->willReturn(1);
     $items->getIterator()->willReturn(new \ArrayIterator([$orderItem->getWrappedObject()]));
     $orderItem->getQuantity()->willReturn(2);
     $orderItem->getVariant()->willReturn($productVariant);
     $taxRateResolver->resolve($productVariant, ['zone' => $zone])->willReturn($taxRate);
     $orderItem->getTotal()->willReturn(1000);
     $calculator->calculate(1000, $taxRate)->willReturn(0);
     $taxRate->getLabel()->willReturn('Simple tax (0%)');
     $taxRate->isIncludedInPrice()->willReturn(false);
     $orderItem->getUnits()->willReturn($units);
     $units->getIterator()->willReturn(new \ArrayIterator([$unit1->getWrappedObject(), $unit2->getWrappedObject()]));
     $distributor->distribute(0, 2)->willReturn([0, 0]);
     $adjustmentsFactory->createWithData(AdjustmentInterface::TAX_ADJUSTMENT, 'Simple tax (0%)', 0, false)->shouldNotBeCalled();
     $this->apply($order, $zone);
 }