public function testGetSubtotals()
 {
     $this->translator->expects($this->once())->method('trans')->with(sprintf('orob2b.order.subtotals.%s', Subtotal::TYPE_SUBTOTAL))->willReturn(ucfirst(Subtotal::TYPE_SUBTOTAL));
     $order = new Order();
     $perUnitLineItem = new OrderLineItem();
     $perUnitLineItem->setPriceType(OrderLineItem::PRICE_TYPE_UNIT);
     $perUnitLineItem->setPrice(Price::create(20, 'USD'));
     $perUnitLineItem->setQuantity(2);
     $bundledUnitLineItem = new OrderLineItem();
     $bundledUnitLineItem->setPriceType(OrderLineItem::PRICE_TYPE_BUNDLED);
     $bundledUnitLineItem->setPrice(Price::create(2, 'USD'));
     $bundledUnitLineItem->setQuantity(10);
     $otherCurrencyLineItem = new OrderLineItem();
     $otherCurrencyLineItem->setPriceType(OrderLineItem::PRICE_TYPE_UNIT);
     $otherCurrencyLineItem->setPrice(Price::create(10, 'EUR'));
     $otherCurrencyLineItem->setQuantity(10);
     $emptyLineItem = new OrderLineItem();
     $order->addLineItem($perUnitLineItem);
     $order->addLineItem($bundledUnitLineItem);
     $order->addLineItem($emptyLineItem);
     $order->addLineItem($otherCurrencyLineItem);
     $order->setCurrency('USD');
     $subtotals = $this->provider->getSubtotals($order);
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $subtotals);
     $subtotal = $subtotals->get(Subtotal::TYPE_SUBTOTAL);
     $this->assertInstanceOf('OroB2B\\Bundle\\OrderBundle\\Model\\Subtotal', $subtotal);
     $this->assertEquals(Subtotal::TYPE_SUBTOTAL, $subtotal->getType());
     $this->assertEquals(ucfirst(Subtotal::TYPE_SUBTOTAL), $subtotal->getLabel());
     $this->assertEquals($order->getCurrency(), $subtotal->getCurrency());
     $this->assertInternalType('float', $subtotal->getAmount());
     $this->assertEquals(142.0, $subtotal->getAmount());
 }
 /**
  * {@inheritdoc}
  */
 protected function addItem(Product $product, $entity, array $itemData = [])
 {
     if (!$entity instanceof Order) {
         return;
     }
     $lineItem = new OrderLineItem();
     $lineItem->setProduct($product)->setProductSku($product->getSku());
     if (array_key_exists(ProductDataStorage::PRODUCT_QUANTITY_KEY, $itemData)) {
         $lineItem->setQuantity($itemData[ProductDataStorage::PRODUCT_QUANTITY_KEY]);
     }
     $this->fillEntityData($lineItem, $itemData);
     if (!$lineItem->getProductUnit()) {
         /** @var ProductUnitPrecision $unitPrecision */
         $unitPrecision = $product->getUnitPrecisions()->first();
         if (!$unitPrecision) {
             return;
         }
         /** @var ProductUnit $unit */
         $unit = $unitPrecision->getUnit();
         if (!$unit) {
             return;
         }
         $lineItem->setProductUnit($unit);
         $lineItem->setProductUnitCode($unit->getCode());
     }
     if ($lineItem->getProduct()) {
         $entity->addLineItem($lineItem);
     }
 }
 /**
  * @param QuoteProductOffer $quoteProductOffer
  * @param float|null $quantity
  * @return OrderLineItem
  */
 protected function createOrderLineItem(QuoteProductOffer $quoteProductOffer, $quantity = null)
 {
     $quoteProduct = $quoteProductOffer->getQuoteProduct();
     if ($quoteProduct->getProductReplacement()) {
         $product = $quoteProduct->getProductReplacement();
     } else {
         $product = $quoteProduct->getProduct();
     }
     $orderLineItem = new OrderLineItem();
     $orderLineItem->setProduct($product)->setProductUnit($quoteProductOffer->getProductUnit())->setQuantity($quantity ?: $quoteProductOffer->getQuantity())->setPriceType($quoteProductOffer->getPriceType())->setPrice($quoteProductOffer->getPrice())->setFromExternalSource(true);
     return $orderLineItem;
 }
 /**
  * @param string $sku
  * @param string $unitCode
  * @param float $qty
  * @param int $priceType
  * @param float $priceValue
  * @param string $priceCurrency
  * @return OrderLineItem
  */
 protected function createOrderLineItem($sku, $unitCode, $qty, $priceType, $priceValue, $priceCurrency)
 {
     $orderLineItem = new OrderLineItem();
     $orderLineItem->setProduct((new Product())->setSku($sku))->setProductUnit((new ProductUnit())->setCode($unitCode))->setQuantity($qty)->setPriceType($priceType)->setPrice(Price::create($priceValue, $priceCurrency))->setFromExternalSource(true);
     return $orderLineItem;
 }
 /**
  * @dataProvider isRequirePriceRecalculationDataProvider
  *
  * @param OrderLineItem $entity
  * @param string $method
  * @param mixed $value
  * @param bool $expectedResult
  */
 public function testIsRequirePriceRecalculation(OrderLineItem $entity, $method, $value, $expectedResult)
 {
     $this->assertFalse($entity->isRequirePriceRecalculation());
     $entity->{$method}($value);
     $this->assertEquals($expectedResult, $entity->isRequirePriceRecalculation());
 }
 /**
  * @param OrderLineItem $lineItem
  * @return bool
  */
 protected function isValidForPriceUpdate(OrderLineItem $lineItem)
 {
     return $lineItem->getProduct() && $lineItem->getProductUnit() && $lineItem->getQuantity() && !$lineItem->getPrice() && !$lineItem->isFromExternalSource() && $lineItem->isRequirePriceRecalculation();
 }
 /**
  * @param OrderLineItem $item
  * @return array|ProductUnit[]
  */
 protected function getProductAvailableChoices(OrderLineItem $item)
 {
     /** @var ProductPriceRepository $repository */
     $repository = $this->registry->getManagerForClass($this->priceClass)->getRepository($this->priceClass);
     $priceList = $this->priceListRequestHandler->getPriceList();
     $choices = $repository->getProductUnitsByPriceList($priceList, $item->getProduct(), $item->getOrder()->getCurrency());
     $hasChoice = false;
     foreach ($choices as $unit) {
         if ($unit->getCode() === $item->getProductUnit()->getCode()) {
             $hasChoice = true;
             break;
         }
     }
     if (!$hasChoice) {
         $choices[] = $item->getProductUnit();
     }
     return $choices;
 }