improved invoice handling for fixed rates (#1058)

This commit is contained in:
Kevin Papst
2019-08-24 00:02:16 +02:00
committed by GitHub
parent 46e5650882
commit ceeec7b8a6
40 changed files with 581 additions and 131 deletions

View File

@@ -9,8 +9,8 @@
namespace App\Invoice\Calculator;
use App\Entity\Timesheet;
use App\Model\InvoiceModel;
use App\Invoice\InvoiceItem;
use App\Invoice\InvoiceModel;
abstract class AbstractCalculator
{
@@ -25,7 +25,7 @@ abstract class AbstractCalculator
protected $model;
/**
* @return Timesheet[]
* @return InvoiceItem[]
*/
abstract public function getEntries();
@@ -103,7 +103,9 @@ abstract class AbstractCalculator
{
$time = 0;
foreach ($this->model->getEntries() as $entry) {
$time += $entry->getDuration();
if (null === $entry->getFixedRate()) {
$time += $entry->getDuration();
}
}
return $time;

View File

@@ -10,52 +10,60 @@
namespace App\Invoice\Calculator;
use App\Entity\Timesheet;
use App\Invoice\InvoiceItem;
abstract class AbstractMergedCalculator extends AbstractCalculator
{
/**
* @param Timesheet $timesheet
* @param Timesheet $entry
*/
protected function mergeTimesheets(Timesheet $timesheet, Timesheet $entry)
protected function mergeTimesheets(InvoiceItem $invoiceItem, Timesheet $entry)
{
$timesheet->setUser($entry->getUser());
$timesheet->setRate($timesheet->getRate() + $entry->getRate());
$invoiceItem->setAmount($invoiceItem->getAmount() + 1);
$invoiceItem->setUser($entry->getUser());
$invoiceItem->setRate($invoiceItem->getRate() + $entry->getRate());
$invoiceItem->setDuration($invoiceItem->getDuration() + $entry->getDuration());
if (null !== $timesheet->getFixedRate() || null !== $entry->getFixedRate()) {
$timesheet->setFixedRate($timesheet->getRate());
}
if (null === $timesheet->getHourlyRate() && null !== $entry->getHourlyRate() && $timesheet->getHourlyRate() !== $entry->getHourlyRate()) {
$timesheet->setHourlyRate($entry->getHourlyRate());
if (null !== $entry->getFixedRate()) {
/*
if (null !== $invoiceItem->getFixedRate() && $invoiceItem->getFixedRate() !== $entry->getFixedRate()) {
throw new \InvalidArgumentException('Cannot mix different fixed-rates');
}
*/
$invoiceItem->setFixedRate($entry->getFixedRate());
}
$timesheet->setDuration($timesheet->getDuration() + $entry->getDuration());
if (null === $timesheet->getBegin() || $timesheet->getBegin()->getTimestamp() > $entry->getBegin()->getTimestamp()) {
$timesheet->setBegin($entry->getBegin());
if (null !== $entry->getHourlyRate()) {
/*
if (null !== $invoiceItem->getHourlyRate() && $invoiceItem->getHourlyRate() !== $entry->getHourlyRate()) {
throw new \InvalidArgumentException('Cannot mix different hourly-rates');
}
*/
$invoiceItem->setHourlyRate($entry->getHourlyRate());
}
if (null === $timesheet->getEnd() || $timesheet->getEnd()->getTimestamp() < $entry->getEnd()->getTimestamp()) {
$timesheet->setEnd($entry->getEnd());
if (null === $invoiceItem->getBegin() || $invoiceItem->getBegin()->getTimestamp() > $entry->getBegin()->getTimestamp()) {
$invoiceItem->setBegin($entry->getBegin());
}
if (null === $invoiceItem->getEnd() || $invoiceItem->getEnd()->getTimestamp() < $entry->getEnd()->getTimestamp()) {
$invoiceItem->setEnd($entry->getEnd());
}
if (null !== $this->model->getQuery()->getActivity()) {
$timesheet->setActivity($this->model->getQuery()->getActivity());
$timesheet->setDescription($this->model->getQuery()->getActivity()->getName());
$invoiceItem->setActivity($this->model->getQuery()->getActivity());
$invoiceItem->setDescription($this->model->getQuery()->getActivity()->getName());
} elseif (null !== $this->model->getQuery()->getProject()) {
$timesheet->setDescription($this->model->getQuery()->getProject()->getName());
$invoiceItem->setDescription($this->model->getQuery()->getProject()->getName());
}
if (null === $timesheet->getActivity()) {
$timesheet->setActivity($entry->getActivity());
if (null === $invoiceItem->getActivity()) {
$invoiceItem->setActivity($entry->getActivity());
}
if (null === $timesheet->getProject()) {
$timesheet->setProject($entry->getProject());
if (null === $invoiceItem->getProject()) {
$invoiceItem->setProject($entry->getProject());
}
if (empty($timesheet->getDescription())) {
$timesheet->setDescription($entry->getActivity()->getName());
if (empty($invoiceItem->getDescription())) {
$invoiceItem->setDescription($entry->getActivity()->getName());
}
}
}

View File

@@ -11,16 +11,17 @@ namespace App\Invoice\Calculator;
use App\Entity\Timesheet;
use App\Invoice\CalculatorInterface;
use App\Invoice\InvoiceItem;
/**
* A calculator that sums up the timesheet records by activity.
* An abstract calculator that sums up the timesheet records.
*/
abstract class AbstractSumInvoiceCalculator extends AbstractMergedCalculator implements CalculatorInterface
{
abstract protected function calculateSumIdentifier(Timesheet $timesheet): string;
/**
* @return Timesheet[]
* @return InvoiceItem[]
*/
public function getEntries()
{
@@ -29,18 +30,25 @@ abstract class AbstractSumInvoiceCalculator extends AbstractMergedCalculator imp
return [];
}
/** @var Timesheet[] $timesheets */
$timesheets = [];
/** @var InvoiceItem[] $invoiceItems */
$invoiceItems = [];
foreach ($entries as $entry) {
$id = $this->calculateSumIdentifier($entry);
if (!isset($timesheets[$id])) {
$timesheets[$id] = new Timesheet();
if (null !== $entry->getFixedRate()) {
$id = $id . '_fixed_' . (string) $entry->getFixedRate();
} else {
$id = $id . '_hourly_' . (string) $entry->getHourlyRate();
}
$timesheet = $timesheets[$id];
if (!isset($invoiceItems[$id])) {
$invoiceItems[$id] = new InvoiceItem();
}
$timesheet = $invoiceItems[$id];
$this->mergeTimesheets($timesheet, $entry);
}
return array_values($timesheets);
return array_values($invoiceItems);
}
}

View File

@@ -19,6 +19,10 @@ class DateInvoiceCalculator extends AbstractSumInvoiceCalculator implements Calc
{
protected function calculateSumIdentifier(Timesheet $timesheet): string
{
if (null === $timesheet->getBegin()) {
throw new \Exception('Cannot handle timesheets without start date');
}
return $timesheet->getBegin()->format('Y-m-d');
}

View File

@@ -9,8 +9,8 @@
namespace App\Invoice\Calculator;
use App\Entity\Timesheet;
use App\Invoice\CalculatorInterface;
use App\Invoice\InvoiceItem;
/**
* Class DefaultCalculator works on all given entries using:
@@ -18,14 +18,25 @@ use App\Invoice\CalculatorInterface;
* - the invoice template vat rate
* - the entries rate
*/
class DefaultCalculator extends AbstractCalculator implements CalculatorInterface
class DefaultCalculator extends AbstractMergedCalculator implements CalculatorInterface
{
/**
* @return Timesheet[]
* @return InvoiceItem[]
*/
public function getEntries()
{
return $this->model->getEntries();
$entries = [];
foreach ($this->model->getEntries() as $entry) {
$item = new InvoiceItem();
$this->mergeTimesheets($item, $entry);
foreach ($entry->getVisibleMetaFields() as $field) {
$item->addAdditionalField($field->getName(), $field->getValue());
}
$entries[] = $item;
}
return $entries;
}
/**

View File

@@ -11,6 +11,7 @@ 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
@@ -19,7 +20,7 @@ use App\Invoice\CalculatorInterface;
class ShortInvoiceCalculator extends AbstractMergedCalculator implements CalculatorInterface
{
/**
* @return Timesheet[]
* @return InvoiceItem[]
*/
public function getEntries()
{
@@ -28,16 +29,27 @@ class ShortInvoiceCalculator extends AbstractMergedCalculator implements Calcula
return [];
}
$timesheet = new Timesheet();
$invoiceItem = new InvoiceItem();
$keys = [];
foreach ($entries as $entry) {
$this->mergeTimesheets($timesheet, $entry);
$key = 'hourly_' . (string) $entry->getHourlyRate();
if (null !== $entry->getFixedRate()) {
$key = 'fixed_' . (string) $entry->getFixedRate();
}
if (!in_array($key, $keys)) {
$keys[] = $key;
}
$this->mergeTimesheets($invoiceItem, $entry);
}
$timesheet->setFixedRate($timesheet->getRate());
$timesheet->setHourlyRate($timesheet->getRate());
if (count($keys) > 1) {
$invoiceItem->setAmount(1);
$invoiceItem->setFixedRate($invoiceItem->getRate());
}
$invoiceItem->setHourlyRate($invoiceItem->getRate());
return [$timesheet];
return [$invoiceItem];
}
/**

View File

@@ -9,8 +9,8 @@
namespace App\Invoice\Calculator;
use App\Entity\Timesheet;
use App\Invoice\CalculatorInterface;
use App\Invoice\InvoiceItem;
/**
* A calculator that sums up the timesheet records by user.
@@ -18,7 +18,7 @@ use App\Invoice\CalculatorInterface;
class UserInvoiceCalculator extends AbstractMergedCalculator implements CalculatorInterface
{
/**
* @return Timesheet[]
* @return InvoiceItem[]
*/
public function getEntries()
{
@@ -27,18 +27,19 @@ class UserInvoiceCalculator extends AbstractMergedCalculator implements Calculat
return [];
}
/** @var Timesheet[] $timesheets */
$timesheets = [];
/** @var InvoiceItem[] $invoiceItems */
$invoiceItems = [];
foreach ($entries as $entry) {
if (!isset($timesheets[$entry->getUser()->getId()])) {
$timesheets[$entry->getUser()->getId()] = new Timesheet();
$id = $entry->getUser()->getId();
if (!isset($invoiceItems[$id])) {
$invoiceItems[$id] = new InvoiceItem();
}
$timesheet = $timesheets[$entry->getUser()->getId()];
$this->mergeTimesheets($timesheet, $entry);
$invoiceItem = $invoiceItems[$id];
$this->mergeTimesheets($invoiceItem, $entry);
}
return array_values($timesheets);
return array_values($invoiceItems);
}
/**

View File

@@ -9,18 +9,15 @@
namespace App\Invoice;
use App\Entity\Timesheet;
use App\Model\InvoiceModel;
/**
* CalculatorInterface defines all methods for any invoice price calculator.
*/
interface CalculatorInterface
{
/**
* Return the timesheet records that will be displayed on the invoice.
* Return the invoice items that will be displayed on the invoice.
*
* @return Timesheet[]
* @return InvoiceItem[]
*/
public function getEntries();

215
src/Invoice/InvoiceItem.php Normal file
View File

@@ -0,0 +1,215 @@
<?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\Project;
use App\Entity\User;
class InvoiceItem
{
/**
* @var float
*/
private $fixedRate;
/**
* @var float
*/
private $hourlyRate;
/**
* @var float
*/
private $rate = 0.00;
/**
* @var int
*/
private $amount = 0;
/**
* @var string
*/
private $description;
/**
* @var int
*/
private $duration = 0;
/**
* @var \DateTime
*/
private $begin;
/**
* @var \DateTime
*/
private $end;
/**
* @var User
*/
private $user;
/**
* @var Activity
*/
private $activity;
/**
* @var Project
*/
private $project;
/**
* @var array
*/
private $additionalFields = [];
public function addAdditionalField(string $name, string $value): InvoiceItem
{
$this->additionalFields[$name] = $value;
return $this;
}
public function getAdditionalFields(): array
{
return $this->additionalFields;
}
public function getActivity(): ?Activity
{
return $this->activity;
}
public function setActivity(?Activity $activity): InvoiceItem
{
$this->activity = $activity;
return $this;
}
public function getProject(): ?Project
{
return $this->project;
}
public function setProject(?Project $project): InvoiceItem
{
$this->project = $project;
return $this;
}
public function isFixedRate(): bool
{
return null !== $this->getFixedRate();
}
public function getFixedRate(): ?float
{
return $this->fixedRate;
}
public function setFixedRate(?float $fixedRate): InvoiceItem
{
$this->fixedRate = $fixedRate;
return $this;
}
public function getHourlyRate(): ?float
{
return $this->hourlyRate;
}
public function setHourlyRate(float $hourlyRate): InvoiceItem
{
$this->hourlyRate = $hourlyRate;
return $this;
}
public function getRate(): float
{
return $this->rate;
}
public function setRate(float $rate): InvoiceItem
{
$this->rate = $rate;
return $this;
}
public function getAmount(): int
{
return $this->amount;
}
public function setAmount(int $amount): InvoiceItem
{
$this->amount = $amount;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): InvoiceItem
{
$this->description = $description;
return $this;
}
public function getDuration(): int
{
return $this->duration;
}
public function setDuration(int $duration): InvoiceItem
{
$this->duration = $duration;
return $this;
}
public function getBegin(): ?\DateTime
{
return $this->begin;
}
public function setBegin(\DateTime $begin): InvoiceItem
{
$this->begin = $begin;
return $this;
}
public function getEnd(): ?\DateTime
{
return $this->end;
}
public function setEnd(?\DateTime $end): InvoiceItem
{
$this->end = $end;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): InvoiceItem
{
$this->user = $user;
return $this;
}
}

View File

@@ -0,0 +1,173 @@
<?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\Customer;
use App\Entity\InvoiceTemplate;
use App\Entity\Timesheet;
use App\Repository\Query\InvoiceQuery;
/**
* InvoiceModel is the ONLY value that a RendererInterface receives for generating the invoice,
* besides the InvoiceDocument which is used as a "template".
*/
class InvoiceModel
{
/**
* @var Customer|null
*/
protected $customer;
/**
* @var InvoiceQuery
*/
protected $query;
/**
* @var Timesheet[]
*/
protected $entries = [];
/**
* @var InvoiceTemplate
*/
protected $template;
/**
* @var CalculatorInterface
*/
protected $calculator;
/**
* @var NumberGeneratorInterface
*/
protected $generator;
/**
* @var \DateTime
*/
protected $invoiceDate;
public function __construct()
{
$this->invoiceDate = new \DateTime();
}
/**
* @return InvoiceQuery
*/
public function getQuery(): ?InvoiceQuery
{
return $this->query;
}
/**
* @param InvoiceQuery $query
* @return InvoiceModel
*/
public function setQuery(InvoiceQuery $query)
{
$this->query = $query;
return $this;
}
/**
* Do not use this method for rendering the invoice, use InvoiceModel::getCalculator()->getEntries() instead.
*
* @return Timesheet[]
*/
public function getEntries(): array
{
return $this->entries;
}
/**
* @param Timesheet[] $entries
* @return InvoiceModel
*/
public function setEntries(array $entries): InvoiceModel
{
$this->entries = $entries;
return $this;
}
public function getTemplate(): ?InvoiceTemplate
{
return $this->template;
}
public function setTemplate(InvoiceTemplate $template): InvoiceModel
{
$this->template = $template;
return $this;
}
public function getCustomer(): ?Customer
{
return $this->customer;
}
/**
* @param Customer $customer
* @return InvoiceModel
*/
public function setCustomer($customer): InvoiceModel
{
$this->customer = $customer;
return $this;
}
public function getDueDate(): ?\DateTime
{
if (null === $this->getTemplate()) {
return null;
}
return new \DateTime('+' . $this->getTemplate()->getDueDays() . ' days');
}
/**
* @return \DateTime
*/
public function getInvoiceDate(): \DateTime
{
return $this->invoiceDate;
}
public function setNumberGenerator(NumberGeneratorInterface $generator): InvoiceModel
{
$this->generator = $generator;
$this->generator->setModel($this);
return $this;
}
public function getNumberGenerator(): ?NumberGeneratorInterface
{
return $this->generator;
}
public function setCalculator(CalculatorInterface $calculator): InvoiceModel
{
$this->calculator = $calculator;
$this->calculator->setModel($this);
return $this;
}
public function getCalculator(): ?CalculatorInterface
{
return $this->calculator;
}
}

View File

@@ -9,8 +9,8 @@
namespace App\Invoice\NumberGenerator;
use App\Invoice\InvoiceModel;
use App\Invoice\NumberGeneratorInterface;
use App\Model\InvoiceModel;
/**
* Class DateNumberGenerator generates the invoice number based on the current day.

View File

@@ -9,8 +9,6 @@
namespace App\Invoice;
use App\Model\InvoiceModel;
/**
* Class NumberGeneratorInterface defines all methods that invoice number generator have to implement.
*/

View File

@@ -10,7 +10,7 @@
namespace App\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Model\InvoiceModel;
use App\Invoice\InvoiceModel;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

View File

@@ -10,8 +10,8 @@
namespace App\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Invoice\InvoiceModel;
use App\Invoice\RendererInterface;
use App\Model\InvoiceModel;
use PhpOffice\PhpWord\Escaper\Xml;
use PhpOffice\PhpWord\Exception\Exception as OfficeException;
use PhpOffice\PhpWord\Settings;

View File

@@ -10,9 +10,9 @@
namespace App\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Entity\Timesheet;
use App\Entity\UserPreference;
use App\Model\InvoiceModel;
use App\Invoice\InvoiceItem;
use App\Invoice\InvoiceModel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
@@ -176,40 +176,36 @@ trait RendererTrait
return $values;
}
/**
* @param Timesheet $timesheet
* @return array
*/
protected function timesheetToArray(Timesheet $timesheet)
protected function timesheetToArray(InvoiceItem $invoiceItem): array
{
$rate = $timesheet->getRate();
$hourlyRate = $timesheet->getHourlyRate();
$amount = $this->getFormattedDuration($timesheet->getDuration());
$description = $timesheet->getDescription();
$rate = $invoiceItem->getRate();
$hourlyRate = $invoiceItem->getHourlyRate();
$amount = $this->getFormattedDuration($invoiceItem->getDuration());
$description = $invoiceItem->getDescription();
if (null !== $timesheet->getFixedRate()) {
$rate = $timesheet->getFixedRate();
$hourlyRate = $timesheet->getFixedRate();
$amount = 1; // FIXME fixed rates
if (null !== $invoiceItem->getFixedRate()) {
$rate = $invoiceItem->getFixedRate();
$hourlyRate = $invoiceItem->getFixedRate();
$amount = $invoiceItem->getAmount();
}
if (empty($description)) {
$description = $timesheet->getActivity()->getName();
$description = $invoiceItem->getActivity()->getName();
}
$user = $timesheet->getUser();
$user = $invoiceItem->getUser();
if (empty($hourlyRate)) {
$hourlyRate = $user->getPreferenceValue(UserPreference::HOURLY_RATE);
}
$activity = $timesheet->getActivity();
$project = $timesheet->getProject();
$activity = $invoiceItem->getActivity();
$project = $invoiceItem->getProject();
$customer = $project->getCustomer();
$currency = $customer->getCurrency();
$begin = $timesheet->getBegin();
$end = $timesheet->getEnd();
$begin = $invoiceItem->getBegin();
$end = $invoiceItem->getEnd();
$values = [
'entry.row' => '',
@@ -218,9 +214,9 @@ trait RendererTrait
'entry.rate' => $this->getFormattedMoney($hourlyRate, $currency),
'entry.total' => $this->getFormattedMoney($rate, $currency),
'entry.currency' => $currency,
'entry.duration' => $timesheet->getDuration(),
'entry.duration_decimal' => $this->getFormattedDecimalDuration($timesheet->getDuration()),
'entry.duration_minutes' => number_format($timesheet->getDuration() / 60),
'entry.duration' => $invoiceItem->getDuration(),
'entry.duration_decimal' => $this->getFormattedDecimalDuration($invoiceItem->getDuration()),
'entry.duration_minutes' => number_format($invoiceItem->getDuration() / 60),
'entry.begin' => $this->getFormattedDateTime($begin),
'entry.begin_time' => $this->getFormattedTime($begin),
'entry.begin_timestamp' => $begin->getTimestamp(),
@@ -240,9 +236,9 @@ trait RendererTrait
'entry.customer_id' => $customer->getId(),
];
foreach ($timesheet->getVisibleMetaFields() as $metaField) {
foreach ($invoiceItem->getAdditionalFields() as $name => $value) {
$values = array_merge($values, [
'entry.meta.' . $metaField->getName() => $metaField->getValue(),
'entry.meta.' . $name => $value,
]);
}

View File

@@ -10,8 +10,8 @@
namespace App\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Invoice\InvoiceModel;
use App\Invoice\RendererInterface;
use App\Model\InvoiceModel;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

View File

@@ -10,7 +10,6 @@
namespace App\Invoice;
use App\Entity\InvoiceDocument;
use App\Model\InvoiceModel;
use Symfony\Component\HttpFoundation\Response;
interface RendererInterface