fix statistics in user profile for first year not starting in january (#2712)

This commit is contained in:
Kevin Papst
2021-08-10 14:39:52 +02:00
committed by GitHub
parent b31ded22b9
commit 7873bd812e
3 changed files with 46 additions and 10 deletions

View File

@@ -52,11 +52,16 @@ final class ProfileController extends AbstractController
*/ */
public function indexAction(User $profile, TimesheetRepository $repository, TimesheetStatisticService $statisticService): Response public function indexAction(User $profile, TimesheetRepository $repository, TimesheetStatisticService $statisticService): Response
{ {
$dateFactory = $this->getDateTimeFactory();
$userStats = $repository->getUserStatistics($profile, false); $userStats = $repository->getUserStatistics($profile, false);
$firstEntry = $statisticService->findFirstRecordDate($profile); $firstEntry = $statisticService->findFirstRecordDate($profile);
$begin = $firstEntry ?? $this->getDateTimeFactory()->getStartOfMonth(); $begin = $firstEntry ?? $dateFactory->getStartOfMonth();
$end = $this->getDateTimeFactory()->getEndOfMonth(); $end = $dateFactory->getEndOfMonth();
// statistic service does not fill up the complete year by default!
// but we need a full year, because the chart needs always 12 month
$begin = $dateFactory->createStartOfYear($begin);
$viewVars = [ $viewVars = [
'tab' => 'charts', 'tab' => 'charts',

View File

@@ -52,9 +52,10 @@ class DateTimeFactory
{ {
if (null === $date) { if (null === $date) {
$date = $this->createDateTime(); $date = $this->createDateTime();
} else {
$date = clone $date;
} }
$date = clone $date;
$date->modify('first day of this month'); $date->modify('first day of this month');
$date->setTime(0, 0, 0); $date->setTime(0, 0, 0);
@@ -64,11 +65,11 @@ class DateTimeFactory
public function getStartOfWeek(?DateTime $date = null): DateTime public function getStartOfWeek(?DateTime $date = null): DateTime
{ {
if (null === $date) { if (null === $date) {
$date = $this->createDateTime('now'); $from = $this->createDateTime('now');
} else {
$from = clone $date;
} }
$from = clone $date;
$year = $from->format('o'); $year = $from->format('o');
$week = $from->format('W'); $week = $from->format('W');
$firstDay = 1; $firstDay = 1;
@@ -118,9 +119,7 @@ class DateTimeFactory
public function createDateTime(string $datetime = 'now'): DateTime public function createDateTime(string $datetime = 'now'): DateTime
{ {
$date = new DateTime($datetime, $this->getTimezone()); return new DateTime($datetime, $this->getTimezone());
return $date;
} }
/** /**
@@ -130,7 +129,18 @@ class DateTimeFactory
*/ */
public function createDateTimeFromFormat(string $format, ?string $datetime = 'now') public function createDateTimeFromFormat(string $format, ?string $datetime = 'now')
{ {
$date = DateTime::createFromFormat($format, $datetime, $this->getTimezone()); return DateTime::createFromFormat($format, $datetime, $this->getTimezone());
}
public function createStartOfYear(?DateTime $date = null): DateTime
{
if (null === $date) {
$date = $this->createDateTime();
} else {
$date = clone $date;
}
$date->modify('first day of january 00:00:00');
return $date; return $date;
} }

View File

@@ -217,4 +217,25 @@ class DateTimeFactoryTest extends TestCase
self::assertEquals($expected, $end); self::assertEquals($expected, $end);
} }
public function testCreateStartOfYear()
{
$sut = $this->createDateTimeFactory(self::TEST_TIMEZONE);
$now = $sut->createDateTime();
$year = $sut->createStartOfYear();
self::assertEquals($now->format('Y'), $year->format('Y'));
self::assertEquals('01', $year->format('m'));
self::assertEquals('01', $year->format('d'));
self::assertEquals('00:00:00', $year->format('H:i:s'));
$now->setTime(0, 0, 0);
self::assertEquals($now->format('H:i:s'), $year->format('H:i:s'));
$begin = $sut->createDateTime('2017-12-31 23:59:59');
$year = $sut->createStartOfYear($begin);
self::assertEquals('2017', $year->format('Y'));
self::assertEquals('01', $year->format('m'));
self::assertEquals('01', $year->format('d'));
self::assertEquals('00:00:00', $year->format('H:i:s'));
}
} }