improvements

This commit is contained in:
Kevin Papst
2016-10-30 23:39:55 +01:00
parent 55ac11d517
commit 1678129fe9
47 changed files with 4390 additions and 1746 deletions

View File

@@ -17,6 +17,7 @@ use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
use TimesheetBundle\Model\ActivityStatistic;
/**
* Class ActivityRepository
@@ -26,17 +27,48 @@ use Pagerfanta\Pagerfanta;
class ActivityRepository extends EntityRepository
{
/**
* Return statistic data for all user.
*
* @return ActivityStatistic
*/
public function getGlobalStatistics()
{
$countAll = $this->getEntityManager()
->createQuery('SELECT COUNT(a.id) FROM TimesheetBundle:Activity a')
->getSingleScalarResult();
$stats = new ActivityStatistic();
$stats->setTotalAmount($countAll);
return $stats;
}
/**
* @param User $user
* @return Query
*/
public function queryLatest(User $user = null)
protected function queryLatest(User $user = null)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('a')
->from('TimesheetBundle:Activity', 'a')
->orderBy('a.name', 'DESC');
->orderBy('a.id', 'DESC');
return $qb->getQuery();
}
/**
* @param string $orderBy
* @return Query
*/
protected function queryAll($orderBy = 'id')
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('a')
->from('TimesheetBundle:Activity', 'a')
->orderBy('a.' . $orderBy, 'ASC');
return $qb->getQuery();
}

View File

@@ -17,6 +17,7 @@ use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
use TimesheetBundle\Model\ProjectStatistic;
/**
* Class ProjectRepository
@@ -26,11 +27,27 @@ use Pagerfanta\Pagerfanta;
class ProjectRepository extends EntityRepository
{
/**
* Return statistic data for all user.
*
* @return ProjectStatistic
*/
public function getGlobalStatistics()
{
$countAll = $this->getEntityManager()
->createQuery('SELECT COUNT(p.id) FROM TimesheetBundle:Project p')
->getSingleScalarResult();
$stats = new ProjectStatistic();
$stats->setTotalAmount($countAll);
return $stats;
}
/**
* @param User $user
* @return Query
*/
public function queryLatest(User $user = null)
protected function queryLatest(User $user = null)
{
$qb = $this->getEntityManager()->createQueryBuilder();
@@ -41,7 +58,11 @@ class ProjectRepository extends EntityRepository
return $qb->getQuery();
}
public function queryAll($orderBy = 'id')
/**
* @param string $orderBy
* @return Query
*/
protected function queryAll($orderBy = 'id')
{
$qb = $this->getEntityManager()->createQueryBuilder();

View File

@@ -15,8 +15,12 @@ use AppBundle\Entity\User;
use TimesheetBundle\Entity\Timesheet;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use Doctrine\DBAL\Types\Type;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
use TimesheetBundle\Model\TimesheetGlobalStatistic;
use TimesheetBundle\Model\TimesheetStatistic;
use DateTime;
/**
* Class TimesheetRepository
@@ -26,6 +30,122 @@ use Pagerfanta\Pagerfanta;
class TimesheetRepository extends EntityRepository
{
protected function queryThisMonth($select, User $user = null)
{
$qb = $this->getEntityManager()->createQueryBuilder();
// FIXME
$end = new DateTime();
$begin = $end->sub(new \DateInterval("P30D"));
$qb->select($select)
->from('TimesheetBundle:Timesheet', 't')
->where($qb->expr()->gt('t.begin', ':from'))
->andWhere($qb->expr()->gt('t.end', ':to'))
->setParameter('from', $begin, Type::DATETIME)
->setParameter('to', $end, Type::DATETIME);
if (null !== $user) {
$qb->andWhere('t.user = :user')
->setParameter('user', $user);
}
return $qb;
}
/**
* Fetch statistic data for one user.
*
* @param User $user
* @return TimesheetStatistic
*/
public function getUserStatistics(User $user)
{
$durationTotal = $this->getEntityManager()
->createQuery('SELECT SUM(t.duration) FROM TimesheetBundle:Timesheet t WHERE t.user = :user')
->setParameter('user', $user)
->getSingleScalarResult();
$rateTotal = $this->getEntityManager()
->createQuery('SELECT SUM(t.rate) FROM TimesheetBundle:Timesheet t WHERE t.user = :user')
->setParameter('user', $user)
->getSingleScalarResult();
$amountMonth = $this->queryThisMonth('SUM(t.rate)', $user)
->getQuery()
->getSingleScalarResult();
$durationMonth = $this->queryThisMonth('SUM(t.duration)', $user)
->getQuery()
->getSingleScalarResult();
$stats = new TimesheetStatistic();
$stats->setAmountTotal($rateTotal);
$stats->setDurationTotal($durationTotal);
$stats->setAmountThisMonth($amountMonth);
$stats->setDurationThisMonth($durationMonth);
return $stats;
}
/**
* Fetch statistic data for all user.
*
* @return TimesheetStatistic
*/
public function getGlobalStatistics()
{
$durationTotal = $this->getEntityManager()
->createQuery('SELECT SUM(t.duration) FROM TimesheetBundle:Timesheet t')
->getSingleScalarResult();
$rateTotal = $this->getEntityManager()
->createQuery('SELECT SUM(t.rate) FROM TimesheetBundle:Timesheet t')
->getSingleScalarResult();
$userTotal = $this->getEntityManager()
->createQuery('SELECT COUNT(DISTINCT(t.user)) FROM TimesheetBundle:Timesheet t')
->getSingleScalarResult();
$activeNow = $this->getActiveEntry();
$amountMonth = $this->queryThisMonth('SUM(t.rate)')
->getQuery()
->getSingleScalarResult();
$durationMonth = $this->queryThisMonth('SUM(t.duration)')
->getQuery()
->getSingleScalarResult();
$activeMonth = $this->queryThisMonth('COUNT(DISTINCT(t.user))')
->getQuery()
->getSingleScalarResult();
$stats = new TimesheetGlobalStatistic();
$stats->setAmountTotal($rateTotal);
$stats->setDurationTotal($durationTotal);
$stats->setActiveTotal($userTotal);
$stats->setActiveCurrently(count($activeNow));
$stats->setActiveThisMonth($activeMonth);
$stats->setAmountThisMonth($amountMonth);
$stats->setDurationThisMonth($durationMonth);
return $stats;
}
/**
* @param User $user
* @return Query
*/
public function getActiveEntry(User $user = null)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('t')
->from('TimesheetBundle:Timesheet', 't')
->where('t.begin > 0')
->andWhere('t.end is NULL');
$params = [];
if (null !== $user) {
$qb->andWhere('t.user = :user');
$params['user'] = $user;
}
return $qb->getQuery()->execute($params);
}
/**
* @param User $user
* @return Query
@@ -36,7 +156,7 @@ class TimesheetRepository extends EntityRepository
$qb->select('t')
->from('TimesheetBundle:Timesheet', 't')
->orderBy('t.start', 'DESC');
->orderBy('t.begin', 'DESC');
if (null !== $user) {
$qb->where('t.user = :user')