project grouped invoice calculator (#892)

This commit is contained in:
Kevin Papst
2019-07-24 17:45:51 +02:00
committed by GitHub
parent 56e276939b
commit 53eda31179
12 changed files with 346 additions and 97 deletions

View File

@@ -69,7 +69,7 @@ abstract class AbstractCalculator
public function getTax(): float
{
$vat = $this->getVat();
if (0 == $vat) {
if (0 === $vat) {
return 0;
}

View File

@@ -20,9 +20,15 @@ abstract class AbstractMergedCalculator extends AbstractCalculator
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());
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());
}
$timesheet->setDuration($timesheet->getDuration() + $entry->getDuration());
if (null === $timesheet->getBegin() || $timesheet->getBegin()->getTimestamp() > $entry->getBegin()->getTimestamp()) {

View File

@@ -0,0 +1,46 @@
<?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 activity.
*/
abstract class AbstractSumInvoiceCalculator extends AbstractMergedCalculator implements CalculatorInterface
{
abstract protected function calculateSumIdentifier(Timesheet $timesheet): string;
/**
* @return Timesheet[]
*/
public function getEntries()
{
$entries = $this->model->getEntries();
if (empty($entries)) {
return [];
}
/** @var Timesheet[] $timesheets */
$timesheets = [];
foreach ($entries as $entry) {
$id = $this->calculateSumIdentifier($entry);
if (!isset($timesheets[$id])) {
$timesheets[$id] = new Timesheet();
}
$timesheet = $timesheets[$id];
$this->mergeTimesheets($timesheet, $entry);
}
return array_values($timesheets);
}
}

View File

@@ -15,30 +15,15 @@ use App\Invoice\CalculatorInterface;
/**
* A calculator that sums up the timesheet records by activity.
*/
class ActivityInvoiceCalculator extends AbstractMergedCalculator implements CalculatorInterface
class ActivityInvoiceCalculator extends AbstractSumInvoiceCalculator implements CalculatorInterface
{
/**
* @return Timesheet[]
*/
public function getEntries()
protected function calculateSumIdentifier(Timesheet $timesheet): string
{
$entries = $this->model->getEntries();
if (empty($entries)) {
return [];
if (null === $timesheet->getActivity()->getId()) {
throw new \Exception('Cannot handle un-persisted activities');
}
/** @var Timesheet[] $timesheets */
$timesheets = [];
foreach ($entries as $entry) {
if (!isset($timesheets[$entry->getActivity()->getId()])) {
$timesheets[$entry->getActivity()->getId()] = new Timesheet();
}
$timesheet = $timesheets[$entry->getActivity()->getId()];
$this->mergeTimesheets($timesheet, $entry);
}
return array_values($timesheets);
return (string) $timesheet->getActivity()->getId();
}
/**

View File

@@ -0,0 +1,36 @@
<?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 project.
*/
class ProjectInvoiceCalculator extends AbstractSumInvoiceCalculator implements CalculatorInterface
{
protected function calculateSumIdentifier(Timesheet $timesheet): string
{
if (null === $timesheet->getProject()->getId()) {
throw new \Exception('Cannot handle un-persisted projects');
}
return (string) $timesheet->getProject()->getId();
}
/**
* @return string
*/
public function getId(): string
{
return 'project';
}
}