rounding rules via admin screen, round begin when starting record (#1229)
This commit is contained in:
@@ -11,6 +11,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\API;
|
||||
|
||||
use App\Entity\User;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
|
||||
|
||||
@@ -18,4 +19,12 @@ abstract class BaseApiController extends AbstractController
|
||||
{
|
||||
public const DATE_FORMAT = DateTimeType::HTML5_FORMAT;
|
||||
public const DATE_FORMAT_PHP = 'Y-m-d\TH:m:s';
|
||||
|
||||
/**
|
||||
* @return User|null
|
||||
*/
|
||||
protected function getUser()
|
||||
{
|
||||
return parent::getUser();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ use App\Form\API\TimesheetApiEditForm;
|
||||
use App\Repository\Query\TimesheetQuery;
|
||||
use App\Repository\TagRepository;
|
||||
use App\Repository\TimesheetRepository;
|
||||
use App\Timesheet\RoundingService;
|
||||
use App\Timesheet\TrackingMode\TrackingModeInterface;
|
||||
use App\Timesheet\TrackingModeService;
|
||||
use App\Timesheet\UserDateTimeFactory;
|
||||
@@ -75,6 +76,10 @@ class TimesheetController extends BaseApiController
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
private $dispatcher;
|
||||
/**
|
||||
* @var RoundingService
|
||||
*/
|
||||
private $roundingService;
|
||||
|
||||
public function __construct(
|
||||
ViewHandlerInterface $viewHandler,
|
||||
@@ -83,7 +88,8 @@ class TimesheetController extends BaseApiController
|
||||
TimesheetConfiguration $configuration,
|
||||
TagRepository $tagRepository,
|
||||
TrackingModeService $trackingModeService,
|
||||
EventDispatcherInterface $dispatcher
|
||||
EventDispatcherInterface $dispatcher,
|
||||
RoundingService $roundingService
|
||||
) {
|
||||
$this->viewHandler = $viewHandler;
|
||||
$this->repository = $repository;
|
||||
@@ -92,6 +98,7 @@ class TimesheetController extends BaseApiController
|
||||
$this->tagRepository = $tagRepository;
|
||||
$this->trackingModeService = $trackingModeService;
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->roundingService = $roundingService;
|
||||
}
|
||||
|
||||
protected function getTrackingMode(): TrackingModeInterface
|
||||
@@ -293,12 +300,12 @@ class TimesheetController extends BaseApiController
|
||||
{
|
||||
$timesheet = new Timesheet();
|
||||
$timesheet->setUser($this->getUser());
|
||||
$timesheet->setBegin($this->dateTime->createDateTime());
|
||||
|
||||
$event = new TimesheetMetaDefinitionEvent($timesheet);
|
||||
$this->dispatcher->dispatch($event);
|
||||
|
||||
$mode = $this->getTrackingMode();
|
||||
$mode->create($timesheet, $request);
|
||||
|
||||
$form = $this->createForm(TimesheetApiEditForm::class, $timesheet, [
|
||||
'include_rate' => $this->isGranted('edit_rate', $timesheet),
|
||||
@@ -608,6 +615,7 @@ class TimesheetController extends BaseApiController
|
||||
->setActivity($timesheet->getActivity())
|
||||
->setProject($timesheet->getProject())
|
||||
;
|
||||
$this->roundingService->roundBegin($copyTimesheet);
|
||||
|
||||
if (null !== ($copy = $paramFetcher->get('copy'))) {
|
||||
if (in_array($copy, ['rates', 'all'])) {
|
||||
|
||||
@@ -47,4 +47,29 @@ class TimesheetConfiguration implements SystemBundleConfiguration
|
||||
{
|
||||
return (int) $this->find('active_entries.soft_limit');
|
||||
}
|
||||
|
||||
public function getDefaultRoundingDays(): string
|
||||
{
|
||||
return (string) $this->find('rounding.default.days');
|
||||
}
|
||||
|
||||
public function getDefaultRoundingMode(): string
|
||||
{
|
||||
return (string) $this->find('rounding.default.mode');
|
||||
}
|
||||
|
||||
public function getDefaultRoundingBegin(): int
|
||||
{
|
||||
return (int) $this->find('rounding.default.begin');
|
||||
}
|
||||
|
||||
public function getDefaultRoundingEnd(): int
|
||||
{
|
||||
return (int) $this->find('rounding.default.end');
|
||||
}
|
||||
|
||||
public function getDefaultRoundingDuration(): int
|
||||
{
|
||||
return (int) $this->find('rounding.default.duration');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\User;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as BaseAbstractController;
|
||||
use Symfony\Component\Translation\DataCollectorTranslator;
|
||||
use Symfony\Contracts\Service\ServiceSubscriberInterface;
|
||||
@@ -36,6 +37,14 @@ abstract class AbstractController extends BaseAbstractController implements Serv
|
||||
return $this->container->get('translator');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return User|null
|
||||
*/
|
||||
protected function getUser()
|
||||
{
|
||||
return parent::getUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a "successful" flash message to the stack.
|
||||
*
|
||||
|
||||
@@ -16,8 +16,10 @@ use App\Form\Model\SystemConfiguration as SystemConfigurationModel;
|
||||
use App\Form\SystemConfigurationForm;
|
||||
use App\Form\Type\EnhancedSelectboxType;
|
||||
use App\Form\Type\LanguageType;
|
||||
use App\Form\Type\RoundingModeType;
|
||||
use App\Form\Type\SkinType;
|
||||
use App\Form\Type\TrackingModeType;
|
||||
use App\Form\Type\WeekDaysType;
|
||||
use App\Repository\ConfigurationRepository;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
@@ -215,6 +217,39 @@ class SystemConfigurationController extends AbstractController
|
||||
new GreaterThanOrEqual(['value' => 1])
|
||||
]),
|
||||
]),
|
||||
(new SystemConfigurationModel())
|
||||
->setSection(SystemConfigurationModel::SECTION_ROUNDING)
|
||||
->setConfiguration([
|
||||
(new Configuration())
|
||||
->setName('timesheet.rounding.default.mode')
|
||||
->setType(RoundingModeType::class)
|
||||
->setTranslationDomain('system-configuration'),
|
||||
(new Configuration())
|
||||
->setName('timesheet.rounding.default.begin')
|
||||
->setType(IntegerType::class)
|
||||
->setTranslationDomain('system-configuration')
|
||||
->setConstraints([
|
||||
new GreaterThanOrEqual(['value' => 0])
|
||||
]),
|
||||
(new Configuration())
|
||||
->setName('timesheet.rounding.default.end')
|
||||
->setType(IntegerType::class)
|
||||
->setTranslationDomain('system-configuration')
|
||||
->setConstraints([
|
||||
new GreaterThanOrEqual(['value' => 0])
|
||||
]),
|
||||
(new Configuration())
|
||||
->setName('timesheet.rounding.default.duration')
|
||||
->setType(IntegerType::class)
|
||||
->setTranslationDomain('system-configuration')
|
||||
->setConstraints([
|
||||
new GreaterThanOrEqual(['value' => 0])
|
||||
]),
|
||||
(new Configuration())
|
||||
->setName('timesheet.rounding.default.days')
|
||||
->setType(WeekDaysType::class)
|
||||
->setTranslationDomain('system-configuration'),
|
||||
]),
|
||||
(new SystemConfigurationModel())
|
||||
->setSection(SystemConfigurationModel::SECTION_FORM_CUSTOMER)
|
||||
->setConfiguration([
|
||||
|
||||
@@ -178,7 +178,6 @@ abstract class TimesheetAbstractController extends AbstractController
|
||||
{
|
||||
$entry = new Timesheet();
|
||||
$entry->setUser($this->getUser());
|
||||
$entry->setBegin($this->dateTime->createDateTime());
|
||||
|
||||
if ($request->query->get('project')) {
|
||||
$project = $projectRepository->find($request->query->get('project'));
|
||||
|
||||
@@ -36,10 +36,15 @@ class AppExtension extends Extension
|
||||
if (isset($config['timesheet']['duration_only'])) {
|
||||
@trigger_error('Configuration "kimai.timesheet.duration_only" is deprecated, please remove it', E_USER_DEPRECATED);
|
||||
if (true === $config['timesheet']['duration_only'] && 'duration_only' !== $config['timesheet']['mode']) {
|
||||
trigger_error('Found ambiguous configuration. Please remove "kimai.timesheet.duration_only" and set "kimai.timesheet.mode" instead.');
|
||||
trigger_error('Found ambiguous configuration: remove "kimai.timesheet.duration_only" and set "kimai.timesheet.mode" instead.');
|
||||
}
|
||||
}
|
||||
|
||||
// we use a comma sepearated string internally, to be able to use it in combination with the database configuration system
|
||||
foreach ($config['timesheet']['rounding'] as $name => $settings) {
|
||||
$config['timesheet']['rounding'][$name]['days'] = implode(',', $settings['days']);
|
||||
}
|
||||
|
||||
// safe alternatives to %kernel.project_dir%
|
||||
$container->setParameter('kimai.data_dir', $config['data_dir']);
|
||||
$container->setParameter('kimai.plugin_dir', $config['plugin_dir']);
|
||||
|
||||
@@ -98,17 +98,15 @@ class Configuration implements ConfigurationInterface
|
||||
->arrayPrototype()
|
||||
->children()
|
||||
->arrayNode('days')
|
||||
->requiresAtLeastOneElement()
|
||||
->useAttributeAsKey('key')
|
||||
->isRequired()
|
||||
->scalarPrototype()->end()
|
||||
->defaultValue([])
|
||||
->defaultValue(['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'])
|
||||
->end()
|
||||
->integerNode('begin')
|
||||
->defaultValue(0)
|
||||
->defaultValue(1)
|
||||
->end()
|
||||
->integerNode('end')
|
||||
->defaultValue(0)
|
||||
->defaultValue(1)
|
||||
->end()
|
||||
->integerNode('duration')
|
||||
->defaultValue(0)
|
||||
@@ -131,7 +129,15 @@ class Configuration implements ConfigurationInterface
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->defaultValue([])
|
||||
->defaultValue([
|
||||
'default' => [
|
||||
'days' => ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'],
|
||||
'begin' => 1,
|
||||
'end' => 1,
|
||||
'duration' => 0,
|
||||
'mode' => 'default'
|
||||
]
|
||||
])
|
||||
->end()
|
||||
->arrayNode('rates')
|
||||
->requiresAtLeastOneElement()
|
||||
|
||||
@@ -26,18 +26,10 @@ class TimesheetSubscriber implements EventSubscriber
|
||||
protected $calculator;
|
||||
|
||||
/**
|
||||
* @param iterable $calculators
|
||||
* @param CalculatorInterface[] $calculators
|
||||
*/
|
||||
public function __construct(iterable $calculators)
|
||||
{
|
||||
foreach ($calculators as $calculator) {
|
||||
if (!($calculator instanceof CalculatorInterface)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Invalid TimesheetCalculator implementation given. Expected CalculatorInterface but received ' .
|
||||
get_class($calculator)
|
||||
);
|
||||
}
|
||||
}
|
||||
$this->calculator = $calculators;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace App\Form\Model;
|
||||
|
||||
class SystemConfiguration
|
||||
{
|
||||
public const SECTION_ROUNDING = 'rounding';
|
||||
public const SECTION_TIMESHEET = 'timesheet';
|
||||
public const SECTION_FORM_CUSTOMER = 'form_customer';
|
||||
public const SECTION_FORM_USER = 'form_user';
|
||||
|
||||
56
src/Form/Type/RoundingModeType.php
Normal file
56
src/Form/Type/RoundingModeType.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?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\Form\Type;
|
||||
|
||||
use App\Timesheet\RoundingService;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
* Custom form field type to select the timesheet mode.
|
||||
*/
|
||||
class RoundingModeType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* @var RoundingService
|
||||
*/
|
||||
private $service;
|
||||
|
||||
public function __construct(RoundingService $service)
|
||||
{
|
||||
$this->service = $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$choices = [];
|
||||
|
||||
foreach ($this->service->getRoundingModes() as $mode) {
|
||||
$id = $mode->getId();
|
||||
$choices[ucfirst($id)] = $id;
|
||||
}
|
||||
|
||||
$resolver->setDefaults([
|
||||
'choices' => $choices,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
return ChoiceType::class;
|
||||
}
|
||||
}
|
||||
63
src/Form/Type/WeekDaysType.php
Normal file
63
src/Form/Type/WeekDaysType.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?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\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\CallbackTransformer;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
* Custom form field type to select weekdays.
|
||||
*/
|
||||
class WeekDaysType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->addModelTransformer(new CallbackTransformer(
|
||||
function ($weekdays) {
|
||||
return explode(',', $weekdays);
|
||||
},
|
||||
function ($weekdays) {
|
||||
return implode(',', $weekdays);
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$choices = [
|
||||
'Monday' => 'monday',
|
||||
'Tuesday' => 'tuesday',
|
||||
'Wednesday' => 'wednesday',
|
||||
'Thursday' => 'thursday',
|
||||
'Friday' => 'friday',
|
||||
'Saturday' => 'saturday',
|
||||
'Sunday' => 'sunday'
|
||||
];
|
||||
|
||||
$resolver->setDefaults([
|
||||
'multiple' => true,
|
||||
'choices' => $choices,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
return ChoiceType::class;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,8 @@ use App\Invoice\RendererInterface as InvoiceRendererInterface;
|
||||
use App\Ldap\FormLoginLdapFactory;
|
||||
use App\Plugin\PluginInterface;
|
||||
use App\Timesheet\CalculatorInterface as TimesheetCalculator;
|
||||
use App\Timesheet\Rounding\RoundingInterface;
|
||||
use App\Timesheet\TrackingMode\TrackingModeInterface;
|
||||
use App\Widget\WidgetInterface;
|
||||
use App\Widget\WidgetRendererInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||
@@ -52,6 +54,8 @@ class Kernel extends BaseKernel
|
||||
public const TAG_INVOICE_REPOSITORY = 'invoice.repository';
|
||||
public const TAG_TIMESHEET_CALCULATOR = 'timesheet.calculator';
|
||||
public const TAG_TIMESHEET_EXPORTER = 'timesheet.exporter';
|
||||
public const TAG_TIMESHEET_TRACKING_MODE = 'timesheet.tracking_mode';
|
||||
public const TAG_TIMESHEET_ROUNDING_MODE = 'timesheet.rounding_mode';
|
||||
|
||||
public function getCacheDir()
|
||||
{
|
||||
@@ -75,6 +79,8 @@ class Kernel extends BaseKernel
|
||||
$container->registerForAutoconfiguration(WidgetRendererInterface::class)->addTag(self::TAG_WIDGET_RENDERER);
|
||||
$container->registerForAutoconfiguration(WidgetInterface::class)->addTag(self::TAG_WIDGET);
|
||||
$container->registerForAutoconfiguration(TimesheetExportInterface::class)->addTag(self::TAG_TIMESHEET_EXPORTER);
|
||||
$container->registerForAutoconfiguration(TrackingModeInterface::class)->addTag(self::TAG_TIMESHEET_TRACKING_MODE);
|
||||
$container->registerForAutoconfiguration(RoundingInterface::class)->addTag(self::TAG_TIMESHEET_ROUNDING_MODE);
|
||||
|
||||
/** @var SecurityExtension $extension */
|
||||
$extension = $container->getExtension('security');
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -33,4 +33,9 @@ interface RoundingInterface
|
||||
* @param int $minutes
|
||||
*/
|
||||
public function roundDuration(Timesheet $record, $minutes);
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getId(): string;
|
||||
}
|
||||
|
||||
147
src/Timesheet/RoundingService.php
Normal file
147
src/Timesheet/RoundingService.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user