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.
*

View File

@@ -87,6 +87,8 @@
{%- set logLineClass = '' -%}
{%- if '.CRITICAL' in logLine -%}
{%- set logLineClass = 'text-danger text-bold' -%}
{%- elseif '.WARNING' in logLine -%}
{%- set logLineClass = 'text-warning text-bold' -%}
{%- elseif '.ERROR' in logLine -%}
{%- set logLineClass = 'text-warning text-bold' -%}
{%- elseif '.DEBUG' in logLine -%}

View File

@@ -0,0 +1,7 @@
{% set json = model.toArray %}
{% set items = [] %}
{% for entry in model.calculator.entries %}
{% set items = items|merge([model.itemToArray(entry)]) %}
{% endfor %}
{% set json = json|merge({'items': items}) %}
{{ json|json_encode|raw}}

View File

@@ -0,0 +1,19 @@
{% for key, value in model.toArray %}
{{ key }}:
{% if value is not empty %}
{{ value|multiline_indent(' ') }}
{% endif %}
{% endfor %}
{% for entry in model.calculator.entries %}
---
{% set items = model.itemToArray(entry) %}
{% for key, value in items %}
{{ key }}:
{% if value is not empty %}
{{ value|multiline_indent(' ') }}
{% endif %}
{% endfor %}
{% endfor %}

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<kimai version="{{ constant('App\\Constants::VERSION') }}">
{% for key, value in model.toArray %}
<{{ key }}{% if value is empty %}/>{% else %}>{{ value|escape }}</{{ key }}>{% endif %}
{% endfor %}
<items>
{%- for entry in model.calculator.entries %}
<item>
{% for key, value in model.itemToArray(entry) %}
<{{ key }}{% if value is empty %}/>{% else %}>{{ value|escape }}</{{ key }}>{% endif %}
{% endfor %}
</item>
{%- endfor %}
</items>
</kimai>

View File

@@ -77,20 +77,20 @@ class InvoiceCreateCommandTest extends KernelTestCase
}
/**
'user'
'start'
'end'
'timezone'
'customer'
'template'
'search'
'exported'
'by-customer'
'by-project'
'set-exported'
'template-meta'
* @param $user
* @param array $params
* Allowed option: user
* Allowed option: start
* Allowed option: end
* Allowed option: timezone
* Allowed option: customer
* Allowed option: template
* Allowed option: search
* Allowed option: exported
* Allowed option: by-customer
* Allowed option: by-project
* Allowed option: set-exported
* Allowed option: template-meta
*
* @param array $options
* @return CommandTester
*/
protected function createInvoice(array $options = [])

View File

@@ -51,47 +51,47 @@ class ConfigurableNumberGeneratorTest extends TestCase
public function getTestData()
{
$timestamp = time();
$invoiceDate = new \DateTime();
return [
// simple tests for single calls
['{date}', date('ymd'), $timestamp],
['{Y}', date('Y'), $timestamp],
['{y}', date('y'), $timestamp],
['{M}', date('m'), $timestamp],
['{m}', date('n'), $timestamp],
['{D}', date('d'), $timestamp],
['{d}', date('j'), $timestamp],
['{c}', '2', $timestamp],
['{cy}', '2', $timestamp],
['{cm}', '2', $timestamp],
['{cd}', '2', $timestamp],
['{date}', $invoiceDate->format('ymd'), $invoiceDate],
['{Y}', $invoiceDate->format('Y'), $invoiceDate],
['{y}', $invoiceDate->format('y'), $invoiceDate],
['{M}', $invoiceDate->format('m'), $invoiceDate],
['{m}', $invoiceDate->format('n'), $invoiceDate],
['{D}', $invoiceDate->format('d'), $invoiceDate],
['{d}', $invoiceDate->format('j'), $invoiceDate],
['{c}', '2', $invoiceDate],
['{cy}', '2', $invoiceDate],
['{cm}', '2', $invoiceDate],
['{cd}', '2', $invoiceDate],
// number formatting (not testing the lower case versions, as the tests might break depending on the date)
['{date,10}', '0000' . date('ymd'), $timestamp],
['{Y,6}', '00' . date('Y'), $timestamp],
['{M,3}', '0' . date('m'), $timestamp],
['{D,3}', '0' . date('d'), $timestamp],
['{c,2}', '02', $timestamp],
['{cy,2}', '02', $timestamp],
['{cm,2}', '02', $timestamp],
['{cd,2}', '02', $timestamp],
['{date,10}', '0000' . $invoiceDate->format('ymd'), $invoiceDate],
['{Y,6}', '00' . $invoiceDate->format('Y'), $invoiceDate],
['{M,3}', '0' . $invoiceDate->format('m'), $invoiceDate],
['{D,3}', '0' . $invoiceDate->format('d'), $invoiceDate],
['{c,2}', '02', $invoiceDate],
['{cy,2}', '02', $invoiceDate],
['{cm,2}', '02', $invoiceDate],
['{cd,2}', '02', $invoiceDate],
// mixing identifiers
['{Y}{cy}', date('Y') . '2', $timestamp],
['{Y}{cy}{m}', date('Y') . '2' . date('n'), $timestamp],
['{Y}-{cy}/{m}', date('Y') . '-2/' . date('n'), $timestamp],
['{Y}-{cy}/{m}', date('Y') . '-2/' . date('n'), $timestamp],
['{Y,5}/{cy,5}', '0' . date('Y') . '/00002', $timestamp],
['{Y}{cy}', $invoiceDate->format('Y') . '2', $invoiceDate],
['{Y}{cy}{m}', $invoiceDate->format('Y') . '2' . $invoiceDate->format('n'), $invoiceDate],
['{Y}-{cy}/{m}', $invoiceDate->format('Y') . '-2/' . $invoiceDate->format('n'), $invoiceDate],
['{Y}-{cy}/{m}', $invoiceDate->format('Y') . '-2/' . $invoiceDate->format('n'), $invoiceDate],
['{Y,5}/{cy,5}', '0' . $invoiceDate->format('Y') . '/00002', $invoiceDate],
];
}
/**
* @dataProvider getTestData
*/
public function testGetInvoiceNumber(string $format, string $expectedInvoiceNumber, int $timestamp)
public function testGetInvoiceNumber(string $format, string $expectedInvoiceNumber, \DateTime $invoiceDate)
{
$sut = $this->getSut($format);
$model = new InvoiceModel(new DebugFormatter());
$model->setInvoiceDate((new \DateTime())->setTimestamp($timestamp));
$model->setInvoiceDate($invoiceDate);
$sut->setModel($model);
$this->assertEquals($expectedInvoiceNumber, $sut->getInvoiceNumber());

View File

@@ -0,0 +1,77 @@
<?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\Tests\Invoice\Renderer;
use App\Invoice\Renderer\JsonRenderer;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
/**
* @covers \App\Invoice\Renderer\JsonRenderer
* @group integration
*/
class JsonRendererTest extends KernelTestCase
{
use RendererTestTrait;
public function testSupports()
{
$loader = new FilesystemLoader();
$env = new Environment($loader);
$sut = new JsonRenderer($env);
$this->assertFalse($sut->supports($this->getInvoiceDocument('default.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('freelancer.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('timesheet.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('foo.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('export.csv')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('text.txt.twig')));
$this->assertTrue($sut->supports($this->getInvoiceDocument('javascript.json.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('xml.xml.twig')));
}
public function testRender()
{
$kernel = self::bootKernel();
/** @var Environment $twig */
$twig = $kernel->getContainer()->get('twig');
$stack = $kernel->getContainer()->get('request_stack');
$request = new Request();
$request->setLocale('en');
$stack->push($request);
/** @var FilesystemLoader $loader */
$loader = $twig->getLoader();
$loader->addPath($this->getInvoiceTemplatePath(), 'invoice');
$sut = new JsonRenderer($twig);
$model = $this->getInvoiceModel();
$document = $this->getInvoiceDocument('javascript.json.twig');
$response = $sut->render($document, $model);
self::assertEquals('application/json', $response->headers->get('Content-Type'));
$content = $response->getContent();
$json = json_decode($content, true);
$expected = $model->toArray();
$expected['items'] = [];
foreach ($model->getCalculator()->getEntries() as $entry) {
$expected['items'][] = $model->itemToArray($entry);
}
self::assertEquals($expected, $json);
}
}

View File

@@ -0,0 +1,79 @@
<?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\Tests\Invoice\Renderer;
use App\Invoice\Renderer\TextRenderer;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
/**
* @covers \App\Invoice\Renderer\TextRenderer
* @group integration
*/
class TextRendererTest extends KernelTestCase
{
use RendererTestTrait;
public function testSupports()
{
$loader = new FilesystemLoader();
$env = new Environment($loader);
$sut = new TextRenderer($env);
$this->assertFalse($sut->supports($this->getInvoiceDocument('default.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('freelancer.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('timesheet.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('foo.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('export.csv')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods')));
$this->assertTrue($sut->supports($this->getInvoiceDocument('text.txt.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('javascript.json.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('xml.xml.twig')));
}
public function testRender()
{
$kernel = self::bootKernel();
/** @var Environment $twig */
$twig = $kernel->getContainer()->get('twig');
$stack = $kernel->getContainer()->get('request_stack');
$request = new Request();
$request->setLocale('en');
$stack->push($request);
/** @var FilesystemLoader $loader */
$loader = $twig->getLoader();
$loader->addPath($this->getInvoiceTemplatePath(), 'invoice');
$sut = new TextRenderer($twig);
$model = $this->getInvoiceModel();
$document = $this->getInvoiceDocument('text.txt.twig');
$response = $sut->render($document, $model);
self::assertEquals('text/plain', $response->headers->get('Content-Type'));
$content = $response->getContent();
foreach ($model->toArray() as $key => $value) {
if (null === $value || '' === $value) {
self::assertStringContainsString(sprintf("%s:\n\n", $key), $content);
} else {
self::assertStringContainsString(sprintf("%s:\n %s", $key, explode("\n", $value)[0]), $content);
}
}
self::assertEquals(count($model->getCalculator()->getEntries()), substr_count($content, PHP_EOL . '---' . PHP_EOL));
}
}

View File

@@ -0,0 +1,83 @@
<?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\Tests\Invoice\Renderer;
use App\Invoice\Renderer\XmlRenderer;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
/**
* @covers \App\Invoice\Renderer\XmlRenderer
* @group integration
*/
class XmlRendererTest extends KernelTestCase
{
use RendererTestTrait;
public function testSupports()
{
$loader = new FilesystemLoader();
$env = new Environment($loader);
$sut = new XmlRenderer($env);
$this->assertFalse($sut->supports($this->getInvoiceDocument('default.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('freelancer.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('timesheet.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('javascript.json.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('foo.html.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('company.docx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('export.csv')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('spreadsheet.xlsx')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('open-spreadsheet.ods')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('text.txt.twig')));
$this->assertFalse($sut->supports($this->getInvoiceDocument('javascript.json.twig')));
$this->assertTrue($sut->supports($this->getInvoiceDocument('xml.xml.twig')));
}
public function testRender()
{
$kernel = self::bootKernel();
/** @var Environment $twig */
$twig = $kernel->getContainer()->get('twig');
$stack = $kernel->getContainer()->get('request_stack');
$request = new Request();
$request->setLocale('en');
$stack->push($request);
/** @var FilesystemLoader $loader */
$loader = $twig->getLoader();
$loader->addPath($this->getInvoiceTemplatePath(), 'invoice');
$sut = new XmlRenderer($twig);
$model = $this->getInvoiceModel();
$document = $this->getInvoiceDocument('xml.xml.twig');
$response = $sut->render($document, $model);
self::assertEquals('application/xml', $response->headers->get('Content-Type'));
$content = $response->getContent();
$xml = new \SimpleXMLElement($content);
$expected = $model->toArray();
foreach ($xml as $element) {
$name = $element->getName();
if ($name === 'items') {
continue;
}
self::assertEquals((string) $expected[$name], (string) $element);
}
self::assertEquals(count($model->getCalculator()->getEntries()), count($xml->items->item));
}
}

View File

@@ -24,7 +24,7 @@ use Symfony\Component\HttpKernel\HttpKernelBrowser;
trait KernelTestTrait
{
/**
* @param $client HttpKernelBrowser|EntityManager|KernelTestCase
* @param HttpKernelBrowser|EntityManager|KernelTestCase $client
* @param Fixture $fixture
*/
protected function importFixture($client, Fixture $fixture)

View File

@@ -27,6 +27,9 @@ class InvoiceDocumentRepositoryTest extends TestCase
'default.html.twig',
'freelancer.html.twig',
'timesheet.html.twig',
'text.txt.twig',
'javascript.json.twig',
'xml.xml.twig',
];
public function testWithEmptyDirectory()

View File

@@ -50,7 +50,7 @@ class ExtensionsTest extends TestCase
public function testGetFilters()
{
$filters = ['duration', 'duration_decimal', 'money', 'currency', 'country', 'language', 'amount', 'docu_link'];
$filters = ['duration', 'duration_decimal', 'money', 'currency', 'country', 'language', 'amount', 'docu_link', 'multiline_indent'];
$sut = $this->getSut($this->localeDe);
$twigFilters = $sut->getFilters();
$this->assertCount(count($filters), $twigFilters);
@@ -305,4 +305,43 @@ class ExtensionsTest extends TestCase
$this->assertNull($sut->getClassName(null));
$this->assertEquals('App\Entity\User', $sut->getClassName(new User()));
}
public function getMultilineTestData()
{
return [
[' ', null, ['']],
[' ', '', ['']],
[' ', 0, [' 0']],
[' ', 'sdfsdf
sdfsdf
aksljdfh laksjd hflka sjhdf lakjhsdflak jsdfh
dfsdfsdfsdfsdf',
[' sdfsdf', ' sdfsdf', ' ', ' aksljdfh laksjd hflka sjhdf lakjhsdflak jsdfh', ' dfsdfsdfsdfsdf']
],
['###', 'sdfsdf' . PHP_EOL .
'sdfsdf' . PHP_EOL .
'' . PHP_EOL .
' aksljdfh laksjd hflka sjhdf lakjhsdflak jsdfh' . PHP_EOL .
'dfsdfsdfsdfsdf',
['###sdfsdf', '###sdfsdf', '###', '### aksljdfh laksjd hflka sjhdf lakjhsdflak jsdfh', '###dfsdfsdfsdfsdf']
],
[' ', 'sdfsdf' . "\r\n" .
'sdfsdf' . "\r\n" .
'' . "\r\n" .
' aksljdfh laksjd hflka sjhdf lakjhsdflak jsdfh' . "\r\n" .
'dfsdfsdfsdfsdf',
[' sdfsdf', ' sdfsdf', ' ', ' aksljdfh laksjd hflka sjhdf lakjhsdflak jsdfh', ' dfsdfsdfsdfsdf']
],
];
}
/**
* @dataProvider getMultilineTestData
*/
public function testMultilineIndent($indent, $string, $expected)
{
$sut = $this->getSut($this->localeEn);
self::assertEquals(implode("\n", $expected), $sut->multilineIndent($string, $indent));
}
}