|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Form\Model\Calendar; |
|
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
|
8
|
|
|
|
|
9
|
|
|
class WorkdayRepository extends EntityRepository |
|
10
|
|
|
{ |
|
11
|
|
|
public function createCalendar(Calendar $calendar, Agreement $agreement) |
|
12
|
|
|
{ |
|
13
|
|
|
$date = $calendar->getStartDate(); |
|
14
|
|
|
$hours = $calendar->getTotalHours(); |
|
15
|
|
|
$nonSchoolDays = $this->getEntityManager()->getRepository('AppBundle:NonSchoolDay') |
|
16
|
|
|
->createQueryBuilder('n') |
|
17
|
|
|
->select('n.date') |
|
18
|
|
|
->getQuery()->getArrayResult(); |
|
19
|
|
|
|
|
20
|
|
|
$nonSchoolDays = array_map('current', $nonSchoolDays); |
|
21
|
|
|
|
|
22
|
|
|
$collection = new ArrayCollection(); |
|
23
|
|
|
|
|
24
|
|
|
while ($hours > 0) { |
|
25
|
|
|
if (false === in_array($date, $nonSchoolDays, false)) { |
|
26
|
|
|
$current = new Workday(); |
|
27
|
|
|
$current->setDate(clone $date); |
|
28
|
|
|
$current->setAgreement($agreement); |
|
29
|
|
|
switch ($date->format('w')) { |
|
30
|
|
|
case 1: |
|
31
|
|
|
$current->setHours(min($hours, $calendar->getHoursMon())); |
|
32
|
|
|
break; |
|
33
|
|
|
case 2: |
|
34
|
|
|
$current->setHours(min($hours, $calendar->getHoursTue())); |
|
35
|
|
|
break; |
|
36
|
|
|
case 3: |
|
37
|
|
|
$current->setHours(min($hours, $calendar->getHoursWed())); |
|
38
|
|
|
break; |
|
39
|
|
|
case 4: |
|
40
|
|
|
$current->setHours(min($hours, $calendar->getHoursThu())); |
|
41
|
|
|
break; |
|
42
|
|
|
case 5: |
|
43
|
|
|
$current->setHours(min($hours, $calendar->getHoursFri())); |
|
44
|
|
|
break; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$hours -= $current->getHours(); |
|
48
|
|
|
|
|
49
|
|
|
if ($current->getHours()) { |
|
50
|
|
|
$collection->add($current); |
|
51
|
|
|
$this->getEntityManager()->persist($current); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$date->add(new \DateInterval('P1D')); |
|
56
|
|
|
} |
|
57
|
|
|
return $collection; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|