new system-config to prevent overlapping records (#1720)

This commit is contained in:
Kevin Papst
2020-06-28 22:09:50 +02:00
committed by GitHub
parent e566305426
commit e22830790b
17 changed files with 246 additions and 20 deletions

View File

@@ -23,6 +23,11 @@ class TimesheetConfiguration implements SystemBundleConfiguration
return (bool) $this->find('rules.allow_future_times');
}
public function isAllowOverlappingRecords(): bool
{
return (bool) $this->find('rules.allow_overlapping_records');
}
public function getTrackingMode(): string
{
return (string) $this->find('mode');

View File

@@ -228,6 +228,10 @@ final class SystemConfigurationController extends AbstractController
->setName('timesheet.rules.allow_future_times')
->setType(CheckboxType::class)
->setTranslationDomain('system-configuration'),
(new Configuration())
->setName('timesheet.rules.allow_overlapping_records')
->setType(CheckboxType::class)
->setTranslationDomain('system-configuration'),
(new Configuration())
->setName('timesheet.active_entries.hard_limit')
->setType(IntegerType::class)

View File

@@ -195,6 +195,9 @@ class Configuration implements ConfigurationInterface
->booleanNode('allow_future_times')
->defaultTrue()
->end()
->booleanNode('allow_overlapping_records')
->defaultTrue()
->end()
->end()
->end()
->end()

View File

@@ -20,9 +20,10 @@ class ConfigurationRepository extends EntityRepository implements ConfigLoaderIn
private static $cacheByPrefix = null;
private static $cacheAll = [];
private function clearCache()
public function clearCache()
{
static::$cacheByPrefix = null;
static::$cacheAll = null;
}
private function prefillCache()
@@ -44,6 +45,14 @@ class ConfigurationRepository extends EntityRepository implements ConfigLoaderIn
}
}
public function saveConfiguration(Configuration $configuration)
{
$entityManager = $this->getEntityManager();
$entityManager->persist($configuration);
$entityManager->flush();
$this->clearCache();
}
/**
* @param string $prefix
* @return Configuration[]

View File

@@ -939,4 +939,33 @@ class TimesheetRepository extends EntityRepository
return $results;
}
public function hasRecordForTime(Timesheet $timesheet): bool
{
$qb = $this->getEntityManager()->createQueryBuilder();
$or = $qb->expr()->orX(
$qb->expr()->between(':begin', 't.begin', 't.end')
);
if (null !== $timesheet->getEnd()) {
$or->add($qb->expr()->between(':end', 't.begin', 't.end'));
$or->add($qb->expr()->between('t.begin', ':begin', ':end'));
$or->add($qb->expr()->between('t.end', ':begin', ':end'));
$qb->setParameter('end', $timesheet->getEnd());
}
$qb->select('t')
->from(Timesheet::class, 't')
->andWhere($qb->expr()->eq('t.user', ':user'))
->andWhere($qb->expr()->isNotNull('t.end'))
->andWhere($or)
->setParameter('begin', $timesheet->getBegin())
->setParameter('user', $timesheet->getUser())
;
$result = $qb->getQuery()->getResult();
return !empty($result);
}
}

View File

@@ -30,6 +30,7 @@ class Timesheet extends Constraint
public const START_DISALLOWED = 'kimai-timesheet-90';
public const PROJECT_NOT_STARTED = 'kimai-timesheet-91';
public const PROJECT_ALREADY_ENDED = 'kimai-timesheet-92';
public const RECORD_OVERLAPPING = 'kimai-timesheet-93';
protected static $errorNames = [
self::MISSING_BEGIN_ERROR => 'You must submit a begin date.',
@@ -44,6 +45,7 @@ class Timesheet extends Constraint
self::START_DISALLOWED => 'You are not allowed to start this timesheet record.',
self::PROJECT_NOT_STARTED => 'The project has not started at that time.',
self::PROJECT_ALREADY_ENDED => 'The project is finished at that time.',
self::RECORD_OVERLAPPING => 'You already have an entry for this time.',
];
public $message = 'This timesheet has invalid settings.';

View File

@@ -11,6 +11,7 @@ namespace App\Validator\Constraints;
use App\Configuration\TimesheetConfiguration;
use App\Entity\Timesheet as TimesheetEntity;
use App\Repository\TimesheetRepository;
use App\Timesheet\TrackingModeService;
use App\Validator\Constraints\Timesheet as TimesheetConstraint;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
@@ -33,16 +34,17 @@ class TimesheetValidator extends ConstraintValidator
* @var TrackingModeService
*/
protected $trackingModeService;
/**
* @param AuthorizationCheckerInterface $auth
* @param TimesheetConfiguration $configuration
* @var TimesheetRepository
*/
public function __construct(AuthorizationCheckerInterface $auth, TimesheetConfiguration $configuration, TrackingModeService $service)
private $repository;
public function __construct(AuthorizationCheckerInterface $auth, TimesheetConfiguration $configuration, TrackingModeService $service, TimesheetRepository $repository)
{
$this->auth = $auth;
$this->configuration = $configuration;
$this->trackingModeService = $service;
$this->repository = $repository;
}
/**
@@ -62,6 +64,38 @@ class TimesheetValidator extends ConstraintValidator
$this->validateBeginAndEnd($value, $this->context);
$this->validateActivityAndProject($value, $this->context);
$this->validatePermissions($value, $this->context);
$this->validateActiveLimit($value, $this->context);
$this->validateOverlapping($value, $this->context);
}
/**
* @param TimesheetEntity $timesheet
* @param ExecutionContextInterface $context
*/
protected function validateOverlapping(TimesheetEntity $timesheet, ExecutionContextInterface $context)
{
if ($this->configuration->isAllowOverlappingRecords()) {
return;
}
if (!$this->repository->hasRecordForTime($timesheet)) {
return;
}
$context->buildViolation('You already have an entry for this time.')
->atPath('begin')
->setTranslationDomain('validators')
->setCode(TimesheetConstraint::RECORD_OVERLAPPING)
->addViolation();
}
/**
* @param TimesheetEntity $timesheet
* @param ExecutionContextInterface $context
*/
protected function validateActiveLimit(TimesheetEntity $timesheet, ExecutionContextInterface $context)
{
// TODO check active entries against hard_limit
}
/**
@@ -91,8 +125,6 @@ class TimesheetValidator extends ConstraintValidator
return;
}
}
// TODO check active entries against hard_limit
}
/**