diff --git a/src/Constants.php b/src/Constants.php index a0eb0c00..f50d2b51 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -17,11 +17,11 @@ class Constants /** * The current release version */ - public const VERSION = '1.19.3'; + public const VERSION = '1.19.4'; /** * The current release: major * 10000 + minor * 100 + patch */ - public const VERSION_ID = 11903; + public const VERSION_ID = 11904; /** * The current release status, either "stable" or "dev" */ diff --git a/src/Controller/AbstractController.php b/src/Controller/AbstractController.php index 109857b8..8a05149d 100644 --- a/src/Controller/AbstractController.php +++ b/src/Controller/AbstractController.php @@ -18,6 +18,7 @@ use App\Timesheet\DateTimeFactory; use App\Utils\LocaleFormats; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as BaseAbstractController; +use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Contracts\Service\ServiceSubscriberInterface; @@ -39,6 +40,13 @@ abstract class AbstractController extends BaseAbstractController implements Serv return $this->container->get('translator'); } + public function createFormForGetRequest(string $type = FormType::class, $data = null, array $options = []): FormInterface + { + return $this->container + ->get('form.factory') + ->createNamed('', $type, $data, $options); + } + private function getLogger(): LoggerInterface { return $this->container->get('logger'); diff --git a/src/Controller/Reporting/CustomerMonthlyProjectsController.php b/src/Controller/Reporting/CustomerMonthlyProjectsController.php new file mode 100644 index 00000000..f4a74f72 --- /dev/null +++ b/src/Controller/Reporting/CustomerMonthlyProjectsController.php @@ -0,0 +1,111 @@ +render( + 'reporting/customer/monthly_projects.html.twig', + $this->getData($request, $statisticService, $userRepository) + ); + } + + /** + * @Route(path="/export", name="report_customer_monthly_projects_export", methods={"GET","POST"}) + */ + public function export(Request $request, TimesheetStatisticService $statisticService, UserRepository $userRepository): Response + { + $data = $this->getData($request, $statisticService, $userRepository); + + $content = $this->render('reporting/customer/monthly_projects_export.html.twig', $data)->getContent(); + + $reader = new Html(); + $spreadsheet = $reader->loadFromString($content); + + $writer = new BinaryFileResponseWriter(new XlsxWriter(), 'kimai-export-users-monthly'); + + return $writer->getFileResponse($spreadsheet); + } + + private function getData(Request $request, TimesheetStatisticService $statisticService, UserRepository $userRepository): array + { + $currentUser = $this->getUser(); + $dateTimeFactory = $this->getDateTimeFactory(); + + $query = new UserQuery(); + $query->setCurrentUser($currentUser); + $allUsers = $userRepository->getUsersForQuery($query); + + $values = new CustomerMonthlyProjects(); + $values->setDate($dateTimeFactory->getStartOfMonth()); + + $form = $this->createFormForGetRequest(CustomerMonthlyProjectsForm::class, $values, [ + 'timezone' => $dateTimeFactory->getTimezone()->getName(), + 'start_date' => $values->getDate(), + ]); + + $form->submit($request->query->all(), false); + + if ($form->isSubmitted() && !$form->isValid()) { + $values->setDate($dateTimeFactory->getStartOfMonth()); + } + + if ($values->getDate() === null) { + $values->setDate($dateTimeFactory->getStartOfMonth()); + } + + $start = $values->getDate(); + $start = $dateTimeFactory->getStartOfMonth($start); + $end = $dateTimeFactory->getEndOfMonth($start); + + $previous = clone $start; + $previous->modify('-1 month'); + + $next = clone $start; + $next->modify('+1 month'); + + $stats = $statisticService->getGroupedByCustomerProjectActivityUser($start, $end, $allUsers); + + return [ + 'dataType' => $values->getSumType(), + 'report_title' => 'report_customer_monthly_projects', + 'export_route' => 'report_customer_monthly_projects_export', + 'form' => $form->createView(), + 'current' => $start, + 'next' => $next, + 'previous' => $previous, + 'decimal' => $values->isDecimal(), + 'stats' => $stats, + ]; + } +} diff --git a/src/Controller/Security/SecurityController.php b/src/Controller/Security/SecurityController.php index 66a1b866..ebea51d7 100644 --- a/src/Controller/Security/SecurityController.php +++ b/src/Controller/Security/SecurityController.php @@ -32,6 +32,10 @@ final class SecurityController extends AbstractController */ public function loginAction(Request $request): Response { + if ($this->isGranted('IS_AUTHENTICATED_FULLY')) { + return $this->redirectToRoute('homepage'); + } + /** @var SessionInterface $session */ $session = $request->getSession(); diff --git a/src/Invoice/Hydrator/InvoiceItemDefaultHydrator.php b/src/Invoice/Hydrator/InvoiceItemDefaultHydrator.php index 2ba3caa9..f3607ab5 100644 --- a/src/Invoice/Hydrator/InvoiceItemDefaultHydrator.php +++ b/src/Invoice/Hydrator/InvoiceItemDefaultHydrator.php @@ -32,7 +32,11 @@ class InvoiceItemDefaultHydrator implements InvoiceItemHydrator $rate = $item->getRate(); $internalRate = $item->getInternalRate(); $appliedRate = $item->getHourlyRate(); - $amount = $formatter->getFormattedDuration($item->getDuration()); + if ($this->model->getTemplate()->isDecimalDuration()) { + $amount = $formatter->getFormattedDecimalDuration($item->getDuration()); + } else { + $amount = $formatter->getFormattedDuration($item->getDuration()); + } $description = $item->getDescription(); if ($item->isFixedRate()) { diff --git a/src/Reporting/CustomerMonthlyProjects/CustomerMonthlyProjects.php b/src/Reporting/CustomerMonthlyProjects/CustomerMonthlyProjects.php new file mode 100644 index 00000000..18194669 --- /dev/null +++ b/src/Reporting/CustomerMonthlyProjects/CustomerMonthlyProjects.php @@ -0,0 +1,16 @@ +add('date', MonthPickerType::class, [ + 'model_timezone' => $options['timezone'], + 'view_timezone' => $options['timezone'], + 'start_date' => $options['start_date'], + ]); + $builder->add('sumType', ReportSumType::class); + } + + /** + * {@inheritdoc} + */ + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => CustomerMonthlyProjects::class, + 'timezone' => date_default_timezone_get(), + 'start_date' => new \DateTime(), + 'csrf_protection' => false, + 'method' => 'GET', + ]); + } +} diff --git a/src/Reporting/ReportingService.php b/src/Reporting/ReportingService.php index f37ca5d8..be8dcbaa 100644 --- a/src/Reporting/ReportingService.php +++ b/src/Reporting/ReportingService.php @@ -60,6 +60,9 @@ final class ReportingService $event->addReport(new Report('daterange_projects', 'report_project_daterange', 'report_project_daterange', 'project')); $event->addReport(new Report('inactive_projects', 'report_project_inactive', 'report_inactive_project', 'project')); } + if ($this->security->isGranted('view_other_reporting') && $this->security->isGranted('view_other_timesheet')) { + $event->addReport(new Report('report_customer_monthly_projects', 'report_customer_monthly_projects', 'report_customer_monthly_projects', 'customer')); + } $this->dispatcher->dispatch($event); } diff --git a/src/Repository/TimesheetRepository.php b/src/Repository/TimesheetRepository.php index 2659e7e6..c2a5b060 100644 --- a/src/Repository/TimesheetRepository.php +++ b/src/Repository/TimesheetRepository.php @@ -55,7 +55,7 @@ class TimesheetRepository extends EntityRepository public const STATS_QUERY_MONTHLY = 'monthly'; /** - * Fetches the raw data of an timesheet, to allow comparison eg. of submitted and previously stored data. + * Fetches the raw data of a timesheet, to allow comparison e.g. of submitted and previously stored data. * * @param Timesheet $id * @return array @@ -66,6 +66,8 @@ class TimesheetRepository extends EntityRepository $qb ->select([ 't.rate', + 't.begin', + 't.end', 't.duration', 't.hourlyRate', 't.billable', diff --git a/src/Timesheet/TimesheetStatisticService.php b/src/Timesheet/TimesheetStatisticService.php index bb0944cd..13737364 100644 --- a/src/Timesheet/TimesheetStatisticService.php +++ b/src/Timesheet/TimesheetStatisticService.php @@ -9,11 +9,16 @@ namespace App\Timesheet; +use App\Entity\Activity; +use App\Entity\Customer; +use App\Entity\Project; use App\Entity\User; use App\Model\DailyStatistic; use App\Model\MonthlyStatistic; use App\Repository\TimesheetRepository; use DateTime; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Query\Expr\Join; final class TimesheetStatisticService { @@ -21,10 +26,12 @@ final class TimesheetStatisticService * @var TimesheetRepository */ private $repository; + private $entityManager; - public function __construct(TimesheetRepository $repository) + public function __construct(TimesheetRepository $repository, EntityManagerInterface $entityManager) { $this->repository = $repository; + $this->entityManager = $entityManager; } /** @@ -323,4 +330,141 @@ final class TimesheetStatisticService return array_values($stats); } + + /** + * @param DateTime $begin + * @param DateTime $end + * @param User[] $users + * @return array + */ + public function getGroupedByCustomerProjectActivityUser(DateTime $begin, DateTime $end, array $users): array + { + $stats = []; + + $qb = $this->repository->createQueryBuilder('t'); + $qb + ->select('COALESCE(SUM(t.rate), 0) as rate') + ->addSelect('COALESCE(SUM(t.duration), 0) as duration') + ->addSelect('COALESCE(SUM(t.internalRate), 0) as internalRate') + ->addSelect('IDENTITY(t.user) as user') + ->addSelect('IDENTITY(t.activity) as activity') + ->addSelect('IDENTITY(t.project) as project') + ->where($qb->expr()->isNotNull('t.end')) + ->andWhere($qb->expr()->between('t.begin', ':begin', ':end')) + ->andWhere($qb->expr()->in('t.user', ':user')) + ->setParameter('begin', $begin) + ->setParameter('end', $end) + ->setParameter('user', $users) + ->groupBy('project') + ->addGroupBy('activity') + ->addGroupBy('user') + ; + + $results = $qb->getQuery()->getResult(); + + $projectIds = []; + $activityIds = []; + $userIds = []; + + foreach ($results as $row) { + $projectId = $row['project']; + $activityId = $row['activity']; + $userId = $row['user']; + + $projectIds[$projectId] = $projectId; + $activityIds[$activityId] = $activityId; + $userIds[$userId] = $userId; + + if (!isset($stats[$projectId])) { + $stats[$projectId] = [ + 'id' => $projectId, + 'name' => null, + 'activities' => [], + 'duration' => 0, + 'rate' => 0, + 'internalRate' => 0, + 'max_users' => 0, + ]; + } + + $stats[$projectId]['duration'] += (int) $row['duration']; + $stats[$projectId]['rate'] += (int) $row['rate']; + $stats[$projectId]['internalRate'] += (int) $row['internalRate']; + + if (!isset($stats[$projectId]['activities'][$activityId])) { + $stats[$projectId]['activities'][$activityId] = [ + 'id' => $activityId, + 'name' => null, + 'users' => [], + 'duration' => 0, + 'rate' => 0, + 'internalRate' => 0, + ]; + } + + $stats[$projectId]['activities'][$activityId]['duration'] += (int) $row['duration']; + $stats[$projectId]['activities'][$activityId]['rate'] += (int) $row['rate']; + $stats[$projectId]['activities'][$activityId]['internalRate'] += (int) $row['internalRate']; + + if (!isset($stats[$projectId]['activities'][$activityId]['users'][$userId])) { + $stats[$projectId]['activities'][$activityId]['users'][$userId] = [ + 'id' => $userId, + 'name' => null, + 'duration' => 0, + 'rate' => 0, + 'internalRate' => 0, + ]; + } + + $stats[$projectId]['activities'][$activityId]['users'][$userId]['duration'] += (int) $row['duration']; + $stats[$projectId]['activities'][$activityId]['users'][$userId]['rate'] += (int) $row['rate']; + $stats[$projectId]['activities'][$activityId]['users'][$userId]['internalRate'] += (int) $row['internalRate']; + } + + $qb = $this->entityManager->createQueryBuilder(); + $qb + ->select('a.id, a.name') + ->from(Activity::class, 'a', 'a.id') + ->where($qb->expr()->in('a.id', ':id')) + ->setParameter('id', array_values($activityIds)) + ; + $activities = $qb->getQuery()->getResult(); + + $qb = $this->entityManager->createQueryBuilder(); + $qb + ->select('p.id, p.name, c.id as customer_id, c.name as customer, c.currency') + ->from(Project::class, 'p', 'p.id') + ->leftJoin(Customer::class, 'c', Join::WITH, 'c.id = p.customer') + ->where($qb->expr()->in('p.id', ':id')) + ->setParameter('id', array_values($projectIds)) + ; + $projects = $qb->getQuery()->getResult(); + + $qb = $this->entityManager->createQueryBuilder(); + $qb + ->select('u') + ->from(User::class, 'u', 'u.id') + ->where($qb->expr()->in('u.id', ':id')) + ->setParameter('id', array_values($userIds)) + ; + $users = $qb->getQuery()->getResult(); + + foreach (array_keys($stats) as $pid) { + $stats[$pid]['name'] = $projects[$pid]['name']; + foreach (array_keys($stats[$pid]['activities']) as $aid) { + $stats[$pid]['activities'][$aid]['name'] = $activities[$aid]['name']; + foreach (array_keys($stats[$pid]['activities'][$aid]['users']) as $uid) { + $stats[$pid]['activities'][$aid]['users'][$uid]['name'] = $users[$uid]->getDisplayName(); + } + $stats[$pid]['max_users'] = max($stats[$pid]['max_users'], \count($stats[$pid]['activities'][$aid]['users'])); + } + } + + return [ + 'stats' => $stats, + 'projects' => $projects, + 'activities' => $activities, + 'users' => $users, + ]; + } } diff --git a/src/Validator/Constraints/TimesheetBudgetUsedValidator.php b/src/Validator/Constraints/TimesheetBudgetUsedValidator.php index 420fd6da..8a7e7d3b 100644 --- a/src/Validator/Constraints/TimesheetBudgetUsedValidator.php +++ b/src/Validator/Constraints/TimesheetBudgetUsedValidator.php @@ -97,7 +97,7 @@ final class TimesheetBudgetUsedValidator extends ConstraintValidator // if an existing entry was updated, but "duration", "rate" and "billable" were not changed: // do not validate! this could for example happen when export flag is changed OR if "prevent overbooking" // config was recently activated and this is an old entry - if ($duration === $rawData['duration'] && $rate === $rawData['rate'] && $timesheet->isBillable() === $rawData['billable']) { + if ($duration === $rawData['duration'] && $rate === $rawData['rate'] && $timesheet->isBillable() === $rawData['billable'] && $timesheet->getBegin()->format('Y.m.d') === $rawData['begin']->format('Y.m.d')) { return; } @@ -174,12 +174,12 @@ final class TimesheetBudgetUsedValidator extends ConstraintValidator private function addBudgetViolation(TimesheetBudgetUsed $constraint, Timesheet $timesheet, string $field, float $budget, float $rate) { - // using the locale of the assigned user is not the best solution, but allows to be independent from the request stack + // using the locale of the assigned user is not the best solution, but allows to be independent of the request stack $helper = new LocaleHelper($timesheet->getUser()->getLanguage()); $currency = $timesheet->getProject()->getCustomer()->getCurrency(); $free = $budget - $rate; - $free = $free > 0 ? $free : 0; + $free = max($free, 0); $this->context->buildViolation($constraint->messageRate) ->atPath($field) @@ -198,7 +198,7 @@ final class TimesheetBudgetUsedValidator extends ConstraintValidator $durationFormat = new Duration(); $free = $budget - $duration; - $free = $free > 0 ? $free : 0; + $free = max($free, 0); $this->context->buildViolation($constraint->messageTime) ->atPath($field) diff --git a/templates/reporting/customer/monthly_projects.html.twig b/templates/reporting/customer/monthly_projects.html.twig new file mode 100644 index 00000000..74a55ef8 --- /dev/null +++ b/templates/reporting/customer/monthly_projects.html.twig @@ -0,0 +1,52 @@ +{% extends 'reporting/layout.html.twig' %} + +{% block report_title %}{{ report_title|trans({}, 'reporting') }}{% endblock %} + +{% block report %} + + {% embed '@AdminLTE/Widgets/box-widget.html.twig' %} + {% from "macros/widgets.html.twig" import nothing_found %} + {% block box_before %} + {{ form_start(form, {'attr': {'class': 'form-inline form-reporting', 'id': 'report-toolbar-form'}}) }} + {% endblock %} + {% block box_after %} + {{ form_end(form) }} + {% endblock %} + {% block box_title %} + {{ form_widget(form.date) }} + {% if form.sumType.vars.choices|length > 1 %} +
+ + +
+ {% endif %} + + {% endblock %} + {% block box_body_class %} table-responsive {% if stats is not empty %}no-padding{% endif %}{% endblock %} + {% block box_body %} + {% if stats is empty %} + {{ nothing_found() }} + {% else %} + {% embed 'reporting/customer/monthly_projects_data.html.twig' with {'dataTypeFormat': null, 'stats': stats, 'dataType': dataType, 'decimal': decimal} only %} + {% set rowspanStyle = 'vertical-align: middle' %} + {% endembed %} + {% endif %} + {% endblock %} + {% endembed %} + +{% endblock %} + +{% block javascripts %} + {{ parent() }} + +{% endblock %} diff --git a/templates/reporting/customer/monthly_projects_data.html.twig b/templates/reporting/customer/monthly_projects_data.html.twig new file mode 100644 index 00000000..225003f6 --- /dev/null +++ b/templates/reporting/customer/monthly_projects_data.html.twig @@ -0,0 +1,100 @@ +{%- set absoluteDuration = 0 -%} +{%- set absoluteInternalRate = 0 -%} +{%- set absoluteRate = 0 -%} +{%- set totalsDuration = {} -%} +{%- set totalsInternalRate = {} -%} +{%- set totalsRate = {} -%} +{% if dataType == 'rate' %} + {% set dataTypeTitle = 'stats.amountTotal' %} +{% elseif dataType == 'internalRate' %} + {% set dataTypeTitle = 'label.rate_internal' %} +{% else %} + {% set dataTypeTitle = 'stats.durationTotal' %} +{% endif %} + + + + + {% for activity in stats.activities %} + + {% endfor %} + + + + {% for activity in stats.activities %} + + + + {% endfor %} + + + {% set customer = null %} + {% set maxLength = (stats.activities|length) * 3 + 2 %} + + {% for project in stats.stats %} + {% set activityId = null %} + {% set rowspan = project['max_users'] %} + {% set currency = stats.projects[project.id]['currency'] %} + {% if customer != stats.projects[project.id]['customer_id'] %} + + + + {% set customer = stats.projects[project.id]['customer_id'] %} + {% endif %} + + {% for i in 1..rowspan %} + + {% if loop.first %} + 1 %} rowspan="{{ rowspan }}" style="{{ rowspanStyle }}"{% endif %}>{{ project.name }} + {% endif %} + {% for activity in stats.activities %} + {% if project.activities[activity.id] is defined and i <= project.activities[activity.id]['users']|length %} + {% set user = project.activities[activity.id]['users']|slice(-i) %} + + + {% if loop.parent.loop.first %} + 1 %} rowspan="{{ rowspan }}"{% endif %} class="text-center text-nowrap"{% if dataTypeFormat is not null %} data-format="{{ dataTypeFormat }}"{% endif %}> + {% set value = project.activities[activity.id][dataType] %} + {% block project_activity %} + {% if dataType == 'rate' or dataType == 'internalRate' %} + {{ value|money(currency) }} + {% else %} + {{ value|duration(decimal) }} + {% endif %} + {% endblock %} + + {% endif %} + {% else %} + + + {% if loop.parent.loop.first %} + 1 %} rowspan="{{ rowspan }}"{% endif %}> + {% endif %} + {% endif %} + {% endfor %} + {% if loop.first %} + 1 %} rowspan="{{ rowspan }}" style="{{ rowspanStyle }}"{% endif %} class="text-center text-nowrap"{% if dataTypeFormat is not null %} data-format="{{ dataTypeFormat }}"{% endif %}> + {% set value = project[dataType] %} + {% block project_total %} + {% if dataType == 'rate' or dataType == 'internalRate' %} + {{ value|money(currency) }} + {% else %} + {{ value|duration(decimal) }} + {% endif %} + {% endblock %} + + {% endif %} + + {% endfor %} + {% endfor %} + +
{{ 'label.project'|trans }}{{ activity.name }}{{ dataTypeTitle|trans }}
{{ 'label.user'|trans }}{{ dataTypeTitle|trans }}{{ 'sum.total'|trans }}
{{ stats.projects[project.id]['customer'] }}
{{ user.0.name }} + {% set value = user.0[dataType] %} + {% block user_activity %} + {% if dataType == 'rate' or dataType == 'internalRate' %} + {{ value|money(currency) }} + {% else %} + {{ value|duration(decimal) }} + {% endif %} + {% endblock %} +
diff --git a/templates/reporting/customer/monthly_projects_export.html.twig b/templates/reporting/customer/monthly_projects_export.html.twig new file mode 100644 index 00000000..52186efe --- /dev/null +++ b/templates/reporting/customer/monthly_projects_export.html.twig @@ -0,0 +1,30 @@ +{% embed 'reporting/customer/monthly_projects_data.html.twig' with {'decimal': true, 'stats': stats, 'dataType': dataType} only %} + {% set dataTypeFormat = null %} + {% if dataType == 'rate' or dataType == 'internalRate' %} + {% set dataTypeFormat = constant('App\\Export\\Base\\AbstractSpreadsheetRenderer::RATE_FORMAT_NO_CURRENCY') %} + {% elseif dataType == 'duration' %} + {% set dataTypeFormat = constant('App\\Export\\Base\\AbstractSpreadsheetRenderer::DURATION_DECIMAL') %} + {% endif %} + {% set rowspanStyle = 'vertical-align:center' %} + {% block user_activity %} + {% if dataType == 'rate' or dataType == 'internalRate' %} + {{ value|money(currency) }} + {% else %} + {{ value / 3600 }} + {% endif %} + {% endblock %} + {% block project_activity %} + {% if dataType == 'rate' or dataType == 'internalRate' %} + {{ value|money(currency) }} + {% else %} + {{ value / 3600 }} + {% endif %} + {% endblock %} + {% block project_total %} + {% if dataType == 'rate' or dataType == 'internalRate' %} + {{ value|money(currency) }} + {% else %} + {{ value / 3600 }} + {% endif %} + {% endblock %} +{% endembed %} diff --git a/tests/Controller/Reporting/CustomerMonthlyProjectsControllerTest.php b/tests/Controller/Reporting/CustomerMonthlyProjectsControllerTest.php new file mode 100644 index 00000000..30f0f551 --- /dev/null +++ b/tests/Controller/Reporting/CustomerMonthlyProjectsControllerTest.php @@ -0,0 +1,103 @@ +assertUrlIsSecured('/reporting/customer/monthly_projects/view'); + } + + public function testExportReportIsSecure() + { + $this->assertUrlIsSecured('/reporting/customer/monthly_projects/export'); + } + + private function prepareReport(): HttpKernelBrowser + { + $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); + + $customers = new CustomerFixtures(); + $customers->setIsVisible(true); + $customers->setAmount(1); + $customers = $this->importFixture($customers); + + $projects = new ProjectFixtures(); + $projects->setCustomers($customers); + $projects->setAmount(2); + $projects->setIsVisible(true); + $projects->setCallback(function (Project $project) { + $project->setIsMonthlyBudget(); + }); + $this->importFixture($projects); + + $activities = new ActivityFixtures(); + $activities->setAmount(5); + $activities->setIsGlobal(true); + $activities = $this->importFixture($activities); + + $timesheets = new TimesheetFixtures(); + $timesheets->setAmount(10); + $timesheets->setActivities($activities); + $timesheets->setStartDate(new \DateTime('first day of this month')); + $timesheets->setUser($this->getUserByRole(User::ROLE_TEAMLEAD)); + $this->importFixture($timesheets); + $timesheets = new TimesheetFixtures(); + $timesheets->setAmount(10); + $timesheets->setActivities($activities); + $timesheets->setStartDate(new \DateTime('first day of last month')); + $timesheets->setUser($this->getUserByRole(User::ROLE_TEAMLEAD)); + $this->importFixture($timesheets); + + return $client; + } + + public function testReport() + { + $client = $this->prepareReport(); + + $this->assertAccessIsGranted($client, '/reporting/customer/monthly_projects/view'); + self::assertStringContainsString('
', $client->getResponse()->getContent()); + $rows = $client->getCrawler()->filterXPath("//table[contains(@class, 'dataTable')]/tbody/tr[not(@class='summary')]"); + self::assertGreaterThan(0, $rows->count()); + } + + public function testExport() + { + $client = $this->prepareReport(); + + $this->assertAccessIsGranted($client, '/reporting/customer/monthly_projects/export'); + + $response = $client->getResponse(); + $this->assertTrue($response->isSuccessful()); + self::assertInstanceOf(BinaryFileResponse::class, $response); + + // temporary file! + $file = $response->getFile(); + self::assertFileDoesNotExist($response->getFile()); + + $this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $response->headers->get('Content-Type')); + $this->assertStringContainsString('attachment; filename=kimai-export-users-', $response->headers->get('Content-Disposition')); + } +} diff --git a/tests/Controller/Security/SecurityControllerTest.php b/tests/Controller/Security/SecurityControllerTest.php index d423bc88..047bdbd7 100644 --- a/tests/Controller/Security/SecurityControllerTest.php +++ b/tests/Controller/Security/SecurityControllerTest.php @@ -10,6 +10,7 @@ namespace App\Tests\Controller\Security; use App\Controller\Security\SecurityController; +use App\Entity\User; use App\Tests\Controller\ControllerBaseTest; use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; @@ -76,6 +77,21 @@ class SecurityControllerTest extends ControllerBaseTest $this->assertTrue($client->getResponse()->isSuccessful()); } + public function testLoginAlreadyLoggedIn() + { + $client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD); + + $this->request($client, '/login'); + + $this->assertIsRedirect($client, '/homepage'); // redirect to homepage + $client->followRedirect(); + + $this->assertIsRedirect($client, '/timesheet/'); // redirect to configured start page + $client->followRedirect(); + + $this->assertTrue($client->getResponse()->isSuccessful()); + } + public function testLoginNegative() { $client = self::createClient(); diff --git a/tests/Reporting/ReportingServiceTest.php b/tests/Reporting/ReportingServiceTest.php index 58af2c63..ed195eb6 100644 --- a/tests/Reporting/ReportingServiceTest.php +++ b/tests/Reporting/ReportingServiceTest.php @@ -47,6 +47,6 @@ class ReportingServiceTest extends TestCase $sut = $this->getSut(true); $reports = $sut->getAvailableReports(new User()); self::assertIsArray($reports); - self::assertCount(10, $reports); + self::assertCount(11, $reports); } } diff --git a/tests/Validator/Constraints/TimesheetBudgetUsedValidatorTest.php b/tests/Validator/Constraints/TimesheetBudgetUsedValidatorTest.php index 07bf1f64..25bedff6 100644 --- a/tests/Validator/Constraints/TimesheetBudgetUsedValidatorTest.php +++ b/tests/Validator/Constraints/TimesheetBudgetUsedValidatorTest.php @@ -190,66 +190,72 @@ class TimesheetBudgetUsedValidatorTest extends ConstraintValidatorTestCase { return [ // activity: violations ---------------------------------------------------------------------- - // previously logged available budgets expected violation duration entry currently in database - 'a_a' => [1230, null, null, null, null, null, 3600, null, null, null, null, null, '00:20', '00:39', '01:00', 'activity', '+3600 seconds'], - 'a_b' => [null, 1001.0, null, null, null, null, null, 1000.0, null, null, null, null, '€1,001.00', '€0.00', '€1,000.00', 'activity', '+3600 seconds'], + // previously logged available budgets expected violation duration entry currently in database + 'a_a' => [1230, null, null, null, null, null, null, 3600, null, null, null, null, null, null, null, '00:20', '00:39', '01:00', 'activity', '+3600 seconds'], + 'a_b' => [null, 1001.0, null, null, null, null, null, null, 1000.0, null, null, null, null, null, null, '€1,001.00', '€0.00', '€1,000.00', 'activity', '+3600 seconds'], // activity: no violations - 'a_c' => [1230, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'a_d' => [null, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'a_e' => [1230, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'a_c' => [1230, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'a_d' => [null, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'a_e' => [1230, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - // previously logged available budgets expected violation duration entry currently in database - 'a_f1' => [1320, null, null, null, null, null, 3600, null, null, null, null, null, '00:22', '00:38', '01:00', 'activity', '+3600 seconds', ['rate' => 1.0, 'duration' => 1000]], - 'a_h1' => [7200, null, null, null, null, null, 7200, null, null, null, null, null, '02:00', '00:00', '02:00', 'activity', '+3601 seconds', ['rate' => 1.0, 'duration' => 3600]], - 'a_h2' => [3601, null, null, null, null, null, 3600, null, null, null, null, null, null, null, null, null, '+3600 seconds', ['rate' => 1.0, 'duration' => 3601]], - 'a_g0' => [null, 1002.0, null, null, null, null, null, 1000.0, null, null, null, null, '1,002.00', '0.00', '1,000.00', 'activity', '+3600 seconds', ['rate' => 1.0, 'duration' => 1010]], - 'a_g1' => [null, 1002.0, null, null, null, null, null, 1000.0, null, null, null, null, null, null, null, null, '+3600 seconds', ['rate' => 2.0, 'duration' => 0]], + // previously logged available budgets expected violation duration entry currently in database + 'a_f1' => [1320, null, null, null, null, null, null, 3600, null, null, null, null, null, null, null, '00:22', '00:38', '01:00', 'activity', '+3600 seconds', ['rate' => 1.0, 'duration' => 1000]], + 'a_h1' => [7200, null, null, null, null, null, null, 7200, null, null, null, null, null, null, null, '02:00', '00:00', '02:00', 'activity', '+3601 seconds', ['rate' => 1.0, 'duration' => 3600]], + 'a_h2' => [3601, null, null, null, null, null, null, 3600, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds', ['rate' => 1.0, 'duration' => 3601]], + 'a_g0' => [null, 1002.0, null, null, null, null, null, null, 1000.0, null, null, null, null, null, null, '1,002.00', '0.00', '1,000.00', 'activity', '+3600 seconds', ['rate' => 1.0, 'duration' => 1010]], + 'a_g1' => [null, 1002.0, null, null, null, null, null, null, 1000.0, null, null, null, null, null, null, null, null, null, null, '+3600 seconds', ['rate' => 2.0, 'duration' => 0]], // nothing changed => no violation - 'a_x1' => [3600, 1000.0, null, null, null, null, 3600, 1000.0, null, null, null, null, null, null, null, null, '+3600 seconds', ['rate' => 1000.0, 'duration' => 3600], new Rate(1000.0, 0.00)], + 'a_x1' => [3600, 1000.0, null, null, null, null, null, 3600, 1000.0, null, null, null, null, null, null, null, null, null, null, '+3600 seconds', ['rate' => 1000.0, 'duration' => 3600], new Rate(1000.0, 0.00)], + // date changed => violation + 'a_x2' => [3600, 1000.0, null, null, null, null, 'month', 3600, 1000.0, null, null, null, null, null, null, '1,000.00', '0.00', '1,000.00', 'activity', '+3600 seconds', ['rate' => 999.0, 'duration' => 3599, 'begin' => new DateTime('2021-03-17 16:15:00'), 'end' => new DateTime('2021-03-17 17:15:01'), 'billable' => true], new Rate(1000.0, 0.00)], + // date changed but not violation was raised + 'a_x3' => [3600, 1000.0, null, null, null, null, 'month', 3600, 1000.0, null, null, null, null, null, null, null, null, null, null, '+3600 seconds', ['rate' => 999.0, 'duration' => 3599, 'begin' => new DateTime('2021-03-17 16:15:00'), 'end' => new DateTime('2021-03-17 17:15:01'), 'billable' => false], new Rate(1000.0, 0.00)], + 'a_x4' => [3600, 1000.0, null, null, null, null, 'month', 3600, 1000.0, null, null, null, null, null, null, null, null, null, null, '+3600 seconds', ['rate' => 1000.0, 'duration' => 3600, 'begin' => new DateTime('2021-03-17 16:15:00'), 'end' => new DateTime('2021-03-17 17:15:01'), 'billable' => true], new Rate(1000.0, 0.00)], + 'a_x5' => [3600, 1000.0, null, null, null, null, 'month', 3600, 1000.0, null, null, null, null, null, null, null, null, null, null, '+3600 seconds', ['rate' => 1000.0, 'duration' => 3600, 'begin' => new DateTime('2021-03-17 16:15:00'), 'end' => new DateTime('2021-03-17 17:15:01'), 'billable' => false], new Rate(1000.0, 0.00)], // project: violations ---------------------------------------------------------------------- - 'p_j' => [null, null, 1230, null, null, null, null, null, 3600, null, null, null, '00:20', '00:39', '01:00', 'project', '+3600 seconds'], - 'p_k' => [null, null, null, 1001.0, null, null, null, null, null, 1000.0, null, null, '€1,001.00', '€0.00', '€1,000.00', 'project', '+3600 seconds'], + 'p_j' => [null, null, 1230, null, null, null, null, null, null, null, 3600, null, null, null, null, '00:20', '00:39', '01:00', 'project', '+3600 seconds'], + 'p_k' => [null, null, null, 1001.0, null, null, null, null, null, null, null, 1000.0, null, null, null, '€1,001.00', '€0.00', '€1,000.00', 'project', '+3600 seconds'], - // previously logged available budgets expected violation duration entry currently in database - 'p_f1' => [null, null, 1320, null, null, null, null, null, 3600, null, null, null, '00:22', '00:38', '01:00', 'project', '+3600 seconds', ['rate' => 1.0, 'duration' => 1000]], - 'p_h1' => [null, null, 7200, null, null, null, null, null, 7200, null, null, null, '02:00', '00:00', '02:00', 'project', '+3601 seconds', ['rate' => 1.0, 'duration' => 3600]], - 'p_h2' => [null, null, 3601, null, null, null, null, null, 3600, null, null, null, null, null, null, null, '+3600 seconds', ['rate' => 1.0, 'duration' => 3601]], - 'p_g0' => [null, null, null, 1002.0, null, null, null, null, null, 1000.0, null, null, '1,002.00', '0.00', '1,000.00', 'project', '+3600 seconds', ['rate' => 1.0, 'duration' => 1010]], - 'p_g1' => [null, null, null, 1002.0, null, null, null, null, null, 1000.0, null, null, null, null, null, null, '+3600 seconds', ['rate' => 2.0, 'duration' => 0]], + // previously logged available budgets expected violation duration entry currently in database + 'p_f1' => [null, null, 1320, null, null, null, null, null, null, null, 3600, null, null, null, null, '00:22', '00:38', '01:00', 'project', '+3600 seconds', ['rate' => 1.0, 'duration' => 1000]], + 'p_h1' => [null, null, 7200, null, null, null, null, null, null, null, 7200, null, null, null, null, '02:00', '00:00', '02:00', 'project', '+3601 seconds', ['rate' => 1.0, 'duration' => 3600]], + 'p_h2' => [null, null, 3601, null, null, null, null, null, null, null, 3600, null, null, null, null, null, null, null, null, '+3600 seconds', ['rate' => 1.0, 'duration' => 3601]], + 'p_g0' => [null, null, null, 1002.0, null, null, null, null, null, null, null, 1000.0, null, null, null, '1,002.00', '0.00', '1,000.00', 'project', '+3600 seconds', ['rate' => 1.0, 'duration' => 1010]], + 'p_g1' => [null, null, null, 1002.0, null, null, null, null, null, null, null, 1000.0, null, null, null, null, null, null, null, '+3600 seconds', ['rate' => 2.0, 'duration' => 0]], // project: no violations - 'p_n' => [null, null, 1230, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'p_o' => [null, null, null, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'p_p' => [null, null, 1230, 1001, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'p_n' => [null, null, 1230, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'p_o' => [null, null, null, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'p_p' => [null, null, 1230, 1001, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'p_q' => [1230, null, 1230, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'p_r' => [1230, 1001.0, 1230, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'p_s' => [null, 1001.0, null, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'p_t' => [null, 1001.0, 1230, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'p_u' => [1230, 1001.0, 1230, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'p_q' => [1230, null, 1230, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'p_r' => [1230, 1001.0, 1230, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'p_s' => [null, 1001.0, null, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'p_t' => [null, 1001.0, 1230, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'p_u' => [1230, 1001.0, 1230, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], // customer: violations ---------------------------------------------------------------------- - 'c_v' => [null, null, null, null, 1230, null, null, null, null, null, 3600, null, '00:20', '00:39', '01:00', 'customer', '+3600 seconds'], - 'c_w' => [null, null, null, null, null, 1001.0, null, null, null, null, null, 1000.0, '€1,001.00', '€0.00', '€1,000.00', 'customer', '+3600 seconds'], + 'c_v' => [null, null, null, null, 1230, null, null, null, null, null, null, null, null, 3600, null, '00:20', '00:39', '01:00', 'customer', '+3600 seconds'], + 'c_w' => [null, null, null, null, null, 1001.0, null, null, null, null, null, null, null, null, 1000.0, '€1,001.00', '€0.00', '€1,000.00', 'customer', '+3600 seconds'], - // previously logged available budgets expected violation duration entry currently in database - 'c_f1' => [null, null, null, null, 1320, null, null, null, null, null, 3600, null, '00:22', '00:38', '01:00', 'customer', '+3600 seconds', ['rate' => 1.0, 'duration' => 1000]], - 'c_h1' => [null, null, null, null, 7200, null, null, null, null, null, 7200, null, '02:00', '00:00', '02:00', 'customer', '+3601 seconds', ['rate' => 1.0, 'duration' => 3600]], - 'c_h2' => [null, null, null, null, 3601, null, null, null, null, null, 3600, null, null, null, null, null, '+3600 seconds', ['rate' => 1.0, 'duration' => 3601]], - 'c_g0' => [null, null, null, null, null, 1002.0, null, null, null, null, null, 1000.0, '1,002.00', '0.00', '1,000.00', 'customer', '+3600 seconds', ['rate' => 1.0, 'duration' => 1010]], - 'c_g1' => [null, null, null, null, null, 1002.0, null, null, null, null, null, 1000.0, null, null, null, null, '+3600 seconds', ['rate' => 2.0, 'duration' => 0]], + // previously logged available budgets expected violation duration entry currently in database + 'c_f1' => [null, null, null, null, 1320, null, null, null, null, null, null, null, null, 3600, null, '00:22', '00:38', '01:00', 'customer', '+3600 seconds', ['rate' => 1.0, 'duration' => 1000]], + 'c_h1' => [null, null, null, null, 7200, null, null, null, null, null, null, null, null, 7200, null, '02:00', '00:00', '02:00', 'customer', '+3601 seconds', ['rate' => 1.0, 'duration' => 3600]], + 'c_h2' => [null, null, null, null, 3601, null, null, null, null, null, null, null, null, 3600, null, null, null, null, null, '+3600 seconds', ['rate' => 1.0, 'duration' => 3601]], + 'c_g0' => [null, null, null, null, null, 1002.0, null, null, null, null, null, null, null, null, 1000.0, '1,002.00', '0.00', '1,000.00', 'customer', '+3600 seconds', ['rate' => 1.0, 'duration' => 1010]], + 'c_g1' => [null, null, null, null, null, 1002.0, null, null, null, null, null, null, null, null, 1000.0, null, null, null, null, '+3600 seconds', ['rate' => 2.0, 'duration' => 0]], // customer: no violations - 'c_z' => [null, null, null, null, 1230, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'c_1' => [null, null, null, null, null, 1001.0, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'c_2' => [null, null, null, null, 1230, 1001.0, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'c_3' => [1230, null, 1230, null, 1230, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'c_4' => [1230, 1001.0, 1230, null, null, 1001.0, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'c_5' => [null, 1001.0, null, 1001.0, 1230, 1001.0, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'c_6' => [null, 1001.0, 1230, 1001.0, 1230, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], - 'c_7' => [1230, 1001.0, 1230, 1001.0, null, 1001.0, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'c_z' => [null, null, null, null, 1230, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'c_1' => [null, null, null, null, null, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'c_2' => [null, null, null, null, 1230, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'c_3' => [1230, null, 1230, null, 1230, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'c_4' => [1230, 1001.0, 1230, null, null, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'c_5' => [null, 1001.0, null, 1001.0, 1230, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'c_6' => [null, 1001.0, 1230, 1001.0, 1230, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], + 'c_7' => [1230, 1001.0, 1230, 1001.0, null, 1001.0, null, null, null, null, null, null, null, null, null, null, null, null, null, '+3600 seconds'], ]; } @@ -263,10 +269,13 @@ class TimesheetBudgetUsedValidatorTest extends ConstraintValidatorTestCase ?float $projectRate, ?int $customerDuration, ?float $customerRate, + ?string $activityBudgetType, ?int $activityTimeBudget, ?float $activityBudget, + ?string $projectBudgetType, ?int $projectTimeBudget, ?float $projectBudget, + ?string $customerBudgetType, ?int $customerTimeBudget, ?float $customerBudget, ?string $used, @@ -334,9 +343,19 @@ class TimesheetBudgetUsedValidatorTest extends ConstraintValidatorTestCase if (!\array_key_exists('duration', $rawData)) { $rawData['duration'] = 0; } + if (!\array_key_exists('begin', $rawData)) { + $rawData['begin'] = clone $begin; + } + if (!\array_key_exists('end', $rawData)) { + $rawData['end'] = clone $end; + } $activity = $this->createMock(Activity::class); $activity->method('getId')->willReturn($rawData['activity']); $activity->method('isMonthlyBudget')->willReturn(false); + if ($activityBudgetType !== null) { + $activity->method('getBudgetType')->willReturn($activityBudgetType); + $activity->method('isMonthlyBudget')->willReturn($activityBudgetType === 'month'); + } if ($activityTimeBudget !== null) { $activity->method('getTimeBudget')->willReturn($activityTimeBudget); $activity->method('hasTimeBudget')->willReturn(true); @@ -351,6 +370,10 @@ class TimesheetBudgetUsedValidatorTest extends ConstraintValidatorTestCase $customer = $this->createMock(Customer::class); $customer->method('getId')->willReturn($rawData['customer']); $customer->method('isMonthlyBudget')->willReturn(false); + if ($customerBudgetType !== null) { + $customer->method('getBudgetType')->willReturn($customerBudgetType); + $customer->method('isMonthlyBudget')->willReturn($customerBudgetType === 'month'); + } if ($customerTimeBudget !== null) { $customer->method('getTimeBudget')->willReturn($customerTimeBudget); $customer->method('hasTimeBudget')->willReturn(true); @@ -366,6 +389,10 @@ class TimesheetBudgetUsedValidatorTest extends ConstraintValidatorTestCase $project->method('getId')->willReturn($rawData['project']); $project->method('getCustomer')->willReturn($customer); $project->method('isMonthlyBudget')->willReturn(false); + if ($projectBudgetType !== null) { + $project->method('getBudgetType')->willReturn($projectBudgetType); + $project->method('isMonthlyBudget')->willReturn($projectBudgetType === 'month'); + } if ($projectTimeBudget !== null) { $project->method('getTimeBudget')->willReturn($projectTimeBudget); $project->method('hasTimeBudget')->willReturn(true); @@ -376,7 +403,6 @@ class TimesheetBudgetUsedValidatorTest extends ConstraintValidatorTestCase $project->method('hasBudget')->willReturn(true); $project->method('hasBudgets')->willReturn(true); } - $timesheet = $this->createMock(Timesheet::class); $timesheet->method('getId')->willReturn(1); $timesheet->method('getRate')->willReturn($rawData['rate']); diff --git a/translations/reporting.de.xlf b/translations/reporting.de.xlf index 15ed4421..a77feb02 100644 --- a/translations/reporting.de.xlf +++ b/translations/reporting.de.xlf @@ -46,6 +46,10 @@ reporting.initial_view Initialer Bericht + + report_customer_monthly_projects + Projekte nach Monat, Tätigkeit und Benutzer + diff --git a/translations/reporting.en.xlf b/translations/reporting.en.xlf index 0b796312..5262ed77 100644 --- a/translations/reporting.en.xlf +++ b/translations/reporting.en.xlf @@ -46,6 +46,10 @@ reporting.initial_view Initial report + + report_customer_monthly_projects + Projects by month, activity and user +