public function create(Chart $chart, $paymentSystem = 'bank_transfer', $isPaid = false)
 {
     //if it already has an invoice, we don't create an other one...
     if ($chart->getInvoice()) {
         return $chart->getInvoice();
     }
     $invoice = new Invoice();
     $invoice->setChart($chart);
     $user = $chart->getOwner();
     $vatRate = $this->vatManager->getVatFromOwner($user) ? 0 : $this->vatManager->getVATRate($this->vatManager->getCountryCodeFromOwner($user));
     $invoice->setVatRate($vatRate);
     $netTotal = 0;
     foreach ($chart->getOrders() as $order) {
         $netTotal += $order->getPriceSolution()->getPrice() * $order->getQuantity();
     }
     $invoice->setAmount($netTotal);
     $invoice->setVatAmount($netTotal * $vatRate);
     $invoice->setTotalAmount($netTotal + $netTotal * $vatRate);
     $invoice->setInvoiceNumber($this->getInvoiceCode());
     $invoice->setPaymentSystemName($paymentSystem);
     $invoice->setIsPayed($isPaid);
     $chart->setInvoice($invoice);
     $this->om->persist($chart);
     $this->om->persist($invoice);
     $this->om->flush();
     return $invoice;
 }
 /**
  * @EXT\Route(
  *      "/payment_pending/chart/{chart}",
  *      name="chart_payment_pending"
  * )
  * @EXT\Template
  *
  * @return Response
  */
 public function pendingPaymentAction(Chart $chart)
 {
     if ($chart->getOwner() !== $this->tokenStorage->getToken()->getUser()) {
         throw new AccessDeniedException();
     }
     $chart->setExtendedData(array('communication' => $this->chartManager->getCommunication()));
     $extData = $chart->getExtendedData();
     $invoice = $this->invoiceManager->create($chart);
     $this->invoiceManager->send($invoice);
     $this->em->persist($chart);
     $this->em->flush();
     return array('communication' => $extData['communication'], 'chart' => $chart);
 }
 public function getChartVatRate(Chart $chart)
 {
     $vatManager = $this->container->get('formalibre.manager.vat_manager');
     $user = $chart->getOwner();
     $vatRate = $vatManager->getVatFromOwner($user) ? 0 : $vatManager->getVATRate($vatManager->getCountryCodeFromOwner($user));
     return $vatRate;
 }
 /**
  * @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(
  *      "/free_test/{product}",
  *      name="formalibre_free_test_workspace"
  * )
  * @return Response
  */
 public function createFreeTestWorkspace(Product $product)
 {
     if (!$this->authorization->isGranted('ROLE_USER')) {
         $redirectRoute = $this->router->generate('formalibre_free_test_workspace', array('product' => $product->getId()));
         $this->session->set('redirect_route', $redirectRoute);
         $route = $this->router->generate('claro_security_login', array());
         return new RedirectResponse($route);
     }
     $user = $this->tokenStorage->getToken()->getUser();
     if (!$this->sharedWorkspaceManager->hasFreeTestMonth($user)) {
         $content = $this->renderView('FormaLibreInvoiceBundle:errors:freeTestMonthUsedException.html.twig');
         return new Response($content);
     }
     $chart = new Chart();
     $ps = $this->productManager->getPriceSolution($product, 1);
     $order = new Order();
     $chart->setOwner($user);
     $chart->setIpAdress($_SERVER['REMOTE_ADDR']);
     $order->setPriceSolution($ps);
     $order->setProduct($product);
     $chart->addOrder($order);
     $order->setChart($chart);
     $this->em->persist($order);
     $this->em->persist($chart);
     $this->sharedWorkspaceManager->useFreeTestMonth($user);
     $invoice = $this->invoiceManager->create($chart, 'free');
     $this->invoiceManager->validate($invoice);
     $invoice->setPaymentSystemName('none');
     $this->em->persist($invoice);
     $this->em->flush();
     return new RedirectResponse($this->router->generate('claro_desktop_open', array()));
 }