/**
  * @EXT\Route(
  *     "/shared/workspace/create",
  *     name="formalibre_admin_shared_workspace_create",
  *     options={"expose"=true}
  * )
  * @EXT\ParamConverter("authenticatedUser", options={"authenticatedUser" = true})
  * @EXT\Template("FormaLibreInvoiceBundle:AdminSharedWorkspaces:sharedWorkspaceCreateForm.html.twig")
  */
 public function sharedWorkspaceCreateAction()
 {
     $products = $this->productManager->getProductsBy(array('type' => 'SHARED_WS'), array('code' => 'ASC'));
     $product = count($products) > 0 ? $products[0] : null;
     $workspaces = $this->sharedWorkspaceManager->getNonPersonalRemoteWorkspacesDatas();
     $sharedWorkspace = new SharedWorkspace();
     $form = $this->formFactory->create(new SharedWorkspaceType($this->translator, $product, $workspaces), $sharedWorkspace);
     $form->handleRequest($this->request);
     if ($form->isValid()) {
         $owner = $sharedWorkspace->getOwner();
         $remoteUser = $this->sharedWorkspaceManager->getRemoteUser($owner->getUsername());
         if (isset($remoteUser['error']['code'])) {
             $remoteUser = $this->sharedWorkspaceManager->createRemoteUser($owner);
         }
         $remoteWorkspaceId = $form->get('remoteWorkspace')->getData();
         if (!is_null($remoteWorkspaceId)) {
             $sharedWorkspace->setRemoteId($remoteWorkspaceId);
             $this->om->persist($sharedWorkspace);
             $datas = 'success';
         } else {
             $name = $form->get('name')->getData();
             $code = $form->get('code')->getData();
             $datas = $this->sharedWorkspaceManager->createRemoteWorkspace($sharedWorkspace, $owner, $name, $code);
         }
         if ($datas === 'success') {
             $product = $form->get('product')->getData();
             $priceSolutionId = $form->get('price')->getData();
             $priceSolutions = $product->getPriceSolutions();
             $priceSolution = $priceSolutions[$priceSolutionId];
             $chart = new Chart();
             $chart->setOwner($owner);
             $now = new \DateTime();
             $chart->setCreationDate($now);
             $chart->setValidationDate($now);
             $order = new Order();
             $order->setPriceSolution($priceSolution);
             $order->setProduct($product);
             $order->setSharedWorkspace($sharedWorkspace);
             $order->setChart($chart);
             $this->om->persist($order);
             $chart->addOrder($order);
             $this->om->persist($chart);
             $this->invoiceManager->create($chart, 'bank_transfer', true);
             return new RedirectResponse($this->router->generate('formalibre_admin_shared_workspaces_admin_tool_index'));
         } else {
             $form->addError(new FormError($this->translator->trans('probably_invalid_code', array(), 'invoice')));
         }
     }
     return array('form' => $form->createView());
 }
 /**
  * @EXT\Route(
  *      "/renew/test/workspace/{remoteId}",
  *      name="renew_test_workspace"
  * )
  * @EXT\Template
  *
  * @return Response
  */
 public function renewWorkspaceAction($remoteId)
 {
     $sws = $this->em->getRepository('FormaLibre\\InvoiceBundle\\Entity\\Product\\SharedWorkspace')->findOneByRemoteId($remoteId);
     if (!$sws) {
         throw new \Exception('unknown remote id');
     }
     if (!$this->authorization->isGranted('ROLE_ADMIN') && $this->tokenStorage->getToken()->getUser() !== $sws->getOwner()) {
         throw new AccessDeniedException();
     }
     $order = new Order();
     $chart = new Chart();
     $order->setChart($chart);
     $order->setSharedWorkspace($sws);
     $product = $this->sharedWorkspaceManager->getLastOrder($sws)->getProduct();
     $order->setProduct($product);
     $this->em->persist($chart);
     $this->em->persist($order);
     $this->em->flush();
     $formType = new SharedWorkspaceForm($product);
     $form = $this->createForm($formType)->createView();
     $workspace = $this->sharedWorkspaceManager->getWorkspaceData($sws);
     return array('form' => $form, 'chart' => $chart, 'product' => $product, 'order' => $order, 'workspace' => $workspace);
 }