/** * {@inheritdoc} * * @param Entity\QuoteProductOffer $quoteProductOffer * @param Constraints\QuoteProductOffer $constraint */ public function validate($quoteProductOffer, Constraint $constraint) { if (!$quoteProductOffer instanceof Entity\QuoteProductOffer) { throw new UnexpectedTypeException($quoteProductOffer, 'OroB2B\\Bundle\\SaleBundle\\Entity\\QuoteProductOffer'); } if (null === ($quoteProduct = $quoteProductOffer->getQuoteProduct())) { $this->addViolation($constraint); return; } if ($quoteProduct->isTypeNotAvailable()) { $product = $quoteProduct->getProductReplacement(); } else { $product = $quoteProduct->getProduct(); } if (null === $product) { $this->addViolation($constraint); return; } if ([] === ($allowedUnits = $product->getAvailableUnitCodes())) { $this->addViolation($constraint); return; } if (null === ($productUnit = $quoteProductOffer->getProductUnit())) { $this->addViolation($constraint); return; } if (!in_array($productUnit->getCode(), $allowedUnits, true)) { $this->addViolation($constraint); return; } }
public function testAddQuoteProductOffer() { $quoteProduct = new QuoteProduct(); $quoteProductOffer = new QuoteProductOffer(); $this->assertNull($quoteProductOffer->getQuoteProduct()); $quoteProduct->addQuoteProductOffer($quoteProductOffer); $this->assertEquals($quoteProduct, $quoteProductOffer->getQuoteProduct()); }
/** * @param string $unitCode * @param float $quantity * @param bool $allowIncrements * @return QuoteProductOffer */ protected function createQuoteProductOffer($unitCode, $quantity, $allowIncrements) { $unit = new ProductUnit(); $unit->setCode($unitCode); $item = new QuoteProductOffer(); $item->setProductUnit($unit)->setQuantity($quantity)->setAllowIncrements($allowIncrements); return $item; }
/** * @param QuoteProductOffer|null $offer * @return array */ protected function createResponseData(QuoteProductOffer $offer = null) { if (!$offer) { return []; } $price = $offer->getPrice(); if (!$price) { return []; } $formatter = $this->get('oro_locale.formatter.number'); return ['id' => $offer->getId(), 'unit' => $offer->getProductUnitCode(), 'qty' => $offer->getQuantity(), 'price' => $formatter->formatCurrency($price->getValue(), $price->getCurrency())]; }
/** * @param QuoteProductOffer $inputData * @param array $expectedData * * @dataProvider preSetDataProvider */ public function testPreSetData(QuoteProductOffer $inputData = null, array $expectedData = []) { $this->translator->expects($expectedData['empty_value'] ? $this->once() : $this->never())->method('trans')->with($expectedData['empty_value'], ['{title}' => $inputData ? $inputData->getProductUnitCode() : ''])->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); } }
/** * @param ObjectManager $manager * @param Quote $quote * @param string $sku * @param array $items */ protected function addQuoteProduct(ObjectManager $manager, Quote $quote, $sku, $items) { $product = new QuoteProduct(); if ($this->hasReference($sku)) { $product->setProduct($this->getReference($sku)); } else { $product->setProductSku($sku); } foreach ($items as $item) { $productOffer = new QuoteProductOffer(); $productOffer->setQuantity($item['quantity'])->setPrice((new Price())->setValue($item['price'])->setCurrency($item['currency'])); if ($this->hasReference($item['unit'])) { $productOffer->setProductUnit($this->getReference($item['unit'])); } else { $productOffer->setProductUnitCode($item['unit']); } $manager->persist($productOffer); $product->addQuoteProductOffer($productOffer); } $manager->persist($product); $quote->addQuoteProduct($product); }
/** * @return array */ public function validatorDataProvider() { $offer = new QuoteProductOffer(); $offer->setQuantity(10); $moreOffer = new QuoteProductOffer(); $moreOffer->setAllowIncrements(true); $moreOffer->setQuantity(10); return [[null, 'orob2b.sale.quoteproductoffer.configurable.offer.blank', QuoteProductToOrderType::FIELD_QUANTITY], [[], 'orob2b.sale.quoteproductoffer.configurable.offer.blank', QuoteProductToOrderType::FIELD_QUANTITY], [[QuoteProductToOrderType::FIELD_QUANTITY => 10], 'orob2b.sale.quoteproductoffer.configurable.offer.blank', QuoteProductToOrderType::FIELD_QUANTITY], [[QuoteProductToOrderType::FIELD_OFFER => new \stdClass(), QuoteProductToOrderType::FIELD_QUANTITY => 10], 'orob2b.sale.quoteproductoffer.configurable.offer.blank', QuoteProductToOrderType::FIELD_QUANTITY], [[QuoteProductToOrderType::FIELD_OFFER => $offer, QuoteProductToOrderType::FIELD_QUANTITY => $offer->getQuantity() - 5], 'orob2b.sale.quoteproductoffer.configurable.quantity.equal', QuoteProductToOrderType::FIELD_QUANTITY], [[QuoteProductToOrderType::FIELD_OFFER => $offer, QuoteProductToOrderType::FIELD_QUANTITY => $offer->getQuantity() + 5], 'orob2b.sale.quoteproductoffer.configurable.quantity.equal', QuoteProductToOrderType::FIELD_QUANTITY], [[QuoteProductToOrderType::FIELD_OFFER => $moreOffer, QuoteProductToOrderType::FIELD_QUANTITY => $moreOffer->getQuantity() - 5], 'orob2b.sale.quoteproductoffer.configurable.quantity.less', QuoteProductToOrderType::FIELD_QUANTITY], [[QuoteProductToOrderType::FIELD_OFFER => $moreOffer, QuoteProductToOrderType::FIELD_QUANTITY => $moreOffer->getQuantity() + 5], null, null], [[QuoteProductToOrderType::FIELD_OFFER => $moreOffer, QuoteProductToOrderType::FIELD_QUANTITY => $moreOffer->getQuantity()], null, null], [[QuoteProductToOrderType::FIELD_OFFER => $offer, QuoteProductToOrderType::FIELD_QUANTITY => $offer->getQuantity()], null, null]]; }
/** * @param ObjectManager $manager * @param Quote $quote * @param string $sku * @param array $items */ protected function addQuoteProduct(ObjectManager $manager, Quote $quote, $sku, $items) { $product = new QuoteProduct(); if ($this->hasReference($sku)) { $product->setProduct($this->getReference($sku)); } else { $product->setProductSku($sku); } foreach ($items as $index => $item) { $productOffer = new QuoteProductOffer(); $productOffer->setAllowIncrements($item['allow_increments'])->setQuantity($item['quantity'])->setPriceType($item['priceType'])->setPrice((new Price())->setValue($item['price'])->setCurrency($item['currency'])); if ($this->hasReference($item['unit'])) { $productOffer->setProductUnit($this->getReference($item['unit'])); } else { $productOffer->setProductUnitCode($item['unit']); } $manager->persist($productOffer); // e.g sale.quote.1.product.1.offer.1 $this->addReference($quote->getQid() . '.' . $sku . '.offer.' . ($index + 1), $productOffer); $product->addQuoteProductOffer($productOffer); } $manager->persist($product); $quote->addQuoteProduct($product); }
/** * @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; }
/** * @return array */ public function hasIncrementalOffersDataProvider() { $firstNotIncrementedOffer = new QuoteProductOffer(); $firstNotIncrementedOffer->setAllowIncrements(false); $secondNotIncrementedOffer = new QuoteProductOffer(); $secondNotIncrementedOffer->setAllowIncrements(false); $incrementedOffer = new QuoteProductOffer(); $incrementedOffer->setAllowIncrements(true); return ['no offers' => ['offers' => [], 'expected' => false], 'no incremented offers' => ['offers' => [$firstNotIncrementedOffer, $secondNotIncrementedOffer], 'expected' => false], 'one incremented offer' => ['offers' => [$firstNotIncrementedOffer, $secondNotIncrementedOffer, $incrementedOffer], 'expected' => true]]; }
/** * @param QuoteProductOffer $item * @return string */ public function formatOffer(QuoteProductOffer $item) { switch ($item->getPriceType()) { case QuoteProductOffer::PRICE_TYPE_BUNDLED: $transConstant = 'orob2b.sale.quoteproductoffer.item_bundled'; break; default: $transConstant = 'orob2b.sale.quoteproductoffer.item'; } $str = $this->translator->transChoice($transConstant, (int) $item->isAllowIncrements(), ['{units}' => $this->formatProductUnit($item), '{price}' => $this->formatPrice($item), '{unit}' => $this->formatUnitCode($item)]); return $str; }
/** * @param int $quoteProductCount * @param int $quoteProductOfferCount * @param bool|false $allowIncrements * @return Quote */ protected function createQuote($quoteProductCount, $quoteProductOfferCount, $allowIncrements = false) { $quote = new Quote(); for ($i = 0; $i < $quoteProductCount; $i++) { $quoteProduct = new QuoteProduct(); for ($j = 0; $j < $quoteProductOfferCount; $j++) { $quoteProductOffer = new QuoteProductOffer(); $quoteProductOffer->setAllowIncrements($allowIncrements); $quoteProduct->addQuoteProductOffer($quoteProductOffer); } $quote->addQuoteProduct($quoteProduct); } return $quote; }
/** * @param Quote $quote * @param ObjectManager $manager */ protected function processQuoteProducts(Quote $quote, ObjectManager $manager) { $products = $this->getProducts($manager); $currencies = $this->getCurrencies(); $types = [QuoteProduct::TYPE_REQUESTED]; $priceTypes = [QuoteProductOffer::PRICE_TYPE_UNIT]; if ($quote->getRequest()) { foreach ($quote->getRequest()->getRequestProducts() as $requestProduct) { $type = $types[rand(0, count($types) - 1)]; $quoteProduct = $this->createQuoteProduct($requestProduct->getProduct(), $type); $this->processRequestProductItems($quoteProduct, $requestProduct); $quote->addQuoteProduct($quoteProduct); } } else { $numProducts = rand(1, 3); for ($i = 0; $i < $numProducts; $i++) { $product = $products[rand(1, count($products) - 1)]; $quote->addQuoteProduct($this->createQuoteProduct($product, QuoteProduct::TYPE_OFFER)); } } foreach ($quote->getQuoteProducts() as $quoteProduct) { $unitPrecisions = $quoteProduct->getProduct()->getUnitPrecisions(); $numProductOffers = rand(1, 3); for ($j = 0; $j < $numProductOffers; $j++) { if (!count($unitPrecisions)) { continue; } $productUnit = $unitPrecisions[rand(0, count($unitPrecisions) - 1)]->getUnit(); $currency = $currencies[rand(0, count($currencies) - 1)]; $priceType = $priceTypes[rand(0, count($priceTypes) - 1)]; $quoteProductOffer = new QuoteProductOffer(); $quoteProductOffer->setPrice(Price::create(rand(1, 100), $currency))->setQuantity(rand(1, 100))->setProductUnit($productUnit)->setPriceType($priceType)->setAllowIncrements((bool) rand(0, 1)); if ($quoteProduct->isTypeNotAvailable()) { $productReplacement = $products[rand(1, count($products) - 1)]; $unitPrecisionsRepl = $productReplacement->getUnitPrecisions(); $productUnitRepl = $unitPrecisionsRepl[rand(0, count($unitPrecisionsRepl) - 1)]->getUnit(); $quoteProduct->setProductReplacement($productReplacement); $quoteProductOffer->setProductUnit($productUnitRepl); } $quoteProduct->addQuoteProductOffer($quoteProductOffer); } } }
public function testGetPriceTypes() { $this->assertEquals([QuoteProductOffer::PRICE_TYPE_UNIT => 'unit', QuoteProductOffer::PRICE_TYPE_BUNDLED => 'bundled'], QuoteProductOffer::getPriceTypes()); }
/** * @param int $productId * @param float $quantity * @param string $unitCode * @param int $priceType * @param Price $price * @return QuoteProductOffer */ protected function getQuoteProductOffer($productId = null, $quantity = null, $unitCode = null, $priceType = null, Price $price = null) { $quoteProductOffer = new QuoteProductOffer(); $quoteProductOffer->setQuoteProduct($this->getQuoteProduct($productId)); if (null !== $quantity) { $quoteProductOffer->setQuantity($quantity); } if (null !== $unitCode) { $quoteProductOffer->setProductUnit($this->getEntity('OroB2B\\Bundle\\ProductBundle\\Entity\\ProductUnit', $unitCode, 'code')); } if (null !== $priceType) { $quoteProductOffer->setPriceType($priceType); } if (null !== $price) { $quoteProductOffer->setPrice($price); } return $quoteProductOffer; }
/** * @param string $unitCode * @param float $quantity * @param float $priceValue * @param string $priceCurrency * @param int $priceType * @return QuoteProductOffer */ protected function createQuoteProductOffer($unitCode, $quantity, $priceType, $priceValue, $priceCurrency) { $unit = new ProductUnit(); $unit->setCode($unitCode); $price = Price::create($priceValue, $priceCurrency); $quoteProductOffer = new QuoteProductOffer(); $quoteProductOffer->setProductUnit($unit)->setQuantity($quantity)->setPrice($price)->setPriceType($priceType); return $quoteProductOffer; }
/** * "Success" form handler * * @param RFPRequest $entity * @return bool */ protected function onSuccess(RFPRequest $entity) { $quote = new Quote(); $quote->setRequest($entity)->setOwner($this->user)->setAccount($entity->getAccount())->setAccountUser($entity->getAccountUser())->setOrganization($this->user->getOrganization()); foreach ($entity->getRequestProducts() as $requestProduct) { $quoteProduct = new QuoteProduct(); $quoteProduct->setProduct($requestProduct->getProduct())->setType(QuoteProduct::TYPE_REQUESTED)->setCommentAccount($requestProduct->getComment()); foreach ($requestProduct->getRequestProductItems() as $requestProductItem) { $quoteProductRequest = new QuoteProductRequest(); $quoteProductRequest->setQuantity($requestProductItem->getQuantity())->setPrice($requestProductItem->getPrice())->setProductUnit($requestProductItem->getProductUnit())->setRequestProductItem($requestProductItem); $quoteProduct->addQuoteProductRequest($quoteProductRequest); $quoteProductOffer = new QuoteProductOffer(); $quoteProductOffer->setQuantity($requestProductItem->getQuantity())->setProductUnit($requestProductItem->getProductUnit())->setPriceType(QuoteProductOffer::PRICE_TYPE_UNIT)->setAllowIncrements(true); $quoteProduct->addQuoteProductOffer($quoteProductOffer); } $quote->addQuoteProduct($quoteProduct); } try { $this->manager->persist($quote); $this->manager->flush(); $this->quote = $quote; } catch (DBALException $e) { $this->exception = $e; return false; } return true; }