-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorklogRepository.php
More file actions
327 lines (279 loc) · 12.2 KB
/
WorklogRepository.php
File metadata and controls
327 lines (279 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
namespace App\Repository;
use App\Entity\InvoiceEntry;
use App\Entity\Issue;
use App\Entity\Project;
use App\Entity\Worklog;
use App\Enum\NonBillableEpicsEnum;
use App\Enum\NonBillableVersionsEnum;
use App\Model\Invoices\InvoiceEntryWorklogsFilterData;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Worklog>
*
* @method Worklog|null find($id, $lockMode = null, $lockVersion = null)
* @method Worklog|null findOneBy(array $criteria, array $orderBy = null)
* @method findAll()
* @method findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class WorklogRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Worklog::class);
}
public function save(Worklog $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Worklog $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function findByFilterData(Project $project, InvoiceEntry $invoiceEntry, InvoiceEntryWorklogsFilterData $filterData): iterable
{
$qb = $this->createQueryBuilder('worklog');
$qb->where('worklog.project = :project')->setParameter('project', $project);
if (isset($filterData->isBilled)) {
$qb->andWhere(
$filterData->isBilled
? 'worklog.isBilled = TRUE'
: 'worklog.isBilled = FALSE OR worklog.isBilled is NULL'
);
}
if (!empty($filterData->worker)) {
$qb->andWhere('worklog.worker LIKE :worker')->setParameter('worker', '%'.$filterData->worker.'%');
}
if (!empty($filterData->periodFrom)) {
$qb->andWhere('worklog.started >= :periodFrom')->setParameter('periodFrom', $filterData->periodFrom);
}
if (!empty($filterData->periodTo)) {
// Period to must include the selected day.
$periodTo = $filterData->periodTo->modify('tomorrow');
$qb->andWhere('worklog.started < :periodTo')->setParameter('periodTo', $periodTo);
}
if (!empty($filterData->version) || !empty($filterData->epic)) {
$qb->leftJoin(Issue::class, 'issue', 'WITH', 'issue.id = worklog.issue');
}
if (!empty($filterData->version)) {
$qb->andWhere(':version MEMBER OF issue.versions')
->setParameter('version', $filterData->version);
}
if (!empty($filterData->epic)) {
$qb->andWhere(':epic = issue.epicKey')
->setParameter('epic', $filterData->epic);
}
if (isset($filterData->onlyAvailable) && $filterData->onlyAvailable) {
$qb->andWhere($qb->expr()->orX(
$qb->expr()->isNull('worklog.invoiceEntry'),
$qb->expr()->eq('worklog.invoiceEntry', ':invoiceEntry')
))->setParameter('invoiceEntry', $invoiceEntry);
}
return $qb->getQuery()->execute();
}
public function findWorklogsByWorkerAndDateRange(string $workerIdentifier, \DateTime $dateFrom, \DateTime $dateTo)
{
$qb = $this->createQueryBuilder('worklog');
return $qb
->where($qb->expr()->between('worklog.started', ':dateFrom', ':dateTo'))
->andWhere('worklog.worker = :worker')
->setParameters([
'worker' => $workerIdentifier,
'dateFrom' => $dateFrom,
'dateTo' => $dateTo,
])
->getQuery()->getResult();
}
/**
* Gets the sum of time spent grouped by week of year for a specific worker within a range of weeks.
*
* @param string $workerEmail The email of the worker
* @param \DateTimeInterface $from The starting date time
* @param \DateTimeInterface $to The ending date time
* @param string $groupBy The function to group by, accepts 'week', 'month' and 'year'
*
* @return array An array of results containing total time spent, week number, and worker, indexed by week/month/year number
*/
public function getTimeSpentByWorkerInWeekRange(
string $workerEmail,
\DateTimeInterface $from,
\DateTimeInterface $to,
string $groupBy,
): array {
$qb = $this->createQueryBuilder('w');
if ('month' !== $groupBy && 'week' !== $groupBy && 'year' !== $groupBy) {
throw new \InvalidArgumentException('Invalid group by parameter, function accepts only "week", "month" or "year"');
}
$groupByFunction = 'week' === $groupBy ? 'WEEKOFYEAR' : \strtoupper($groupBy);
$dqlPart = sprintf('%s(w.started) as %s', $groupByFunction, $groupBy);
$qb
->select(
'SUM(w.timeSpentSeconds) as totalTimeSpent',
$dqlPart,
'w.worker'
)
->where('w.worker = :worker')
->andWhere('w.started >= :from')
->andWhere('w.started <= :to')
->setParameter('worker', $workerEmail)
->setParameter('from', $from)
->setParameter('to', $to)
->groupBy($groupBy)
->orderBy('w.started', 'ASC');
$results = $qb->getQuery()->getResult();
// Index results by week/month/year number
$indexedResults = [];
foreach ($results as $result) {
$indexedResults[$result[$groupBy]] = $result;
}
return $indexedResults;
}
/**
* Finds billable worklogs within a specific date range for a given worker.
*
* @param \DateTime $dateFrom the start date for the date range filter
* @param \DateTime $dateTo the end date for the date range filter
* @param string|null $workerIdentifier optional worker identifier to filter worklogs by worker
* @param mixed|null $isBilled optional indicator for whether the worklog has been billed or not
*
* @return array an array of worklogs matching the specified criteria
*/
public function findBillableWorklogsByWorkerAndDateRange(\DateTime $dateFrom, \DateTime $dateTo, ?string $workerIdentifier = null, mixed $isBilled = null): array
{
$nonBillableEpics = NonBillableEpicsEnum::getAsArray();
$nonBillableVersions = NonBillableVersionsEnum::getAsArray();
$qb = $this->createQueryBuilder('worklog');
$qb->leftJoin(Project::class, 'project', 'WITH', 'project.id = worklog.project')
->leftJoin('worklog.issue', 'issue')
->leftJoin('issue.epics', 'epic')
->leftJoin('issue.versions', 'version');
$qb->where($qb->expr()->between('worklog.started', ':dateFrom', ':dateTo'))
->andWhere($qb->expr()->andX(
$qb->expr()->eq('project.isBillable', '1')
))
->andWhere($qb->expr()->orX(
$qb->expr()->isNull('epic.title'),
$qb->expr()->notIn('epic.title', ':nonBillableEpics')
))
->andWhere($qb->expr()->orX(
$qb->expr()->isNull('version.name'),
$qb->expr()->notIn('version.name', ':nonBillableVersions')
))
->setParameter('dateFrom', $dateFrom)
->setParameter('dateTo', $dateTo)
->setParameter('nonBillableEpics', array_values($nonBillableEpics))
->setParameter('nonBillableVersions', array_values($nonBillableVersions));
if (null !== $isBilled) {
if ($isBilled) {
$qb->andWhere('worklog.isBilled = :isBilled');
} else {
$qb->andWhere(
$qb->expr()->orX(
$qb->expr()->eq('worklog.isBilled', ':isBilled'),
$qb->expr()->isNull('worklog.isBilled')
)
);
}
$qb->setParameter('isBilled', $isBilled);
}
if (null !== $workerIdentifier) {
$qb->andWhere('worklog.worker = :worker')
->setParameter('worker', $workerIdentifier);
}
return $qb->getQuery()->getResult();
}
public function findBilledWorklogsByWorkerAndDateRange(string $workerIdentifier, \DateTime $dateFrom, \DateTime $dateTo)
{
$nonBillableEpics = NonBillableEpicsEnum::getAsArray();
$nonBillableVersions = NonBillableVersionsEnum::getAsArray();
$qb = $this->createQueryBuilder('worklog');
$qb->leftJoin(Project::class, 'project', 'WITH', 'project.id = worklog.project')
->leftJoin('worklog.issue', 'issue')
->leftJoin('issue.epics', 'epic')
->leftJoin('issue.versions', 'version');
return $qb
->where($qb->expr()->between('worklog.started', ':dateFrom', ':dateTo'))
->andWhere('worklog.worker = :worker')
->andWhere($qb->expr()->andX(
$qb->expr()->eq('worklog.isBilled', '1'),
))
// notIn will only work if the string it is checked against is not null
->andWhere($qb->expr()->orX(
$qb->expr()->isNull('epic.title'),
$qb->expr()->notIn('epic.title', ':nonBillableEpics'),
))
->andWhere($qb->expr()->orX(
$qb->expr()->isNull('version.name'),
$qb->expr()->notIn('version.name', ':nonBillableVersions')
))
->setParameters([
'worker' => $workerIdentifier,
'dateFrom' => $dateFrom,
'dateTo' => $dateTo,
'nonBillableEpics' => array_values($nonBillableEpics),
'nonBillableVersions' => array_values($nonBillableVersions),
])
->getQuery()->getResult();
}
/**
* @throws \Exception
*/
public function getWorklogsAttachedToInvoiceInDateRange(\DateTimeInterface $periodStart, \DateTimeInterface $periodEnd, int $page = 1, int $pageSize = 50): array
{
$from = new \DateTimeImmutable($periodStart->format('Y-m-d').' 00:00:00');
$to = new \DateTimeImmutable($periodEnd->format('Y-m-d').' 23:59:59');
$query = $this->createQueryBuilder('worklog')
->leftJoin(Issue::class, 'issue', 'WITH', 'worklog.issue = issue.id')
->leftJoin(Project::class, 'project', 'WITH', 'issue.project = project.id')
->where('worklog.invoiceEntry IS NOT NULL')
->andWhere('worklog.started BETWEEN :from AND :to')
->setParameter('from', $from)
->setParameter('to', $to)
->getQuery()
->setFirstResult(($page - 1) * $pageSize)
->setMaxResults($pageSize);
$paginator = new Paginator($query, true);
$totalItemCount = count($paginator);
$pagesCount = ceil($totalItemCount / $pageSize);
return [
'total_count' => $totalItemCount,
'pages_count' => $pagesCount,
'current_page' => $page,
'page_size' => $pageSize,
'paginator' => $paginator,
];
}
/**
* Get worklogs for a given issue, optionally restricted by period.
*
* @param int $issueId
* @param ?\DateTimeInterface $fromDate
* @param ?\DateTimeInterface $toDate
*
* @return Worklog[]
*/
public function getWorklogsByIssueAndPeriod(int $issueId, ?\DateTimeInterface $fromDate, ?\DateTimeInterface $toDate): array
{
$qb = $this->createQueryBuilder('w')
->andWhere('w.issue = :issue') // <-- use the entity relation
->setParameter('issue', $issueId); // Doctrine can accept the ID for a ManyToOne
if ($fromDate) {
$qb->andWhere('w.started >= :fromDate')
->setParameter('fromDate', $fromDate->format('Y-m-d 00:00:00'));
}
if ($toDate) {
$qb->andWhere('w.started <= :toDate')
->setParameter('toDate', $toDate->format('Y-m-d 23:59:59'));
}
$qb->orderBy('w.started', 'ASC');
return $qb->getQuery()->getResult();
}
}