support multiple invoice repositories (#1084)

This commit is contained in:
Kevin Papst
2019-09-04 22:50:53 +02:00
committed by GitHub
parent 3f27ab1e44
commit 3ea05cb705
29 changed files with 323 additions and 144 deletions

View File

@@ -11,10 +11,21 @@ namespace App\Invoice\Calculator;
use App\Entity\Timesheet;
use App\Invoice\InvoiceItem;
use App\Invoice\InvoiceItemInterface;
abstract class AbstractMergedCalculator extends AbstractCalculator
{
/**
* @deprecated since 1.3 - will be removed with 2.0
*/
protected function mergeTimesheets(InvoiceItem $invoiceItem, Timesheet $entry)
{
@trigger_error('mergeTimesheets() is deprecated and will be removed with 2.0', E_USER_DEPRECATED);
$this->mergeInvoiceItems($invoiceItem, $entry);
}
protected function mergeInvoiceItems(InvoiceItem $invoiceItem, InvoiceItemInterface $entry)
{
$invoiceItem->setAmount($invoiceItem->getAmount() + 1);
$invoiceItem->setUser($entry->getUser());

View File

@@ -9,16 +9,16 @@
namespace App\Invoice\Calculator;
use App\Entity\Timesheet;
use App\Invoice\CalculatorInterface;
use App\Invoice\InvoiceItem;
use App\Invoice\InvoiceItemInterface;
/**
* An abstract calculator that sums up the timesheet records.
* An abstract calculator that sums up the invoice item records.
*/
abstract class AbstractSumInvoiceCalculator extends AbstractMergedCalculator implements CalculatorInterface
{
abstract protected function calculateSumIdentifier(Timesheet $timesheet): string;
abstract protected function calculateSumIdentifier(InvoiceItemInterface $invoiceItem): string;
/**
* @return InvoiceItem[]
@@ -45,15 +45,15 @@ abstract class AbstractSumInvoiceCalculator extends AbstractMergedCalculator imp
if (!isset($invoiceItems[$id])) {
$invoiceItems[$id] = new InvoiceItem();
}
$timesheet = $invoiceItems[$id];
$this->mergeTimesheets($timesheet, $entry);
$this->mergeSumTimesheet($timesheet, $entry);
$invoiceItem = $invoiceItems[$id];
$this->mergeInvoiceItems($invoiceItem, $entry);
$this->mergeSumTimesheet($invoiceItem, $entry);
}
return array_values($invoiceItems);
}
protected function mergeSumTimesheet(InvoiceItem $invoiceItem, Timesheet $entry)
protected function mergeSumTimesheet(InvoiceItem $invoiceItem, InvoiceItemInterface $entry)
{
// allows to set values per calculator after merging the timesheet
}

View File

@@ -9,25 +9,25 @@
namespace App\Invoice\Calculator;
use App\Entity\Timesheet;
use App\Invoice\CalculatorInterface;
use App\Invoice\InvoiceItem;
use App\Invoice\InvoiceItemInterface;
/**
* A calculator that sums up the timesheet records by activity.
* A calculator that sums up the invoice item records by activity.
*/
class ActivityInvoiceCalculator extends AbstractSumInvoiceCalculator implements CalculatorInterface
{
protected function calculateSumIdentifier(Timesheet $timesheet): string
protected function calculateSumIdentifier(InvoiceItemInterface $invoiceItem): string
{
if (null === $timesheet->getActivity()->getId()) {
if (null === $invoiceItem->getActivity()->getId()) {
throw new \Exception('Cannot handle un-persisted activities');
}
return (string) $timesheet->getActivity()->getId();
return (string) $invoiceItem->getActivity()->getId();
}
protected function mergeSumTimesheet(InvoiceItem $invoiceItem, Timesheet $entry)
protected function mergeSumTimesheet(InvoiceItem $invoiceItem, InvoiceItemInterface $entry)
{
$invoiceItem->setActivity($entry->getActivity());
$invoiceItem->setDescription($entry->getActivity()->getName());

View File

@@ -9,21 +9,21 @@
namespace App\Invoice\Calculator;
use App\Entity\Timesheet;
use App\Invoice\CalculatorInterface;
use App\Invoice\InvoiceItemInterface;
/**
* A calculator that sums up the timesheet records for each day.
* A calculator that sums up the invoice item records for each day.
*/
class DateInvoiceCalculator extends AbstractSumInvoiceCalculator implements CalculatorInterface
{
protected function calculateSumIdentifier(Timesheet $timesheet): string
protected function calculateSumIdentifier(InvoiceItemInterface $invoiceItem): string
{
if (null === $timesheet->getBegin()) {
throw new \Exception('Cannot handle timesheets without start date');
if (null === $invoiceItem->getBegin()) {
throw new \Exception('Cannot handle invoice items without start date');
}
return $timesheet->getBegin()->format('Y-m-d');
return $invoiceItem->getBegin()->format('Y-m-d');
}
/**

View File

@@ -29,7 +29,7 @@ class DefaultCalculator extends AbstractMergedCalculator implements CalculatorIn
foreach ($this->model->getEntries() as $entry) {
$item = new InvoiceItem();
$this->mergeTimesheets($item, $entry);
$this->mergeInvoiceItems($item, $entry);
foreach ($entry->getVisibleMetaFields() as $field) {
$item->addAdditionalField($field->getName(), $field->getValue());
}

View File

@@ -9,25 +9,25 @@
namespace App\Invoice\Calculator;
use App\Entity\Timesheet;
use App\Invoice\CalculatorInterface;
use App\Invoice\InvoiceItem;
use App\Invoice\InvoiceItemInterface;
/**
* A calculator that sums up the timesheet records by project.
* A calculator that sums up the invoice item records by project.
*/
class ProjectInvoiceCalculator extends AbstractSumInvoiceCalculator implements CalculatorInterface
{
protected function calculateSumIdentifier(Timesheet $timesheet): string
protected function calculateSumIdentifier(InvoiceItemInterface $invoiceItem): string
{
if (null === $timesheet->getProject()->getId()) {
if (null === $invoiceItem->getProject()->getId()) {
throw new \Exception('Cannot handle un-persisted projects');
}
return (string) $timesheet->getProject()->getId();
return (string) $invoiceItem->getProject()->getId();
}
protected function mergeSumTimesheet(InvoiceItem $invoiceItem, Timesheet $entry)
protected function mergeSumTimesheet(InvoiceItem $invoiceItem, InvoiceItemInterface $entry)
{
$invoiceItem->setProject($entry->getProject());
$invoiceItem->setDescription($entry->getProject()->getName());

View File

@@ -9,12 +9,11 @@
namespace App\Invoice\Calculator;
use App\Entity\Timesheet;
use App\Invoice\CalculatorInterface;
use App\Invoice\InvoiceItem;
/**
* A calculator that sums up all timesheet records from the model and returns only one
* A calculator that sums up all invoice item records from the model and returns only one
* entry for a compact invoice version.
*/
class ShortInvoiceCalculator extends AbstractMergedCalculator implements CalculatorInterface
@@ -40,7 +39,7 @@ class ShortInvoiceCalculator extends AbstractMergedCalculator implements Calcula
if (!in_array($key, $keys)) {
$keys[] = $key;
}
$this->mergeTimesheets($invoiceItem, $entry);
$this->mergeInvoiceItems($invoiceItem, $entry);
}
if (count($keys) > 1) {

View File

@@ -9,21 +9,21 @@
namespace App\Invoice\Calculator;
use App\Entity\Timesheet;
use App\Invoice\CalculatorInterface;
use App\Invoice\InvoiceItemInterface;
/**
* A calculator that sums up the timesheet records by user.
* A calculator that sums up the invoice item records by user.
*/
class UserInvoiceCalculator extends AbstractSumInvoiceCalculator implements CalculatorInterface
{
protected function calculateSumIdentifier(Timesheet $timesheet): string
protected function calculateSumIdentifier(InvoiceItemInterface $invoiceItem): string
{
if (null === $timesheet->getUser()->getId()) {
if (null === $invoiceItem->getUser()->getId()) {
throw new \Exception('Cannot handle un-persisted user');
}
return (string) $timesheet->getUser()->getId();
return (string) $invoiceItem->getUser()->getId();
}
/**

View File

@@ -0,0 +1,43 @@
<?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\Activity;
use App\Entity\MetaTableTypeInterface;
use App\Entity\Project;
use App\Entity\User;
interface InvoiceItemInterface
{
public function getActivity(): ?Activity;
public function getProject(): ?Project;
public function getFixedRate(): ?float;
public function getHourlyRate(): ?float;
public function getRate(): float;
public function getUser(): ?User;
public function getBegin(): ?\DateTime;
public function getEnd(): ?\DateTime;
public function getDuration(): int;
public function getDescription(): ?string;
/**
* @return MetaTableTypeInterface[]
*/
public function getVisibleMetaFields(): array;
}

View File

@@ -0,0 +1,26 @@
<?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\Repository\Query\InvoiceQuery;
interface InvoiceItemRepositoryInterface
{
/**
* @param InvoiceItemInterface[] $invoiceItems
*/
public function setExported(array $invoiceItems);
/**
* @param InvoiceQuery $query
* @return InvoiceItemInterface[]
*/
public function getInvoiceItemsForQuery(InvoiceQuery $query): iterable;
}

View File

@@ -11,7 +11,6 @@ namespace App\Invoice;
use App\Entity\Customer;
use App\Entity\InvoiceTemplate;
use App\Entity\Timesheet;
use App\Repository\Query\InvoiceQuery;
/**
@@ -31,7 +30,7 @@ class InvoiceModel
protected $query;
/**
* @var Timesheet[]
* @var InvoiceItemInterface[]
*/
protected $entries = [];
@@ -82,7 +81,7 @@ class InvoiceModel
/**
* Do not use this method for rendering the invoice, use InvoiceModel::getCalculator()->getEntries() instead.
*
* @return Timesheet[]
* @return InvoiceItemInterface[]
*/
public function getEntries(): array
{
@@ -90,16 +89,30 @@ class InvoiceModel
}
/**
* @param Timesheet[] $entries
* @deprecated since 1.3 - will be removed with 2.0
* @param InvoiceItemInterface[] $entries
* @return InvoiceModel
*/
public function setEntries(array $entries): InvoiceModel
{
@trigger_error('setEntries() is deprecated and will be removed with 2.0', E_USER_DEPRECATED);
$this->entries = $entries;
return $this;
}
/**
* @param InvoiceItemInterface[] $entries
* @return InvoiceModel
*/
public function addEntries(array $entries): InvoiceModel
{
$this->entries = array_merge($this->entries, $entries);
return $this;
}
public function getTemplate(): ?InvoiceTemplate
{
return $this->template;

View File

@@ -41,9 +41,9 @@ abstract class AbstractSpreadsheetRenderer extends AbstractRenderer
$worksheet = $spreadsheet->getActiveSheet();
$entries = $model->getCalculator()->getEntries();
$replacer = $this->modelToReplacer($model);
$timesheetAmount = count($entries);
if ($timesheetAmount > 1) {
$this->addTemplateRows($worksheet, $timesheetAmount);
$invoiceItemCount = count($entries);
if ($invoiceItemCount > 1) {
$this->addTemplateRows($worksheet, $invoiceItemCount);
}
$worksheet->setTitle($model->getTemplate()->getTitle());
@@ -51,13 +51,13 @@ abstract class AbstractSpreadsheetRenderer extends AbstractRenderer
$entryRow = 0;
foreach ($worksheet->getRowIterator() as $row) {
$timesheet = $entries[$entryRow];
$invoiceItem = $entries[$entryRow];
$sheetValues = false;
foreach ($row->getCellIterator() as $cell) {
$value = $cell->getValue();
if (stripos($value, '${entry.') !== false) {
if ($sheetValues === false) {
$sheetValues = $this->timesheetToArray($timesheet);
$sheetValues = $this->invoiceItemToArray($invoiceItem);
}
$searcher = str_replace('${', '', $value);
$searcher = str_replace('}', '', $searcher);
@@ -73,7 +73,7 @@ abstract class AbstractSpreadsheetRenderer extends AbstractRenderer
}
}
if ($sheetValues !== false && $entryRow < $timesheetAmount - 1) {
if ($sheetValues !== false && $entryRow < $invoiceItemCount - 1) {
$entryRow++;
}
}
@@ -85,9 +85,10 @@ abstract class AbstractSpreadsheetRenderer extends AbstractRenderer
/**
* @param Worksheet $worksheet
* @param int $timesheets
* @param int $invoiceItemCount
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
protected function addTemplateRows(Worksheet $worksheet, int $timesheets)
protected function addTemplateRows(Worksheet $worksheet, int $invoiceItemCount)
{
$startRow = null;
$rowCounter = 0;
@@ -98,7 +99,7 @@ abstract class AbstractSpreadsheetRenderer extends AbstractRenderer
$value = $cell->getValue();
if (stripos($value, '${entry.') !== false) {
$startRow = $row->getRowIndex();
$worksheet->insertNewRowBefore($row->getRowIndex(), $timesheets - 1);
$worksheet->insertNewRowBefore($row->getRowIndex(), $invoiceItemCount - 1);
break 2;
}
@@ -117,7 +118,7 @@ abstract class AbstractSpreadsheetRenderer extends AbstractRenderer
}
// fill up all new rows with template values
$templateRow = $timesheets + $startRow;
$templateRow = $invoiceItemCount + $startRow;
$iterator = $worksheet->getRowIterator($templateRow - 1, $templateRow);
$templateColumns = [];
foreach ($iterator->current()->getCellIterator() as $cell) {

View File

@@ -50,7 +50,7 @@ class DocxRenderer extends AbstractRenderer implements RendererInterface
$i = 1;
foreach ($model->getCalculator()->getEntries() as $entry) {
$values = $this->timesheetToArray($entry);
$values = $this->invoiceItemToArray($entry);
foreach ($values as $search => $replace) {
$replace = $xmlEscaper->escape($replace);
$replace = str_replace(PHP_EOL, '</w:t><w:br /><w:t xml:space="preserve">', $replace);

View File

@@ -176,7 +176,17 @@ trait RendererTrait
return $values;
}
/**
* @deprecated since 1.3 - will be removed with 2.0
*/
protected function timesheetToArray(InvoiceItem $invoiceItem): array
{
@trigger_error('timesheetToArray() is deprecated and will be removed with 2.0', E_USER_DEPRECATED);
return $this->invoiceItemToArray($invoiceItem);
}
protected function invoiceItemToArray(InvoiceItem $invoiceItem): array
{
$rate = $invoiceItem->getRate();
$hourlyRate = $invoiceItem->getHourlyRate();

View File

@@ -13,43 +13,37 @@ use App\Entity\InvoiceDocument;
use App\Repository\InvoiceDocumentRepository;
/**
* A service to manage invoice dependencies.
* Service to manage invoice dependencies.
*/
class ServiceInvoice
final class ServiceInvoice
{
/**
* @var CalculatorInterface[]
*/
protected $calculator = [];
private $calculator = [];
/**
* @var RendererInterface[]
*/
protected $renderer = [];
private $renderer = [];
/**
* @var NumberGeneratorInterface[]
*/
protected $numberGenerator = [];
private $numberGenerator = [];
/**
* @var array InvoiceItemRepositoryInterface[]
*/
private $invoiceItemRepositories = [];
/**
* @var InvoiceDocumentRepository
*/
protected $documents;
private $documents;
/**
* @param InvoiceDocumentRepository $repository
*/
public function __construct(InvoiceDocumentRepository $repository)
{
$this->documents = $repository;
}
/**
* @param NumberGeneratorInterface $generator
* @return $this
*/
public function addNumberGenerator(NumberGeneratorInterface $generator)
public function addNumberGenerator(NumberGeneratorInterface $generator): ServiceInvoice
{
$this->numberGenerator[] = $generator;
@@ -59,16 +53,12 @@ class ServiceInvoice
/**
* @return NumberGeneratorInterface[]
*/
public function getNumberGenerator()
public function getNumberGenerator(): array
{
return $this->numberGenerator;
}
/**
* @param string $name
* @return NumberGeneratorInterface|null
*/
public function getNumberGeneratorByName(string $name)
public function getNumberGeneratorByName(string $name): ?NumberGeneratorInterface
{
foreach ($this->getNumberGenerator() as $generator) {
if ($generator->getId() === $name) {
@@ -79,11 +69,7 @@ class ServiceInvoice
return null;
}
/**
* @param CalculatorInterface $calculator
* @return $this
*/
public function addCalculator(CalculatorInterface $calculator)
public function addCalculator(CalculatorInterface $calculator): ServiceInvoice
{
$this->calculator[] = $calculator;
@@ -93,16 +79,12 @@ class ServiceInvoice
/**
* @return CalculatorInterface[]
*/
public function getCalculator()
public function getCalculator(): array
{
return $this->calculator;
}
/**
* @param string $name
* @return CalculatorInterface|null
*/
public function getCalculatorByName(string $name)
public function getCalculatorByName(string $name): ?CalculatorInterface
{
foreach ($this->getCalculator() as $calculator) {
if ($calculator->getId() === $name) {
@@ -113,11 +95,7 @@ class ServiceInvoice
return null;
}
/**
* @param string $name
* @return InvoiceDocument|null
*/
public function getDocumentByName(string $name)
public function getDocumentByName(string $name): ?InvoiceDocument
{
return $this->documents->findByName($name);
}
@@ -127,16 +105,12 @@ class ServiceInvoice
*
* @return InvoiceDocument[]
*/
public function getDocuments()
public function getDocuments(): array
{
return $this->documents->findAll();
}
/**
* @param RendererInterface $renderer
* @return $this
*/
public function addRenderer(RendererInterface $renderer)
public function addRenderer(RendererInterface $renderer): ServiceInvoice
{
$this->renderer[] = $renderer;
@@ -148,8 +122,23 @@ class ServiceInvoice
*
* @return RendererInterface[]
*/
public function getRenderer()
public function getRenderer(): array
{
return $this->renderer;
}
/**
* @return InvoiceItemRepositoryInterface[]
*/
public function getInvoiceItemRepositories(): array
{
return $this->invoiceItemRepositories;
}
public function addInvoiceItemRepository(InvoiceItemRepositoryInterface $invoiceItemRepository): ServiceInvoice
{
$this->invoiceItemRepositories[] = $invoiceItemRepository;
return $this;
}
}