Skip to content

Commit

Permalink
Release v2.0 works with Kimai2 >= 2.0.22
Browse files Browse the repository at this point in the history
  • Loading branch information
arno1979 committed Jun 4, 2023
1 parent 483663d commit ecca9bf
Show file tree
Hide file tree
Showing 37 changed files with 318 additions and 877 deletions.
99 changes: 0 additions & 99 deletions Base/CustomHtmlBase.php

This file was deleted.

109 changes: 0 additions & 109 deletions Base/CustomPDFBase.php

This file was deleted.

7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ cd /kimai/var/plugins/
git clone https://github.com/arno1979/CustomExportBundle.git
```

And then rebuild the cache:
Copy the TTF files from the `Resources/fonts` folder to `var/data/fonts`

And then rebuild the cache:
```
cd /kimai/
bin/console cache:clear
bin/console cache:warmup
bin/console kimai:reload --env=prod
```

## Notes
Expand Down
27 changes: 0 additions & 27 deletions Renderer/CustomHtmlRenderer.php

This file was deleted.

111 changes: 108 additions & 3 deletions Renderer/CustomPDFRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,123 @@

namespace KimaiPlugin\CustomExportBundle\Renderer;

use KimaiPlugin\CustomExportBundle\Base\CustomPDFBase as BasePDFRenderer;
use App\Entity\ExportableItem;
use App\Export\ExportFilename;
use App\Export\ExportRendererInterface;
use App\Entity\Timesheet;
use App\Timesheet\DateTimeFactory;
use App\Pdf\HtmlToPdfConverter;
use App\Pdf\PdfContext;
use App\Pdf\PdfRendererTrait;
use App\Project\ProjectStatisticService;
use App\Export\RendererInterface;
use App\Export\Base\RendererTrait;
use App\Repository\Query\TimesheetQuery;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
use Doctrine\DBAL\Connection;

final class CustomPDFRenderer extends BasePDFRenderer implements RendererInterface
final class CustomPDFRenderer implements RendererInterface
{
use RendererTrait;
use PDFRendererTrait;

private string $id = 'ascustompdf';
private string $template = 'arno-leistungsnachweis.pdf.twig';
private array $pdfOptions = [];

public function __construct(Environment $twig, DateTimeFactory $dateTime, HtmlToPdfConverter $converter, Connection $conn, private ProjectStatisticService $projectStatisticService)
{
$this->twig = $twig;
$this->dateTime = $dateTime;
$this->converter = $converter;
$this->conn = $conn;
}

/**
* @param ExportItemInterface[] $timesheets
* @param TimesheetQuery $query
* @return Response
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function render(array $timesheets, TimesheetQuery $query): Response
{
$filename = new ExportFilename($query);
$context = new PdfContext();
$context->setOption('filename', $filename->getFilename());

$summary = $this->calculateSummary($timesheets);
$content = $this->twig->render($this->getTemplate(), array_merge([
'entries' => $timesheets,
'query' => $query,
'now' => $this->dateTime->createDateTime(),
'summaries' => $summary,
'budgets' => $this->calculateProjectBudget($timesheets, $query, $this->projectStatisticService),
'decimal' => false,
'pdfContext' => $context,
'projectRate' => $this->getRate($query),
], $this->getOptions($query)));

$pdfOptions = array_merge($context->getOptions(), $this->getPdfOptions());

$content = $this->converter->convertToPdf($content, $pdfOptions);

return $this->createPdfResponse($content, $context);

}

public function getId(): string
{
return 'ascustompdf';
}

public function getIcon(): string
{
return 'pdf';
return 'pdf-ascustom';
}

public function getTitle(): string
{
return 'pdf-ascustom';
}

protected function getTemplate(): string
{
return '@CustomExport/export/' . $this->template;
}

protected function getOptions(TimesheetQuery $query): array
{
$decimal = false;
if (null !== $query->getCurrentUser()) {
$decimal = $query->getCurrentUser()->isExportDecimal();
} elseif (null !== $query->getUser()) {
$decimal = $query->getUser()->isExportDecimal();
}

return ['decimal' => $decimal];
}

public function getPdfOptions(): array
{
return $this->pdfOptions;
}

public function setPdfOption(string $key, string $value): CustomPDFRenderer
{
$this->pdfOptions[$key] = $value;

return $this;
}

protected function getRate(TimesheetQuery $query)
{
$queryBuilder = $this->conn->createQueryBuilder();
$data = $queryBuilder->select('rate')->from('kimai2_projects_rates')->where('project_id = '.$query->getProjectIds()[0].' and user_id = '.$query->getCurrentUser()->getId())->execute()->fetchAll();
$projectRate = $data[0]['rate'] ? $data[0]['rate'] : '';
return $projectRate;
}

}
3 changes: 0 additions & 3 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ services:
KimaiPlugin\CustomExportBundle\:
resource: '../../*'
exclude: '../../{Resources}'

KimaiPlugin\CustomExportBundle\Utils\MPdfConverter:
arguments: ['%kernel.cache_dir%']
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added Resources/fonts/RobotoSlab-Regular.ttf
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit ecca9bf

Please sign in to comment.