/**
  * @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;
 }
 /**
  * @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;
 }
 /**
  * @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
  * @param string $type
  *
  * @return bool
  */
 public function isAddressGranted(Order $order, $type)
 {
     return $this->isAccountAddressGranted($type, $order->getAccount()) || $this->isAccountUserAddressGranted($type, $order->getAccountUser());
 }