Release 1.19.4 (#3255)

* login redirects to homepage if already being logged-in
* fix budget check for entries that were moved to another moth
* invoice: fix amount should be decimal if decimal template is used
* added new month grouped by project/activity/user report
This commit is contained in:
Kevin Papst
2022-04-18 12:41:45 +02:00
committed by GitHub
parent a179719c3e
commit bc908b4534
20 changed files with 729 additions and 56 deletions

View File

@@ -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"
*/

View File

@@ -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');

View File

@@ -0,0 +1,111 @@
<?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\Controller\Reporting;
use App\Controller\AbstractController;
use App\Export\Spreadsheet\Writer\BinaryFileResponseWriter;
use App\Export\Spreadsheet\Writer\XlsxWriter;
use App\Reporting\CustomerMonthlyProjects\CustomerMonthlyProjects;
use App\Reporting\CustomerMonthlyProjects\CustomerMonthlyProjectsForm;
use App\Repository\Query\UserQuery;
use App\Repository\UserRepository;
use App\Timesheet\TimesheetStatisticService;
use PhpOffice\PhpSpreadsheet\Reader\Html;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(path="/reporting/customer/monthly_projects")
* @Security("is_granted('view_reporting') and is_granted('view_other_reporting') and is_granted('view_other_timesheet')")
*/
final class CustomerMonthlyProjectsController extends AbstractController
{
/**
* @Route(path="/view", name="report_customer_monthly_projects", methods={"GET","POST"})
*/
public function report(Request $request, TimesheetStatisticService $statisticService, UserRepository $userRepository): Response
{
return $this->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,
];
}
}

View File

@@ -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();

View File

@@ -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()) {

View File

@@ -0,0 +1,16 @@
<?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\Reporting\CustomerMonthlyProjects;
use App\Reporting\AbstractUserList;
final class CustomerMonthlyProjects extends AbstractUserList
{
}

View File

@@ -0,0 +1,46 @@
<?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\Reporting\CustomerMonthlyProjects;
use App\Form\Type\MonthPickerType;
use App\Form\Type\ReportSumType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CustomerMonthlyProjectsForm extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->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',
]);
}
}

View File

@@ -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);
}

View File

@@ -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',

View File

@@ -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,
];
}
}

View File

@@ -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)

View File

@@ -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 %}
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="{{ 'display'|icon }}"></i> <span class="caret"></span>
</button>
<ul class="dropdown-menu checkbox-menu">
<li>
{{ form_widget(form.sumType) }}
</li>
</ul>
</div>
{% endif %}
<button class="btn btn-primary" formaction="{{ path(export_route) }}" type="submit"><i class="{{ 'download'|icon }}"></i></button>
{% 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() }}
<script type="text/javascript">
jQuery('#report-toolbar-form').on('change', function(ev) {
jQuery(this).submit();
});
</script>
{% endblock %}

View File

@@ -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 %}
<table class="table table-bordered dataTable">
<thead>
<tr>
<th rowspan="2" style="{{ rowspanStyle }}">{{ 'label.project'|trans }}</th>
{% for activity in stats.activities %}
<th colspan="3" class="text-center">{{ activity.name }}</th>
{% endfor %}
<th rowspan="2" style="{{ rowspanStyle }}">{{ dataTypeTitle|trans }}</th>
</tr>
<tr>
{% for activity in stats.activities %}
<th class="text-center">{{ 'label.user'|trans }}</th>
<th class="text-center">{{ dataTypeTitle|trans }}</th>
<th class="text-center">{{ 'sum.total'|trans }}</th>
{% endfor %}
</tr>
</thead>
{% set customer = null %}
{% set maxLength = (stats.activities|length) * 3 + 2 %}
<tbody>
{% 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'] %}
<tr class="summary">
<td colspan="{{ maxLength }}">{{ stats.projects[project.id]['customer'] }}</td>
</tr>
{% set customer = stats.projects[project.id]['customer_id'] %}
{% endif %}
{% for i in 1..rowspan %}
<tr>
{% if loop.first %}
<th{% if rowspan > 1 %} rowspan="{{ rowspan }}" style="{{ rowspanStyle }}"{% endif %}>{{ project.name }}</th>
{% 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) %}
<td>{{ user.0.name }}</td>
<td class="text-center"{% if dataTypeFormat is not null %} data-format="{{ dataTypeFormat }}"{% endif %}>
{% set value = user.0[dataType] %}
{% block user_activity %}
{% if dataType == 'rate' or dataType == 'internalRate' %}
{{ value|money(currency) }}
{% else %}
{{ value|duration(decimal) }}
{% endif %}
{% endblock %}
</td>
{% if loop.parent.loop.first %}
<td{% if rowspan > 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 %}
</td>
{% endif %}
{% else %}
<td class="text-center"></td>
<td class="text-center"></td>
{% if loop.parent.loop.first %}
<td{% if rowspan > 1 %} rowspan="{{ rowspan }}"{% endif %}></td>
{% endif %}
{% endif %}
{% endfor %}
{% if loop.first %}
<th{% if rowspan > 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 %}
</th>
{% endif %}
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>

View File

@@ -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 %}

View File

@@ -0,0 +1,103 @@
<?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\Tests\Controller\Reporting;
use App\Entity\Project;
use App\Entity\User;
use App\Tests\Controller\ControllerBaseTest;
use App\Tests\DataFixtures\ActivityFixtures;
use App\Tests\DataFixtures\CustomerFixtures;
use App\Tests\DataFixtures\ProjectFixtures;
use App\Tests\DataFixtures\TimesheetFixtures;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
/**
* @group integration
*/
class CustomerMonthlyProjectsControllerTest extends ControllerBaseTest
{
public function testReportIsSecure()
{
$this->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('<form method="get" class="form-inline form-reporting" id="report-toolbar-form">', $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'));
}
}

View File

@@ -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();

View File

@@ -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);
}
}

View File

@@ -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']);

View File

@@ -46,6 +46,10 @@
<source>reporting.initial_view</source>
<target>Initialer Bericht</target>
</trans-unit>
<trans-unit id="8RaUHlW" resname="report_customer_monthly_projects">
<source>report_customer_monthly_projects</source>
<target>Projekte nach Monat, Tätigkeit und Benutzer</target>
</trans-unit>
</body>
</file>
</xliff>

View File

@@ -46,6 +46,10 @@
<source>reporting.initial_view</source>
<target>Initial report</target>
</trans-unit>
<trans-unit id="8RaUHlW" resname="report_customer_monthly_projects">
<source>report_customer_monthly_projects</source>
<target>Projects by month, activity and user</target>
</trans-unit>
</body>
</file>
</xliff>