implement pdf invoice generation workflow

This commit is contained in:
Manuel Raynaud
2013-10-02 16:33:43 +02:00
parent 0116cb2891
commit df08809a48
8 changed files with 75 additions and 7 deletions

View File

@@ -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)
);
}

View File

@@ -111,6 +111,11 @@
<tag name="kernel.event_subscriber"/>
</service>
<service id="thelia.action.pdf" class="Thelia\Action\Pdf">
<argument type="service" id="service_container"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
</config>

View File

@@ -150,6 +150,11 @@
<default key="_controller">Thelia\Controller\Admin\OrderController::updateAddress</default>
</route>
<route id="admin.order.pdf.invoice" path="/admin/order/pdf/invoice/{order_id}">
<default key="_controller">Thelia\Controller\Admin\OrderController::generateInvoicePdf</default>
<requirement key="order_id">\d+</requirement>
</route>
<!-- end order management -->
<!-- Categories management -->

View File

@@ -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) {

View File

@@ -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
))));
}
}

View File

@@ -65,4 +65,9 @@ class PdfEvent extends ActionEvent
{
return $this->pdf;
}
public function hasPdf()
{
return null !== $this->pdf;
}
}

View File

@@ -193,7 +193,7 @@
<caption class="clearfix">
{intl l='Invoice informations'}
<div class="pull-right">
<a class="btn btn-default btn-primary" title="{intl l='Download invoice as PDF'}" href="#">
<a class="btn btn-default btn-primary" title="{intl l='Download invoice as PDF'}" href="{url path="/admin/order/pdf/invoice/$ID"}">
<span class="glyphicon glyphicon-cloud-download"></span> {intl l='PDF | Invoice'}
</a>
<a class="btn btn-default btn-primary js-update-order-address" data-address-id="{$INVOICE_ADDRESS}" title="{intl l='Edit invoice address'}" href="#edit_order_address_dialog" data-toggle="modal">

View File

@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>REF</title>
</head>
<body>
generate invoice
</body>
</html>