added punch-in punch-out / time-clock mode (#812)
This commit is contained in:
@@ -275,6 +275,7 @@ class TimesheetController extends BaseApiController
|
||||
'csrf_protection' => false,
|
||||
'include_rate' => $this->isGranted('edit_rate', $timesheet),
|
||||
'include_exported' => $this->isGranted('edit_export', $timesheet),
|
||||
'include_datetime' => !$this->configuration->isPunchInOut(),
|
||||
'date_format' => self::DATE_FORMAT,
|
||||
]);
|
||||
|
||||
@@ -352,6 +353,7 @@ class TimesheetController extends BaseApiController
|
||||
'csrf_protection' => false,
|
||||
'include_rate' => $this->isGranted('edit_rate', $timesheet),
|
||||
'include_exported' => $this->isGranted('edit_export', $timesheet),
|
||||
'include_datetime' => !$this->configuration->isPunchInOut(),
|
||||
'date_format' => self::DATE_FORMAT,
|
||||
]);
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ class TimesheetConfiguration implements SystemBundleConfiguration
|
||||
{
|
||||
public const MODE_DURATION_ONLY = 'duration_only';
|
||||
public const MODE_DEFAULT = 'default';
|
||||
public const MODE_PUNCH_IN_OUT = 'punch';
|
||||
|
||||
use StringAccessibleConfigTrait;
|
||||
|
||||
@@ -31,6 +32,11 @@ class TimesheetConfiguration implements SystemBundleConfiguration
|
||||
return $this->find('mode') === self::MODE_DURATION_ONLY;
|
||||
}
|
||||
|
||||
public function isPunchInOut(): bool
|
||||
{
|
||||
return $this->find('mode') === self::MODE_PUNCH_IN_OUT;
|
||||
}
|
||||
|
||||
public function isMarkdownEnabled(): bool
|
||||
{
|
||||
return (bool) $this->find('markdown_content');
|
||||
|
||||
@@ -146,44 +146,7 @@ abstract class TimesheetAbstractController extends AbstractController
|
||||
$entry->setUser($this->getUser());
|
||||
$entry->setBegin($this->dateTime->createDateTime());
|
||||
|
||||
$start = $request->get('begin');
|
||||
if ($start !== null) {
|
||||
$start = $this->dateTime->createDateTimeFromFormat('Y-m-d', $start);
|
||||
if ($start !== false) {
|
||||
$entry->setBegin($start);
|
||||
|
||||
// only check for an end date if a begin date was given
|
||||
$end = $request->get('end');
|
||||
if ($end !== null) {
|
||||
$end = $this->dateTime->createDateTimeFromFormat('Y-m-d', $end);
|
||||
if ($end !== false) {
|
||||
$start->setTime(10, 0, 0);
|
||||
$end->setTime(18, 0, 0);
|
||||
|
||||
$entry->setEnd($end);
|
||||
$entry->setDuration($end->getTimestamp() - $start->getTimestamp());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$from = $request->get('from');
|
||||
if ($from !== null) {
|
||||
$from = $this->dateTime->createDateTime($from);
|
||||
if ($from !== false) {
|
||||
$entry->setBegin($from);
|
||||
|
||||
// only check for an end datetime if a begin datetime was given
|
||||
$to = $request->get('to');
|
||||
if ($to !== null) {
|
||||
$to = $this->dateTime->createDateTime($to);
|
||||
if ($to !== false) {
|
||||
$entry->setEnd($to);
|
||||
$entry->setDuration($to->getTimestamp() - $from->getTimestamp());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->setBeginEndFromRequest($request, $entry);
|
||||
|
||||
if ($request->query->get('project')) {
|
||||
$project = $projectRepository->find($request->query->get('project'));
|
||||
@@ -225,6 +188,52 @@ abstract class TimesheetAbstractController extends AbstractController
|
||||
]);
|
||||
}
|
||||
|
||||
protected function setBeginEndFromRequest(Request $request, Timesheet $entry)
|
||||
{
|
||||
if ($this->configuration->isPunchInOut()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$start = $request->get('begin');
|
||||
if ($start !== null) {
|
||||
$start = $this->dateTime->createDateTimeFromFormat('Y-m-d', $start);
|
||||
if ($start !== false) {
|
||||
$entry->setBegin($start);
|
||||
|
||||
// only check for an end date if a begin date was given
|
||||
$end = $request->get('end');
|
||||
if ($end !== null) {
|
||||
$end = $this->dateTime->createDateTimeFromFormat('Y-m-d', $end);
|
||||
if ($end !== false) {
|
||||
$start->setTime(10, 0, 0);
|
||||
$end->setTime(18, 0, 0);
|
||||
|
||||
$entry->setEnd($end);
|
||||
$entry->setDuration($end->getTimestamp() - $start->getTimestamp());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$from = $request->get('from');
|
||||
if ($from !== null) {
|
||||
$from = $this->dateTime->createDateTime($from);
|
||||
if ($from !== false) {
|
||||
$entry->setBegin($from);
|
||||
|
||||
// only check for an end datetime if a begin datetime was given
|
||||
$to = $request->get('to');
|
||||
if ($to !== null) {
|
||||
$to = $this->dateTime->createDateTime($to);
|
||||
if ($to !== false) {
|
||||
$entry->setEnd($to);
|
||||
$entry->setDuration($to->getTimestamp() - $from->getTimestamp());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param string $renderTemplate
|
||||
@@ -274,10 +283,13 @@ abstract class TimesheetAbstractController extends AbstractController
|
||||
*/
|
||||
protected function getCreateForm(Timesheet $entry)
|
||||
{
|
||||
return $this->createForm(TimesheetEditForm::class, $entry, [
|
||||
return $this->createForm($this->getCreateFormClassName(), $entry, [
|
||||
'action' => $this->generateUrl($this->getCreateRoute()),
|
||||
'include_rate' => $this->isGranted('edit_rate', $entry),
|
||||
'include_exported' => $this->isGranted('edit_export', $entry),
|
||||
'include_user' => $this->includeUserInForms(),
|
||||
'use_duration' => $this->configuration->isDurationOnly(),
|
||||
'include_datetime' => !$this->configuration->isPunchInOut(),
|
||||
'customer' => true,
|
||||
]);
|
||||
}
|
||||
@@ -289,7 +301,7 @@ abstract class TimesheetAbstractController extends AbstractController
|
||||
*/
|
||||
protected function getEditForm(Timesheet $entry, $page)
|
||||
{
|
||||
return $this->createForm(TimesheetEditForm::class, $entry, [
|
||||
return $this->createForm($this->getEditFormClassName(), $entry, [
|
||||
'action' => $this->generateUrl($this->getEditRoute(), [
|
||||
'id' => $entry->getId(),
|
||||
'page' => $page,
|
||||
@@ -297,6 +309,8 @@ abstract class TimesheetAbstractController extends AbstractController
|
||||
'include_rate' => $this->isGranted('edit_rate', $entry),
|
||||
'include_exported' => $this->isGranted('edit_export', $entry),
|
||||
'include_user' => $this->includeUserInForms(),
|
||||
'include_datetime' => !$this->configuration->isPunchInOut(),
|
||||
'use_duration' => $this->configuration->isDurationOnly(),
|
||||
'customer' => true,
|
||||
]);
|
||||
}
|
||||
@@ -316,6 +330,16 @@ abstract class TimesheetAbstractController extends AbstractController
|
||||
]);
|
||||
}
|
||||
|
||||
protected function getCreateFormClassName()
|
||||
{
|
||||
return TimesheetEditForm::class;
|
||||
}
|
||||
|
||||
protected function getEditFormClassName()
|
||||
{
|
||||
return TimesheetEditForm::class;
|
||||
}
|
||||
|
||||
protected function includeSummary(): bool
|
||||
{
|
||||
return (bool) $this->getUser()->getPreferenceValue('timesheet.daily_stats', false);
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
use App\Form\TimesheetAdminEditForm;
|
||||
use App\Repository\ActivityRepository;
|
||||
use App\Repository\ProjectRepository;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
|
||||
@@ -74,6 +75,16 @@ class TimesheetTeamController extends TimesheetAbstractController
|
||||
return $this->create($request, 'timesheet-team/edit.html.twig', $projectRepository, $activityRepository);
|
||||
}
|
||||
|
||||
protected function getCreateFormClassName()
|
||||
{
|
||||
return TimesheetAdminEditForm::class;
|
||||
}
|
||||
|
||||
protected function getEditFormClassName()
|
||||
{
|
||||
return TimesheetAdminEditForm::class;
|
||||
}
|
||||
|
||||
protected function includeUserInForms(): bool
|
||||
{
|
||||
return true;
|
||||
|
||||
@@ -80,9 +80,9 @@ class Configuration implements ConfigurationInterface
|
||||
->defaultValue('default')
|
||||
->validate()
|
||||
->ifTrue(function ($value) {
|
||||
return !in_array($value, ['default', 'duration_only']);
|
||||
return !in_array($value, ['default', 'duration_only', 'punch']);
|
||||
})
|
||||
->thenInvalid('Chosen timesheet mode is invalid, allowed values: default, duration_only')
|
||||
->thenInvalid('Chosen timesheet mode is invalid, allowed values: default, duration_only, punch')
|
||||
->end()
|
||||
->end()
|
||||
->booleanNode('markdown_content')
|
||||
|
||||
23
src/Form/TimesheetAdminEditForm.php
Normal file
23
src/Form/TimesheetAdminEditForm.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?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;
|
||||
|
||||
class TimesheetAdminEditForm extends TimesheetEditForm
|
||||
{
|
||||
protected function showTimeFields(array $options): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function showCustomer(array $options, bool $isNew, int $customerCount): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Configuration\TimesheetConfiguration;
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\Project;
|
||||
@@ -52,23 +51,17 @@ class TimesheetEditForm extends AbstractType
|
||||
* @var UserDateTimeFactory
|
||||
*/
|
||||
protected $dateTime;
|
||||
/**
|
||||
* @var TimesheetConfiguration
|
||||
*/
|
||||
private $configuration;
|
||||
|
||||
/**
|
||||
* @param CustomerRepository $customer
|
||||
* @param ProjectRepository $project
|
||||
* @param UserDateTimeFactory $dateTime
|
||||
* @param TimesheetConfiguration $config
|
||||
*/
|
||||
public function __construct(CustomerRepository $customer, ProjectRepository $project, UserDateTimeFactory $dateTime, TimesheetConfiguration $config)
|
||||
public function __construct(CustomerRepository $customer, ProjectRepository $project, UserDateTimeFactory $dateTime)
|
||||
{
|
||||
$this->customers = $customer;
|
||||
$this->projects = $project;
|
||||
$this->dateTime = $dateTime;
|
||||
$this->configuration = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,10 +73,10 @@ class TimesheetEditForm extends AbstractType
|
||||
$project = null;
|
||||
$customer = null;
|
||||
$currency = false;
|
||||
$end = null;
|
||||
$begin = null;
|
||||
$customerCount = $this->customers->countCustomer(true);
|
||||
$projectCount = $this->projects->countProject(true);
|
||||
$timezone = $this->dateTime->getTimezone()->getName();
|
||||
$isNew = true;
|
||||
|
||||
if (isset($options['data'])) {
|
||||
@@ -106,14 +99,9 @@ class TimesheetEditForm extends AbstractType
|
||||
$currency = $customer->getCurrency();
|
||||
}
|
||||
|
||||
$begin = $entry->getBegin();
|
||||
$end = $entry->getEnd();
|
||||
}
|
||||
|
||||
$timezone = $this->dateTime->getTimezone()->getName();
|
||||
|
||||
if (null !== $begin) {
|
||||
$timezone = $begin->getTimezone()->getName();
|
||||
if (null !== ($begin = $entry->getBegin())) {
|
||||
$timezone = $begin->getTimezone()->getName();
|
||||
}
|
||||
}
|
||||
|
||||
$dateTimeOptions = [
|
||||
@@ -126,44 +114,49 @@ class TimesheetEditForm extends AbstractType
|
||||
$dateTimeOptions['format'] = $options['date_format'];
|
||||
}
|
||||
|
||||
if ($isNew || null === $end || !$this->configuration->isDurationOnly()) {
|
||||
if ($this->showTimeFields($options)) {
|
||||
$this->addBegin($builder, $dateTimeOptions);
|
||||
|
||||
if ($options['use_duration']) {
|
||||
$this->addDuration($builder);
|
||||
} else {
|
||||
$this->addEnd($builder, $dateTimeOptions);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->configuration->isDurationOnly()) {
|
||||
$this->addDuration($builder);
|
||||
} else {
|
||||
$this->addEnd($builder, $dateTimeOptions);
|
||||
}
|
||||
|
||||
$projectOptions = [];
|
||||
|
||||
if ($customerCount < 2) {
|
||||
$projectOptions['group_by'] = null;
|
||||
} elseif ($options['customer']) {
|
||||
if ($this->showCustomer($options, $isNew, $customerCount)) {
|
||||
$this->addCustomer($builder, $customer);
|
||||
}
|
||||
|
||||
if ($projectCount <= 1) {
|
||||
$projectOptions['group_by'] = null;
|
||||
}
|
||||
|
||||
$this->addProject($builder, $projectOptions, $project, $customer);
|
||||
$this->addProject($builder, $customerCount, $projectCount, $project, $customer);
|
||||
$this->addActivity($builder, $activity, $project);
|
||||
$this->addDescription($builder);
|
||||
$this->addTags($builder);
|
||||
$this->addRates($builder, $currency, $options);
|
||||
$this->addUser($builder, $options);
|
||||
$this->addExported($builder, $options);
|
||||
}
|
||||
|
||||
if ($options['include_rate']) {
|
||||
$this->addRates($builder, $currency);
|
||||
protected function showCustomer(array $options, bool $isNew, int $customerCount): bool
|
||||
{
|
||||
if (!$isNew && $options['customer']) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($options['include_user']) {
|
||||
$this->addUser($builder);
|
||||
if ($customerCount < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($options['include_exported']) {
|
||||
$this->addExported($builder);
|
||||
if (!$options['customer']) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function showTimeFields(array $options): bool
|
||||
{
|
||||
return $options['include_datetime'];
|
||||
}
|
||||
|
||||
protected function addCustomer(FormBuilderInterface $builder, ?Customer $customer = null)
|
||||
@@ -181,8 +174,18 @@ class TimesheetEditForm extends AbstractType
|
||||
]);
|
||||
}
|
||||
|
||||
protected function addProject(FormBuilderInterface $builder, array $projectOptions, ?Project $project = null, ?Customer $customer = null)
|
||||
protected function addProject(FormBuilderInterface $builder, int $customerCount, int $projectCount, ?Project $project = null, ?Customer $customer = null)
|
||||
{
|
||||
$projectOptions = [];
|
||||
|
||||
if ($customerCount < 2) {
|
||||
$projectOptions['group_by'] = null;
|
||||
}
|
||||
|
||||
if ($projectCount < 2) {
|
||||
$projectOptions['group_by'] = null;
|
||||
}
|
||||
|
||||
$builder
|
||||
->add(
|
||||
'project',
|
||||
@@ -322,8 +325,12 @@ class TimesheetEditForm extends AbstractType
|
||||
]);
|
||||
}
|
||||
|
||||
protected function addRates(FormBuilderInterface $builder, $currency)
|
||||
protected function addRates(FormBuilderInterface $builder, $currency, array $options)
|
||||
{
|
||||
if (!$options['include_rate']) {
|
||||
return;
|
||||
}
|
||||
|
||||
$builder
|
||||
->add('fixedRate', FixedRateType::class, [
|
||||
'currency' => $currency,
|
||||
@@ -333,13 +340,21 @@ class TimesheetEditForm extends AbstractType
|
||||
]);
|
||||
}
|
||||
|
||||
protected function addUser(FormBuilderInterface $builder)
|
||||
protected function addUser(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
if (!$options['include_user']) {
|
||||
return;
|
||||
}
|
||||
|
||||
$builder->add('user', UserType::class);
|
||||
}
|
||||
|
||||
protected function addExported(FormBuilderInterface $builder)
|
||||
protected function addExported(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
if (!$options['include_exported']) {
|
||||
return;
|
||||
}
|
||||
|
||||
$builder->add('exported', YesNoType::class, [
|
||||
'label' => 'label.exported'
|
||||
]);
|
||||
@@ -362,6 +377,8 @@ class TimesheetEditForm extends AbstractType
|
||||
'method' => 'POST',
|
||||
'date_format' => null,
|
||||
'customer' => false, // for API usage
|
||||
'use_duration' => false, // duration instead of end (for duration_only mode)
|
||||
'include_datetime' => true,
|
||||
'attr' => [
|
||||
'data-form-event' => 'kimai.timesheetUpdate',
|
||||
'data-msg-success' => 'action.update.success',
|
||||
|
||||
@@ -28,6 +28,7 @@ class TimesheetModeType extends AbstractType
|
||||
'label' => 'label.timesheet.mode',
|
||||
'choices' => [
|
||||
'label.timesheet.mode_default' => TimesheetConfiguration::MODE_DEFAULT,
|
||||
'label.timesheet.mode_punch' => TimesheetConfiguration::MODE_PUNCH_IN_OUT,
|
||||
'label.timesheet.mode_duration_only' => TimesheetConfiguration::MODE_DURATION_ONLY,
|
||||
],
|
||||
]);
|
||||
|
||||
@@ -32,6 +32,7 @@ class TimesheetConfigExtension extends AbstractExtension
|
||||
{
|
||||
return [
|
||||
new TwigFunction('is_duration_only', [$this, 'isDurationOnly']),
|
||||
new TwigFunction('is_punch_mode', [$this, 'isPunchInOut']),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -39,4 +40,9 @@ class TimesheetConfigExtension extends AbstractExtension
|
||||
{
|
||||
return $this->configuration->isDurationOnly();
|
||||
}
|
||||
|
||||
public function isPunchInOut(): bool
|
||||
{
|
||||
return $this->configuration->isPunchInOut();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user