/**
  * {@inheritdoc}
  *
  * @param RequestProductItemEntity $requestProductItem
  * @param Constraint|RequestProductItem $constraint
  */
 public function validate($requestProductItem, Constraint $constraint)
 {
     if (!$requestProductItem instanceof RequestProductItemEntity) {
         throw new UnexpectedTypeException($requestProductItem, 'OroB2B\\Bundle\\RFPBundle\\Entity\\RequestProductItem');
     }
     if (null === ($requestProduct = $requestProductItem->getRequestProduct())) {
         $this->addViolation($constraint);
         return;
     }
     if (null === ($product = $requestProduct->getProduct())) {
         $this->addViolation($constraint);
         return;
     }
     if ([] === ($allowedUnits = $product->getAvailableUnitCodes())) {
         $this->addViolation($constraint);
         return;
     }
     if (null === ($productUnit = $requestProductItem->getProductUnit())) {
         $this->addViolation($constraint);
         return;
     }
     if (!in_array($productUnit->getCode(), $allowedUnits, true)) {
         $this->addViolation($constraint);
         return;
     }
 }
 /**
  * @depends testProperties
  */
 public function testAddRequestProductItem()
 {
     $requestProduct = new RequestProduct();
     $requestProductItem = new RequestProductItem();
     $this->assertNull($requestProductItem->getRequestProduct());
     $requestProduct->addRequestProductItem($requestProductItem);
     $this->assertEquals($requestProduct, $requestProductItem->getRequestProduct());
 }
 public function testUpdatePrice()
 {
     $item = new RequestProductItem();
     $item->setPrice(Price::create(11, 'EUR'));
     $item->updatePrice();
     $this->assertEquals(11, $this->getProperty($item, 'value'));
     $this->assertEquals('EUR', $this->getProperty($item, 'currency'));
 }
 /**
  * @param RequestProductItem $inputData
  * @param array $expectedData
  *
  * @dataProvider preSetDataProvider
  */
 public function testPreSetData(RequestProductItem $inputData = null, array $expectedData = [])
 {
     $unitCode = $inputData ? $inputData->getProductUnitCode() : '';
     $this->translator->expects($expectedData['empty_value'] ? $this->once() : $this->never())->method('trans')->with($expectedData['empty_value'], ['{title}' => $unitCode])->will($this->returnValue($expectedData['empty_value']));
     $form = $this->factory->create($this->formType);
     $this->formType->preSetData(new FormEvent($form, $inputData));
     $this->assertTrue($form->has('productUnit'));
     $config = $form->get('productUnit')->getConfig();
     $this->assertEquals(ProductUnitSelectionType::NAME, $config->getType()->getName());
     $options = $form->get('productUnit')->getConfig()->getOptions();
     foreach ($expectedData as $key => $value) {
         $this->assertEquals($value, $options[$key], $key);
     }
 }
 /**
  * "Success" form handler
  *
  * @param ShoppingList $entity
  * @return bool
  */
 protected function onSuccess(ShoppingList $entity)
 {
     $rfpRequest = new RFPRequest();
     $rfpRequest->setFirstName($this->user->getFirstName())->setLastName($this->user->getLastName())->setEmail($this->user->getEmail())->setPhone('')->setRole('')->setBody('')->setCompany($this->user->getOrganization() ? $this->user->getOrganization()->getName() : '')->setAccountUser($this->user)->setAccount($this->user->getAccount())->setStatus($this->requestStatus);
     foreach ($entity->getLineItems() as $shoppingListLineItem) {
         $requestProduct = new RequestProduct();
         $requestProduct->setProduct($shoppingListLineItem->getProduct());
         $requestProductItem = new RequestProductItem();
         $requestProductItem->setQuantity($shoppingListLineItem->getQuantity())->setProductUnit($shoppingListLineItem->getUnit());
         $requestProduct->addRequestProductItem($requestProductItem);
         $rfpRequest->addRequestProduct($requestProduct);
     }
     try {
         $this->manager->persist($rfpRequest);
         $this->manager->flush();
         $this->rfpRequest = $rfpRequest;
     } catch (DBALException $e) {
         $this->exception = $e;
         return false;
     }
     return true;
 }
 /**
  * @param Request $request
  * @param ObjectManager $manager
  */
 protected function processRequestProducts(Request $request, ObjectManager $manager)
 {
     $products = $this->getProducts($manager);
     $currencies = $this->getCurrencies();
     for ($i = 0; $i < rand(1, 10); $i++) {
         $product = $products[rand(0, count($products) - 1)];
         $unitPrecisions = $product->getUnitPrecisions();
         if (!count($unitPrecisions)) {
             continue;
         }
         $requestProduct = new RequestProduct();
         $requestProduct->setProduct($product);
         $requestProduct->setComment(sprintf('Notes %s', $i));
         for ($j = 0; $j < rand(1, 10); $j++) {
             $productUnit = $unitPrecisions[rand(0, count($unitPrecisions) - 1)]->getUnit();
             $currency = $currencies[rand(0, count($currencies) - 1)];
             $requestProductItem = new RequestProductItem();
             $requestProductItem->setPrice(Price::create(rand(1, 100), $currency))->setQuantity(rand(1, 100))->setProductUnit($productUnit);
             $requestProduct->addRequestProductItem($requestProductItem);
         }
         $request->addRequestProduct($requestProduct);
     }
 }
Esempio n. 7
0
 /**
  * @param int $productId
  * @param int $quantity
  * @param string $unitCode
  * @param OptionalPrice $price
  * @return RequestProductItem
  */
 protected function getRequestProductItem($productId, $quantity = null, $unitCode = null, OptionalPrice $price = null)
 {
     $requestProductItem = new RequestProductItem();
     $requestProductItem->setRequestProduct($this->getRequestProduct($productId));
     if (null !== $quantity) {
         $requestProductItem->setQuantity($quantity);
     }
     if (null !== $unitCode) {
         $requestProductItem->setProductUnit($this->getEntity('OroB2B\\Bundle\\ProductBundle\\Entity\\ProductUnit', $unitCode, 'code'));
     }
     if (null !== $price) {
         $requestProductItem->setPrice($price);
     }
     return $requestProductItem;
 }