public function generateAction(Request $request, $order_id)
    {
        $manager = $this->getDoctrine()->getManager();
        $repo = $manager->getRepository('LibrinfoEcommerceBundle:Order');

        $order = $repo->find($order_id);
        if (!$order) {
            throw $this->createNotFoundException(sprintf('Unable to find the order with id : %s', $order_id));
        }

        $this->admin->checkAccess('create');

        if ($order->getCheckoutState() != 'completed') {
            throw new \Exception('Checkout is not completed');
        }

        if (count($order->getInvoices()) > 0) {
            throw new \Exception('Invoice has already been generated');
        }

        $invoiceFactory = $this->container->get('librinfo_ecommerce.factory.invoice');
        $invoice = $invoiceFactory->createForOrder($order);
        $manager->persist($invoice);
        $manager->flush();

        $order->addInvoice($invoice);

        $html = $this->renderView('LibrinfoEcommerceBundle:OrderAdmin/Show:_invoices_inner.html.twig', ['object' => $order]);

        return new JsonResponse(['html' => $html]);
    }