/**
  * @param OrderItemInterface $item
  * @param array $distributedAmounts
  * @param PromotionInterface $promotion
  */
 private function setUnitsAdjustments(OrderItemInterface $item, array $distributedAmounts, PromotionInterface $promotion)
 {
     $i = 0;
     foreach ($item->getUnits() as $unit) {
         if (0 === $distributedAmounts[$i]) {
             break;
         }
         $this->addAdjustmentToUnit($unit, $distributedAmounts[$i], $promotion);
         $i++;
     }
 }
 /**
  * @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);
     }
 }
 function it_reverts_order_units_order_promotion_adjustments(AdjustmentInterface $firstAdjustment, AdjustmentInterface $secondAdjustment, OrderInterface $order, OrderItemInterface $item, OrderItemUnitInterface $unit, OriginatorInterface $originator, PromotionInterface $otherPromotion, PromotionInterface $promotion)
 {
     $order->countItems()->willReturn(1);
     $order->getItems()->willReturn(new \ArrayIterator([$item->getWrappedObject()]));
     $item->getUnits()->willReturn(new \ArrayIterator([$unit->getWrappedObject()]));
     $unit->getAdjustments(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT)->willReturn(new \ArrayIterator([$firstAdjustment->getWrappedObject(), $secondAdjustment->getWrappedObject()]));
     $originator->getOrigin($firstAdjustment)->willReturn($promotion);
     $originator->getOrigin($secondAdjustment)->willReturn($otherPromotion);
     $unit->removeAdjustment($firstAdjustment)->shouldBeCalled();
     $unit->removeAdjustment($secondAdjustment)->shouldNotBeCalled();
     $this->revert($order, [], $promotion);
 }
 function it_removes_units_if_target_quantity_is_greater_than_current(Collection $orderItemUnits, \Iterator $iterator, OrderItemInterface $orderItem, OrderItemUnitInterface $unit1, OrderItemUnitInterface $unit2, OrderItemUnitInterface $unit3, OrderItemUnitInterface $unit4)
 {
     $orderItem->getQuantity()->willReturn(4);
     $orderItem->getUnits()->willReturn($orderItemUnits);
     $orderItemUnits->count()->willReturn(4);
     $orderItemUnits->getIterator()->willReturn($iterator);
     $iterator->rewind()->shouldBeCalled();
     $iterator->valid()->willReturn(true, true, true, true, false);
     $iterator->current()->willReturn($unit1, $unit2, $unit3, $unit4);
     $iterator->next()->shouldBeCalled();
     $orderItem->removeUnit($unit1)->shouldBeCalled();
     $this->modify($orderItem, 3);
 }
 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);
 }
 function it_reverts_proper_promotion_adjustment_from_all_units($originator, AdjustmentInterface $promotionAdjustment1, AdjustmentInterface $promotionAdjustment2, Collection $items, Collection $units, Collection $adjustments, OrderInterface $order, OrderItemInterface $orderItem, OrderItemUnitInterface $unit, PromotionInterface $promotion, PromotionInterface $someOtherPromotion)
 {
     $order->getItems()->willReturn($items);
     $items->getIterator()->willReturn(new \ArrayIterator([$orderItem->getWrappedObject()]));
     $orderItem->getUnits()->willReturn($units);
     $units->getIterator()->willReturn(new \ArrayIterator([$unit->getWrappedObject()]));
     $unit->getAdjustments(AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT)->willReturn($adjustments);
     $adjustments->getIterator()->willReturn(new \ArrayIterator([$promotionAdjustment1->getWrappedObject(), $promotionAdjustment2->getWrappedObject()]));
     $originator->getOrigin($promotionAdjustment1)->willReturn($promotion);
     $unit->removeAdjustment($promotionAdjustment1)->shouldBeCalled();
     $originator->getOrigin($promotionAdjustment2)->willReturn($someOtherPromotion);
     $unit->removeAdjustment($promotionAdjustment2)->shouldNotBeCalled();
     $this->revert($order, ['percentage' => 0.2], $promotion);
 }
 function it_holds_the_variant_stock_via_inventory_operator($inventoryOperator, $stateMachineFactory, OrderInterface $order, OrderItemInterface $item, ProductVariantInterface $variant, OrderItemUnitInterface $unit1, OrderItemUnitInterface $unit2, StateMachineInterface $sm1, StateMachineInterface $sm2)
 {
     $order->getItems()->willReturn([$item]);
     $item->getVariant()->willReturn($variant);
     $item->getQuantity()->willReturn(2);
     $item->getUnits()->willReturn(new ArrayCollection([$unit1, $unit2]));
     $stateMachineFactory->get($unit1, InventoryUnitTransitions::GRAPH)->willReturn($sm1);
     $sm1->can(InventoryUnitTransitions::SYLIUS_HOLD)->willReturn(false);
     $sm1->apply(InventoryUnitTransitions::SYLIUS_HOLD)->shouldNotBeCalled();
     $stateMachineFactory->get($unit2, InventoryUnitTransitions::GRAPH)->willReturn($sm2);
     $sm1->can(InventoryUnitTransitions::SYLIUS_HOLD)->willReturn(true);
     $sm1->apply(InventoryUnitTransitions::SYLIUS_HOLD)->shouldBeCalled();
     $inventoryOperator->hold($variant, 1)->shouldBeCalled();
     $this->holdInventory($order);
 }
Example #8
0
 /**
  * {@inheritdoc}
  */
 public function processInventoryUnits(OrderItemInterface $item)
 {
     $nbUnits = $item->getUnits()->count();
     if ($item->getQuantity() > $nbUnits) {
         $this->createInventoryUnits($item, $item->getQuantity() - $nbUnits);
     } elseif ($item->getQuantity() < $nbUnits) {
         foreach ($item->getUnits()->slice(0, $nbUnits - $item->getQuantity()) as $unit) {
             $item->removeUnit($unit);
         }
     }
     foreach ($item->getUnits() as $unit) {
         if ($unit->getStockable() !== $item->getVariant()) {
             $unit->setStockable($item->getVariant());
         }
     }
 }
 function it_decreases_the_variant_stock_via_inventory_operator($inventoryOperator, $stateMachineFactory, OrderInterface $order, OrderItemInterface $item, ProductVariantInterface $variant, OrderItemUnitInterface $unit1, OrderItemUnitInterface $unit2, StateMachineInterface $sm1, StateMachineInterface $sm2)
 {
     $order->getItems()->willReturn([$item]);
     $item->getVariant()->willReturn($variant);
     $item->getQuantity()->willReturn(2);
     $item->getUnits()->shouldBeCalled()->willReturn([$unit1, $unit2]);
     $stateMachineFactory->get($unit1, InventoryUnitTransitions::GRAPH)->willReturn($sm1);
     $sm1->can(InventoryUnitTransitions::SYLIUS_SELL)->willReturn(true);
     $sm1->can(InventoryUnitTransitions::SYLIUS_RELEASE)->willReturn(true);
     $sm1->apply(InventoryUnitTransitions::SYLIUS_SELL)->shouldBeCalled();
     $stateMachineFactory->get($unit2, InventoryUnitTransitions::GRAPH)->willReturn($sm2);
     $sm2->can(InventoryUnitTransitions::SYLIUS_SELL)->willReturn(true);
     $sm2->can(InventoryUnitTransitions::SYLIUS_RELEASE)->willReturn(false);
     $sm2->apply(InventoryUnitTransitions::SYLIUS_SELL)->shouldBeCalled();
     $inventoryOperator->decrease([$unit1, $unit2])->shouldBeCalled();
     $inventoryOperator->release($variant, 1)->shouldBeCalled();
     $this->updateInventory($order);
 }
Example #10
0
 function it_not_a_backorder_if_contains_no_backordered_units(InventoryUnitInterface $unit1, InventoryUnitInterface $unit2, OrderItemInterface $item)
 {
     $unit1->getInventoryState()->willReturn(InventoryUnitInterface::STATE_SOLD);
     $unit2->getInventoryState()->willReturn(InventoryUnitInterface::STATE_SOLD);
     $item->getUnits()->willReturn(array($unit1, $unit2));
     $item->setOrder($this)->shouldBeCalled();
     $this->addItem($item);
     $this->shouldNotBeBackorder();
 }
 /**
  * @param OrderItemInterface $item
  * @param int $promotionAmount
  * @param PromotionInterface $promotion
  */
 private function setUnitsAdjustments(OrderItemInterface $item, $promotionAmount, PromotionInterface $promotion)
 {
     foreach ($item->getUnits() as $unit) {
         $this->addAdjustmentToUnit($unit, $promotionAmount, $promotion);
     }
 }
 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]);
 }
 function it_revert_proper_promotion_adjustment_from_all_units(AdjustmentInterface $promotionAdjustment1, AdjustmentInterface $promotionAdjustment2, Collection $items, Collection $units, Collection $adjustments, OrderInterface $order, OrderItemInterface $orderItem, OrderItemUnitInterface $unit, PromotionInterface $promotion)
 {
     $order->getItems()->willReturn($items);
     $items->getIterator()->willReturn(new \ArrayIterator([$orderItem->getWrappedObject()]));
     $orderItem->getUnits()->willReturn($units);
     $units->getIterator()->willReturn(new \ArrayIterator([$unit->getWrappedObject()]));
     $unit->getAdjustments(AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT)->willReturn($adjustments);
     $adjustments->getIterator()->willReturn(new \ArrayIterator([$promotionAdjustment1->getWrappedObject(), $promotionAdjustment2->getWrappedObject()]));
     $promotion->getCode()->willReturn('PROMOTION');
     $promotionAdjustment1->getOriginCode()->willReturn('PROMOTION');
     $unit->removeAdjustment($promotionAdjustment1)->shouldBeCalled();
     $promotionAdjustment2->getOriginCode()->willReturn('OTHER_PROMOTION');
     $unit->removeAdjustment($promotionAdjustment2)->shouldNotBeCalled();
     $this->revert($order, ['amount' => 1000], $promotion);
 }
 function it_reverts_a_proper_promotion_adjustment_from_all_units(AdjustmentInterface $promotionAdjustment1, AdjustmentInterface $promotionAdjustment2, ChannelInterface $channel, OrderInterface $order, OrderItemInterface $orderItem, OrderItemUnitInterface $unit, PromotionInterface $promotion)
 {
     $order->getChannel()->willReturn($channel);
     $channel->getCode()->willReturn('WEB_US');
     $order->getItems()->willReturn(new ArrayCollection([$orderItem->getWrappedObject()]));
     $orderItem->getUnits()->willReturn(new ArrayCollection([$unit->getWrappedObject()]));
     $unit->getAdjustments(AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT)->willReturn(new ArrayCollection([$promotionAdjustment1->getWrappedObject(), $promotionAdjustment2->getWrappedObject()]));
     $promotion->getCode()->willReturn('PROMOTION');
     $promotionAdjustment1->getOriginCode()->willReturn('PROMOTION');
     $unit->removeAdjustment($promotionAdjustment1)->shouldBeCalled();
     $promotionAdjustment2->getOriginCode()->willReturn('OTHER_PROMOTION');
     $unit->removeAdjustment($promotionAdjustment2)->shouldNotBeCalled();
     $this->revert($order, ['WEB_US' => ['amount' => 1000]], $promotion);
 }
Example #15
0
 /**
  * @param OrderItemInterface $item
  * @param PromotionInterface $promotion
  */
 protected function removeUnitsAdjustment(OrderItemInterface $item, PromotionInterface $promotion)
 {
     foreach ($item->getUnits() as $unit) {
         $this->removeUnitOrderItemAdjustments($unit, $promotion);
     }
 }
 function it_does_not_apply_taxes_with_amount_0(CalculatorInterface $calculator, AdjustmentFactoryInterface $adjustmentsFactory, 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(2);
     $items->getIterator()->willReturn(new \ArrayIterator([$orderItem->getWrappedObject()]));
     $orderItem->getQuantity()->willReturn(2);
     $orderItem->getVariant()->willReturn($productVariant);
     $taxRateResolver->resolve($productVariant, ['zone' => $zone])->willReturn($taxRate);
     $orderItem->getUnits()->willReturn($units);
     $units->getIterator()->willReturn(new \ArrayIterator([$unit1->getWrappedObject(), $unit2->getWrappedObject()]));
     $unit1->getTotal()->willReturn(1000);
     $calculator->calculate(1000, $taxRate)->willReturn(0);
     $unit2->getTotal()->willReturn(900);
     $calculator->calculate(900, $taxRate)->willReturn(0);
     $adjustmentsFactory->createWithData(AdjustmentInterface::TAX_ADJUSTMENT, Argument::cetera())->shouldNotBeCalled();
     $unit1->addAdjustment(Argument::any())->shouldNotBeCalled();
     $unit2->addAdjustment(Argument::any())->shouldNotBeCalled();
     $this->apply($order, $zone);
 }
 function it_does_not_apply_taxes_with_amount_0($adjustmentsFactory, $calculator, $distributor, $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);
 }