improved invoices with new renderer (#306)
* added docx renderer and demo template * added csv renderer and demo template * added xlsx renderer and demo template * added ods renderer and demo template * added user calculator * added invoice documentation
This commit is contained in:
@@ -7,18 +7,12 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Invoice;
|
||||
namespace App\Invoice\Calculator;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
use App\Model\InvoiceModel;
|
||||
|
||||
/**
|
||||
* Class DefaultCalculator works on all given entries using:
|
||||
* - the customers currency
|
||||
* - the invoice template vat rate
|
||||
* - the entries rate
|
||||
*/
|
||||
class DefaultCalculator implements CalculatorInterface
|
||||
abstract class AbstractCalculator
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
@@ -33,10 +27,12 @@ class DefaultCalculator implements CalculatorInterface
|
||||
/**
|
||||
* @return Timesheet[]
|
||||
*/
|
||||
public function getEntries()
|
||||
{
|
||||
return $this->model->getEntries();
|
||||
}
|
||||
abstract public function getEntries();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getId(): string;
|
||||
|
||||
/**
|
||||
* @param InvoiceModel $model
|
||||
51
src/Invoice/Calculator/AbstractMergedCalculator.php
Normal file
51
src/Invoice/Calculator/AbstractMergedCalculator.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?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\Calculator;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
|
||||
abstract class AbstractMergedCalculator extends AbstractCalculator
|
||||
{
|
||||
/**
|
||||
* @param Timesheet $timesheet
|
||||
* @param Timesheet $entry
|
||||
*/
|
||||
protected function mergeTimesheets(Timesheet $timesheet, Timesheet $entry)
|
||||
{
|
||||
$timesheet->setUser($entry->getUser());
|
||||
$timesheet->setFixedRate($entry->getFixedRate()); // FIXME invoice
|
||||
$timesheet->setHourlyRate($entry->getHourlyRate()); // FIXME invoice
|
||||
$timesheet->setRate($timesheet->getRate() + $entry->getRate());
|
||||
$timesheet->setDuration($timesheet->getDuration() + $entry->getDuration());
|
||||
|
||||
if (null === $timesheet->getBegin() || $timesheet->getBegin()->getTimestamp() > $entry->getBegin()->getTimestamp()) {
|
||||
$timesheet->setBegin($entry->getBegin());
|
||||
}
|
||||
|
||||
if (null === $timesheet->getEnd() || $timesheet->getEnd()->getTimestamp() < $entry->getEnd()->getTimestamp()) {
|
||||
$timesheet->setEnd($entry->getEnd());
|
||||
}
|
||||
|
||||
if (null !== $this->model->getQuery()->getActivity()) {
|
||||
$timesheet->setActivity($this->model->getQuery()->getActivity());
|
||||
$timesheet->setDescription($this->model->getQuery()->getActivity()->getName());
|
||||
} elseif (null !== $this->model->getQuery()->getProject()) {
|
||||
$timesheet->setDescription($this->model->getQuery()->getProject()->getName());
|
||||
}
|
||||
|
||||
if (null === $timesheet->getActivity()) {
|
||||
$timesheet->setActivity($entry->getActivity());
|
||||
}
|
||||
|
||||
if (empty($timesheet->getDescription())) {
|
||||
$timesheet->setDescription($entry->getActivity()->getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
38
src/Invoice/Calculator/DefaultCalculator.php
Normal file
38
src/Invoice/Calculator/DefaultCalculator.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?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\Calculator;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
use App\Invoice\CalculatorInterface;
|
||||
|
||||
/**
|
||||
* Class DefaultCalculator works on all given entries using:
|
||||
* - the customers currency
|
||||
* - the invoice template vat rate
|
||||
* - the entries rate
|
||||
*/
|
||||
class DefaultCalculator extends AbstractCalculator implements CalculatorInterface
|
||||
{
|
||||
/**
|
||||
* @return Timesheet[]
|
||||
*/
|
||||
public function getEntries()
|
||||
{
|
||||
return $this->model->getEntries();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getId(): string
|
||||
{
|
||||
return 'default';
|
||||
}
|
||||
}
|
||||
47
src/Invoice/Calculator/ShortInvoiceCalculator.php
Normal file
47
src/Invoice/Calculator/ShortInvoiceCalculator.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?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\Calculator;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
use App\Invoice\CalculatorInterface;
|
||||
|
||||
/**
|
||||
* A calculator that sums up all timesheet records from the model and returns only one
|
||||
* entry for a compact invoice version.
|
||||
*/
|
||||
class ShortInvoiceCalculator extends AbstractMergedCalculator implements CalculatorInterface
|
||||
{
|
||||
/**
|
||||
* @return Timesheet[]
|
||||
*/
|
||||
public function getEntries()
|
||||
{
|
||||
$entries = $this->model->getEntries();
|
||||
if (empty($entries)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$timesheet = new Timesheet();
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
$this->mergeTimesheets($timesheet, $entry);
|
||||
}
|
||||
|
||||
return [$timesheet];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getId(): string
|
||||
{
|
||||
return 'short';
|
||||
}
|
||||
}
|
||||
51
src/Invoice/Calculator/UserInvoiceCalculator.php
Normal file
51
src/Invoice/Calculator/UserInvoiceCalculator.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?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\Calculator;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
use App\Invoice\CalculatorInterface;
|
||||
|
||||
/**
|
||||
* A calculator that sums up the timesheet records by user.
|
||||
*/
|
||||
class UserInvoiceCalculator extends AbstractMergedCalculator implements CalculatorInterface
|
||||
{
|
||||
/**
|
||||
* @return Timesheet[]
|
||||
*/
|
||||
public function getEntries()
|
||||
{
|
||||
$entries = $this->model->getEntries();
|
||||
if (empty($entries)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
/** @var Timesheet[] $timesheets */
|
||||
$timesheets = [];
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
if (!isset($timesheets[$entry->getUser()->getId()])) {
|
||||
$timesheets[$entry->getUser()->getId()] = new Timesheet();
|
||||
}
|
||||
$timesheet = $timesheets[$entry->getUser()->getId()];
|
||||
$this->mergeTimesheets($timesheet, $entry);
|
||||
}
|
||||
|
||||
return array_values($timesheets);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getId(): string
|
||||
{
|
||||
return 'user';
|
||||
}
|
||||
}
|
||||
@@ -72,4 +72,14 @@ interface CalculatorInterface
|
||||
* @return int
|
||||
*/
|
||||
public function getTimeWorked(): int;
|
||||
|
||||
/**
|
||||
* Returns the unique ID of this calculator.
|
||||
*
|
||||
* Prefix it with your company name followed by a hyphen (e.g. "acme-"),
|
||||
* if this is a third-party calculator.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getId(): string;
|
||||
}
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Invoice;
|
||||
namespace App\Invoice\NumberGenerator;
|
||||
|
||||
use App\Invoice\NumberGeneratorInterface;
|
||||
use App\Model\InvoiceModel;
|
||||
|
||||
/**
|
||||
@@ -22,6 +23,14 @@ class DateNumberGenerator implements NumberGeneratorInterface
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getId(): string
|
||||
{
|
||||
return 'default';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param InvoiceModel $model
|
||||
*/
|
||||
@@ -35,6 +44,6 @@ class DateNumberGenerator implements NumberGeneratorInterface
|
||||
*/
|
||||
public function getInvoiceNumber(): string
|
||||
{
|
||||
return date('ymd');
|
||||
return date('ymd', $this->model->getInvoiceDate()->getTimestamp());
|
||||
}
|
||||
}
|
||||
@@ -25,4 +25,14 @@ interface NumberGeneratorInterface
|
||||
* @return string
|
||||
*/
|
||||
public function getInvoiceNumber(): string;
|
||||
|
||||
/**
|
||||
* Returns the unique ID of this number generator.
|
||||
*
|
||||
* Prefix it with your company name followed by a hyphen (e.g. "acme-"),
|
||||
* if this is a third-party generator.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getId(): string;
|
||||
}
|
||||
|
||||
82
src/Invoice/Renderer/AbstractRenderer.php
Normal file
82
src/Invoice/Renderer/AbstractRenderer.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?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\Twig\DateExtensions;
|
||||
use App\Twig\Extensions;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
abstract class AbstractRenderer
|
||||
{
|
||||
use RendererTrait;
|
||||
|
||||
/**
|
||||
* @var DateExtensions
|
||||
*/
|
||||
protected $dateExtension;
|
||||
|
||||
/**
|
||||
* @var Extensions
|
||||
*/
|
||||
protected $extension;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* @param TranslatorInterface $translator
|
||||
* @param DateExtensions $dateExtension
|
||||
* @param Extensions $extensions
|
||||
*/
|
||||
public function __construct(TranslatorInterface $translator, DateExtensions $dateExtension, Extensions $extensions)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
$this->dateExtension = $dateExtension;
|
||||
$this->extension = $extensions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $date
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getFormattedDateTime(\DateTime $date)
|
||||
{
|
||||
return $this->dateExtension->dateShort($date);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $amount
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getFormattedMoney($amount)
|
||||
{
|
||||
return $this->extension->money($amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $date
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getFormattedMonthName(\DateTime $date)
|
||||
{
|
||||
return $this->translator->trans($this->dateExtension->monthName($date));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $seconds
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getFormattedDuration($seconds)
|
||||
{
|
||||
return $this->extension->duration($seconds);
|
||||
}
|
||||
}
|
||||
129
src/Invoice/Renderer/AbstractSpreadsheetRenderer.php
Normal file
129
src/Invoice/Renderer/AbstractSpreadsheetRenderer.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?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\Model\InvoiceModel;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
abstract class AbstractSpreadsheetRenderer extends AbstractRenderer
|
||||
{
|
||||
/**
|
||||
* @param Spreadsheet $spreadsheet
|
||||
* @return bool|string
|
||||
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
|
||||
*/
|
||||
abstract protected function saveSpreadsheet(Spreadsheet $spreadsheet);
|
||||
|
||||
/**
|
||||
* Render the given InvoiceDocument with the data from the InvoiceModel.
|
||||
*
|
||||
* @param InvoiceDocument $document
|
||||
* @param InvoiceModel $model
|
||||
* @return Response
|
||||
*/
|
||||
public function render(InvoiceDocument $document, InvoiceModel $model): Response
|
||||
{
|
||||
$spreadsheet = IOFactory::load($document->getFilename());
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
$entries = $model->getCalculator()->getEntries();
|
||||
$replacer = $this->modelToReplacer($model);
|
||||
$timesheetAmount = count($entries);
|
||||
$this->addTemplateRows($worksheet, $timesheetAmount);
|
||||
|
||||
$worksheet->setTitle($model->getTemplate()->getTitle());
|
||||
|
||||
$entryRow = 0;
|
||||
|
||||
foreach ($worksheet->getRowIterator() as $row) {
|
||||
$timesheet = $entries[$entryRow];
|
||||
$sheetValues = false;
|
||||
foreach ($row->getCellIterator() as $cell) {
|
||||
$value = $cell->getValue();
|
||||
if (stripos($value, '${entry.') !== false) {
|
||||
if ($sheetValues === false) {
|
||||
$sheetValues = $this->timesheetToArray($timesheet);
|
||||
}
|
||||
$searcher = str_replace('${', '', $value);
|
||||
$searcher = str_replace('}', '', $searcher);
|
||||
if (isset($sheetValues[$searcher])) {
|
||||
$cell->setValue($sheetValues[$searcher]);
|
||||
}
|
||||
} elseif (stripos($value, '${') !== false) {
|
||||
$searcher = str_replace('${', '', $value);
|
||||
$searcher = str_replace('}', '', $searcher);
|
||||
if (isset($replacer[$searcher])) {
|
||||
$cell->setValue($replacer[$searcher]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($sheetValues !== false && $entryRow < $timesheetAmount - 1) {
|
||||
$entryRow++;
|
||||
}
|
||||
}
|
||||
|
||||
$filename = $this->saveSpreadsheet($spreadsheet);
|
||||
|
||||
return $this->getFileResponse($filename, basename($document->getFilename()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Worksheet $worksheet
|
||||
* @param int $timesheets
|
||||
*/
|
||||
protected function addTemplateRows(Worksheet $worksheet, int $timesheets)
|
||||
{
|
||||
$startRow = null;
|
||||
$rowCounter = 0;
|
||||
|
||||
foreach ($worksheet->getRowIterator() as $row) {
|
||||
$cellCounter = 0;
|
||||
foreach ($row->getCellIterator() as $cell) {
|
||||
$value = $cell->getValue();
|
||||
if (stripos($value, '${entry.') !== false) {
|
||||
$startRow = $row->getRowIndex();
|
||||
$worksheet->insertNewRowBefore($row->getRowIndex(), $timesheets - 1);
|
||||
break 2;
|
||||
}
|
||||
|
||||
if ($cellCounter++ >= 10) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($rowCounter++ >= 100) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($startRow === null) {
|
||||
throw new \Exception('Invalid invoice document, no template row found.');
|
||||
}
|
||||
|
||||
// fill up all new rows with template values
|
||||
$templateRow = $timesheets + $startRow;
|
||||
$iterator = $worksheet->getRowIterator($templateRow - 1, $templateRow);
|
||||
$templateColumns = [];
|
||||
foreach ($iterator->current()->getCellIterator() as $cell) {
|
||||
$templateColumns[$cell->getColumn()] = $cell->getValue();
|
||||
}
|
||||
|
||||
$iterator = $worksheet->getRowIterator($startRow, $templateRow - 2);
|
||||
foreach ($iterator as $row) {
|
||||
foreach ($row->getCellIterator() as $cell) {
|
||||
$cell->setValue($templateColumns[$cell->getColumn()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
47
src/Invoice/Renderer/CsvRenderer.php
Normal file
47
src/Invoice/Renderer/CsvRenderer.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?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\Invoice\RendererInterface;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
class CsvRenderer extends AbstractSpreadsheetRenderer implements RendererInterface
|
||||
{
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getFileExtensions()
|
||||
{
|
||||
return ['.csv'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getContentType()
|
||||
{
|
||||
return 'text/csv';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Spreadsheet $spreadsheet
|
||||
* @return bool|string
|
||||
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
|
||||
*/
|
||||
protected function saveSpreadsheet(Spreadsheet $spreadsheet)
|
||||
{
|
||||
$filename = tempnam(sys_get_temp_dir(), 'kimai-csv');
|
||||
$writer = IOFactory::createWriter($spreadsheet, 'Csv');
|
||||
$writer->save($filename);
|
||||
|
||||
return $filename;
|
||||
}
|
||||
}
|
||||
88
src/Invoice/Renderer/DocxRenderer.php
Normal file
88
src/Invoice/Renderer/DocxRenderer.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?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\RendererInterface;
|
||||
use App\Model\InvoiceModel;
|
||||
use PhpOffice\PhpWord\PhpWord;
|
||||
use PhpOffice\PhpWord\TemplateProcessor;
|
||||
use Symfony\Component\HttpFoundation\File\Stream;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class DocxRenderer extends AbstractRenderer implements RendererInterface
|
||||
{
|
||||
/*
|
||||
protected function setPhpWordOptions(PhpWord $phpWord)
|
||||
{
|
||||
if (!extension_loaded('zip')) {
|
||||
\PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
|
||||
}
|
||||
|
||||
// \PhpOffice\PhpWord\Settings::setPdfRendererPath(__DIR__ . '/../../vendor/tecnickcom/tcpdf/');
|
||||
// \PhpOffice\PhpWord\Settings::setPdfRendererName(\PhpOffice\PhpWord\Settings::PDF_RENDERER_TCPDF);
|
||||
// \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
|
||||
// $phpWord->getSettings()->setThemeFontLang(new Language(Language::EN_US));
|
||||
|
||||
$properties = $phpWord->getDocInfo();
|
||||
$properties->setCreator('Kimai 2');
|
||||
$properties->setDescription('Created with Kimai 2, the open-source time-tracking software! Get more information at www.kimai.org.');
|
||||
$properties->setCreated(time());
|
||||
$properties->setModified(time());
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param InvoiceDocument $document
|
||||
* @param InvoiceModel $model
|
||||
* @return Response
|
||||
*/
|
||||
public function render(InvoiceDocument $document, InvoiceModel $model): Response
|
||||
{
|
||||
$filename = basename($document->getFilename());
|
||||
|
||||
$template = new TemplateProcessor($document->getFilename());
|
||||
foreach ($this->modelToReplacer($model) as $key => $value) {
|
||||
$template->setValue($key, $value);
|
||||
}
|
||||
|
||||
$template->cloneRow('entry.description', count($model->getCalculator()->getEntries()));
|
||||
$i = 1;
|
||||
foreach ($model->getCalculator()->getEntries() as $entry) {
|
||||
$values = $this->timesheetToArray($entry);
|
||||
foreach ($values as $search => $replace) {
|
||||
$template->setValue($search . '#' . $i, $replace);
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
|
||||
$cacheFile = $template->save();
|
||||
|
||||
clearstatcache(true, $cacheFile);
|
||||
|
||||
return $this->getFileResponse(new Stream($cacheFile), $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getFileExtensions()
|
||||
{
|
||||
return ['.docx'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getContentType()
|
||||
{
|
||||
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
||||
}
|
||||
}
|
||||
47
src/Invoice/Renderer/OdsRenderer.php
Normal file
47
src/Invoice/Renderer/OdsRenderer.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?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\Invoice\RendererInterface;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
class OdsRenderer extends AbstractSpreadsheetRenderer implements RendererInterface
|
||||
{
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getFileExtensions()
|
||||
{
|
||||
return ['.ods'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getContentType()
|
||||
{
|
||||
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Spreadsheet $spreadsheet
|
||||
* @return bool|string
|
||||
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
|
||||
*/
|
||||
protected function saveSpreadsheet(Spreadsheet $spreadsheet)
|
||||
{
|
||||
$filename = tempnam(sys_get_temp_dir(), 'kimai-ods');
|
||||
$writer = IOFactory::createWriter($spreadsheet, 'Ods');
|
||||
$writer->save($filename);
|
||||
|
||||
return $filename;
|
||||
}
|
||||
}
|
||||
180
src/Invoice/Renderer/RendererTrait.php
Normal file
180
src/Invoice/Renderer/RendererTrait.php
Normal file
@@ -0,0 +1,180 @@
|
||||
<?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\Entity\Timesheet;
|
||||
use App\Entity\UserPreference;
|
||||
use App\Model\InvoiceModel;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
|
||||
trait RendererTrait
|
||||
{
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
abstract protected function getFileExtensions();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function getContentType();
|
||||
|
||||
/**
|
||||
* @param InvoiceDocument $document
|
||||
* @return bool
|
||||
*/
|
||||
public function supports(InvoiceDocument $document): bool
|
||||
{
|
||||
foreach ($this->getFileExtensions() as $extension) {
|
||||
if (stripos($document->getFilename(), $extension) !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $date
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function getFormattedDateTime(\DateTime $date);
|
||||
|
||||
/**
|
||||
* @param $amount
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function getFormattedMoney($amount);
|
||||
|
||||
/**
|
||||
* @param \DateTime $date
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function getFormattedMonthName(\DateTime $date);
|
||||
|
||||
/**
|
||||
* @param $seconds
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function getFormattedDuration($seconds);
|
||||
|
||||
/**
|
||||
* @param InvoiceModel $model
|
||||
* @return array
|
||||
*/
|
||||
protected function modelToReplacer(InvoiceModel $model)
|
||||
{
|
||||
return [
|
||||
'invoice.due_date' => $this->getFormattedDateTime($model->getDueDate()),
|
||||
'invoice.date' => $this->getFormattedDateTime($model->getInvoiceDate()),
|
||||
'invoice.number' => $model->getNumberGenerator()->getInvoiceNumber(),
|
||||
'invoice.currency' => $model->getCalculator()->getCurrency(),
|
||||
'invoice.vat' => $model->getCalculator()->getVat(),
|
||||
'invoice.tax' => $this->getFormattedMoney($model->getCalculator()->getTax()),
|
||||
'invoice.total_time' => $this->getFormattedDuration($model->getCalculator()->getTimeWorked()),
|
||||
'invoice.total' => $this->getFormattedMoney($model->getCalculator()->getTotal()),
|
||||
'invoice.subtotal' => $this->getFormattedMoney($model->getCalculator()->getSubtotal()),
|
||||
|
||||
'template.name' => $model->getTemplate()->getName(),
|
||||
'template.company' => $model->getTemplate()->getCompany(),
|
||||
'template.address' => $model->getTemplate()->getAddress(),
|
||||
'template.title' => $model->getTemplate()->getTitle(),
|
||||
'template.payment_terms' => $model->getTemplate()->getPaymentTerms(),
|
||||
'template.due_days' => $model->getTemplate()->getDueDays(),
|
||||
|
||||
'query.begin' => $this->getFormattedDateTime($model->getQuery()->getBegin()),
|
||||
'query.end' => $this->getFormattedDateTime($model->getQuery()->getEnd()),
|
||||
'query.month' => $this->getFormattedMonthName($model->getQuery()->getBegin()),
|
||||
'query.year' => $model->getQuery()->getBegin()->format('Y'),
|
||||
|
||||
'customer.address' => $model->getCustomer()->getAddress(),
|
||||
'customer.name' => $model->getCustomer()->getName(),
|
||||
'customer.contact' => $model->getCustomer()->getContact(),
|
||||
'customer.company' => $model->getCustomer()->getCompany(),
|
||||
'customer.number' => $model->getCustomer()->getNumber(),
|
||||
'customer.country' => $model->getCustomer()->getCountry(),
|
||||
'customer.homepage' => $model->getCustomer()->getHomepage(),
|
||||
'customer.comment' => $model->getCustomer()->getComment(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Timesheet $timesheet
|
||||
* @return array
|
||||
*/
|
||||
protected function timesheetToArray(Timesheet $timesheet)
|
||||
{
|
||||
$rate = $timesheet->getRate();
|
||||
$hourlyRate = $timesheet->getHourlyRate();
|
||||
$amount = $this->getFormattedDuration($timesheet->getDuration());
|
||||
$description = $timesheet->getDescription();
|
||||
|
||||
if (null !== $timesheet->getFixedRate()) {
|
||||
$rate = $timesheet->getFixedRate();
|
||||
$hourlyRate = $timesheet->getFixedRate();
|
||||
$amount = 1;
|
||||
}
|
||||
|
||||
if (empty($description)) {
|
||||
$description = $timesheet->getActivity()->getName();
|
||||
}
|
||||
|
||||
$user = $timesheet->getUser();
|
||||
|
||||
if (empty($hourlyRate)) {
|
||||
$hourlyRate = $user->getPreferenceValue(UserPreference::HOURLY_RATE);
|
||||
}
|
||||
|
||||
$activity = $timesheet->getActivity();
|
||||
$project = $activity->getProject();
|
||||
$customer = $project->getCustomer();
|
||||
|
||||
return [
|
||||
'entry.description' => $description,
|
||||
'entry.amount' => $amount,
|
||||
'entry.rate' => $this->getFormattedMoney($hourlyRate),
|
||||
'entry.total' => $this->getFormattedMoney($rate),
|
||||
'entry.duration' => $timesheet->getDuration(),
|
||||
'entry.begin' => $this->getFormattedDateTime($timesheet->getBegin()),
|
||||
'entry.begin_timestamp' => $timesheet->getBegin()->getTimestamp(),
|
||||
'entry.end' => $this->getFormattedDateTime($timesheet->getEnd()),
|
||||
'entry.end_timestamp' => $timesheet->getEnd()->getTimestamp(),
|
||||
'entry.date' => $this->getFormattedDateTime($timesheet->getBegin()),
|
||||
'entry.user_id' => $user->getId(),
|
||||
'entry.user_name' => $user->getUsername(),
|
||||
'entry.user_alias' => $user->getAlias(),
|
||||
'entry.activity' => $activity->getName(),
|
||||
'entry.activity_id' => $activity->getId(),
|
||||
'entry.project' => $project->getName(),
|
||||
'entry.project_id' => $project->getId(),
|
||||
'entry.customer' => $customer->getName(),
|
||||
'entry.customer_id' => $customer->getId(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $file
|
||||
* @param string $filename
|
||||
* @return BinaryFileResponse
|
||||
*/
|
||||
protected function getFileResponse($file, $filename)
|
||||
{
|
||||
$response = new BinaryFileResponse($file);
|
||||
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename);
|
||||
|
||||
$response->headers->set('Content-Type', $this->getContentType());
|
||||
$response->headers->set('Content-Disposition', $disposition);
|
||||
$response->deleteFileAfterSend(true);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
57
src/Invoice/Renderer/TwigRenderer.php
Normal file
57
src/Invoice/Renderer/TwigRenderer.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?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\RendererInterface;
|
||||
use App\Model\InvoiceModel;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class TwigRenderer implements RendererInterface
|
||||
{
|
||||
/**
|
||||
* @var \Twig_Environment
|
||||
*/
|
||||
protected $twig;
|
||||
|
||||
/**
|
||||
* @param \Twig_Environment $twig
|
||||
*/
|
||||
public function __construct(\Twig_Environment $twig)
|
||||
{
|
||||
$this->twig = $twig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param InvoiceDocument $document
|
||||
* @return bool
|
||||
*/
|
||||
public function supports(InvoiceDocument $document): bool
|
||||
{
|
||||
return stripos($document->getFilename(), '.twig') !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param InvoiceDocument $document
|
||||
* @param InvoiceModel $model
|
||||
* @return Response
|
||||
*/
|
||||
public function render(InvoiceDocument $document, InvoiceModel $model): Response
|
||||
{
|
||||
$content = $this->twig->render('@invoice/' . basename($document->getFilename()), [
|
||||
'model' => $model
|
||||
]);
|
||||
|
||||
$response = new Response();
|
||||
$response->setContent($content);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
47
src/Invoice/Renderer/XlsxRenderer.php
Normal file
47
src/Invoice/Renderer/XlsxRenderer.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?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\Invoice\RendererInterface;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
class XlsxRenderer extends AbstractSpreadsheetRenderer implements RendererInterface
|
||||
{
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getFileExtensions()
|
||||
{
|
||||
return ['.xlsx', '.xls'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getContentType()
|
||||
{
|
||||
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Spreadsheet $spreadsheet
|
||||
* @return bool|string
|
||||
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
|
||||
*/
|
||||
protected function saveSpreadsheet(Spreadsheet $spreadsheet)
|
||||
{
|
||||
$filename = tempnam(sys_get_temp_dir(), 'kimai-xslx');
|
||||
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
|
||||
$writer->save($filename);
|
||||
|
||||
return $filename;
|
||||
}
|
||||
}
|
||||
34
src/Invoice/RendererInterface.php
Normal file
34
src/Invoice/RendererInterface.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?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;
|
||||
|
||||
use App\Entity\InvoiceDocument;
|
||||
use App\Model\InvoiceModel;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
interface RendererInterface
|
||||
{
|
||||
/**
|
||||
* Checks whether the given InvoiceDocument can be rendered.
|
||||
*
|
||||
* @param InvoiceDocument $document
|
||||
* @return bool
|
||||
*/
|
||||
public function supports(InvoiceDocument $document): bool;
|
||||
|
||||
/**
|
||||
* Render the given InvoiceDocument with the data from the InvoiceModel.
|
||||
*
|
||||
* @param InvoiceDocument $document
|
||||
* @param InvoiceModel $model
|
||||
* @return Response
|
||||
*/
|
||||
public function render(InvoiceDocument $document, InvoiceModel $model): Response;
|
||||
}
|
||||
@@ -9,34 +9,59 @@
|
||||
|
||||
namespace App\Invoice;
|
||||
|
||||
use App\Entity\InvoiceDocument;
|
||||
use App\Repository\InvoiceDocumentRepository;
|
||||
|
||||
/**
|
||||
* A service to manage the invoice configuration:
|
||||
* - invoice number generator
|
||||
* - invoice sum calculator
|
||||
* - template renderer
|
||||
* A service to manage invoice dependencies.
|
||||
*/
|
||||
class ServiceInvoice
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
* @var CalculatorInterface[]
|
||||
*/
|
||||
protected $config = [];
|
||||
protected $calculator = [];
|
||||
|
||||
/**
|
||||
* ServiceInvoice constructor.
|
||||
* @param array $invoiceConfig
|
||||
* @var RendererInterface[]
|
||||
*/
|
||||
public function __construct(array $invoiceConfig)
|
||||
protected $renderer = [];
|
||||
|
||||
/**
|
||||
* @var NumberGeneratorInterface[]
|
||||
*/
|
||||
protected $numberGenerator = [];
|
||||
|
||||
/**
|
||||
* @var InvoiceDocumentRepository
|
||||
*/
|
||||
protected $documents;
|
||||
|
||||
/**
|
||||
* @param InvoiceDocumentRepository $repository
|
||||
*/
|
||||
public function __construct(InvoiceDocumentRepository $repository)
|
||||
{
|
||||
$this->config = $invoiceConfig;
|
||||
$this->documents = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @param NumberGeneratorInterface $generator
|
||||
* @return $this
|
||||
*/
|
||||
public function addNumberGenerator(NumberGeneratorInterface $generator)
|
||||
{
|
||||
$this->numberGenerator[] = $generator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return NumberGeneratorInterface[]
|
||||
*/
|
||||
public function getNumberGenerator()
|
||||
{
|
||||
return $this->config['number_generator'];
|
||||
return $this->numberGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,9 +70,9 @@ class ServiceInvoice
|
||||
*/
|
||||
public function getNumberGeneratorByName(string $name)
|
||||
{
|
||||
foreach ($this->getNumberGenerator() as $key => $class) {
|
||||
if ($key === $name) {
|
||||
return new $class();
|
||||
foreach ($this->getNumberGenerator() as $generator) {
|
||||
if ($generator->getId() === $name) {
|
||||
return $generator;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,11 +80,22 @@ class ServiceInvoice
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @param CalculatorInterface $calculator
|
||||
* @return $this
|
||||
*/
|
||||
public function addCalculator(CalculatorInterface $calculator)
|
||||
{
|
||||
$this->calculator[] = $calculator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CalculatorInterface[]
|
||||
*/
|
||||
public function getCalculator()
|
||||
{
|
||||
return $this->config['calculator'];
|
||||
return $this->calculator;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,38 +104,52 @@ class ServiceInvoice
|
||||
*/
|
||||
public function getCalculatorByName(string $name)
|
||||
{
|
||||
foreach ($this->getCalculator() as $key => $class) {
|
||||
if ($key === $name) {
|
||||
return new $class();
|
||||
foreach ($this->getCalculator() as $calculator) {
|
||||
if ($calculator->getId() === $name) {
|
||||
return $calculator;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return InvoiceDocument|null
|
||||
*/
|
||||
public function getDocumentByName(string $name)
|
||||
{
|
||||
return $this->documents->findByName($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of invoice renderer, which will consist of a unique name and a controller action.
|
||||
*
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
* @return InvoiceDocument[]
|
||||
*/
|
||||
public function getRenderer()
|
||||
public function getDocuments()
|
||||
{
|
||||
return $this->config['renderer'];
|
||||
return $this->documents->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $renderer
|
||||
* @return string|null
|
||||
* @param RendererInterface $renderer
|
||||
* @return $this
|
||||
*/
|
||||
public function getRendererActionByName($renderer)
|
||||
public function addRenderer(RendererInterface $renderer)
|
||||
{
|
||||
foreach ($this->config['renderer'] as $name => $action) {
|
||||
if ($name == $renderer) {
|
||||
return $action;
|
||||
}
|
||||
}
|
||||
$this->renderer[] = $renderer;
|
||||
|
||||
return null;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of invoice renderer.
|
||||
*
|
||||
* @return RendererInterface[]
|
||||
*/
|
||||
public function getRenderer()
|
||||
{
|
||||
return $this->renderer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
<?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;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
|
||||
/**
|
||||
* A calculator that sums up all timesheet records from the model and returns only one
|
||||
* entry for a compact invoice version.
|
||||
*/
|
||||
class ShortInvoiceCalculator extends DefaultCalculator
|
||||
{
|
||||
/**
|
||||
* @return Timesheet[]
|
||||
*/
|
||||
public function getEntries()
|
||||
{
|
||||
$timesheet = new Timesheet();
|
||||
|
||||
foreach ($this->model->getEntries() as $entry) {
|
||||
$timesheet->setRate($timesheet->getRate() + $entry->getRate());
|
||||
$timesheet->setDuration($timesheet->getDuration() + $entry->getDuration());
|
||||
$timesheet->setBegin($entry->getBegin());
|
||||
if (null === $timesheet->getActivity()) {
|
||||
$timesheet->setActivity($entry->getActivity());
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== $this->model->getQuery()->getActivity()) {
|
||||
$timesheet->setDescription($this->model->getQuery()->getActivity()->getName());
|
||||
} elseif (null !== $this->model->getQuery()->getProject()) {
|
||||
$timesheet->setDescription($this->model->getQuery()->getProject()->getName());
|
||||
}
|
||||
|
||||
return [$timesheet];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user