/**
  * {@inheritdoc}
  */
 public function finishView(FormView $view, FormInterface $form, array $options)
 {
     $units = [];
     /* @var $products Product[] */
     $products = [];
     if ($view->vars['value']) {
         /* @var $quoteProduct QuoteProduct */
         $quoteProduct = $view->vars['value'];
         if ($quoteProduct->getProduct()) {
             $product = $quoteProduct->getProduct();
             $products[$product->getId()] = $product;
         }
         if ($quoteProduct->getProductReplacement()) {
             $product = $quoteProduct->getProductReplacement();
             $products[$product->getId()] = $product;
         }
     }
     foreach ($products as $product) {
         $units[$product->getId()] = [];
         foreach ($product->getAvailableUnitCodes() as $unitCode) {
             $units[$product->getId()][$unitCode] = $this->labelFormatter->format($unitCode);
         }
     }
     $componentOptions = ['units' => $units, 'typeOffer' => QuoteProduct::TYPE_OFFER, 'typeReplacement' => QuoteProduct::TYPE_NOT_AVAILABLE];
     $view->vars['componentOptions'] = $componentOptions;
 }
 /**
  * Get choices list for unit field.
  *
  * @return array
  */
 protected function getUnitChoices()
 {
     $choices = [];
     $units = $this->registry->getManagerForClass('OroB2BProductBundle:ProductUnit')->getRepository('OroB2BProductBundle:ProductUnit')->findAll();
     foreach ($units as $unit) {
         $choices[$unit->getCode()] = $this->formatter->format($unit->getCode());
     }
     return $choices;
 }
 public function testGetMetadata()
 {
     $this->formatter->expects($this->once())->method('format')->with('test value', true)->willReturn('formatted test label');
     $formView = $this->createFormView();
     $formView->vars['formatter_options'] = [];
     $typeFormView = $this->createFormView($formView);
     $typeFormView->vars['choices'] = [];
     $unitFormView = $this->createFormView($formView);
     $unitFormView->vars['choices'] = [new ChoiceView('test data', 'test value', 'test label')];
     $formView->children = ['type' => $typeFormView, 'unit' => $unitFormView];
     $this->form->expects($this->any())->method('createView')->willReturn($formView);
     $metadata = $this->productPriceFilter->getMetadata();
     $this->assertArrayHasKey('unitChoices', $metadata);
     $this->assertInternalType('array', $metadata['unitChoices']);
     $this->assertEquals([['data' => 'test data', 'value' => 'test value', 'label' => 'test label', 'shortLabel' => 'formatted test label']], $metadata['unitChoices']);
 }
 /**
  * {@inheritDoc}
  */
 public function finishView(FormView $view, FormInterface $form, array $options)
 {
     $formParent = $form->getParent();
     if (!$formParent) {
         return;
     }
     /* @var $productUnitHolder ProductUnitHolderInterface */
     $productUnitHolder = $formParent->getData();
     if (!$productUnitHolder) {
         return;
     }
     $product = $productUnitHolder->getProductHolder()->getProduct();
     $choices = $this->getProductUnits($product);
     $productUnit = $productUnitHolder->getProductUnit();
     if (!$productUnit || $product && !in_array($productUnit, $choices, true)) {
         $emptyValueTitle = $this->translator->trans($this->options['empty_label'], ['{title}' => $productUnitHolder->getProductUnitCode()]);
         $view->vars['empty_value'] = $emptyValueTitle;
     }
     $choices = $this->productUnitFormatter->formatChoices($choices, $options['compact']);
     $choicesViews = [];
     foreach ($choices as $key => $value) {
         $choicesViews[] = new ChoiceView($value, $key, $value);
     }
     $view->vars['choices'] = $choicesViews;
 }
 /**
  * {@inheritdoc}
  */
 public function getMetadata()
 {
     $metadata = parent::getMetadata();
     $metadata['unitChoices'] = [];
     $unitChoices = $this->getForm()->createView()['unit']->vars['choices'];
     foreach ($unitChoices as $choice) {
         $metadata['unitChoices'][] = ['data' => $choice->data, 'value' => $choice->value, 'label' => $choice->label, 'shortLabel' => $this->formatter->format($choice->value, true)];
     }
     return $metadata;
 }
 /**
  * @param array $inputData
  * @param array $expectedData
  *
  * @dataProvider formatOfferProvider
  */
 public function testFormatOffer(array $inputData, array $expectedData)
 {
     /* @var $item QuoteProductOffer */
     $item = $inputData['item'];
     $item->setQuantity($inputData['quantity'])->setProductUnit($inputData['unit'])->setProductUnitCode($inputData['unitCode'])->setPrice($inputData['price']);
     $this->productUnitValueFormatter->expects($inputData['unit'] ? $this->once() : $this->never())->method('format')->with($inputData['quantity'], $inputData['unitCode'])->will($this->returnValue($expectedData['formattedUnits']));
     $price = $inputData['price'] ?: new Price();
     $this->numberFormatter->expects($price ? $this->once() : $this->never())->method('formatCurrency')->with($price->getValue(), $price->getCurrency())->will($this->returnValue($expectedData['formattedPrice']));
     $this->productUnitLabelFormatter->expects($this->once())->method('format')->with($inputData['unitCode'])->will($this->returnValue($expectedData['formattedUnit']));
     $this->translator->expects($this->once())->method('transChoice')->with($expectedData['transConstant'], $expectedData['transIndex'], ['{units}' => $expectedData['formattedUnits'], '{price}' => $expectedData['formattedPrice'], '{unit}' => $expectedData['formattedUnit']]);
     $this->formatter->formatOffer($inputData['item']);
 }
 /**
  * {@inheritdoc}
  */
 public function finishView(FormView $view, FormInterface $form, array $options)
 {
     $units = [];
     /* @var $products Product[] */
     $products = [];
     if ($view->vars['value']) {
         /* @var $requestProduct RequestProduct */
         $requestProduct = $view->vars['value'];
         if ($requestProduct->getProduct()) {
             $product = $requestProduct->getProduct();
             $products[$product->getId()] = $product;
         }
     }
     foreach ($products as $product) {
         $units[$product->getId()] = [];
         foreach ($product->getAvailableUnitCodes() as $unitCode) {
             $units[$product->getId()][$unitCode] = $this->labelFormatter->format($unitCode, $options['compact_units']);
         }
     }
     $componentOptions = ['units' => $units, 'compactUnits' => $options['compact_units']];
     $view->vars['componentOptions'] = $componentOptions;
 }
 protected function getFreeFormUnits()
 {
     $units = $this->registry->getRepository($this->productUnitClass)->findBy([], ['code' => 'ASC']);
     $units = $this->productUnitFormatter->formatChoices($units);
     return $units;
 }
 /**
  * @param bool $isShort
  * @param array $expected
  *
  * @dataProvider formatChoicesProvider
  */
 public function testFormatChoices($isShort, array $expected)
 {
     $units = [(new ProductUnit())->setCode('kg'), (new ProductUnit())->setCode('item')];
     $this->translator->expects($this->exactly(2))->method('trans')->will($this->returnValueMap([['orob2b.product_unit.kg.label.full', [], null, null, '_KG'], ['orob2b.product_unit.item.label.full', [], null, null, '_ITEM'], ['orob2b.product_unit.kg.label.short', [], null, null, '_KG_SHORT'], ['orob2b.product_unit.item.label.short', [], null, null, '_ITEM_SHORT']]));
     $this->assertEquals($expected, $this->formatter->formatChoices($units, $isShort));
 }
 /**
  * @param BaseQuoteProductItem $item
  * @return string
  */
 protected function formatUnitCode(BaseQuoteProductItem $item)
 {
     $unit = $this->productUnitLabelFormatter->format($item->getProductUnitCode());
     return $unit;
 }
 /**
  * @param string $unitCode
  * @param bool $isShort
  * @return string
  */
 public function format($unitCode, $isShort = false)
 {
     return $this->formatter->format($unitCode, $isShort);
 }
 /**
  * @param string $unitCode
  * @param bool $isShort
  * @param string $expected
  *
  * @dataProvider formatProvider
  */
 public function testFormat($unitCode, $isShort, $expected)
 {
     $this->translator->expects($this->once())->method('trans')->with($expected);
     $this->formatter->format($unitCode, $isShort);
 }
 /**
  * @param string $unitCode
  * @param bool $isShort
  * @param string $expected
  *
  * @dataProvider formatProvider
  */
 public function testFormat($unitCode, $isShort, $expected)
 {
     $this->formatter->expects($this->once())->method('format')->with($unitCode, $isShort)->willReturn($expected);
     $this->assertEquals($expected, $this->extension->format($unitCode, $isShort));
 }