diff --git a/src/API/ConfigurationController.php b/src/API/ConfigurationController.php index a02438ecb..5e1c1eca6 100644 --- a/src/API/ConfigurationController.php +++ b/src/API/ConfigurationController.php @@ -41,7 +41,7 @@ final class ConfigurationController extends BaseApiController $model->setTrackingMode($configuration->getTimesheetTrackingMode()); $model->setDefaultBeginTime($configuration->getTimesheetDefaultBeginTime()); $model->setActiveEntriesHardLimit($configuration->getTimesheetActiveEntriesHardLimit()); - $model->setIsAllowFutureTimes($configuration->isTimesheetAllowFutureTimes()); + $model->setIsAllowFutureTimes($configuration->getTimesheetAllowFutureRule()); $model->setIsAllowOverlapping($configuration->isTimesheetAllowOverlappingRecords()); $view = new View($model, 200); diff --git a/src/API/Model/TimesheetConfig.php b/src/API/Model/TimesheetConfig.php index b04a23091..397c25c37 100644 --- a/src/API/Model/TimesheetConfig.php +++ b/src/API/Model/TimesheetConfig.php @@ -9,6 +9,7 @@ namespace App\API\Model; +use App\Timesheet\FutureTimesEnum; use JMS\Serializer\Annotation as Serializer; #[Serializer\ExclusionPolicy('none')] @@ -36,7 +37,7 @@ final class TimesheetConfig #[Serializer\Type(name: 'integer')] public int $activeEntriesHardLimit = 1; /** - * Whether entries for future times are allowed + * Whether entries for future times are allowed (end time maybe limited) */ #[Serializer\Expose] #[Serializer\Groups(['Default'])] @@ -65,9 +66,9 @@ final class TimesheetConfig $this->activeEntriesHardLimit = $activeEntriesHardLimit; } - public function setIsAllowFutureTimes(bool $isAllowFutureTimes): void + public function setIsAllowFutureTimes(FutureTimesEnum $futureTimesEnum): void { - $this->isAllowFutureTimes = $isAllowFutureTimes; + $this->isAllowFutureTimes = ($futureTimesEnum !== FutureTimesEnum::DENY); } public function setIsAllowOverlapping(bool $isAllowOverlapping): void diff --git a/src/Configuration/SystemConfiguration.php b/src/Configuration/SystemConfiguration.php index 9bb7d79f9..fe0dd6b18 100644 --- a/src/Configuration/SystemConfiguration.php +++ b/src/Configuration/SystemConfiguration.php @@ -9,6 +9,8 @@ namespace App\Configuration; +use App\Timesheet\FutureTimesEnum; + final class SystemConfiguration { private bool $initialized = false; @@ -377,9 +379,35 @@ final class SystemConfiguration return (string) $this->find('timesheet.default_begin'); } + /** + * @deprecated since 2.4 + */ public function isTimesheetAllowFutureTimes(): bool { - return (bool) $this->find('timesheet.rules.allow_future_times'); + @trigger_error('isTimesheetAllowFutureTimes() is deprecated and will be removed with 2.2.0, use getTimesheetAllowFutureRule() instead', E_USER_DEPRECATED); + + $rule = $this->getTimesheetAllowFutureRule(); + + if ($rule === FutureTimesEnum::DENY) { + return false; + } + + return true; + } + + public function getTimesheetAllowFutureRule(): FutureTimesEnum + { + $value = $this->getString('timesheet.rules.allow_future_times', 'allow'); + + if (($rule = FutureTimesEnum::tryFrom($value)) === null) { + if ((bool) $value === false) { + return FutureTimesEnum::DENY; + } + + return FutureTimesEnum::ALLOW; + } + + return $rule; } public function isTimesheetAllowZeroDuration(): bool diff --git a/src/Timesheet/FutureTimesEnum.php b/src/Timesheet/FutureTimesEnum.php new file mode 100644 index 000000000..496f517bb --- /dev/null +++ b/src/Timesheet/FutureTimesEnum.php @@ -0,0 +1,18 @@ +configuration->isTimesheetAllowFutureTimes()) { + $rule = $this->configuration->getTimesheetAllowFutureRule(); + + if ($rule === FutureTimesEnum::ALLOW) { return; } + // TODO apply configured future times validation rule + $now = new \DateTime('now', $value->getBegin()->getTimezone()); // allow configured default rounding time + 1 minute - see #1295 diff --git a/tests/API/Model/TimesheetConfigTest.php b/tests/API/Model/TimesheetConfigTest.php index 0f16218d2..2c5576920 100644 --- a/tests/API/Model/TimesheetConfigTest.php +++ b/tests/API/Model/TimesheetConfigTest.php @@ -10,6 +10,7 @@ namespace App\Tests\API\Model; use App\API\Model\TimesheetConfig; +use App\Timesheet\FutureTimesEnum; use PHPUnit\Framework\TestCase; /** @@ -20,7 +21,7 @@ class TimesheetConfigTest extends TestCase public function testSetter(): void { $sut = new TimesheetConfig(); - $sut->setIsAllowFutureTimes(false); + $sut->setIsAllowFutureTimes(FutureTimesEnum::DENY); $sut->setIsAllowOverlapping(false); $sut->setDefaultBeginTime('08:00'); $sut->setTrackingMode('punch');