Skip to content

Commit

Permalink
added json, xml and txt invoice renderer (#1576)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed Mar 21, 2020
1 parent c89750f commit 1ca1b00
Show file tree
Hide file tree
Showing 19 changed files with 560 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/Constants.php
Expand Up @@ -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
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Form/Type/LanguageType.php
Expand Up @@ -48,7 +48,8 @@ public function configureOptions(OptionsResolver $resolver)
}

$resolver->setDefaults([
'choices' => $choices
'choices' => $choices,
'label' => 'label.language',
]);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Invoice/NumberGenerator/ConfigurableNumberGenerator.php
Expand Up @@ -75,31 +75,31 @@ public function getInvoiceNumber(): string

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':
Expand Down
53 changes: 53 additions & 0 deletions src/Invoice/Renderer/JsonRenderer.php
@@ -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;
}
}
53 changes: 53 additions & 0 deletions src/Invoice/Renderer/TextRenderer.php
@@ -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;
}
}
53 changes: 53 additions & 0 deletions src/Invoice/Renderer/XmlRenderer.php
@@ -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;
}
}
19 changes: 19 additions & 0 deletions src/Twig/Extensions.php
Expand Up @@ -71,6 +71,7 @@ public function getFilters()
new TwigFilter('language', [$this, 'language']),
new TwigFilter('amount', [$this, 'amount']),
new TwigFilter('docu_link', [$this, 'documentationLink']),
new TwigFilter('multiline_indent', [$this, 'multilineIndent']),
];
}

Expand Down Expand Up @@ -98,6 +99,24 @@ public function getClassName($object)
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.
*
Expand Down
2 changes: 2 additions & 0 deletions templates/doctor/index.html.twig
Expand Up @@ -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 -%}
Expand Down
7 changes: 7 additions & 0 deletions templates/invoice/renderer/javascript.json.twig
@@ -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}}
19 changes: 19 additions & 0 deletions templates/invoice/renderer/text.txt.twig
@@ -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 %}
19 changes: 19 additions & 0 deletions templates/invoice/renderer/xml.xml.twig
@@ -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>
28 changes: 14 additions & 14 deletions tests/Command/InvoiceCreateCommandTest.php
Expand Up @@ -77,20 +77,20 @@ protected function setUp(): void
}

/**
'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 = [])
Expand Down

0 comments on commit 1ca1b00

Please sign in to comment.