|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Form\Model\Calendar; |
|
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
7
|
|
|
use Doctrine\Common\Collections\Collection; |
|
8
|
|
|
use Doctrine\ORM\EntityRepository; |
|
9
|
|
|
|
|
10
|
|
|
class WorkdayRepository extends EntityRepository |
|
11
|
|
|
{ |
|
12
|
|
|
public function createCalendar(Calendar $calendar, Agreement $agreement) |
|
13
|
|
|
{ |
|
14
|
|
|
$date = $calendar->getStartDate(); |
|
15
|
|
|
$hours = $calendar->getTotalHours(); |
|
16
|
|
|
$nonSchoolDays = $this->getEntityManager()->getRepository('AppBundle:NonSchoolDay') |
|
17
|
|
|
->createQueryBuilder('n') |
|
18
|
|
|
->select('n.date') |
|
19
|
|
|
->getQuery()->getArrayResult(); |
|
20
|
|
|
|
|
21
|
|
|
$nonSchoolDays = array_map('current', $nonSchoolDays); |
|
22
|
|
|
|
|
23
|
|
|
$collection = new ArrayCollection(); |
|
24
|
|
|
|
|
25
|
|
|
while ($hours > 0) { |
|
26
|
|
|
if (false === in_array($date, $nonSchoolDays, false)) { |
|
27
|
|
|
$current = $this->getEntityManager()->getRepository('AppBundle:Workday')->findOneBy([ |
|
28
|
|
|
'date' => $date, |
|
29
|
|
|
'agreement' => $agreement |
|
30
|
|
|
]); |
|
31
|
|
|
if (null === $current) { |
|
32
|
|
|
$current = new Workday(); |
|
33
|
|
|
} |
|
34
|
|
|
$current->setDate(clone $date); |
|
35
|
|
|
$current->setAgreement($agreement); |
|
36
|
|
|
$assignedHours = 0; |
|
37
|
|
|
switch ($date->format('w')) { |
|
38
|
|
|
case 0: |
|
39
|
|
|
$assignedHours = min($hours, $calendar->getHoursSun()); |
|
40
|
|
|
break; |
|
41
|
|
|
case 1: |
|
42
|
|
|
$assignedHours = min($hours, $calendar->getHoursMon()); |
|
43
|
|
|
break; |
|
44
|
|
|
case 2: |
|
45
|
|
|
$assignedHours = min($hours, $calendar->getHoursTue()); |
|
46
|
|
|
break; |
|
47
|
|
|
case 3: |
|
48
|
|
|
$assignedHours = min($hours, $calendar->getHoursWed()); |
|
49
|
|
|
break; |
|
50
|
|
|
case 4: |
|
51
|
|
|
$assignedHours = min($hours, $calendar->getHoursThu()); |
|
52
|
|
|
break; |
|
53
|
|
|
case 5: |
|
54
|
|
|
$assignedHours = min($hours, $calendar->getHoursFri()); |
|
55
|
|
|
break; |
|
56
|
|
|
case 6: |
|
57
|
|
|
$assignedHours = min($hours, $calendar->getHoursSat()); |
|
58
|
|
|
break; |
|
59
|
|
|
} |
|
60
|
|
|
$current->setHours($current->getHours() + $assignedHours); |
|
61
|
|
|
|
|
62
|
|
|
$hours -= $assignedHours; |
|
63
|
|
|
|
|
64
|
|
|
if ($current->getHours()) { |
|
65
|
|
|
$collection->add($current); |
|
66
|
|
|
$this->getEntityManager()->persist($current); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$date->add(new \DateInterval('P1D')); |
|
71
|
|
|
} |
|
72
|
|
|
return $collection; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getArrayCalendar(Collection $workdays) |
|
76
|
|
|
{ |
|
77
|
|
|
if ($workdays->isEmpty()) { |
|
78
|
|
|
return []; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$calendar = []; |
|
82
|
|
|
|
|
83
|
|
|
/** @var Workday $current */ |
|
84
|
|
|
$currentWorkday = $workdays->first(); |
|
85
|
|
|
|
|
86
|
|
|
/** @var \DateTime $currentDate */ |
|
87
|
|
|
$currentDate = $currentWorkday->getDate(); |
|
88
|
|
|
|
|
89
|
|
|
$currentMonth = $currentDate->format('m'); |
|
90
|
|
|
$currentYear = $currentDate->format('Y'); |
|
91
|
|
|
$currentDate = new \DateTime(); |
|
92
|
|
|
$currentDate->setDate($currentYear, $currentMonth, 1); |
|
93
|
|
|
$currentDate->setTime(0, 0); |
|
94
|
|
|
$dayOfWeek = $currentDate->format('w'); |
|
95
|
|
|
$currentWeek = $currentDate->format('W'); |
|
96
|
|
|
$numFillDays = (7 + (int) $dayOfWeek - 1) % 7; |
|
97
|
|
|
|
|
98
|
|
|
$month = []; |
|
99
|
|
|
$week = []; |
|
100
|
|
|
while ($numFillDays--) { |
|
101
|
|
|
$week[] = ['day' => '']; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$count = 0; |
|
105
|
|
|
$locked = 0; |
|
106
|
|
|
|
|
107
|
|
|
/** @var \ArrayIterator $iterator */ |
|
108
|
|
|
$iterator = $workdays->getIterator(); |
|
109
|
|
|
|
|
110
|
|
|
/** @var \DateTime $targetDate */ |
|
111
|
|
|
while ($iterator->valid() && ($targetDate = $iterator->current()->getDate()) >= $currentDate) { |
|
112
|
|
|
while ($targetDate >= $currentDate) { |
|
113
|
|
|
if ($currentWeek != $currentDate->format('W') || $currentMonth != $currentDate->format('m')) { |
|
114
|
|
|
$month[(int) $currentWeek] = ['days' => $week, 'count' => $count, 'locked' => $locked]; |
|
115
|
|
|
$week = []; |
|
116
|
|
|
$count = 0; |
|
117
|
|
|
$locked = 0; |
|
118
|
|
|
if ($currentMonth != $currentDate->format('m')) { |
|
119
|
|
|
$calendar[((int) $currentYear * 12 + (int) $currentMonth - 1)] = $month; |
|
120
|
|
|
$dayOfWeek = $currentDate->format('w'); |
|
121
|
|
|
$numFillDays = (7 + (int) $dayOfWeek - 1) % 7; |
|
122
|
|
|
$month = []; |
|
123
|
|
|
while ($numFillDays--) { |
|
124
|
|
|
$week[] = ['day' => '']; |
|
125
|
|
|
} |
|
126
|
|
|
$currentMonth = $currentDate->format('m'); |
|
127
|
|
|
$currentYear = $currentDate->format('Y'); |
|
128
|
|
|
} |
|
129
|
|
|
$currentWeek = $currentDate->format('W'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($targetDate != $currentDate) { |
|
133
|
|
|
$week[] = ['day' => $currentDate->format('d')]; |
|
134
|
|
|
} |
|
135
|
|
|
$currentDate->add(new \DateInterval('P1D')); |
|
136
|
|
|
$currentDate->setTime(0, 0); |
|
137
|
|
|
} |
|
138
|
|
|
$week[] = ['day' => $targetDate->format('d'), 'data' => $iterator->current()]; |
|
139
|
|
|
$count++; |
|
140
|
|
|
if ($iterator->current()->isLocked()) { |
|
141
|
|
|
$locked++; |
|
142
|
|
|
} |
|
143
|
|
|
$iterator->next(); |
|
144
|
|
|
} |
|
145
|
|
|
$month[(int) $currentWeek] = ['days' => $week, 'count' => $count, 'locked' => $locked]; |
|
146
|
|
|
|
|
147
|
|
|
$calendar[((int) $currentYear * 12 + (int) $currentMonth - 1)] = $month; |
|
148
|
|
|
|
|
149
|
|
|
return $calendar; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
View Code Duplication |
public function getNext(Workday $workday) |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
|
|
$query = $this->createQueryBuilder('w') |
|
155
|
|
|
->where('w.agreement = :agreement') |
|
156
|
|
|
->andWhere('w.date > :date') |
|
157
|
|
|
->orderBy('w.date', 'ASC') |
|
158
|
|
|
->setParameter('date', $workday->getDate()) |
|
159
|
|
|
->setParameter('agreement', $workday->getAgreement()) |
|
160
|
|
|
->getQuery(); |
|
161
|
|
|
|
|
162
|
|
|
$result = $query->setMaxResults(1)->getResult(); |
|
163
|
|
|
|
|
164
|
|
|
return $result ? $result[0] : null; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
View Code Duplication |
public function getPrevious(Workday $workday) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
$query = $this->createQueryBuilder('w') |
|
170
|
|
|
->where('w.agreement = :agreement') |
|
171
|
|
|
->andWhere('w.date < :date') |
|
172
|
|
|
->orderBy('w.date', 'DESC') |
|
173
|
|
|
->setParameter('date', $workday->getDate()) |
|
174
|
|
|
->setParameter('agreement', $workday->getAgreement()) |
|
175
|
|
|
->getQuery(); |
|
176
|
|
|
|
|
177
|
|
|
$result = $query->setMaxResults(1)->getResult(); |
|
178
|
|
|
|
|
179
|
|
|
return $result ? $result[0] : null; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function getWorkdaysInRange(Agreement $agreement, \DateTime $start, \DateTime $end) |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->createQueryBuilder('w') |
|
185
|
|
|
->where('w.agreement = :agreement') |
|
186
|
|
|
->andWhere('w.date >= :start') |
|
187
|
|
|
->andWhere('w.date <= :end') |
|
188
|
|
|
->orderBy('w.date', 'DESC') |
|
189
|
|
|
->setParameter('agreement', $agreement) |
|
190
|
|
|
->setParameter('start', $start) |
|
191
|
|
|
->setParameter('end', $end) |
|
192
|
|
|
->getQuery() |
|
193
|
|
|
->getResult(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function getWorkdaysInWeek(Agreement $agreement, $week, $year) |
|
197
|
|
|
{ |
|
198
|
|
|
$date = new \DateTime(); |
|
199
|
|
|
$date->setTime(0, 0, 0); |
|
200
|
|
|
$start = $date->setISODate($year, $week); |
|
201
|
|
|
$end = clone $date; |
|
202
|
|
|
$end = $end->modify('+6 days'); |
|
203
|
|
|
|
|
204
|
|
|
return $this->getWorkdaysInRange($agreement, $start, $end); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function isWeekLocked(Agreement $agreement, $week, $year) |
|
208
|
|
|
{ |
|
209
|
|
|
$workdays = $this->getWorkdaysInWeek($agreement, $week, $year); |
|
210
|
|
|
|
|
211
|
|
|
$count = 0; |
|
212
|
|
|
$locked = 0; |
|
213
|
|
|
|
|
214
|
|
|
/** @var Workday $workday */ |
|
215
|
|
|
foreach ($workdays as $workday) { |
|
216
|
|
|
$count++; |
|
217
|
|
|
if ($workday->isLocked()) { |
|
218
|
|
|
$locked++; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
// la semana está bloqueada si hay días de trabajo y todos están bloqueados |
|
223
|
|
|
return $count && ($count === $locked); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function getWeekInformation(Workday $workday) |
|
227
|
|
|
{ |
|
228
|
|
|
$total = 0; |
|
229
|
|
|
$current = 0; |
|
230
|
|
|
|
|
231
|
|
|
$oldNumWeek = NAN; |
|
232
|
|
|
|
|
233
|
|
|
$workDays = $workday->getAgreement()->getWorkdays(); |
|
234
|
|
|
|
|
235
|
|
|
/** @var Workday $day */ |
|
236
|
|
|
foreach ($workDays as $day) { |
|
237
|
|
|
$numWeek = $day->getDate()->format('W'); |
|
238
|
|
|
|
|
239
|
|
|
if ($numWeek != $oldNumWeek) { |
|
240
|
|
|
$total++; |
|
241
|
|
|
$oldNumWeek = $numWeek; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
if ($workday->getDate() == $day->getDate()) { |
|
245
|
|
|
$current = $total; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return ['total' => $total, 'current' => $current]; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.