/**
  * 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;
 }
 /**
  * @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)];
     });
 }