function it_applies_calculated_shipping_charge_for_each_shipment_associated_with_the_order(
        $adjustmentRepository,
        $calculator,
        AdjustmentInterface $adjustment,
        OrderInterface $order,
        ShipmentInterface $shipment,
        ShippingMethodInterface $shippingMethod
    ) {
        $adjustmentRepository->createNew()->willReturn($adjustment);
        $order->getShipments()->willReturn(array($shipment));

        $calculator->calculate($shipment)->willReturn(450);

        $shipment->getMethod()->willReturn($shippingMethod);
        $shippingMethod->getName()->willReturn('FedEx');

        $adjustment->setAmount(450)->shouldBeCalled();
        $adjustment->setType(AdjustmentInterface::SHIPPING_ADJUSTMENT)->shouldBeCalled();
        $adjustment->setDescription('FedEx')->shouldBeCalled();

        $order->removeAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->shouldBeCalled();
        $order->addAdjustment($adjustment)->shouldBeCalled();

        $order->calculateTotal()->shouldBeCalled();

        $this->applyShippingCharges($order);
    }
Esempio n. 2
0
 /**
  * Update order's shipping state.
  *
  * @param OrderInterface $order
  * @param string         $transition
  */
 public function updateOrderShipmentStates(OrderInterface $order, $transition = ShipmentTransitions::SYLIUS_PREPARE)
 {
     if ($order->isBackorder()) {
         $transition = ShipmentTransitions::SYLIUS_BACKORDER;
     }
     $this->processor->updateShipmentStates($order->getShipments(), $transition);
 }
Esempio n. 3
0
 /**
  * @param OrderInterface $order
  */
 public function updateOrderShippingState(OrderInterface $order)
 {
     foreach ($order->getShipments() as $shipment) {
         if ($this->factory->get($shipment, ShipmentTransitions::GRAPH)->can(ShipmentTransitions::SYLIUS_SHIP)) {
             return;
         }
     }
     $this->factory->get($order, OrderTransitions::GRAPH)->apply(OrderTransitions::SYLIUS_SHIP, true);
 }
 function it_does_not_update_order_state_if_one_shipment_is_not_shipped($factory, ShipmentInterface $shipment, OrderInterface $order, StateMachineInterface $sm1, StateMachineInterface $sm2)
 {
     $order->getShipments()->willReturn(array($shipment));
     $factory->get($order, OrderTransitions::GRAPH)->willReturn($sm1);
     $factory->get($shipment, ShipmentTransitions::GRAPH)->willReturn($sm2);
     $sm2->can(ShipmentTransitions::SYLIUS_SHIP)->shouldBeCalled()->willReturn(false);
     $sm1->apply(OrderTransitions::SYLIUS_SHIP, true)->shouldBeCalled();
     $this->updateOrderShippingState($order);
 }
Esempio n. 5
0
 function it_marks_order_as_returned_if_all_shipments_were_returned(OrderInterface $order, ShipmentInterface $shipment1, ShipmentInterface $shipment2)
 {
     $order->isBackorder()->shouldBeCalled()->willReturn(false);
     $order->getShipments()->willReturn(array($shipment1, $shipment2));
     $shipment1->getState()->willReturn(ShipmentInterface::STATE_RETURNED);
     $shipment2->getState()->willReturn(ShipmentInterface::STATE_RETURNED);
     $order->setShippingState(OrderShippingStates::RETURNED)->shouldBeCalled();
     $this->resolveShippingState($order);
 }
Esempio n. 6
0
 /**
  * Update order shipping state.
  *
  * @param OrderInterface $order
  */
 public function updateOrderShippingState(OrderInterface $order)
 {
     // Check if all shipments are shipped (= transition "ship" cannot be applied)
     foreach ($order->getShipments() as $shipment) {
         if ($this->factory->get($shipment, ShipmentTransitions::GRAPH)->can(ShipmentTransitions::SYLIUS_SHIP)) {
             return;
         }
     }
     $this->factory->get($order, OrderTransitions::GRAPH)->apply(OrderTransitions::SYLIUS_SHIP, true);
 }
Esempio n. 7
0
 function it_adds_new_inventory_units_to_existing_shipment(OrderInterface $order, ShipmentInterface $shipment, ArrayCollection $shipments, InventoryUnitInterface $inventoryUnit, InventoryUnitInterface $inventoryUnitWithoutShipment)
 {
     $shipments->first()->willReturn($shipment)->shouldBeCalled();
     $inventoryUnit->getShipment()->willReturn($shipment);
     $order->getInventoryUnits()->willReturn(array($inventoryUnit, $inventoryUnitWithoutShipment));
     $order->getShipments()->willReturn($shipments)->shouldBeCalled();
     $shipment->addItem($inventoryUnitWithoutShipment)->shouldBeCalled();
     $shipment->addItem($inventoryUnit)->shouldNotBeCalled();
     $this->createShipment($order);
 }
 function it_does_nothing_if_tax_rate_cannot_be_resolved(CalculatorInterface $calculator, TaxRateResolverInterface $taxRateResolver, OrderInterface $order, ShipmentInterface $shipment, ShippingMethodInterface $shippingMethod, ZoneInterface $zone)
 {
     $order->getShippingTotal()->willReturn(100);
     $order->getShipments()->willReturn(new ArrayCollection([$shipment->getWrappedObject()]));
     $shipment->getMethod()->willReturn($shippingMethod);
     $taxRateResolver->resolve($shippingMethod, ['zone' => $zone])->willReturn(null);
     $calculator->calculate(Argument::any())->shouldNotBeCalled();
     $order->addAdjustment(Argument::any())->shouldNotBeCalled();
     $this->apply($order, $zone);
 }
Esempio n. 9
0
 /**
  * @param OrderInterface $order
  *
  * @return ShipmentInterface
  */
 private function getOrderShipment(OrderInterface $order)
 {
     if ($order->hasShipments()) {
         return $order->getShipments()->first();
     }
     /** @var ShipmentInterface $shipment */
     $shipment = $this->shipmentFactory->createNew();
     $order->addShipment($shipment);
     $shipment->setMethod($this->defaultShippingMethodResolver->getDefaultShippingMethod($shipment));
     return $shipment;
 }
Esempio n. 10
0
 function it_adds_new_item_units_to_existing_shipment(OrderInterface $order, ShipmentInterface $shipment, ArrayCollection $shipments, OrderItemUnitInterface $itemUnit, OrderItemUnitInterface $itemUnitWithoutShipment)
 {
     $shipments->first()->willReturn($shipment)->shouldBeCalled();
     $order->hasShipments()->willReturn(true)->shouldBeCalled();
     $itemUnit->getShipment()->willReturn($shipment);
     $order->getItemUnits()->willReturn([$itemUnit, $itemUnitWithoutShipment]);
     $order->getShipments()->willReturn($shipments)->shouldBeCalled();
     $shipment->addUnit($itemUnitWithoutShipment)->shouldBeCalled();
     $shipment->addUnit($itemUnit)->shouldNotBeCalled();
     $this->createForOrder($order);
 }
 function it_does_not_mark_an_order_if_it_is_already_in_this_shipping_state(FactoryInterface $stateMachineFactory, OrderInterface $order, ShipmentInterface $shipment1, ShipmentInterface $shipment2, StateMachineInterface $orderStateMachine)
 {
     $shipments = new ArrayCollection();
     $shipments->add($shipment1->getWrappedObject());
     $shipments->add($shipment2->getWrappedObject());
     $order->getShipments()->willReturn($shipments);
     $order->getShippingState()->willReturn(OrderShippingStates::STATE_SHIPPED);
     $stateMachineFactory->get($order, OrderShippingTransitions::GRAPH)->willReturn($orderStateMachine);
     $shipment1->getState()->willReturn(ShipmentInterface::STATE_SHIPPED);
     $shipment2->getState()->willReturn(ShipmentInterface::STATE_SHIPPED);
     $orderStateMachine->apply(OrderShippingTransitions::TRANSITION_SHIP)->shouldNotBeCalled();
     $this->resolve($order);
 }
 /**
  * {@inheritdoc}
  */
 public function applyShippingCharges(OrderInterface $order)
 {
     // Remove all shipping adjustments, we recalculate everything from scratch.
     $order->removeAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT);
     foreach ($order->getShipments() as $shipment) {
         $shippingCharge = $this->calculator->calculate($shipment);
         $adjustment = $this->adjustmentFactory->createNew();
         $adjustment->setType(AdjustmentInterface::SHIPPING_ADJUSTMENT);
         $adjustment->setAmount($shippingCharge);
         $adjustment->setDescription($shipment->getMethod()->getName());
         $order->addAdjustment($adjustment);
     }
 }
Esempio n. 13
0
 /**
  * {@inheritdoc}
  */
 public function createShipment(OrderInterface $order)
 {
     $shipment = $order->getShipments()->first();
     if (!$shipment) {
         $shipment = $this->shipmentRepository->createNew();
         $order->addShipment($shipment);
     }
     foreach ($order->getInventoryUnits() as $inventoryUnit) {
         if (null === $inventoryUnit->getShipment()) {
             $shipment->addItem($inventoryUnit);
         }
     }
 }
Esempio n. 14
0
 /**
  * {@inheritdoc}
  */
 public function createForOrder(OrderInterface $order)
 {
     if ($order->hasShipments()) {
         $shipment = $order->getShipments()->first();
     } else {
         $shipment = $this->shipmentFactory->createNew();
         $order->addShipment($shipment);
     }
     foreach ($order->getItemUnits() as $itemUnit) {
         if (null === $itemUnit->getShipment()) {
             $shipment->addUnit($itemUnit);
         }
     }
 }
 function it_applies_calculated_shipping_charge_for_each_shipment_associated_with_the_order(FactoryInterface $adjustmentFactory, DelegatingCalculatorInterface $calculator, AdjustmentInterface $adjustment, OrderInterface $order, ShipmentInterface $shipment, ShippingMethodInterface $shippingMethod)
 {
     $adjustmentFactory->createNew()->willReturn($adjustment);
     $order->getShipments()->willReturn([$shipment]);
     $calculator->calculate($shipment)->willReturn(450);
     $shipment->getMethod()->willReturn($shippingMethod);
     $shippingMethod->getName()->willReturn('FedEx');
     $adjustment->setAmount(450)->shouldBeCalled();
     $adjustment->setType(AdjustmentInterface::SHIPPING_ADJUSTMENT)->shouldBeCalled();
     $adjustment->setLabel('FedEx')->shouldBeCalled();
     $order->removeAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->shouldBeCalled();
     $order->addAdjustment($adjustment)->shouldBeCalled();
     $this->process($order);
 }
Esempio n. 16
0
 protected function getShippingState(OrderInterface $order)
 {
     $states = [];
     foreach ($order->getShipments() as $shipment) {
         $states[] = $shipment->getState();
     }
     $states = array_unique($states);
     $acceptableStates = [ShipmentInterface::STATE_CHECKOUT => OrderShippingStates::CHECKOUT, ShipmentInterface::STATE_ONHOLD => OrderShippingStates::ONHOLD, ShipmentInterface::STATE_READY => OrderShippingStates::READY, ShipmentInterface::STATE_SHIPPED => OrderShippingStates::SHIPPED, ShipmentInterface::STATE_RETURNED => OrderShippingStates::RETURNED, ShipmentInterface::STATE_CANCELLED => OrderShippingStates::CANCELLED];
     foreach ($acceptableStates as $shipmentState => $orderState) {
         if ([$shipmentState] == $states) {
             return $orderState;
         }
     }
     return OrderShippingStates::PARTIALLY_SHIPPED;
 }
Esempio n. 17
0
 /**
  * {@inheritdoc}
  */
 public function process(OrderInterface $order)
 {
     // Remove all shipping adjustments, we recalculate everything from scratch.
     $order->removeAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT);
     foreach ($order->getShipments() as $shipment) {
         try {
             $shippingCharge = $this->shippingChargesCalculator->calculate($shipment);
             $adjustment = $this->adjustmentFactory->createNew();
             $adjustment->setType(AdjustmentInterface::SHIPPING_ADJUSTMENT);
             $adjustment->setAmount($shippingCharge);
             $adjustment->setLabel($shipment->getMethod()->getName());
             $order->addAdjustment($adjustment);
         } catch (UndefinedShippingMethodException $exception) {
         }
     }
 }
Esempio n. 18
0
 /**
  * @param OrderInterface $order
  */
 private function selectShipping(OrderInterface $order)
 {
     $shippingMethod = $this->faker->randomElement($this->shippingMethodRepository->findEnabledForChannel($order->getChannel()));
     Assert::notNull($shippingMethod, 'Shipping method should not be null.');
     foreach ($order->getShipments() as $shipment) {
         $shipment->setMethod($shippingMethod);
     }
     $this->applyCheckoutStateTransition($order, OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);
 }
Esempio n. 19
0
 /**
  * @param OrderInterface $order
  * @param string $transition
  */
 private function applyShipmentTransitionOnOrder(OrderInterface $order, $transition)
 {
     $shipment = $order->getShipments()->first();
     $this->stateMachineFactory->get($shipment, ShipmentTransitions::GRAPH)->apply($transition);
 }
Esempio n. 20
0
 /**
  * @param OrderInterface $order
  * @param ShippingMethodInterface $shippingMethod
  * @param PaymentMethodInterface $paymentMethod
  */
 private function proceedSelectingShippingAndPaymentMethod(OrderInterface $order, ShippingMethodInterface $shippingMethod, PaymentMethodInterface $paymentMethod)
 {
     foreach ($order->getShipments() as $shipment) {
         $shipment->setMethod($shippingMethod);
     }
     $this->applyTransitionOnOrderCheckout($order, OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);
     $payment = $order->getLastPayment(PaymentInterface::STATE_CART);
     $payment->setMethod($paymentMethod);
     $this->applyTransitionOnOrderCheckout($order, OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
     $this->applyTransitionOnOrderCheckout($order, OrderCheckoutTransitions::TRANSITION_COMPLETE);
 }
Esempio n. 21
0
 /**
  * @param OrderInterface $order
  */
 private function selectShipping(OrderInterface $order)
 {
     $shippingMethod = $this->faker->randomElement($order->getChannel()->getShippingMethods()->toArray());
     Assert::notNull($shippingMethod);
     foreach ($order->getShipments() as $shipment) {
         $shipment->setMethod($shippingMethod);
     }
     $this->applyCheckoutStateTransition($order, OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);
 }
Esempio n. 22
0
 /**
  * @param OrderInterface $order
  * @param string $shipmentState
  *
  * @return int
  */
 private function countOrderShipmentsInState(OrderInterface $order, $shipmentState)
 {
     $shipments = $order->getShipments();
     return $shipments->filter(function (ShipmentInterface $shipment) use($shipmentState) {
         return $shipment->getState() === $shipmentState;
     })->count();
 }
Esempio n. 23
0
 /**
  * @param OrderInterface $order
  *
  * @return string
  */
 private function getShippingMethodName(OrderInterface $order)
 {
     /** @var ShipmentInterface $shipment */
     $shipment = $order->getShipments()->first();
     if (false === $shipment) {
         throw new \LogicException('Order should have at least one shipment.');
     }
     return $shipment->getMethod()->getName();
 }
Esempio n. 24
0
 /**
  * @param OrderInterface $order
  * @param string $transition
  */
 private function applyShipmentTransitionOnOrder(OrderInterface $order, $transition)
 {
     foreach ($order->getShipments() as $shipment) {
         $this->stateMachineFactory->get($shipment, ShipmentTransitions::GRAPH)->apply($transition);
     }
 }
Esempio n. 25
0
 /**
  * @param OrderInterface $order
  *
  * @return NodeElement|null
  */
 private function getLastOrderShipmentElement(OrderInterface $order)
 {
     $shipment = $order->getShipments()->last();
     $shipmentStateElements = $this->getElement('shipments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($shipment->getState())));
     $shipmentStateElement = end($shipmentStateElements);
     return $shipmentStateElement->getParent()->getParent();
 }
 function it_updates_order_shipment_states_to_other_if_order_is_not_be_backordered(ShipmentInterface $shipment, OrderInterface $order)
 {
     $order->isBackorder()->willReturn(false);
     $order->getShipments()->willReturn([$shipment]);
     $this->updateOrderShipmentStates($order, 'foo');
 }