added new rounding mode: closest (#611)
This commit is contained in:
@@ -11,6 +11,7 @@ namespace App\DependencyInjection;
|
||||
|
||||
use App\Model\DashboardSection;
|
||||
use App\Model\Widget;
|
||||
use App\Timesheet\Rounding\RoundingInterface;
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||
|
||||
@@ -81,6 +82,21 @@ class Configuration implements ConfigurationInterface
|
||||
->integerNode('duration')
|
||||
->defaultValue(0)
|
||||
->end()
|
||||
->scalarNode('mode')
|
||||
->defaultValue('default')
|
||||
->validate()
|
||||
->thenInvalid('Chosen rounding mode is invalid')
|
||||
->ifTrue(function ($value) {
|
||||
$class = 'App\\Timesheet\\Rounding\\' . ucfirst($value) . 'Rounding';
|
||||
if (class_exists($class)) {
|
||||
$rounding = new $class();
|
||||
return !($rounding instanceof RoundingInterface);
|
||||
}
|
||||
return false;
|
||||
})
|
||||
->thenInvalid('Chosen rounding mode is invalid')
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->defaultValue([])
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace App\Timesheet\Calculator;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
use App\Timesheet\CalculatorInterface;
|
||||
use App\Timesheet\Rounding\RoundingInterface;
|
||||
|
||||
/**
|
||||
* Implementation to calculate the durations for a timesheet record.
|
||||
@@ -66,74 +67,14 @@ class DurationCalculator implements CalculatorInterface
|
||||
$days = array_map('strtolower', $rounding['days']);
|
||||
|
||||
if (in_array(strtolower($weekday), $days)) {
|
||||
$this->roundBegin($record, $rounding['begin']);
|
||||
$this->roundEnd($record, $rounding['end']);
|
||||
$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);
|
||||
$this->roundDuration($record, $rounding['duration']);
|
||||
$rounder->roundDuration($record, $rounding['duration']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
* @param int $minutes
|
||||
*/
|
||||
protected function roundBegin(Timesheet $record, $minutes)
|
||||
{
|
||||
if ($minutes <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$timestamp = $record->getBegin()->getTimestamp();
|
||||
$seconds = $minutes * 60;
|
||||
$diff = $timestamp % $seconds;
|
||||
|
||||
if (0 === $diff) {
|
||||
return;
|
||||
}
|
||||
|
||||
$record->getBegin()->setTimestamp($timestamp - $diff);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
* @param int $minutes
|
||||
*/
|
||||
protected function roundEnd(Timesheet $record, $minutes)
|
||||
{
|
||||
if ($minutes <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$timestamp = $record->getEnd()->getTimestamp();
|
||||
$seconds = $minutes * 60;
|
||||
$diff = $timestamp % $seconds;
|
||||
|
||||
if (0 === $diff) {
|
||||
return;
|
||||
}
|
||||
|
||||
$record->getEnd()->setTimestamp($timestamp - $diff + $seconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
* @param int $minutes
|
||||
*/
|
||||
protected function roundDuration(Timesheet $record, $minutes)
|
||||
{
|
||||
if ($minutes <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$timestamp = $record->getDuration();
|
||||
$seconds = $minutes * 60;
|
||||
$diff = $timestamp % $seconds;
|
||||
|
||||
if (0 === $diff) {
|
||||
return;
|
||||
}
|
||||
|
||||
$record->setDuration($timestamp - $diff + $seconds);
|
||||
}
|
||||
}
|
||||
|
||||
90
src/Timesheet/Rounding/ClosestRounding.php
Normal file
90
src/Timesheet/Rounding/ClosestRounding.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?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\Rounding;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
|
||||
class ClosestRounding implements RoundingInterface
|
||||
{
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
* @param int $minutes
|
||||
*/
|
||||
public function roundBegin(Timesheet $record, $minutes)
|
||||
{
|
||||
if ($minutes <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$timestamp = $record->getBegin()->getTimestamp();
|
||||
$seconds = $minutes * 60;
|
||||
$diff = $timestamp % $seconds;
|
||||
|
||||
if (0 === $diff) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($diff > ($seconds / 2)) {
|
||||
$record->getBegin()->setTimestamp($timestamp - $diff + $seconds);
|
||||
} else {
|
||||
$record->getBegin()->setTimestamp($timestamp - $diff);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
* @param int $minutes
|
||||
*/
|
||||
public function roundEnd(Timesheet $record, $minutes)
|
||||
{
|
||||
if ($minutes <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$timestamp = $record->getEnd()->getTimestamp();
|
||||
$seconds = $minutes * 60;
|
||||
$diff = $timestamp % $seconds;
|
||||
|
||||
if (0 === $diff) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($diff > ($seconds / 2)) {
|
||||
$record->getEnd()->setTimestamp($timestamp - $diff + $seconds);
|
||||
} else {
|
||||
$record->getEnd()->setTimestamp($timestamp - $diff);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
* @param int $minutes
|
||||
*/
|
||||
public function roundDuration(Timesheet $record, $minutes)
|
||||
{
|
||||
if ($minutes <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$timestamp = $record->getDuration();
|
||||
$seconds = $minutes * 60;
|
||||
$diff = $timestamp % $seconds;
|
||||
|
||||
if (0 === $diff) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($diff > ($seconds / 2)) {
|
||||
$record->setDuration($timestamp - $diff + $seconds);
|
||||
} else {
|
||||
$record->setDuration($timestamp - $diff);
|
||||
}
|
||||
}
|
||||
}
|
||||
78
src/Timesheet/Rounding/DefaultRounding.php
Normal file
78
src/Timesheet/Rounding/DefaultRounding.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?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\Rounding;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
|
||||
class DefaultRounding implements RoundingInterface
|
||||
{
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
* @param int $minutes
|
||||
*/
|
||||
public function roundBegin(Timesheet $record, $minutes)
|
||||
{
|
||||
if ($minutes <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$timestamp = $record->getBegin()->getTimestamp();
|
||||
$seconds = $minutes * 60;
|
||||
$diff = $timestamp % $seconds;
|
||||
|
||||
if (0 === $diff) {
|
||||
return;
|
||||
}
|
||||
|
||||
$record->getBegin()->setTimestamp($timestamp - $diff);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
* @param int $minutes
|
||||
*/
|
||||
public function roundEnd(Timesheet $record, $minutes)
|
||||
{
|
||||
if ($minutes <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$timestamp = $record->getEnd()->getTimestamp();
|
||||
$seconds = $minutes * 60;
|
||||
$diff = $timestamp % $seconds;
|
||||
|
||||
if (0 === $diff) {
|
||||
return;
|
||||
}
|
||||
|
||||
$record->getEnd()->setTimestamp($timestamp - $diff + $seconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
* @param int $minutes
|
||||
*/
|
||||
public function roundDuration(Timesheet $record, $minutes)
|
||||
{
|
||||
if ($minutes <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$timestamp = $record->getDuration();
|
||||
$seconds = $minutes * 60;
|
||||
$diff = $timestamp % $seconds;
|
||||
|
||||
if (0 === $diff) {
|
||||
return;
|
||||
}
|
||||
|
||||
$record->setDuration($timestamp - $diff + $seconds);
|
||||
}
|
||||
}
|
||||
37
src/Timesheet/Rounding/RoundingInterface.php
Normal file
37
src/Timesheet/Rounding/RoundingInterface.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?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\Rounding;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
|
||||
/**
|
||||
* Apply rounding rules to the given timesheet.
|
||||
*/
|
||||
interface RoundingInterface
|
||||
{
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
* @param int $minutes
|
||||
*/
|
||||
public function roundBegin(Timesheet $record, $minutes);
|
||||
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
* @param int $minutes
|
||||
*/
|
||||
public function roundEnd(Timesheet $record, $minutes);
|
||||
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
* @param $minutes
|
||||
*/
|
||||
public function roundDuration(Timesheet $record, $minutes);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user