added json, xml and txt invoice renderer (#1576)

This commit is contained in:
Kevin Papst
2020-03-21 01:01:47 +01:00
committed by GitHub
parent c89750fe94
commit 1ca1b00d11
19 changed files with 560 additions and 53 deletions

View File

@@ -17,11 +17,11 @@ class Constants
/**
* The current release version
*/
public const VERSION = '1.8';
public const VERSION = '1.9';
/**
* The current release status, either "stable" or "dev"
*/
public const STATUS = 'stable';
public const STATUS = 'dev';
/**
* The software name
*/

View File

@@ -48,7 +48,8 @@ class LanguageType extends AbstractType
}
$resolver->setDefaults([
'choices' => $choices
'choices' => $choices,
'label' => 'label.language',
]);
}

View File

@@ -75,31 +75,31 @@ final class ConfigurableNumberGenerator implements NumberGeneratorInterface
switch ($tmp) {
case 'Y':
$partialResult = date('Y', $timestamp);
$partialResult = $invoiceDate->format('Y');
break;
case 'y':
$partialResult = date('y', $timestamp);
$partialResult = $invoiceDate->format('y');
break;
case 'M':
$partialResult = date('m', $timestamp);
$partialResult = $invoiceDate->format('m');
break;
case 'm':
$partialResult = date('n', $timestamp);
$partialResult = $invoiceDate->format('n');
break;
case 'D':
$partialResult = date('d', $timestamp);
$partialResult = $invoiceDate->format('d');
break;
case 'd':
$partialResult = date('j', $timestamp);
$partialResult = $invoiceDate->format('j');
break;
case 'date':
$partialResult = date('ymd', $timestamp);
$partialResult = $invoiceDate->format('ymd');
break;
case 'c':

View File

@@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Invoice\InvoiceFilename;
use App\Invoice\InvoiceModel;
use App\Invoice\RendererInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Twig\Environment;
final class JsonRenderer implements RendererInterface
{
/**
* @var Environment
*/
private $twig;
public function __construct(Environment $twig)
{
$this->twig = $twig;
}
public function supports(InvoiceDocument $document): bool
{
return stripos($document->getFilename(), '.json.twig') !== false;
}
public function render(InvoiceDocument $document, InvoiceModel $model): Response
{
$content = $this->twig->render('@invoice/' . basename($document->getFilename()), [
'model' => $model
]);
$filename = (string) new InvoiceFilename($model);
$response = new Response($content);
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename . '.json');
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Content-Disposition', $disposition);
return $response;
}
}

View File

@@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Invoice\InvoiceFilename;
use App\Invoice\InvoiceModel;
use App\Invoice\RendererInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Twig\Environment;
final class TextRenderer implements RendererInterface
{
/**
* @var Environment
*/
private $twig;
public function __construct(Environment $twig)
{
$this->twig = $twig;
}
public function supports(InvoiceDocument $document): bool
{
return stripos($document->getFilename(), '.txt.twig') !== false;
}
public function render(InvoiceDocument $document, InvoiceModel $model): Response
{
$content = $this->twig->render('@invoice/' . basename($document->getFilename()), [
'model' => $model
]);
$filename = (string) new InvoiceFilename($model);
$response = new Response($content);
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename . '.txt');
$response->headers->set('Content-Type', 'text/plain');
$response->headers->set('Content-Disposition', $disposition);
return $response;
}
}

View File

@@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Invoice\InvoiceFilename;
use App\Invoice\InvoiceModel;
use App\Invoice\RendererInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Twig\Environment;
final class XmlRenderer implements RendererInterface
{
/**
* @var Environment
*/
private $twig;
public function __construct(Environment $twig)
{
$this->twig = $twig;
}
public function supports(InvoiceDocument $document): bool
{
return stripos($document->getFilename(), '.xml.twig') !== false;
}
public function render(InvoiceDocument $document, InvoiceModel $model): Response
{
$content = $this->twig->render('@invoice/' . basename($document->getFilename()), [
'model' => $model
]);
$filename = (string) new InvoiceFilename($model);
$response = new Response($content);
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename . '.xml');
$response->headers->set('Content-Type', 'application/xml');
$response->headers->set('Content-Disposition', $disposition);
return $response;
}
}

View File

@@ -71,6 +71,7 @@ class Extensions extends AbstractExtension
new TwigFilter('language', [$this, 'language']),
new TwigFilter('amount', [$this, 'amount']),
new TwigFilter('docu_link', [$this, 'documentationLink']),
new TwigFilter('multiline_indent', [$this, 'multilineIndent']),
];
}
@@ -98,6 +99,24 @@ class Extensions extends AbstractExtension
return get_class($object);
}
public function multilineIndent(?string $string, string $indent): string
{
if (null === $string || '' === $string) {
return '';
}
$parts = explode("\r\n", $string);
if (count($parts) === 1) {
$parts = explode("\n", $string);
}
$parts = array_map(function ($part) use ($indent) {
return $indent . $part;
}, $parts);
return implode("\n", $parts);
}
/**
* Transforms seconds into a duration string.
*