rounding rules via admin screen, round begin when starting record (#1229)

This commit is contained in:
Kevin Papst
2019-11-10 13:57:05 +01:00
committed by GitHub
parent fa1c79e15c
commit c6c4098759
47 changed files with 1100 additions and 185 deletions

View File

@@ -11,26 +11,19 @@ namespace App\Timesheet\Calculator;
use App\Entity\Timesheet;
use App\Timesheet\CalculatorInterface;
use App\Timesheet\Rounding\RoundingInterface;
use App\Timesheet\RoundingService;
/**
* Implementation to calculate the durations for a timesheet record.
*
* This calculator takes the configuration %kimai.timesheet.rounding% as argument,
* so its rounding behaviour can be customized.
*/
class DurationCalculator implements CalculatorInterface
final class DurationCalculator implements CalculatorInterface
{
/**
* @var array
* @var RoundingService
*/
protected $roundings;
private $roundings;
/**
* DurationCalculator constructor.
* @param array $roundings
*/
public function __construct(array $roundings)
public function __construct(RoundingService $roundings)
{
$this->roundings = $roundings;
}
@@ -44,37 +37,9 @@ class DurationCalculator implements CalculatorInterface
return;
}
$this->applyDuration($record);
$this->applyRoundings($record);
}
/**
* @param Timesheet $record
*/
protected function applyDuration(Timesheet $record)
{
$duration = $record->getEnd()->getTimestamp() - $record->getBegin()->getTimestamp();
$record->setDuration($duration);
}
/**
* @param Timesheet $record
*/
protected function applyRoundings(Timesheet $record)
{
foreach ($this->roundings as $rounding) {
$weekday = $record->getEnd()->format('l');
$days = array_map('strtolower', $rounding['days']);
if (in_array(strtolower($weekday), $days)) {
$class = 'App\\Timesheet\\Rounding\\' . ucfirst($rounding['mode']) . 'Rounding';
/* @var $rounder RoundingInterface */
$rounder = new $class();
$rounder->roundBegin($record, $rounding['begin']);
$rounder->roundEnd($record, $rounding['end']);
$this->applyDuration($record);
$rounder->roundDuration($record, $rounding['duration']);
}
}
$this->roundings->applyRoundings($record);
}
}

View File

@@ -11,8 +11,13 @@ namespace App\Timesheet\Rounding;
use App\Entity\Timesheet;
class CeilRounding implements RoundingInterface
final class CeilRounding implements RoundingInterface
{
public function getId(): string
{
return 'ceil';
}
/**
* @param Timesheet $record
* @param int $minutes

View File

@@ -11,8 +11,13 @@ namespace App\Timesheet\Rounding;
use App\Entity\Timesheet;
class ClosestRounding implements RoundingInterface
final class ClosestRounding implements RoundingInterface
{
public function getId(): string
{
return 'closest';
}
/**
* @param Timesheet $record
* @param int $minutes

View File

@@ -11,8 +11,13 @@ namespace App\Timesheet\Rounding;
use App\Entity\Timesheet;
class DefaultRounding implements RoundingInterface
final class DefaultRounding implements RoundingInterface
{
public function getId(): string
{
return 'default';
}
/**
* @param Timesheet $record
* @param int $minutes

View File

@@ -11,8 +11,13 @@ namespace App\Timesheet\Rounding;
use App\Entity\Timesheet;
class FloorRounding implements RoundingInterface
final class FloorRounding implements RoundingInterface
{
public function getId(): string
{
return 'floor';
}
/**
* @param Timesheet $record
* @param int $minutes

View File

@@ -33,4 +33,9 @@ interface RoundingInterface
* @param int $minutes
*/
public function roundDuration(Timesheet $record, $minutes);
/**
* @return string
*/
public function getId(): string;
}

View File

@@ -0,0 +1,147 @@
<?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\Timesheet;
use App\Configuration\TimesheetConfiguration;
use App\Entity\Timesheet;
use App\Timesheet\Rounding\RoundingInterface;
final class RoundingService
{
/**
* @var array
*/
private $rules;
/**
* @var array
*/
private $rulesCache;
/**
* @var TimesheetConfiguration
*/
private $configuration;
/**
* @var RoundingInterface[]
*/
private $roundingModes;
/**
* @param TimesheetConfiguration $configuration
* @param RoundingInterface[] $roundingModes
* @param array $rules
*/
public function __construct(TimesheetConfiguration $configuration, iterable $roundingModes, array $rules)
{
$this->configuration = $configuration;
$this->roundingModes = $roundingModes;
$this->rules = $rules;
}
private function getRoundingRules(): array
{
if (empty($this->rulesCache)) {
$this->rulesCache = $this->rules;
if (empty($this->rulesCache) || array_key_exists('default', $this->rulesCache)) {
$this->rulesCache['default']['days'] = $this->configuration->getDefaultRoundingDays();
$this->rulesCache['default']['begin'] = $this->configuration->getDefaultRoundingBegin();
$this->rulesCache['default']['end'] = $this->configuration->getDefaultRoundingEnd();
$this->rulesCache['default']['duration'] = $this->configuration->getDefaultRoundingDuration();
$this->rulesCache['default']['mode'] = $this->configuration->getDefaultRoundingMode();
}
// see AppExtension, conversion from string to array due to system configuration ont allowing to store arrays
foreach ($this->rulesCache as $key => $settings) {
$days = explode(',', $settings['days']);
$days = array_map('trim', $days);
$days = array_map('strtolower', $days);
$this->rulesCache[$key]['days'] = $days;
}
}
return $this->rulesCache;
}
public function roundBegin(Timesheet $record): void
{
foreach ($this->getRoundingRules() as $rounding) {
$weekday = $record->getBegin()->format('l');
if (in_array(strtolower($weekday), $rounding['days'])) {
$rounder = $this->getRoundingMode($rounding['mode']);
$rounder->roundBegin($record, $rounding['begin']);
}
}
}
public function roundEnd(Timesheet $record): void
{
foreach ($this->getRoundingRules() as $rounding) {
$weekday = $record->getEnd()->format('l');
if (in_array(strtolower($weekday), $rounding['days'])) {
$rounder = $this->getRoundingMode($rounding['mode']);
$rounder->roundEnd($record, $rounding['end']);
}
}
}
public function roundDuration(Timesheet $record): void
{
foreach ($this->getRoundingRules() as $rounding) {
$weekday = $record->getEnd()->format('l');
if (in_array(strtolower($weekday), $rounding['days'])) {
$rounder = $this->getRoundingMode($rounding['mode']);
$rounder->roundDuration($record, $rounding['duration']);
}
}
}
public function applyRoundings(Timesheet $record): void
{
if (null === $record->getEnd()) {
return;
}
foreach ($this->getRoundingRules() as $rounding) {
$weekday = $record->getEnd()->format('l');
if (in_array(strtolower($weekday), $rounding['days'])) {
$rounder = $this->getRoundingMode($rounding['mode']);
$rounder->roundBegin($record, $rounding['begin']);
$rounder->roundEnd($record, $rounding['end']);
$duration = $record->getEnd()->getTimestamp() - $record->getBegin()->getTimestamp();
$record->setDuration($duration);
$rounder->roundDuration($record, $rounding['duration']);
}
}
}
/**
* @return RoundingInterface[]
*/
public function getRoundingModes(): iterable
{
return $this->roundingModes;
}
public function getRoundingMode(string $id): RoundingInterface
{
foreach ($this->roundingModes as $mode) {
if ($mode->getId() === $id) {
return $mode;
}
}
throw new \InvalidArgumentException('Unknown rounding mode: ' . $id);
}
}

View File

@@ -9,8 +9,25 @@
namespace App\Timesheet\TrackingMode;
class DefaultMode extends AbstractTrackingMode
use App\Configuration\TimesheetConfiguration;
use App\Entity\Timesheet;
use App\Timesheet\RoundingService;
use App\Timesheet\UserDateTimeFactory;
use Symfony\Component\HttpFoundation\Request;
final class DefaultMode extends AbstractTrackingMode
{
/**
* @var RoundingService
*/
private $rounding;
public function __construct(UserDateTimeFactory $dateTime, TimesheetConfiguration $configuration, RoundingService $rounding)
{
parent::__construct($dateTime, $configuration);
$this->rounding = $rounding;
}
public function canEditBegin(): bool
{
return true;
@@ -40,4 +57,23 @@ class DefaultMode extends AbstractTrackingMode
{
return true;
}
public function create(Timesheet $timesheet, Request $request): void
{
parent::create($timesheet, $request);
if (null === $timesheet->getBegin()) {
$timesheet->setBegin($this->dateTime->createDateTime());
}
$this->rounding->roundBegin($timesheet);
if (null !== $timesheet->getEnd()) {
$this->rounding->roundEnd($timesheet);
if (null !== $timesheet->getDuration()) {
$this->rounding->roundDuration($timesheet);
}
}
}
}

View File

@@ -14,16 +14,16 @@ use App\Entity\Timesheet;
use App\Timesheet\UserDateTimeFactory;
use Symfony\Component\HttpFoundation\Request;
class DurationFixedBeginMode implements TrackingModeInterface
final class DurationFixedBeginMode implements TrackingModeInterface
{
/**
* @var UserDateTimeFactory
*/
protected $dateTime;
private $dateTime;
/**
* @var TimesheetConfiguration
*/
protected $configuration;
private $configuration;
public function __construct(UserDateTimeFactory $dateTime, TimesheetConfiguration $configuration)
{

View File

@@ -12,7 +12,7 @@ namespace App\Timesheet\TrackingMode;
use App\Entity\Timesheet;
use Symfony\Component\HttpFoundation\Request;
class DurationOnlyMode extends AbstractTrackingMode
final class DurationOnlyMode extends AbstractTrackingMode
{
public function canEditBegin(): bool
{

View File

@@ -10,10 +10,21 @@
namespace App\Timesheet\TrackingMode;
use App\Entity\Timesheet;
use App\Timesheet\UserDateTimeFactory;
use Symfony\Component\HttpFoundation\Request;
class PunchInOutMode implements TrackingModeInterface
final class PunchInOutMode implements TrackingModeInterface
{
/**
* @var UserDateTimeFactory
*/
private $dateTime;
public function __construct(UserDateTimeFactory $dateTime)
{
$this->dateTime = $dateTime;
}
public function canEditBegin(): bool
{
return false;
@@ -36,6 +47,9 @@ class PunchInOutMode implements TrackingModeInterface
public function create(Timesheet $timesheet, Request $request): void
{
if (null === $timesheet->getBegin()) {
$timesheet->setBegin($this->dateTime->createDateTime());
}
}
public function getId(): string

View File

@@ -10,28 +10,28 @@
namespace App\Timesheet;
use App\Configuration\TimesheetConfiguration;
use App\Timesheet\TrackingMode\DefaultMode;
use App\Timesheet\TrackingMode\DurationFixedBeginMode;
use App\Timesheet\TrackingMode\DurationOnlyMode;
use App\Timesheet\TrackingMode\PunchInOutMode;
use App\Timesheet\TrackingMode\TrackingModeInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
class TrackingModeService
final class TrackingModeService
{
/**
* @var UserDateTimeFactory
* @var TrackingModeInterface[]
*/
protected $dateTime;
private $modes = [];
/**
* @var TimesheetConfiguration
*/
protected $configuration;
private $configuration;
public function __construct(UserDateTimeFactory $dateTime, TimesheetConfiguration $configuration)
/**
* @param TimesheetConfiguration $configuration
* @param TrackingModeInterface[] $modes
*/
public function __construct(TimesheetConfiguration $configuration, iterable $modes)
{
$this->dateTime = $dateTime;
$this->configuration = $configuration;
$this->modes = $modes;
}
/**
@@ -39,12 +39,7 @@ class TrackingModeService
*/
public function getModes(): iterable
{
return [
new DefaultMode($this->dateTime, $this->configuration),
new PunchInOutMode(),
new DurationOnlyMode($this->dateTime, $this->configuration),
new DurationFixedBeginMode($this->dateTime, $this->configuration),
];
return $this->modes;
}
public function getActiveMode(): TrackingModeInterface