change isWeekend() handling to use work-hour configuration (#4261)
This commit is contained in:
committed by
GitHub
parent
18d953e79b
commit
452de88e18
@@ -26,7 +26,6 @@ use Twig\TwigTest;
|
|||||||
|
|
||||||
final class LocaleFormatExtensions extends AbstractExtension implements LocaleAwareInterface
|
final class LocaleFormatExtensions extends AbstractExtension implements LocaleAwareInterface
|
||||||
{
|
{
|
||||||
private ?bool $fdowSunday = null;
|
|
||||||
private ?LocaleFormatter $formatter = null;
|
private ?LocaleFormatter $formatter = null;
|
||||||
private ?string $locale = null;
|
private ?string $locale = null;
|
||||||
|
|
||||||
@@ -116,29 +115,21 @@ final class LocaleFormatExtensions extends AbstractExtension implements LocaleAw
|
|||||||
return $this->locale;
|
return $this->locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isWeekend(\DateTimeInterface|string|null $dateTime): bool
|
public function isWeekend(\DateTimeInterface|string|null $dateTime, ?User $user = null): bool
|
||||||
{
|
{
|
||||||
if (!$dateTime instanceof \DateTimeInterface) {
|
if (!$dateTime instanceof \DateTimeInterface) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$day = (int) $dateTime->format('w');
|
$day = (int) $dateTime->format('N');
|
||||||
|
|
||||||
if ($this->fdowSunday === null) {
|
/** @var User|null $tmp */
|
||||||
/** @var User|null $user */
|
$tmp = $user ?? $this->security->getUser();
|
||||||
$user = $this->security->getUser();
|
if ($tmp !== null && $tmp->hasWorkHourConfiguration()) {
|
||||||
if ($user !== null) {
|
return !$tmp->isWorkDay($dateTime);
|
||||||
$this->fdowSunday = $user->isFirstDayOfWeekSunday();
|
|
||||||
} else {
|
|
||||||
$this->fdowSunday = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->fdowSunday) {
|
return ($day === 6 || $day === 7);
|
||||||
return ($day === 5 || $day === 6);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ($day === 0 || $day === 6);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dateShort(\DateTimeInterface|string|null $date): string
|
public function dateShort(\DateTimeInterface|string|null $date): string
|
||||||
|
|||||||
@@ -213,7 +213,7 @@
|
|||||||
{% set dayCount = 0 %}
|
{% set dayCount = 0 %}
|
||||||
{% for day in month.days %}
|
{% for day in month.days %}
|
||||||
{% set class = 'text-end contractDay text-nowrap' %}
|
{% set class = 'text-end contractDay text-nowrap' %}
|
||||||
{% if day.day is weekend %}
|
{% if day.day is weekend(user) %}
|
||||||
{% set class = class ~ ' weekend' %}
|
{% set class = class ~ ' weekend' %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if day.workingTime.expectedTime > 0 and day.workingTime.actualTime == 0 and now > day.day %}
|
{% if day.workingTime.expectedTime > 0 and day.workingTime.actualTime == 0 and now > day.day %}
|
||||||
|
|||||||
@@ -3,18 +3,18 @@
|
|||||||
{% block report_content %}
|
{% block report_content %}
|
||||||
{% embed 'reporting/report_by_user_data.html.twig' %}
|
{% embed 'reporting/report_by_user_data.html.twig' %}
|
||||||
{% block period_name %}
|
{% block period_name %}
|
||||||
<th class="text-center text-nowrap{% if column is weekend %} weekend{% endif %}{% if column is today %} today{% endif %}">
|
<th class="text-center text-nowrap{% if column is weekend(user) %} weekend{% endif %}{% if column is today %} today{% endif %}">
|
||||||
{{ column|date_weekday }}
|
{{ column|date_weekday }}
|
||||||
</th>
|
</th>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block column_classes_project -%}
|
{% block column_classes_project -%}
|
||||||
{% if column.date is weekend %} weekend{% endif %}{% if column.date is today %} today{% endif %}
|
{% if column.date is weekend(user) %} weekend{% endif %}{% if column.date is today %} today{% endif %}
|
||||||
{%- endblock %}
|
{%- endblock %}
|
||||||
{% block column_classes_activity -%}
|
{% block column_classes_activity -%}
|
||||||
{% if column.date is weekend %} weekend{% endif %}{% if column.date is today %} today{% endif %}
|
{% if column.date is weekend(user) %} weekend{% endif %}{% if column.date is today %} today{% endif %}
|
||||||
{%- endblock %}
|
{%- endblock %}
|
||||||
{% block column_classes_total -%}
|
{% block column_classes_total -%}
|
||||||
{% if column is weekend %} weekend{% endif %}
|
{% if column is weekend(user) %} weekend{% endif %}
|
||||||
{%- endblock %}
|
{%- endblock %}
|
||||||
{% endembed %}
|
{% endembed %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
{% block report_content %}
|
{% block report_content %}
|
||||||
{% embed 'reporting/user_list_period_data.html.twig' with {stats: stats, dataType: dataType, period_attribute: period_attribute, subReportRoute: subReportRoute, subReportDate: subReportDate, decimal: decimal} only %}
|
{% embed 'reporting/user_list_period_data.html.twig' with {stats: stats, dataType: dataType, period_attribute: period_attribute, subReportRoute: subReportRoute, subReportDate: subReportDate, decimal: decimal} only %}
|
||||||
{% block period_name %}
|
{% block period_name %}
|
||||||
<th class="text-center text-nowrap{% if column is weekend %} weekend{% endif %}{% if column is today %} today{% endif %}">
|
<th class="text-center text-nowrap{% if column is today %} today{% endif %}">
|
||||||
{{ column|date_weekday }}
|
{{ column|date_weekday }}
|
||||||
</th>
|
</th>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@@ -25,6 +25,6 @@
|
|||||||
{% block duration %}
|
{% block duration %}
|
||||||
{{ period.totalDuration|duration(decimal) }}
|
{{ period.totalDuration|duration(decimal) }}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block period_cell_class %}{% if period.date is weekend %} weekend{% endif %}{% if period.date is today %} today{% endif %}{% endblock %}
|
{% block period_cell_class %}{% if period.date is weekend(userPeriod.user) %} weekend{% endif %}{% if period.date is today %} today{% endif %}{% endblock %}
|
||||||
{% endembed %}
|
{% endembed %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ namespace App\Tests\Twig;
|
|||||||
use App\Configuration\LocaleService;
|
use App\Configuration\LocaleService;
|
||||||
use App\Entity\Timesheet;
|
use App\Entity\Timesheet;
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use App\Entity\UserPreference;
|
|
||||||
use App\Twig\LocaleFormatExtensions;
|
use App\Twig\LocaleFormatExtensions;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Bundle\SecurityBundle\Security;
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
@@ -51,10 +50,9 @@ class LocaleFormatExtensionsTest extends TestCase
|
|||||||
/**
|
/**
|
||||||
* @param string|array $locale
|
* @param string|array $locale
|
||||||
* @param array|string $dateSettings
|
* @param array|string $dateSettings
|
||||||
* @param bool $fdowSunday
|
|
||||||
* @return LocaleFormatExtensions
|
* @return LocaleFormatExtensions
|
||||||
*/
|
*/
|
||||||
protected function getSut($locale, $dateSettings, $fdowSunday = false)
|
protected function getSut($locale, $dateSettings, ?User $user = null)
|
||||||
{
|
{
|
||||||
$language = $locale;
|
$language = $locale;
|
||||||
if (\is_array($locale)) {
|
if (\is_array($locale)) {
|
||||||
@@ -62,9 +60,10 @@ class LocaleFormatExtensionsTest extends TestCase
|
|||||||
$dateSettings = $locale;
|
$dateSettings = $locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($user === null) {
|
||||||
$user = new User();
|
$user = new User();
|
||||||
$user->setPreferenceValue(UserPreference::FIRST_WEEKDAY, ($fdowSunday ? 'sunday' : 'monday'));
|
|
||||||
$user->setTimezone('Europe/Vienna');
|
$user->setTimezone('Europe/Vienna');
|
||||||
|
}
|
||||||
$security = $this->createMock(Security::class);
|
$security = $this->createMock(Security::class);
|
||||||
$security->expects($this->any())->method('getUser')->willReturn($user);
|
$security->expects($this->any())->method('getUser')->willReturn($user);
|
||||||
|
|
||||||
@@ -553,7 +552,11 @@ class LocaleFormatExtensionsTest extends TestCase
|
|||||||
|
|
||||||
public function testIsWeekend(): void
|
public function testIsWeekend(): void
|
||||||
{
|
{
|
||||||
$sut = $this->getSut('en', $this->localeEn, false);
|
$sut = $this->getSut('en', $this->localeEn);
|
||||||
|
self::assertFalse($sut->isWeekend(null));
|
||||||
|
self::assertFalse($sut->isWeekend('2022-01-01'));
|
||||||
|
|
||||||
|
// default case with default user (saturday and sunday is weekend)
|
||||||
self::assertFalse($sut->isWeekend(new \DateTime('first monday this month')));
|
self::assertFalse($sut->isWeekend(new \DateTime('first monday this month')));
|
||||||
self::assertFalse($sut->isWeekend(new \DateTime('first tuesday this month')));
|
self::assertFalse($sut->isWeekend(new \DateTime('first tuesday this month')));
|
||||||
self::assertFalse($sut->isWeekend(new \DateTime('first wednesday this month')));
|
self::assertFalse($sut->isWeekend(new \DateTime('first wednesday this month')));
|
||||||
@@ -561,17 +564,22 @@ class LocaleFormatExtensionsTest extends TestCase
|
|||||||
self::assertFalse($sut->isWeekend(new \DateTime('first friday this month')));
|
self::assertFalse($sut->isWeekend(new \DateTime('first friday this month')));
|
||||||
self::assertTrue($sut->isWeekend(new \DateTime('first saturday this month')));
|
self::assertTrue($sut->isWeekend(new \DateTime('first saturday this month')));
|
||||||
self::assertTrue($sut->isWeekend(new \DateTime('first sunday this month')));
|
self::assertTrue($sut->isWeekend(new \DateTime('first sunday this month')));
|
||||||
self::assertFalse($sut->isWeekend(null));
|
|
||||||
|
|
||||||
$sut = $this->getSut('en', $this->localeEn, true);
|
// seconds case: a user with work-hour configuration
|
||||||
self::assertFalse($sut->isWeekend(new \DateTime('first monday this month')));
|
$user = new User();
|
||||||
self::assertFalse($sut->isWeekend(new \DateTime('first tuesday this month')));
|
$user->setWorkHoursTuesday(1);
|
||||||
self::assertFalse($sut->isWeekend(new \DateTime('first wednesday this month')));
|
$user->setWorkHoursThursday(1);
|
||||||
self::assertFalse($sut->isWeekend(new \DateTime('first thursday this month')));
|
$user->setWorkHoursSaturday(1);
|
||||||
self::assertTrue($sut->isWeekend(new \DateTime('first friday this month')));
|
$user->setWorkHoursSunday(1);
|
||||||
self::assertTrue($sut->isWeekend(new \DateTime('first saturday this month')));
|
|
||||||
self::assertFalse($sut->isWeekend(new \DateTime('first sunday this month')));
|
$sut = $this->getSut('en', $this->localeEn);
|
||||||
self::assertFalse($sut->isWeekend(null));
|
self::assertTrue($sut->isWeekend(new \DateTime('first monday this month'), $user));
|
||||||
|
self::assertFalse($sut->isWeekend(new \DateTime('first tuesday this month'), $user));
|
||||||
|
self::assertTrue($sut->isWeekend(new \DateTime('first wednesday this month'), $user));
|
||||||
|
self::assertFalse($sut->isWeekend(new \DateTime('first thursday this month'), $user));
|
||||||
|
self::assertTrue($sut->isWeekend(new \DateTime('first friday this month'), $user));
|
||||||
|
self::assertFalse($sut->isWeekend(new \DateTime('first saturday this month'), $user));
|
||||||
|
self::assertFalse($sut->isWeekend(new \DateTime('first sunday this month'), $user));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getTimesheet($seconds): Timesheet
|
protected function getTimesheet($seconds): Timesheet
|
||||||
@@ -589,7 +597,7 @@ class LocaleFormatExtensionsTest extends TestCase
|
|||||||
|
|
||||||
public function testChartMoney(): void
|
public function testChartMoney(): void
|
||||||
{
|
{
|
||||||
$sut = $this->getSut('en', $this->localeEn, false);
|
$sut = $this->getSut('en', $this->localeEn);
|
||||||
$this->assertEquals('-123456.78', $sut->moneyChart(-123456.78));
|
$this->assertEquals('-123456.78', $sut->moneyChart(-123456.78));
|
||||||
$this->assertEquals('123456.78', $sut->moneyChart(123456.78));
|
$this->assertEquals('123456.78', $sut->moneyChart(123456.78));
|
||||||
$this->assertEquals('123456.00', $sut->moneyChart(123456));
|
$this->assertEquals('123456.00', $sut->moneyChart(123456));
|
||||||
@@ -598,7 +606,7 @@ class LocaleFormatExtensionsTest extends TestCase
|
|||||||
|
|
||||||
public function testChartDuration(): void
|
public function testChartDuration(): void
|
||||||
{
|
{
|
||||||
$sut = $this->getSut('en', $this->localeEn, false);
|
$sut = $this->getSut('en', $this->localeEn);
|
||||||
$this->assertEquals('34.29', $sut->durationChart(123456));
|
$this->assertEquals('34.29', $sut->durationChart(123456));
|
||||||
$this->assertEquals('-34.29', $sut->durationChart(-123456));
|
$this->assertEquals('-34.29', $sut->durationChart(-123456));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user