function it_does_nothing_if_order_item_has_no_units(CalculatorInterface $calculator, AdjustmentFactoryInterface $adjustmentsFactory, TaxRateResolverInterface $taxRateResolver, OrderInterface $order, OrderItemInterface $orderItem, ProductVariantInterface $productVariant, TaxRateInterface $taxRate, ZoneInterface $zone)
 {
     $orderItems = new ArrayCollection([$orderItem->getWrappedObject()]);
     $order->getItems()->willReturn($orderItems);
     $orderItem->getVariant()->willReturn($productVariant);
     $orderItem->getUnits()->willReturn(new ArrayCollection());
     $taxRateResolver->resolve(Argument::cetera())->willReturn($taxRate);
     $calculator->calculate(Argument::cetera())->shouldNotBeCalled();
     $adjustmentsFactory->createWithData(Argument::cetera())->shouldNotBeCalled();
     $this->apply($order, $zone);
 }
 /**
  * {@inheritdoc}
  */
 public function apply(OrderInterface $order, ZoneInterface $zone)
 {
     $shippingTotal = $order->getShippingTotal();
     if (0 === $shippingTotal) {
         return;
     }
     $taxRate = $this->taxRateResolver->resolve($this->getShippingMethod($order), ['zone' => $zone]);
     if (null === $taxRate) {
         return;
     }
     $taxAmount = $this->calculator->calculate($shippingTotal, $taxRate);
     if (0 === $taxAmount) {
         return;
     }
     $this->addAdjustment($order, $taxAmount, $taxRate->getLabel(), $taxRate->isIncludedInPrice());
 }
 /**
  * {@inheritdoc}
  */
 public function apply(OrderInterface $order, ZoneInterface $zone)
 {
     foreach ($order->getItems() as $item) {
         $taxRate = $this->taxRateResolver->resolve($item->getVariant(), ['zone' => $zone]);
         if (null === $taxRate) {
             continue;
         }
         foreach ($item->getUnits() as $unit) {
             $taxAmount = $this->calculator->calculate($unit->getTotal(), $taxRate);
             if (0 === $taxAmount) {
                 continue;
             }
             $this->addTaxAdjustment($unit, $taxAmount, $taxRate->getLabel(), $taxRate->isIncludedInPrice());
         }
     }
 }
 function it_does_nothing_if_order_has_0_shipping_total(TaxRateResolverInterface $taxRateResolver, OrderInterface $order, ZoneInterface $zone)
 {
     $order->getShippingTotal()->willReturn(0);
     $taxRateResolver->resolve(Argument::any())->shouldNotBeCalled();
     $order->addAdjustment(Argument::any())->shouldNotBeCalled();
     $this->apply($order, $zone);
 }
 /**
  * {@inheritdoc}
  */
 public function apply(OrderInterface $order, ZoneInterface $zone)
 {
     $lastShipment = $order->getLastShipment();
     if (!$lastShipment) {
         return;
     }
     $shippingAdjustments = $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT);
     if ($shippingAdjustments->isEmpty()) {
         return;
     }
     $taxRate = $this->taxRateResolver->resolve($lastShipment->getMethod(), array('zone' => $zone));
     if (null === $taxRate) {
         return;
     }
     $lastShippingAdjustment = $shippingAdjustments->last();
     $taxAmount = $this->calculator->calculate($lastShippingAdjustment->getAmount(), $taxRate);
     $this->addAdjustment($order, $taxAmount, $taxRate->getLabel(), $taxRate->isIncludedInPrice());
 }
 /**
  * {@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->getProduct(), array('zone' => $zone));
         if (null === $taxRate) {
             continue;
         }
         $totalTaxAmount = $this->calculator->calculate($item->getTotal(), $taxRate);
         $splitTaxes = $this->distributor->distribute($totalTaxAmount, $quantity);
         $units = $item->getUnits();
         foreach ($splitTaxes as $key => $tax) {
             if (0 === $tax) {
                 continue;
             }
             $this->addAdjustment($units->get($key), $tax, $taxRate->getLabel(), $taxRate->isIncludedInPrice());
         }
     }
 }
 /**
  * {@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++;
         }
     }
 }
 /**
  * @param OrderInterface $order
  * @param ZoneInterface $zone
  *
  * @return array
  */
 protected function processTaxes(OrderInterface $order, $zone)
 {
     $taxes = array();
     foreach ($order->getItems() as $item) {
         $rate = $this->taxRateResolver->resolve($item->getProduct(), array('zone' => $zone));
         // Skip this item is there is not matching tax rate.
         if (null === $rate) {
             continue;
         }
         $amount = $this->calculator->calculate($item->getTotal(), $rate);
         $taxAmount = $rate->getAmountAsPercentage();
         $description = sprintf('%s (%s%%)', $rate->getName(), (double) $taxAmount);
         $taxes[$description] = array('amount' => (isset($taxes[$description]['amount']) ? $taxes[$description]['amount'] : 0) + $amount, 'included' => $rate->isIncludedInPrice());
     }
     return $taxes;
 }
 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_nothing_if_order_has_no_shipping_adjustments(TaxRateResolverInterface $taxRateResolver, Collection $shippingAdjustments, OrderInterface $order, ShipmentInterface $shipment, ZoneInterface $zone)
 {
     $order->getLastShipment()->willReturn($shipment);
     $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->willReturn($shippingAdjustments);
     $shippingAdjustments->isEmpty()->willReturn(true);
     $taxRateResolver->resolve(Argument::any())->shouldNotBeCalled();
     $this->apply($order, $zone);
 }
 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);
 }