InvoiceController.php in Commerce Invoice 8.2
File
src/Controller/InvoiceController.php
View source
<?php
namespace Drupal\commerce_invoice\Controller;
use Drupal\commerce_invoice\Entity\InvoiceInterface;
use Drupal\commerce_invoice\Entity\InvoiceTypeInterface;
use Drupal\commerce_invoice\InvoiceFileManagerInterface;
use Drupal\commerce_invoice\InvoiceGeneratorInterface;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_price\Price;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class InvoiceController extends ControllerBase implements ContainerInjectionInterface {
use DependencySerializationTrait;
protected $configFactory;
protected $invoiceFileManager;
protected $invoiceGenerator;
public function __construct(ConfigFactoryInterface $config_factory, InvoiceFileManagerInterface $invoice_file_manager, InvoiceGeneratorInterface $invoice_generator) {
$this->configFactory = $config_factory;
$this->invoiceFileManager = $invoice_file_manager;
$this->invoiceGenerator = $invoice_generator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('commerce_invoice.invoice_file_manager'), $container
->get('commerce_invoice.invoice_generator'));
}
public function title(InvoiceInterface $commerce_invoice) {
return $this
->t('@invoice_type #@invoice_number', [
'@invoice_type' => $commerce_invoice
->get('type')->entity
->label(),
'@invoice_number' => $commerce_invoice
->label(),
]);
}
public function addForm(OrderInterface $commerce_order, InvoiceTypeInterface $commerce_invoice_type) {
$invoice = $this->invoiceGenerator
->generate([
$commerce_order,
], $commerce_order
->getStore(), $commerce_order
->getBillingProfile(), [
'type' => $commerce_invoice_type
->id(),
], FALSE);
$total_paid = new Price('0', $commerce_order
->getTotalPrice()
->getCurrencyCode());
$invoice
->setTotalPaid($total_paid);
return $this
->entityFormBuilder()
->getForm($invoice);
}
public function download(RouteMatchInterface $route_match) {
$invoice = $route_match
->getParameter('commerce_invoice');
$file = $this->invoiceFileManager
->getInvoiceFile($invoice);
$config = $this->configFactory
->get('entity_print.settings');
$content_disposition = $config
->get('force_download') ? 'attachment' : NULL;
$headers = file_get_content_headers($file);
return new BinaryFileResponse($file
->getFileUri(), 200, $headers, FALSE, $content_disposition);
}
}