added new report: project view (#1738)

This commit is contained in:
Willian Gustavo Veiga
2021-03-09 19:19:11 -03:00
committed by GitHub
parent 0b7d551048
commit c34e9f576d
28 changed files with 892 additions and 83 deletions

View File

@@ -0,0 +1,61 @@
<?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\ProjectView;
use App\Form\Type\CustomerType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ProjectViewForm extends AbstractType
{
/**
* Simplify cross linking between pages by removing the block prefix.
*
* @return null|string
*/
public function getBlockPrefix()
{
return null;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('customer', CustomerType::class, [
'required' => false,
'label' => false,
'width' => false,
]);
$builder->add('includeNoBudget', CheckboxType::class, [
'required' => false,
'label' => 'label.includeNoBudget',
]);
$builder->add('includeNoWork', CheckboxType::class, [
'required' => false,
'label' => 'label.includeNoWork',
]);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => ProjectViewQuery::class,
'csrf_protection' => false,
'method' => 'GET',
]);
}
}

View File

@@ -0,0 +1,128 @@
<?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\ProjectView;
use App\Entity\Project;
final class ProjectViewModel
{
/**
* @var Project
*/
private $project;
/**
* @var int
*/
private $durationDay = 0;
/**
* @var int
*/
private $durationWeek = 0;
/**
* @var int
*/
private $durationMonth = 0;
/**
* @var int
*/
private $durationTotal = 0;
/**
* @var int
*/
private $rateTotal = 0;
/**
* @var int
*/
private $notExportedDuration = 0;
/**
* @var float
*/
private $notExportedRate = 0.00;
public function getProject(): ?Project
{
return $this->project;
}
public function setProject(Project $project): void
{
$this->project = $project;
}
public function getDurationDay(): int
{
return $this->durationDay;
}
public function setDurationDay(int $durationDay): void
{
$this->durationDay = $durationDay;
}
public function getDurationWeek(): int
{
return $this->durationWeek;
}
public function setDurationWeek(int $durationWeek): void
{
$this->durationWeek = $durationWeek;
}
public function getDurationMonth(): int
{
return $this->durationMonth;
}
public function setDurationMonth(int $durationMonth): void
{
$this->durationMonth = $durationMonth;
}
public function getDurationTotal(): int
{
return $this->durationTotal;
}
public function setDurationTotal(int $durationTotal): void
{
$this->durationTotal = $durationTotal;
}
public function getNotExportedDuration(): int
{
return $this->notExportedDuration;
}
public function setNotExportedDuration(int $notExportedDuration): void
{
$this->notExportedDuration = $notExportedDuration;
}
public function getNotExportedRate(): float
{
return $this->notExportedRate;
}
public function setNotExportedRate(float $notExportedRate): void
{
$this->notExportedRate = $notExportedRate;
}
public function getRateTotal(): int
{
return $this->rateTotal;
}
public function setRateTotal(int $rateTotal): void
{
$this->rateTotal = $rateTotal;
}
}

View File

@@ -0,0 +1,84 @@
<?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\ProjectView;
use App\Entity\Customer;
use App\Entity\User;
use DateTime;
final class ProjectViewQuery
{
/**
* @var Customer|null
*/
private $customer;
/**
* @var DateTime
*/
private $today;
/**
* @var User|null
*/
private $user;
/**
* @var bool
*/
private $includeNoBudget = false;
/**
* @var bool
*/
private $includeNoWork = false;
public function __construct(DateTime $today, User $user)
{
$this->today = $today;
$this->user = $user;
}
public function getUser(): ?User
{
return $this->user;
}
public function isIncludeNoBudget(): bool
{
return $this->includeNoBudget;
}
public function setIncludeNoBudget(bool $includeNoBudget): void
{
$this->includeNoBudget = $includeNoBudget;
}
public function isIncludeNoWork(): bool
{
return $this->includeNoWork;
}
public function setIncludeNoWork(bool $includeNoWork): void
{
$this->includeNoWork = $includeNoWork;
}
public function getCustomer(): ?Customer
{
return $this->customer;
}
public function setCustomer(Customer $customer): void
{
$this->customer = $customer;
}
public function getToday(): DateTime
{
return $this->today;
}
}

View File

@@ -0,0 +1,157 @@
<?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\ProjectView;
use App\Entity\Timesheet;
use App\Repository\ProjectRepository;
use App\Repository\TimesheetRepository;
use App\Timesheet\DateTimeFactory;
use Doctrine\DBAL\Types\Types;
use Exception;
final class ProjectViewService
{
private $repository;
private $timesheetRepository;
public function __construct(ProjectRepository $projectRepository, TimesheetRepository $timesheetRepository)
{
$this->repository = $projectRepository;
$this->timesheetRepository = $timesheetRepository;
}
/**
* @param ProjectViewQuery $query
* @return ProjectViewModel[]
* @throws Exception
*/
public function getProjectView(ProjectViewQuery $query): array
{
$factory = new DateTimeFactory($query->getToday()->getTimezone());
$user = $query->getUser();
$today = clone $query->getToday();
$begin = $factory->getStartOfWeek($today);
$end = $factory->getEndOfWeek($today);
$startMonth = (clone $begin)->modify('first day of this month');
$endMonth = (clone $begin)->modify('last day of this month');
$qb = $this->repository->createQueryBuilder('p');
$qb
->select('p AS project')
->addSelect('SUM(t.duration) AS totalDuration')
->addSelect('SUM(t.rate) AS totalRate')
->leftJoin('p.customer', 'c')
->leftJoin(Timesheet::class, 't', 'WITH', 'p.id = t.project')
->andWhere($qb->expr()->eq('p.visible', true))
->andWhere($qb->expr()->eq('c.visible', true))
->addGroupBy('p')
->addGroupBy('t.project')
;
if ($query->getCustomer() !== null) {
$qb->andWhere($qb->expr()->eq('c', ':customer'));
$qb->setParameter('customer', $query->getCustomer()->getId());
}
if (!$query->isIncludeNoWork()) {
$qb->andHaving($qb->expr()->gt('totalDuration', 0));
}
if (!$query->isIncludeNoBudget()) {
$qb->andWhere($qb->expr()->gt('p.timeBudget', 0));
}
$this->repository->addPermissionCriteria($qb, $user);
$result = $qb->getQuery()->getResult();
$projectViews = [];
foreach ($result as $res) {
$entity = new ProjectViewModel();
$entity->setProject($res['project']);
$entity->setDurationTotal($res['totalDuration'] ?? 0);
$entity->setRateTotal($res['totalRate'] ?? 0);
$projectViews[$entity->getProject()->getId()] = $entity;
}
$projectIds = array_keys($projectViews);
// values for today
$qb = $this->timesheetRepository->createQueryBuilder('t');
$qb
->select('IDENTITY(t.project) AS id, SUM(t.duration) AS duration')
->andWhere($qb->expr()->in('t.project', ':project'))
->andWhere('DATE(t.begin) = :starting_date')
->groupBy('t.project')
->setParameter('starting_date', $today->format('Y-m-d'))
->setParameter('project', array_values($projectIds))
;
$result = $qb->getQuery()->getScalarResult();
foreach ($result as $row) {
$projectViews[$row['id']]->setDurationDay($row['duration']);
}
// values for the current week
$qb = $this->timesheetRepository->createQueryBuilder('t');
$qb
->select('IDENTITY(t.project) AS id, SUM(t.duration) AS duration')
->andWhere($qb->expr()->in('t.project', ':project'))
->andWhere('DATE(t.begin) BETWEEN :start_date AND :end_date')
->groupBy('t.project')
->setParameter('start_date', $begin->format('Y-m-d'))
->setParameter('end_date', $end->format('Y-m-d'))
->setParameter('project', array_values($projectIds))
;
$result = $qb->getQuery()->getScalarResult();
foreach ($result as $row) {
$projectViews[$row['id']]->setDurationWeek($row['duration']);
}
// values for the current month
$qb = $this->timesheetRepository->createQueryBuilder('t');
$qb
->select('IDENTITY(t.project) AS id, SUM(t.duration) AS duration')
->andWhere($qb->expr()->in('t.project', ':project'))
->andWhere('DATE(t.begin) BETWEEN :start_month AND :end_month')
->groupBy('t.project')
->setParameter('start_month', $startMonth)
->setParameter('end_month', $endMonth)
->setParameter('project', array_values($projectIds))
;
$result = $qb->getQuery()->getScalarResult();
foreach ($result as $row) {
$projectViews[$row['id']]->setDurationMonth($row['duration']);
}
// values for the all time (not exported)
$qb = $this->timesheetRepository->createQueryBuilder('t');
$qb
->select('IDENTITY(t.project) AS id, SUM(t.duration) AS duration, SUM(t.rate) AS rate')
->andWhere($qb->expr()->in('t.project', ':project'))
->andWhere('t.exported = :exported')
->groupBy('t.project')
->setParameter('exported', false, Types::BOOLEAN)
->setParameter('project', array_values($projectIds))
;
$result = $qb->getQuery()->getScalarResult();
foreach ($result as $row) {
$projectViews[$row['id']]->setNotExportedDuration($row['duration']);
$projectViews[$row['id']]->setNotExportedRate($row['rate']);
}
return array_values($projectViews);
}
}