diff --git a/core/lib/Thelia/Action/Pdf.php b/core/lib/Thelia/Action/Pdf.php index 82a6a86f4..e24626655 100644 --- a/core/lib/Thelia/Action/Pdf.php +++ b/core/lib/Thelia/Action/Pdf.php @@ -41,6 +41,7 @@ class Pdf extends BaseAction implements EventSubscriberInterface $domPdf = new \DOMPDF(); $domPdf->load_html($event->getContent()); + $domPdf->render(); $event->setPdf($domPdf->output()); } @@ -67,7 +68,7 @@ class Pdf extends BaseAction implements EventSubscriberInterface */ public static function getSubscribedEvents() { - array( + return array( TheliaEvents::GENERATE_PDF => array("generatePdf", 128) ); } diff --git a/core/lib/Thelia/Config/Resources/action.xml b/core/lib/Thelia/Config/Resources/action.xml index f153a8de0..87575f59b 100755 --- a/core/lib/Thelia/Config/Resources/action.xml +++ b/core/lib/Thelia/Config/Resources/action.xml @@ -111,6 +111,11 @@ + + + + + diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index a3ccc577d..f83874631 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -150,6 +150,11 @@ Thelia\Controller\Admin\OrderController::updateAddress + + Thelia\Controller\Admin\OrderController::generateInvoicePdf + \d+ + + diff --git a/core/lib/Thelia/Controller/Admin/BaseAdminController.php b/core/lib/Thelia/Controller/Admin/BaseAdminController.php index d80d8ce9f..2401617f3 100755 --- a/core/lib/Thelia/Controller/Admin/BaseAdminController.php +++ b/core/lib/Thelia/Controller/Admin/BaseAdminController.php @@ -187,12 +187,12 @@ class BaseAdminController extends BaseController /** * @return a ParserInterface instance parser */ - protected function getParser() + protected function getParser($template = null) { $parser = $this->container->get("thelia.parser"); // Define the template thant shoud be used - $parser->setTemplate(ConfigQuery::read('base_admin_template', 'admin/default')); + $parser->setTemplate($template ?: ConfigQuery::read('base_admin_template', 'admin/default')); return $parser; } @@ -378,10 +378,12 @@ class BaseAdminController extends BaseController * Render the given template, and returns the result as a string. * * @param $templateName the complete template name, with extension - * @param array $args the template arguments + * @param array $args the template arguments + * @param null $templateDir + * * @return \Symfony\Component\HttpFoundation\Response */ - protected function renderRaw($templateName, $args = array()) + protected function renderRaw($templateName, $args = array(), $templateDir = null) { // Add the template standard extension @@ -417,7 +419,7 @@ class BaseAdminController extends BaseController // Render the template. try { - $data = $this->getParser()->render($templateName, $args); + $data = $this->getParser($templateDir)->render($templateName, $args); return $data; } catch (AuthenticationException $ex) { diff --git a/core/lib/Thelia/Controller/Admin/OrderController.php b/core/lib/Thelia/Controller/Admin/OrderController.php index 77df4f9b3..7f1f0c56c 100644 --- a/core/lib/Thelia/Controller/Admin/OrderController.php +++ b/core/lib/Thelia/Controller/Admin/OrderController.php @@ -23,10 +23,13 @@ namespace Thelia\Controller\Admin; +use Symfony\Component\HttpFoundation\Response; use Thelia\Core\Event\Order\OrderAddressEvent; use Thelia\Core\Event\Order\OrderEvent; +use Thelia\Core\Event\PdfEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Form\OrderUpdateAddress; +use Thelia\Model\ConfigQuery; use Thelia\Model\Base\OrderAddressQuery; use Thelia\Model\OrderQuery; use Thelia\Model\OrderStatusQuery; @@ -193,4 +196,42 @@ class OrderController extends BaseAdminController $this->redirect(URL::getInstance()->absoluteUrl($this->getRoute("admin.order.update.view", $params))); } + + public function generateInvoicePdf($order_id) + { + if (null !== $response = $this->checkAuth("admin.order.update")) return $response; + + + $html = $this->renderRaw( + ConfigQuery::read('pdf_invoice_file', 'invoice'), + array( + 'order_id' => $order_id + ), + ConfigQuery::read('pdf_template', 'pdf') + ); + + $order = OrderQuery::create()->findPk($order_id); + + try { + $pdfEvent = new PdfEvent($html); + + $this->dispatch(TheliaEvents::GENERATE_PDF, $pdfEvent); + + if($pdfEvent->hasPdf()) { + return Response::create($pdfEvent->getPdf(), 200, + array( + 'Content-type' => "application/pdf", + 'Content-Disposition' => sprintf('Attachment;filename=%s.pdf', $order->getRef()), + )); + } + + } catch (\Exception $e) { + \Thelia\Log\Tlog::getInstance()->error(sprintf('error during generating invoice pdf for order id : %d', $order_id)); + + } + + $this->redirect(URL::getInstance()->absoluteUrl($this->getRoute("admin.order.update.view", array( + 'order_id' => $order_id + )))); + } } diff --git a/core/lib/Thelia/Core/Event/PdfEvent.php b/core/lib/Thelia/Core/Event/PdfEvent.php index ef8e68503..b65ba39a2 100644 --- a/core/lib/Thelia/Core/Event/PdfEvent.php +++ b/core/lib/Thelia/Core/Event/PdfEvent.php @@ -65,4 +65,9 @@ class PdfEvent extends ActionEvent { return $this->pdf; } + + public function hasPdf() + { + return null !== $this->pdf; + } } \ No newline at end of file diff --git a/templates/admin/default/order-edit.html b/templates/admin/default/order-edit.html index 76e4b2465..59c3d9cce 100644 --- a/templates/admin/default/order-edit.html +++ b/templates/admin/default/order-edit.html @@ -193,7 +193,7 @@ {intl l='Invoice informations'}
- + {intl l='PDF | Invoice'} diff --git a/templates/pdf/invoice.html b/templates/pdf/invoice.html new file mode 100644 index 000000000..e563f30b8 --- /dev/null +++ b/templates/pdf/invoice.html @@ -0,0 +1,9 @@ + + + + REF + + + generate invoice + + \ No newline at end of file