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());
 }
 /**
  * @param EntityManager $manager
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     $locator = $this->container->get('file_locator');
     $filePath = $locator->locate('@OroB2BOrderBundle/Migrations/Data/Demo/ORM/data/orders.csv');
     if (is_array($filePath)) {
         $filePath = current($filePath);
     }
     $handler = fopen($filePath, 'r');
     $headers = fgetcsv($handler, 1000, ',');
     /** @var EntityRepository $userRepository */
     $userRepository = $manager->getRepository('OroUserBundle:User');
     /** @var User $user */
     $user = $userRepository->findOneBy([]);
     $accountUser = $this->getAccountUser($manager);
     while (($data = fgetcsv($handler, 1000, ',')) !== false) {
         $row = array_combine($headers, array_values($data));
         $order = new Order();
         $billingAddress = ['label' => $row['billingAddressLabel'], 'country' => $row['billingAddressCountry'], 'city' => $row['billingAddressCity'], 'region' => $row['billingAddressRegion'], 'street' => $row['billingAddressStreet'], 'postalCode' => $row['billingAddressPostalCode']];
         $shippingAddress = ['label' => $row['shippingAddressLabel'], 'country' => $row['shippingAddressCountry'], 'city' => $row['shippingAddressCity'], 'region' => $row['shippingAddressRegion'], 'street' => $row['shippingAddressStreet'], 'postalCode' => $row['shippingAddressPostalCode']];
         $order->setOwner($user)->setAccount($accountUser->getAccount())->setIdentifier($row['identifier'])->setAccountUser($accountUser)->setOrganization($user->getOrganization())->setBillingAddress($this->createOrderAddress($manager, $billingAddress))->setShippingAddress($this->createOrderAddress($manager, $shippingAddress))->setPaymentTerm($this->getPaymentTerm($manager, $row['paymentTerm']))->setPriceList($this->getPriceList($manager, $row['priceListName']))->setShipUntil(new \DateTime())->setCurrency($row['currency'])->setPoNumber($row['poNumber'])->setSubtotal($row['subtotal']);
         if (!empty($row['customerNotes'])) {
             $order->setCustomerNotes($row['customerNotes']);
         }
         $manager->persist($order);
     }
     fclose($handler);
     $manager->flush();
 }
예제 #3
0
 /**
  * @param ObjectManager $manager
  * @param string        $name
  *
  * @return Order
  */
 protected function createOrder(ObjectManager $manager, $name)
 {
     $user = $this->getReference('order.simple_user');
     $order = new Order();
     $order->setOwner($user);
     $order->setOrganization($user->getOrganization());
     $manager->persist($order);
     $this->addReference($name, $order);
     return $order;
 }
 /**
  * @param Order $order
  * @return PriceList
  */
 protected function getPriceList(Order $order)
 {
     $priceList = null;
     if ($this->getUser() instanceof User) {
         $priceList = $order->getPriceList();
     }
     if (!$priceList) {
         $priceList = $this->get('orob2b_pricing.model.frontend.price_list_request_handler')->getPriceList();
     }
     return $priceList;
 }
예제 #5
0
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     /** @var User[] $users */
     $users = $manager->getRepository('OroUserBundle:User')->findAll();
     // create orders
     foreach ($users as $user) {
         $order = new Order();
         $order->setOwner($user)->setOrganization($user->getOrganization())->setIdentifier($user->getId());
         $manager->persist($order);
     }
     $manager->flush();
 }
 public function testBuildWithoutUnit()
 {
     $sku = 'TEST';
     $qty = 3;
     $data = [ProductDataStorage::ENTITY_ITEMS_DATA_KEY => [[ProductDataStorage::PRODUCT_SKU_KEY => $sku, ProductDataStorage::PRODUCT_QUANTITY_KEY => $qty]]];
     $order = new Order();
     $product = $this->getProductEntity($sku);
     $this->assertMetadataCalled();
     $this->assertRequestGetCalled();
     $this->assertStorageCalled($data);
     $this->assertProductRepositoryCalled($product);
     /** @var \PHPUnit_Framework_MockObject_MockObject|FormBuilderInterface $builder */
     $builder = $this->getMock('Symfony\\Component\\Form\\FormBuilderInterface');
     $this->extension->buildForm($builder, ['data' => $order]);
     $this->assertEmpty($order->getLineItems());
 }
예제 #7
0
 /**
  * @param ObjectManager $manager
  * @param string $name
  * @param array $orderData
  * @return Order
  */
 protected function createOrder(ObjectManager $manager, $name, array $orderData)
 {
     /** @var User $user */
     $user = $this->getReference($orderData['user']);
     if (!$user->getOrganization()) {
         $user->setOrganization($manager->getRepository('OroOrganizationBundle:Organization')->findOneBy([]));
     }
     /** @var AccountUser $accountUser */
     $accountUser = $manager->getRepository('OroB2BAccountBundle:AccountUser')->findOneBy(['username' => $orderData['accountUser']]);
     /** @var PaymentTerm $paymentTerm */
     $paymentTerm = $this->getReference($orderData['paymentTerm']);
     $order = new Order();
     $order->setIdentifier($name)->setOwner($user)->setOrganization($user->getOrganization())->setPaymentTerm($paymentTerm)->setShipUntil(new \DateTime())->setCurrency($orderData['currency'])->setPoNumber($orderData['poNumber'])->setSubtotal($orderData['subtotal'])->setAccount($accountUser->getAccount())->setAccountUser($accountUser);
     $manager->persist($order);
     $this->addReference($name, $order);
     return $order;
 }
 /**
  * Get order related data
  *
  * @Route("/related-data", name="orob2b_order_related_data")
  * @Method({"GET"})
  * @AclAncestor("orob2b_order_update")
  *
  * @return JsonResponse
  */
 public function getRelatedDataAction()
 {
     $order = new Order();
     $accountUser = $this->getOrderHandler()->getAccountUser();
     $account = $this->getAccount($accountUser);
     $order->setAccount($account);
     $order->setAccountUser($accountUser);
     $accountPaymentTerm = null;
     if ($account) {
         $accountPaymentTerm = $this->getPaymentTermProvider()->getAccountPaymentTerm($account);
     }
     $accountGroupPaymentTerm = null;
     if ($account->getGroup()) {
         $accountGroupPaymentTerm = $this->getPaymentTermProvider()->getAccountGroupPaymentTerm($account->getGroup());
     }
     $orderForm = $this->createForm($this->getOrderFormTypeName(), $order);
     return new JsonResponse(['billingAddress' => $this->renderForm($orderForm->get(AddressType::TYPE_BILLING . 'Address')->createView()), 'shippingAddress' => $this->renderForm($orderForm->get(AddressType::TYPE_SHIPPING . 'Address')->createView()), 'accountPaymentTerm' => $accountPaymentTerm ? $accountPaymentTerm->getId() : null, 'accountGroupPaymentTerm' => $accountGroupPaymentTerm ? $accountGroupPaymentTerm->getId() : null]);
 }
 /**
  * @param Quote $quote
  * @param AccountUser|null $user
  * @param array|null $selectedOffers
  * @return Order
  */
 public function convert(Quote $quote, AccountUser $user = null, array $selectedOffers = null)
 {
     $accountUser = $user ?: $quote->getAccountUser();
     $account = $user ? $user->getAccount() : $quote->getAccount();
     $order = new Order();
     $order->setAccount($account)->setAccountUser($accountUser)->setOwner($quote->getOwner())->setOrganization($quote->getOrganization());
     if (!$selectedOffers) {
         foreach ($quote->getQuoteProducts() as $quoteProduct) {
             /** @var QuoteProductOffer $productOffer */
             $productOffer = $quoteProduct->getQuoteProductOffers()->first();
             $order->addLineItem($this->createOrderLineItem($productOffer));
         }
     } else {
         foreach ($selectedOffers as $selectedOffer) {
             /** @var QuoteProductOffer $offer */
             $offer = $selectedOffer[self::FIELD_OFFER];
             $order->addLineItem($this->createOrderLineItem($offer, (double) $selectedOffer[self::FIELD_QUANTITY]));
         }
     }
     $this->orderCurrencyHandler->setOrderCurrency($order);
     $this->fillSubtotals($order);
     return $order;
 }
 /**
  * Get order subtotal
  *
  * @param Order $order
  *
  * @return Subtotal
  */
 protected function getSubtotal(Order $order)
 {
     $subtotal = new Subtotal();
     $subtotal->setType(Subtotal::TYPE_SUBTOTAL);
     $translation = sprintf('orob2b.order.subtotals.%s', $subtotal->getType());
     $subtotal->setLabel($this->translator->trans($translation));
     $subtotalAmount = 0.0;
     foreach ($order->getLineItems() as $lineItem) {
         if (!$lineItem->getPrice()) {
             continue;
         }
         $rowTotal = $lineItem->getPrice()->getValue();
         if ($lineItem->getPriceType() === OrderLineItem::PRICE_TYPE_UNIT) {
             $rowTotal *= $lineItem->getQuantity();
         }
         if ($order->getCurrency() !== $lineItem->getPrice()->getCurrency()) {
             $rowTotal *= $this->getExchangeRate($lineItem->getPrice()->getCurrency(), $order->getCurrency());
         }
         $subtotalAmount += $rowTotal;
     }
     $subtotal->setAmount($subtotalAmount);
     $subtotal->setCurrency($order->getCurrency());
     return $subtotal;
 }
 /**
  * @param Order $order
  * @param array $offers
  * @param int $customQuantity
  */
 protected function assertOrderLineItems(Order $order, array $offers, $customQuantity)
 {
     $this->assertCount(count($offers), $order->getLineItems());
     foreach ($order->getLineItems() as $orderLineItem) {
         /** @var QuoteProductOffer $selectedOffer */
         foreach ($offers as $selectedOffer) {
             $quoteProduct = $selectedOffer->getQuoteProduct();
             if ($quoteProduct->getProduct()->getId() === $orderLineItem->getProduct()->getId()) {
                 if ($selectedOffer->isAllowIncrements()) {
                     $this->assertEquals($customQuantity, $orderLineItem->getQuantity());
                 } else {
                     $this->assertEquals($selectedOffer->getQuantity(), $orderLineItem->getQuantity());
                 }
             }
         }
     }
 }
예제 #12
0
 /**
  * @param Order $order
  * @param string $type
  * @param array $addresses
  *
  * @return null|string
  */
 protected function getDefaultAddressKey(Order $order, $type, array $addresses)
 {
     if (!$addresses) {
         return null;
     }
     $addresses = call_user_func_array('array_merge', array_values($addresses));
     $accountUser = $order->getAccountUser();
     $addressKey = null;
     /** @var AbstractDefaultTypedAddress $address */
     foreach ($addresses as $key => $address) {
         if ($address->hasDefault($type)) {
             $addressKey = $key;
             if ($address instanceof AccountUserAddress && $address->getFrontendOwner()->getId() === $accountUser->getId()) {
                 break;
             }
         }
     }
     return $addressKey;
 }
예제 #13
0
 /**
  * @param Order $order
  * @param Request $request
  *
  * @return array|RedirectResponse
  */
 protected function update(Order $order, Request $request)
 {
     if (!$order->getAccountUser()) {
         $accountUser = $this->get('oro_security.security_facade')->getLoggedUser();
         if (!$accountUser instanceof AccountUser) {
             throw new \InvalidArgumentException('Only AccountUser can create an Order.');
         }
         $order->setAccountUser($accountUser);
     }
     if ($order->getAccount()) {
         $paymentTerm = $this->get('orob2b_payment.provider.payment_term')->getPaymentTerm($order->getAccount());
         if ($paymentTerm) {
             $order->setPaymentTerm($paymentTerm);
         }
     }
     //TODO: set correct owner in task BB-929
     if (!$order->getOwner()) {
         $user = $this->getDoctrine()->getManagerForClass('OroUserBundle:User')->getRepository('OroUserBundle:User')->findOneBy([]);
         $order->setOwner($user);
     }
     $form = $this->createForm(FrontendOrderType::NAME, $order);
     $handler = new OrderHandler($form, $request, $this->getDoctrine()->getManagerForClass(ClassUtils::getClass($order)), $this->get('orob2b_order.provider.subtotals'));
     return $this->get('oro_form.model.update_handler')->handleUpdate($order, $form, function (Order $order) {
         return ['route' => 'orob2b_order_frontend_update', 'parameters' => ['id' => $order->getId()]];
     }, function (Order $order) {
         return ['route' => 'orob2b_order_frontend_view', 'parameters' => ['id' => $order->getId()]];
     }, $this->get('translator')->trans('orob2b.order.controller.order.saved.message'), $handler, function (Order $order, FormInterface $form, Request $request) {
         return ['form' => $form->createView(), 'entity' => $order, 'isWidgetContext' => (bool) $request->get('_wid', false), 'isShippingAddressGranted' => $this->getOrderAddressSecurityProvider()->isAddressGranted($order, AddressType::TYPE_SHIPPING), 'isBillingAddressGranted' => $this->getOrderAddressSecurityProvider()->isAddressGranted($order, AddressType::TYPE_BILLING), 'tierPrices' => $this->getTierPrices($order), 'matchedPrices' => $this->getMatchedPrices($order)];
     });
 }
 /**
  * @param Order $order
  */
 public function setOrderCurrency(Order $order)
 {
     if (!$order->getCurrency()) {
         $order->setCurrency($this->localeSettings->getCurrency());
     }
 }
 /**
  * @param Order $order
  * @param string $type
  *
  * @return bool
  */
 public function isAddressGranted(Order $order, $type)
 {
     return $this->isAccountAddressGranted($type, $order->getAccount()) || $this->isAccountUserAddressGranted($type, $order->getAccountUser());
 }
예제 #16
0
 public function testPreUpdate()
 {
     $order = new Order();
     $order->preUpdate();
     $this->assertInstanceOf('\\DateTime', $order->getUpdatedAt());
 }
 /**
  * @return array
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function submitDataProvider()
 {
     /** @var Product $product */
     $product = $this->getEntity('OroB2B\\Bundle\\ProductBundle\\Entity\\Product', 1, 'id');
     $date = \DateTime::createFromFormat('Y-m-d H:i:s', '2015-02-03 00:00:00', new \DateTimeZone('UTC'));
     $currency = 'USD';
     $order = new Order();
     $order->setCurrency($currency);
     return ['default' => ['options' => ['currency' => $currency], 'submittedData' => ['product' => 1, 'quantity' => 10, 'productUnit' => 'item', 'shipBy' => '2015-02-03', 'comment' => 'Comment'], 'expectedData' => (new OrderLineItem())->setProduct($product)->setQuantity(10)->setProductUnit($this->getEntity('OroB2B\\Bundle\\ProductBundle\\Entity\\ProductUnit', 'item', 'code'))->setPriceType(OrderLineItem::PRICE_TYPE_UNIT)->setShipBy($date)->setComment('Comment'), 'data' => null], 'restricted modifications' => ['options' => ['currency' => $currency], 'submittedData' => ['product' => 2, 'quantity' => 10, 'productUnit' => 'item', 'shipBy' => '2015-05-07', 'comment' => 'Comment'], 'expectedData' => (new OrderLineItem())->setOrder($order)->setFromExternalSource(true)->setProduct($product)->setQuantity(5)->setProductUnit($this->getEntity('OroB2B\\Bundle\\ProductBundle\\Entity\\ProductUnit', 'kg', 'code'))->setPriceType(OrderLineItem::PRICE_TYPE_UNIT)->setShipBy($date)->setComment('Comment'), 'data' => (new OrderLineItem())->setOrder($order)->setFromExternalSource(true)->setProduct($product)->setQuantity(5)->setProductUnit($this->getEntity('OroB2B\\Bundle\\ProductBundle\\Entity\\ProductUnit', 'kg', 'code'))->setPriceType(OrderLineItem::PRICE_TYPE_UNIT)->setShipBy($date)->setComment('Comment'), 'choices' => []], 'modifications with choices' => ['options' => ['currency' => $currency], 'submittedData' => ['product' => 2, 'quantity' => 10, 'productUnit' => 'item', 'shipBy' => '2015-05-07', 'comment' => 'Comment'], 'expectedData' => (new OrderLineItem())->setOrder($order)->setFromExternalSource(true)->setProduct($product)->setQuantity(5)->setProductUnit($this->getEntity('OroB2B\\Bundle\\ProductBundle\\Entity\\ProductUnit', 'item', 'code'))->setPriceType(OrderLineItem::PRICE_TYPE_UNIT)->setShipBy($date)->setComment('Comment'), 'data' => (new OrderLineItem())->setOrder($order)->setFromExternalSource(true)->setProduct($product)->setQuantity(5)->setProductUnit($this->getEntity('OroB2B\\Bundle\\ProductBundle\\Entity\\ProductUnit', 'item', 'code'))->setPriceType(OrderLineItem::PRICE_TYPE_UNIT)->setShipBy($date)->setComment('Comment'), 'choices' => [$this->getEntity('OroB2B\\Bundle\\ProductBundle\\Entity\\ProductUnit', 'item', 'code')]], 'free form' => ['options' => ['currency' => $currency], 'submittedData' => ['product' => null, 'quantity' => 10, 'productUnit' => 'item', 'comment' => 'Comment Updated'], 'expectedData' => (new OrderLineItem())->setOrder($order)->setFromExternalSource(false)->setProductSku('SKU')->setFreeFormProduct('Service')->setQuantity(10)->setProductUnit($this->getEntity('OroB2B\\Bundle\\ProductBundle\\Entity\\ProductUnit', 'item', 'code'))->setPriceType(OrderLineItem::PRICE_TYPE_UNIT)->setComment('Comment Updated'), 'data' => (new OrderLineItem())->setOrder($order)->setFromExternalSource(false)->setProductSku('SKU')->setFreeFormProduct('Service')->setQuantity(5)->setProductUnit($this->getEntity('OroB2B\\Bundle\\ProductBundle\\Entity\\ProductUnit', 'item', 'code'))->setPriceType(OrderLineItem::PRICE_TYPE_UNIT)->setComment('Comment'), 'choices' => [$this->getEntity('OroB2B\\Bundle\\ProductBundle\\Entity\\ProductUnit', 'item', 'code')]]];
 }
예제 #18
0
 /**
  * @param Order $order
  * @return int|null
  */
 protected function getAccountGroupPaymentTermId(Order $order)
 {
     $account = $order->getAccount();
     if (!$account || !$account->getGroup()) {
         return null;
     }
     $paymentTerm = $this->paymentTermProvider->getAccountGroupPaymentTerm($account->getGroup());
     return $paymentTerm ? $paymentTerm->getId() : null;
 }
 /**
  * @param Order $order
  * @param string $type
  * @return array
  */
 public function getGroupedAddresses(Order $order, $type)
 {
     $addresses = [];
     $account = $order->getAccount();
     if ($account) {
         $accountAddresses = $this->orderAddressProvider->getAccountAddresses($account, $type);
         foreach ($accountAddresses as $accountAddress) {
             $addresses[self::ACCOUNT_LABEL][$this->getIdentifier($accountAddress)] = $accountAddress;
         }
     }
     $accountUser = $order->getAccountUser();
     if ($accountUser) {
         $accountUserAddresses = $this->orderAddressProvider->getAccountUserAddresses($accountUser, $type);
         if ($accountUserAddresses) {
             foreach ($accountUserAddresses as $accountUserAddress) {
                 $addresses[self::ACCOUNT_USER_LABEL][$this->getIdentifier($accountUserAddress)] = $accountUserAddress;
             }
         }
     }
     return $addresses;
 }
예제 #20
0
 /**
  * @param Order $order
  * @param Request $request
  *
  * @return array|RedirectResponse
  */
 protected function update(Order $order, Request $request)
 {
     if (in_array($request->getMethod(), ['POST', 'PUT'], true)) {
         $order->setAccount($this->getOrderHandler()->getAccount());
         $order->setAccountUser($this->getOrderHandler()->getAccountUser());
     }
     $form = $this->createForm(OrderType::NAME, $order);
     $handler = new OrderHandler($form, $request, $this->getDoctrine()->getManagerForClass(ClassUtils::getClass($order)), $this->get('orob2b_order.provider.subtotals'));
     return $this->get('oro_form.model.update_handler')->handleUpdate($order, $form, function (Order $order) {
         return ['route' => 'orob2b_order_update', 'parameters' => ['id' => $order->getId()]];
     }, function (Order $order) {
         return ['route' => 'orob2b_order_view', 'parameters' => ['id' => $order->getId()]];
     }, $this->get('translator')->trans('orob2b.order.controller.order.saved.message'), $handler, function (Order $order, FormInterface $form, Request $request) {
         return ['form' => $form->createView(), 'entity' => $order, 'isWidgetContext' => (bool) $request->get('_wid', false), 'isShippingAddressGranted' => $this->getOrderAddressSecurityProvider()->isAddressGranted($order, AddressType::TYPE_SHIPPING), 'isBillingAddressGranted' => $this->getOrderAddressSecurityProvider()->isAddressGranted($order, AddressType::TYPE_BILLING), 'tierPrices' => $this->getTierPrices($order), 'matchedPrices' => $this->getMatchedPrices($order)];
     });
 }